← All changes
|
vendor/codeinwp/themeisle-sdk/src/Modules/Licenser.php
+122
-43
3.1.3
→
3.4.2
View file →
| @@ -88,8 +88,11 @@ | ||
| 88 | 88 | public function register_settings() { |
| 89 | 89 | if ( ! is_admin() ) { |
| 90 | 90 | return false; |
| 91 | 91 | } |
| 92 | + if ( apply_filters( $this->product->get_key() . '_hide_license_field', false ) ) { | |
| 93 | + return; | |
| 94 | + } | |
| 92 | 95 | add_settings_field( |
| 93 | 96 | $this->product->get_key() . '_license', |
| 94 | 97 | $this->product->get_name() . ' license', |
| 95 | 98 | array( $this, 'license_view' ), |
| @@ -108,11 +111,73 @@ | ||
| 108 | 111 | $deactivate_string = apply_filters( $this->product->get_key() . '_lc_deactivate_string', 'Deactivate' ); |
| 109 | 112 | $valid_string = apply_filters( $this->product->get_key() . '_lc_valid_string', 'Valid' ); |
| 110 | 113 | $invalid_string = apply_filters( $this->product->get_key() . '_lc_invalid_string', 'Invalid' ); |
| 111 | 114 | $license_message = apply_filters( $this->product->get_key() . '_lc_license_message', 'Enter your license from %s purchase history in order to get %s updates' ); |
| 115 | + $error_message = $this->get_error(); | |
| 116 | + ?> | |
| 117 | + <style type="text/css"> | |
| 118 | + input.themeisle-sdk-text-input-valid { | |
| 119 | + border: 1px solid #7ad03a; | |
| 120 | + } | |
| 112 | 121 | |
| 113 | - echo '<p ><input ' . ( ( 'valid' === $status ) ? ( 'style="border:1px solid #7ad03a; "' ) : '' ) . ' type="text" id="' . $this->product->get_key() . '_license" name="' . $this->product->get_key() . '_license" value="' . $value . '" /><a ' . ( ( 'valid' === $status ) ? ( 'style="color:#fff;background: #7ad03a; display: inline-block;text-decoration: none;font-size: 13px;line-height: 26px;height: 26px; margin-left:5px; padding: 0 10px 1px; -webkit-border-radius: 3px;border-radius: 3px; ">' . $valid_string ) : ( 'style="color:#fff;background: #dd3d36; display: inline-block;text-decoration: none;font-size: 13px;line-height: 26px;height: 26px; margin-left:5px; padding: 0 10px 1px; -webkit-border-radius: 3px;border-radius: 3px; ">' . $invalid_string ) ) . ' </a> <button name="' . $this->product->get_key() . '_btn_trigger" ' . ( ( 'valid' === $status ) ? ( ' class="button button-primary">' . $deactivate_string ) : ( ' class="button button-primary" value="yes" type="submit" >' . $activate_string ) ) . ' </button></p><p class="description">' . sprintf( $license_message, '<a href="' . $this->get_api_url() . '">' . $this->get_distributor_name() . '</a> ', $this->product->get_type() ) . '</p>'; | |
| 122 | + input.themeisle-sdk-license-input { | |
| 123 | + width: 300px; | |
| 124 | + padding: 0 8px; | |
| 125 | + line-height: 2; | |
| 126 | + min-height: 30px; | |
| 127 | + } | |
| 114 | 128 | |
| 129 | + .themeisle-sdk-license-deactivate-cta { | |
| 130 | + color: #fff; | |
| 131 | + background: #7ad03a; | |
| 132 | + display: inline-block; | |
| 133 | + text-decoration: none; | |
| 134 | + font-size: 13px; | |
| 135 | + line-height: 30px; | |
| 136 | + height: 26px; | |
| 137 | + margin-left: 5px; | |
| 138 | + padding: 0 10px 3px; | |
| 139 | + -webkit-border-radius: 3px; | |
| 140 | + border-radius: 3px; | |
| 141 | + } | |
| 142 | + | |
| 143 | + .themeisle-sdk-license-activate-cta { | |
| 144 | + color: #fff; | |
| 145 | + background: #dd3d36; | |
| 146 | + display: inline-block; | |
| 147 | + text-decoration: none; | |
| 148 | + font-size: 13px; | |
| 149 | + line-height: 30px; | |
| 150 | + height: 26px; | |
| 151 | + margin-left: 5px; | |
| 152 | + padding: 0 10px 3px; | |
| 153 | + -webkit-border-radius: 3px; | |
| 154 | + border-radius: 3px; | |
| 155 | + } | |
| 156 | + | |
| 157 | + button.button.themeisle-sdk-licenser-button-cta { | |
| 158 | + line-height: 26px; | |
| 159 | + height: 29px; | |
| 160 | + vertical-align: top; | |
| 161 | + } | |
| 162 | + | |
| 163 | + </style> | |
| 164 | + <?php | |
| 165 | + echo sprintf( | |
| 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> <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', | |
| 167 | + ( ( 'valid' === $status ) ? sprintf( '<input type="hidden" value="%s" name="%s_license" />', $value, $this->product->get_key() ) : '' ), | |
| 168 | + ( ( 'valid' === $status ) ? 'themeisle-sdk-text-input-valid' : '' ), | |
| 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 | + ); | |
| 179 | + | |
| 115 | 180 | } |
| 116 | 181 | |
| 117 | 182 | /** |
| 118 | 183 | * Return the license status. |
| @@ -165,12 +230,15 @@ | ||
| 165 | 230 | function show_notice() { |
| 166 | 231 | if ( ! is_admin() ) { |
| 167 | 232 | return false; |
| 168 | 233 | } |
| 234 | + | |
| 235 | + if ( apply_filters( $this->product->get_key() . '_hide_license_notices', false ) ) { | |
| 236 | + return; | |
| 237 | + } | |
| 169 | 238 | $status = $this->get_license_status(); |
| 170 | 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.' ); |
| 171 | 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>. ' ); |
| 172 | - $expiration_string = apply_filters( $this->product->get_key() . '_lc_expiration_string', 'Your license is about to expire for %s. You can go to %s and renew it ' ); | |
| 173 | 241 | |
| 174 | 242 | // No activations left for this license. |
| 175 | 243 | if ( 'valid' != $status && $this->check_activation() ) { |
| 176 | 244 | ?> |
| @@ -194,9 +262,9 @@ | ||
| 194 | 262 | if ( 'valid' != $status ) { |
| 195 | 263 | ?> |
| 196 | 264 | <div class="error"> |
| 197 | 265 | <p> |
| 198 | - <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() ); ?> </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> | |
| 199 | 267 | </p> |
| 200 | 268 | </div> |
| 201 | 269 | <?php |
| 202 | 270 | |
| @@ -202,28 +270,8 @@ | ||
| 202 | 270 | |
| 203 | 271 | return false; |
| 204 | 272 | } |
| 205 | 273 | |
| 206 | - // Expired and soon to expire license. | |
| 207 | - if ( 'valid' == $status && $this->check_expiration() ) { | |
| 208 | - ?> | |
| 209 | - <div class="update-nag"> | |
| 210 | - <p> | |
| 211 | - <strong> | |
| 212 | - <?php | |
| 213 | - echo sprintf( | |
| 214 | - $expiration_string, | |
| 215 | - $this->product->get_name() . ' ' . $this->product->get_type(), | |
| 216 | - '<a href="' . $this->renew_url() . '" target="_blank">' . $this->get_distributor_name() . '</a>' | |
| 217 | - ); | |
| 218 | - ?> | |
| 219 | - </strong> | |
| 220 | - </p> | |
| 221 | - </div> | |
| 222 | - <?php | |
| 223 | - return false; | |
| 224 | - } | |
| 225 | - | |
| 226 | 274 | return true; |
| 227 | 275 | } |
| 228 | 276 | |
| 229 | 277 | /** |
| @@ -376,8 +424,28 @@ | ||
| 376 | 424 | update_option( $this->product->get_key() . '_failed_checks', $this->failed_checks ); |
| 377 | 425 | } |
| 378 | 426 | |
| 379 | 427 | /** |
| 428 | + * Set license validation error message. | |
| 429 | + * | |
| 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. | |
| 440 | + * | |
| 441 | + * @return mixed Error message. | |
| 442 | + */ | |
| 443 | + public function get_error() { | |
| 444 | + return get_transient( $this->product->get_key() . 'act_err' ); | |
| 445 | + } | |
| 446 | + | |
| 447 | + /** | |
| 380 | 448 | * Activate the license remotely. |
| 381 | 449 | */ |
| 382 | 450 | function activate_license() { |
| 383 | 451 | // listen for our activate button to be clicked. |
| @@ -401,21 +469,24 @@ | ||
| 401 | 469 | // Call the custom API. |
| 402 | 470 | $response = wp_remote_get( add_query_arg( $api_params, $this->get_api_url() ) ); |
| 403 | 471 | // make sure the response came back okay. |
| 404 | 472 | if ( is_wp_error( $response ) ) { |
| 405 | - $license_data = new \stdClass(); | |
| 406 | - $license_data->license = ( 'valid' != $status ) ? 'valid' : 'invalid'; | |
| 473 | + $this->set_error( sprintf( 'ERROR: Failed to connect to the license service. Please try again later. Reason: %s', $response->get_error_message() ) ); | |
| 407 | 474 | |
| 408 | - } else { | |
| 409 | - $license_data = json_decode( wp_remote_retrieve_body( $response ) ); | |
| 410 | - if ( ! is_object( $license_data ) ) { | |
| 411 | - $license_data = new \stdClass(); | |
| 412 | - $license_data->license = ( 'valid' != $status ) ? 'valid' : 'invalid'; | |
| 413 | - } | |
| 414 | - if ( ! isset( $license_data->license ) ) { | |
| 415 | - $license_data->license = 'invalid'; | |
| 416 | - } | |
| 475 | + return; | |
| 417 | 476 | } |
| 477 | + | |
| 478 | + $license_data = json_decode( wp_remote_retrieve_body( $response ) ); | |
| 479 | + | |
| 480 | + if ( ! is_object( $license_data ) ) { | |
| 481 | + $this->set_error( 'ERROR: Failed to validate license. Please try again in one minute.' ); | |
| 482 | + | |
| 483 | + return; | |
| 484 | + } | |
| 485 | + if ( ! isset( $license_data->license ) ) { | |
| 486 | + $license_data->license = 'invalid'; | |
| 487 | + } | |
| 488 | + | |
| 418 | 489 | if ( ! isset( $license_data->key ) ) { |
| 419 | 490 | $license_data->key = $license; |
| 420 | 491 | } |
| 421 | 492 | if ( 'valid' == $license_data->license ) { |
| @@ -421,12 +492,21 @@ | ||
| 421 | 492 | if ( 'valid' == $license_data->license ) { |
| 422 | 493 | $this->reset_failed_checks(); |
| 423 | 494 | } |
| 424 | 495 | |
| 496 | + $this->set_error( '' ); | |
| 497 | + | |
| 498 | + if ( 'deactivate_license' === $api_params['edd_action'] ) { | |
| 499 | + | |
| 500 | + delete_option( $this->product->get_key() . '_license_data' ); | |
| 501 | + delete_option( $this->product->get_key() . '_license_plan' ); | |
| 502 | + delete_transient( $this->product->get_key() . '_license_data' ); | |
| 503 | + | |
| 504 | + return; | |
| 505 | + } | |
| 425 | 506 | if ( isset( $license_data->plan ) ) { |
| 426 | 507 | update_option( $this->product->get_key() . '_license_plan', $license_data->plan ); |
| 427 | 508 | } |
| 428 | - | |
| 429 | 509 | update_option( $this->product->get_key() . '_license_data', $license_data ); |
| 430 | 510 | set_transient( $this->product->get_key() . '_license_data', $license_data, 12 * HOUR_IN_SECONDS ); |
| 431 | 511 | |
| 432 | 512 | } |
| @@ -444,9 +524,9 @@ | ||
| 444 | 524 | */ |
| 445 | 525 | function update_nag() { |
| 446 | 526 | $theme = wp_get_theme( $this->product->get_slug() ); |
| 447 | 527 | $api_response = get_transient( $this->product_key ); |
| 448 | - if ( false === $api_response ) { | |
| 528 | + if ( false === $api_response || ! isset( $api_response->new_version ) ) { | |
| 449 | 529 | return; |
| 450 | 530 | } |
| 451 | 531 | $update_url = wp_nonce_url( 'update.php?action=upgrade-theme&theme=' . urlencode( $this->product->get_slug() ), 'upgrade-theme_' . $this->product->get_slug() ); |
| 452 | 532 | $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.' ); |
| @@ -456,9 +536,9 @@ | ||
| 456 | 536 | printf( |
| 457 | 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>.', |
| 458 | 538 | $theme->get( 'Name' ), |
| 459 | 539 | $api_response->new_version, |
| 460 | - '#TB_inline?width=640&inlineId=' . $this->product->get_version() . '_changelog', | |
| 540 | + sprintf( '%s&TB_iframe=true&width=1024&height=800', $this->product->get_changelog() ), | |
| 461 | 541 | $theme->get( 'Name' ), |
| 462 | 542 | $update_url, |
| 463 | 543 | $update_onclick |
| 464 | 544 | ); |
| @@ -506,9 +586,9 @@ | ||
| 506 | 586 | set_transient( $this->product_key, $data, 30 * MINUTE_IN_SECONDS ); |
| 507 | 587 | |
| 508 | 588 | return false; |
| 509 | 589 | } |
| 510 | - $update_data->sections = maybe_unserialize( $update_data->sections ); | |
| 590 | + $update_data->sections = isset( $update_data->sections ) ? maybe_unserialize( $update_data->sections ) : null; | |
| 511 | 591 | |
| 512 | 592 | set_transient( $this->product_key, $update_data, 12 * HOUR_IN_SECONDS ); |
| 513 | 593 | } |
| 514 | 594 | if ( ! isset( $update_data->new_version ) ) { |
| @@ -529,20 +609,19 @@ | ||
| 529 | 609 | private function get_version_data() { |
| 530 | 610 | $api_params = array( |
| 531 | 611 | 'edd_action' => 'get_version', |
| 532 | 612 | 'version' => $this->product->get_version(), |
| 533 | - 'license' => $this->license_key, | |
| 534 | - 'name' => $this->product->get_name(), | |
| 613 | + 'license' => empty( $this->license_key ) ? 'free' : $this->license_key, | |
| 614 | + 'name' => rawurlencode( $this->product->get_name() ), | |
| 535 | 615 | 'slug' => $this->product->get_slug(), |
| 536 | - 'author' => $this->get_distributor_name(), | |
| 616 | + 'author' => rawurlencode( $this->get_distributor_name() ), | |
| 537 | 617 | 'url' => rawurlencode( home_url() ), |
| 538 | 618 | ); |
| 539 | 619 | $response = wp_remote_get( |
| 540 | - $this->get_api_url(), | |
| 620 | + add_query_arg( $api_params, $this->get_api_url() ), | |
| 541 | 621 | array( |
| 542 | 622 | 'timeout' => 15, |
| 543 | 623 | 'sslverify' => false, |
| 544 | - 'body' => $api_params, | |
| 545 | 624 | ) |
| 546 | 625 | ); |
| 547 | 626 | if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) ) { |
| 548 | 627 | return false; |