| 1 |
<?php |
| 2 |
/** |
| 3 |
* The main loader class for license handling. |
| 4 |
* |
| 5 |
* @package ThemeIsleSDK |
| 6 |
* @subpackage Licenser |
| 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 |
if ( ! class_exists( 'ThemeIsle_SDK_Licenser' ) ) : |
| 12 |
/** |
| 13 |
* Class ThemeIsle_SDK_Licenser |
| 14 |
* |
| 15 |
* Used to update the themeisle products |
| 16 |
*/ |
| 17 |
class ThemeIsle_SDK_Licenser { |
| 18 |
|
| 19 |
/** |
| 20 |
* @var string $license_key The license key string |
| 21 |
*/ |
| 22 |
public $license_key; |
| 23 |
|
| 24 |
/** |
| 25 |
* @var bool $do_check This ensures that the custom API request only runs on the second time that WP fires the update check |
| 26 |
*/ |
| 27 |
private $do_check = false; |
| 28 |
|
| 29 |
/** |
| 30 |
* @var bool $failed_checks Number of failed checks to the api endpoint |
| 31 |
*/ |
| 32 |
private $failed_checks = 0; |
| 33 |
/** |
| 34 |
* @var ThemeIsle_SDK_Product $product The ThemeIsle Product. |
| 35 |
*/ |
| 36 |
private $product; |
| 37 |
/** |
| 38 |
* @var string $product_key The product update response key. |
| 39 |
*/ |
| 40 |
private $product_key; |
| 41 |
/** |
| 42 |
* @var int $max_failed Maximum failed checks allowed before show the notice |
| 43 |
*/ |
| 44 |
private static $max_failed = 5; |
| 45 |
|
| 46 |
/** |
| 47 |
* ThemeIsle_SDK_Licenser constructor. |
| 48 |
* |
| 49 |
* @param ThemeIsle_SDK_Product $product The product object. |
| 50 |
*/ |
| 51 |
public function __construct( $product ) { |
| 52 |
$this->product = $product; |
| 53 |
$this->product_key = $this->product->get_key() . '-update-response'; |
| 54 |
if ( ! $this->product->requires_license() ) { |
| 55 |
$this->license_key = 'free'; |
| 56 |
} else { |
| 57 |
$license_data = get_option( $this->product->get_key() . '_license_data', '' ); |
| 58 |
$this->failed_checks = intval( get_option( $this->product->get_key() . '_failed_checks', 0 ) ); |
| 59 |
if ( $license_data !== '' ) { |
| 60 |
$this->license_key = isset( $license_data->key ) ? $license_data->key : get_option( $this->product->get_key() . '_license', '' ); |
| 61 |
} else { |
| 62 |
$this->license_key = get_option( $this->product->get_key() . '_license', '' ); |
| 63 |
} |
| 64 |
$this->register_license_hooks(); |
| 65 |
} |
| 66 |
} |
| 67 |
|
| 68 |
/** |
| 69 |
* Register license hooks for the themeisle products |
| 70 |
*/ |
| 71 |
public function register_license_hooks() { |
| 72 |
add_action( 'admin_init', array( $this, 'register_settings' ) ); |
| 73 |
add_action( 'admin_init', array( $this, 'activate_license' ) ); |
| 74 |
add_action( 'admin_init', array( $this, 'product_valid' ), 99999999 ); |
| 75 |
add_action( 'admin_notices', array( $this, 'show_notice' ) ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Register the setting for the license of the product |
| 80 |
* |
| 81 |
* @return bool |
| 82 |
*/ |
| 83 |
public function register_settings() { |
| 84 |
if ( ! is_admin() ) { |
| 85 |
return false; |
| 86 |
} |
| 87 |
add_settings_field( |
| 88 |
$this->product->get_key() . '_license', |
| 89 |
$this->product->get_name() . ' license', |
| 90 |
array( $this, 'license_view' ), |
| 91 |
'general' |
| 92 |
); |
| 93 |
} |
| 94 |
|
| 95 |
/** |
| 96 |
* The license view field |
| 97 |
*/ |
| 98 |
public function license_view() { |
| 99 |
$status = $this->get_license_status(); |
| 100 |
$value = $this->license_key; |
| 101 |
|
| 102 |
$activate_string = apply_filters( $this->product->get_key() . '_lc_activate_string', 'Activate' ); |
| 103 |
$deactivate_string = apply_filters( $this->product->get_key() . '_lc_deactivate_string', 'Deactivate' ); |
| 104 |
$valid_string = apply_filters( $this->product->get_key() . '_lc_valid_string', 'Valid' ); |
| 105 |
$invalid_string = apply_filters( $this->product->get_key() . '_lc_invalid_string', 'Invalid' ); |
| 106 |
$license_message = apply_filters( $this->product->get_key() . '_lc_license_message', 'Enter your license from %s purchase history in order to get %s updates' ); |
| 107 |
|
| 108 |
echo '<p ><input ' . ( ( $status === 'valid' ) ? ( 'style="border:1px solid #7ad03a; "' ) : '' ) . ' type="text" id="' . $this->product->get_key() . '_license" name="' . $this->product->get_key() . '_license" value="' . $value . '" /><a ' . ( ( $status === 'valid' ) ? ( '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" ' . ( ( $status === 'valid' ) ? ( ' 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->product->get_store_url() . '">' . $this->product->get_store_name() . '</a> ', $this->product->get_type() ) . '</p>'; |
| 109 |
|
| 110 |
} |
| 111 |
|
| 112 |
/** |
| 113 |
* Return the license status. |
| 114 |
* |
| 115 |
* @return string The License status. |
| 116 |
*/ |
| 117 |
public function get_license_status() { |
| 118 |
$license_data = get_option( $this->product->get_key() . '_license_data', '' ); |
| 119 |
if ( $license_data !== '' ) { |
| 120 |
return isset( $license_data->license ) ? $license_data->license : get_option( $this->product->get_key() . '_license_status', 'not_active' ); |
| 121 |
} else { |
| 122 |
return get_option( $this->product->get_key() . '_license_status', 'not_active' ); |
| 123 |
} |
| 124 |
|
| 125 |
} |
| 126 |
|
| 127 |
/** |
| 128 |
* Check if the license is active or not |
| 129 |
* |
| 130 |
* @return bool |
| 131 |
*/ |
| 132 |
public function check_activation() { |
| 133 |
$license_data = get_option( $this->product->get_key() . '_license_data', '' ); |
| 134 |
if ( $license_data !== '' ) { |
| 135 |
return isset( $license_data->error ) ? ( $license_data->error == 'no_activations_left' ) : false; |
| 136 |
} |
| 137 |
|
| 138 |
return false; |
| 139 |
} |
| 140 |
|
| 141 |
/** |
| 142 |
* Check if the license is about to expire in the next month |
| 143 |
* |
| 144 |
* @return bool |
| 145 |
*/ |
| 146 |
function check_expiration() { |
| 147 |
$license_data = get_option( $this->product->get_key() . '_license_data', '' ); |
| 148 |
if ( $license_data !== '' ) { |
| 149 |
if ( isset( $license_data->expires ) ) { |
| 150 |
if ( strtotime( $license_data->expires ) - time() < 30 * 24 * 3600 ) { |
| 151 |
return true; |
| 152 |
} |
| 153 |
} |
| 154 |
} |
| 155 |
|
| 156 |
return false; |
| 157 |
} |
| 158 |
|
| 159 |
/** |
| 160 |
* Return the renew url from the store used |
| 161 |
* |
| 162 |
* @return string The renew url. |
| 163 |
*/ |
| 164 |
function renew_url() { |
| 165 |
$license_data = get_option( $this->product->get_key() . '_license_data', '' ); |
| 166 |
if ( $license_data !== '' ) { |
| 167 |
if ( isset( $license_data->download_id ) && isset( $license_data->key ) ) { |
| 168 |
return $this->product->get_store_url() . '/checkout/?edd_license_key=' . $license_data->key . '&download_id=' . $license_data->download_id; |
| 169 |
} |
| 170 |
} |
| 171 |
|
| 172 |
return $this->product->get_store_url(); |
| 173 |
} |
| 174 |
|
| 175 |
/** |
| 176 |
* Check if we hide the notificatin nag or not |
| 177 |
* |
| 178 |
* @param string $hide The notification to hide. |
| 179 |
* |
| 180 |
* @return bool Either hide them or not. |
| 181 |
*/ |
| 182 |
function check_hide( $hide ) { |
| 183 |
if ( isset( $_GET[ $this->product->get_key() . '_hide_' . $hide ] ) ) { |
| 184 |
if ( $_GET[ $this->product->get_key() . '_hide_' . $hide ] === 'yes' ) { |
| 185 |
update_option( $this->product->get_key() . '_hide_' . $hide, 'yes' ); |
| 186 |
|
| 187 |
return false; |
| 188 |
} |
| 189 |
} else { |
| 190 |
$license = get_option( $this->product->get_key() . '_hide_' . $hide, '' ); |
| 191 |
if ( $license === 'yes' ) { |
| 192 |
return false; |
| 193 |
} |
| 194 |
} |
| 195 |
|
| 196 |
return true; |
| 197 |
} |
| 198 |
|
| 199 |
/** |
| 200 |
* Show the admin notice regarding the license status |
| 201 |
* |
| 202 |
* @return bool |
| 203 |
*/ |
| 204 |
function show_notice() { |
| 205 |
if ( ! is_admin() ) { |
| 206 |
return false; |
| 207 |
} |
| 208 |
$status = $this->get_license_status(); |
| 209 |
$no_activations_string = apply_filters( $this->product->get_key() . '_lc_no_activations_string', 'No activations left for %s !!!. You need to |
| 210 |
upgrade your plan in order to use %s on more |
| 211 |
websites. Please ask the %s |
| 212 |
Staff for more details.' ); |
| 213 |
$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 |
| 214 |
your license code from your <a href="%s" target="_blank">purchase history</a> and validate it <a |
| 215 |
href="%s">here</a> ' ); |
| 216 |
$expiration_string = apply_filters( $this->product->get_key() . '_lc_expiration_string', 'Your license is about to expire |
| 217 |
for %s. You can go to %s and renew it ' ); |
| 218 |
$hide_notice_string = apply_filters( $this->product->get_key() . '_lc_hide_notice_string', 'Hide Notice' ); |
| 219 |
if ( $status != 'valid' ) { |
| 220 |
if ( $this->check_activation() ) { |
| 221 |
if ( $this->check_hide( 'activation' ) ) { |
| 222 |
?> |
| 223 |
<div class="error"> |
| 224 |
<p><strong><?php |
| 225 |
echo sprintf( $no_activations_string, $this->product->get_name(), $this->product->get_name(), '<a href="' . $this->product->get_store_url() . '" |
| 226 |
target="_blank">' . $this->product->get_store_name() . '</a>' ); |
| 227 |
?></strong> | <a |
| 228 |
href="<?php echo add_query_arg( $this->product->get_key() . '_activation', 'yes' ); ?> "><?php echo $hide_notice_string; ?></a> |
| 229 |
</p> |
| 230 |
</div> |
| 231 |
<?php |
| 232 |
return false; |
| 233 |
} |
| 234 |
} |
| 235 |
?> |
| 236 |
<?php if ( $this->check_hide( 'valid' ) ) : ?> |
| 237 |
<div class="error"> |
| 238 |
<p> |
| 239 |
<strong><?php echo sprintf( $no_valid_string, $this->product->get_name() . ' ' . $this->product->get_type(), $this->product->get_type(), admin_url( 'options-general.php' ) . '#' . $this->product->get_key() ); ?> </strong>| |
| 240 |
<a |
| 241 |
href="<?php echo add_query_arg( $this->product->get_key() . '_hide_valid', 'yes' ); ?> "><?php echo $hide_notice_string; ?></a> |
| 242 |
</p> |
| 243 |
</div> |
| 244 |
<?php endif; ?> |
| 245 |
<?php |
| 246 |
} else { |
| 247 |
if ( $this->check_expiration() ) { |
| 248 |
if ( $this->check_hide( 'expiration' ) ) { |
| 249 |
?> |
| 250 |
<div class="update-nag"> |
| 251 |
<p> |
| 252 |
<strong><?php echo sprintf( $expiration_string, $this->product->get_name() . ' ' . $this->product->get_type(), '<a |
| 253 |
href="' . $this->renew_url() . '" |
| 254 |
target="_blank">' . $this->product->get_store_name() . '</a>' ); ?> </strong> | |
| 255 |
<a |
| 256 |
href="<?php echo add_query_arg( $this->product->get_key() . '_hide_expiration', 'yes' ); ?> "><?php echo $hide_notice_string; ?></a> |
| 257 |
</p> |
| 258 |
</div> |
| 259 |
<?php |
| 260 |
} |
| 261 |
} |
| 262 |
} |
| 263 |
} |
| 264 |
|
| 265 |
/** |
| 266 |
* Run the license check call |
| 267 |
*/ |
| 268 |
public function product_valid() { |
| 269 |
if ( false === ( $license = get_transient( $this->product->get_key() . '_license_data' ) ) ) { |
| 270 |
$license = $this->check_license(); |
| 271 |
set_transient( $this->product->get_key() . '_license_data', $license, 12 * HOUR_IN_SECONDS ); |
| 272 |
update_option( $this->product->get_key() . '_license_data', $license ); |
| 273 |
} |
| 274 |
|
| 275 |
} |
| 276 |
|
| 277 |
/** |
| 278 |
* Increment the failed checks |
| 279 |
*/ |
| 280 |
private function increment_failed_checks() { |
| 281 |
$this->failed_checks ++; |
| 282 |
update_option( $this->product->get_key() . '_failed_checks', $this->failed_checks ); |
| 283 |
} |
| 284 |
|
| 285 |
/** |
| 286 |
* Reset the failed checks |
| 287 |
*/ |
| 288 |
private function reset_failed_checks() { |
| 289 |
$this->failed_checks = 1; |
| 290 |
update_option( $this->product->get_key() . '_failed_checks', $this->failed_checks ); |
| 291 |
} |
| 292 |
|
| 293 |
/** |
| 294 |
* Check the license status |
| 295 |
* |
| 296 |
* @return object The license data. |
| 297 |
*/ |
| 298 |
public function check_license() { |
| 299 |
$status = $this->get_license_status(); |
| 300 |
if ( $status == 'not_active' ) { |
| 301 |
$license_data = new stdClass(); |
| 302 |
$license_data->license = 'not_active'; |
| 303 |
|
| 304 |
return $license_data; |
| 305 |
} |
| 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( add_query_arg( $api_params, $this->product->get_store_url() ), array( |
| 315 |
'timeout' => 15, |
| 316 |
'sslverify' => false, |
| 317 |
) ); |
| 318 |
if ( is_wp_error( $response ) ) { |
| 319 |
$license_data = new stdClass(); |
| 320 |
$license_data->license = 'valid'; |
| 321 |
|
| 322 |
} else { |
| 323 |
$license_data = json_decode( wp_remote_retrieve_body( $response ) ); |
| 324 |
if ( ! is_object( $license_data ) ) { |
| 325 |
$license_data = new stdClass(); |
| 326 |
$license_data->license = 'valid'; |
| 327 |
} |
| 328 |
} |
| 329 |
$license_old = get_option( $this->product->get_key() . '_license_data', '' ); |
| 330 |
if ( $license_old->license == 'valid' && ( $license_data->license != $license_old->license ) ) { |
| 331 |
$this->increment_failed_checks(); |
| 332 |
} else { |
| 333 |
$this->reset_failed_checks(); |
| 334 |
} |
| 335 |
|
| 336 |
if ( $this->failed_checks <= self::$max_failed ) { |
| 337 |
return $license_old; |
| 338 |
} |
| 339 |
|
| 340 |
if ( isset( $license_old->hide_valid ) ) { |
| 341 |
$license_data->hide_valid = true; |
| 342 |
} |
| 343 |
|
| 344 |
if ( ! isset( $license_data->key ) ) { |
| 345 |
$license_data->key = isset( $license_old->key ) ? $license_old->key : ''; |
| 346 |
} |
| 347 |
|
| 348 |
if ( isset( $license_old->hide_expiration ) ) { |
| 349 |
$license_data->hide_expiration = true; |
| 350 |
} |
| 351 |
|
| 352 |
if ( isset( $license_old->hide_activation ) ) { |
| 353 |
$license_data->hide_activation = true; |
| 354 |
} |
| 355 |
|
| 356 |
return $license_data; |
| 357 |
|
| 358 |
} |
| 359 |
|
| 360 |
/** |
| 361 |
* Activate the license remotely |
| 362 |
*/ |
| 363 |
function activate_license() { |
| 364 |
// listen for our activate button to be clicked |
| 365 |
if ( isset( $_POST[ $this->product->get_key() . '_btn_trigger' ] ) ) { |
| 366 |
$status = $this->get_license_status(); |
| 367 |
// retrieve the license from the database |
| 368 |
$license = $_POST[ $this->product->get_key() . '_license' ]; |
| 369 |
$api_params = array( |
| 370 |
'license' => $license, |
| 371 |
'item_name' => rawurlencode( $this->product->get_name() ), |
| 372 |
'url' => rawurlencode( home_url() ), |
| 373 |
); |
| 374 |
if ( $status != 'valid' ) { |
| 375 |
// data to send in our API request |
| 376 |
$api_params['edd_action'] = 'activate_license'; |
| 377 |
} else { |
| 378 |
$api_params['edd_action'] = 'deactivate_license'; |
| 379 |
} |
| 380 |
// Call the custom API. |
| 381 |
$response = wp_remote_get( add_query_arg( $api_params, $this->product->get_store_url() ) ); |
| 382 |
// make sure the response came back okay |
| 383 |
if ( is_wp_error( $response ) ) { |
| 384 |
$license_data = new stdClass(); |
| 385 |
$license_data->license = ( $status != 'valid' ) ? 'valid' : 'invalid'; |
| 386 |
|
| 387 |
} else { |
| 388 |
$license_data = json_decode( wp_remote_retrieve_body( $response ) ); |
| 389 |
if ( ! is_object( $license_data ) ) { |
| 390 |
$license_data = new stdClass(); |
| 391 |
$license_data->license = ( $status != 'valid' ) ? 'valid' : 'invalid'; |
| 392 |
} |
| 393 |
} |
| 394 |
if ( ! isset( $license_data->key ) ) { |
| 395 |
$license_data->key = $license; |
| 396 |
} |
| 397 |
if ( $license_data->license == 'valid' ) { |
| 398 |
$this->reset_failed_checks(); |
| 399 |
} |
| 400 |
|
| 401 |
if ( isset( $license_data->plan ) ) { |
| 402 |
update_option( $this->product->get_key() . '_license_plan', $license_data->plan ); |
| 403 |
} |
| 404 |
|
| 405 |
update_option( $this->product->get_key() . '_license_data', $license_data ); |
| 406 |
delete_transient( $this->product->get_key() . '_license_data' ); |
| 407 |
set_transient( $this->product->get_key() . '_license_data', $license_data, 12 * HOUR_IN_SECONDS ); |
| 408 |
|
| 409 |
} |
| 410 |
} |
| 411 |
|
| 412 |
/** |
| 413 |
* Enable the license system |
| 414 |
*/ |
| 415 |
public function enable() { |
| 416 |
if ( $this->product->get_type() == 'plugin' ) { |
| 417 |
add_filter( 'pre_set_site_transient_update_plugins', array( |
| 418 |
$this, |
| 419 |
'pre_set_site_transient_update_plugins_filter', |
| 420 |
) ); |
| 421 |
add_filter( 'plugins_api', array( $this, 'plugins_api_filter' ), 10, 3 ); |
| 422 |
add_filter( 'http_request_args', array( $this, 'http_request_args' ), 10, 2 ); |
| 423 |
} |
| 424 |
if ( $this->product->get_type() == 'theme' ) { |
| 425 |
add_filter( 'site_transient_update_themes', array( &$this, 'theme_update_transient' ) ); |
| 426 |
add_filter( 'delete_site_transient_update_themes', array( &$this, 'delete_theme_update_transient' ) ); |
| 427 |
add_action( 'load-update-core.php', array( &$this, 'delete_theme_update_transient' ) ); |
| 428 |
add_action( 'load-themes.php', array( &$this, 'delete_theme_update_transient' ) ); |
| 429 |
add_action( 'load-themes.php', array( &$this, 'load_themes_screen' ) ); |
| 430 |
|
| 431 |
} |
| 432 |
|
| 433 |
} |
| 434 |
|
| 435 |
/** |
| 436 |
* Load the Themes screen |
| 437 |
*/ |
| 438 |
function load_themes_screen() { |
| 439 |
add_thickbox(); |
| 440 |
add_action( 'admin_notices', array( &$this, 'update_nag' ) ); |
| 441 |
} |
| 442 |
|
| 443 |
/** |
| 444 |
* Alter the nag for themes update |
| 445 |
*/ |
| 446 |
function update_nag() { |
| 447 |
$theme = wp_get_theme( $this->product->get_slug() ); |
| 448 |
$api_response = get_transient( $this->product_key ); |
| 449 |
if ( false === $api_response ) { |
| 450 |
return; |
| 451 |
} |
| 452 |
$update_url = wp_nonce_url( 'update.php?action=upgrade-theme&theme=' . urlencode( $this->product->get_slug() ), 'upgrade-theme_' . $this->product->get_slug() ); |
| 453 |
$update_onclick = ' onclick="if ( confirm(\'' . esc_js( __( 'Updating this theme will lose any customizations you have made. Cancel to stop, OK to update.' ) ) . '\') ) {return true;}return false;"'; |
| 454 |
if ( version_compare( $this->product->get_version(), $api_response->new_version, '<' ) ) { |
| 455 |
echo '<div id="update-nag">'; |
| 456 |
printf( '<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>.', |
| 457 |
$theme->get( 'Name' ), |
| 458 |
$api_response->new_version, |
| 459 |
'#TB_inline?width=640&inlineId=' . $this->product->get_version() . '_changelog', |
| 460 |
$theme->get( 'Name' ), |
| 461 |
$update_url, |
| 462 |
$update_onclick |
| 463 |
); |
| 464 |
echo '</div>'; |
| 465 |
echo '<div id="' . $this->product->get_slug() . '_' . 'changelog" style="display:none;">'; |
| 466 |
echo wpautop( $api_response->sections['changelog'] ); |
| 467 |
echo '</div>'; |
| 468 |
} |
| 469 |
} |
| 470 |
|
| 471 |
/** |
| 472 |
* @param mixed $value The transient data. |
| 473 |
* |
| 474 |
* @return mixed |
| 475 |
*/ |
| 476 |
function theme_update_transient( $value ) { |
| 477 |
$update_data = $this->check_for_update(); |
| 478 |
if ( $update_data ) { |
| 479 |
$value->response[ $this->product->get_slug() ] = $update_data; |
| 480 |
} |
| 481 |
|
| 482 |
return $value; |
| 483 |
} |
| 484 |
|
| 485 |
/** |
| 486 |
* Delete the update transient |
| 487 |
*/ |
| 488 |
function delete_theme_update_transient() { |
| 489 |
delete_transient( $this->product_key ); |
| 490 |
} |
| 491 |
|
| 492 |
/** |
| 493 |
* Check for updates |
| 494 |
* |
| 495 |
* @return array|bool Either the update data or false in case of failure |
| 496 |
*/ |
| 497 |
function check_for_update() { |
| 498 |
$theme = wp_get_theme( $this->product->get_slug() ); |
| 499 |
$update_data = get_transient( $this->product_key ); |
| 500 |
if ( false === $update_data ) { |
| 501 |
$failed = false; |
| 502 |
if ( empty( $this->license_key ) ) { |
| 503 |
return false; |
| 504 |
} |
| 505 |
$api_params = array( |
| 506 |
'edd_action' => 'get_version', |
| 507 |
'version' => $this->product->get_version(), |
| 508 |
'license' => $this->license_key, |
| 509 |
'name' => $this->product->get_name(), |
| 510 |
'slug' => $this->product->get_slug(), |
| 511 |
'author' => $this->product->get_store_name(), |
| 512 |
'url' => rawurlencode( home_url() ), |
| 513 |
); |
| 514 |
$response = wp_remote_post( $this->product->get_store_url(), array( |
| 515 |
'timeout' => 15, |
| 516 |
'sslverify' => false, |
| 517 |
'body' => $api_params, |
| 518 |
) ); |
| 519 |
// make sure the response was successful |
| 520 |
if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) ) { |
| 521 |
$failed = true; |
| 522 |
} |
| 523 |
$update_data = json_decode( wp_remote_retrieve_body( $response ) ); |
| 524 |
if ( ! is_object( $update_data ) ) { |
| 525 |
$failed = true; |
| 526 |
} |
| 527 |
// if the response failed, try again in 30 minutes |
| 528 |
if ( $failed ) { |
| 529 |
$data = new stdClass; |
| 530 |
$data->new_version = $this->product->get_version(); |
| 531 |
set_transient( $this->product_key, $data, strtotime( '+30 minutes' ) ); |
| 532 |
|
| 533 |
return false; |
| 534 |
} |
| 535 |
// if the status is 'ok', return the update arguments |
| 536 |
if ( ! $failed ) { |
| 537 |
$update_data->sections = maybe_unserialize( $update_data->sections ); |
| 538 |
set_transient( $this->product_key, $update_data, strtotime( '+12 hours' ) ); |
| 539 |
} |
| 540 |
} |
| 541 |
if ( version_compare( $this->product->get_version(), $update_data->new_version, '>=' ) ) { |
| 542 |
return false; |
| 543 |
} |
| 544 |
|
| 545 |
return (array) $update_data; |
| 546 |
} |
| 547 |
|
| 548 |
/** |
| 549 |
* Check for Updates at the defined API endpoint and modify the update array. |
| 550 |
* |
| 551 |
* This function dives into the update API just when WordPress creates its update array, |
| 552 |
* then adds a custom API call and injects the custom plugin data retrieved from the API. |
| 553 |
* It is reassembled from parts of the native WordPress plugin update code. |
| 554 |
* See wp-includes/update.php line 121 for the original wp_update_plugins() function. |
| 555 |
* |
| 556 |
* @uses api_request() |
| 557 |
* |
| 558 |
* @param array $_transient_data Update array build by WordPress. |
| 559 |
* |
| 560 |
* @return array Modified update array with custom plugin data. |
| 561 |
*/ |
| 562 |
public function pre_set_site_transient_update_plugins_filter( $_transient_data ) { |
| 563 |
if ( empty( $_transient_data ) || ! $this->do_check ) { |
| 564 |
$this->do_check = true; |
| 565 |
|
| 566 |
return $_transient_data; |
| 567 |
} |
| 568 |
$api_response = $this->api_request(); |
| 569 |
if ( false !== $api_response && is_object( $api_response ) && isset( $api_response->new_version ) ) { |
| 570 |
if ( version_compare( $this->product->get_version(), $api_response->new_version, '<' ) ) { |
| 571 |
$_transient_data->response[ $this->product->get_slug() . '/' . $this->product->get_file() ] = $api_response; |
| 572 |
} |
| 573 |
} |
| 574 |
|
| 575 |
return $_transient_data; |
| 576 |
} |
| 577 |
|
| 578 |
/** |
| 579 |
* Calls the API and, if successfull, returns the object delivered by the API. |
| 580 |
* |
| 581 |
* @uses get_bloginfo() |
| 582 |
* @uses wp_remote_post() |
| 583 |
* @uses is_wp_error() |
| 584 |
* |
| 585 |
* @param string $_action The requested action. |
| 586 |
* @param array $_data Parameters for the API action. |
| 587 |
* |
| 588 |
* @return false||object |
| 589 |
*/ |
| 590 |
private function api_request( $_action = '', $_data = '' ) { |
| 591 |
if ( empty( $this->license_key ) ) { |
| 592 |
return; |
| 593 |
} |
| 594 |
$api_params = array( |
| 595 |
'edd_action' => 'get_version', |
| 596 |
'license' => $this->license_key, |
| 597 |
'name' => rawurlencode( $this->product->get_name() ), |
| 598 |
'slug' => rawurlencode( $this->product->get_slug() ), |
| 599 |
'author' => $this->product->get_store_name(), |
| 600 |
'url' => rawurlencode( home_url() ), |
| 601 |
); |
| 602 |
$request = wp_remote_post( $this->product->get_store_url(), array( |
| 603 |
'timeout' => 15, |
| 604 |
'sslverify' => false, |
| 605 |
'body' => $api_params, |
| 606 |
) ); |
| 607 |
if ( ! is_wp_error( $request ) ) : |
| 608 |
$request = json_decode( wp_remote_retrieve_body( $request ) ); |
| 609 |
if ( $request && isset( $request->sections ) ) { |
| 610 |
$request->sections = maybe_unserialize( $request->sections ); |
| 611 |
} |
| 612 |
|
| 613 |
return $request; |
| 614 |
else : |
| 615 |
return false; |
| 616 |
endif; |
| 617 |
} |
| 618 |
|
| 619 |
/** |
| 620 |
* Updates information on the "View version x.x details" page with custom data. |
| 621 |
* |
| 622 |
* @uses api_request() |
| 623 |
* |
| 624 |
* @param mixed $_data Plugin data. |
| 625 |
* @param string $_action Action to send. |
| 626 |
* @param object $_args Arguments to use. |
| 627 |
* |
| 628 |
* @return object $_data |
| 629 |
*/ |
| 630 |
public function plugins_api_filter( $_data, $_action = '', $_args = null ) { |
| 631 |
if ( ( $_action != 'plugin_information' ) || ! isset( $_args->slug ) || ( $_args->slug != $this->product->get_slug() ) ) { |
| 632 |
return $_data; |
| 633 |
} |
| 634 |
$api_response = $this->api_request(); |
| 635 |
if ( false !== $api_response ) { |
| 636 |
$_data = $api_response; |
| 637 |
} |
| 638 |
|
| 639 |
return $_data; |
| 640 |
} |
| 641 |
|
| 642 |
/** |
| 643 |
* Disable SSL verification in order to prevent download update failures |
| 644 |
* |
| 645 |
* @param array $args Http args. |
| 646 |
* @param string $url Url to check. |
| 647 |
* |
| 648 |
* @return object $array |
| 649 |
*/ |
| 650 |
function http_request_args( $args, $url ) { |
| 651 |
// If it is an https request and we are performing a package download, disable ssl verification |
| 652 |
if ( strpos( $url, 'https://' ) !== false && strpos( $url, 'edd_action=package_download' ) ) { |
| 653 |
$args['sslverify'] = false; |
| 654 |
} |
| 655 |
|
| 656 |
return $args; |
| 657 |
} |
| 658 |
|
| 659 |
} |
| 660 |
endif; |
| 661 |
|