← All changes
|
vendor/codeinwp/themeisle-sdk/src/Modules/Licenser.php
+305
-66
3.1.2
→
trunk
View file →
| @@ -23,16 +23,33 @@ | ||
| 23 | 23 | /** |
| 24 | 24 | * Licenser module for ThemeIsle SDK. |
| 25 | 25 | */ |
| 26 | 26 | class Licenser extends Abstract_Module { |
| 27 | - | |
| 28 | 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 | + /** | |
| 29 | 40 | * Number of max failed checks before showing the license message. |
| 30 | 41 | * |
| 31 | 42 | * @var int $max_failed Maximum failed checks allowed before show the notice |
| 32 | 43 | */ |
| 33 | - private static $max_failed = 5; | |
| 44 | + private static $max_failed = 1; | |
| 34 | 45 | /** |
| 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 | + /** | |
| 35 | 52 | * License key string. |
| 36 | 53 | * |
| 37 | 54 | * @var string $license_key The license key string |
| 38 | 55 | */ |
| @@ -120,13 +137,13 @@ | ||
| 120 | 137 | public function license_view() { |
| 121 | 138 | $status = $this->get_license_status(); |
| 122 | 139 | $value = $this->license_key; |
| 123 | 140 | |
| 124 | - $activate_string = apply_filters( $this->product->get_key() . '_lc_activate_string', 'Activate' ); | |
| 125 | - $deactivate_string = apply_filters( $this->product->get_key() . '_lc_deactivate_string', 'Deactivate' ); | |
| 126 | - $valid_string = apply_filters( $this->product->get_key() . '_lc_valid_string', 'Valid' ); | |
| 127 | - $invalid_string = apply_filters( $this->product->get_key() . '_lc_invalid_string', 'Invalid' ); | |
| 128 | - $license_message = apply_filters( $this->product->get_key() . '_lc_license_message', 'Enter your license from %s purchase history in order to get %s updates' ); | |
| 141 | + $activate_string = apply_filters( $this->product->get_key() . '_lc_activate_string', Loader::$labels['licenser']['activate'] ); | |
| 142 | + $deactivate_string = apply_filters( $this->product->get_key() . '_lc_deactivate_string', Loader::$labels['licenser']['deactivate'] ); | |
| 143 | + $valid_string = apply_filters( $this->product->get_key() . '_lc_valid_string', Loader::$labels['licenser']['valid'] ); | |
| 144 | + $invalid_string = apply_filters( $this->product->get_key() . '_lc_invalid_string', Loader::$labels['licenser']['invalid'] ); | |
| 145 | + $license_message = apply_filters( $this->product->get_key() . '_lc_license_message', Loader::$labels['licenser']['notice'] ); | |
| 129 | 146 | $error_message = $this->get_error(); |
| 130 | 147 | ?> |
| 131 | 148 | <style type="text/css"> |
| 132 | 149 | input.themeisle-sdk-text-input-valid { |
| @@ -215,8 +232,95 @@ | ||
| 215 | 232 | return ( 'valid' === $status && isset( $license_data->is_expired ) && 'yes' === $license_data->is_expired ) ? 'active_expired' : $status; |
| 216 | 233 | } |
| 217 | 234 | |
| 218 | 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 | + } | |
| 253 | + | |
| 254 | + /** | |
| 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 | + * Get license hash. | |
| 269 | + * | |
| 270 | + * @param string $key Product key. | |
| 271 | + * | |
| 272 | + * @return bool|string | |
| 273 | + */ | |
| 274 | + public static function create_license_hash( $key ) { | |
| 275 | + $data = self::get_license_data( $key ); | |
| 276 | + | |
| 277 | + if ( ! $data ) { | |
| 278 | + return false; | |
| 279 | + } | |
| 280 | + | |
| 281 | + return isset( $data->key ) ? wp_hash( $data->key ) : false; | |
| 282 | + } | |
| 283 | + | |
| 284 | + /** | |
| 285 | + * Check if license is valid. | |
| 286 | + * | |
| 287 | + * @param string $product_file Product basefile. | |
| 288 | + * | |
| 289 | + * @return bool Is valid? | |
| 290 | + */ | |
| 291 | + public static function is_valid( $product_file ) { | |
| 292 | + return self::status( $product_file ) === self::STATUS_VALID; | |
| 293 | + } | |
| 294 | + | |
| 295 | + /** | |
| 296 | + * Get product plan. | |
| 297 | + * | |
| 298 | + * @param string $product_file Product file. | |
| 299 | + * | |
| 300 | + * @return int Plan id. | |
| 301 | + */ | |
| 302 | + public static function plan( $product_file ) { | |
| 303 | + $product = Product::get( $product_file ); | |
| 304 | + $data = self::get_license_data( $product->get_key() ); | |
| 305 | + | |
| 306 | + return isset( $data->price_id ) ? (int) $data->price_id : - 1; | |
| 307 | + } | |
| 308 | + | |
| 309 | + /** | |
| 310 | + * Get product license key. | |
| 311 | + * | |
| 312 | + * @param string $product_file Product file. | |
| 313 | + * | |
| 314 | + * @return string | |
| 315 | + */ | |
| 316 | + public static function key( $product_file ) { | |
| 317 | + $product = Product::get( $product_file ); | |
| 318 | + | |
| 319 | + return $product->get_license(); | |
| 320 | + } | |
| 321 | + | |
| 322 | + /** | |
| 219 | 323 | * Return the last error message. |
| 220 | 324 | * |
| 221 | 325 | * @return mixed Error message. |
| 222 | 326 | */ |
| @@ -243,9 +347,9 @@ | ||
| 243 | 347 | * @return string Remote api url. |
| 244 | 348 | */ |
| 245 | 349 | public function get_distributor_name() { |
| 246 | 350 | if ( $this->is_from_partner( $this->product ) ) { |
| 247 | - return 'ThemeIsle'; | |
| 351 | + return 'Themeisle'; | |
| 248 | 352 | } |
| 249 | 353 | |
| 250 | 354 | return $this->product->get_store_name(); |
| 251 | 355 | } |
| @@ -255,14 +359,9 @@ | ||
| 255 | 359 | * |
| 256 | 360 | * @return int License plan. |
| 257 | 361 | */ |
| 258 | 362 | public function get_plan() { |
| 259 | - $license_data = get_option( $this->product->get_key() . '_license_data', '' ); | |
| 260 | - if ( ! isset( $license_data->price_id ) ) { | |
| 261 | - return - 1; | |
| 262 | - } | |
| 263 | - | |
| 264 | - return (int) $license_data->price_id; | |
| 363 | + return self::plan( $this->product->get_basefile() ); | |
| 265 | 364 | } |
| 266 | 365 | |
| 267 | 366 | /** |
| 268 | 367 | * Show the admin notice regarding the license status. |
| @@ -278,11 +377,11 @@ | ||
| 278 | 377 | return false; |
| 279 | 378 | } |
| 280 | 379 | |
| 281 | 380 | $status = $this->get_license_status( true ); |
| 282 | - $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.' ); | |
| 283 | - $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>. ' ); | |
| 284 | - $expired_license_string = apply_filters( $this->product->get_key() . '_lc_expired_string', 'Your %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.' ); | |
| 381 | + $no_activations_string = apply_filters( $this->product->get_key() . '_lc_no_activations_string', Loader::$labels['licenser']['no_activations'] ); | |
| 382 | + $no_valid_string = apply_filters( $this->product->get_key() . '_lc_no_valid_string', sprintf( Loader::$labels['licenser']['inactive'], '%s', '<a href="%s" target="_blank">', '</a>', '<a href="%s">', '</a>' ) ); | |
| 383 | + $expired_license_string = apply_filters( $this->product->get_key() . '_lc_expired_heading_string', Loader::$labels['licenser']['expired'] ); | |
| 285 | 384 | // No activations left for this license. |
| 286 | 385 | if ( 'valid' != $status && $this->check_activation() ) { |
| 287 | 386 | ?> |
| 288 | 387 | <div class="error"> |
| @@ -303,14 +402,74 @@ | ||
| 303 | 402 | } |
| 304 | 403 | |
| 305 | 404 | // Invalid license key. |
| 306 | 405 | if ( 'active_expired' === $status ) { |
| 406 | + // Check if the notice was dismissed. | |
| 407 | + $dismiss_option_key = $this->product->get_key() . '_expired_notice_dismissed'; | |
| 408 | + if ( get_option( $dismiss_option_key, false ) ) { | |
| 409 | + return false; | |
| 410 | + } | |
| 411 | + | |
| 412 | + $license_data = get_option( $this->product->get_key() . '_license_data', '' ); | |
| 413 | + $expiration_date = ''; | |
| 414 | + if ( is_object( $license_data ) && isset( $license_data->expires ) ) { | |
| 415 | + $timestamp = strtotime( (string) $license_data->expires ); | |
| 416 | + if ( false !== $timestamp ) { | |
| 417 | + $expiration_date = gmdate( 'F j, Y', $timestamp ); | |
| 418 | + } | |
| 419 | + } | |
| 420 | + | |
| 421 | + $discount_config = apply_filters( $this->product->get_key() . '_lc_renew_discount', false ); | |
| 422 | + | |
| 423 | + if ( is_array( $discount_config ) && isset( $discount_config['url'] ) && isset( $discount_config['renew_button'] ) ) { | |
| 424 | + $renew_url = $discount_config['url']; | |
| 425 | + $renew_button = $discount_config['renew_button']; | |
| 426 | + } else { | |
| 427 | + $renew_url = apply_filters( $this->product->get_key() . '_lc_renew_url', $this->renew_url() ); | |
| 428 | + $renew_button = apply_filters( $this->product->get_key() . '_lc_renew_button_string', Loader::$labels['licenser']['renew_license'] ); | |
| 429 | + } | |
| 430 | + | |
| 431 | + $learn_more_url = apply_filters( $this->product->get_key() . '_lc_learn_more_url', $this->get_api_url() ); | |
| 432 | + $learn_more_button = apply_filters( $this->product->get_key() . '_lc_learn_more_button_string', Loader::$labels['licenser']['learn_more'] ); | |
| 433 | + $notice_message = apply_filters( $this->product->get_key() . '_lc_expired_notice_message', Loader::$labels['licenser']['expired_notice'] ); | |
| 434 | + | |
| 435 | + $expired_date_string = apply_filters( $this->product->get_key() . '_lc_expired_date_string', sprintf( Loader::$labels['licenser']['expired_date'], esc_html( $expiration_date ) ) ); | |
| 436 | + $heading = apply_filters( $this->product->get_key() . '_lc_expired_heading_string', sprintf( Loader::$labels['licenser']['expired'], $this->product->get_name() ) ); | |
| 437 | + $notice_id = $this->product->get_key() . '_expired_notice'; | |
| 307 | 438 | ?> |
| 308 | - <div class="error"> | |
| 309 | - <p> | |
| 310 | - <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> | |
| 439 | + <div class="notice notice-warning notice-alt is-dismissible themeisle-sdk-license-notice" id="<?php echo esc_attr( $notice_id ); ?>" data-notice-id="<?php echo esc_attr( $notice_id ); ?>" style="position: relative; border-left: 4px solid #d63638; padding: 12px; background-color: #fff;"> | |
| 440 | + <p style="margin: 0.5em 0; font-size: 13px;"> | |
| 441 | + <strong><?php echo wp_kses_post( $heading ); ?></strong> | |
| 442 | + · <?php echo esc_html( $expired_date_string ); ?> | |
| 311 | 443 | </p> |
| 444 | + <p style="margin: 0.5em 0 1em 0; font-size: 13px;"> | |
| 445 | + <?php echo esc_html( $notice_message ); ?> | |
| 446 | + </p> | |
| 447 | + <p style="margin: 0.5em 0;"> | |
| 448 | + <a href="<?php echo esc_url( $renew_url ); ?>" class="button button-primary" target="_blank" rel="noopener noreferrer"> | |
| 449 | + <?php echo esc_html( $renew_button ); ?> | |
| 450 | + </a> | |
| 451 | + <a href="<?php echo esc_url( $learn_more_url ); ?>" class="button" target="_blank" rel="noopener noreferrer" style="border: none; background-color: transparent;"> | |
| 452 | + <?php echo esc_html( $learn_more_button ); ?> | |
| 453 | + </a> | |
| 454 | + </p> | |
| 312 | 455 | </div> |
| 456 | + <script type="text/javascript"> | |
| 457 | + jQuery(document).ready(function($) { | |
| 458 | + $('#<?php echo esc_js( $notice_id ); ?>').on('click', '.notice-dismiss', function(e) { | |
| 459 | + const noticeId = '<?php echo esc_js( $notice_id ); ?>'; | |
| 460 | + $.ajax({ | |
| 461 | + url: ajaxurl, | |
| 462 | + type: 'POST', | |
| 463 | + data: { | |
| 464 | + action: 'themeisle_sdk_dismiss_license_notice', | |
| 465 | + notice_id: noticeId, | |
| 466 | + nonce: '<?php echo esc_js( wp_create_nonce( 'themeisle_sdk_dismiss_license_notice' ) ); ?>' | |
| 467 | + } | |
| 468 | + }); | |
| 469 | + }); | |
| 470 | + }); | |
| 471 | + </script> | |
| 313 | 472 | <?php |
| 314 | 473 | |
| 315 | 474 | return false; |
| 316 | 475 | } |
| @@ -378,9 +537,9 @@ | ||
| 378 | 537 | if ( ! isset( $license_data->download_id ) || ! isset( $license_data->key ) ) { |
| 379 | 538 | return $this->get_api_url(); |
| 380 | 539 | } |
| 381 | 540 | |
| 382 | - return $this->get_api_url() . '/checkout/?edd_license_key=' . $license_data->key . '&download_id=' . $license_data->download_id; | |
| 541 | + return trim( $this->get_api_url(), '/' ) . '/checkout/?edd_license_key=' . $license_data->key . '&download_id=' . $license_data->download_id; | |
| 383 | 542 | } |
| 384 | 543 | |
| 385 | 544 | /** |
| 386 | 545 | * Run the license check call. |
| @@ -412,22 +571,17 @@ | ||
| 412 | 571 | $response = $this->do_license_process( $license, 'check' ); |
| 413 | 572 | |
| 414 | 573 | if ( is_wp_error( $response ) ) { |
| 415 | 574 | $license_data = new \stdClass(); |
| 416 | - $license_data->license = 'valid'; | |
| 575 | + $license_data->license = 'invalid'; | |
| 417 | 576 | } else { |
| 418 | 577 | $license_data = $response; |
| 419 | 578 | } |
| 420 | 579 | |
| 421 | 580 | $license_old = get_option( $this->product->get_key() . '_license_data', '' ); |
| 422 | - | |
| 423 | - if ( 'valid' === $license_old->license && ( $license_data->license !== $license_old->license ) ) { | |
| 581 | + if ( 'valid' === $license_old->license && ( $license_data->license !== $license_old->license ) && $this->failed_checks <= self::$max_failed ) { | |
| 424 | 582 | $this->increment_failed_checks(); |
| 425 | - } else { | |
| 426 | - $this->reset_failed_checks(); | |
| 427 | - } | |
| 428 | 583 | |
| 429 | - if ( $this->failed_checks <= self::$max_failed ) { | |
| 430 | 584 | return $license_old; |
| 431 | 585 | } |
| 432 | 586 | |
| 433 | 587 | if ( ! isset( $license_data->key ) ) { |
| @@ -432,8 +586,9 @@ | ||
| 432 | 586 | |
| 433 | 587 | if ( ! isset( $license_data->key ) ) { |
| 434 | 588 | $license_data->key = isset( $license_old->key ) ? $license_old->key : ''; |
| 435 | 589 | } |
| 590 | + $this->reset_failed_checks(); | |
| 436 | 591 | |
| 437 | 592 | return $license_data; |
| 438 | 593 | |
| 439 | 594 | } |
| @@ -447,17 +602,17 @@ | ||
| 447 | 602 | * @return bool|\WP_Error |
| 448 | 603 | */ |
| 449 | 604 | public function do_license_process( $license, $action = 'toggle' ) { |
| 450 | 605 | if ( strlen( $license ) < 10 ) { |
| 451 | - return new \WP_Error( 'themeisle-license-invalid-format', 'Invalid license.' ); | |
| 606 | + return new \WP_Error( 'themeisle-license-invalid-format', Loader::$labels['licenser']['invalid_msg'] ); | |
| 452 | 607 | } |
| 453 | 608 | $status = $this->get_license_status(); |
| 454 | 609 | |
| 455 | 610 | if ( 'valid' === $status && 'activate' === $action ) { |
| 456 | - return new \WP_Error( 'themeisle-license-already-active', 'License is already active.' ); | |
| 611 | + return new \WP_Error( 'themeisle-license-already-active', Loader::$labels['licenser']['already_active'] ); | |
| 457 | 612 | } |
| 458 | 613 | if ( 'valid' !== $status && 'deactivate' === $action ) { |
| 459 | - return new \WP_Error( 'themeisle-license-already-deactivate', 'License not active.' ); | |
| 614 | + return new \WP_Error( 'themeisle-license-already-deactivate', Loader::$labels['licenser']['not_active'] ); | |
| 460 | 615 | } |
| 461 | 616 | |
| 462 | 617 | if ( 'toggle' === $action ) { |
| 463 | 618 | $action = ( 'valid' !== $status ? ( 'activate' ) : ( 'deactivate' ) ); |
| @@ -483,15 +638,15 @@ | ||
| 483 | 638 | } |
| 484 | 639 | |
| 485 | 640 | // make sure the response came back okay. |
| 486 | 641 | if ( is_wp_error( $response ) ) { |
| 487 | - 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() ) ); | |
| 642 | + return new \WP_Error( 'themeisle-license-500', sprintf( Loader::$labels['licenser']['error_notice'], $response->get_error_message() ) ); | |
| 488 | 643 | } |
| 489 | 644 | |
| 490 | 645 | $license_data = json_decode( wp_remote_retrieve_body( $response ) ); |
| 491 | 646 | |
| 492 | 647 | if ( ! is_object( $license_data ) ) { |
| 493 | - return new \WP_Error( 'themeisle-license-404', 'ERROR: Failed to validate license. Please try again in one minute.' ); | |
| 648 | + return new \WP_Error( 'themeisle-license-404', Loader::$labels['licenser']['error_notice2'] ); | |
| 494 | 649 | } |
| 495 | 650 | if ( 'check' === $action ) { |
| 496 | 651 | return $license_data; |
| 497 | 652 | } |
| @@ -522,11 +677,15 @@ | ||
| 522 | 677 | } |
| 523 | 678 | update_option( $this->product->get_key() . '_license_data', $license_data ); |
| 524 | 679 | set_transient( $this->product->get_key() . '_license_data', $license_data, 12 * HOUR_IN_SECONDS ); |
| 525 | 680 | if ( 'activate' === $action && 'valid' !== $license_data->license ) { |
| 526 | - return new \WP_Error( 'themeisle-license-invalid', 'ERROR: Invalid license provided.' ); | |
| 681 | + return new \WP_Error( 'themeisle-license-invalid', Loader::$labels['licenser']['error_invalid'] ); | |
| 527 | 682 | } |
| 528 | 683 | |
| 684 | + // Remove the versions transient upon activation so that newer version for rollback can be acquired. | |
| 685 | + $versions_cache = $this->product->get_cache_key(); | |
| 686 | + delete_transient( $versions_cache ); | |
| 687 | + | |
| 529 | 688 | return true; |
| 530 | 689 | } |
| 531 | 690 | |
| 532 | 691 | /** |
| @@ -553,9 +712,9 @@ | ||
| 553 | 712 | if ( ! isset( $_POST[ $this->product->get_key() . '_btn_trigger' ] ) ) { |
| 554 | 713 | return; |
| 555 | 714 | } |
| 556 | 715 | if ( ! isset( $_POST[ $this->product->get_key() . 'nonce_field' ] ) |
| 557 | - || ! wp_verify_nonce( $_POST[ $this->product->get_key() . 'nonce_field' ], $this->product->get_key() . 'nonce' ) //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 716 | + || ! wp_verify_nonce( $_POST[ $this->product->get_key() . 'nonce_field' ], $this->product->get_key() . 'nonce' ) //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 558 | 717 | ) { |
| 559 | 718 | return; |
| 560 | 719 | } |
| 561 | 720 | if ( ! current_user_can( 'manage_options' ) ) { |
| @@ -603,20 +762,18 @@ | ||
| 603 | 762 | if ( false === $api_response || ! isset( $api_response->new_version ) ) { |
| 604 | 763 | return; |
| 605 | 764 | } |
| 606 | 765 | $update_url = wp_nonce_url( 'update.php?action=upgrade-theme&theme=' . urlencode( $this->product->get_slug() ), 'upgrade-theme_' . $this->product->get_slug() ); |
| 607 | - $update_message = apply_filters( 'themeisle_sdk_license_update_message', 'Updating this theme will lose any customizations you have made. Cancel to stop, OK to update.' ); | |
| 766 | + $update_message = apply_filters( 'themeisle_sdk_license_update_message', Loader::$labels['licenser']['update_license'] ); | |
| 608 | 767 | $update_onclick = ' onclick="if ( confirm(\'' . esc_js( $update_message ) . '\') ) {return true;}return false;"'; |
| 609 | 768 | if ( version_compare( $this->product->get_version(), $api_response->new_version, '<' ) ) { |
| 610 | 769 | echo '<div id="update-nag">'; |
| 611 | 770 | printf( |
| 612 | - '<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>.', | |
| 613 | - esc_attr( $theme->get( 'Name' ) ), | |
| 614 | - esc_attr( $api_response->new_version ), | |
| 615 | - esc_url( sprintf( '%s&TB_iframe=true&width=1024&height=800', $this->product->get_changelog() ) ), | |
| 616 | - esc_attr( $theme->get( 'Name' ) ), | |
| 617 | - esc_url( $update_url ), | |
| 618 | - $update_onclick // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped, Already escaped. | |
| 771 | + esc_html( Loader::$labels['licenser']['notice_update'] ), | |
| 772 | + '<strong>' . esc_attr( $theme->get( 'Name' ) ) . ' ' . esc_attr( $api_response->new_version ) . '</strong>', | |
| 773 | + '<a href="' . esc_url( sprintf( '%s&TB_iframe=true&width=1024&height=800', $this->product->get_changelog() ) ) . '" class="thickbox" title="' . esc_attr( $theme->get( 'Name' ) ) . '">', | |
| 774 | + '</a>', | |
| 775 | + '<a href="' . esc_url( $update_url ) . '" ' . $update_onclick . '>' // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped, Already escaped. | |
| 619 | 776 | ); |
| 620 | 777 | echo '</div>'; |
| 621 | 778 | echo '<div id="' . esc_attr( $this->product->get_slug() ) . '_changelog" style="display:none;">'; |
| 622 | 779 | echo wp_kses_data( wpautop( $api_response->sections['changelog'] ) ); |
| @@ -632,12 +789,22 @@ | ||
| 632 | 789 | * @return mixed |
| 633 | 790 | */ |
| 634 | 791 | public function theme_update_transient( $value ) { |
| 635 | 792 | $update_data = $this->check_for_update(); |
| 636 | - if ( $update_data ) { | |
| 637 | - $value->response[ $this->product->get_slug() ] = $update_data; | |
| 793 | + if ( empty( $value ) ) { | |
| 794 | + return $value; | |
| 638 | 795 | } |
| 639 | 796 | |
| 797 | + if ( ! isset( $value->response ) ) { | |
| 798 | + return $value; | |
| 799 | + } | |
| 800 | + | |
| 801 | + if ( ! $update_data ) { | |
| 802 | + return $value; | |
| 803 | + } | |
| 804 | + | |
| 805 | + $value->response[ $this->product->get_slug() ] = $update_data; | |
| 806 | + | |
| 640 | 807 | return $value; |
| 641 | 808 | } |
| 642 | 809 | |
| 643 | 810 | /** |
| @@ -736,17 +903,21 @@ | ||
| 736 | 903 | $this->do_check = true; |
| 737 | 904 | |
| 738 | 905 | return $_transient_data; |
| 739 | 906 | } |
| 907 | + $this->product->setup_from_fileheaders(); | |
| 740 | 908 | $api_response = $this->api_request(); |
| 741 | 909 | if ( false !== $api_response && is_object( $api_response ) && isset( $api_response->new_version ) ) { |
| 910 | + $plugin = $this->product->get_slug() . '/' . $this->product->get_file(); | |
| 742 | 911 | if ( ! isset( $api_response->plugin ) ) { |
| 743 | - $api_response->plugin = $this->product->get_slug() . '/' . $this->product->get_file(); | |
| 912 | + $api_response->plugin = $plugin; | |
| 744 | 913 | } |
| 745 | 914 | if ( version_compare( $this->product->get_version(), $api_response->new_version, '<' ) ) { |
| 746 | - $_transient_data->response[ $this->product->get_slug() . '/' . $this->product->get_file() ] = $api_response; | |
| 915 | + $_transient_data->response[ $plugin ] = $api_response; | |
| 916 | + unset( $_transient_data->no_update[ $plugin ] ); | |
| 747 | 917 | } else { |
| 748 | - $_transient_data->no_update[ $this->product->get_slug() . '/' . $this->product->get_file() ] = $api_response; | |
| 918 | + $_transient_data->no_update[ $plugin ] = $api_response; | |
| 919 | + unset( $_transient_data->response[ $plugin ] ); | |
| 749 | 920 | } |
| 750 | 921 | } |
| 751 | 922 | |
| 752 | 923 | return $_transient_data; |
| @@ -818,9 +989,9 @@ | ||
| 818 | 989 | * @return bool Should we load the module? |
| 819 | 990 | */ |
| 820 | 991 | public function can_load( $product ) { |
| 821 | 992 | |
| 822 | - if ( $product->is_wordpress_available() ) { | |
| 993 | + if ( ! $product->requires_license() ) { | |
| 823 | 994 | return false; |
| 824 | 995 | } |
| 825 | 996 | |
| 826 | 997 | return ( apply_filters( $product->get_key() . '_enable_licenser', true ) === true ); |
| @@ -843,9 +1014,15 @@ | ||
| 843 | 1014 | if ( $this->product->requires_license() ) { |
| 844 | 1015 | $this->failed_checks = intval( get_option( $this->product->get_key() . '_failed_checks', 0 ) ); |
| 845 | 1016 | $this->register_license_hooks(); |
| 846 | 1017 | } |
| 847 | - | |
| 1018 | + if ( ! self::$globals_loaded ) { | |
| 1019 | + add_filter( 'themeisle_sdk_license/status', [ __CLASS__, 'status' ], 999, 1 ); | |
| 1020 | + add_filter( 'themeisle_sdk_license/is-valid', [ __CLASS__, 'is_valid' ], 999, 1 ); | |
| 1021 | + add_filter( 'themeisle_sdk_license/plan', [ __CLASS__, 'plan' ], 999, 1 ); | |
| 1022 | + add_filter( 'themeisle_sdk_license/key', [ __CLASS__, 'key' ], 999, 1 ); | |
| 1023 | + $globals_loaded = true; | |
| 1024 | + } | |
| 848 | 1025 | $namespace = apply_filters( 'themesle_sdk_namespace_' . md5( $product->get_basefile() ), false ); |
| 849 | 1026 | |
| 850 | 1027 | if ( false !== $namespace ) { |
| 851 | 1028 | $this->namespace = $namespace; |
| @@ -861,27 +1038,61 @@ | ||
| 861 | 1038 | } |
| 862 | 1039 | |
| 863 | 1040 | add_action( 'admin_head', [ $this, 'auto_activate' ] ); |
| 864 | 1041 | if ( $this->product->is_plugin() ) { |
| 865 | - add_filter( | |
| 866 | - 'pre_set_site_transient_update_plugins', | |
| 867 | - [ | |
| 868 | - $this, | |
| 869 | - 'pre_set_site_transient_update_plugins_filter', | |
| 870 | - ] | |
| 871 | - ); | |
| 872 | - add_filter( 'plugins_api', array( $this, 'plugins_api_filter' ), 10, 3 ); | |
| 873 | - add_filter( 'http_request_args', array( $this, 'http_request_args' ), 10, 2 ); //phpcs:ignore WordPressVIPMinimum.Hooks.RestrictedHooks.http_request_args | |
| 1042 | + if ( ! $product->is_wordpress_available() ) { | |
| 1043 | + add_filter( | |
| 1044 | + 'pre_set_site_transient_update_plugins', | |
| 1045 | + [ | |
| 1046 | + $this, | |
| 1047 | + 'pre_set_site_transient_update_plugins_filter', | |
| 1048 | + ] | |
| 1049 | + ); | |
| 1050 | + add_filter( 'plugins_api', array( $this, 'plugins_api_filter' ), 10, 3 ); | |
| 1051 | + add_filter( //phpcs:ignore WordPressVIPMinimum.Hooks.RestrictedHooks.http_request_args | |
| 1052 | + 'http_request_args', | |
| 1053 | + array( | |
| 1054 | + $this, | |
| 1055 | + 'http_request_args', | |
| 1056 | + ), | |
| 1057 | + 10, | |
| 1058 | + 2 | |
| 1059 | + ); | |
| 1060 | + if ( ! self::is_valid( $product->get_basefile() ) ) { | |
| 1061 | + add_filter( | |
| 1062 | + 'plugin_action_links_' . plugin_basename( $product->get_basefile() ), | |
| 1063 | + function ( $actions ) { | |
| 1064 | + if ( $this->get_license_status( true ) !== self::STATUS_ACTIVE_EXPIRED ) { | |
| 1065 | + return $actions; | |
| 1066 | + } | |
| 1067 | + $new_actions['deactivate'] = $actions['deactivate']; | |
| 1068 | + $new_actions['renew_link'] = '<a style="color:#d63638" href="' . esc_url( $this->renew_url() ) . '" target="_blank" rel="external noopener noreferrer">' . esc_html( Loader::$labels['licenser']['renew_cta'] ) . '</a>'; | |
| 874 | 1069 | |
| 1070 | + return $new_actions; | |
| 1071 | + } | |
| 1072 | + ); | |
| 1073 | + } | |
| 1074 | + } | |
| 1075 | + | |
| 875 | 1076 | return $this; |
| 876 | 1077 | } |
| 877 | 1078 | if ( $this->product->is_theme() ) { |
| 878 | - add_filter( 'site_transient_update_themes', array( &$this, 'theme_update_transient' ) ); | |
| 879 | - add_action( 'delete_site_transient_update_themes', array( &$this, 'delete_theme_update_transient' ) ); | |
| 880 | - add_action( 'load-update-core.php', array( &$this, 'delete_theme_update_transient' ) ); | |
| 881 | - add_action( 'load-themes.php', array( &$this, 'delete_theme_update_transient' ) ); | |
| 882 | - add_action( 'load-themes.php', array( &$this, 'load_themes_screen' ) ); | |
| 883 | - add_filter( 'http_request_args', array( $this, 'disable_wporg_update' ), 5, 2 ); //phpcs:ignore WordPressVIPMinimum.Hooks.RestrictedHooks.http_request_args | |
| 1079 | + if ( ! $product->is_wordpress_available() ) { | |
| 1080 | + add_filter( 'site_transient_update_themes', array( &$this, 'theme_update_transient' ) ); | |
| 1081 | + add_action( 'delete_site_transient_update_themes', array( &$this, 'delete_theme_update_transient' ) ); | |
| 1082 | + add_action( 'load-update-core.php', array( &$this, 'delete_theme_update_transient' ) ); | |
| 1083 | + add_action( 'load-themes.php', array( &$this, 'delete_theme_update_transient' ) ); | |
| 1084 | + add_action( 'load-themes.php', array( &$this, 'load_themes_screen' ) ); | |
| 1085 | + add_filter( //phpcs:ignore WordPressVIPMinimum.Hooks.RestrictedHooks.http_request_args | |
| 1086 | + 'http_request_args', | |
| 1087 | + array( | |
| 1088 | + $this, | |
| 1089 | + 'disable_wporg_update', | |
| 1090 | + ), | |
| 1091 | + 5, | |
| 1092 | + 2 | |
| 1093 | + ); | |
| 1094 | + } | |
| 884 | 1095 | |
| 885 | 1096 | return $this; |
| 886 | 1097 | |
| 887 | 1098 | } |
| @@ -897,11 +1108,37 @@ | ||
| 897 | 1108 | add_action( 'admin_init', array( $this, 'process_license' ) ); |
| 898 | 1109 | add_action( 'admin_init', array( $this, 'product_valid' ), 99999999 ); |
| 899 | 1110 | add_action( 'admin_notices', array( $this, 'show_notice' ) ); |
| 900 | 1111 | add_filter( $this->product->get_key() . '_license_status', array( $this, 'get_license_status' ) ); |
| 1112 | + add_action( 'wp_ajax_themeisle_sdk_dismiss_license_notice', array( $this, 'dismiss_license_notice' ) ); | |
| 901 | 1113 | } |
| 902 | 1114 | |
| 903 | 1115 | /** |
| 1116 | + * Handle AJAX request to dismiss the license notice. | |
| 1117 | + */ | |
| 1118 | + public function dismiss_license_notice() { | |
| 1119 | + if ( ! isset( $_POST['nonce'] ) || ! wp_verify_nonce( sanitize_text_field( $_POST['nonce'] ), 'themeisle_sdk_dismiss_license_notice' ) ) { | |
| 1120 | + wp_send_json_error( 'Invalid nonce' ); | |
| 1121 | + } | |
| 1122 | + | |
| 1123 | + if ( ! current_user_can( 'manage_options' ) ) { | |
| 1124 | + wp_send_json_error( 'Insufficient permissions' ); | |
| 1125 | + } | |
| 1126 | + | |
| 1127 | + $notice_id = isset( $_POST['notice_id'] ) ? sanitize_text_field( $_POST['notice_id'] ) : ''; | |
| 1128 | + | |
| 1129 | + if ( empty( $notice_id ) ) { | |
| 1130 | + wp_send_json_error( 'Missing notice ID' ); | |
| 1131 | + } | |
| 1132 | + | |
| 1133 | + // Save the dismissal option. | |
| 1134 | + $dismiss_option_key = $notice_id . '_dismissed'; | |
| 1135 | + update_option( $dismiss_option_key, true ); | |
| 1136 | + | |
| 1137 | + wp_send_json_success(); | |
| 1138 | + } | |
| 1139 | + | |
| 1140 | + /** | |
| 904 | 1141 | * Check license on filesystem. |
| 905 | 1142 | * |
| 906 | 1143 | * @return mixed License key. |
| 907 | 1144 | */ |
| @@ -922,10 +1159,12 @@ | ||
| 922 | 1159 | } |
| 923 | 1160 | if ( ! isset( $content->key ) ) { |
| 924 | 1161 | return false; |
| 925 | 1162 | } |
| 1163 | + | |
| 926 | 1164 | return $content->key; |
| 927 | 1165 | } |
| 1166 | + | |
| 928 | 1167 | /** |
| 929 | 1168 | * Run license activation on plugin activate. |
| 930 | 1169 | */ |
| 931 | 1170 | public function auto_activate() { |
| @@ -973,9 +1212,9 @@ | ||
| 973 | 1212 | */ |
| 974 | 1213 | public function autoactivate_notice() { |
| 975 | 1214 | ?> |
| 976 | 1215 | <div class="notice notice-success is-dismissible"> |
| 977 | - <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> | |
| 1216 | + <p><?php echo sprintf( esc_html( Loader::$labels['licenser']['autoactivate_notice'] ), '<strong>' . esc_attr( $this->product->get_name() ) . '</strong>', '<strong>' . esc_attr( str_repeat( '*', 20 ) . substr( $this->license_local, - 10 ) ) . '</strong>' ); ?></p> | |
| 978 | 1217 | </div> |
| 979 | 1218 | <?php |
| 980 | 1219 | } |
| 981 | 1220 | |