|
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 static $free_license; |
|
13
|
|
|
|
|
14
|
|
|
/** |
|
15
|
|
|
* @since 3.06 |
|
16
|
|
|
*/ |
|
17
|
|
|
protected function set_cache_key() { |
|
18
|
|
|
$this->cache_key = 'frm_form_templates_l'; |
|
19
|
|
|
|
|
20
|
|
|
if ( ! empty( $this->license ) ) { |
|
21
|
|
|
$this->cache_key .= md5( $this->license ); |
|
22
|
|
|
} else { |
|
23
|
|
|
$free_license = $this->get_free_license(); |
|
24
|
|
|
if ( $free_license ) { |
|
25
|
|
|
$this->cache_key .= md5( $free_license ); |
|
26
|
|
|
} |
|
27
|
|
|
} |
|
28
|
|
|
} |
|
29
|
|
|
|
|
30
|
|
|
/** |
|
31
|
|
|
* @since 3.06 |
|
32
|
|
|
*/ |
|
33
|
|
|
protected function api_url() { |
|
34
|
|
|
$url = self::$base_api_url . 'list'; |
|
35
|
|
|
|
|
36
|
|
|
if ( empty( $this->license ) ) { |
|
37
|
|
|
$free_license = $this->get_free_license(); |
|
38
|
|
|
|
|
39
|
|
|
if ( $free_license ) { |
|
40
|
|
|
$url .= '?l=' . urlencode( base64_encode( $free_license ) ); |
|
41
|
|
|
$url .= '&v=' . FrmAppHelper::plugin_version(); |
|
42
|
|
|
} |
|
43
|
|
|
} |
|
44
|
|
|
|
|
45
|
|
|
return $url; |
|
46
|
|
|
} |
|
47
|
|
|
|
|
48
|
|
|
/** |
|
49
|
|
|
* @since 3.06 |
|
50
|
|
|
*/ |
|
51
|
|
|
protected function skip_categories() { |
|
52
|
|
|
return array(); |
|
53
|
|
|
} |
|
54
|
|
|
|
|
55
|
|
|
/** |
|
56
|
|
|
* @return string |
|
57
|
|
|
*/ |
|
58
|
|
|
public function get_free_license() { |
|
59
|
|
|
if ( ! isset( self::$free_license ) ) { |
|
60
|
|
|
self::$free_license = get_option( self::$code_option_name ); |
|
61
|
|
|
} |
|
62
|
|
|
|
|
63
|
|
|
return self::$free_license; |
|
64
|
|
|
} |
|
65
|
|
|
|
|
66
|
|
|
/** |
|
67
|
|
|
* Check to make sure the free code is being used. |
|
68
|
|
|
* |
|
69
|
|
|
* @since 4.09.02 |
|
70
|
|
|
*/ |
|
71
|
|
|
public function has_free_access() { |
|
72
|
|
|
$free_access = $this->get_free_license(); |
|
73
|
|
|
if ( ! $free_access ) { |
|
74
|
|
|
return false; |
|
75
|
|
|
} |
|
76
|
|
|
|
|
77
|
|
|
$templates = $this->get_api_info(); |
|
78
|
|
|
$contact_form = 20872734; |
|
79
|
|
|
return isset( $templates[ $contact_form ] ) && ! empty( $templates[ $contact_form ]['url'] ); |
|
80
|
|
|
} |
|
81
|
|
|
|
|
82
|
|
|
/** |
|
83
|
|
|
* @param string $code the code from the email sent for the API |
|
84
|
|
|
*/ |
|
85
|
|
|
private static function verify_code( $code ) { |
|
86
|
|
|
$base64_code = base64_encode( $code ); |
|
87
|
|
|
$api_url = self::$base_api_url . 'code?l=' . urlencode( $base64_code ); |
|
88
|
|
|
$response = wp_remote_get( $api_url ); |
|
89
|
|
|
|
|
90
|
|
|
self::handle_verify_response_errors_if_any( $response ); |
|
91
|
|
|
|
|
92
|
|
|
$decoded = json_decode( $response['body'] ); |
|
93
|
|
|
$successful = ! empty( $decoded->response ); |
|
94
|
|
|
|
|
95
|
|
|
if ( $successful ) { |
|
96
|
|
|
self::on_api_verify_code_success( $code ); |
|
97
|
|
|
} else { |
|
98
|
|
|
wp_send_json_error( new WP_Error( $decoded->code, $decoded->message ) ); |
|
99
|
|
|
} |
|
100
|
|
|
} |
|
101
|
|
|
|
|
102
|
|
|
private static function clear_template_cache_before_getting_free_templates() { |
|
103
|
|
|
delete_option( 'frm_form_templates_l' ); |
|
104
|
|
|
} |
|
105
|
|
|
|
|
106
|
|
|
/** |
|
107
|
|
|
* @param array $response |
|
108
|
|
|
*/ |
|
109
|
|
|
private static function handle_verify_response_errors_if_any( $response ) { |
|
110
|
|
|
if ( is_wp_error( $response ) ) { |
|
111
|
|
|
wp_send_json_error( $response ); |
|
112
|
|
|
} |
|
113
|
|
|
|
|
114
|
|
|
if ( ! is_array( $response ) ) { |
|
115
|
|
|
wp_send_json_error(); |
|
116
|
|
|
} |
|
117
|
|
|
} |
|
118
|
|
|
|
|
119
|
|
|
/** |
|
120
|
|
|
* @param string $code the base64 encoded code |
|
121
|
|
|
*/ |
|
122
|
|
|
private static function on_api_verify_code_success( $code ) { |
|
123
|
|
|
self::$free_license = $code; |
|
124
|
|
|
update_option( self::$code_option_name, $code, 'no' ); |
|
125
|
|
|
|
|
126
|
|
|
$data = array(); |
|
127
|
|
|
$key = FrmAppHelper::get_param( 'key', '', 'post', 'sanitize_key' ); |
|
128
|
|
|
|
|
129
|
|
|
if ( $key ) { |
|
130
|
|
|
self::clear_template_cache_before_getting_free_templates(); |
|
131
|
|
|
|
|
132
|
|
|
$data['urlByKey'] = array(); |
|
133
|
|
|
$api = new self(); |
|
134
|
|
|
$templates = $api->get_api_info(); |
|
135
|
|
|
|
|
136
|
|
|
foreach ( $templates as $template ) { |
|
137
|
|
|
if ( ! isset( $template['url'] ) || ! in_array( 'free', $template['categories'], true ) ) { |
|
138
|
|
|
continue; |
|
139
|
|
|
} |
|
140
|
|
|
|
|
141
|
|
|
$data['urlByKey'][ $template['key'] ] = $template['url']; |
|
142
|
|
|
} |
|
143
|
|
|
|
|
144
|
|
|
if ( ! isset( $data['urlByKey'][ $key ] ) ) { |
|
145
|
|
|
$error = new WP_Error( 400, 'We were unable to retrieve the template' ); |
|
146
|
|
|
wp_send_json_error( $error ); |
|
147
|
|
|
} |
|
148
|
|
|
|
|
149
|
|
|
$data['url'] = $data['urlByKey'][ $key ]; |
|
150
|
|
|
} |
|
151
|
|
|
|
|
152
|
|
|
wp_send_json_success( $data ); |
|
153
|
|
|
} |
|
154
|
|
|
|
|
155
|
|
|
/** |
|
156
|
|
|
* AJAX Hook for signing free users up for a template API key |
|
157
|
|
|
*/ |
|
158
|
|
|
public static function signup() { |
|
159
|
|
|
$code = FrmAppHelper::get_param( 'code', '', 'post' ); |
|
160
|
|
|
|
|
161
|
|
|
if ( ! $code ) { |
|
162
|
|
|
wp_send_json_error(); |
|
163
|
|
|
} |
|
164
|
|
|
|
|
165
|
|
|
self::verify_code( $code ); |
|
166
|
|
|
} |
|
167
|
|
|
} |
|
168
|
|
|
|