← All changes
|
vendor/codeinwp/themeisle-sdk/src/Modules/Licenser.php
+473
-152
3.1.3
→
3.7.1
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; |
| @@ -28,9 +29,9 @@ | ||
| 28 | 29 | * Number of max failed checks before showing the license message. |
| 29 | 30 | * |
| 30 | 31 | * @var int $max_failed Maximum failed checks allowed before show the notice |
| 31 | 32 | */ |
| 32 | - private static $max_failed = 5; | |
| 33 | + private static $max_failed = 2; | |
| 33 | 34 | /** |
| 34 | 35 | * License key string. |
| 35 | 36 | * |
| 36 | 37 | * @var string $license_key The license key string |
| @@ -55,8 +56,21 @@ | ||
| 55 | 56 | */ |
| 56 | 57 | private $product_key; |
| 57 | 58 | |
| 58 | 59 | /** |
| 60 | + * Holds local license object. | |
| 61 | + * | |
| 62 | + * @var null Local license object. | |
| 63 | + */ | |
| 64 | + private $license_local = null; | |
| 65 | + /** | |
| 66 | + * Product namespace, used for fixed name filters/cli commands. | |
| 67 | + * | |
| 68 | + * @var string $namespace Product namespace. | |
| 69 | + */ | |
| 70 | + private $namespace = null; | |
| 71 | + | |
| 72 | + /** | |
| 59 | 73 | * Disable wporg updates for premium products. |
| 60 | 74 | * |
| 61 | 75 | * @param string $r Update payload. |
| 62 | 76 | * @param string $url The api url. |
| @@ -62,9 +76,9 @@ | ||
| 62 | 76 | * @param string $url The api url. |
| 63 | 77 | * |
| 64 | 78 | * @return mixed List of themes to check for update. |
| 65 | 79 | */ |
| 66 | - function disable_wporg_update( $r, $url ) { | |
| 80 | + public function disable_wporg_update( $r, $url ) { | |
| 67 | 81 | |
| 68 | 82 | if ( 0 !== strpos( $url, 'https://api.wordpress.org/themes/update-check/' ) ) { |
| 69 | 83 | return $r; |
| 70 | 84 | } |
| @@ -74,9 +88,9 @@ | ||
| 74 | 88 | |
| 75 | 89 | unset( $themes->themes->{$this->product->get_slug()} ); |
| 76 | 90 | |
| 77 | 91 | // Encode the updated JSON response. |
| 78 | - $r['body']['themes'] = json_encode( $themes ); | |
| 92 | + $r['body']['themes'] = wp_json_encode( $themes ); | |
| 79 | 93 | |
| 80 | 94 | return $r; |
| 81 | 95 | } |
| 82 | 96 | |
| @@ -88,8 +102,11 @@ | ||
| 88 | 102 | public function register_settings() { |
| 89 | 103 | if ( ! is_admin() ) { |
| 90 | 104 | return false; |
| 91 | 105 | } |
| 106 | + if ( apply_filters( $this->product->get_key() . '_hide_license_field', false ) ) { | |
| 107 | + return; | |
| 108 | + } | |
| 92 | 109 | add_settings_field( |
| 93 | 110 | $this->product->get_key() . '_license', |
| 94 | 111 | $this->product->get_name() . ' license', |
| 95 | 112 | array( $this, 'license_view' ), |
| @@ -108,19 +125,83 @@ | ||
| 108 | 125 | $deactivate_string = apply_filters( $this->product->get_key() . '_lc_deactivate_string', 'Deactivate' ); |
| 109 | 126 | $valid_string = apply_filters( $this->product->get_key() . '_lc_valid_string', 'Valid' ); |
| 110 | 127 | $invalid_string = apply_filters( $this->product->get_key() . '_lc_invalid_string', 'Invalid' ); |
| 111 | 128 | $license_message = apply_filters( $this->product->get_key() . '_lc_license_message', 'Enter your license from %s purchase history in order to get %s updates' ); |
| 129 | + $error_message = $this->get_error(); | |
| 130 | + ?> | |
| 131 | + <style type="text/css"> | |
| 132 | + input.themeisle-sdk-text-input-valid { | |
| 133 | + border: 1px solid #7ad03a; | |
| 134 | + } | |
| 112 | 135 | |
| 113 | - echo '<p ><input ' . ( ( 'valid' === $status ) ? ( 'style="border:1px solid #7ad03a; "' ) : '' ) . ' type="text" id="' . $this->product->get_key() . '_license" name="' . $this->product->get_key() . '_license" value="' . $value . '" /><a ' . ( ( 'valid' === $status ) ? ( 'style="color:#fff;background: #7ad03a; display: inline-block;text-decoration: none;font-size: 13px;line-height: 26px;height: 26px; margin-left:5px; padding: 0 10px 1px; -webkit-border-radius: 3px;border-radius: 3px; ">' . $valid_string ) : ( 'style="color:#fff;background: #dd3d36; display: inline-block;text-decoration: none;font-size: 13px;line-height: 26px;height: 26px; margin-left:5px; padding: 0 10px 1px; -webkit-border-radius: 3px;border-radius: 3px; ">' . $invalid_string ) ) . ' </a> <button name="' . $this->product->get_key() . '_btn_trigger" ' . ( ( 'valid' === $status ) ? ( ' class="button button-primary">' . $deactivate_string ) : ( ' class="button button-primary" value="yes" type="submit" >' . $activate_string ) ) . ' </button></p><p class="description">' . sprintf( $license_message, '<a href="' . $this->get_api_url() . '">' . $this->get_distributor_name() . '</a> ', $this->product->get_type() ) . '</p>'; | |
| 136 | + input.themeisle-sdk-license-input { | |
| 137 | + width: 300px; | |
| 138 | + padding: 0 8px; | |
| 139 | + line-height: 2; | |
| 140 | + min-height: 30px; | |
| 141 | + } | |
| 114 | 142 | |
| 143 | + .themeisle-sdk-license-deactivate-cta { | |
| 144 | + color: #fff; | |
| 145 | + background: #7ad03a; | |
| 146 | + display: inline-block; | |
| 147 | + text-decoration: none; | |
| 148 | + font-size: 13px; | |
| 149 | + line-height: 30px; | |
| 150 | + height: 26px; | |
| 151 | + margin-left: 5px; | |
| 152 | + padding: 0 10px 3px; | |
| 153 | + -webkit-border-radius: 3px; | |
| 154 | + border-radius: 3px; | |
| 155 | + } | |
| 156 | + | |
| 157 | + .themeisle-sdk-license-activate-cta { | |
| 158 | + color: #fff; | |
| 159 | + background: #dd3d36; | |
| 160 | + display: inline-block; | |
| 161 | + text-decoration: none; | |
| 162 | + font-size: 13px; | |
| 163 | + line-height: 30px; | |
| 164 | + height: 26px; | |
| 165 | + margin-left: 5px; | |
| 166 | + padding: 0 10px 3px; | |
| 167 | + -webkit-border-radius: 3px; | |
| 168 | + border-radius: 3px; | |
| 169 | + } | |
| 170 | + | |
| 171 | + button.button.themeisle-sdk-licenser-button-cta { | |
| 172 | + line-height: 26px; | |
| 173 | + height: 29px; | |
| 174 | + vertical-align: top; | |
| 175 | + } | |
| 176 | + | |
| 177 | + </style> | |
| 178 | + <?php | |
| 179 | + echo sprintf( | |
| 180 | + '<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', | |
| 181 | + ( ( 'valid' === $status ) ? sprintf( '<input type="hidden" value="%s" name="%s_license" />', esc_attr( $value ), esc_attr( $this->product->get_key() ) ) : '' ), | |
| 182 | + ( ( 'valid' === $status ) ? 'themeisle-sdk-text-input-valid' : '' ), | |
| 183 | + esc_attr( $this->product->get_key() ), | |
| 184 | + esc_attr( ( ( 'valid' === $status ) ? $this->product->get_key() . '_mask' : $this->product->get_key() ) ), | |
| 185 | + esc_attr( ( ( 'valid' === $status ) ? ( str_repeat( '*', 30 ) . substr( $value, - 5 ) ) : $value ) ), | |
| 186 | + esc_attr( ( 'valid' === $status ? 'themeisle-sdk-license-deactivate-cta' : 'themeisle-sdk-license-activate-cta' ) ), | |
| 187 | + esc_attr( 'valid' === $status ? $valid_string : $invalid_string ), | |
| 188 | + esc_attr( $this->product->get_key() ), | |
| 189 | + esc_attr( 'valid' === $status ? $deactivate_string : $activate_string ), | |
| 190 | + 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() ) ), | |
| 191 | + wp_kses_data( empty( $error_message ) ? '' : sprintf( '<p style="color:#dd3d36">%s</p>', ( $error_message ) ) ) | |
| 192 | + ) . wp_nonce_field( $this->product->get_key() . 'nonce', $this->product->get_key() . 'nonce_field', false, false );//phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 193 | + | |
| 115 | 194 | } |
| 116 | 195 | |
| 117 | 196 | /** |
| 118 | 197 | * Return the license status. |
| 119 | 198 | * |
| 199 | + * @param bool $check_expiration Should check if license is valid, but expired. | |
| 200 | + * | |
| 120 | 201 | * @return string The License status. |
| 121 | 202 | */ |
| 122 | - public function get_license_status() { | |
| 203 | + public function get_license_status( $check_expiration = false ) { | |
| 123 | 204 | |
| 124 | 205 | $license_data = get_option( $this->product->get_key() . '_license_data', '' ); |
| 125 | 206 | |
| 126 | 207 | if ( '' === $license_data ) { |
| @@ -125,11 +206,23 @@ | ||
| 125 | 206 | |
| 126 | 207 | if ( '' === $license_data ) { |
| 127 | 208 | return get_option( $this->product->get_key() . '_license_status', 'not_active' ); |
| 128 | 209 | } |
| 210 | + $status = isset( $license_data->license ) ? $license_data->license : get_option( $this->product->get_key() . '_license_status', 'not_active' ); | |
| 211 | + if ( false === $check_expiration ) { | |
| 212 | + return $status; | |
| 213 | + } | |
| 129 | 214 | |
| 130 | - return isset( $license_data->license ) ? $license_data->license : get_option( $this->product->get_key() . '_license_status', 'not_active' ); | |
| 215 | + return ( 'valid' === $status && isset( $license_data->is_expired ) && 'yes' === $license_data->is_expired ) ? 'active_expired' : $status; | |
| 216 | + } | |
| 131 | 217 | |
| 218 | + /** | |
| 219 | + * Return the last error message. | |
| 220 | + * | |
| 221 | + * @return mixed Error message. | |
| 222 | + */ | |
| 223 | + public function get_error() { | |
| 224 | + return get_transient( $this->product->get_key() . 'act_err' ); | |
| 132 | 225 | } |
| 133 | 226 | |
| 134 | 227 | /** |
| 135 | 228 | * Get remote api url. |
| @@ -150,9 +243,9 @@ | ||
| 150 | 243 | * @return string Remote api url. |
| 151 | 244 | */ |
| 152 | 245 | public function get_distributor_name() { |
| 153 | 246 | if ( $this->is_from_partner( $this->product ) ) { |
| 154 | - return 'ThemeIsle'; | |
| 247 | + return 'Themeisle'; | |
| 155 | 248 | } |
| 156 | 249 | |
| 157 | 250 | return $this->product->get_store_name(); |
| 158 | 251 | } |
| @@ -157,21 +250,39 @@ | ||
| 157 | 250 | return $this->product->get_store_name(); |
| 158 | 251 | } |
| 159 | 252 | |
| 160 | 253 | /** |
| 254 | + * License price id. | |
| 255 | + * | |
| 256 | + * @return int License plan. | |
| 257 | + */ | |
| 258 | + public function get_plan() { | |
| 259 | + $license_data = get_option( $this->product->get_key() . '_license_data', '' ); | |
| 260 | + if ( ! isset( $license_data->price_id ) ) { | |
| 261 | + return - 1; | |
| 262 | + } | |
| 263 | + | |
| 264 | + return (int) $license_data->price_id; | |
| 265 | + } | |
| 266 | + | |
| 267 | + /** | |
| 161 | 268 | * Show the admin notice regarding the license status. |
| 162 | 269 | * |
| 163 | 270 | * @return bool Should we show the notice ? |
| 164 | 271 | */ |
| 165 | - function show_notice() { | |
| 272 | + public function show_notice() { | |
| 166 | 273 | if ( ! is_admin() ) { |
| 167 | 274 | return false; |
| 168 | 275 | } |
| 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 | 276 | |
| 277 | + if ( apply_filters( $this->product->get_key() . '_hide_license_notices', false ) ) { | |
| 278 | + return false; | |
| 279 | + } | |
| 280 | + | |
| 281 | + $status = $this->get_license_status( true ); | |
| 282 | + $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.' ); | |
| 283 | + $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>. ' ); | |
| 284 | + $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 | 285 | // No activations left for this license. |
| 175 | 286 | if ( 'valid' != $status && $this->check_activation() ) { |
| 176 | 287 | ?> |
| 177 | 288 | <div class="error"> |
| @@ -177,12 +288,12 @@ | ||
| 177 | 288 | <div class="error"> |
| 178 | 289 | <p><strong> |
| 179 | 290 | <?php |
| 180 | 291 | 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>' | |
| 292 | + wp_kses_data( $no_activations_string ), | |
| 293 | + esc_attr( $this->product->get_name() ), | |
| 294 | + esc_attr( $this->product->get_name() ), | |
| 295 | + '<a href="' . esc_url( $this->get_api_url() ) . '" target="_blank">' . esc_attr( $this->get_distributor_name() ) . '</a>' | |
| 185 | 296 | ); |
| 186 | 297 | ?> |
| 187 | 298 | </strong> |
| 188 | 299 | </p> |
| @@ -189,14 +300,15 @@ | ||
| 189 | 300 | </div> |
| 190 | 301 | <?php |
| 191 | 302 | return false; |
| 192 | 303 | } |
| 304 | + | |
| 193 | 305 | // Invalid license key. |
| 194 | - if ( 'valid' != $status ) { | |
| 306 | + if ( 'active_expired' === $status ) { | |
| 195 | 307 | ?> |
| 196 | 308 | <div class="error"> |
| 197 | 309 | <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> | |
| 310 | + <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 | 311 | </p> |
| 200 | 312 | </div> |
| 201 | 313 | <?php |
| 202 | 314 | |
| @@ -201,26 +313,18 @@ | ||
| 201 | 313 | <?php |
| 202 | 314 | |
| 203 | 315 | return false; |
| 204 | 316 | } |
| 205 | - | |
| 206 | - // Expired and soon to expire license. | |
| 207 | - if ( 'valid' == $status && $this->check_expiration() ) { | |
| 317 | + // Invalid license key. | |
| 318 | + if ( 'valid' != $status ) { | |
| 208 | 319 | ?> |
| 209 | - <div class="update-nag"> | |
| 320 | + <div class="error"> | |
| 210 | 321 | <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> | |
| 322 | + <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 | 323 | </p> |
| 221 | 324 | </div> |
| 222 | 325 | <?php |
| 326 | + | |
| 223 | 327 | return false; |
| 224 | 328 | } |
| 225 | 329 | |
| 226 | 330 | return true; |
| @@ -236,9 +340,9 @@ | ||
| 236 | 340 | if ( '' === $license_data ) { |
| 237 | 341 | return false; |
| 238 | 342 | } |
| 239 | 343 | |
| 240 | - return isset( $license_data->error ) ? ( 'no_activations_left' == $license_data->error ) : false; | |
| 344 | + return isset( $license_data->license ) ? ( 'no_activations_left' == $license_data->license ) : false; | |
| 241 | 345 | |
| 242 | 346 | } |
| 243 | 347 | |
| 244 | 348 | /** |
| @@ -245,9 +349,9 @@ | ||
| 245 | 349 | * Check if the license is about to expire in the next month. |
| 246 | 350 | * |
| 247 | 351 | * @return bool |
| 248 | 352 | */ |
| 249 | - function check_expiration() { | |
| 353 | + public function check_expiration() { | |
| 250 | 354 | $license_data = get_option( $this->product->get_key() . '_license_data', '' ); |
| 251 | 355 | if ( '' === $license_data ) { |
| 252 | 356 | return false; |
| 253 | 357 | } |
| @@ -265,9 +369,9 @@ | ||
| 265 | 369 | * Return the renew url from the store used. |
| 266 | 370 | * |
| 267 | 371 | * @return string The renew url. |
| 268 | 372 | */ |
| 269 | - function renew_url() { | |
| 373 | + public function renew_url() { | |
| 270 | 374 | $license_data = get_option( $this->product->get_key() . '_license_data', '' ); |
| 271 | 375 | if ( '' === $license_data ) { |
| 272 | 376 | return $this->get_api_url(); |
| 273 | 377 | } |
| @@ -281,9 +385,9 @@ | ||
| 281 | 385 | /** |
| 282 | 386 | * Run the license check call. |
| 283 | 387 | */ |
| 284 | 388 | public function product_valid() { |
| 285 | - if ( false !== ( $license = get_transient( $this->product->get_key() . '_license_data' ) ) ) { | |
| 389 | + if ( false !== ( $license = get_transient( $this->product->get_key() . '_license_data' ) ) ) { //phpcs:ignore Squiz.PHP.DisallowMultipleAssignments.FoundInControlStructure | |
| 286 | 390 | return; |
| 287 | 391 | } |
| 288 | 392 | $license = $this->check_license(); |
| 289 | 393 | set_transient( $this->product->get_key() . '_license_data', $license, 12 * HOUR_IN_SECONDS ); |
| @@ -296,84 +400,145 @@ | ||
| 296 | 400 | * @return object The license data. |
| 297 | 401 | */ |
| 298 | 402 | public function check_license() { |
| 299 | 403 | $status = $this->get_license_status(); |
| 300 | - if ( 'not_active' == $status ) { | |
| 404 | + if ( 'not_active' === $status ) { | |
| 301 | 405 | $license_data = new \stdClass(); |
| 302 | 406 | $license_data->license = 'not_active'; |
| 303 | 407 | |
| 304 | 408 | return $license_data; |
| 305 | 409 | } |
| 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 | - ); | |
| 410 | + $license = trim( $this->license_key ); | |
| 411 | + | |
| 412 | + $response = $this->do_license_process( $license, 'check' ); | |
| 413 | + | |
| 321 | 414 | if ( is_wp_error( $response ) ) { |
| 322 | 415 | $license_data = new \stdClass(); |
| 323 | - $license_data->license = 'valid'; | |
| 324 | - | |
| 416 | + $license_data->license = 'invalid'; | |
| 325 | 417 | } 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 | - } | |
| 418 | + $license_data = $response; | |
| 331 | 419 | } |
| 420 | + | |
| 332 | 421 | $license_old = get_option( $this->product->get_key() . '_license_data', '' ); |
| 333 | - if ( 'valid' == $license_old->license && ( $license_data->license != $license_old->license ) ) { | |
| 422 | + if ( 'valid' === $license_old->license && ( $license_data->license !== $license_old->license ) && $this->failed_checks <= self::$max_failed ) { | |
| 334 | 423 | $this->increment_failed_checks(); |
| 424 | + | |
| 425 | + return $license_old; | |
| 426 | + } | |
| 427 | + | |
| 428 | + if ( ! isset( $license_data->key ) ) { | |
| 429 | + $license_data->key = isset( $license_old->key ) ? $license_old->key : ''; | |
| 430 | + } | |
| 431 | + $this->reset_failed_checks(); | |
| 432 | + | |
| 433 | + return $license_data; | |
| 434 | + | |
| 435 | + } | |
| 436 | + | |
| 437 | + /** | |
| 438 | + * Do license activation/deactivation. | |
| 439 | + * | |
| 440 | + * @param string $license License key. | |
| 441 | + * @param string $action What do to. | |
| 442 | + * | |
| 443 | + * @return bool|\WP_Error | |
| 444 | + */ | |
| 445 | + public function do_license_process( $license, $action = 'toggle' ) { | |
| 446 | + if ( strlen( $license ) < 10 ) { | |
| 447 | + return new \WP_Error( 'themeisle-license-invalid-format', 'Invalid license.' ); | |
| 448 | + } | |
| 449 | + $status = $this->get_license_status(); | |
| 450 | + | |
| 451 | + if ( 'valid' === $status && 'activate' === $action ) { | |
| 452 | + return new \WP_Error( 'themeisle-license-already-active', 'License is already active.' ); | |
| 453 | + } | |
| 454 | + if ( 'valid' !== $status && 'deactivate' === $action ) { | |
| 455 | + return new \WP_Error( 'themeisle-license-already-deactivate', 'License not active.' ); | |
| 456 | + } | |
| 457 | + | |
| 458 | + if ( 'toggle' === $action ) { | |
| 459 | + $action = ( 'valid' !== $status ? ( 'activate' ) : ( 'deactivate' ) ); | |
| 460 | + } | |
| 461 | + | |
| 462 | + // Call the custom API. | |
| 463 | + if ( 'check' === $action ) { | |
| 464 | + $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 | 465 | } else { |
| 336 | - $this->reset_failed_checks(); | |
| 466 | + $response = wp_remote_post( | |
| 467 | + sprintf( '%slicense/%s/%s/%s', Product::API_URL, $action, rawurlencode( $this->product->get_name() ), $license ), | |
| 468 | + array( | |
| 469 | + 'body' => wp_json_encode( | |
| 470 | + array( | |
| 471 | + 'url' => rawurlencode( home_url() ), | |
| 472 | + ) | |
| 473 | + ), | |
| 474 | + 'headers' => array( | |
| 475 | + 'Content-Type' => 'application/json', | |
| 476 | + ), | |
| 477 | + ) | |
| 478 | + ); | |
| 337 | 479 | } |
| 338 | 480 | |
| 339 | - if ( $this->failed_checks <= self::$max_failed ) { | |
| 340 | - return $license_old; | |
| 481 | + // make sure the response came back okay. | |
| 482 | + if ( is_wp_error( $response ) ) { | |
| 483 | + 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 | 484 | } |
| 342 | 485 | |
| 343 | - if ( isset( $license_old->hide_valid ) ) { | |
| 344 | - $license_data->hide_valid = true; | |
| 486 | + $license_data = json_decode( wp_remote_retrieve_body( $response ) ); | |
| 487 | + | |
| 488 | + if ( ! is_object( $license_data ) ) { | |
| 489 | + return new \WP_Error( 'themeisle-license-404', 'ERROR: Failed to validate license. Please try again in one minute.' ); | |
| 345 | 490 | } |
| 491 | + if ( 'check' === $action ) { | |
| 492 | + return $license_data; | |
| 493 | + } | |
| 346 | 494 | |
| 495 | + Loader::clear_cache_token(); | |
| 496 | + | |
| 497 | + if ( ! isset( $license_data->license ) ) { | |
| 498 | + $license_data->license = 'invalid'; | |
| 499 | + } | |
| 500 | + | |
| 347 | 501 | if ( ! isset( $license_data->key ) ) { |
| 348 | - $license_data->key = isset( $license_old->key ) ? $license_old->key : ''; | |
| 502 | + $license_data->key = $license; | |
| 349 | 503 | } |
| 504 | + if ( 'valid' === $license_data->license ) { | |
| 505 | + $this->reset_failed_checks(); | |
| 506 | + } | |
| 350 | 507 | |
| 351 | - if ( isset( $license_old->hide_expiration ) ) { | |
| 352 | - $license_data->hide_expiration = true; | |
| 508 | + if ( 'deactivate' === $action ) { | |
| 509 | + | |
| 510 | + delete_option( $this->product->get_key() . '_license_data' ); | |
| 511 | + delete_option( $this->product->get_key() . '_license_plan' ); | |
| 512 | + delete_transient( $this->product->get_key() . '_license_data' ); | |
| 513 | + | |
| 514 | + return true; | |
| 353 | 515 | } |
| 354 | - | |
| 355 | - if ( isset( $license_old->hide_activation ) ) { | |
| 356 | - $license_data->hide_activation = true; | |
| 516 | + if ( isset( $license_data->plan ) ) { | |
| 517 | + update_option( $this->product->get_key() . '_license_plan', $license_data->plan ); | |
| 357 | 518 | } |
| 519 | + update_option( $this->product->get_key() . '_license_data', $license_data ); | |
| 520 | + set_transient( $this->product->get_key() . '_license_data', $license_data, 12 * HOUR_IN_SECONDS ); | |
| 521 | + if ( 'activate' === $action && 'valid' !== $license_data->license ) { | |
| 522 | + return new \WP_Error( 'themeisle-license-invalid', 'ERROR: Invalid license provided.' ); | |
| 523 | + } | |
| 358 | 524 | |
| 359 | - return $license_data; | |
| 360 | - | |
| 525 | + return true; | |
| 361 | 526 | } |
| 362 | 527 | |
| 363 | 528 | /** |
| 364 | - * Increment the failed checks. | |
| 529 | + * Reset the failed checks | |
| 365 | 530 | */ |
| 366 | - private function increment_failed_checks() { | |
| 367 | - $this->failed_checks ++; | |
| 531 | + private function reset_failed_checks() { | |
| 532 | + $this->failed_checks = 1; | |
| 368 | 533 | update_option( $this->product->get_key() . '_failed_checks', $this->failed_checks ); |
| 369 | 534 | } |
| 370 | 535 | |
| 371 | 536 | /** |
| 372 | - * Reset the failed checks | |
| 537 | + * Increment the failed checks. | |
| 373 | 538 | */ |
| 374 | - private function reset_failed_checks() { | |
| 375 | - $this->failed_checks = 1; | |
| 539 | + private function increment_failed_checks() { | |
| 540 | + $this->failed_checks ++; | |
| 376 | 541 | update_option( $this->product->get_key() . '_failed_checks', $this->failed_checks ); |
| 377 | 542 | } |
| 378 | 543 | |
| 379 | 544 | /** |
| @@ -378,64 +543,50 @@ | ||
| 378 | 543 | |
| 379 | 544 | /** |
| 380 | 545 | * Activate the license remotely. |
| 381 | 546 | */ |
| 382 | - function activate_license() { | |
| 547 | + public function process_license() { | |
| 383 | 548 | // listen for our activate button to be clicked. |
| 384 | 549 | if ( ! isset( $_POST[ $this->product->get_key() . '_btn_trigger' ] ) ) { |
| 385 | 550 | return; |
| 386 | 551 | } |
| 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'; | |
| 552 | + if ( ! isset( $_POST[ $this->product->get_key() . 'nonce_field' ] ) | |
| 553 | + || ! wp_verify_nonce( $_POST[ $this->product->get_key() . 'nonce_field' ], $this->product->get_key() . 'nonce' ) //phpcs:ignore WordPress.Security.ValidatedSanitizedInput.InputNotSanitized | |
| 554 | + ) { | |
| 555 | + return; | |
| 400 | 556 | } |
| 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. | |
| 557 | + if ( ! current_user_can( 'manage_options' ) ) { | |
| 558 | + return; | |
| 559 | + } | |
| 560 | + $license = isset( $_POST[ $this->product->get_key() . '_license' ] ) | |
| 561 | + ? sanitize_text_field( $_POST[ $this->product->get_key() . '_license' ] ) | |
| 562 | + : ''; | |
| 563 | + | |
| 564 | + $response = $this->do_license_process( $license, 'toggle' ); | |
| 404 | 565 | if ( is_wp_error( $response ) ) { |
| 405 | - $license_data = new \stdClass(); | |
| 406 | - $license_data->license = ( 'valid' != $status ) ? 'valid' : 'invalid'; | |
| 566 | + $this->set_error( $response->get_error_message() ); | |
| 407 | 567 | |
| 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 | - } | |
| 568 | + return; | |
| 417 | 569 | } |
| 418 | - if ( ! isset( $license_data->key ) ) { | |
| 419 | - $license_data->key = $license; | |
| 570 | + if ( true === $response ) { | |
| 571 | + $this->set_error( '' ); | |
| 420 | 572 | } |
| 421 | - if ( 'valid' == $license_data->license ) { | |
| 422 | - $this->reset_failed_checks(); | |
| 423 | - } | |
| 573 | + } | |
| 424 | 574 | |
| 425 | - if ( isset( $license_data->plan ) ) { | |
| 426 | - update_option( $this->product->get_key() . '_license_plan', $license_data->plan ); | |
| 427 | - } | |
| 575 | + /** | |
| 576 | + * Set license validation error message. | |
| 577 | + * | |
| 578 | + * @param string $message Error message. | |
| 579 | + */ | |
| 580 | + public function set_error( $message = '' ) { | |
| 581 | + set_transient( $this->product->get_key() . 'act_err', $message, MINUTE_IN_SECONDS ); | |
| 428 | 582 | |
| 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 | 583 | } |
| 433 | 584 | |
| 434 | 585 | /** |
| 435 | 586 | * Load the Themes screen. |
| 436 | 587 | */ |
| 437 | - function load_themes_screen() { | |
| 588 | + public function load_themes_screen() { | |
| 438 | 589 | add_thickbox(); |
| 439 | 590 | add_action( 'admin_notices', array( &$this, 'update_nag' ) ); |
| 440 | 591 | } |
| 441 | 592 | |
| @@ -441,12 +592,12 @@ | ||
| 441 | 592 | |
| 442 | 593 | /** |
| 443 | 594 | * Alter the nag for themes update. |
| 444 | 595 | */ |
| 445 | - function update_nag() { | |
| 596 | + public function update_nag() { | |
| 446 | 597 | $theme = wp_get_theme( $this->product->get_slug() ); |
| 447 | 598 | $api_response = get_transient( $this->product_key ); |
| 448 | - if ( false === $api_response ) { | |
| 599 | + if ( false === $api_response || ! isset( $api_response->new_version ) ) { | |
| 449 | 600 | return; |
| 450 | 601 | } |
| 451 | 602 | $update_url = wp_nonce_url( 'update.php?action=upgrade-theme&theme=' . urlencode( $this->product->get_slug() ), 'upgrade-theme_' . $this->product->get_slug() ); |
| 452 | 603 | $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 +605,18 @@ | ||
| 454 | 605 | if ( version_compare( $this->product->get_version(), $api_response->new_version, '<' ) ) { |
| 455 | 606 | echo '<div id="update-nag">'; |
| 456 | 607 | printf( |
| 457 | 608 | '<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&inlineId=' . $this->product->get_version() . '_changelog', | |
| 461 | - $theme->get( 'Name' ), | |
| 462 | - $update_url, | |
| 463 | - $update_onclick | |
| 609 | + esc_attr( $theme->get( 'Name' ) ), | |
| 610 | + esc_attr( $api_response->new_version ), | |
| 611 | + esc_url( sprintf( '%s&TB_iframe=true&width=1024&height=800', $this->product->get_changelog() ) ), | |
| 612 | + esc_attr( $theme->get( 'Name' ) ), | |
| 613 | + esc_url( $update_url ), | |
| 614 | + $update_onclick // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped, Already escaped. | |
| 464 | 615 | ); |
| 465 | 616 | echo '</div>'; |
| 466 | - echo '<div id="' . $this->product->get_slug() . '_' . 'changelog" style="display:none;">'; | |
| 467 | - echo wpautop( $api_response->sections['changelog'] ); | |
| 617 | + echo '<div id="' . esc_attr( $this->product->get_slug() ) . '_changelog" style="display:none;">'; | |
| 618 | + echo wp_kses_data( wpautop( $api_response->sections['changelog'] ) ); | |
| 468 | 619 | echo '</div>'; |
| 469 | 620 | } |
| 470 | 621 | } |
| 471 | 622 | |
| @@ -475,9 +626,9 @@ | ||
| 475 | 626 | * @param mixed $value The transient data. |
| 476 | 627 | * |
| 477 | 628 | * @return mixed |
| 478 | 629 | */ |
| 479 | - function theme_update_transient( $value ) { | |
| 630 | + public function theme_update_transient( $value ) { | |
| 480 | 631 | $update_data = $this->check_for_update(); |
| 481 | 632 | if ( $update_data ) { |
| 482 | 633 | $value->response[ $this->product->get_slug() ] = $update_data; |
| 483 | 634 | } |
| @@ -489,9 +640,9 @@ | ||
| 489 | 640 | * Check for updates |
| 490 | 641 | * |
| 491 | 642 | * @return array|bool Either the update data or false in case of failure. |
| 492 | 643 | */ |
| 493 | - function check_for_update() { | |
| 644 | + public function check_for_update() { | |
| 494 | 645 | $update_data = get_transient( $this->product_key ); |
| 495 | 646 | |
| 496 | 647 | if ( false === $update_data ) { |
| 497 | 648 | $failed = false; |
| @@ -506,9 +657,9 @@ | ||
| 506 | 657 | set_transient( $this->product_key, $data, 30 * MINUTE_IN_SECONDS ); |
| 507 | 658 | |
| 508 | 659 | return false; |
| 509 | 660 | } |
| 510 | - $update_data->sections = maybe_unserialize( $update_data->sections ); | |
| 661 | + $update_data->sections = isset( $update_data->sections ) ? maybe_unserialize( $update_data->sections ) : null; | |
| 511 | 662 | |
| 512 | 663 | set_transient( $this->product_key, $update_data, 12 * HOUR_IN_SECONDS ); |
| 513 | 664 | } |
| 514 | 665 | if ( ! isset( $update_data->new_version ) ) { |
| @@ -526,23 +677,21 @@ | ||
| 526 | 677 | * |
| 527 | 678 | * @return bool|mixed Update api response. |
| 528 | 679 | */ |
| 529 | 680 | 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(), | |
| 681 | + | |
| 682 | + $response = $this->safe_get( | |
| 683 | + sprintf( | |
| 684 | + '%slicense/version/%s/%s/%s/%s', | |
| 685 | + Product::API_URL, | |
| 686 | + rawurlencode( $this->product->get_name() ), | |
| 687 | + ( empty( $this->license_key ) ? 'free' : $this->license_key ), | |
| 688 | + $this->product->get_version(), | |
| 689 | + rawurlencode( home_url() ) | |
| 690 | + ), | |
| 541 | 691 | array( |
| 542 | - 'timeout' => 15, | |
| 692 | + 'timeout' => 15, //phpcs:ignore WordPressVIPMinimum.Performance.RemoteRequestTimeout.timeout_timeout, Inherited by wp_remote_get only, for vip environment we use defaults. | |
| 543 | 693 | 'sslverify' => false, |
| 544 | - 'body' => $api_params, | |
| 545 | 694 | ) |
| 546 | 695 | ); |
| 547 | 696 | if ( is_wp_error( $response ) || 200 != wp_remote_retrieve_response_code( $response ) ) { |
| 548 | 697 | return false; |
| @@ -550,8 +699,17 @@ | ||
| 550 | 699 | $update_data = json_decode( wp_remote_retrieve_body( $response ) ); |
| 551 | 700 | if ( ! is_object( $update_data ) ) { |
| 552 | 701 | return false; |
| 553 | 702 | } |
| 703 | + if ( isset( $update_data->slug ) ) { | |
| 704 | + $update_data->slug = $this->product->get_slug(); | |
| 705 | + } | |
| 706 | + if ( isset( $update_data->icons ) ) { | |
| 707 | + $update_data->icons = (array) $update_data->icons; | |
| 708 | + } | |
| 709 | + if ( isset( $update_data->banners ) ) { | |
| 710 | + $update_data->banners = (array) $update_data->banners; | |
| 711 | + } | |
| 554 | 712 | |
| 555 | 713 | return $update_data; |
| 556 | 714 | } |
| 557 | 715 | |
| @@ -557,10 +715,10 @@ | ||
| 557 | 715 | |
| 558 | 716 | /** |
| 559 | 717 | * Delete the update transient |
| 560 | 718 | */ |
| 561 | - function delete_theme_update_transient() { | |
| 562 | - delete_transient( $this->product_key ); | |
| 719 | + public function delete_theme_update_transient() { | |
| 720 | + return delete_transient( $this->product_key ); | |
| 563 | 721 | } |
| 564 | 722 | |
| 565 | 723 | /** |
| 566 | 724 | * Check for Updates at the defined API endpoint and modify the update array. |
| @@ -576,10 +734,15 @@ | ||
| 576 | 734 | return $_transient_data; |
| 577 | 735 | } |
| 578 | 736 | $api_response = $this->api_request(); |
| 579 | 737 | if ( false !== $api_response && is_object( $api_response ) && isset( $api_response->new_version ) ) { |
| 738 | + if ( ! isset( $api_response->plugin ) ) { | |
| 739 | + $api_response->plugin = $this->product->get_slug() . '/' . $this->product->get_file(); | |
| 740 | + } | |
| 580 | 741 | if ( version_compare( $this->product->get_version(), $api_response->new_version, '<' ) ) { |
| 581 | 742 | $_transient_data->response[ $this->product->get_slug() . '/' . $this->product->get_file() ] = $api_response; |
| 743 | + } else { | |
| 744 | + $_transient_data->no_update[ $this->product->get_slug() . '/' . $this->product->get_file() ] = $api_response; | |
| 582 | 745 | } |
| 583 | 746 | } |
| 584 | 747 | |
| 585 | 748 | return $_transient_data; |
| @@ -614,9 +777,9 @@ | ||
| 614 | 777 | * |
| 615 | 778 | * @return object $_data |
| 616 | 779 | */ |
| 617 | 780 | public function plugins_api_filter( $_data, $_action = '', $_args = null ) { |
| 618 | - if ( ( 'plugin_information' != $_action ) || ! isset( $_args->slug ) || ( $_args->slug != $this->product->get_slug() ) ) { | |
| 781 | + if ( ( 'plugin_information' !== $_action ) || ! isset( $_args->slug ) || ( $_args->slug !== $this->product->get_slug() ) ) { | |
| 619 | 782 | return $_data; |
| 620 | 783 | } |
| 621 | 784 | $api_response = $this->api_request(); |
| 622 | 785 | if ( false !== $api_response ) { |
| @@ -633,9 +796,9 @@ | ||
| 633 | 796 | * @param string $url Url to check. |
| 634 | 797 | * |
| 635 | 798 | * @return array $array |
| 636 | 799 | */ |
| 637 | - function http_request_args( $args, $url ) { | |
| 800 | + public function http_request_args( $args, $url ) { | |
| 638 | 801 | // If it is an https request and we are performing a package download, disable ssl verification. |
| 639 | 802 | if ( strpos( $url, 'https://' ) !== false && strpos( $url, 'edd_action=package_download' ) ) { |
| 640 | 803 | $args['sslverify'] = false; |
| 641 | 804 | } |
| @@ -677,8 +840,24 @@ | ||
| 677 | 840 | $this->failed_checks = intval( get_option( $this->product->get_key() . '_failed_checks', 0 ) ); |
| 678 | 841 | $this->register_license_hooks(); |
| 679 | 842 | } |
| 680 | 843 | |
| 844 | + $namespace = apply_filters( 'themesle_sdk_namespace_' . md5( $product->get_basefile() ), false ); | |
| 845 | + | |
| 846 | + if ( false !== $namespace ) { | |
| 847 | + $this->namespace = $namespace; | |
| 848 | + add_filter( 'themeisle_sdk_license_process_' . $namespace, [ $this, 'do_license_process' ], 10, 2 ); | |
| 849 | + add_filter( 'product_' . $namespace . '_license_status', [ $this, 'get_license_status' ], PHP_INT_MAX ); | |
| 850 | + add_filter( 'product_' . $namespace . '_license_key', [ $this->product, 'get_license' ] ); | |
| 851 | + add_filter( 'product_' . $namespace . '_license_plan', [ $this, 'get_plan' ], PHP_INT_MAX ); | |
| 852 | + if ( defined( 'WP_CLI' ) && WP_CLI ) { | |
| 853 | + \WP_CLI::add_command( $namespace . ' activate', [ $this, 'cli_activate' ] ); | |
| 854 | + \WP_CLI::add_command( $namespace . ' deactivate', [ $this, 'cli_deactivate' ] ); | |
| 855 | + \WP_CLI::add_command( $namespace . ' is-active', [ $this, 'cli_is_active' ] ); | |
| 856 | + } | |
| 857 | + } | |
| 858 | + | |
| 859 | + add_action( 'admin_head', [ $this, 'auto_activate' ] ); | |
| 681 | 860 | if ( $this->product->is_plugin() ) { |
| 682 | 861 | add_filter( |
| 683 | 862 | 'pre_set_site_transient_update_plugins', |
| 684 | 863 | [ |
| @@ -686,19 +865,19 @@ | ||
| 686 | 865 | 'pre_set_site_transient_update_plugins_filter', |
| 687 | 866 | ] |
| 688 | 867 | ); |
| 689 | 868 | add_filter( 'plugins_api', array( $this, 'plugins_api_filter' ), 10, 3 ); |
| 690 | - add_filter( 'http_request_args', array( $this, 'http_request_args' ), 10, 2 ); | |
| 869 | + add_filter( 'http_request_args', array( $this, 'http_request_args' ), 10, 2 ); //phpcs:ignore WordPressVIPMinimum.Hooks.RestrictedHooks.http_request_args | |
| 691 | 870 | |
| 692 | 871 | return $this; |
| 693 | 872 | } |
| 694 | 873 | if ( $this->product->is_theme() ) { |
| 695 | 874 | 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' ) ); | |
| 875 | + add_action( 'delete_site_transient_update_themes', array( &$this, 'delete_theme_update_transient' ) ); | |
| 697 | 876 | add_action( 'load-update-core.php', array( &$this, 'delete_theme_update_transient' ) ); |
| 698 | 877 | add_action( 'load-themes.php', array( &$this, 'delete_theme_update_transient' ) ); |
| 699 | 878 | add_action( 'load-themes.php', array( &$this, 'load_themes_screen' ) ); |
| 700 | - add_filter( 'http_request_args', array( $this, 'disable_wporg_update' ), 5, 2 ); | |
| 879 | + add_filter( 'http_request_args', array( $this, 'disable_wporg_update' ), 5, 2 ); //phpcs:ignore WordPressVIPMinimum.Hooks.RestrictedHooks.http_request_args | |
| 701 | 880 | |
| 702 | 881 | return $this; |
| 703 | 882 | |
| 704 | 883 | } |
| @@ -710,10 +889,152 @@ | ||
| 710 | 889 | * Register license fields for the products. |
| 711 | 890 | */ |
| 712 | 891 | public function register_license_hooks() { |
| 713 | 892 | add_action( 'admin_init', array( $this, 'register_settings' ) ); |
| 714 | - add_action( 'admin_init', array( $this, 'activate_license' ) ); | |
| 893 | + add_action( 'admin_init', array( $this, 'process_license' ) ); | |
| 715 | 894 | add_action( 'admin_init', array( $this, 'product_valid' ), 99999999 ); |
| 716 | 895 | add_action( 'admin_notices', array( $this, 'show_notice' ) ); |
| 717 | 896 | add_filter( $this->product->get_key() . '_license_status', array( $this, 'get_license_status' ) ); |
| 897 | + } | |
| 898 | + | |
| 899 | + /** | |
| 900 | + * Check license on filesystem. | |
| 901 | + * | |
| 902 | + * @return mixed License key. | |
| 903 | + */ | |
| 904 | + public function get_file_license() { | |
| 905 | + | |
| 906 | + $license_file = dirname( $this->product->get_basefile() ) . '/license.json'; | |
| 907 | + | |
| 908 | + global $wp_filesystem; | |
| 909 | + if ( ! is_file( $license_file ) ) { | |
| 910 | + return false; | |
| 911 | + } | |
| 912 | + | |
| 913 | + require_once ABSPATH . '/wp-admin/includes/file.php'; | |
| 914 | + \WP_Filesystem(); | |
| 915 | + $content = json_decode( $wp_filesystem->get_contents( $license_file ) ); | |
| 916 | + if ( ! is_object( $content ) ) { | |
| 917 | + return false; | |
| 918 | + } | |
| 919 | + if ( ! isset( $content->key ) ) { | |
| 920 | + return false; | |
| 921 | + } | |
| 922 | + return $content->key; | |
| 923 | + } | |
| 924 | + /** | |
| 925 | + * Run license activation on plugin activate. | |
| 926 | + */ | |
| 927 | + public function auto_activate() { | |
| 928 | + $status = $this->get_license_status(); | |
| 929 | + if ( 'not_active' !== $status ) { | |
| 930 | + return false; | |
| 931 | + } | |
| 932 | + | |
| 933 | + if ( ! empty( $this->namespace ) ) { | |
| 934 | + $license_key = apply_filters( 'product_' . $this->namespace . '_license_key_constant', '' ); | |
| 935 | + } | |
| 936 | + | |
| 937 | + if ( empty( $license_key ) ) { | |
| 938 | + $license_key = $this->get_file_license(); | |
| 939 | + } | |
| 940 | + if ( empty( $license_key ) ) { | |
| 941 | + return; | |
| 942 | + } | |
| 943 | + | |
| 944 | + | |
| 945 | + $this->license_local = $license_key; | |
| 946 | + $lock_key = $this->product->get_key() . '_autoactivated'; | |
| 947 | + | |
| 948 | + if ( 'yes' === get_option( $lock_key, '' ) ) { | |
| 949 | + return; | |
| 950 | + } | |
| 951 | + if ( 'yes' === get_transient( $lock_key ) ) { | |
| 952 | + return; | |
| 953 | + } | |
| 954 | + $response = $this->do_license_process( $license_key, 'activate' ); | |
| 955 | + | |
| 956 | + set_transient( $lock_key, 'yes', 6 * HOUR_IN_SECONDS ); | |
| 957 | + | |
| 958 | + if ( apply_filters( $this->product->get_key() . '_hide_license_notices', false ) ) { | |
| 959 | + return; | |
| 960 | + } | |
| 961 | + | |
| 962 | + if ( true === $response ) { | |
| 963 | + add_action( 'admin_notices', [ $this, 'autoactivate_notice' ] ); | |
| 964 | + } | |
| 965 | + } | |
| 966 | + | |
| 967 | + /** | |
| 968 | + * Show auto-activate notice. | |
| 969 | + */ | |
| 970 | + public function autoactivate_notice() { | |
| 971 | + ?> | |
| 972 | + <div class="notice notice-success is-dismissible"> | |
| 973 | + <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> | |
| 974 | + </div> | |
| 975 | + <?php | |
| 976 | + } | |
| 977 | + | |
| 978 | + /** | |
| 979 | + * Activate product license on this site. | |
| 980 | + * | |
| 981 | + * ## OPTIONS | |
| 982 | + * | |
| 983 | + * @param array $args Command args. | |
| 984 | + * | |
| 985 | + * [<license-key>] | |
| 986 | + * : Product license key. | |
| 987 | + */ | |
| 988 | + public function cli_activate( $args ) { | |
| 989 | + $license_key = isset( $args[0] ) ? trim( $args[0] ) : ''; | |
| 990 | + $response = $this->do_license_process( $license_key, 'activate' ); | |
| 991 | + if ( true !== $response ) { | |
| 992 | + \WP_CLI::error( $response->get_error_message() ); | |
| 993 | + | |
| 994 | + return; | |
| 995 | + } | |
| 996 | + | |
| 997 | + \WP_CLI::success( 'Product successfully activated.' ); | |
| 998 | + } | |
| 999 | + | |
| 1000 | + /** | |
| 1001 | + * Deactivate product license on this site. | |
| 1002 | + * | |
| 1003 | + * @param array $args Command args. | |
| 1004 | + * | |
| 1005 | + * ## OPTIONS | |
| 1006 | + * | |
| 1007 | + * [<license-key>] | |
| 1008 | + * : Product license key. | |
| 1009 | + */ | |
| 1010 | + public function cli_deactivate( $args ) { | |
| 1011 | + $license_key = isset( $args[0] ) ? trim( $args[0] ) : ''; | |
| 1012 | + $response = $this->do_license_process( $license_key, 'deactivate' ); | |
| 1013 | + if ( true !== $response ) { | |
| 1014 | + \WP_CLI::error( $response->get_error_message() ); | |
| 1015 | + | |
| 1016 | + return; | |
| 1017 | + } | |
| 1018 | + | |
| 1019 | + \WP_CLI::success( 'Product successfully deactivated.' ); | |
| 1020 | + } | |
| 1021 | + | |
| 1022 | + /** | |
| 1023 | + * Checks if product has license activated. | |
| 1024 | + * | |
| 1025 | + * @param array $args Command args. | |
| 1026 | + * | |
| 1027 | + * @subcommand is-active | |
| 1028 | + */ | |
| 1029 | + public function cli_is_active( $args ) { | |
| 1030 | + | |
| 1031 | + $status = $this->get_license_status(); | |
| 1032 | + if ( 'valid' === $status ) { | |
| 1033 | + \WP_CLI::halt( 0 ); | |
| 1034 | + | |
| 1035 | + return; | |
| 1036 | + } | |
| 1037 | + | |
| 1038 | + \WP_CLI::halt( 1 ); | |
| 718 | 1039 | } |
| 719 | 1040 | } |