|
1
|
|
|
<?php |
|
2
|
|
|
if ( ! defined( 'ABSPATH' ) ) { |
|
3
|
|
|
die( 'You are not allowed to call this page directly.' ); |
|
4
|
|
|
} |
|
5
|
|
|
|
|
6
|
|
|
class FrmFormTemplateApi extends FrmFormApi { |
|
7
|
|
|
|
|
8
|
|
|
protected static $code_option_name = 'frm_free_license_code'; |
|
9
|
|
|
|
|
10
|
|
|
private static $base_api_url = 'https://formidableforms.com/wp-json/form-templates/v1/'; |
|
11
|
|
|
|
|
12
|
|
|
protected $free_license; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @since 3.06 |
|
16
|
|
|
*/ |
|
17
|
|
|
protected function set_cache_key() { |
|
18
|
|
|
$this->cache_key = 'frm_form_templates_l' . ( empty( $this->license ) ? '' : md5( $this->license ) ); |
|
19
|
|
|
} |
|
20
|
|
|
|
|
21
|
|
|
/** |
|
22
|
|
|
* @since 3.06 |
|
23
|
|
|
*/ |
|
24
|
|
|
protected function api_url() { |
|
25
|
|
|
$url = self::$base_api_url . 'list'; |
|
26
|
|
|
|
|
27
|
|
|
if ( empty( $this->license ) ) { |
|
28
|
|
|
$free_license = $this->get_free_license(); |
|
29
|
|
|
|
|
30
|
|
|
if ( $free_license ) { |
|
31
|
|
|
$url .= '?l=' . urlencode( base64_encode( $free_license ) ); |
|
32
|
|
|
$url .= '&v=' . FrmAppHelper::plugin_version(); |
|
33
|
|
|
} |
|
34
|
|
|
} |
|
35
|
|
|
|
|
36
|
|
|
return $url; |
|
37
|
|
|
} |
|
38
|
|
|
|
|
39
|
|
|
/** |
|
40
|
|
|
* @since 3.06 |
|
41
|
|
|
*/ |
|
42
|
|
|
protected function skip_categories() { |
|
43
|
|
|
return array(); |
|
44
|
|
|
} |
|
45
|
|
|
|
|
46
|
|
|
/** |
|
47
|
|
|
* @return string |
|
48
|
|
|
*/ |
|
49
|
|
|
public function get_free_license() { |
|
50
|
|
|
if ( ! isset( $this->free_license ) ) { |
|
51
|
|
|
$this->free_license = get_option( self::$code_option_name ); |
|
52
|
|
|
} |
|
53
|
|
|
|
|
54
|
|
|
return $this->free_license; |
|
55
|
|
|
} |
|
56
|
|
|
|
|
57
|
|
|
/** |
|
58
|
|
|
* @param string $code the code from the email sent for the API |
|
59
|
|
|
*/ |
|
60
|
|
|
private static function verify_code( $code ) { |
|
61
|
|
|
$base64_code = base64_encode( $code ); |
|
62
|
|
|
$api_url = self::$base_api_url . 'code?l=' . $base64_code; |
|
63
|
|
|
$response = wp_remote_get( $api_url ); |
|
64
|
|
|
|
|
65
|
|
|
self::handle_verify_response_errors_if_any( $response ); |
|
66
|
|
|
|
|
67
|
|
|
$decoded = json_decode( $response['body'] ); |
|
68
|
|
|
$successful = ! empty( $decoded->response ); |
|
69
|
|
|
|
|
70
|
|
|
if ( $successful ) { |
|
71
|
|
|
self::on_api_verify_code_success( $base64_code ); |
|
72
|
|
|
} else { |
|
73
|
|
|
wp_send_json_error( new WP_Error( $decoded->code, $decoded->message ) ); |
|
74
|
|
|
} |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
/** |
|
78
|
|
|
* @param array $response |
|
79
|
|
|
*/ |
|
80
|
|
|
private static function handle_verify_response_errors_if_any( $response ) { |
|
81
|
|
|
if ( is_wp_error( $response ) ) { |
|
82
|
|
|
wp_send_json_error( $response ); |
|
83
|
|
|
} |
|
84
|
|
|
|
|
85
|
|
|
if ( ! is_array( $response ) ) { |
|
86
|
|
|
wp_send_json_error(); |
|
87
|
|
|
} |
|
88
|
|
|
} |
|
89
|
|
|
|
|
90
|
|
|
/** |
|
91
|
|
|
* @param string $code the base64 encoded code |
|
92
|
|
|
*/ |
|
93
|
|
|
private static function on_api_verify_code_success( $code ) { |
|
94
|
|
|
update_option( self::$code_option_name, $code ); |
|
95
|
|
|
|
|
96
|
|
|
$data = array(); |
|
97
|
|
|
$key = FrmAppHelper::get_param( 'key', '', 'post', 'sanitize_key' ); |
|
98
|
|
|
|
|
99
|
|
|
if ( $key ) { |
|
100
|
|
|
$api = new self(); |
|
101
|
|
|
$templates = $api->get_api_info(); |
|
102
|
|
|
|
|
103
|
|
|
foreach ( $templates as $template ) { |
|
104
|
|
|
if ( $key === $template['key'] ) { |
|
105
|
|
|
$data['url'] = $template['url']; |
|
106
|
|
|
break; |
|
107
|
|
|
} |
|
108
|
|
|
} |
|
109
|
|
|
} |
|
110
|
|
|
|
|
111
|
|
|
wp_send_json_success( $data ); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
/** |
|
115
|
|
|
* AJAX Hook for signing free users up for a template API key |
|
116
|
|
|
*/ |
|
117
|
|
|
public static function signup() { |
|
118
|
|
|
$code = FrmAppHelper::get_param( 'code', '', 'post' ); |
|
119
|
|
|
|
|
120
|
|
|
if ( ! $code ) { |
|
121
|
|
|
wp_send_json_error(); |
|
122
|
|
|
} |
|
123
|
|
|
|
|
124
|
|
|
self::verify_code( $code ); |
|
125
|
|
|
} |
|
126
|
|
|
} |
|
127
|
|
|
|