← All changes
|
vendor/codeinwp/themeisle-sdk/src/Modules/Licenser.php
+390
-106
3.1.3
→
3.4.9
View file →
| @@ -12,8 +12,9 @@ | ||
| 12 | 12 | namespace ThemeisleSDK\Modules; |
| 13 | 13 | |
| 14 | 14 | // Exit if accessed directly. |
| 15 | 15 | use ThemeisleSDK\Common\Abstract_Module; |
| 16 | +use ThemeisleSDK\Loader; | |
| 16 | 17 | use ThemeisleSDK\Product; |
| 17 | 18 | |
| 18 | 19 | if ( ! defined( 'ABSPATH' ) ) { |
| 19 | 20 | exit; |
| @@ -55,8 +56,15 @@ | ||
| 55 | 56 | */ |
| 56 | 57 | private $product_key; |
| 57 | 58 | |
| 58 | 59 | /** |
| 60 | + * Holds local license object. | |
| 61 | + * | |
| 62 | + * @var null Local license object. | |
| 63 | + */ | |
| 64 | + private $license_local = null; | |
| 65 | + | |
| 66 | + /** | |
| 59 | 67 | * Disable wporg updates for premium products. |
| 60 | 68 | * |
| 61 | 69 | * @param string $r Update payload. |
| 62 | 70 | * @param string $url The api url. |
| @@ -88,8 +96,11 @@ | ||
| 88 | 96 | public function register_settings() { |
| 89 | 97 | if ( ! is_admin() ) { |
| 90 | 98 | return false; |
| 91 | 99 | } |
| 100 | + if ( apply_filters( $this->product->get_key() . '_hide_license_field', false ) ) { | |
| 101 | + return; | |
| 102 | + } | |
| 92 | 103 | add_settings_field( |
| 93 | 104 | $this->product->get_key() . '_license', |
| 94 | 105 | $this->product->get_name() . ' license', |
| 95 | 106 | array( $this, 'license_view' ), |
| @@ -108,19 +119,83 @@ | ||
| 108 | 119 | $deactivate_string = apply_filters( $this->product->get_key() . '_lc_deactivate_string', 'Deactivate' ); |
| 109 | 120 | $valid_string = apply_filters( $this->product->get_key() . '_lc_valid_string', 'Valid' ); |
| 110 | 121 | $invalid_string = apply_filters( $this->product->get_key() . '_lc_invalid_string', 'Invalid' ); |
| 111 | 122 | $license_message = apply_filters( $this->product->get_key() . '_lc_license_message', 'Enter your license from %s purchase history in order to get %s updates' ); |
| 123 | + $error_message = $this->get_error(); | |
| 124 | + ?> | |
| 125 | + <style type="text/css"> | |
| 126 | + input.themeisle-sdk-text-input-valid { | |
| 127 | + border: 1px solid #7ad03a; | |
| 128 | + } | |
| 112 | 129 | |
| 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>'; | |
| 130 | + input.themeisle-sdk-license-input { | |
| 131 | + width: 300px; | |
| 132 | + padding: 0 8px; | |
| 133 | + line-height: 2; | |
| 134 | + min-height: 30px; | |
| 135 | + } | |
| 114 | 136 | |
| 137 | + .themeisle-sdk-license-deactivate-cta { | |
| 138 | + color: #fff; | |
| 139 | + background: #7ad03a; | |
| 140 | + display: inline-block; | |
| 141 | + text-decoration: none; | |
| 142 | + font-size: 13px; | |
| 143 | + line-height: 30px; | |
| 144 | + height: 26px; | |
| 145 | + margin-left: 5px; | |
| 146 | + padding: 0 10px 3px; | |
| 147 | + -webkit-border-radius: 3px; | |
| 148 | + border-radius: 3px; | |
| 149 | + } | |
| 150 | + | |
| 151 | + .themeisle-sdk-license-activate-cta { | |
| 152 | + color: #fff; | |
| 153 | + background: #dd3d36; | |
| 154 | + display: inline-block; | |
| 155 | + text-decoration: none; | |
| 156 | + font-size: 13px; | |
| 157 | + line-height: 30px; | |
| 158 | + height: 26px; | |
| 159 | + margin-left: 5px; | |
| 160 | + padding: 0 10px 3px; | |
| 161 | + -webkit-border-radius: 3px; | |
| 162 | + border-radius: 3px; | |
| 163 | + } | |
| 164 | + | |
| 165 | + button.button.themeisle-sdk-licenser-button-cta { | |
| 166 | + line-height: 26px; | |
| 167 | + height: 29px; | |
| 168 | + vertical-align: top; | |
| 169 | + } | |
| 170 | + | |
| 171 | + </style> | |
| 172 | + <?php | |
| 173 | + echo sprintf( | |
| 174 | + '<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', | |
| 175 | + ( ( 'valid' === $status ) ? sprintf( '<input type="hidden" value="%s" name="%s_license" />', $value, $this->product->get_key() ) : '' ), | |
| 176 | + ( ( 'valid' === $status ) ? 'themeisle-sdk-text-input-valid' : '' ), | |
| 177 | + $this->product->get_key(), | |
| 178 | + ( ( 'valid' === $status ) ? $this->product->get_key() . '_mask' : $this->product->get_key() ), | |
| 179 | + ( ( 'valid' === $status ) ? ( str_repeat( '*', 30 ) . substr( $value, - 5 ) ) : $value ), | |
| 180 | + ( 'valid' === $status ? 'themeisle-sdk-license-deactivate-cta' : 'themeisle-sdk-license-activate-cta' ), | |
| 181 | + ( 'valid' === $status ? $valid_string : $invalid_string ), | |
| 182 | + $this->product->get_key(), | |
| 183 | + ( 'valid' === $status ? $deactivate_string : $activate_string ), | |
| 184 | + sprintf( $license_message, '<a href="' . $this->get_api_url() . '">' . $this->get_distributor_name() . '</a> ', $this->product->get_type() ), | |
| 185 | + empty( $error_message ) ? '' : sprintf( '<p style="color:#dd3d36">%s</p>', $error_message ) | |
| 186 | + ); | |
| 187 | + | |
| 115 | 188 | } |
| 116 | 189 | |
| 117 | 190 | /** |
| 118 | 191 | * Return the license status. |
| 119 | 192 | * |
| 193 | + * @param bool $check_expiration Should check if license is valid, but expired. | |
| 194 | + * | |
| 120 | 195 | * @return string The License status. |
| 121 | 196 | */ |
| 122 | - public function get_license_status() { | |
| 197 | + public function get_license_status( $check_expiration = false ) { | |
| 123 | 198 | |
| 124 | 199 | $license_data = get_option( $this->product->get_key() . '_license_data', '' ); |
| 125 | 200 | |
| 126 | 201 | if ( '' === $license_data ) { |
| @@ -125,11 +200,28 @@ | ||
| 125 | 200 | |
| 126 | 201 | if ( '' === $license_data ) { |
| 127 | 202 | return get_option( $this->product->get_key() . '_license_status', 'not_active' ); |
| 128 | 203 | } |
| 204 | + $status = isset( $license_data->license ) ? $license_data->license : get_option( $this->product->get_key() . '_license_status', 'not_active' ); | |
| 205 | + if ( false === $check_expiration ) { | |
| 206 | + return $status; | |
| 207 | + } | |
| 129 | 208 | |
| 130 | - return isset( $license_data->license ) ? $license_data->license : get_option( $this->product->get_key() . '_license_status', 'not_active' ); | |
| 209 | + return ( 'valid' === $status && isset( $license_data->is_expired ) && 'yes' === $license_data->is_expired ) ? 'active_expired' : $status; | |
| 210 | + } | |
| 131 | 211 | |
| 212 | + /** | |
| 213 | + * License price id. | |
| 214 | + * | |
| 215 | + * @return int License plan. | |
| 216 | + */ | |
| 217 | + public function get_plan() { | |
| 218 | + $license_data = get_option( $this->product->get_key() . '_license_data', '' ); | |
| 219 | + if ( ! isset( $license_data->price_id ) ) { | |
| 220 | + return -1; | |
| 221 | + } | |
| 222 | + | |
| 223 | + return (int) $license_data->price_id; | |
| 132 | 224 | } |
| 133 | 225 | |
| 134 | 226 | /** |
| 135 | 227 | * Get remote api url. |
| @@ -165,13 +257,17 @@ | ||
| 165 | 257 | function show_notice() { |
| 166 | 258 | if ( ! is_admin() ) { |
| 167 | 259 | return false; |
| 168 | 260 | } |
| 169 | - $status = $this->get_license_status(); | |
| 170 | - $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 | - $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 | 261 | |
| 262 | + if ( apply_filters( $this->product->get_key() . '_hide_license_notices', false ) ) { | |
| 263 | + return false; | |
| 264 | + } | |
| 265 | + | |
| 266 | + $status = $this->get_license_status( true ); | |
| 267 | + $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.' ); | |
| 268 | + $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>. ' ); | |
| 269 | + $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.' ); | |
| 174 | 270 | // No activations left for this license. |
| 175 | 271 | if ( 'valid' != $status && $this->check_activation() ) { |
| 176 | 272 | ?> |
| 177 | 273 | <div class="error"> |
| @@ -189,14 +285,15 @@ | ||
| 189 | 285 | </div> |
| 190 | 286 | <?php |
| 191 | 287 | return false; |
| 192 | 288 | } |
| 289 | + | |
| 193 | 290 | // Invalid license key. |
| 194 | - if ( 'valid' != $status ) { | |
| 291 | + if ( 'active_expired' === $status ) { | |
| 195 | 292 | ?> |
| 196 | 293 | <div class="error"> |
| 197 | 294 | <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> | |
| 295 | + <strong><?php echo sprintf( $expired_license_string, $this->product->get_name() . ' ' . $this->product->get_type(), $this->get_api_url() . '?license=' . $this->license_key ); ?> </strong> | |
| 199 | 296 | </p> |
| 200 | 297 | </div> |
| 201 | 298 | <?php |
| 202 | 299 | |
| @@ -201,26 +298,18 @@ | ||
| 201 | 298 | <?php |
| 202 | 299 | |
| 203 | 300 | return false; |
| 204 | 301 | } |
| 205 | - | |
| 206 | - // Expired and soon to expire license. | |
| 207 | - if ( 'valid' == $status && $this->check_expiration() ) { | |
| 302 | + // Invalid license key. | |
| 303 | + if ( 'valid' != $status ) { | |
| 208 | 304 | ?> |
| 209 | - <div class="update-nag"> | |
| 305 | + <div class="error"> | |
| 210 | 306 | <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> | |
| 307 | + <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> | |
| 220 | 308 | </p> |
| 221 | 309 | </div> |
| 222 | 310 | <?php |
| 311 | + | |
| 223 | 312 | return false; |
| 224 | 313 | } |
| 225 | 314 | |
| 226 | 315 | return true; |
| @@ -236,9 +325,9 @@ | ||
| 236 | 325 | if ( '' === $license_data ) { |
| 237 | 326 | return false; |
| 238 | 327 | } |
| 239 | 328 | |
| 240 | - return isset( $license_data->error ) ? ( 'no_activations_left' == $license_data->error ) : false; | |
| 329 | + return isset( $license_data->license ) ? ( 'no_activations_left' == $license_data->license ) : false; | |
| 241 | 330 | |
| 242 | 331 | } |
| 243 | 332 | |
| 244 | 333 | /** |
| @@ -296,42 +385,28 @@ | ||
| 296 | 385 | * @return object The license data. |
| 297 | 386 | */ |
| 298 | 387 | public function check_license() { |
| 299 | 388 | $status = $this->get_license_status(); |
| 300 | - if ( 'not_active' == $status ) { | |
| 389 | + if ( 'not_active' === $status ) { | |
| 301 | 390 | $license_data = new \stdClass(); |
| 302 | 391 | $license_data->license = 'not_active'; |
| 303 | 392 | |
| 304 | 393 | return $license_data; |
| 305 | 394 | } |
| 306 | - $license = trim( $this->license_key ); | |
| 307 | - $api_params = array( | |
| 308 | - 'edd_action' => 'check_license', | |
| 309 | - 'license' => $license, | |
| 310 | - 'item_name' => rawurlencode( $this->product->get_name() ), | |
| 311 | - 'url' => rawurlencode( home_url() ), | |
| 312 | - ); | |
| 313 | - // Call the custom API. | |
| 314 | - $response = wp_remote_get( | |
| 315 | - add_query_arg( $api_params, $this->get_api_url() ), | |
| 316 | - array( | |
| 317 | - 'timeout' => 15, | |
| 318 | - 'sslverify' => false, | |
| 319 | - ) | |
| 320 | - ); | |
| 395 | + $license = trim( $this->license_key ); | |
| 396 | + | |
| 397 | + $response = $this->do_license_process( $license, 'check' ); | |
| 398 | + | |
| 321 | 399 | if ( is_wp_error( $response ) ) { |
| 322 | 400 | $license_data = new \stdClass(); |
| 323 | 401 | $license_data->license = 'valid'; |
| 324 | - | |
| 325 | 402 | } else { |
| 326 | - $license_data = json_decode( wp_remote_retrieve_body( $response ) ); | |
| 327 | - if ( ! is_object( $license_data ) ) { | |
| 328 | - $license_data = new \stdClass(); | |
| 329 | - $license_data->license = 'valid'; | |
| 330 | - } | |
| 403 | + $license_data = $response; | |
| 331 | 404 | } |
| 405 | + | |
| 332 | 406 | $license_old = get_option( $this->product->get_key() . '_license_data', '' ); |
| 333 | - if ( 'valid' == $license_old->license && ( $license_data->license != $license_old->license ) ) { | |
| 407 | + | |
| 408 | + if ( 'valid' === $license_old->license && ( $license_data->license !== $license_old->license ) ) { | |
| 334 | 409 | $this->increment_failed_checks(); |
| 335 | 410 | } else { |
| 336 | 411 | $this->reset_failed_checks(); |
| 337 | 412 | } |
| @@ -339,24 +414,12 @@ | ||
| 339 | 414 | if ( $this->failed_checks <= self::$max_failed ) { |
| 340 | 415 | return $license_old; |
| 341 | 416 | } |
| 342 | 417 | |
| 343 | - if ( isset( $license_old->hide_valid ) ) { | |
| 344 | - $license_data->hide_valid = true; | |
| 345 | - } | |
| 346 | - | |
| 347 | 418 | if ( ! isset( $license_data->key ) ) { |
| 348 | 419 | $license_data->key = isset( $license_old->key ) ? $license_old->key : ''; |
| 349 | 420 | } |
| 350 | 421 | |
| 351 | - if ( isset( $license_old->hide_expiration ) ) { | |
| 352 | - $license_data->hide_expiration = true; | |
| 353 | - } | |
| 354 | - | |
| 355 | - if ( isset( $license_old->hide_activation ) ) { | |
| 356 | - $license_data->hide_activation = true; | |
| 357 | - } | |
| 358 | - | |
| 359 | 422 | return $license_data; |
| 360 | 423 | |
| 361 | 424 | } |
| 362 | 425 | |
| @@ -376,63 +439,141 @@ | ||
| 376 | 439 | update_option( $this->product->get_key() . '_failed_checks', $this->failed_checks ); |
| 377 | 440 | } |
| 378 | 441 | |
| 379 | 442 | /** |
| 380 | - * Activate the license remotely. | |
| 443 | + * Set license validation error message. | |
| 444 | + * | |
| 445 | + * @param string $message Error message. | |
| 381 | 446 | */ |
| 382 | - function activate_license() { | |
| 383 | - // listen for our activate button to be clicked. | |
| 384 | - if ( ! isset( $_POST[ $this->product->get_key() . '_btn_trigger' ] ) ) { | |
| 385 | - return; | |
| 447 | + public function set_error( $message = '' ) { | |
| 448 | + set_transient( $this->product->get_key() . 'act_err', $message, MINUTE_IN_SECONDS ); | |
| 449 | + | |
| 450 | + return; | |
| 451 | + } | |
| 452 | + | |
| 453 | + /** | |
| 454 | + * Return the last error message. | |
| 455 | + * | |
| 456 | + * @return mixed Error message. | |
| 457 | + */ | |
| 458 | + public function get_error() { | |
| 459 | + return get_transient( $this->product->get_key() . 'act_err' ); | |
| 460 | + } | |
| 461 | + | |
| 462 | + /** | |
| 463 | + * Do license activation/deactivation. | |
| 464 | + * | |
| 465 | + * @param string $license License key. | |
| 466 | + * @param string $action What do to. | |
| 467 | + * | |
| 468 | + * @return bool|\WP_Error | |
| 469 | + */ | |
| 470 | + public function do_license_process( $license, $action = 'toggle' ) { | |
| 471 | + if ( strlen( $license ) < 10 ) { | |
| 472 | + return new \WP_Error( 'themeisle-license-invalid-format', 'Invalid license.' ); | |
| 386 | 473 | } |
| 387 | 474 | $status = $this->get_license_status(); |
| 388 | - // retrieve the license from the database. | |
| 389 | - $license = $_POST[ $this->product->get_key() . '_license' ]; | |
| 390 | - $api_params = array( | |
| 391 | - 'license' => $license, | |
| 392 | - 'item_name' => rawurlencode( $this->product->get_name() ), | |
| 393 | - 'url' => rawurlencode( home_url() ), | |
| 394 | - ); | |
| 395 | - if ( 'valid' != $status ) { | |
| 396 | - // data to send in our API request. | |
| 397 | - $api_params['edd_action'] = 'activate_license'; | |
| 475 | + | |
| 476 | + if ( 'valid' === $status && 'activate' === $action ) { | |
| 477 | + return new \WP_Error( 'themeisle-license-already-active', 'License is already active.' ); | |
| 478 | + } | |
| 479 | + if ( 'valid' !== $status && 'deactivate' === $action ) { | |
| 480 | + return new \WP_Error( 'themeisle-license-already-deactivate', 'License not active.' ); | |
| 481 | + } | |
| 482 | + | |
| 483 | + if ( 'toggle' === $action ) { | |
| 484 | + $action = ( 'valid' !== $status ? ( 'activate' ) : ( 'deactivate' ) ); | |
| 485 | + } | |
| 486 | + | |
| 487 | + // Call the custom API. | |
| 488 | + if ( 'check' === $action ) { | |
| 489 | + $response = wp_remote_get( sprintf( '%slicense/check/%s/%s/%s/%s', Product::API_URL, rawurlencode( $this->product->get_name() ), $license, rawurlencode( home_url() ), Loader::get_cache_token() ) ); | |
| 398 | 490 | } else { |
| 399 | - $api_params['edd_action'] = 'deactivate_license'; | |
| 491 | + $response = wp_remote_post( | |
| 492 | + sprintf( '%slicense/%s/%s/%s', Product::API_URL, $action, rawurlencode( $this->product->get_name() ), $license ), | |
| 493 | + array( | |
| 494 | + 'body' => wp_json_encode( | |
| 495 | + array( | |
| 496 | + 'url' => rawurlencode( home_url() ), | |
| 497 | + ) | |
| 498 | + ), | |
| 499 | + 'headers' => array( | |
| 500 | + 'Content-Type' => 'application/json', | |
| 501 | + ), | |
| 502 | + ) | |
| 503 | + ); | |
| 400 | 504 | } |
| 401 | - // Call the custom API. | |
| 402 | - $response = wp_remote_get( add_query_arg( $api_params, $this->get_api_url() ) ); | |
| 505 | + | |
| 403 | 506 | // make sure the response came back okay. |
| 404 | 507 | if ( is_wp_error( $response ) ) { |
| 405 | - $license_data = new \stdClass(); | |
| 406 | - $license_data->license = ( 'valid' != $status ) ? 'valid' : 'invalid'; | |
| 508 | + 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() ) ); | |
| 509 | + } | |
| 407 | 510 | |
| 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 | - } | |
| 511 | + $license_data = json_decode( wp_remote_retrieve_body( $response ) ); | |
| 512 | + | |
| 513 | + if ( ! is_object( $license_data ) ) { | |
| 514 | + return new \WP_Error( 'themeisle-license-404', 'ERROR: Failed to validate license. Please try again in one minute.' ); | |
| 417 | 515 | } |
| 516 | + if ( 'check' === $action ) { | |
| 517 | + return $license_data; | |
| 518 | + } | |
| 519 | + | |
| 520 | + Loader::clear_cache_token(); | |
| 521 | + | |
| 522 | + if ( ! isset( $license_data->license ) ) { | |
| 523 | + $license_data->license = 'invalid'; | |
| 524 | + } | |
| 525 | + | |
| 418 | 526 | if ( ! isset( $license_data->key ) ) { |
| 419 | 527 | $license_data->key = $license; |
| 420 | 528 | } |
| 421 | - if ( 'valid' == $license_data->license ) { | |
| 529 | + if ( 'valid' === $license_data->license ) { | |
| 422 | 530 | $this->reset_failed_checks(); |
| 423 | 531 | } |
| 424 | 532 | |
| 533 | + if ( 'deactivate' === $action ) { | |
| 534 | + | |
| 535 | + delete_option( $this->product->get_key() . '_license_data' ); | |
| 536 | + delete_option( $this->product->get_key() . '_license_plan' ); | |
| 537 | + delete_transient( $this->product->get_key() . '_license_data' ); | |
| 538 | + | |
| 539 | + return true; | |
| 540 | + } | |
| 425 | 541 | if ( isset( $license_data->plan ) ) { |
| 426 | 542 | update_option( $this->product->get_key() . '_license_plan', $license_data->plan ); |
| 427 | 543 | } |
| 428 | - | |
| 429 | 544 | update_option( $this->product->get_key() . '_license_data', $license_data ); |
| 430 | 545 | set_transient( $this->product->get_key() . '_license_data', $license_data, 12 * HOUR_IN_SECONDS ); |
| 546 | + if ( 'activate' === $action && 'valid' !== $license_data->license ) { | |
| 547 | + return new \WP_Error( 'themeisle-license-invalid', 'ERROR: Invalid license provided.' ); | |
| 548 | + } | |
| 431 | 549 | |
| 550 | + return true; | |
| 432 | 551 | } |
| 433 | 552 | |
| 434 | 553 | /** |
| 554 | + * Activate the license remotely. | |
| 555 | + */ | |
| 556 | + function process_license() { | |
| 557 | + // listen for our activate button to be clicked. | |
| 558 | + if ( ! isset( $_POST[ $this->product->get_key() . '_btn_trigger' ] ) ) { | |
| 559 | + return; | |
| 560 | + } | |
| 561 | + $license = $_POST[ $this->product->get_key() . '_license' ]; | |
| 562 | + $response = $this->do_license_process( $license, 'toggle' ); | |
| 563 | + if ( is_wp_error( $response ) ) { | |
| 564 | + $this->set_error( $response->get_error_message() ); | |
| 565 | + | |
| 566 | + return; | |
| 567 | + } | |
| 568 | + if ( true === $response ) { | |
| 569 | + $this->set_error( '' ); | |
| 570 | + } | |
| 571 | + | |
| 572 | + return; | |
| 573 | + } | |
| 574 | + | |
| 575 | + /** | |
| 435 | 576 | * Load the Themes screen. |
| 436 | 577 | */ |
| 437 | 578 | function load_themes_screen() { |
| 438 | 579 | add_thickbox(); |
| @@ -444,9 +585,9 @@ | ||
| 444 | 585 | */ |
| 445 | 586 | function update_nag() { |
| 446 | 587 | $theme = wp_get_theme( $this->product->get_slug() ); |
| 447 | 588 | $api_response = get_transient( $this->product_key ); |
| 448 | - if ( false === $api_response ) { | |
| 589 | + if ( false === $api_response || ! isset( $api_response->new_version ) ) { | |
| 449 | 590 | return; |
| 450 | 591 | } |
| 451 | 592 | $update_url = wp_nonce_url( 'update.php?action=upgrade-theme&theme=' . urlencode( $this->product->get_slug() ), 'upgrade-theme_' . $this->product->get_slug() ); |
| 452 | 593 | $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 +597,9 @@ | ||
| 456 | 597 | printf( |
| 457 | 598 | '<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 | 599 | $theme->get( 'Name' ), |
| 459 | 600 | $api_response->new_version, |
| 460 | - '#TB_inline?width=640&inlineId=' . $this->product->get_version() . '_changelog', | |
| 601 | + sprintf( '%s&TB_iframe=true&width=1024&height=800', $this->product->get_changelog() ), | |
| 461 | 602 | $theme->get( 'Name' ), |
| 462 | 603 | $update_url, |
| 463 | 604 | $update_onclick |
| 464 | 605 | ); |
| @@ -506,9 +647,9 @@ | ||
| 506 | 647 | set_transient( $this->product_key, $data, 30 * MINUTE_IN_SECONDS ); |
| 507 | 648 | |
| 508 | 649 | return false; |
| 509 | 650 | } |
| 510 | - $update_data->sections = maybe_unserialize( $update_data->sections ); | |
| 651 | + $update_data->sections = isset( $update_data->sections ) ? maybe_unserialize( $update_data->sections ) : null; | |
| 511 | 652 | |
| 512 | 653 | set_transient( $this->product_key, $update_data, 12 * HOUR_IN_SECONDS ); |
| 513 | 654 | } |
| 514 | 655 | if ( ! isset( $update_data->new_version ) ) { |
| @@ -526,23 +667,21 @@ | ||
| 526 | 667 | * |
| 527 | 668 | * @return bool|mixed Update api response. |
| 528 | 669 | */ |
| 529 | 670 | private function get_version_data() { |
| 530 | - $api_params = array( | |
| 531 | - 'edd_action' => 'get_version', | |
| 532 | - 'version' => $this->product->get_version(), | |
| 533 | - 'license' => $this->license_key, | |
| 534 | - 'name' => $this->product->get_name(), | |
| 535 | - 'slug' => $this->product->get_slug(), | |
| 536 | - 'author' => $this->get_distributor_name(), | |
| 537 | - 'url' => rawurlencode( home_url() ), | |
| 538 | - ); | |
| 539 | - $response = wp_remote_get( | |
| 540 | - $this->get_api_url(), | |
| 671 | + | |
| 672 | + $response = wp_remote_get( | |
| 673 | + sprintf( | |
| 674 | + '%slicense/version/%s/%s/%s/%s', | |
| 675 | + Product::API_URL, | |
| 676 | + rawurlencode( $this->product->get_name() ), | |
| 677 | + ( empty( $this->license_key ) ? 'free' : $this->license_key ), | |
| 678 | + $this->product->get_version(), | |
| 679 | + rawurlencode( home_url() ) | |
| 680 | + ), | |
| 541 | 681 | array( |
| 542 | 682 | 'timeout' => 15, |
| 543 | 683 | 'sslverify' => false, |
| 544 | - 'body' => $api_params, | |
| 545 | 684 | ) |
| 546 | 685 | ); |
| 547 | 686 | if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) ) { |
| 548 | 687 | return false; |
| @@ -550,8 +689,17 @@ | ||
| 550 | 689 | $update_data = json_decode( wp_remote_retrieve_body( $response ) ); |
| 551 | 690 | if ( ! is_object( $update_data ) ) { |
| 552 | 691 | return false; |
| 553 | 692 | } |
| 693 | + if ( isset( $update_data->slug ) ) { | |
| 694 | + $update_data->slug = $this->product->get_slug(); | |
| 695 | + } | |
| 696 | + if ( isset( $update_data->icons ) ) { | |
| 697 | + $update_data->icons = (array) $update_data->icons; | |
| 698 | + } | |
| 699 | + if ( isset( $update_data->banners ) ) { | |
| 700 | + $update_data->banners = (array) $update_data->banners; | |
| 701 | + } | |
| 554 | 702 | |
| 555 | 703 | return $update_data; |
| 556 | 704 | } |
| 557 | 705 | |
| @@ -614,9 +762,9 @@ | ||
| 614 | 762 | * |
| 615 | 763 | * @return object $_data |
| 616 | 764 | */ |
| 617 | 765 | public function plugins_api_filter( $_data, $_action = '', $_args = null ) { |
| 618 | - if ( ( 'plugin_information' != $_action ) || ! isset( $_args->slug ) || ( $_args->slug != $this->product->get_slug() ) ) { | |
| 766 | + if ( ( 'plugin_information' !== $_action ) || ! isset( $_args->slug ) || ( $_args->slug !== $this->product->get_slug() ) ) { | |
| 619 | 767 | return $_data; |
| 620 | 768 | } |
| 621 | 769 | $api_response = $this->api_request(); |
| 622 | 770 | if ( false !== $api_response ) { |
| @@ -677,8 +825,23 @@ | ||
| 677 | 825 | $this->failed_checks = intval( get_option( $this->product->get_key() . '_failed_checks', 0 ) ); |
| 678 | 826 | $this->register_license_hooks(); |
| 679 | 827 | } |
| 680 | 828 | |
| 829 | + $namespace = apply_filters( 'themesle_sdk_namespace_' . md5( $product->get_basefile() ), false ); | |
| 830 | + | |
| 831 | + if ( false !== $namespace ) { | |
| 832 | + add_filter( 'themeisle_sdk_license_process_' . $namespace, [ $this, 'do_license_process' ], 10, 2 ); | |
| 833 | + add_filter( 'product_' . $namespace . '_license_status', [ $this, 'get_license_status' ], PHP_INT_MAX ); | |
| 834 | + add_filter( 'product_' . $namespace . '_license_key', [ $this->product, 'get_license' ] ); | |
| 835 | + add_filter( 'product_' . $namespace . '_license_plan', [ $this, 'get_plan' ], PHP_INT_MAX ); | |
| 836 | + if ( defined( 'WP_CLI' ) && WP_CLI ) { | |
| 837 | + \WP_CLI::add_command( $namespace . ' activate', [ $this, 'cli_activate' ] ); | |
| 838 | + \WP_CLI::add_command( $namespace . ' deactivate', [ $this, 'cli_deactivate' ] ); | |
| 839 | + \WP_CLI::add_command( $namespace . ' is-active', [ $this, 'cli_is_active' ] ); | |
| 840 | + } | |
| 841 | + } | |
| 842 | + | |
| 843 | + add_action( 'admin_head', [ $this, 'auto_activate' ] ); | |
| 681 | 844 | if ( $this->product->is_plugin() ) { |
| 682 | 845 | add_filter( |
| 683 | 846 | 'pre_set_site_transient_update_plugins', |
| 684 | 847 | [ |
| @@ -706,13 +869,134 @@ | ||
| 706 | 869 | return $this; |
| 707 | 870 | } |
| 708 | 871 | |
| 709 | 872 | /** |
| 873 | + * Run license activation on plugin activate. | |
| 874 | + */ | |
| 875 | + public function auto_activate() { | |
| 876 | + if ( ! current_user_can( 'switch_themes' ) ) { | |
| 877 | + return; | |
| 878 | + } | |
| 879 | + $status = $this->get_license_status(); | |
| 880 | + if ( 'not_active' !== $status ) { | |
| 881 | + return; | |
| 882 | + } | |
| 883 | + | |
| 884 | + $license_file = dirname( $this->product->get_basefile() ) . '/license.json'; | |
| 885 | + | |
| 886 | + global $wp_filesystem; | |
| 887 | + if ( ! is_file( $license_file ) ) { | |
| 888 | + return; | |
| 889 | + } | |
| 890 | + | |
| 891 | + require_once( ABSPATH . '/wp-admin/includes/file.php' ); | |
| 892 | + \WP_Filesystem(); | |
| 893 | + $content = json_decode( $wp_filesystem->get_contents( $license_file ) ); | |
| 894 | + if ( ! is_object( $content ) ) { | |
| 895 | + return; | |
| 896 | + } | |
| 897 | + if ( ! isset( $content->key ) ) { | |
| 898 | + return; | |
| 899 | + } | |
| 900 | + $this->license_local = $content; | |
| 901 | + $lock_key = $this->product->get_key() . '_autoactivated'; | |
| 902 | + | |
| 903 | + if ( 'yes' === get_option( $lock_key, '' ) ) { | |
| 904 | + return; | |
| 905 | + } | |
| 906 | + $response = $this->do_license_process( $content->key, 'activate' ); | |
| 907 | + | |
| 908 | + update_option( $lock_key, 'yes' ); | |
| 909 | + | |
| 910 | + if ( apply_filters( $this->product->get_key() . '_hide_license_notices', false ) ) { | |
| 911 | + return; | |
| 912 | + } | |
| 913 | + | |
| 914 | + if ( true === $response ) { | |
| 915 | + add_action( 'admin_notices', [ $this, 'autoactivate_notice' ] ); | |
| 916 | + } | |
| 917 | + } | |
| 918 | + | |
| 919 | + /** | |
| 920 | + * Show auto-activate notice. | |
| 921 | + */ | |
| 922 | + public function autoactivate_notice() { | |
| 923 | + ?> | |
| 924 | + <div class="notice notice-success is-dismissible"> | |
| 925 | + <p><?php echo sprintf( '<strong>%s</strong> has been successfully activated using <strong>%s</strong> license !', $this->product->get_name(), str_repeat( '*', 20 ) . substr( $this->license_local->key, - 10 ) ); ?></p> | |
| 926 | + </div> | |
| 927 | + <?php | |
| 928 | + } | |
| 929 | + | |
| 930 | + /** | |
| 931 | + * Activate product license on this site. | |
| 932 | + * | |
| 933 | + * ## OPTIONS | |
| 934 | + * | |
| 935 | + * @param array $args Command args. | |
| 936 | + * | |
| 937 | + * [<license-key>] | |
| 938 | + * : Product license key. | |
| 939 | + */ | |
| 940 | + public function cli_activate( $args ) { | |
| 941 | + $license_key = isset( $args[0] ) ? trim( $args[0] ) : ''; | |
| 942 | + $response = $this->do_license_process( $license_key, 'activate' ); | |
| 943 | + if ( true !== $response ) { | |
| 944 | + \WP_CLI::error( $response->get_error_message() ); | |
| 945 | + | |
| 946 | + return; | |
| 947 | + } | |
| 948 | + | |
| 949 | + \WP_CLI::success( 'Product successfully activated.' ); | |
| 950 | + } | |
| 951 | + | |
| 952 | + /** | |
| 953 | + * Deactivate product license on this site. | |
| 954 | + * | |
| 955 | + * @param array $args Command args. | |
| 956 | + * | |
| 957 | + * ## OPTIONS | |
| 958 | + * | |
| 959 | + * [<license-key>] | |
| 960 | + * : Product license key. | |
| 961 | + */ | |
| 962 | + public function cli_deactivate( $args ) { | |
| 963 | + $license_key = isset( $args[0] ) ? trim( $args[0] ) : ''; | |
| 964 | + $response = $this->do_license_process( $license_key, 'deactivate' ); | |
| 965 | + if ( true !== $response ) { | |
| 966 | + \WP_CLI::error( $response->get_error_message() ); | |
| 967 | + | |
| 968 | + return; | |
| 969 | + } | |
| 970 | + | |
| 971 | + \WP_CLI::success( 'Product successfully deactivated.' ); | |
| 972 | + } | |
| 973 | + | |
| 974 | + /** | |
| 975 | + * Checks if product has license activated. | |
| 976 | + * | |
| 977 | + * @param array $args Command args. | |
| 978 | + * | |
| 979 | + * @subcommand is-active | |
| 980 | + */ | |
| 981 | + public function cli_is_active( $args ) { | |
| 982 | + | |
| 983 | + $status = $this->get_license_status(); | |
| 984 | + if ( 'valid' === $status ) { | |
| 985 | + \WP_CLI::halt( 0 ); | |
| 986 | + | |
| 987 | + return; | |
| 988 | + } | |
| 989 | + | |
| 990 | + \WP_CLI::halt( 1 ); | |
| 991 | + } | |
| 992 | + | |
| 993 | + /** | |
| 710 | 994 | * Register license fields for the products. |
| 711 | 995 | */ |
| 712 | 996 | public function register_license_hooks() { |
| 713 | 997 | add_action( 'admin_init', array( $this, 'register_settings' ) ); |
| 714 | - add_action( 'admin_init', array( $this, 'activate_license' ) ); | |
| 998 | + add_action( 'admin_init', array( $this, 'process_license' ) ); | |
| 715 | 999 | add_action( 'admin_init', array( $this, 'product_valid' ), 99999999 ); |
| 716 | 1000 | add_action( 'admin_notices', array( $this, 'show_notice' ) ); |
| 717 | 1001 | add_filter( $this->product->get_key() . '_license_status', array( $this, 'get_license_status' ) ); |
| 718 | 1002 | } |