PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.4.2
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.4.2
4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.10.1 3.10.10 3.10.11 3.10.12 3.10.13 3.10.14 3.10.15 3.10.2 3.10.3 All 149 releases
← All changes | vendor/codeinwp/themeisle-sdk/src/Modules/Licenser.php +170 -527 3.10.03.4.2 View file →
@@ -12,9 +12,8 @@
12 12 namespace ThemeisleSDK\Modules;
13 13
14 14 // Exit if accessed directly.
15 15 use ThemeisleSDK\Common\Abstract_Module;
16 -use ThemeisleSDK\Loader;
17 16 use ThemeisleSDK\Product;
18 17
19 18 if ( ! defined( 'ABSPATH' ) ) {
20 19 exit;
@@ -23,33 +22,16 @@
23 22 /**
24 23 * Licenser module for ThemeIsle SDK.
25 24 */
26 25 class Licenser extends Abstract_Module {
26 +
27 27 /**
28 - * License VALID status string.
29 - */
30 - const STATUS_VALID = 'valid';
31 - /**
32 - * License NOT_ACTIVE status string.
33 - */
34 - const STATUS_NOT_ACTIVE = 'not_active';
35 - /**
36 - * License active expired status string.
37 - */
38 - const STATUS_ACTIVE_EXPIRED = 'active_expired';
39 - /**
40 28 * Number of max failed checks before showing the license message.
41 29 *
42 30 * @var int $max_failed Maximum failed checks allowed before show the notice
43 31 */
44 - private static $max_failed = 1;
32 + private static $max_failed = 5;
45 33 /**
46 - * Flag to check if the global actions were loaded.
47 - *
48 - * @var bool If the globals actions were loaded.
49 - */
50 - private static $globals_loaded = false;
51 - /**
52 34 * License key string.
53 35 *
54 36 * @var string $license_key The license key string
55 37 */
@@ -73,21 +55,8 @@
73 55 */
74 56 private $product_key;
75 57
76 58 /**
77 - * Holds local license object.
78 - *
79 - * @var null Local license object.
80 - */
81 - private $license_local = null;
82 - /**
83 - * Product namespace, used for fixed name filters/cli commands.
84 - *
85 - * @var string $namespace Product namespace.
86 - */
87 - private $namespace = null;
88 -
89 - /**
90 59 * Disable wporg updates for premium products.
91 60 *
92 61 * @param string $r Update payload.
93 62 * @param string $url The api url.
@@ -93,9 +62,9 @@
93 62 * @param string $url The api url.
94 63 *
95 64 * @return mixed List of themes to check for update.
96 65 */
97 - public function disable_wporg_update( $r, $url ) {
66 + function disable_wporg_update( $r, $url ) {
98 67
99 68 if ( 0 !== strpos( $url, 'https://api.wordpress.org/themes/update-check/' ) ) {
100 69 return $r;
101 70 }
@@ -105,9 +74,9 @@
105 74
106 75 unset( $themes->themes->{$this->product->get_slug()} );
107 76
108 77 // Encode the updated JSON response.
109 - $r['body']['themes'] = wp_json_encode( $themes );
78 + $r['body']['themes'] = json_encode( $themes );
110 79
111 80 return $r;
112 81 }
113 82
@@ -194,20 +163,20 @@
194 163 </style>
195 164 <?php
196 165 echo sprintf(
197 166 '<p>%s<input class="themeisle-sdk-license-input %s" type="text" id="%s_license" name="%s_license" value="%s" /><a class="%s">%s</a>&nbsp;&nbsp;&nbsp;<button name="%s_btn_trigger" class="button button-primary themeisle-sdk-licenser-button-cta" value="yes" type="submit" >%s</button></p><p class="description">%s</p>%s',
198 - ( ( 'valid' === $status ) ? sprintf( '<input type="hidden" value="%s" name="%s_license" />', esc_attr( $value ), esc_attr( $this->product->get_key() ) ) : '' ),
167 + ( ( 'valid' === $status ) ? sprintf( '<input type="hidden" value="%s" name="%s_license" />', $value, $this->product->get_key() ) : '' ),
199 168 ( ( 'valid' === $status ) ? 'themeisle-sdk-text-input-valid' : '' ),
200 - esc_attr( $this->product->get_key() ),
201 - esc_attr( ( ( 'valid' === $status ) ? $this->product->get_key() . '_mask' : $this->product->get_key() ) ),
202 - esc_attr( ( ( 'valid' === $status ) ? ( str_repeat( '*', 30 ) . substr( $value, - 5 ) ) : $value ) ),
203 - esc_attr( ( 'valid' === $status ? 'themeisle-sdk-license-deactivate-cta' : 'themeisle-sdk-license-activate-cta' ) ),
204 - esc_attr( 'valid' === $status ? $valid_string : $invalid_string ),
205 - esc_attr( $this->product->get_key() ),
206 - esc_attr( 'valid' === $status ? $deactivate_string : $activate_string ),
207 - sprintf( wp_kses_data( $license_message ), '<a href="' . esc_url( $this->get_api_url() ) . '">' . esc_attr( $this->get_distributor_name() ) . '</a> ', esc_attr( $this->product->get_type() ) ),
208 - wp_kses_data( empty( $error_message ) ? '' : sprintf( '<p style="color:#dd3d36">%s</p>', ( $error_message ) ) )
209 - ) . wp_nonce_field( $this->product->get_key() . 'nonce', $this->product->get_key() . 'nonce_field', false, false );//phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
169 + $this->product->get_key(),
170 + ( ( 'valid' === $status ) ? $this->product->get_key() . '_mask' : $this->product->get_key() ),
171 + ( ( 'valid' === $status ) ? ( str_repeat( '*', 30 ) . substr( $value, - 5 ) ) : $value ),
172 + ( 'valid' === $status ? 'themeisle-sdk-license-deactivate-cta' : 'themeisle-sdk-license-activate-cta' ),
173 + ( 'valid' === $status ? $valid_string : $invalid_string ),
174 + $this->product->get_key(),
175 + ( 'valid' === $status ? $deactivate_string : $activate_string ),
176 + sprintf( $license_message, '<a href="' . $this->get_api_url() . '">' . $this->get_distributor_name() . '</a> ', $this->product->get_type() ),
177 + empty( $error_message ) ? '' : sprintf( '<p style="color:#dd3d36">%s</p>', $error_message )
178 + );
210 179
211 180 }
212 181
213 182 /**
@@ -212,13 +181,11 @@
212 181
213 182 /**
214 183 * Return the license status.
215 184 *
216 - * @param bool $check_expiration Should check if license is valid, but expired.
217 - *
218 185 * @return string The License status.
219 186 */
220 - public function get_license_status( $check_expiration = false ) {
187 + public function get_license_status() {
221 188
222 189 $license_data = get_option( $this->product->get_key() . '_license_data', '' );
223 190
224 191 if ( '' === $license_data ) {
@@ -223,96 +190,14 @@
223 190
224 191 if ( '' === $license_data ) {
225 192 return get_option( $this->product->get_key() . '_license_status', 'not_active' );
226 193 }
227 - $status = isset( $license_data->license ) ? $license_data->license : get_option( $this->product->get_key() . '_license_status', 'not_active' );
228 - if ( false === $check_expiration ) {
229 - return $status;
230 - }
231 194
232 - return ( 'valid' === $status && isset( $license_data->is_expired ) && 'yes' === $license_data->is_expired ) ? 'active_expired' : $status;
233 - }
195 + return isset( $license_data->license ) ? $license_data->license : get_option( $this->product->get_key() . '_license_status', 'not_active' );
234 196
235 - /**
236 - * Check status.
237 - *
238 - * @param string $product_file Product basefile.
239 - *
240 - * @return string Status license.
241 - */
242 - public static function status( $product_file ) {
243 - $product = Product::get( $product_file );
244 - if ( ! $product->requires_license() ) {
245 - return self::STATUS_VALID;
246 - }
247 - $license_data = self::get_license_data( $product->get_key() );
248 -
249 - $status = isset( $license_data->license ) ? $license_data->license : self::STATUS_NOT_ACTIVE;
250 -
251 - return ( 'valid' === $status && isset( $license_data->is_expired ) && 'yes' === $license_data->is_expired ) ? 'active_expired' : $status;
252 197 }
253 198
254 199 /**
255 - * Product license data.
256 - *
257 - * @param string $key Product key.
258 - *
259 - * @return false|mixed|null
260 - */
261 - private static function get_license_data( $key ) {
262 - $license_data = get_option( $key . '_license_data', '' );
263 -
264 - return isset( $license_data->license ) ? $license_data : false;
265 - }
266 -
267 - /**
268 - * Check if license is valid.
269 - *
270 - * @param string $product_file Product basefile.
271 - *
272 - * @return bool Is valid?
273 - */
274 - public static function is_valid( $product_file ) {
275 - return self::status( $product_file ) === self::STATUS_VALID;
276 - }
277 -
278 - /**
279 - * Get product plan.
280 - *
281 - * @param string $product_file Product file.
282 - *
283 - * @return int Plan id.
284 - */
285 - public static function plan( $product_file ) {
286 - $product = Product::get( $product_file );
287 - $data = self::get_license_data( $product->get_key() );
288 -
289 - return isset( $data->price_id ) ? (int) $data->price_id : - 1;
290 - }
291 -
292 - /**
293 - * Get product license key.
294 - *
295 - * @param string $product_file Product file.
296 - *
297 - * @return string
298 - */
299 - public static function key( $product_file ) {
300 - $product = Product::get( $product_file );
301 -
302 - return $product->get_license();
303 - }
304 -
305 - /**
306 - * Return the last error message.
307 - *
308 - * @return mixed Error message.
309 - */
310 - public function get_error() {
311 - return get_transient( $this->product->get_key() . 'act_err' );
312 - }
313 -
314 - /**
315 200 * Get remote api url.
316 201 *
317 202 * @return string Remote api url.
318 203 */
@@ -330,9 +215,9 @@
330 215 * @return string Remote api url.
331 216 */
332 217 public function get_distributor_name() {
333 218 if ( $this->is_from_partner( $this->product ) ) {
334 - return 'Themeisle';
219 + return 'ThemeIsle';
335 220 }
336 221
337 222 return $this->product->get_store_name();
338 223 }
@@ -337,34 +222,24 @@
337 222 return $this->product->get_store_name();
338 223 }
339 224
340 225 /**
341 - * License price id.
342 - *
343 - * @return int License plan.
344 - */
345 - public function get_plan() {
346 - return self::plan( $this->product->get_basefile() );
347 - }
348 -
349 - /**
350 226 * Show the admin notice regarding the license status.
351 227 *
352 228 * @return bool Should we show the notice ?
353 229 */
354 - public function show_notice() {
230 + function show_notice() {
355 231 if ( ! is_admin() ) {
356 232 return false;
357 233 }
358 234
359 235 if ( apply_filters( $this->product->get_key() . '_hide_license_notices', false ) ) {
360 - return false;
236 + return;
361 237 }
238 + $status = $this->get_license_status();
239 + $no_activations_string = apply_filters( $this->product->get_key() . '_lc_no_activations_string', 'No activations left for %s !!!. You need to upgrade your plan in order to use %s on more websites. Please ask the %s Staff for more details.' );
240 + $no_valid_string = apply_filters( $this->product->get_key() . '_lc_no_valid_string', 'In order to benefit from updates and support for %s, please add your license code from your <a href="%s" target="_blank">purchase history</a> and validate it <a href="%s">here</a>. ' );
362 241
363 - $status = $this->get_license_status( true );
364 - $no_activations_string = apply_filters( $this->product->get_key() . '_lc_no_activations_string', 'No more activations left for %s. You need to upgrade your plan in order to use %s on more websites. If you need assistance, please get in touch with %s staff.' );
365 - $no_valid_string = apply_filters( $this->product->get_key() . '_lc_no_valid_string', 'In order to benefit from updates and support for %s, please add your license code from your <a href="%s" target="_blank">purchase history</a> and validate it <a href="%s">here</a>. ' );
366 - $expired_license_string = apply_filters( $this->product->get_key() . '_lc_expired_string', 'Your %s\'s License Key has expired. In order to continue receiving support and software updates you must <a href="%s" target="_blank">renew</a> your license key.' );
367 242 // No activations left for this license.
368 243 if ( 'valid' != $status && $this->check_activation() ) {
369 244 ?>
370 245 <div class="error">
@@ -370,12 +245,12 @@
370 245 <div class="error">
371 246 <p><strong>
372 247 <?php
373 248 echo sprintf(
374 - wp_kses_data( $no_activations_string ),
375 - esc_attr( $this->product->get_name() ),
376 - esc_attr( $this->product->get_name() ),
377 - '<a href="' . esc_url( $this->get_api_url() ) . '" target="_blank">' . esc_attr( $this->get_distributor_name() ) . '</a>'
249 + $no_activations_string,
250 + $this->product->get_name(),
251 + $this->product->get_name(),
252 + '<a href="' . $this->get_api_url() . '" target="_blank">' . $this->get_distributor_name() . '</a>'
378 253 );
379 254 ?>
380 255 </strong>
381 256 </p>
@@ -382,27 +257,14 @@
382 257 </div>
383 258 <?php
384 259 return false;
385 260 }
386 -
387 261 // Invalid license key.
388 - if ( 'active_expired' === $status ) {
389 - ?>
390 - <div class="error">
391 - <p>
392 - <strong><?php echo sprintf( wp_kses_data( $expired_license_string ), esc_attr( $this->product->get_name() . ' ' . $this->product->get_type() ), esc_url( $this->get_api_url() . '?license=' . $this->license_key ) ); ?> </strong>
393 - </p>
394 - </div>
395 - <?php
396 -
397 - return false;
398 - }
399 - // Invalid license key.
400 262 if ( 'valid' != $status ) {
401 263 ?>
402 264 <div class="error">
403 265 <p>
404 - <strong><?php echo sprintf( wp_kses_data( $no_valid_string ), esc_attr( $this->product->get_name() . ' ' . $this->product->get_type() ), esc_url( $this->get_api_url() ), esc_url( admin_url( 'options-general.php' ) . '#' . $this->product->get_key() . '_license' ) ); ?> </strong>
266 + <strong><?php echo sprintf( $no_valid_string, $this->product->get_name() . ' ' . $this->product->get_type(), $this->get_api_url(), admin_url( 'options-general.php' ) . '#' . $this->product->get_key() . '_license' ); ?> </strong>
405 267 </p>
406 268 </div>
407 269 <?php
408 270
@@ -422,9 +284,9 @@
422 284 if ( '' === $license_data ) {
423 285 return false;
424 286 }
425 287
426 - return isset( $license_data->license ) ? ( 'no_activations_left' == $license_data->license ) : false;
288 + return isset( $license_data->error ) ? ( 'no_activations_left' == $license_data->error ) : false;
427 289
428 290 }
429 291
430 292 /**
@@ -431,9 +293,9 @@
431 293 * Check if the license is about to expire in the next month.
432 294 *
433 295 * @return bool
434 296 */
435 - public function check_expiration() {
297 + function check_expiration() {
436 298 $license_data = get_option( $this->product->get_key() . '_license_data', '' );
437 299 if ( '' === $license_data ) {
438 300 return false;
439 301 }
@@ -451,9 +313,9 @@
451 313 * Return the renew url from the store used.
452 314 *
453 315 * @return string The renew url.
454 316 */
455 - public function renew_url() {
317 + function renew_url() {
456 318 $license_data = get_option( $this->product->get_key() . '_license_data', '' );
457 319 if ( '' === $license_data ) {
458 320 return $this->get_api_url();
459 321 }
@@ -460,9 +322,9 @@
460 322 if ( ! isset( $license_data->download_id ) || ! isset( $license_data->key ) ) {
461 323 return $this->get_api_url();
462 324 }
463 325
464 - return trim( $this->get_api_url(), '/' ) . '/checkout/?edd_license_key=' . $license_data->key . '&download_id=' . $license_data->download_id;
326 + return $this->get_api_url() . '/checkout/?edd_license_key=' . $license_data->key . '&download_id=' . $license_data->download_id;
465 327 }
466 328
467 329 /**
468 330 * Run the license check call.
@@ -467,9 +329,9 @@
467 329 /**
468 330 * Run the license check call.
469 331 */
470 332 public function product_valid() {
471 - if ( false !== ( $license = get_transient( $this->product->get_key() . '_license_data' ) ) ) { //phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.FoundInControlStructure
333 + if ( false !== ( $license = get_transient( $this->product->get_key() . '_license_data' ) ) ) {
472 334 return;
473 335 }
474 336 $license = $this->check_license();
475 337 set_transient( $this->product->get_key() . '_license_data', $license, 12 * HOUR_IN_SECONDS );
@@ -482,101 +344,145 @@
482 344 * @return object The license data.
483 345 */
484 346 public function check_license() {
485 347 $status = $this->get_license_status();
486 - if ( 'not_active' === $status ) {
348 + if ( 'not_active' == $status ) {
487 349 $license_data = new \stdClass();
488 350 $license_data->license = 'not_active';
489 351
490 352 return $license_data;
491 353 }
492 - $license = trim( $this->license_key );
493 -
494 - $response = $this->do_license_process( $license, 'check' );
495 -
354 + $license = trim( $this->license_key );
355 + $api_params = array(
356 + 'edd_action' => 'check_license',
357 + 'license' => $license,
358 + 'item_name' => rawurlencode( $this->product->get_name() ),
359 + 'url' => rawurlencode( home_url() ),
360 + );
361 + // Call the custom API.
362 + $response = wp_remote_get(
363 + add_query_arg( $api_params, $this->get_api_url() ),
364 + array(
365 + 'timeout' => 15,
366 + 'sslverify' => false,
367 + )
368 + );
496 369 if ( is_wp_error( $response ) ) {
497 370 $license_data = new \stdClass();
498 - $license_data->license = 'invalid';
371 + $license_data->license = 'valid';
372 +
499 373 } else {
500 - $license_data = $response;
374 + $license_data = json_decode( wp_remote_retrieve_body( $response ) );
375 + if ( ! is_object( $license_data ) ) {
376 + $license_data = new \stdClass();
377 + $license_data->license = 'valid';
378 + }
501 379 }
502 -
503 380 $license_old = get_option( $this->product->get_key() . '_license_data', '' );
504 - if ( 'valid' === $license_old->license && ( $license_data->license !== $license_old->license ) && $this->failed_checks <= self::$max_failed ) {
381 + if ( 'valid' == $license_old->license && ( $license_data->license != $license_old->license ) ) {
505 382 $this->increment_failed_checks();
383 + } else {
384 + $this->reset_failed_checks();
385 + }
506 386
387 + if ( $this->failed_checks <= self::$max_failed ) {
507 388 return $license_old;
508 389 }
509 390
391 + if ( isset( $license_old->hide_valid ) ) {
392 + $license_data->hide_valid = true;
393 + }
394 +
510 395 if ( ! isset( $license_data->key ) ) {
511 396 $license_data->key = isset( $license_old->key ) ? $license_old->key : '';
512 397 }
513 - $this->reset_failed_checks();
514 398
399 + if ( isset( $license_old->hide_expiration ) ) {
400 + $license_data->hide_expiration = true;
401 + }
402 +
403 + if ( isset( $license_old->hide_activation ) ) {
404 + $license_data->hide_activation = true;
405 + }
406 +
515 407 return $license_data;
516 408
517 409 }
518 410
519 411 /**
520 - * Do license activation/deactivation.
412 + * Increment the failed checks.
413 + */
414 + private function increment_failed_checks() {
415 + $this->failed_checks ++;
416 + update_option( $this->product->get_key() . '_failed_checks', $this->failed_checks );
417 + }
418 +
419 + /**
420 + * Reset the failed checks
421 + */
422 + private function reset_failed_checks() {
423 + $this->failed_checks = 1;
424 + update_option( $this->product->get_key() . '_failed_checks', $this->failed_checks );
425 + }
426 +
427 + /**
428 + * Set license validation error message.
521 429 *
522 - * @param string $license License key.
523 - * @param string $action What do to.
430 + * @param string $message Error message.
431 + */
432 + public function set_error( $message = '' ) {
433 + set_transient( $this->product->get_key() . 'act_err', $message, MINUTE_IN_SECONDS );
434 +
435 + return;
436 + }
437 +
438 + /**
439 + * Return the last error message.
524 440 *
525 - * @return bool|\WP_Error
441 + * @return mixed Error message.
526 442 */
527 - public function do_license_process( $license, $action = 'toggle' ) {
528 - if ( strlen( $license ) < 10 ) {
529 - return new \WP_Error( 'themeisle-license-invalid-format', 'Invalid license.' );
443 + public function get_error() {
444 + return get_transient( $this->product->get_key() . 'act_err' );
445 + }
446 +
447 + /**
448 + * Activate the license remotely.
449 + */
450 + function activate_license() {
451 + // listen for our activate button to be clicked.
452 + if ( ! isset( $_POST[ $this->product->get_key() . '_btn_trigger' ] ) ) {
453 + return;
530 454 }
531 455 $status = $this->get_license_status();
532 -
533 - if ( 'valid' === $status && 'activate' === $action ) {
534 - return new \WP_Error( 'themeisle-license-already-active', 'License is already active.' );
456 + // retrieve the license from the database.
457 + $license = $_POST[ $this->product->get_key() . '_license' ];
458 + $api_params = array(
459 + 'license' => $license,
460 + 'item_name' => rawurlencode( $this->product->get_name() ),
461 + 'url' => rawurlencode( home_url() ),
462 + );
463 + if ( 'valid' != $status ) {
464 + // data to send in our API request.
465 + $api_params['edd_action'] = 'activate_license';
466 + } else {
467 + $api_params['edd_action'] = 'deactivate_license';
535 468 }
536 - if ( 'valid' !== $status && 'deactivate' === $action ) {
537 - return new \WP_Error( 'themeisle-license-already-deactivate', 'License not active.' );
538 - }
539 -
540 - if ( 'toggle' === $action ) {
541 - $action = ( 'valid' !== $status ? ( 'activate' ) : ( 'deactivate' ) );
542 - }
543 -
544 469 // Call the custom API.
545 - if ( 'check' === $action ) {
546 - $response = $this->safe_get( sprintf( '%slicense/check/%s/%s/%s/%s', Product::API_URL, rawurlencode( $this->product->get_name() ), $license, rawurlencode( home_url() ), Loader::get_cache_token() ) );
547 - } else {
548 - $response = wp_remote_post(
549 - sprintf( '%slicense/%s/%s/%s', Product::API_URL, $action, rawurlencode( $this->product->get_name() ), $license ),
550 - array(
551 - 'body' => wp_json_encode(
552 - array(
553 - 'url' => rawurlencode( home_url() ),
554 - )
555 - ),
556 - 'headers' => array(
557 - 'Content-Type' => 'application/json',
558 - ),
559 - )
560 - );
561 - }
562 -
470 + $response = wp_remote_get( add_query_arg( $api_params, $this->get_api_url() ) );
563 471 // make sure the response came back okay.
564 472 if ( is_wp_error( $response ) ) {
565 - return new \WP_Error( 'themeisle-license-500', sprintf( 'ERROR: Failed to connect to the license service. Please try again later. Reason: %s', $response->get_error_message() ) );
473 + $this->set_error( sprintf( 'ERROR: Failed to connect to the license service. Please try again later. Reason: %s', $response->get_error_message() ) );
474 +
475 + return;
566 476 }
567 477
568 478 $license_data = json_decode( wp_remote_retrieve_body( $response ) );
569 479
570 480 if ( ! is_object( $license_data ) ) {
571 - return new \WP_Error( 'themeisle-license-404', 'ERROR: Failed to validate license. Please try again in one minute.' );
481 + $this->set_error( 'ERROR: Failed to validate license. Please try again in one minute.' );
482 +
483 + return;
572 484 }
573 - if ( 'check' === $action ) {
574 - return $license_data;
575 - }
576 -
577 - Loader::clear_cache_token();
578 -
579 485 if ( ! isset( $license_data->license ) ) {
580 486 $license_data->license = 'invalid';
581 487 }
582 488
@@ -582,19 +488,21 @@
582 488
583 489 if ( ! isset( $license_data->key ) ) {
584 490 $license_data->key = $license;
585 491 }
586 - if ( 'valid' === $license_data->license ) {
492 + if ( 'valid' == $license_data->license ) {
587 493 $this->reset_failed_checks();
588 494 }
589 495
590 - if ( 'deactivate' === $action ) {
496 + $this->set_error( '' );
591 497
498 + if ( 'deactivate_license' === $api_params['edd_action'] ) {
499 +
592 500 delete_option( $this->product->get_key() . '_license_data' );
593 501 delete_option( $this->product->get_key() . '_license_plan' );
594 502 delete_transient( $this->product->get_key() . '_license_data' );
595 503
596 - return true;
504 + return;
597 505 }
598 506 if ( isset( $license_data->plan ) ) {
599 507 update_option( $this->product->get_key() . '_license_plan', $license_data->plan );
600 508 }
@@ -599,80 +507,15 @@
599 507 update_option( $this->product->get_key() . '_license_plan', $license_data->plan );
600 508 }
601 509 update_option( $this->product->get_key() . '_license_data', $license_data );
602 510 set_transient( $this->product->get_key() . '_license_data', $license_data, 12 * HOUR_IN_SECONDS );
603 - if ( 'activate' === $action && 'valid' !== $license_data->license ) {
604 - return new \WP_Error( 'themeisle-license-invalid', 'ERROR: Invalid license provided.' );
605 - }
606 511
607 - // Remove the versions transient upon activation so that newer version for rollback can be acquired.
608 - $versions_cache = $this->product->get_cache_key();
609 - delete_transient( $versions_cache );
610 -
611 - return true;
612 512 }
613 513
614 514 /**
615 - * Reset the failed checks
616 - */
617 - private function reset_failed_checks() {
618 - $this->failed_checks = 1;
619 - update_option( $this->product->get_key() . '_failed_checks', $this->failed_checks );
620 - }
621 -
622 - /**
623 - * Increment the failed checks.
624 - */
625 - private function increment_failed_checks() {
626 - $this->failed_checks ++;
627 - update_option( $this->product->get_key() . '_failed_checks', $this->failed_checks );
628 - }
629 -
630 - /**
631 - * Activate the license remotely.
632 - */
633 - public function process_license() {
634 - // listen for our activate button to be clicked.
635 - if ( ! isset( $_POST[ $this->product->get_key() . '_btn_trigger' ] ) ) {
636 - return;
637 - }
638 - if ( ! isset( $_POST[ $this->product->get_key() . 'nonce_field' ] )
639 - || ! wp_verify_nonce( $_POST[ $this->product->get_key() . 'nonce_field' ], $this->product->get_key() . 'nonce' ) //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
640 - ) {
641 - return;
642 - }
643 - if ( ! current_user_can( 'manage_options' ) ) {
644 - return;
645 - }
646 - $license = isset( $_POST[ $this->product->get_key() . '_license' ] )
647 - ? sanitize_text_field( $_POST[ $this->product->get_key() . '_license' ] )
648 - : '';
649 -
650 - $response = $this->do_license_process( $license, 'toggle' );
651 - if ( is_wp_error( $response ) ) {
652 - $this->set_error( $response->get_error_message() );
653 -
654 - return;
655 - }
656 - if ( true === $response ) {
657 - $this->set_error( '' );
658 - }
659 - }
660 -
661 - /**
662 - * Set license validation error message.
663 - *
664 - * @param string $message Error message.
665 - */
666 - public function set_error( $message = '' ) {
667 - set_transient( $this->product->get_key() . 'act_err', $message, MINUTE_IN_SECONDS );
668 -
669 - }
670 -
671 - /**
672 515 * Load the Themes screen.
673 516 */
674 - public function load_themes_screen() {
517 + function load_themes_screen() {
675 518 add_thickbox();
676 519 add_action( 'admin_notices', array( &$this, 'update_nag' ) );
677 520 }
678 521
@@ -678,9 +521,9 @@
678 521
679 522 /**
680 523 * Alter the nag for themes update.
681 524 */
682 - public function update_nag() {
525 + function update_nag() {
683 526 $theme = wp_get_theme( $this->product->get_slug() );
684 527 $api_response = get_transient( $this->product_key );
685 528 if ( false === $api_response || ! isset( $api_response->new_version ) ) {
686 529 return;
@@ -691,18 +534,18 @@
691 534 if ( version_compare( $this->product->get_version(), $api_response->new_version, '<' ) ) {
692 535 echo '<div id="update-nag">';
693 536 printf(
694 537 '<strong>%1$s %2$s</strong> is available. <a href="%3$s" class="thickbox" title="%4s">Check out what\'s new</a> or <a href="%5$s"%6$s>update now</a>.',
695 - esc_attr( $theme->get( 'Name' ) ),
696 - esc_attr( $api_response->new_version ),
697 - esc_url( sprintf( '%s&TB_iframe=true&amp;width=1024&amp;height=800', $this->product->get_changelog() ) ),
698 - esc_attr( $theme->get( 'Name' ) ),
699 - esc_url( $update_url ),
700 - $update_onclick // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped, Already escaped.
538 + $theme->get( 'Name' ),
539 + $api_response->new_version,
540 + sprintf( '%s&TB_iframe=true&amp;width=1024&amp;height=800', $this->product->get_changelog() ),
541 + $theme->get( 'Name' ),
542 + $update_url,
543 + $update_onclick
701 544 );
702 545 echo '</div>';
703 - echo '<div id="' . esc_attr( $this->product->get_slug() ) . '_changelog" style="display:none;">';
704 - echo wp_kses_data( wpautop( $api_response->sections['changelog'] ) );
546 + echo '<div id="' . $this->product->get_slug() . '_' . 'changelog" style="display:none;">';
547 + echo wpautop( $api_response->sections['changelog'] );
705 548 echo '</div>';
706 549 }
707 550 }
708 551
@@ -712,23 +555,14 @@
712 555 * @param mixed $value The transient data.
713 556 *
714 557 * @return mixed
715 558 */
716 - public function theme_update_transient( $value ) {
559 + function theme_update_transient( $value ) {
717 560 $update_data = $this->check_for_update();
718 - if ( empty( $value ) ) {
719 - return $value;
561 + if ( $update_data ) {
562 + $value->response[ $this->product->get_slug() ] = $update_data;
720 563 }
721 564
722 - if ( ! isset( $value->response ) ) {
723 - return $value;
724 - }
725 -
726 - if ( ! $update_data ) {
727 - return $value;
728 - }
729 -
730 - $value->response[ $this->product->get_slug() ] = $update_data;
731 565 return $value;
732 566 }
733 567
734 568 /**
@@ -735,9 +569,9 @@
735 569 * Check for updates
736 570 *
737 571 * @return array|bool Either the update data or false in case of failure.
738 572 */
739 - public function check_for_update() {
573 + function check_for_update() {
740 574 $update_data = get_transient( $this->product_key );
741 575
742 576 if ( false === $update_data ) {
743 577 $failed = false;
@@ -772,20 +606,21 @@
772 606 *
773 607 * @return bool|mixed Update api response.
774 608 */
775 609 private function get_version_data() {
776 -
777 - $response = $this->safe_get(
778 - sprintf(
779 - '%slicense/version/%s/%s/%s/%s',
780 - Product::API_URL,
781 - rawurlencode( $this->product->get_name() ),
782 - ( empty( $this->license_key ) ? 'free' : $this->license_key ),
783 - $this->product->get_version(),
784 - rawurlencode( home_url() )
785 - ),
610 + $api_params = array(
611 + 'edd_action' => 'get_version',
612 + 'version' => $this->product->get_version(),
613 + 'license' => empty( $this->license_key ) ? 'free' : $this->license_key,
614 + 'name' => rawurlencode( $this->product->get_name() ),
615 + 'slug' => $this->product->get_slug(),
616 + 'author' => rawurlencode( $this->get_distributor_name() ),
617 + 'url' => rawurlencode( home_url() ),
618 + );
619 + $response = wp_remote_get(
620 + add_query_arg( $api_params, $this->get_api_url() ),
786 621 array(
787 - 'timeout' => 15, //phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout, Inherited by wp_remote_get only, for vip environment we use defaults.
622 + 'timeout' => 15,
788 623 'sslverify' => false,
789 624 )
790 625 );
791 626 if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) ) {
@@ -794,17 +629,8 @@
794 629 $update_data = json_decode( wp_remote_retrieve_body( $response ) );
795 630 if ( ! is_object( $update_data ) ) {
796 631 return false;
797 632 }
798 - if ( isset( $update_data->slug ) ) {
799 - $update_data->slug = $this->product->get_slug();
800 - }
801 - if ( isset( $update_data->icons ) ) {
802 - $update_data->icons = (array) $update_data->icons;
803 - }
804 - if ( isset( $update_data->banners ) ) {
805 - $update_data->banners = (array) $update_data->banners;
806 - }
807 633
808 634 return $update_data;
809 635 }
810 636
@@ -810,10 +636,10 @@
810 636
811 637 /**
812 638 * Delete the update transient
813 639 */
814 - public function delete_theme_update_transient() {
815 - return delete_transient( $this->product_key );
640 + function delete_theme_update_transient() {
641 + delete_transient( $this->product_key );
816 642 }
817 643
818 644 /**
819 645 * Check for Updates at the defined API endpoint and modify the update array.
@@ -829,15 +655,10 @@
829 655 return $_transient_data;
830 656 }
831 657 $api_response = $this->api_request();
832 658 if ( false !== $api_response && is_object( $api_response ) && isset( $api_response->new_version ) ) {
833 - if ( ! isset( $api_response->plugin ) ) {
834 - $api_response->plugin = $this->product->get_slug() . '/' . $this->product->get_file();
835 - }
836 659 if ( version_compare( $this->product->get_version(), $api_response->new_version, '<' ) ) {
837 660 $_transient_data->response[ $this->product->get_slug() . '/' . $this->product->get_file() ] = $api_response;
838 - } else {
839 - $_transient_data->no_update[ $this->product->get_slug() . '/' . $this->product->get_file() ] = $api_response;
840 661 }
841 662 }
842 663
843 664 return $_transient_data;
@@ -872,9 +693,9 @@
872 693 *
873 694 * @return object $_data
874 695 */
875 696 public function plugins_api_filter( $_data, $_action = '', $_args = null ) {
876 - if ( ( 'plugin_information' !== $_action ) || ! isset( $_args->slug ) || ( $_args->slug !== $this->product->get_slug() ) ) {
697 + if ( ( 'plugin_information' != $_action ) || ! isset( $_args->slug ) || ( $_args->slug != $this->product->get_slug() ) ) {
877 698 return $_data;
878 699 }
879 700 $api_response = $this->api_request();
880 701 if ( false !== $api_response ) {
@@ -891,9 +712,9 @@
891 712 * @param string $url Url to check.
892 713 *
893 714 * @return array $array
894 715 */
895 - public function http_request_args( $args, $url ) {
716 + function http_request_args( $args, $url ) {
896 717 // If it is an https request and we are performing a package download, disable ssl verification.
897 718 if ( strpos( $url, 'https://' ) !== false && strpos( $url, 'edd_action=package_download' ) ) {
898 719 $args['sslverify'] = false;
899 720 }
@@ -934,31 +755,9 @@
934 755 if ( $this->product->requires_license() ) {
935 756 $this->failed_checks = intval( get_option( $this->product->get_key() . '_failed_checks', 0 ) );
936 757 $this->register_license_hooks();
937 758 }
938 - if ( ! self::$globals_loaded ) {
939 - add_filter( 'themeisle_sdk_license/status', [ __CLASS__, 'status' ], 999, 1 );
940 - add_filter( 'themeisle_sdk_license/is-valid', [ __CLASS__, 'is_valid' ], 999, 1 );
941 - add_filter( 'themeisle_sdk_license/plan', [ __CLASS__, 'plan' ], 999, 1 );
942 - add_filter( 'themeisle_sdk_license/key', [ __CLASS__, 'key' ], 999, 1 );
943 - $globals_loaded = true;
944 - }
945 - $namespace = apply_filters( 'themesle_sdk_namespace_' . md5( $product->get_basefile() ), false );
946 759
947 - if ( false !== $namespace ) {
948 - $this->namespace = $namespace;
949 - add_filter( 'themeisle_sdk_license_process_' . $namespace, [ $this, 'do_license_process' ], 10, 2 );
950 - add_filter( 'product_' . $namespace . '_license_status', [ $this, 'get_license_status' ], PHP_INT_MAX );
951 - add_filter( 'product_' . $namespace . '_license_key', [ $this->product, 'get_license' ] );
952 - add_filter( 'product_' . $namespace . '_license_plan', [ $this, 'get_plan' ], PHP_INT_MAX );
953 - if ( defined( 'WP_CLI' ) && WP_CLI ) {
954 - \WP_CLI::add_command( $namespace . ' activate', [ $this, 'cli_activate' ] );
955 - \WP_CLI::add_command( $namespace . ' deactivate', [ $this, 'cli_deactivate' ] );
956 - \WP_CLI::add_command( $namespace . ' is-active', [ $this, 'cli_is_active' ] );
957 - }
958 - }
959 -
960 - add_action( 'admin_head', [ $this, 'auto_activate' ] );
961 760 if ( $this->product->is_plugin() ) {
962 761 add_filter(
963 762 'pre_set_site_transient_update_plugins',
964 763 [
@@ -966,33 +765,19 @@
966 765 'pre_set_site_transient_update_plugins_filter',
967 766 ]
968 767 );
969 768 add_filter( 'plugins_api', array( $this, 'plugins_api_filter' ), 10, 3 );
970 - add_filter( 'http_request_args', array( $this, 'http_request_args' ), 10, 2 ); //phpcs:ignore WordPressVIPMinimum.Hooks.RestrictedHooks.http_request_args
971 - if ( ! self::is_valid( $product->get_basefile() ) ) {
972 - add_filter(
973 - 'plugin_action_links_' . plugin_basename( $product->get_basefile() ),
974 - function ( $actions ) {
975 - if ( $this->get_license_status( true ) !== self::STATUS_ACTIVE_EXPIRED ) {
976 - return $actions;
977 - }
978 - $new_actions['deactivate'] = $actions['deactivate'];
979 - $new_actions['renew_link'] = '<a style="color:#d63638" href="' . esc_url( $this->renew_url() ) . '" target="_blank" rel="external noopener noreferrer">Renew license to update</a>';
769 + add_filter( 'http_request_args', array( $this, 'http_request_args' ), 10, 2 );
980 770
981 - return $new_actions;
982 - }
983 - );
984 - }
985 -
986 771 return $this;
987 772 }
988 773 if ( $this->product->is_theme() ) {
989 774 add_filter( 'site_transient_update_themes', array( &$this, 'theme_update_transient' ) );
990 - add_action( 'delete_site_transient_update_themes', array( &$this, 'delete_theme_update_transient' ) );
775 + add_filter( 'delete_site_transient_update_themes', array( &$this, 'delete_theme_update_transient' ) );
991 776 add_action( 'load-update-core.php', array( &$this, 'delete_theme_update_transient' ) );
992 777 add_action( 'load-themes.php', array( &$this, 'delete_theme_update_transient' ) );
993 778 add_action( 'load-themes.php', array( &$this, 'load_themes_screen' ) );
994 - add_filter( 'http_request_args', array( $this, 'disable_wporg_update' ), 5, 2 ); //phpcs:ignore WordPressVIPMinimum.Hooks.RestrictedHooks.http_request_args
779 + add_filter( 'http_request_args', array( $this, 'disable_wporg_update' ), 5, 2 );
995 780
996 781 return $this;
997 782
998 783 }
@@ -1004,152 +789,10 @@
1004 789 * Register license fields for the products.
1005 790 */
1006 791 public function register_license_hooks() {
1007 792 add_action( 'admin_init', array( $this, 'register_settings' ) );
1008 - add_action( 'admin_init', array( $this, 'process_license' ) );
793 + add_action( 'admin_init', array( $this, 'activate_license' ) );
1009 794 add_action( 'admin_init', array( $this, 'product_valid' ), 99999999 );
1010 795 add_action( 'admin_notices', array( $this, 'show_notice' ) );
1011 796 add_filter( $this->product->get_key() . '_license_status', array( $this, 'get_license_status' ) );
1012 - }
1013 -
1014 - /**
1015 - * Check license on filesystem.
1016 - *
1017 - * @return mixed License key.
1018 - */
1019 - public function get_file_license() {
1020 -
1021 - $license_file = dirname( $this->product->get_basefile() ) . '/license.json';
1022 -
1023 - global $wp_filesystem;
1024 - if ( ! is_file( $license_file ) ) {
1025 - return false;
1026 - }
1027 -
1028 - require_once ABSPATH . '/wp-admin/includes/file.php';
1029 - \WP_Filesystem();
1030 - $content = json_decode( $wp_filesystem->get_contents( $license_file ) );
1031 - if ( ! is_object( $content ) ) {
1032 - return false;
1033 - }
1034 - if ( ! isset( $content->key ) ) {
1035 - return false;
1036 - }
1037 - return $content->key;
1038 - }
1039 - /**
1040 - * Run license activation on plugin activate.
1041 - */
1042 - public function auto_activate() {
1043 - $status = $this->get_license_status();
1044 - if ( 'not_active' !== $status ) {
1045 - return false;
1046 - }
1047 -
1048 - if ( ! empty( $this->namespace ) ) {
1049 - $license_key = apply_filters( 'product_' . $this->namespace . '_license_key_constant', '' );
1050 - }
1051 -
1052 - if ( empty( $license_key ) ) {
1053 - $license_key = $this->get_file_license();
1054 - }
1055 - if ( empty( $license_key ) ) {
1056 - return;
1057 - }
1058 -
1059 -
1060 - $this->license_local = $license_key;
1061 - $lock_key = $this->product->get_key() . '_autoactivated';
1062 -
1063 - if ( 'yes' === get_option( $lock_key, '' ) ) {
1064 - return;
1065 - }
1066 - if ( 'yes' === get_transient( $lock_key ) ) {
1067 - return;
1068 - }
1069 - $response = $this->do_license_process( $license_key, 'activate' );
1070 -
1071 - set_transient( $lock_key, 'yes', 6 * HOUR_IN_SECONDS );
1072 -
1073 - if ( apply_filters( $this->product->get_key() . '_hide_license_notices', false ) ) {
1074 - return;
1075 - }
1076 -
1077 - if ( true === $response ) {
1078 - add_action( 'admin_notices', [ $this, 'autoactivate_notice' ] );
1079 - }
1080 - }
1081 -
1082 - /**
1083 - * Show auto-activate notice.
1084 - */
1085 - public function autoactivate_notice() {
1086 - ?>
1087 - <div class="notice notice-success is-dismissible">
1088 - <p><?php echo sprintf( '<strong>%s</strong> has been successfully activated using <strong>%s</strong> license !', esc_attr( $this->product->get_name() ), esc_attr( str_repeat( '*', 20 ) . substr( $this->license_local, - 10 ) ) ); ?></p>
1089 - </div>
1090 - <?php
1091 - }
1092 -
1093 - /**
1094 - * Activate product license on this site.
1095 - *
1096 - * ## OPTIONS
1097 - *
1098 - * @param array $args Command args.
1099 - *
1100 - * [<license-key>]
1101 - * : Product license key.
1102 - */
1103 - public function cli_activate( $args ) {
1104 - $license_key = isset( $args[0] ) ? trim( $args[0] ) : '';
1105 - $response = $this->do_license_process( $license_key, 'activate' );
1106 - if ( true !== $response ) {
1107 - \WP_CLI::error( $response->get_error_message() );
1108 -
1109 - return;
1110 - }
1111 -
1112 - \WP_CLI::success( 'Product successfully activated.' );
1113 - }
1114 -
1115 - /**
1116 - * Deactivate product license on this site.
1117 - *
1118 - * @param array $args Command args.
1119 - *
1120 - * ## OPTIONS
1121 - *
1122 - * [<license-key>]
1123 - * : Product license key.
1124 - */
1125 - public function cli_deactivate( $args ) {
1126 - $license_key = isset( $args[0] ) ? trim( $args[0] ) : '';
1127 - $response = $this->do_license_process( $license_key, 'deactivate' );
1128 - if ( true !== $response ) {
1129 - \WP_CLI::error( $response->get_error_message() );
1130 -
1131 - return;
1132 - }
1133 -
1134 - \WP_CLI::success( 'Product successfully deactivated.' );
1135 - }
1136 -
1137 - /**
1138 - * Checks if product has license activated.
1139 - *
1140 - * @param array $args Command args.
1141 - *
1142 - * @subcommand is-active
1143 - */
1144 - public function cli_is_active( $args ) {
1145 -
1146 - $status = $this->get_license_status();
1147 - if ( 'valid' === $status ) {
1148 - \WP_CLI::halt( 0 );
1149 -
1150 - return;
1151 - }
1152 -
1153 - \WP_CLI::halt( 1 );
1154 797 }
1155 798 }