1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
class GV_License_Handler { |
|
|
|
|
4
|
|
|
|
5
|
|
|
/** |
6
|
|
|
* @var GravityView_Settings |
7
|
|
|
*/ |
8
|
|
|
private $Addon; |
9
|
|
|
|
10
|
|
|
const name = 'GravityView'; |
11
|
|
|
|
12
|
|
|
const author = 'Katz Web Services, Inc.'; |
13
|
|
|
|
14
|
|
|
const url = 'https://gravityview.co'; |
15
|
|
|
|
16
|
|
|
const version = GravityView_Plugin::version; |
17
|
|
|
|
18
|
|
|
/** |
19
|
|
|
* Post ID on gravityview.co |
20
|
|
|
* @since 1.15 |
21
|
|
|
*/ |
22
|
|
|
const item_id = 17; |
23
|
|
|
|
24
|
|
|
private $EDD_SL_Plugin_Updater; |
25
|
|
|
|
26
|
|
|
/** |
27
|
|
|
* @var GV_License_Handler |
28
|
|
|
*/ |
29
|
|
|
public static $instance; |
30
|
|
|
|
31
|
|
|
/** |
32
|
|
|
* @param GravityView_Settings $GFAddOn |
33
|
|
|
* |
34
|
|
|
* @return GV_License_Handler |
35
|
|
|
*/ |
36
|
|
|
public static function get_instance( GravityView_Settings $GFAddOn ) { |
37
|
|
|
if( empty( self::$instance ) ) { |
38
|
|
|
self::$instance = new self( $GFAddOn ); |
39
|
|
|
} |
40
|
|
|
return self::$instance; |
41
|
|
|
} |
42
|
|
|
|
43
|
|
|
private function __construct( GravityView_Settings $GFAddOn ) { |
44
|
|
|
|
45
|
|
|
$this->Addon = $GFAddOn; |
46
|
|
|
|
47
|
|
|
$this->setup_edd(); |
48
|
|
|
|
49
|
|
|
$this->add_hooks(); |
50
|
|
|
} |
51
|
|
|
|
52
|
|
|
private function add_hooks() { |
53
|
|
|
add_action( 'wp_ajax_gravityview_license', array( $this, 'license_call' ) ); |
54
|
|
|
} |
55
|
|
|
|
56
|
|
|
function settings_edd_license_activation( $field, $echo ) { |
|
|
|
|
57
|
|
|
|
58
|
|
|
wp_enqueue_script( 'gv-admin-edd-license', GRAVITYVIEW_URL . 'assets/js/admin-edd-license.js', array( 'jquery' ) ); |
59
|
|
|
|
60
|
|
|
$status = trim( $this->Addon->get_app_setting( 'license_key_status' ) ); |
61
|
|
|
$key = trim( $this->Addon->get_app_setting( 'license_key' ) ); |
62
|
|
|
|
63
|
|
|
if( !empty( $key ) ) { |
|
|
|
|
64
|
|
|
$response = $this->Addon->get_app_setting( 'license_key_response' ); |
65
|
|
|
$response = is_array( $response ) ? (object) $response : json_decode( $response ); |
66
|
|
|
} else { |
67
|
|
|
$response = array(); |
68
|
|
|
} |
69
|
|
|
|
70
|
|
|
wp_localize_script( 'gv-admin-edd-license', 'GVGlobals', array( |
71
|
|
|
'license_box' => $this->get_license_message( $response ) |
72
|
|
|
)); |
73
|
|
|
|
|
|
|
|
74
|
|
|
|
75
|
|
|
$fields = array( |
76
|
|
|
array( |
77
|
|
|
'name' => 'edd-activate', |
78
|
|
|
'value' => __('Activate License', 'gravityview'), |
|
|
|
|
79
|
|
|
'data-pending_text' => __('Verifying license…', 'gravityview'), |
|
|
|
|
80
|
|
|
'data-edd_action' => 'activate_license', |
81
|
|
|
'class' => 'button-primary', |
82
|
|
|
), |
83
|
|
|
array( |
84
|
|
|
'name' => 'edd-deactivate', |
85
|
|
|
'value' => __('Deactivate License', 'gravityview'), |
|
|
|
|
86
|
|
|
'data-pending_text' => __('Deactivating license…', 'gravityview'), |
|
|
|
|
87
|
|
|
'data-edd_action' => 'deactivate_license', |
88
|
|
|
'class' => ( empty( $status ) ? 'button-primary hide' : 'button-primary' ), |
89
|
|
|
), |
90
|
|
|
array( |
91
|
|
|
'name' => 'edd-check', |
92
|
|
|
'value' => __('Check License', 'gravityview'), |
|
|
|
|
93
|
|
|
'data-pending_text' => __('Verifying license…', 'gravityview'), |
|
|
|
|
94
|
|
|
'title' => 'Check the license before saving it', |
95
|
|
|
'data-edd_action' => 'check_license', |
96
|
|
|
'class' => 'button-secondary', |
97
|
|
|
), |
98
|
|
|
); |
99
|
|
|
|
|
|
|
|
100
|
|
|
|
101
|
|
|
$class = 'button gv-edd-action'; |
102
|
|
|
|
103
|
|
|
$class .= ( !empty( $key ) && $status !== 'valid' ) ? '' : ' hide'; |
|
|
|
|
104
|
|
|
|
105
|
|
|
$disabled_attribute = GVCommon::has_cap( 'gravityview_edit_settings' ) ? false : 'disabled'; |
106
|
|
|
|
107
|
|
|
$submit = '<div class="gv-edd-button-wrapper">'; |
108
|
|
|
foreach ( $fields as $field ) { |
109
|
|
|
$field['type'] = 'button'; |
110
|
|
|
$field['class'] = isset( $field['class'] ) ? $field['class'] . ' '. $class : $class; |
111
|
|
|
$field['style'] = 'margin-left: 10px;'; |
112
|
|
|
if( $disabled_attribute ) { |
113
|
|
|
$field['disabled'] = $disabled_attribute; |
114
|
|
|
} |
115
|
|
|
$submit .= $this->Addon->settings_submit( $field, $echo ); |
116
|
|
|
} |
117
|
|
|
$submit .= '</div>'; |
118
|
|
|
|
119
|
|
|
return $submit; |
120
|
|
|
} |
121
|
|
|
|
122
|
|
|
/** |
123
|
|
|
* Include the EDD plugin updater class, if not exists |
124
|
|
|
* @since 1.7.4 |
125
|
|
|
* @return void |
126
|
|
|
*/ |
127
|
|
|
private function setup_edd() { |
128
|
|
|
|
129
|
|
|
if( !class_exists('EDD_SL_Plugin_Updater') ) { |
|
|
|
|
130
|
|
|
require_once( GRAVITYVIEW_DIR . 'includes/lib/EDD_SL_Plugin_Updater.php'); |
|
|
|
|
131
|
|
|
} |
132
|
|
|
|
133
|
|
|
// setup the updater |
134
|
|
|
$this->EDD_SL_Plugin_Updater = new EDD_SL_Plugin_Updater( |
135
|
|
|
self::url, |
136
|
|
|
GRAVITYVIEW_FILE, |
137
|
|
|
$this->_get_edd_settings() |
138
|
|
|
); |
139
|
|
|
|
140
|
|
|
} |
141
|
|
|
|
142
|
|
|
/** |
143
|
|
|
* Generate the array of settings passed to the EDD license call |
144
|
|
|
* |
145
|
|
|
* @since 1.7.4 |
146
|
|
|
* |
147
|
|
|
* @param string $action The action to send to edd, such as `check_license` |
148
|
|
|
* @param string $license The license key to have passed to EDD |
149
|
|
|
* |
150
|
|
|
* @return array |
151
|
|
|
*/ |
152
|
|
|
function _get_edd_settings( $action = '', $license = '' ) { |
|
|
|
|
153
|
|
|
|
154
|
|
|
// retrieve our license key from the DB |
155
|
|
|
$license_key = empty( $license ) ? trim( $this->Addon->get_app_setting( 'license_key' ) ) : $license; |
156
|
|
|
|
157
|
|
|
$settings = array( |
158
|
|
|
'version' => self::version, |
159
|
|
|
'license' => $license_key, |
160
|
|
|
'item_name' => self::name, |
161
|
|
|
'item_id' => self::item_id, |
162
|
|
|
'author' => self::author, |
163
|
|
|
'url' => home_url(), |
164
|
|
|
); |
165
|
|
|
|
166
|
|
|
if( !empty( $action ) ) { |
|
|
|
|
167
|
|
|
$settings['edd_action'] = esc_attr( $action ); |
168
|
|
|
} |
169
|
|
|
|
170
|
|
|
$settings = array_map( 'urlencode', $settings ); |
171
|
|
|
|
172
|
|
|
return $settings; |
173
|
|
|
} |
174
|
|
|
|
175
|
|
|
/** |
176
|
|
|
* Perform the call |
177
|
|
|
* @return array|WP_Error |
178
|
|
|
*/ |
179
|
|
|
private function _license_get_remote_response( $data, $license = '' ) { |
180
|
|
|
|
181
|
|
|
$api_params = $this->_get_edd_settings( $data['edd_action'], $license ); |
182
|
|
|
|
183
|
|
|
$url = add_query_arg( $api_params, self::url ); |
184
|
|
|
|
185
|
|
|
$response = wp_remote_get( $url, array( |
|
|
|
|
186
|
|
|
'timeout' => 15, |
187
|
|
|
'sslverify' => false, |
188
|
|
|
)); |
189
|
|
|
|
190
|
|
|
if ( is_wp_error( $response ) ) { |
191
|
|
|
return array(); |
192
|
|
|
} |
193
|
|
|
|
194
|
|
|
$license_data = json_decode( wp_remote_retrieve_body( $response ) ); |
195
|
|
|
|
196
|
|
|
// Not JSON |
197
|
|
|
if ( empty( $license_data ) ) { |
198
|
|
|
|
199
|
|
|
delete_transient( 'gravityview_' . esc_attr( $data['field_id'] ) . '_valid' ); |
200
|
|
|
|
201
|
|
|
// Change status |
202
|
|
|
return array(); |
203
|
|
|
} |
204
|
|
|
|
205
|
|
|
// Store the license key inside the data array |
206
|
|
|
$license_data->license_key = $license; |
207
|
|
|
|
208
|
|
|
return $license_data; |
209
|
|
|
} |
210
|
|
|
|
211
|
|
|
/** |
212
|
|
|
* Generate the status message displayed in the license field |
213
|
|
|
* |
214
|
|
|
* @since 1.7.4 |
215
|
|
|
* @param $license_data |
216
|
|
|
* |
217
|
|
|
* @return string |
218
|
|
|
*/ |
219
|
|
|
function get_license_message( $license_data ) { |
|
|
|
|
220
|
|
|
|
221
|
|
|
if( empty( $license_data ) ) { |
222
|
|
|
$class = 'hide'; |
223
|
|
|
$message = ''; |
224
|
|
|
} else { |
225
|
|
|
|
226
|
|
|
if( ! empty( $license_data->error ) ) { |
227
|
|
|
$class = 'error'; |
228
|
|
|
$string_key = $license_data->license; |
229
|
|
|
} else { |
230
|
|
|
$class = $license_data->license; |
231
|
|
|
$string_key = $license_data->license; |
232
|
|
|
} |
233
|
|
|
|
234
|
|
|
$message = sprintf( '<p><strong>%s: %s</strong></p>', $this->strings('status'), $this->strings( $string_key, $license_data ) ); |
|
|
|
|
235
|
|
|
} |
236
|
|
|
|
237
|
|
|
return $this->generate_license_box( $message, $class ); |
238
|
|
|
} |
239
|
|
|
|
240
|
|
|
/** |
241
|
|
|
* Generate the status message box HTML based on the current status |
242
|
|
|
* |
243
|
|
|
* @since 1.7.4 |
244
|
|
|
* @param $message |
245
|
|
|
* @param string $class |
246
|
|
|
* |
247
|
|
|
* @return string |
248
|
|
|
*/ |
249
|
|
|
private function generate_license_box( $message, $class = '' ) { |
250
|
|
|
|
251
|
|
|
$template = '<div id="gv-edd-status" class="gv-edd-message inline %s">%s</div>'; |
252
|
|
|
|
253
|
|
|
$output = sprintf( $template, esc_attr( $class ), $message ); |
254
|
|
|
|
255
|
|
|
return $output; |
256
|
|
|
} |
257
|
|
|
|
258
|
|
|
/** |
259
|
|
|
* Perform the call to EDD based on the AJAX call or passed data |
260
|
|
|
* |
261
|
|
|
* @since 1.7.4 |
262
|
|
|
* |
263
|
|
|
* @param array $array { |
264
|
|
|
* @type string $license The license key |
265
|
|
|
* @type string $edd_action The EDD action to perform, like `check_license` |
266
|
|
|
* @type string $field_id The ID of the field to check |
267
|
|
|
* @type boolean $update Whether to update plugin settings. Prevent updating the data by setting an `update` key to false |
268
|
|
|
* @type string $format If `object`, return the object of the license data. Else, return the JSON-encoded object |
269
|
|
|
* } |
270
|
|
|
* |
271
|
|
|
* @return mixed|string|void |
272
|
|
|
*/ |
273
|
|
|
public function license_call( $array = array() ) { |
274
|
|
|
|
275
|
|
|
$is_ajax = ( defined('DOING_AJAX') && DOING_AJAX ); |
|
|
|
|
276
|
|
|
$data = empty( $array ) ? $_POST['data'] : $array; |
|
|
|
|
277
|
|
|
$has_cap = GVCommon::has_cap( 'gravityview_edit_settings' ); |
278
|
|
|
|
279
|
|
|
if ( $is_ajax && empty( $data['license'] ) ) { |
280
|
|
|
die( - 1 ); |
|
|
|
|
281
|
|
|
} |
282
|
|
|
|
283
|
|
|
// If the user isn't allowed to edit settings, show an error message |
284
|
|
|
if( ! $has_cap ) { |
285
|
|
|
$license_data = new stdClass(); |
286
|
|
|
$license_data->error = 'capability'; |
287
|
|
|
$license_data->message = $this->get_license_message( $license_data ); |
288
|
|
|
$json = json_encode( $license_data ); |
289
|
|
|
} else { |
290
|
|
|
|
291
|
|
|
$license = esc_attr( rgget( 'license', $data ) ); |
292
|
|
|
$license_data = $this->_license_get_remote_response( $data, $license ); |
293
|
|
|
|
294
|
|
|
// Empty is returned when there's an error. |
295
|
|
|
if ( empty( $license_data ) ) { |
296
|
|
|
if ( $is_ajax ) { |
297
|
|
|
exit( json_encode( array() ) ); |
|
|
|
|
298
|
|
|
} else { // Non-ajax call |
299
|
|
|
return json_encode( array() ); |
300
|
|
|
} |
301
|
|
|
} |
302
|
|
|
|
303
|
|
|
$license_data->message = $this->get_license_message( $license_data ); |
304
|
|
|
|
305
|
|
|
$json = json_encode( $license_data ); |
306
|
|
|
|
307
|
|
|
$update_license = ( ! isset( $data['update'] ) || ! empty( $data['update'] ) ); |
308
|
|
|
|
309
|
|
|
$is_check_action_button = ( 'check_license' === $data['edd_action'] && defined( 'DOING_AJAX' ) && DOING_AJAX ); |
310
|
|
|
|
311
|
|
|
// Failed is the response from trying to de-activate a license and it didn't work. |
312
|
|
|
// This likely happened because people entered in a different key and clicked "Deactivate", |
313
|
|
|
// meaning to deactivate the original key. We don't want to save this response, since it is |
314
|
|
|
// most likely a mistake. |
315
|
|
|
if ( $license_data->license !== 'failed' && ! $is_check_action_button && $update_license ) { |
|
|
|
|
316
|
|
|
|
317
|
|
|
if ( ! empty( $data['field_id'] ) ) { |
318
|
|
|
set_transient( 'gravityview_' . esc_attr( $data['field_id'] ) . '_valid', $license_data, DAY_IN_SECONDS ); |
319
|
|
|
} |
320
|
|
|
|
321
|
|
|
$this->license_call_update_settings( $license_data, $data ); |
322
|
|
|
|
323
|
|
|
} |
324
|
|
|
} // End $has_cap |
325
|
|
|
|
326
|
|
|
if ( $is_ajax ) { |
327
|
|
|
exit( $json ); |
|
|
|
|
328
|
|
|
} else { // Non-ajax call |
329
|
|
|
return ( rgget('format', $data ) === 'object' ) ? $license_data : $json; |
|
|
|
|
330
|
|
|
} |
331
|
|
|
} |
332
|
|
|
|
333
|
|
|
/** |
334
|
|
|
* Update the license after fetching it |
335
|
|
|
* @param object $license_data |
336
|
|
|
* @return void |
337
|
|
|
*/ |
338
|
|
|
private function license_call_update_settings( $license_data, $data ) { |
339
|
|
|
|
340
|
|
|
// Update option with passed data license |
341
|
|
|
$settings = $this->Addon->get_app_settings(); |
342
|
|
|
|
343
|
|
|
$settings['license_key'] = $license_data->license_key = trim( $data['license'] ); |
344
|
|
|
$settings['license_key_status'] = $license_data->license; |
345
|
|
|
$settings['license_key_response'] = (array)$license_data; |
|
|
|
|
346
|
|
|
|
347
|
|
|
$this->Addon->update_app_settings( $settings ); |
348
|
|
|
} |
349
|
|
|
|
350
|
|
|
/** |
351
|
|
|
* URL to direct license renewal, or if license key is not set, then just the account page |
352
|
|
|
* @since 1.13.1 |
353
|
|
|
* @param object|null $license_data Object with license data |
354
|
|
|
* @return string Renewal or account URL |
355
|
|
|
*/ |
356
|
|
|
private function get_license_renewal_url( $license_data ) { |
357
|
|
|
$renew_license_url = ( ! empty( $license_data ) && !empty( $license_data->license_key ) ) ? sprintf( 'https://gravityview.co/checkout/?download_id=17&edd_license_key=%s&utm_source=admin_notice&utm_medium=admin&utm_content=expired&utm_campaign=Activation', $license_data->license_key ) : 'https://gravityview.co/account/'; |
|
|
|
|
358
|
|
|
return $renew_license_url; |
359
|
|
|
} |
360
|
|
|
|
361
|
|
|
/** |
362
|
|
|
* Override the text used in the Redux Framework EDD field extension |
363
|
|
|
* @param array|null $status Status to get. If empty, get all strings. |
364
|
|
|
* @param object|null $license_data Object with license data |
365
|
|
|
* @return array Modified array of content |
366
|
|
|
*/ |
367
|
|
|
public function strings( $status = NULL, $license_data = null ) { |
|
|
|
|
368
|
|
|
|
|
|
|
|
369
|
|
|
|
370
|
|
|
$strings = array( |
371
|
|
|
'status' => esc_html__('Status', 'gravityview'), |
|
|
|
|
372
|
|
|
'error' => esc_html__('There was an error processing the request.', 'gravityview'), |
|
|
|
|
373
|
|
|
'failed' => esc_html__('Could not deactivate the license. The license key you attempted to deactivate may not be active or valid.', 'gravityview'), |
|
|
|
|
374
|
|
|
'site_inactive' => esc_html__('The license key is valid, but it has not been activated for this site.', 'gravityview'), |
|
|
|
|
375
|
|
|
'no_activations_left' => esc_html__('Invalid: this license has reached its activation limit.', 'gravityview') . ' ' . sprintf( esc_html__('You can manage license activations %son your GravityView account page%s.', 'gravityview'), '<a href="https://gravityview.co/account/#licenses">', '</a>' ), |
|
|
|
|
376
|
|
|
'deactivated' => esc_html__('The license has been deactivated.', 'gravityview'), |
|
|
|
|
377
|
|
|
'valid' => esc_html__('The license key is valid and active.', 'gravityview'), |
|
|
|
|
378
|
|
|
'invalid' => esc_html__('The license key entered is invalid.', 'gravityview'), |
|
|
|
|
379
|
|
|
'missing' => esc_html__('The license key was not defined.', 'gravityview'), |
|
|
|
|
380
|
|
|
'revoked' => esc_html__('This license key has been revoked.', 'gravityview'), |
|
|
|
|
381
|
|
|
'expired' => sprintf( esc_html__('This license key has expired. %sRenew your license on the GravityView website%s to receive updates and support.', 'gravityview'), '<a href="'. esc_url( $this->get_license_renewal_url( $license_data ) ) .'">', '</a>' ), |
|
|
|
|
382
|
|
|
'capability' => esc_html__( 'You don\'t have the ability to edit plugin settings.', 'gravityview' ), |
383
|
|
|
|
384
|
|
|
'verifying_license' => esc_html__('Verifying license…', 'gravityview'), |
|
|
|
|
385
|
|
|
'activate_license' => esc_html__('Activate License', 'gravityview'), |
|
|
|
|
386
|
|
|
'deactivate_license' => esc_html__('Deactivate License', 'gravityview'), |
|
|
|
|
387
|
|
|
'check_license' => esc_html__('Verify License', 'gravityview'), |
|
|
|
|
388
|
|
|
); |
389
|
|
|
|
390
|
|
|
if( empty( $status ) ) { |
391
|
|
|
return $strings; |
392
|
|
|
} |
393
|
|
|
|
394
|
|
|
if( isset( $strings[ $status ] ) ) { |
395
|
|
|
return $strings[ $status ]; |
396
|
|
|
} |
397
|
|
|
|
398
|
|
|
return NULL; |
|
|
|
|
399
|
|
|
} |
400
|
|
|
|
401
|
|
|
public function validate_license_key( $value, $field ) { |
402
|
|
|
|
403
|
|
|
// No license? No status. |
404
|
|
|
if( empty( $value ) ) { |
405
|
|
|
return NULL; |
|
|
|
|
406
|
|
|
} |
407
|
|
|
|
408
|
|
|
$response = $this->license_call(array( |
409
|
|
|
'license' => $this->Addon->get_app_setting( 'license_key' ), |
410
|
|
|
'edd_action' => 'check_license', |
411
|
|
|
'field_id' => $field['name'], |
412
|
|
|
)); |
413
|
|
|
|
414
|
|
|
$response = is_string( $response ) ? json_decode( $response, true ) : $response; |
415
|
|
|
|
416
|
|
|
switch( $response['license'] ) { |
417
|
|
|
case 'valid': |
418
|
|
|
$return = true; |
419
|
|
|
break; |
420
|
|
|
case 'invalid': |
421
|
|
|
$return = false; |
422
|
|
|
//$this->Addon->set_field_error( $field, $response['message'] ); |
|
|
|
|
423
|
|
|
break; |
424
|
|
|
default: |
425
|
|
|
//$this->Addon->set_field_error( $field, $response['message'] ); |
|
|
|
|
426
|
|
|
$return = false; |
427
|
|
|
} |
428
|
|
|
|
429
|
|
|
return $return; |
430
|
|
|
} |
431
|
|
|
|
432
|
|
|
} |