| 1 |
<?php |
| 2 |
/** |
| 3 |
* The main loader class for license handling. |
| 4 |
* |
| 5 |
* @package ThemeIsleSDK |
| 6 |
* @subpackage Modules |
| 7 |
* @copyright Copyright (c) 2017, Marius Cristea |
| 8 |
* @license http://opensource.org/licenses/gpl-3.0.php GNU Public License |
| 9 |
* @since 1.0.0 |
| 10 |
*/ |
| 11 |
|
| 12 |
namespace ThemeisleSDK\Modules; |
| 13 |
|
| 14 |
// Exit if accessed directly. |
| 15 |
use ThemeisleSDK\Common\Abstract_Module; |
| 16 |
use ThemeisleSDK\Product; |
| 17 |
|
| 18 |
if ( ! defined( 'ABSPATH' ) ) { |
| 19 |
exit; |
| 20 |
} |
| 21 |
|
| 22 |
/** |
| 23 |
* Licenser module for ThemeIsle SDK. |
| 24 |
*/ |
| 25 |
class Licenser extends Abstract_Module { |
| 26 |
|
| 27 |
/** |
| 28 |
* Number of max failed checks before showing the license message. |
| 29 |
* |
| 30 |
* @var int $max_failed Maximum failed checks allowed before show the notice |
| 31 |
*/ |
| 32 |
private static $max_failed = 5; |
| 33 |
/** |
| 34 |
* License key string. |
| 35 |
* |
| 36 |
* @var string $license_key The license key string |
| 37 |
*/ |
| 38 |
public $license_key; |
| 39 |
/** |
| 40 |
* This ensures that the custom API request only runs on the second time that WP fires the update check. |
| 41 |
* |
| 42 |
* @var bool $do_check Flag for request. |
| 43 |
*/ |
| 44 |
private $do_check = false; |
| 45 |
/** |
| 46 |
* Number of failed checks to the api endpoint. |
| 47 |
* |
| 48 |
* @var bool $failed_checks |
| 49 |
*/ |
| 50 |
private $failed_checks = 0; |
| 51 |
/** |
| 52 |
* The product update response key. |
| 53 |
* |
| 54 |
* @var string $product_key Product key. |
| 55 |
*/ |
| 56 |
private $product_key; |
| 57 |
|
| 58 |
/** |
| 59 |
* Disable wporg updates for premium products. |
| 60 |
* |
| 61 |
* @param string $r Update payload. |
| 62 |
* @param string $url The api url. |
| 63 |
* |
| 64 |
* @return mixed List of themes to check for update. |
| 65 |
*/ |
| 66 |
function disable_wporg_update( $r, $url ) { |
| 67 |
|
| 68 |
if ( 0 !== strpos( $url, 'https://api.wordpress.org/themes/update-check/' ) ) { |
| 69 |
return $r; |
| 70 |
} |
| 71 |
|
| 72 |
// Decode the JSON response. |
| 73 |
$themes = json_decode( $r['body']['themes'] ); |
| 74 |
|
| 75 |
unset( $themes->themes->{$this->product->get_slug()} ); |
| 76 |
|
| 77 |
// Encode the updated JSON response. |
| 78 |
$r['body']['themes'] = json_encode( $themes ); |
| 79 |
|
| 80 |
return $r; |
| 81 |
} |
| 82 |
|
| 83 |
/** |
| 84 |
* Register the setting for the license of the product. |
| 85 |
* |
| 86 |
* @return bool |
| 87 |
*/ |
| 88 |
public function register_settings() { |
| 89 |
if ( ! is_admin() ) { |
| 90 |
return false; |
| 91 |
} |
| 92 |
if ( apply_filters( $this->product->get_key() . '_hide_license_field', false ) ) { |
| 93 |
return; |
| 94 |
} |
| 95 |
add_settings_field( |
| 96 |
$this->product->get_key() . '_license', |
| 97 |
$this->product->get_name() . ' license', |
| 98 |
array( $this, 'license_view' ), |
| 99 |
'general' |
| 100 |
); |
| 101 |
} |
| 102 |
|
| 103 |
/** |
| 104 |
* The license view field. |
| 105 |
*/ |
| 106 |
public function license_view() { |
| 107 |
$status = $this->get_license_status(); |
| 108 |
$value = $this->license_key; |
| 109 |
|
| 110 |
$activate_string = apply_filters( $this->product->get_key() . '_lc_activate_string', 'Activate' ); |
| 111 |
$deactivate_string = apply_filters( $this->product->get_key() . '_lc_deactivate_string', 'Deactivate' ); |
| 112 |
$valid_string = apply_filters( $this->product->get_key() . '_lc_valid_string', 'Valid' ); |
| 113 |
$invalid_string = apply_filters( $this->product->get_key() . '_lc_invalid_string', 'Invalid' ); |
| 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 |
} |
| 121 |
|
| 122 |
input.themeisle-sdk-license-input { |
| 123 |
width: 300px; |
| 124 |
padding: 5px; |
| 125 |
} |
| 126 |
|
| 127 |
.themeisle-sdk-license-deactivate-cta { |
| 128 |
color: #fff; |
| 129 |
background: #7ad03a; |
| 130 |
display: inline-block; |
| 131 |
text-decoration: none; |
| 132 |
font-size: 13px; |
| 133 |
line-height: 30px; |
| 134 |
height: 26px; |
| 135 |
margin-left: 5px; |
| 136 |
padding: 0 10px 3px; |
| 137 |
-webkit-border-radius: 3px; |
| 138 |
border-radius: 3px; |
| 139 |
} |
| 140 |
|
| 141 |
.themeisle-sdk-license-activate-cta { |
| 142 |
color: #fff; |
| 143 |
background: #dd3d36; |
| 144 |
display: inline-block; |
| 145 |
text-decoration: none; |
| 146 |
font-size: 13px; |
| 147 |
line-height: 30px; |
| 148 |
height: 26px; |
| 149 |
margin-left: 5px; |
| 150 |
padding: 0 10px 3px; |
| 151 |
-webkit-border-radius: 3px; |
| 152 |
border-radius: 3px; |
| 153 |
} |
| 154 |
|
| 155 |
button.button.themeisle-sdk-licenser-button-cta { |
| 156 |
line-height: 26px; |
| 157 |
height: 29px; |
| 158 |
vertical-align: top; |
| 159 |
} |
| 160 |
|
| 161 |
</style> |
| 162 |
<?php |
| 163 |
echo sprintf( |
| 164 |
'<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', |
| 165 |
( ( 'valid' === $status ) ? sprintf( '<input type="hidden" value="%s" name="%s_license" />', $value, $this->product->get_key() ) : '' ), |
| 166 |
( ( 'valid' === $status ) ? 'themeisle-sdk-text-input-valid' : '' ), |
| 167 |
$this->product->get_key(), |
| 168 |
( ( 'valid' === $status ) ? $this->product->get_key() . '_mask' : $this->product->get_key() ), |
| 169 |
( ( 'valid' === $status ) ? ( str_repeat( '*', 30 ) . substr( $value, - 5 ) ) : $value ), |
| 170 |
( 'valid' === $status ? 'themeisle-sdk-license-deactivate-cta' : 'themeisle-sdk-license-activate-cta' ), |
| 171 |
( 'valid' === $status ? $valid_string : $invalid_string ), |
| 172 |
$this->product->get_key(), |
| 173 |
( 'valid' === $status ? $deactivate_string : $activate_string ), |
| 174 |
sprintf( $license_message, '<a href="' . $this->get_api_url() . '">' . $this->get_distributor_name() . '</a> ', $this->product->get_type() ), |
| 175 |
empty( $error_message ) ? '' : sprintf( '<p style="color:#dd3d36">%s</p>', $error_message ) |
| 176 |
); |
| 177 |
|
| 178 |
} |
| 179 |
|
| 180 |
/** |
| 181 |
* Return the license status. |
| 182 |
* |
| 183 |
* @return string The License status. |
| 184 |
*/ |
| 185 |
public function get_license_status() { |
| 186 |
|
| 187 |
$license_data = get_option( $this->product->get_key() . '_license_data', '' ); |
| 188 |
|
| 189 |
if ( '' === $license_data ) { |
| 190 |
return get_option( $this->product->get_key() . '_license_status', 'not_active' ); |
| 191 |
} |
| 192 |
|
| 193 |
return isset( $license_data->license ) ? $license_data->license : get_option( $this->product->get_key() . '_license_status', 'not_active' ); |
| 194 |
|
| 195 |
} |
| 196 |
|
| 197 |
/** |
| 198 |
* Get remote api url. |
| 199 |
* |
| 200 |
* @return string Remote api url. |
| 201 |
*/ |
| 202 |
public function get_api_url() { |
| 203 |
if ( $this->is_from_partner( $this->product ) ) { |
| 204 |
return 'https://themeisle.com'; |
| 205 |
} |
| 206 |
|
| 207 |
return $this->product->get_store_url(); |
| 208 |
} |
| 209 |
|
| 210 |
/** |
| 211 |
* Get remote api url. |
| 212 |
* |
| 213 |
* @return string Remote api url. |
| 214 |
*/ |
| 215 |
public function get_distributor_name() { |
| 216 |
if ( $this->is_from_partner( $this->product ) ) { |
| 217 |
return 'ThemeIsle'; |
| 218 |
} |
| 219 |
|
| 220 |
return $this->product->get_store_name(); |
| 221 |
} |
| 222 |
|
| 223 |
/** |
| 224 |
* Show the admin notice regarding the license status. |
| 225 |
* |
| 226 |
* @return bool Should we show the notice ? |
| 227 |
*/ |
| 228 |
function show_notice() { |
| 229 |
if ( ! is_admin() ) { |
| 230 |
return false; |
| 231 |
} |
| 232 |
|
| 233 |
if ( apply_filters( $this->product->get_key() . '_hide_license_notices', false ) ) { |
| 234 |
return; |
| 235 |
} |
| 236 |
$status = $this->get_license_status(); |
| 237 |
$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.' ); |
| 238 |
$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>. ' ); |
| 239 |
|
| 240 |
// No activations left for this license. |
| 241 |
if ( 'valid' != $status && $this->check_activation() ) { |
| 242 |
?> |
| 243 |
<div class="error"> |
| 244 |
<p><strong> |
| 245 |
<?php |
| 246 |
echo sprintf( |
| 247 |
$no_activations_string, |
| 248 |
$this->product->get_name(), |
| 249 |
$this->product->get_name(), |
| 250 |
'<a href="' . $this->get_api_url() . '" target="_blank">' . $this->get_distributor_name() . '</a>' |
| 251 |
); |
| 252 |
?> |
| 253 |
</strong> |
| 254 |
</p> |
| 255 |
</div> |
| 256 |
<?php |
| 257 |
return false; |
| 258 |
} |
| 259 |
// Invalid license key. |
| 260 |
if ( 'valid' != $status ) { |
| 261 |
?> |
| 262 |
<div class="error"> |
| 263 |
<p> |
| 264 |
<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> |
| 265 |
</p> |
| 266 |
</div> |
| 267 |
<?php |
| 268 |
|
| 269 |
return false; |
| 270 |
} |
| 271 |
|
| 272 |
return true; |
| 273 |
} |
| 274 |
|
| 275 |
/** |
| 276 |
* Check if the license is active or not. |
| 277 |
* |
| 278 |
* @return bool |
| 279 |
*/ |
| 280 |
public function check_activation() { |
| 281 |
$license_data = get_option( $this->product->get_key() . '_license_data', '' ); |
| 282 |
if ( '' === $license_data ) { |
| 283 |
return false; |
| 284 |
} |
| 285 |
|
| 286 |
return isset( $license_data->error ) ? ( 'no_activations_left' == $license_data->error ) : false; |
| 287 |
|
| 288 |
} |
| 289 |
|
| 290 |
/** |
| 291 |
* Check if the license is about to expire in the next month. |
| 292 |
* |
| 293 |
* @return bool |
| 294 |
*/ |
| 295 |
function check_expiration() { |
| 296 |
$license_data = get_option( $this->product->get_key() . '_license_data', '' ); |
| 297 |
if ( '' === $license_data ) { |
| 298 |
return false; |
| 299 |
} |
| 300 |
if ( ! isset( $license_data->expires ) ) { |
| 301 |
return false; |
| 302 |
} |
| 303 |
if ( strtotime( $license_data->expires ) - time() > 30 * 24 * 3600 ) { |
| 304 |
return false; |
| 305 |
} |
| 306 |
|
| 307 |
return true; |
| 308 |
} |
| 309 |
|
| 310 |
/** |
| 311 |
* Return the renew url from the store used. |
| 312 |
* |
| 313 |
* @return string The renew url. |
| 314 |
*/ |
| 315 |
function renew_url() { |
| 316 |
$license_data = get_option( $this->product->get_key() . '_license_data', '' ); |
| 317 |
if ( '' === $license_data ) { |
| 318 |
return $this->get_api_url(); |
| 319 |
} |
| 320 |
if ( ! isset( $license_data->download_id ) || ! isset( $license_data->key ) ) { |
| 321 |
return $this->get_api_url(); |
| 322 |
} |
| 323 |
|
| 324 |
return $this->get_api_url() . '/checkout/?edd_license_key=' . $license_data->key . '&download_id=' . $license_data->download_id; |
| 325 |
} |
| 326 |
|
| 327 |
/** |
| 328 |
* Run the license check call. |
| 329 |
*/ |
| 330 |
public function product_valid() { |
| 331 |
if ( false !== ( $license = get_transient( $this->product->get_key() . '_license_data' ) ) ) { |
| 332 |
return; |
| 333 |
} |
| 334 |
$license = $this->check_license(); |
| 335 |
set_transient( $this->product->get_key() . '_license_data', $license, 12 * HOUR_IN_SECONDS ); |
| 336 |
update_option( $this->product->get_key() . '_license_data', $license ); |
| 337 |
} |
| 338 |
|
| 339 |
/** |
| 340 |
* Check the license status. |
| 341 |
* |
| 342 |
* @return object The license data. |
| 343 |
*/ |
| 344 |
public function check_license() { |
| 345 |
$status = $this->get_license_status(); |
| 346 |
if ( 'not_active' == $status ) { |
| 347 |
$license_data = new \stdClass(); |
| 348 |
$license_data->license = 'not_active'; |
| 349 |
|
| 350 |
return $license_data; |
| 351 |
} |
| 352 |
$license = trim( $this->license_key ); |
| 353 |
$api_params = array( |
| 354 |
'edd_action' => 'check_license', |
| 355 |
'license' => $license, |
| 356 |
'item_name' => rawurlencode( $this->product->get_name() ), |
| 357 |
'url' => rawurlencode( home_url() ), |
| 358 |
); |
| 359 |
// Call the custom API. |
| 360 |
$response = wp_remote_get( |
| 361 |
add_query_arg( $api_params, $this->get_api_url() ), |
| 362 |
array( |
| 363 |
'timeout' => 15, |
| 364 |
'sslverify' => false, |
| 365 |
) |
| 366 |
); |
| 367 |
if ( is_wp_error( $response ) ) { |
| 368 |
$license_data = new \stdClass(); |
| 369 |
$license_data->license = 'valid'; |
| 370 |
|
| 371 |
} else { |
| 372 |
$license_data = json_decode( wp_remote_retrieve_body( $response ) ); |
| 373 |
if ( ! is_object( $license_data ) ) { |
| 374 |
$license_data = new \stdClass(); |
| 375 |
$license_data->license = 'valid'; |
| 376 |
} |
| 377 |
} |
| 378 |
$license_old = get_option( $this->product->get_key() . '_license_data', '' ); |
| 379 |
if ( 'valid' == $license_old->license && ( $license_data->license != $license_old->license ) ) { |
| 380 |
$this->increment_failed_checks(); |
| 381 |
} else { |
| 382 |
$this->reset_failed_checks(); |
| 383 |
} |
| 384 |
|
| 385 |
if ( $this->failed_checks <= self::$max_failed ) { |
| 386 |
return $license_old; |
| 387 |
} |
| 388 |
|
| 389 |
if ( isset( $license_old->hide_valid ) ) { |
| 390 |
$license_data->hide_valid = true; |
| 391 |
} |
| 392 |
|
| 393 |
if ( ! isset( $license_data->key ) ) { |
| 394 |
$license_data->key = isset( $license_old->key ) ? $license_old->key : ''; |
| 395 |
} |
| 396 |
|
| 397 |
if ( isset( $license_old->hide_expiration ) ) { |
| 398 |
$license_data->hide_expiration = true; |
| 399 |
} |
| 400 |
|
| 401 |
if ( isset( $license_old->hide_activation ) ) { |
| 402 |
$license_data->hide_activation = true; |
| 403 |
} |
| 404 |
|
| 405 |
return $license_data; |
| 406 |
|
| 407 |
} |
| 408 |
|
| 409 |
/** |
| 410 |
* Increment the failed checks. |
| 411 |
*/ |
| 412 |
private function increment_failed_checks() { |
| 413 |
$this->failed_checks ++; |
| 414 |
update_option( $this->product->get_key() . '_failed_checks', $this->failed_checks ); |
| 415 |
} |
| 416 |
|
| 417 |
/** |
| 418 |
* Reset the failed checks |
| 419 |
*/ |
| 420 |
private function reset_failed_checks() { |
| 421 |
$this->failed_checks = 1; |
| 422 |
update_option( $this->product->get_key() . '_failed_checks', $this->failed_checks ); |
| 423 |
} |
| 424 |
|
| 425 |
/** |
| 426 |
* Set license validation error message. |
| 427 |
* |
| 428 |
* @param string $message Error message. |
| 429 |
*/ |
| 430 |
public function set_error( $message = '' ) { |
| 431 |
set_transient( $this->product->get_key() . 'act_err', $message, MINUTE_IN_SECONDS ); |
| 432 |
|
| 433 |
return; |
| 434 |
} |
| 435 |
|
| 436 |
/** |
| 437 |
* Return the last error message. |
| 438 |
* |
| 439 |
* @return mixed Error message. |
| 440 |
*/ |
| 441 |
public function get_error() { |
| 442 |
return get_transient( $this->product->get_key() . 'act_err' ); |
| 443 |
} |
| 444 |
|
| 445 |
/** |
| 446 |
* Activate the license remotely. |
| 447 |
*/ |
| 448 |
function activate_license() { |
| 449 |
// listen for our activate button to be clicked. |
| 450 |
if ( ! isset( $_POST[ $this->product->get_key() . '_btn_trigger' ] ) ) { |
| 451 |
return; |
| 452 |
} |
| 453 |
$status = $this->get_license_status(); |
| 454 |
// retrieve the license from the database. |
| 455 |
$license = $_POST[ $this->product->get_key() . '_license' ]; |
| 456 |
$api_params = array( |
| 457 |
'license' => $license, |
| 458 |
'item_name' => rawurlencode( $this->product->get_name() ), |
| 459 |
'url' => rawurlencode( home_url() ), |
| 460 |
); |
| 461 |
if ( 'valid' != $status ) { |
| 462 |
// data to send in our API request. |
| 463 |
$api_params['edd_action'] = 'activate_license'; |
| 464 |
} else { |
| 465 |
$api_params['edd_action'] = 'deactivate_license'; |
| 466 |
} |
| 467 |
// Call the custom API. |
| 468 |
$response = wp_remote_get( add_query_arg( $api_params, $this->get_api_url() ) ); |
| 469 |
// make sure the response came back okay. |
| 470 |
if ( is_wp_error( $response ) ) { |
| 471 |
$this->set_error( sprintf( 'ERROR: Failed to connect to the license service. Please try again later. Reason: %s', $response->get_error_message() ) ); |
| 472 |
|
| 473 |
return; |
| 474 |
} |
| 475 |
|
| 476 |
$license_data = json_decode( wp_remote_retrieve_body( $response ) ); |
| 477 |
|
| 478 |
if ( ! is_object( $license_data ) ) { |
| 479 |
$this->set_error( 'ERROR: Failed to validate license. Please try again in one minute.' ); |
| 480 |
|
| 481 |
return; |
| 482 |
} |
| 483 |
if ( ! isset( $license_data->license ) ) { |
| 484 |
$license_data->license = 'invalid'; |
| 485 |
} |
| 486 |
|
| 487 |
if ( ! isset( $license_data->key ) ) { |
| 488 |
$license_data->key = $license; |
| 489 |
} |
| 490 |
if ( 'valid' == $license_data->license ) { |
| 491 |
$this->reset_failed_checks(); |
| 492 |
} |
| 493 |
|
| 494 |
$this->set_error( '' ); |
| 495 |
|
| 496 |
if ( 'deactivate_license' === $api_params['edd_action'] ) { |
| 497 |
|
| 498 |
delete_option( $this->product->get_key() . '_license_data' ); |
| 499 |
delete_option( $this->product->get_key() . '_license_plan' ); |
| 500 |
delete_transient( $this->product->get_key() . '_license_data' ); |
| 501 |
|
| 502 |
return; |
| 503 |
} |
| 504 |
if ( isset( $license_data->plan ) ) { |
| 505 |
update_option( $this->product->get_key() . '_license_plan', $license_data->plan ); |
| 506 |
} |
| 507 |
update_option( $this->product->get_key() . '_license_data', $license_data ); |
| 508 |
set_transient( $this->product->get_key() . '_license_data', $license_data, 12 * HOUR_IN_SECONDS ); |
| 509 |
|
| 510 |
} |
| 511 |
|
| 512 |
/** |
| 513 |
* Load the Themes screen. |
| 514 |
*/ |
| 515 |
function load_themes_screen() { |
| 516 |
add_thickbox(); |
| 517 |
add_action( 'admin_notices', array( &$this, 'update_nag' ) ); |
| 518 |
} |
| 519 |
|
| 520 |
/** |
| 521 |
* Alter the nag for themes update. |
| 522 |
*/ |
| 523 |
function update_nag() { |
| 524 |
$theme = wp_get_theme( $this->product->get_slug() ); |
| 525 |
$api_response = get_transient( $this->product_key ); |
| 526 |
if ( false === $api_response || ! isset( $api_response->new_version ) ) { |
| 527 |
return; |
| 528 |
} |
| 529 |
$update_url = wp_nonce_url( 'update.php?action=upgrade-theme&theme=' . urlencode( $this->product->get_slug() ), 'upgrade-theme_' . $this->product->get_slug() ); |
| 530 |
$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.' ); |
| 531 |
$update_onclick = ' onclick="if ( confirm(\'' . esc_js( $update_message ) . '\') ) {return true;}return false;"'; |
| 532 |
if ( version_compare( $this->product->get_version(), $api_response->new_version, '<' ) ) { |
| 533 |
echo '<div id="update-nag">'; |
| 534 |
printf( |
| 535 |
'<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>.', |
| 536 |
$theme->get( 'Name' ), |
| 537 |
$api_response->new_version, |
| 538 |
sprintf( '%s&TB_iframe=true&width=1024&height=800', $this->product->get_changelog() ), |
| 539 |
$theme->get( 'Name' ), |
| 540 |
$update_url, |
| 541 |
$update_onclick |
| 542 |
); |
| 543 |
echo '</div>'; |
| 544 |
echo '<div id="' . $this->product->get_slug() . '_' . 'changelog" style="display:none;">'; |
| 545 |
echo wpautop( $api_response->sections['changelog'] ); |
| 546 |
echo '</div>'; |
| 547 |
} |
| 548 |
} |
| 549 |
|
| 550 |
/** |
| 551 |
* Alter update transient. |
| 552 |
* |
| 553 |
* @param mixed $value The transient data. |
| 554 |
* |
| 555 |
* @return mixed |
| 556 |
*/ |
| 557 |
function theme_update_transient( $value ) { |
| 558 |
$update_data = $this->check_for_update(); |
| 559 |
if ( $update_data ) { |
| 560 |
$value->response[ $this->product->get_slug() ] = $update_data; |
| 561 |
} |
| 562 |
|
| 563 |
return $value; |
| 564 |
} |
| 565 |
|
| 566 |
/** |
| 567 |
* Check for updates |
| 568 |
* |
| 569 |
* @return array|bool Either the update data or false in case of failure. |
| 570 |
*/ |
| 571 |
function check_for_update() { |
| 572 |
$update_data = get_transient( $this->product_key ); |
| 573 |
|
| 574 |
if ( false === $update_data ) { |
| 575 |
$failed = false; |
| 576 |
$update_data = $this->get_version_data(); |
| 577 |
if ( empty( $update_data ) ) { |
| 578 |
$failed = true; |
| 579 |
} |
| 580 |
// If the response failed, try again in 30 minutes. |
| 581 |
if ( $failed ) { |
| 582 |
$data = new \stdClass(); |
| 583 |
$data->new_version = $this->product->get_version(); |
| 584 |
set_transient( $this->product_key, $data, 30 * MINUTE_IN_SECONDS ); |
| 585 |
|
| 586 |
return false; |
| 587 |
} |
| 588 |
$update_data->sections = isset( $update_data->sections ) ? maybe_unserialize( $update_data->sections ) : null; |
| 589 |
|
| 590 |
set_transient( $this->product_key, $update_data, 12 * HOUR_IN_SECONDS ); |
| 591 |
} |
| 592 |
if ( ! isset( $update_data->new_version ) ) { |
| 593 |
return false; |
| 594 |
} |
| 595 |
if ( version_compare( $this->product->get_version(), $update_data->new_version, '>=' ) ) { |
| 596 |
return false; |
| 597 |
} |
| 598 |
|
| 599 |
return (array) $update_data; |
| 600 |
} |
| 601 |
|
| 602 |
/** |
| 603 |
* Check remote api for latest version. |
| 604 |
* |
| 605 |
* @return bool|mixed Update api response. |
| 606 |
*/ |
| 607 |
private function get_version_data() { |
| 608 |
$api_params = array( |
| 609 |
'edd_action' => 'get_version', |
| 610 |
'version' => $this->product->get_version(), |
| 611 |
'license' => empty( $this->license_key ) ? 'free' : $this->license_key, |
| 612 |
'name' => rawurlencode( $this->product->get_name() ), |
| 613 |
'slug' => $this->product->get_slug(), |
| 614 |
'author' => rawurlencode( $this->get_distributor_name() ), |
| 615 |
'url' => rawurlencode( home_url() ), |
| 616 |
); |
| 617 |
$response = wp_remote_get( |
| 618 |
add_query_arg( $api_params, $this->get_api_url() ), |
| 619 |
array( |
| 620 |
'timeout' => 15, |
| 621 |
'sslverify' => false, |
| 622 |
) |
| 623 |
); |
| 624 |
if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) ) { |
| 625 |
return false; |
| 626 |
} |
| 627 |
$update_data = json_decode( wp_remote_retrieve_body( $response ) ); |
| 628 |
if ( ! is_object( $update_data ) ) { |
| 629 |
return false; |
| 630 |
} |
| 631 |
|
| 632 |
return $update_data; |
| 633 |
} |
| 634 |
|
| 635 |
/** |
| 636 |
* Delete the update transient |
| 637 |
*/ |
| 638 |
function delete_theme_update_transient() { |
| 639 |
delete_transient( $this->product_key ); |
| 640 |
} |
| 641 |
|
| 642 |
/** |
| 643 |
* Check for Updates at the defined API endpoint and modify the update array. |
| 644 |
* |
| 645 |
* @param array $_transient_data Update array build by WordPress. |
| 646 |
* |
| 647 |
* @return mixed Modified update array with custom plugin data. |
| 648 |
*/ |
| 649 |
public function pre_set_site_transient_update_plugins_filter( $_transient_data ) { |
| 650 |
if ( empty( $_transient_data ) || ! $this->do_check ) { |
| 651 |
$this->do_check = true; |
| 652 |
|
| 653 |
return $_transient_data; |
| 654 |
} |
| 655 |
$api_response = $this->api_request(); |
| 656 |
if ( false !== $api_response && is_object( $api_response ) && isset( $api_response->new_version ) ) { |
| 657 |
if ( version_compare( $this->product->get_version(), $api_response->new_version, '<' ) ) { |
| 658 |
$_transient_data->response[ $this->product->get_slug() . '/' . $this->product->get_file() ] = $api_response; |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
return $_transient_data; |
| 663 |
} |
| 664 |
|
| 665 |
/** |
| 666 |
* Calls the API and, if successfull, returns the object delivered by the API. |
| 667 |
* |
| 668 |
* @param string $_action The requested action. |
| 669 |
* @param array $_data Parameters for the API action. |
| 670 |
* |
| 671 |
* @return false||object |
| 672 |
*/ |
| 673 |
private function api_request( $_action = '', $_data = '' ) { |
| 674 |
$update_data = $this->get_version_data(); |
| 675 |
if ( empty( $update_data ) ) { |
| 676 |
return false; |
| 677 |
} |
| 678 |
if ( $update_data && isset( $update_data->sections ) ) { |
| 679 |
$update_data->sections = maybe_unserialize( $update_data->sections ); |
| 680 |
} |
| 681 |
|
| 682 |
return $update_data; |
| 683 |
} |
| 684 |
|
| 685 |
/** |
| 686 |
* Updates information on the "View version x.x details" page with custom data. |
| 687 |
* |
| 688 |
* @param mixed $_data Plugin data. |
| 689 |
* @param string $_action Action to send. |
| 690 |
* @param object $_args Arguments to use. |
| 691 |
* |
| 692 |
* @return object $_data |
| 693 |
*/ |
| 694 |
public function plugins_api_filter( $_data, $_action = '', $_args = null ) { |
| 695 |
if ( ( 'plugin_information' != $_action ) || ! isset( $_args->slug ) || ( $_args->slug != $this->product->get_slug() ) ) { |
| 696 |
return $_data; |
| 697 |
} |
| 698 |
$api_response = $this->api_request(); |
| 699 |
if ( false !== $api_response ) { |
| 700 |
$_data = $api_response; |
| 701 |
} |
| 702 |
|
| 703 |
return $_data; |
| 704 |
} |
| 705 |
|
| 706 |
/** |
| 707 |
* Disable SSL verification in order to prevent download update failures. |
| 708 |
* |
| 709 |
* @param array $args Http args. |
| 710 |
* @param string $url Url to check. |
| 711 |
* |
| 712 |
* @return array $array |
| 713 |
*/ |
| 714 |
function http_request_args( $args, $url ) { |
| 715 |
// If it is an https request and we are performing a package download, disable ssl verification. |
| 716 |
if ( strpos( $url, 'https://' ) !== false && strpos( $url, 'edd_action=package_download' ) ) { |
| 717 |
$args['sslverify'] = false; |
| 718 |
} |
| 719 |
|
| 720 |
return $args; |
| 721 |
} |
| 722 |
|
| 723 |
/** |
| 724 |
* Check if we should load the module for this product. |
| 725 |
* |
| 726 |
* @param Product $product Product data. |
| 727 |
* |
| 728 |
* @return bool Should we load the module? |
| 729 |
*/ |
| 730 |
public function can_load( $product ) { |
| 731 |
|
| 732 |
if ( $product->is_wordpress_available() ) { |
| 733 |
return false; |
| 734 |
} |
| 735 |
|
| 736 |
return ( apply_filters( $product->get_key() . '_enable_licenser', true ) === true ); |
| 737 |
|
| 738 |
} |
| 739 |
|
| 740 |
/** |
| 741 |
* Load module logic. |
| 742 |
* |
| 743 |
* @param Product $product Product to load the module for. |
| 744 |
* |
| 745 |
* @return Licenser Module object. |
| 746 |
*/ |
| 747 |
public function load( $product ) { |
| 748 |
$this->product = $product; |
| 749 |
|
| 750 |
$this->product_key = $this->product->get_key() . '-update-response'; |
| 751 |
|
| 752 |
$this->license_key = $this->product->get_license(); |
| 753 |
if ( $this->product->requires_license() ) { |
| 754 |
$this->failed_checks = intval( get_option( $this->product->get_key() . '_failed_checks', 0 ) ); |
| 755 |
$this->register_license_hooks(); |
| 756 |
} |
| 757 |
|
| 758 |
if ( $this->product->is_plugin() ) { |
| 759 |
add_filter( |
| 760 |
'pre_set_site_transient_update_plugins', |
| 761 |
[ |
| 762 |
$this, |
| 763 |
'pre_set_site_transient_update_plugins_filter', |
| 764 |
] |
| 765 |
); |
| 766 |
add_filter( 'plugins_api', array( $this, 'plugins_api_filter' ), 10, 3 ); |
| 767 |
add_filter( 'http_request_args', array( $this, 'http_request_args' ), 10, 2 ); |
| 768 |
|
| 769 |
return $this; |
| 770 |
} |
| 771 |
if ( $this->product->is_theme() ) { |
| 772 |
add_filter( 'site_transient_update_themes', array( &$this, 'theme_update_transient' ) ); |
| 773 |
add_filter( 'delete_site_transient_update_themes', array( &$this, 'delete_theme_update_transient' ) ); |
| 774 |
add_action( 'load-update-core.php', array( &$this, 'delete_theme_update_transient' ) ); |
| 775 |
add_action( 'load-themes.php', array( &$this, 'delete_theme_update_transient' ) ); |
| 776 |
add_action( 'load-themes.php', array( &$this, 'load_themes_screen' ) ); |
| 777 |
add_filter( 'http_request_args', array( $this, 'disable_wporg_update' ), 5, 2 ); |
| 778 |
|
| 779 |
return $this; |
| 780 |
|
| 781 |
} |
| 782 |
|
| 783 |
return $this; |
| 784 |
} |
| 785 |
|
| 786 |
/** |
| 787 |
* Register license fields for the products. |
| 788 |
*/ |
| 789 |
public function register_license_hooks() { |
| 790 |
add_action( 'admin_init', array( $this, 'register_settings' ) ); |
| 791 |
add_action( 'admin_init', array( $this, 'activate_license' ) ); |
| 792 |
add_action( 'admin_init', array( $this, 'product_valid' ), 99999999 ); |
| 793 |
add_action( 'admin_notices', array( $this, 'show_notice' ) ); |
| 794 |
add_filter( $this->product->get_key() . '_license_status', array( $this, 'get_license_status' ) ); |
| 795 |
} |
| 796 |
} |
| 797 |
|