PluginProbe
Visualizer – Tables & Charts Manager with Built-in AI Generator / 3.9.2
Visualizer – Tables & Charts Manager with Built-in AI Generator v3.9.2
4.0.8 4.0.7 4.0.6 4.0.5 4.0.4 4.0.3 3.0.5 3.0.6 3.0.7 3.0.8 3.0.9 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.10.1 3.10.10 3.10.11 3.10.12 3.10.13 3.10.14 3.10.15 3.10.2 3.10.3 All 149 releases
← All changes | vendor/codeinwp/themeisle-sdk/src/Modules/Licenser.php +591 -155 3.1.33.9.2 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;
@@ -22,16 +23,33 @@
22 23 /**
23 24 * Licenser module for ThemeIsle SDK.
24 25 */
25 26 class Licenser extends Abstract_Module {
26 -
27 27 /**
28 + * License VALID status string.
29 + */
30 + const STATUS_VALID = 'valid';
31 + /**
32 + * License NOT_ACTIVE status string.
33 + */
34 + const STATUS_NOT_ACTIVE = 'not_active';
35 + /**
36 + * License active expired status string.
37 + */
38 + const STATUS_ACTIVE_EXPIRED = 'active_expired';
39 + /**
28 40 * Number of max failed checks before showing the license message.
29 41 *
30 42 * @var int $max_failed Maximum failed checks allowed before show the notice
31 43 */
32 - private static $max_failed = 5;
44 + private static $max_failed = 1;
33 45 /**
46 + * Flag to check if the global actions were loaded.
47 + *
48 + * @var bool If the globals actions were loaded.
49 + */
50 + private static $globals_loaded = false;
51 + /**
34 52 * License key string.
35 53 *
36 54 * @var string $license_key The license key string
37 55 */
@@ -55,8 +73,21 @@
55 73 */
56 74 private $product_key;
57 75
58 76 /**
77 + * Holds local license object.
78 + *
79 + * @var null Local license object.
80 + */
81 + private $license_local = null;
82 + /**
83 + * Product namespace, used for fixed name filters/cli commands.
84 + *
85 + * @var string $namespace Product namespace.
86 + */
87 + private $namespace = null;
88 +
89 + /**
59 90 * Disable wporg updates for premium products.
60 91 *
61 92 * @param string $r Update payload.
62 93 * @param string $url The api url.
@@ -62,9 +93,9 @@
62 93 * @param string $url The api url.
63 94 *
64 95 * @return mixed List of themes to check for update.
65 96 */
66 - function disable_wporg_update( $r, $url ) {
97 + public function disable_wporg_update( $r, $url ) {
67 98
68 99 if ( 0 !== strpos( $url, 'https://api.wordpress.org/themes/update-check/' ) ) {
69 100 return $r;
70 101 }
@@ -74,9 +105,9 @@
74 105
75 106 unset( $themes->themes->{$this->product->get_slug()} );
76 107
77 108 // Encode the updated JSON response.
78 - $r['body']['themes'] = json_encode( $themes );
109 + $r['body']['themes'] = wp_json_encode( $themes );
79 110
80 111 return $r;
81 112 }
82 113
@@ -88,8 +119,11 @@
88 119 public function register_settings() {
89 120 if ( ! is_admin() ) {
90 121 return false;
91 122 }
123 + if ( apply_filters( $this->product->get_key() . '_hide_license_field', false ) ) {
124 + return;
125 + }
92 126 add_settings_field(
93 127 $this->product->get_key() . '_license',
94 128 $this->product->get_name() . ' license',
95 129 array( $this, 'license_view' ),
@@ -108,19 +142,83 @@
108 142 $deactivate_string = apply_filters( $this->product->get_key() . '_lc_deactivate_string', 'Deactivate' );
109 143 $valid_string = apply_filters( $this->product->get_key() . '_lc_valid_string', 'Valid' );
110 144 $invalid_string = apply_filters( $this->product->get_key() . '_lc_invalid_string', 'Invalid' );
111 145 $license_message = apply_filters( $this->product->get_key() . '_lc_license_message', 'Enter your license from %s purchase history in order to get %s updates' );
146 + $error_message = $this->get_error();
147 + ?>
148 + <style type="text/css">
149 + input.themeisle-sdk-text-input-valid {
150 + border: 1px solid #7ad03a;
151 + }
112 152
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>&nbsp;&nbsp;&nbsp;<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>';
153 + input.themeisle-sdk-license-input {
154 + width: 300px;
155 + padding: 0 8px;
156 + line-height: 2;
157 + min-height: 30px;
158 + }
114 159
160 + .themeisle-sdk-license-deactivate-cta {
161 + color: #fff;
162 + background: #7ad03a;
163 + display: inline-block;
164 + text-decoration: none;
165 + font-size: 13px;
166 + line-height: 30px;
167 + height: 26px;
168 + margin-left: 5px;
169 + padding: 0 10px 3px;
170 + -webkit-border-radius: 3px;
171 + border-radius: 3px;
172 + }
173 +
174 + .themeisle-sdk-license-activate-cta {
175 + color: #fff;
176 + background: #dd3d36;
177 + display: inline-block;
178 + text-decoration: none;
179 + font-size: 13px;
180 + line-height: 30px;
181 + height: 26px;
182 + margin-left: 5px;
183 + padding: 0 10px 3px;
184 + -webkit-border-radius: 3px;
185 + border-radius: 3px;
186 + }
187 +
188 + button.button.themeisle-sdk-licenser-button-cta {
189 + line-height: 26px;
190 + height: 29px;
191 + vertical-align: top;
192 + }
193 +
194 + </style>
195 + <?php
196 + echo sprintf(
197 + '<p>%s<input class="themeisle-sdk-license-input %s" type="text" id="%s_license" name="%s_license" value="%s" /><a class="%s">%s</a>&nbsp;&nbsp;&nbsp;<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',
198 + ( ( 'valid' === $status ) ? sprintf( '<input type="hidden" value="%s" name="%s_license" />', esc_attr( $value ), esc_attr( $this->product->get_key() ) ) : '' ),
199 + ( ( 'valid' === $status ) ? 'themeisle-sdk-text-input-valid' : '' ),
200 + esc_attr( $this->product->get_key() ),
201 + esc_attr( ( ( 'valid' === $status ) ? $this->product->get_key() . '_mask' : $this->product->get_key() ) ),
202 + esc_attr( ( ( 'valid' === $status ) ? ( str_repeat( '*', 30 ) . substr( $value, - 5 ) ) : $value ) ),
203 + esc_attr( ( 'valid' === $status ? 'themeisle-sdk-license-deactivate-cta' : 'themeisle-sdk-license-activate-cta' ) ),
204 + esc_attr( 'valid' === $status ? $valid_string : $invalid_string ),
205 + esc_attr( $this->product->get_key() ),
206 + esc_attr( 'valid' === $status ? $deactivate_string : $activate_string ),
207 + sprintf( wp_kses_data( $license_message ), '<a href="' . esc_url( $this->get_api_url() ) . '">' . esc_attr( $this->get_distributor_name() ) . '</a> ', esc_attr( $this->product->get_type() ) ),
208 + wp_kses_data( empty( $error_message ) ? '' : sprintf( '<p style="color:#dd3d36">%s</p>', ( $error_message ) ) )
209 + ) . wp_nonce_field( $this->product->get_key() . 'nonce', $this->product->get_key() . 'nonce_field', false, false );//phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
210 +
115 211 }
116 212
117 213 /**
118 214 * Return the license status.
119 215 *
216 + * @param bool $check_expiration Should check if license is valid, but expired.
217 + *
120 218 * @return string The License status.
121 219 */
122 - public function get_license_status() {
220 + public function get_license_status( $check_expiration = false ) {
123 221
124 222 $license_data = get_option( $this->product->get_key() . '_license_data', '' );
125 223
126 224 if ( '' === $license_data ) {
@@ -125,14 +223,96 @@
125 223
126 224 if ( '' === $license_data ) {
127 225 return get_option( $this->product->get_key() . '_license_status', 'not_active' );
128 226 }
227 + $status = isset( $license_data->license ) ? $license_data->license : get_option( $this->product->get_key() . '_license_status', 'not_active' );
228 + if ( false === $check_expiration ) {
229 + return $status;
230 + }
129 231
130 - return isset( $license_data->license ) ? $license_data->license : get_option( $this->product->get_key() . '_license_status', 'not_active' );
232 + return ( 'valid' === $status && isset( $license_data->is_expired ) && 'yes' === $license_data->is_expired ) ? 'active_expired' : $status;
233 + }
131 234
235 + /**
236 + * Check status.
237 + *
238 + * @param string $product_file Product basefile.
239 + *
240 + * @return string Status license.
241 + */
242 + public static function status( $product_file ) {
243 + $product = Product::get( $product_file );
244 + if ( ! $product->requires_license() ) {
245 + return self::STATUS_VALID;
246 + }
247 + $license_data = self::get_license_data( $product->get_key() );
248 +
249 + $status = isset( $license_data->license ) ? $license_data->license : self::STATUS_NOT_ACTIVE;
250 +
251 + return ( 'valid' === $status && isset( $license_data->is_expired ) && 'yes' === $license_data->is_expired ) ? 'active_expired' : $status;
132 252 }
133 253
134 254 /**
255 + * Product license data.
256 + *
257 + * @param string $key Product key.
258 + *
259 + * @return false|mixed|null
260 + */
261 + private static function get_license_data( $key ) {
262 + $license_data = get_option( $key . '_license_data', '' );
263 +
264 + return isset( $license_data->license ) ? $license_data : false;
265 + }
266 +
267 + /**
268 + * Check if license is valid.
269 + *
270 + * @param string $product_file Product basefile.
271 + *
272 + * @return bool Is valid?
273 + */
274 + public static function is_valid( $product_file ) {
275 + return self::status( $product_file ) === self::STATUS_VALID;
276 + }
277 +
278 + /**
279 + * Get product plan.
280 + *
281 + * @param string $product_file Product file.
282 + *
283 + * @return int Plan id.
284 + */
285 + public static function plan( $product_file ) {
286 + $product = Product::get( $product_file );
287 + $data = self::get_license_data( $product->get_key() );
288 +
289 + return isset( $data->price_id ) ? (int) $data->price_id : - 1;
290 + }
291 +
292 + /**
293 + * Get product license key.
294 + *
295 + * @param string $product_file Product file.
296 + *
297 + * @return string
298 + */
299 + public static function key( $product_file ) {
300 + $product = Product::get( $product_file );
301 +
302 + return $product->get_license();
303 + }
304 +
305 + /**
306 + * Return the last error message.
307 + *
308 + * @return mixed Error message.
309 + */
310 + public function get_error() {
311 + return get_transient( $this->product->get_key() . 'act_err' );
312 + }
313 +
314 + /**
135 315 * Get remote api url.
136 316 *
137 317 * @return string Remote api url.
138 318 */
@@ -150,9 +330,9 @@
150 330 * @return string Remote api url.
151 331 */
152 332 public function get_distributor_name() {
153 333 if ( $this->is_from_partner( $this->product ) ) {
154 - return 'ThemeIsle';
334 + return 'Themeisle';
155 335 }
156 336
157 337 return $this->product->get_store_name();
158 338 }
@@ -157,21 +337,34 @@
157 337 return $this->product->get_store_name();
158 338 }
159 339
160 340 /**
341 + * License price id.
342 + *
343 + * @return int License plan.
344 + */
345 + public function get_plan() {
346 + return self::plan( $this->product->get_basefile() );
347 + }
348 +
349 + /**
161 350 * Show the admin notice regarding the license status.
162 351 *
163 352 * @return bool Should we show the notice ?
164 353 */
165 - function show_notice() {
354 + public function show_notice() {
166 355 if ( ! is_admin() ) {
167 356 return false;
168 357 }
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 358
359 + if ( apply_filters( $this->product->get_key() . '_hide_license_notices', false ) ) {
360 + return false;
361 + }
362 +
363 + $status = $this->get_license_status( true );
364 + $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.' );
365 + $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>. ' );
366 + $expired_license_string = apply_filters( $this->product->get_key() . '_lc_expired_string', 'Your %s\'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 367 // No activations left for this license.
175 368 if ( 'valid' != $status && $this->check_activation() ) {
176 369 ?>
177 370 <div class="error">
@@ -177,12 +370,12 @@
177 370 <div class="error">
178 371 <p><strong>
179 372 <?php
180 373 echo sprintf(
181 - $no_activations_string,
182 - $this->product->get_name(),
183 - $this->product->get_name(),
184 - '<a href="' . $this->get_api_url() . '" target="_blank">' . $this->get_distributor_name() . '</a>'
374 + wp_kses_data( $no_activations_string ),
375 + esc_attr( $this->product->get_name() ),
376 + esc_attr( $this->product->get_name() ),
377 + '<a href="' . esc_url( $this->get_api_url() ) . '" target="_blank">' . esc_attr( $this->get_distributor_name() ) . '</a>'
185 378 );
186 379 ?>
187 380 </strong>
188 381 </p>
@@ -189,14 +382,15 @@
189 382 </div>
190 383 <?php
191 384 return false;
192 385 }
386 +
193 387 // Invalid license key.
194 - if ( 'valid' != $status ) {
388 + if ( 'active_expired' === $status ) {
195 389 ?>
196 390 <div class="error">
197 391 <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>
392 + <strong><?php echo sprintf( wp_kses_data( $expired_license_string ), esc_attr( $this->product->get_name() . ' ' . $this->product->get_type() ), esc_url( $this->get_api_url() . '?license=' . $this->license_key ) ); ?> </strong>
199 393 </p>
200 394 </div>
201 395 <?php
202 396
@@ -201,26 +395,18 @@
201 395 <?php
202 396
203 397 return false;
204 398 }
205 -
206 - // Expired and soon to expire license.
207 - if ( 'valid' == $status && $this->check_expiration() ) {
399 + // Invalid license key.
400 + if ( 'valid' != $status ) {
208 401 ?>
209 - <div class="update-nag">
402 + <div class="error">
210 403 <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>
404 + <strong><?php echo sprintf( wp_kses_data( $no_valid_string ), esc_attr( $this->product->get_name() . ' ' . $this->product->get_type() ), esc_url( $this->get_api_url() ), esc_url( admin_url( 'options-general.php' ) . '#' . $this->product->get_key() . '_license' ) ); ?> </strong>
220 405 </p>
221 406 </div>
222 407 <?php
408 +
223 409 return false;
224 410 }
225 411
226 412 return true;
@@ -236,9 +422,9 @@
236 422 if ( '' === $license_data ) {
237 423 return false;
238 424 }
239 425
240 - return isset( $license_data->error ) ? ( 'no_activations_left' == $license_data->error ) : false;
426 + return isset( $license_data->license ) ? ( 'no_activations_left' == $license_data->license ) : false;
241 427
242 428 }
243 429
244 430 /**
@@ -245,9 +431,9 @@
245 431 * Check if the license is about to expire in the next month.
246 432 *
247 433 * @return bool
248 434 */
249 - function check_expiration() {
435 + public function check_expiration() {
250 436 $license_data = get_option( $this->product->get_key() . '_license_data', '' );
251 437 if ( '' === $license_data ) {
252 438 return false;
253 439 }
@@ -265,9 +451,9 @@
265 451 * Return the renew url from the store used.
266 452 *
267 453 * @return string The renew url.
268 454 */
269 - function renew_url() {
455 + public function renew_url() {
270 456 $license_data = get_option( $this->product->get_key() . '_license_data', '' );
271 457 if ( '' === $license_data ) {
272 458 return $this->get_api_url();
273 459 }
@@ -274,9 +460,9 @@
274 460 if ( ! isset( $license_data->download_id ) || ! isset( $license_data->key ) ) {
275 461 return $this->get_api_url();
276 462 }
277 463
278 - return $this->get_api_url() . '/checkout/?edd_license_key=' . $license_data->key . '&download_id=' . $license_data->download_id;
464 + return trim( $this->get_api_url(), '/' ) . '/checkout/?edd_license_key=' . $license_data->key . '&download_id=' . $license_data->download_id;
279 465 }
280 466
281 467 /**
282 468 * Run the license check call.
@@ -281,9 +467,9 @@
281 467 /**
282 468 * Run the license check call.
283 469 */
284 470 public function product_valid() {
285 - if ( false !== ( $license = get_transient( $this->product->get_key() . '_license_data' ) ) ) {
471 + if ( false !== ( $license = get_transient( $this->product->get_key() . '_license_data' ) ) ) { //phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.FoundInControlStructure
286 472 return;
287 473 }
288 474 $license = $this->check_license();
289 475 set_transient( $this->product->get_key() . '_license_data', $license, 12 * HOUR_IN_SECONDS );
@@ -296,84 +482,149 @@
296 482 * @return object The license data.
297 483 */
298 484 public function check_license() {
299 485 $status = $this->get_license_status();
300 - if ( 'not_active' == $status ) {
486 + if ( 'not_active' === $status ) {
301 487 $license_data = new \stdClass();
302 488 $license_data->license = 'not_active';
303 489
304 490 return $license_data;
305 491 }
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 - );
492 + $license = trim( $this->license_key );
493 +
494 + $response = $this->do_license_process( $license, 'check' );
495 +
321 496 if ( is_wp_error( $response ) ) {
322 497 $license_data = new \stdClass();
323 - $license_data->license = 'valid';
324 -
498 + $license_data->license = 'invalid';
325 499 } 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 - }
500 + $license_data = $response;
331 501 }
502 +
332 503 $license_old = get_option( $this->product->get_key() . '_license_data', '' );
333 - if ( 'valid' == $license_old->license && ( $license_data->license != $license_old->license ) ) {
504 + if ( 'valid' === $license_old->license && ( $license_data->license !== $license_old->license ) && $this->failed_checks <= self::$max_failed ) {
334 505 $this->increment_failed_checks();
506 +
507 + return $license_old;
508 + }
509 +
510 + if ( ! isset( $license_data->key ) ) {
511 + $license_data->key = isset( $license_old->key ) ? $license_old->key : '';
512 + }
513 + $this->reset_failed_checks();
514 +
515 + return $license_data;
516 +
517 + }
518 +
519 + /**
520 + * Do license activation/deactivation.
521 + *
522 + * @param string $license License key.
523 + * @param string $action What do to.
524 + *
525 + * @return bool|\WP_Error
526 + */
527 + public function do_license_process( $license, $action = 'toggle' ) {
528 + if ( strlen( $license ) < 10 ) {
529 + return new \WP_Error( 'themeisle-license-invalid-format', 'Invalid license.' );
530 + }
531 + $status = $this->get_license_status();
532 +
533 + if ( 'valid' === $status && 'activate' === $action ) {
534 + return new \WP_Error( 'themeisle-license-already-active', 'License is already active.' );
535 + }
536 + if ( 'valid' !== $status && 'deactivate' === $action ) {
537 + return new \WP_Error( 'themeisle-license-already-deactivate', 'License not active.' );
538 + }
539 +
540 + if ( 'toggle' === $action ) {
541 + $action = ( 'valid' !== $status ? ( 'activate' ) : ( 'deactivate' ) );
542 + }
543 +
544 + // Call the custom API.
545 + if ( 'check' === $action ) {
546 + $response = $this->safe_get( sprintf( '%slicense/check/%s/%s/%s/%s', Product::API_URL, rawurlencode( $this->product->get_name() ), $license, rawurlencode( home_url() ), Loader::get_cache_token() ) );
335 547 } else {
336 - $this->reset_failed_checks();
548 + $response = wp_remote_post(
549 + sprintf( '%slicense/%s/%s/%s', Product::API_URL, $action, rawurlencode( $this->product->get_name() ), $license ),
550 + array(
551 + 'body' => wp_json_encode(
552 + array(
553 + 'url' => rawurlencode( home_url() ),
554 + )
555 + ),
556 + 'headers' => array(
557 + 'Content-Type' => 'application/json',
558 + ),
559 + )
560 + );
337 561 }
338 562
339 - if ( $this->failed_checks <= self::$max_failed ) {
340 - return $license_old;
563 + // make sure the response came back okay.
564 + if ( is_wp_error( $response ) ) {
565 + 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() ) );
341 566 }
342 567
343 - if ( isset( $license_old->hide_valid ) ) {
344 - $license_data->hide_valid = true;
568 + $license_data = json_decode( wp_remote_retrieve_body( $response ) );
569 +
570 + if ( ! is_object( $license_data ) ) {
571 + return new \WP_Error( 'themeisle-license-404', 'ERROR: Failed to validate license. Please try again in one minute.' );
345 572 }
573 + if ( 'check' === $action ) {
574 + return $license_data;
575 + }
346 576
577 + Loader::clear_cache_token();
578 +
579 + if ( ! isset( $license_data->license ) ) {
580 + $license_data->license = 'invalid';
581 + }
582 +
347 583 if ( ! isset( $license_data->key ) ) {
348 - $license_data->key = isset( $license_old->key ) ? $license_old->key : '';
584 + $license_data->key = $license;
349 585 }
586 + if ( 'valid' === $license_data->license ) {
587 + $this->reset_failed_checks();
588 + }
350 589
351 - if ( isset( $license_old->hide_expiration ) ) {
352 - $license_data->hide_expiration = true;
590 + if ( 'deactivate' === $action ) {
591 +
592 + delete_option( $this->product->get_key() . '_license_data' );
593 + delete_option( $this->product->get_key() . '_license_plan' );
594 + delete_transient( $this->product->get_key() . '_license_data' );
595 +
596 + return true;
353 597 }
354 -
355 - if ( isset( $license_old->hide_activation ) ) {
356 - $license_data->hide_activation = true;
598 + if ( isset( $license_data->plan ) ) {
599 + update_option( $this->product->get_key() . '_license_plan', $license_data->plan );
357 600 }
601 + update_option( $this->product->get_key() . '_license_data', $license_data );
602 + set_transient( $this->product->get_key() . '_license_data', $license_data, 12 * HOUR_IN_SECONDS );
603 + if ( 'activate' === $action && 'valid' !== $license_data->license ) {
604 + return new \WP_Error( 'themeisle-license-invalid', 'ERROR: Invalid license provided.' );
605 + }
358 606
359 - return $license_data;
607 + // Remove the versions transient upon activation so that newer version for rollback can be acquired.
608 + $versions_cache = $this->product->get_cache_key();
609 + delete_transient( $versions_cache );
360 610
611 + return true;
361 612 }
362 613
363 614 /**
364 - * Increment the failed checks.
615 + * Reset the failed checks
365 616 */
366 - private function increment_failed_checks() {
367 - $this->failed_checks ++;
617 + private function reset_failed_checks() {
618 + $this->failed_checks = 1;
368 619 update_option( $this->product->get_key() . '_failed_checks', $this->failed_checks );
369 620 }
370 621
371 622 /**
372 - * Reset the failed checks
623 + * Increment the failed checks.
373 624 */
374 - private function reset_failed_checks() {
375 - $this->failed_checks = 1;
625 + private function increment_failed_checks() {
626 + $this->failed_checks ++;
376 627 update_option( $this->product->get_key() . '_failed_checks', $this->failed_checks );
377 628 }
378 629
379 630 /**
@@ -378,64 +629,50 @@
378 629
379 630 /**
380 631 * Activate the license remotely.
381 632 */
382 - function activate_license() {
633 + public function process_license() {
383 634 // listen for our activate button to be clicked.
384 635 if ( ! isset( $_POST[ $this->product->get_key() . '_btn_trigger' ] ) ) {
385 636 return;
386 637 }
387 - $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';
398 - } else {
399 - $api_params['edd_action'] = 'deactivate_license';
638 + if ( ! isset( $_POST[ $this->product->get_key() . 'nonce_field' ] )
639 + || ! wp_verify_nonce( $_POST[ $this->product->get_key() . 'nonce_field' ], $this->product->get_key() . 'nonce' ) //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized
640 + ) {
641 + return;
400 642 }
401 - // Call the custom API.
402 - $response = wp_remote_get( add_query_arg( $api_params, $this->get_api_url() ) );
403 - // make sure the response came back okay.
643 + if ( ! current_user_can( 'manage_options' ) ) {
644 + return;
645 + }
646 + $license = isset( $_POST[ $this->product->get_key() . '_license' ] )
647 + ? sanitize_text_field( $_POST[ $this->product->get_key() . '_license' ] )
648 + : '';
649 +
650 + $response = $this->do_license_process( $license, 'toggle' );
404 651 if ( is_wp_error( $response ) ) {
405 - $license_data = new \stdClass();
406 - $license_data->license = ( 'valid' != $status ) ? 'valid' : 'invalid';
652 + $this->set_error( $response->get_error_message() );
407 653
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 - }
654 + return;
417 655 }
418 - if ( ! isset( $license_data->key ) ) {
419 - $license_data->key = $license;
656 + if ( true === $response ) {
657 + $this->set_error( '' );
420 658 }
421 - if ( 'valid' == $license_data->license ) {
422 - $this->reset_failed_checks();
423 - }
659 + }
424 660
425 - if ( isset( $license_data->plan ) ) {
426 - update_option( $this->product->get_key() . '_license_plan', $license_data->plan );
427 - }
661 + /**
662 + * Set license validation error message.
663 + *
664 + * @param string $message Error message.
665 + */
666 + public function set_error( $message = '' ) {
667 + set_transient( $this->product->get_key() . 'act_err', $message, MINUTE_IN_SECONDS );
428 668
429 - update_option( $this->product->get_key() . '_license_data', $license_data );
430 - set_transient( $this->product->get_key() . '_license_data', $license_data, 12 * HOUR_IN_SECONDS );
431 -
432 669 }
433 670
434 671 /**
435 672 * Load the Themes screen.
436 673 */
437 - function load_themes_screen() {
674 + public function load_themes_screen() {
438 675 add_thickbox();
439 676 add_action( 'admin_notices', array( &$this, 'update_nag' ) );
440 677 }
441 678
@@ -441,12 +678,12 @@
441 678
442 679 /**
443 680 * Alter the nag for themes update.
444 681 */
445 - function update_nag() {
682 + public function update_nag() {
446 683 $theme = wp_get_theme( $this->product->get_slug() );
447 684 $api_response = get_transient( $this->product_key );
448 - if ( false === $api_response ) {
685 + if ( false === $api_response || ! isset( $api_response->new_version ) ) {
449 686 return;
450 687 }
451 688 $update_url = wp_nonce_url( 'update.php?action=upgrade-theme&amp;theme=' . urlencode( $this->product->get_slug() ), 'upgrade-theme_' . $this->product->get_slug() );
452 689 $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.' );
@@ -454,18 +691,18 @@
454 691 if ( version_compare( $this->product->get_version(), $api_response->new_version, '<' ) ) {
455 692 echo '<div id="update-nag">';
456 693 printf(
457 694 '<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 - $theme->get( 'Name' ),
459 - $api_response->new_version,
460 - '#TB_inline?width=640&amp;inlineId=' . $this->product->get_version() . '_changelog',
461 - $theme->get( 'Name' ),
462 - $update_url,
463 - $update_onclick
695 + esc_attr( $theme->get( 'Name' ) ),
696 + esc_attr( $api_response->new_version ),
697 + esc_url( sprintf( '%s&TB_iframe=true&amp;width=1024&amp;height=800', $this->product->get_changelog() ) ),
698 + esc_attr( $theme->get( 'Name' ) ),
699 + esc_url( $update_url ),
700 + $update_onclick // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped, Already escaped.
464 701 );
465 702 echo '</div>';
466 - echo '<div id="' . $this->product->get_slug() . '_' . 'changelog" style="display:none;">';
467 - echo wpautop( $api_response->sections['changelog'] );
703 + echo '<div id="' . esc_attr( $this->product->get_slug() ) . '_changelog" style="display:none;">';
704 + echo wp_kses_data( wpautop( $api_response->sections['changelog'] ) );
468 705 echo '</div>';
469 706 }
470 707 }
471 708
@@ -475,14 +712,23 @@
475 712 * @param mixed $value The transient data.
476 713 *
477 714 * @return mixed
478 715 */
479 - function theme_update_transient( $value ) {
716 + public function theme_update_transient( $value ) {
480 717 $update_data = $this->check_for_update();
481 - if ( $update_data ) {
482 - $value->response[ $this->product->get_slug() ] = $update_data;
718 + if ( empty( $value ) ) {
719 + return $value;
483 720 }
484 721
722 + if ( ! isset( $value->response ) ) {
723 + return $value;
724 + }
725 +
726 + if ( ! $update_data ) {
727 + return $value;
728 + }
729 +
730 + $value->response[ $this->product->get_slug() ] = $update_data;
485 731 return $value;
486 732 }
487 733
488 734 /**
@@ -489,9 +735,9 @@
489 735 * Check for updates
490 736 *
491 737 * @return array|bool Either the update data or false in case of failure.
492 738 */
493 - function check_for_update() {
739 + public function check_for_update() {
494 740 $update_data = get_transient( $this->product_key );
495 741
496 742 if ( false === $update_data ) {
497 743 $failed = false;
@@ -506,9 +752,9 @@
506 752 set_transient( $this->product_key, $data, 30 * MINUTE_IN_SECONDS );
507 753
508 754 return false;
509 755 }
510 - $update_data->sections = maybe_unserialize( $update_data->sections );
756 + $update_data->sections = isset( $update_data->sections ) ? maybe_unserialize( $update_data->sections ) : null;
511 757
512 758 set_transient( $this->product_key, $update_data, 12 * HOUR_IN_SECONDS );
513 759 }
514 760 if ( ! isset( $update_data->new_version ) ) {
@@ -526,23 +772,21 @@
526 772 *
527 773 * @return bool|mixed Update api response.
528 774 */
529 775 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(),
776 +
777 + $response = $this->safe_get(
778 + sprintf(
779 + '%slicense/version/%s/%s/%s/%s',
780 + Product::API_URL,
781 + rawurlencode( $this->product->get_name() ),
782 + ( empty( $this->license_key ) ? 'free' : $this->license_key ),
783 + $this->product->get_version(),
784 + rawurlencode( home_url() )
785 + ),
541 786 array(
542 - 'timeout' => 15,
787 + 'timeout' => 15, //phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout, Inherited by wp_remote_get only, for vip environment we use defaults.
543 788 'sslverify' => false,
544 - 'body' => $api_params,
545 789 )
546 790 );
547 791 if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) ) {
548 792 return false;
@@ -550,8 +794,17 @@
550 794 $update_data = json_decode( wp_remote_retrieve_body( $response ) );
551 795 if ( ! is_object( $update_data ) ) {
552 796 return false;
553 797 }
798 + if ( isset( $update_data->slug ) ) {
799 + $update_data->slug = $this->product->get_slug();
800 + }
801 + if ( isset( $update_data->icons ) ) {
802 + $update_data->icons = (array) $update_data->icons;
803 + }
804 + if ( isset( $update_data->banners ) ) {
805 + $update_data->banners = (array) $update_data->banners;
806 + }
554 807
555 808 return $update_data;
556 809 }
557 810
@@ -557,10 +810,10 @@
557 810
558 811 /**
559 812 * Delete the update transient
560 813 */
561 - function delete_theme_update_transient() {
562 - delete_transient( $this->product_key );
814 + public function delete_theme_update_transient() {
815 + return delete_transient( $this->product_key );
563 816 }
564 817
565 818 /**
566 819 * Check for Updates at the defined API endpoint and modify the update array.
@@ -576,10 +829,15 @@
576 829 return $_transient_data;
577 830 }
578 831 $api_response = $this->api_request();
579 832 if ( false !== $api_response && is_object( $api_response ) && isset( $api_response->new_version ) ) {
833 + if ( ! isset( $api_response->plugin ) ) {
834 + $api_response->plugin = $this->product->get_slug() . '/' . $this->product->get_file();
835 + }
580 836 if ( version_compare( $this->product->get_version(), $api_response->new_version, '<' ) ) {
581 837 $_transient_data->response[ $this->product->get_slug() . '/' . $this->product->get_file() ] = $api_response;
838 + } else {
839 + $_transient_data->no_update[ $this->product->get_slug() . '/' . $this->product->get_file() ] = $api_response;
582 840 }
583 841 }
584 842
585 843 return $_transient_data;
@@ -614,9 +872,9 @@
614 872 *
615 873 * @return object $_data
616 874 */
617 875 public function plugins_api_filter( $_data, $_action = '', $_args = null ) {
618 - if ( ( 'plugin_information' != $_action ) || ! isset( $_args->slug ) || ( $_args->slug != $this->product->get_slug() ) ) {
876 + if ( ( 'plugin_information' !== $_action ) || ! isset( $_args->slug ) || ( $_args->slug !== $this->product->get_slug() ) ) {
619 877 return $_data;
620 878 }
621 879 $api_response = $this->api_request();
622 880 if ( false !== $api_response ) {
@@ -633,9 +891,9 @@
633 891 * @param string $url Url to check.
634 892 *
635 893 * @return array $array
636 894 */
637 - function http_request_args( $args, $url ) {
895 + public function http_request_args( $args, $url ) {
638 896 // If it is an https request and we are performing a package download, disable ssl verification.
639 897 if ( strpos( $url, 'https://' ) !== false && strpos( $url, 'edd_action=package_download' ) ) {
640 898 $args['sslverify'] = false;
641 899 }
@@ -676,9 +934,31 @@
676 934 if ( $this->product->requires_license() ) {
677 935 $this->failed_checks = intval( get_option( $this->product->get_key() . '_failed_checks', 0 ) );
678 936 $this->register_license_hooks();
679 937 }
938 + if ( ! self::$globals_loaded ) {
939 + add_filter( 'themeisle_sdk_license/status', [ __CLASS__, 'status' ], 999, 1 );
940 + add_filter( 'themeisle_sdk_license/is-valid', [ __CLASS__, 'is_valid' ], 999, 1 );
941 + add_filter( 'themeisle_sdk_license/plan', [ __CLASS__, 'plan' ], 999, 1 );
942 + add_filter( 'themeisle_sdk_license/key', [ __CLASS__, 'key' ], 999, 1 );
943 + $globals_loaded = true;
944 + }
945 + $namespace = apply_filters( 'themesle_sdk_namespace_' . md5( $product->get_basefile() ), false );
680 946
947 + if ( false !== $namespace ) {
948 + $this->namespace = $namespace;
949 + add_filter( 'themeisle_sdk_license_process_' . $namespace, [ $this, 'do_license_process' ], 10, 2 );
950 + add_filter( 'product_' . $namespace . '_license_status', [ $this, 'get_license_status' ], PHP_INT_MAX );
951 + add_filter( 'product_' . $namespace . '_license_key', [ $this->product, 'get_license' ] );
952 + add_filter( 'product_' . $namespace . '_license_plan', [ $this, 'get_plan' ], PHP_INT_MAX );
953 + if ( defined( 'WP_CLI' ) && WP_CLI ) {
954 + \WP_CLI::add_command( $namespace . ' activate', [ $this, 'cli_activate' ] );
955 + \WP_CLI::add_command( $namespace . ' deactivate', [ $this, 'cli_deactivate' ] );
956 + \WP_CLI::add_command( $namespace . ' is-active', [ $this, 'cli_is_active' ] );
957 + }
958 + }
959 +
960 + add_action( 'admin_head', [ $this, 'auto_activate' ] );
681 961 if ( $this->product->is_plugin() ) {
682 962 add_filter(
683 963 'pre_set_site_transient_update_plugins',
684 964 [
@@ -686,19 +966,33 @@
686 966 'pre_set_site_transient_update_plugins_filter',
687 967 ]
688 968 );
689 969 add_filter( 'plugins_api', array( $this, 'plugins_api_filter' ), 10, 3 );
690 - add_filter( 'http_request_args', array( $this, 'http_request_args' ), 10, 2 );
970 + add_filter( 'http_request_args', array( $this, 'http_request_args' ), 10, 2 ); //phpcs:ignore WordPressVIPMinimum.Hooks.RestrictedHooks.http_request_args
971 + if ( ! self::is_valid( $product->get_basefile() ) ) {
972 + add_filter(
973 + 'plugin_action_links_' . plugin_basename( $product->get_basefile() ),
974 + function ( $actions ) {
975 + if ( $this->get_license_status( true ) !== self::STATUS_ACTIVE_EXPIRED ) {
976 + return $actions;
977 + }
978 + $new_actions['deactivate'] = $actions['deactivate'];
979 + $new_actions['renew_link'] = '<a style="color:#d63638" href="' . esc_url( $this->renew_url() ) . '" target="_blank" rel="external noopener noreferrer">Renew license to update</a>';
691 980
981 + return $new_actions;
982 + }
983 + );
984 + }
985 +
692 986 return $this;
693 987 }
694 988 if ( $this->product->is_theme() ) {
695 989 add_filter( 'site_transient_update_themes', array( &$this, 'theme_update_transient' ) );
696 - add_filter( 'delete_site_transient_update_themes', array( &$this, 'delete_theme_update_transient' ) );
990 + add_action( 'delete_site_transient_update_themes', array( &$this, 'delete_theme_update_transient' ) );
697 991 add_action( 'load-update-core.php', array( &$this, 'delete_theme_update_transient' ) );
698 992 add_action( 'load-themes.php', array( &$this, 'delete_theme_update_transient' ) );
699 993 add_action( 'load-themes.php', array( &$this, 'load_themes_screen' ) );
700 - add_filter( 'http_request_args', array( $this, 'disable_wporg_update' ), 5, 2 );
994 + add_filter( 'http_request_args', array( $this, 'disable_wporg_update' ), 5, 2 ); //phpcs:ignore WordPressVIPMinimum.Hooks.RestrictedHooks.http_request_args
701 995
702 996 return $this;
703 997
704 998 }
@@ -710,10 +1004,152 @@
710 1004 * Register license fields for the products.
711 1005 */
712 1006 public function register_license_hooks() {
713 1007 add_action( 'admin_init', array( $this, 'register_settings' ) );
714 - add_action( 'admin_init', array( $this, 'activate_license' ) );
1008 + add_action( 'admin_init', array( $this, 'process_license' ) );
715 1009 add_action( 'admin_init', array( $this, 'product_valid' ), 99999999 );
716 1010 add_action( 'admin_notices', array( $this, 'show_notice' ) );
717 1011 add_filter( $this->product->get_key() . '_license_status', array( $this, 'get_license_status' ) );
1012 + }
1013 +
1014 + /**
1015 + * Check license on filesystem.
1016 + *
1017 + * @return mixed License key.
1018 + */
1019 + public function get_file_license() {
1020 +
1021 + $license_file = dirname( $this->product->get_basefile() ) . '/license.json';
1022 +
1023 + global $wp_filesystem;
1024 + if ( ! is_file( $license_file ) ) {
1025 + return false;
1026 + }
1027 +
1028 + require_once ABSPATH . '/wp-admin/includes/file.php';
1029 + \WP_Filesystem();
1030 + $content = json_decode( $wp_filesystem->get_contents( $license_file ) );
1031 + if ( ! is_object( $content ) ) {
1032 + return false;
1033 + }
1034 + if ( ! isset( $content->key ) ) {
1035 + return false;
1036 + }
1037 + return $content->key;
1038 + }
1039 + /**
1040 + * Run license activation on plugin activate.
1041 + */
1042 + public function auto_activate() {
1043 + $status = $this->get_license_status();
1044 + if ( 'not_active' !== $status ) {
1045 + return false;
1046 + }
1047 +
1048 + if ( ! empty( $this->namespace ) ) {
1049 + $license_key = apply_filters( 'product_' . $this->namespace . '_license_key_constant', '' );
1050 + }
1051 +
1052 + if ( empty( $license_key ) ) {
1053 + $license_key = $this->get_file_license();
1054 + }
1055 + if ( empty( $license_key ) ) {
1056 + return;
1057 + }
1058 +
1059 +
1060 + $this->license_local = $license_key;
1061 + $lock_key = $this->product->get_key() . '_autoactivated';
1062 +
1063 + if ( 'yes' === get_option( $lock_key, '' ) ) {
1064 + return;
1065 + }
1066 + if ( 'yes' === get_transient( $lock_key ) ) {
1067 + return;
1068 + }
1069 + $response = $this->do_license_process( $license_key, 'activate' );
1070 +
1071 + set_transient( $lock_key, 'yes', 6 * HOUR_IN_SECONDS );
1072 +
1073 + if ( apply_filters( $this->product->get_key() . '_hide_license_notices', false ) ) {
1074 + return;
1075 + }
1076 +
1077 + if ( true === $response ) {
1078 + add_action( 'admin_notices', [ $this, 'autoactivate_notice' ] );
1079 + }
1080 + }
1081 +
1082 + /**
1083 + * Show auto-activate notice.
1084 + */
1085 + public function autoactivate_notice() {
1086 + ?>
1087 + <div class="notice notice-success is-dismissible">
1088 + <p><?php echo sprintf( '<strong>%s</strong> has been successfully activated using <strong>%s</strong> license !', esc_attr( $this->product->get_name() ), esc_attr( str_repeat( '*', 20 ) . substr( $this->license_local, - 10 ) ) ); ?></p>
1089 + </div>
1090 + <?php
1091 + }
1092 +
1093 + /**
1094 + * Activate product license on this site.
1095 + *
1096 + * ## OPTIONS
1097 + *
1098 + * @param array $args Command args.
1099 + *
1100 + * [<license-key>]
1101 + * : Product license key.
1102 + */
1103 + public function cli_activate( $args ) {
1104 + $license_key = isset( $args[0] ) ? trim( $args[0] ) : '';
1105 + $response = $this->do_license_process( $license_key, 'activate' );
1106 + if ( true !== $response ) {
1107 + \WP_CLI::error( $response->get_error_message() );
1108 +
1109 + return;
1110 + }
1111 +
1112 + \WP_CLI::success( 'Product successfully activated.' );
1113 + }
1114 +
1115 + /**
1116 + * Deactivate product license on this site.
1117 + *
1118 + * @param array $args Command args.
1119 + *
1120 + * ## OPTIONS
1121 + *
1122 + * [<license-key>]
1123 + * : Product license key.
1124 + */
1125 + public function cli_deactivate( $args ) {
1126 + $license_key = isset( $args[0] ) ? trim( $args[0] ) : '';
1127 + $response = $this->do_license_process( $license_key, 'deactivate' );
1128 + if ( true !== $response ) {
1129 + \WP_CLI::error( $response->get_error_message() );
1130 +
1131 + return;
1132 + }
1133 +
1134 + \WP_CLI::success( 'Product successfully deactivated.' );
1135 + }
1136 +
1137 + /**
1138 + * Checks if product has license activated.
1139 + *
1140 + * @param array $args Command args.
1141 + *
1142 + * @subcommand is-active
1143 + */
1144 + public function cli_is_active( $args ) {
1145 +
1146 + $status = $this->get_license_status();
1147 + if ( 'valid' === $status ) {
1148 + \WP_CLI::halt( 0 );
1149 +
1150 + return;
1151 + }
1152 +
1153 + \WP_CLI::halt( 1 );
718 1154 }
719 1155 }