| @@ -12,15 +12,43 @@ | ||
| 12 | 12 | public $plugin_name; |
| 13 | 13 | public $plugin_slug; |
| 14 | 14 | public $option_name; |
| 15 | 15 | public $version; |
| 16 | - public $author = 'Strategy11'; | |
| 16 | + public $author = 'Strategy11'; | |
| 17 | 17 | public $is_parent_licence = false; |
| 18 | - public $needs_license = true; | |
| 18 | + public $needs_license = true; | |
| 19 | 19 | private $is_expired_addon = false; |
| 20 | 20 | public $license; |
| 21 | 21 | protected $get_beta = false; |
| 22 | + protected $save_status; | |
| 22 | 23 | |
| 24 | + /** | |
| 25 | + * This is used to decide whether the license checks should continue. | |
| 26 | + * The point is to avoid license issues when a site url changes. | |
| 27 | + * | |
| 28 | + * @since 6.8.3 | |
| 29 | + * | |
| 30 | + * @var array | |
| 31 | + */ | |
| 32 | + private $save_response = array(); | |
| 33 | + | |
| 34 | + /** | |
| 35 | + * This is used to flag other add ons not to send a request. | |
| 36 | + * We only want to send a single API request per page load. | |
| 37 | + * | |
| 38 | + * @since 6.8.3 | |
| 39 | + * | |
| 40 | + * @var string | |
| 41 | + */ | |
| 42 | + private $transient_lock_key = 'frm_activate_request_lock'; | |
| 43 | + | |
| 44 | + /** | |
| 45 | + * @since 6.8.3 | |
| 46 | + * | |
| 47 | + * @var bool | |
| 48 | + */ | |
| 49 | + protected $should_clear_cache = true; | |
| 50 | + | |
| 23 | 51 | public function __construct() { |
| 24 | 52 | |
| 25 | 53 | if ( empty( $this->plugin_slug ) ) { |
| 26 | 54 | $this->plugin_slug = preg_replace( '/[^a-zA-Z0-9_\s]/', '', str_replace( ' ', '_', strtolower( $this->plugin_name ) ) ); |
| @@ -74,13 +102,13 @@ | ||
| 74 | 102 | } |
| 75 | 103 | } |
| 76 | 104 | |
| 77 | 105 | /** |
| 78 | - * Updates information on the "View version x.x details" page with custom data. | |
| 106 | + * Updates information on the "View version 6.15 details" page with custom data. | |
| 79 | 107 | * |
| 80 | 108 | * @uses api_request() |
| 81 | 109 | * |
| 82 | - * @param mixed $_data | |
| 110 | + * @param mixed $_data | |
| 83 | 111 | * @param string $_action |
| 84 | 112 | * @param object $_args |
| 85 | 113 | * |
| 86 | 114 | * @return object $_data |
| @@ -170,11 +198,10 @@ | ||
| 170 | 198 | * @since 2.04 |
| 171 | 199 | */ |
| 172 | 200 | public function activate_defined_license() { |
| 173 | 201 | $license = $this->get_defined_license(); |
| 174 | - if ( ! empty( $license ) && ! $this->is_active() && $this->is_time_to_auto_activate() ) { | |
| 202 | + if ( ! empty( $license ) && ! $this->is_active() && ! $this->checked_recently( '1 day' ) ) { | |
| 175 | 203 | $response = $this->activate_license( $license ); |
| 176 | - $this->set_auto_activate_time(); | |
| 177 | 204 | if ( ! $response['success'] ) { |
| 178 | 205 | $license = ''; |
| 179 | 206 | } |
| 180 | 207 | } |
| @@ -187,11 +214,11 @@ | ||
| 187 | 214 | * |
| 188 | 215 | * @since 2.04 |
| 189 | 216 | */ |
| 190 | 217 | public function get_defined_license() { |
| 191 | - $consant_name = 'FRM_' . strtoupper( $this->plugin_slug ) . '_LICENSE'; | |
| 218 | + $constant_name = 'FRM_' . strtoupper( $this->plugin_slug ) . '_LICENSE'; | |
| 192 | 219 | |
| 193 | - return defined( $consant_name ) ? constant( $consant_name ) : false; | |
| 220 | + return defined( $constant_name ) ? constant( $constant_name ) : false; | |
| 194 | 221 | } |
| 195 | 222 | |
| 196 | 223 | public function set_license( $license ) { |
| 197 | 224 | update_option( $this->option_name . 'key', $license ); |
| @@ -196,24 +223,8 @@ | ||
| 196 | 223 | public function set_license( $license ) { |
| 197 | 224 | update_option( $this->option_name . 'key', $license ); |
| 198 | 225 | } |
| 199 | 226 | |
| 200 | - /** | |
| 201 | - * If the license is in the config, limit the frequency of checks. | |
| 202 | - * The license may be entered incorrectly, so we don't want to check on every page load. | |
| 203 | - * | |
| 204 | - * @since 2.04 | |
| 205 | - */ | |
| 206 | - private function is_time_to_auto_activate() { | |
| 207 | - $last_try = get_option( $this->option_name . 'last_activate' ); | |
| 208 | - | |
| 209 | - return ( ! $last_try || $last_try < strtotime( '-1 day' ) ); | |
| 210 | - } | |
| 211 | - | |
| 212 | - private function set_auto_activate_time() { | |
| 213 | - update_option( $this->option_name . 'last_activate', time() ); | |
| 214 | - } | |
| 215 | - | |
| 216 | 227 | public function is_active() { |
| 217 | 228 | return get_option( $this->option_name . 'active' ); |
| 218 | 229 | } |
| 219 | 230 | |
| @@ -219,12 +230,12 @@ | ||
| 219 | 230 | |
| 220 | 231 | /** |
| 221 | 232 | * @since 3.04.03 |
| 222 | 233 | * |
| 223 | - * @param array error | |
| 234 | + * @param array|string $error | |
| 224 | 235 | */ |
| 225 | 236 | public function maybe_clear_license( $error ) { |
| 226 | - if ( $error['code'] === 'disabled' && $error['license'] === $this->license ) { | |
| 237 | + if ( is_array( $error ) && $error['code'] === 'disabled' && $error['license'] === $this->license ) { | |
| 227 | 238 | $this->clear_license(); |
| 228 | 239 | } |
| 229 | 240 | } |
| 230 | 241 | |
| @@ -230,15 +241,41 @@ | ||
| 230 | 241 | |
| 231 | 242 | public function clear_license() { |
| 232 | 243 | delete_option( $this->option_name . 'active' ); |
| 233 | 244 | delete_option( $this->option_name . 'key' ); |
| 234 | - delete_site_option( $this->transient_key() ); | |
| 235 | - delete_option( $this->transient_key() ); | |
| 236 | - $this->delete_cache(); | |
| 245 | + | |
| 246 | + if ( $this->should_clear_cache ) { | |
| 247 | + delete_site_option( $this->transient_key() ); | |
| 248 | + delete_option( $this->transient_key() ); | |
| 249 | + $this->delete_cache(); | |
| 250 | + $this->should_clear_cache = true; | |
| 251 | + } | |
| 237 | 252 | } |
| 238 | 253 | |
| 254 | + /** | |
| 255 | + * Don't save an invalid license. | |
| 256 | + * | |
| 257 | + * @since 6.8.3 | |
| 258 | + * | |
| 259 | + * @param bool $is_valid If license activation was successful. | |
| 260 | + * | |
| 261 | + * @return void | |
| 262 | + */ | |
| 263 | + protected function maybe_set_active( $is_valid ) { | |
| 264 | + update_option( $this->option_name . 'active', $is_valid ); | |
| 265 | + if ( $is_valid ) { | |
| 266 | + $this->set_active( $is_valid ); | |
| 267 | + return; | |
| 268 | + } | |
| 269 | + | |
| 270 | + // Don't save the license if it's invalid. | |
| 271 | + $this->should_clear_cache = false; | |
| 272 | + $this->clear_license(); | |
| 273 | + delete_option( $this->option_name . 'key' ); | |
| 274 | + $this->license = ''; | |
| 275 | + } | |
| 276 | + | |
| 239 | 277 | public function set_active( $is_active ) { |
| 240 | - update_option( $this->option_name . 'active', $is_active ); | |
| 241 | 278 | $this->delete_cache(); |
| 242 | 279 | FrmAppHelper::save_combined_js(); |
| 243 | 280 | $this->update_pro_capabilities(); |
| 244 | 281 | } |
| @@ -261,9 +298,9 @@ | ||
| 261 | 298 | foreach ( $caps as $cap => $cap_desc ) { |
| 262 | 299 | $cap_roles = (array) ( isset( $settings->$cap ) ? $settings->$cap : 'administrator' ); |
| 263 | 300 | |
| 264 | 301 | // Make sure administrators always have permissions. |
| 265 | - if ( ! in_array( 'administrator', $cap_roles ) ) { | |
| 302 | + if ( ! in_array( 'administrator', $cap_roles, true ) ) { | |
| 266 | 303 | array_push( $cap_roles, 'administrator' ); |
| 267 | 304 | } |
| 268 | 305 | |
| 269 | 306 | foreach ( $roles as $role => $details ) { |
| @@ -281,8 +318,11 @@ | ||
| 281 | 318 | */ |
| 282 | 319 | protected function delete_cache() { |
| 283 | 320 | delete_transient( 'frm_api_licence' ); |
| 284 | 321 | |
| 322 | + // Cleanup option that has been removed. | |
| 323 | + delete_option( $this->option_name . 'last_activate' ); | |
| 324 | + | |
| 285 | 325 | $api = new FrmFormApi( $this->license ); |
| 286 | 326 | $api->reset_cached(); |
| 287 | 327 | |
| 288 | 328 | $api = new FrmFormTemplateApi( $this->license ); |
| @@ -289,8 +329,11 @@ | ||
| 289 | 329 | $api->reset_cached(); |
| 290 | 330 | |
| 291 | 331 | $api = new FrmApplicationApi( $this->license ); |
| 292 | 332 | $api->reset_cached(); |
| 333 | + | |
| 334 | + $api = new FrmSalesApi(); | |
| 335 | + $api->reset_cached(); | |
| 293 | 336 | } |
| 294 | 337 | |
| 295 | 338 | /** |
| 296 | 339 | * The Pro version includes the show_license_message function. |
| @@ -327,9 +370,9 @@ | ||
| 327 | 370 | $wp_list_table = _get_list_table( 'WP_Plugins_List_Table' ); |
| 328 | 371 | $id = sanitize_title( $plugin['Name'] ) . '-next'; |
| 329 | 372 | |
| 330 | 373 | echo '<tr class="plugin-update-tr active" id="' . esc_attr( $id ) . '"><td colspan="' . esc_attr( $wp_list_table->get_column_count() ) . '" class="plugin-update colspanchange"><div class="update-message notice error inline notice-error notice-alt"><p>'; |
| 331 | - echo FrmAppHelper::kses( $message, 'a' ); // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped | |
| 374 | + FrmAppHelper::kses_echo( $message, 'a' ); | |
| 332 | 375 | echo '<script type="text/javascript">var d = document.getElementById("' . esc_attr( $id ) . '").previousSibling;if ( d !== null ){ d.className = d.className + " update"; }</script>'; |
| 333 | 376 | echo '</p></div></td></tr>'; |
| 334 | 377 | } |
| 335 | 378 | |
| @@ -338,9 +381,9 @@ | ||
| 338 | 381 | return $transient; |
| 339 | 382 | } |
| 340 | 383 | |
| 341 | 384 | if ( $this->is_current_version( $transient ) ) { |
| 342 | - //make sure it doesn't show there is an update if plugin is up-to-date | |
| 385 | + // Make sure it doesn't show there is an update if plugin is up-to-date. | |
| 343 | 386 | if ( isset( $transient->response[ $this->plugin_folder ] ) ) { |
| 344 | 387 | unset( $transient->response[ $this->plugin_folder ] ); |
| 345 | 388 | } |
| 346 | 389 | } elseif ( isset( $transient->response ) && isset( $transient->response[ $this->plugin_folder ] ) ) { |
| @@ -363,20 +406,21 @@ | ||
| 363 | 406 | * Check if the plugin information is correct to allow an update |
| 364 | 407 | * |
| 365 | 408 | * @since 3.04.03 |
| 366 | 409 | * |
| 367 | - * @param object $transient - the current plugin info saved for update | |
| 410 | + * @param object $transient The current plugin info saved for update. | |
| 368 | 411 | */ |
| 369 | 412 | private function prepare_update_details( &$transient ) { |
| 370 | 413 | $version_info = $transient; |
| 371 | - $has_beta_url = isset( $version_info->beta ) && ! empty( $version_info->beta ); | |
| 414 | + $has_beta_url = ! empty( $version_info->beta ); | |
| 372 | 415 | if ( $this->get_beta && ! $has_beta_url ) { |
| 373 | 416 | $version_info = (object) $this->get_api_info( $this->license ); |
| 374 | 417 | } |
| 375 | 418 | |
| 376 | - if ( isset( $version_info->new_version ) && ! empty( $version_info->new_version ) ) { | |
| 419 | + if ( ! empty( $version_info->new_version ) ) { | |
| 377 | 420 | $this->clear_old_plugin_version( $version_info ); |
| 378 | - if ( $version_info === false ) { // was cleared with timeout | |
| 421 | + if ( $version_info === false ) { | |
| 422 | + // Was cleared with timeout. | |
| 379 | 423 | $transient = false; |
| 380 | 424 | } else { |
| 381 | 425 | $this->maybe_use_beta_url( $version_info ); |
| 382 | 426 | |
| @@ -414,11 +458,12 @@ | ||
| 414 | 458 | * |
| 415 | 459 | * @since 2.05.05 |
| 416 | 460 | */ |
| 417 | 461 | private function clear_old_plugin_version( &$version_info ) { |
| 418 | - $timeout = ( isset( $version_info->timeout ) && ! empty( $version_info->timeout ) ) ? $version_info->timeout : 0; | |
| 462 | + $timeout = ! empty( $version_info->timeout ) ? $version_info->timeout : 0; | |
| 419 | 463 | if ( ! empty( $timeout ) && time() > $timeout ) { |
| 420 | - $version_info = false; // Cache is expired | |
| 464 | + // Cache is expired. | |
| 465 | + $version_info = false; | |
| 421 | 466 | $api = new FrmFormApi( $this->license ); |
| 422 | 467 | $api->reset_cached(); |
| 423 | 468 | } |
| 424 | 469 | } |
| @@ -429,12 +474,12 @@ | ||
| 429 | 474 | * |
| 430 | 475 | * @since 3.04.03 |
| 431 | 476 | */ |
| 432 | 477 | private function maybe_use_beta_url( &$version_info ) { |
| 433 | - if ( $this->get_beta && isset( $version_info->beta ) && ! empty( $version_info->beta ) ) { | |
| 478 | + if ( $this->get_beta && ! empty( $version_info->beta ) ) { | |
| 434 | 479 | $version_info->new_version = $version_info->beta['version']; |
| 435 | 480 | $version_info->package = $version_info->beta['package']; |
| 436 | - if ( isset( $version_info->plugin ) && ! empty( $version_info->plugin ) ) { | |
| 481 | + if ( ! empty( $version_info->plugin ) ) { | |
| 437 | 482 | $version_info->plugin = $version_info->beta['plugin']; |
| 438 | 483 | } |
| 439 | 484 | } |
| 440 | 485 | } |
| @@ -443,9 +488,9 @@ | ||
| 443 | 488 | if ( empty( $transient->checked ) || ! isset( $transient->checked[ $this->plugin_folder ] ) ) { |
| 444 | 489 | return false; |
| 445 | 490 | } |
| 446 | 491 | |
| 447 | - $response = ! isset( $transient->response ) || empty( $transient->response ); | |
| 492 | + $response = empty( $transient->response ); | |
| 448 | 493 | if ( $response ) { |
| 449 | 494 | return true; |
| 450 | 495 | } |
| 451 | 496 | |
| @@ -451,12 +496,14 @@ | ||
| 451 | 496 | |
| 452 | 497 | return isset( $transient->response ) && isset( $transient->response[ $this->plugin_folder ] ) && $transient->checked[ $this->plugin_folder ] === $transient->response[ $this->plugin_folder ]->new_version; |
| 453 | 498 | } |
| 454 | 499 | |
| 500 | + /** | |
| 501 | + * @return bool | |
| 502 | + */ | |
| 455 | 503 | private function has_been_cleared() { |
| 456 | 504 | $last_cleared = get_option( 'frm_last_cleared' ); |
| 457 | - | |
| 458 | - return ( $last_cleared && $last_cleared > gmdate( 'Y-m-d H:i:s', strtotime( '-5 minutes' ) ) ); | |
| 505 | + return $last_cleared && $last_cleared > gmdate( 'Y-m-d H:i:s', strtotime( '-5 minutes' ) ); | |
| 459 | 506 | } |
| 460 | 507 | |
| 461 | 508 | private function cleared_plugins() { |
| 462 | 509 | update_option( 'frm_last_cleared', gmdate( 'Y-m-d H:i:s' ) ); |
| @@ -466,15 +513,18 @@ | ||
| 466 | 513 | if ( empty( $this->license ) || empty( $this->plugin_slug ) || isset( $_POST['license'] ) ) { // phpcs:ignore WordPress.Security.NonceVerification.Missing |
| 467 | 514 | return; |
| 468 | 515 | } |
| 469 | 516 | |
| 517 | + if ( $this->get_defined_license() ) { | |
| 518 | + // Don't check if the license is defined in wp-config.php since we can't remove it. | |
| 519 | + return; | |
| 520 | + } | |
| 521 | + | |
| 470 | 522 | // Only check weekly. |
| 471 | - if ( $this->checked_recently( '7 days' ) ) { | |
| 523 | + if ( $this->checked_recently( '7 days', 'valid' ) || $this->is_running() ) { | |
| 472 | 524 | return; |
| 473 | 525 | } |
| 474 | 526 | |
| 475 | - $this->update_last_checked(); | |
| 476 | - | |
| 477 | 527 | $response = $this->get_license_status(); |
| 478 | 528 | if ( 'revoked' === $response['status'] || 'blocked' === $response['status'] || 'disabled' === $response['status'] || 'missing' === $response['status'] ) { |
| 479 | 529 | $this->clear_license(); |
| 480 | 530 | } |
| @@ -482,19 +532,36 @@ | ||
| 482 | 532 | |
| 483 | 533 | /** |
| 484 | 534 | * Has this been checked too recently? |
| 485 | 535 | * |
| 486 | - * @param string $time ie. '1 day' | |
| 536 | + * @param string $time ie. '1 day'. | |
| 537 | + * @param string $required_status Return false if the last check does not match. ie 'valid'. | |
| 538 | + * | |
| 487 | 539 | * @return bool |
| 488 | 540 | */ |
| 489 | - private function checked_recently( $time ) { | |
| 541 | + private function checked_recently( $time, $required_status = '' ) { | |
| 490 | 542 | $last_checked = $this->last_checked(); |
| 543 | + $is_429 = isset( $last_checked['response_code'] ) && 429 === $last_checked['response_code']; | |
| 544 | + if ( $is_429 ) { | |
| 545 | + // If the last check was a a rate limit, we'll need to check again sooner. | |
| 546 | + $time = '5 minutes'; | |
| 547 | + $required_status = ''; | |
| 548 | + } | |
| 549 | + | |
| 550 | + if ( $required_status && ( ! isset( $last_checked['status'] ) || $last_checked['status'] !== $required_status ) ) { | |
| 551 | + // If the last check was invalid, we don't need to check again. | |
| 552 | + return true; | |
| 553 | + } | |
| 554 | + | |
| 555 | + $checked_time = isset( $last_checked['time'] ) ? $last_checked['time'] : false; | |
| 491 | 556 | $time_ago = gmdate( 'Y-m-d H:i:s', strtotime( '-' . $time ) ); |
| 492 | - return $last_checked && $last_checked > $time_ago; | |
| 557 | + return $checked_time && $checked_time > $time_ago; | |
| 493 | 558 | } |
| 494 | 559 | |
| 495 | 560 | /** |
| 496 | - * @return bool|string | |
| 561 | + * @since 6.8.3 Switched to an array to store extra response info. | |
| 562 | + * | |
| 563 | + * @return array | |
| 497 | 564 | */ |
| 498 | 565 | private function last_checked() { |
| 499 | 566 | if ( is_multisite() ) { |
| 500 | 567 | $last_checked = get_site_option( $this->transient_key() ); |
| @@ -500,9 +567,13 @@ | ||
| 500 | 567 | $last_checked = get_site_option( $this->transient_key() ); |
| 501 | 568 | } else { |
| 502 | 569 | $last_checked = get_option( $this->transient_key() ); |
| 503 | 570 | } |
| 504 | - return $last_checked; | |
| 571 | + if ( $last_checked && ! is_array( $last_checked ) ) { | |
| 572 | + // Get string into array for existing values. | |
| 573 | + $last_checked = array( 'time' => $last_checked ); | |
| 574 | + } | |
| 575 | + return $last_checked ? (array) $last_checked : array(); | |
| 505 | 576 | } |
| 506 | 577 | |
| 507 | 578 | /** |
| 508 | 579 | * @return void |
| @@ -507,12 +578,13 @@ | ||
| 507 | 578 | /** |
| 508 | 579 | * @return void |
| 509 | 580 | */ |
| 510 | 581 | private function update_last_checked() { |
| 582 | + $this->save_response['time'] = gmdate( 'Y-m-d H:i:s' ); | |
| 511 | 583 | if ( is_multisite() ) { |
| 512 | - update_site_option( $this->transient_key(), gmdate( 'Y-m-d H:i:s' ) ); | |
| 584 | + update_site_option( $this->transient_key(), $this->save_response ); | |
| 513 | 585 | } else { |
| 514 | - update_option( $this->transient_key(), gmdate( 'Y-m-d H:i:s' ) ); | |
| 586 | + update_option( $this->transient_key(), $this->save_response ); | |
| 515 | 587 | } |
| 516 | 588 | } |
| 517 | 589 | |
| 518 | 590 | /** |
| @@ -538,8 +610,10 @@ | ||
| 538 | 610 | |
| 539 | 611 | $plugin_slug = FrmAppHelper::get_param( 'plugin', '', 'post', 'sanitize_text_field' ); |
| 540 | 612 | $response = self::activate_license_for_plugin( $license, $plugin_slug ); |
| 541 | 613 | |
| 614 | + FrmInbox::clear_cache(); | |
| 615 | + | |
| 542 | 616 | wp_send_json( $response ); |
| 543 | 617 | } |
| 544 | 618 | |
| 545 | 619 | /** |
| @@ -574,9 +648,9 @@ | ||
| 574 | 648 | if ( 'valid' === $response['status'] ) { |
| 575 | 649 | $is_valid = 'valid'; |
| 576 | 650 | $response['success'] = true; |
| 577 | 651 | } |
| 578 | - $this->set_active( $is_valid ); | |
| 652 | + $this->maybe_set_active( $is_valid ); | |
| 579 | 653 | } |
| 580 | 654 | |
| 581 | 655 | $this->update_last_checked(); |
| 582 | 656 | |
| @@ -602,8 +676,10 @@ | ||
| 602 | 676 | ); |
| 603 | 677 | } |
| 604 | 678 | |
| 605 | 679 | private function get_license_status() { |
| 680 | + $this->set_running(); | |
| 681 | + | |
| 606 | 682 | $response = array( |
| 607 | 683 | 'status' => 'missing', |
| 608 | 684 | 'error' => true, |
| 609 | 685 | ); |
| @@ -618,10 +694,11 @@ | ||
| 618 | 694 | $license_data = $this->send_mothership_request( 'activate_license' ); |
| 619 | 695 | |
| 620 | 696 | // $license_data->license will be either "valid" or "invalid" |
| 621 | 697 | if ( is_array( $license_data ) ) { |
| 622 | - if ( isset( $license_data['license'] ) && in_array( $license_data['license'], array( 'valid', 'invalid' ), true ) ) { | |
| 623 | - $response['status'] = $license_data['license']; | |
| 698 | + if ( ! empty( $license_data['license'] ) && in_array( $license_data['license'], array( 'valid', 'invalid' ), true ) ) { | |
| 699 | + $response['status'] = $license_data['license']; | |
| 700 | + $this->save_status['status'] = $license_data['license']; | |
| 624 | 701 | } |
| 625 | 702 | } else { |
| 626 | 703 | $response['status'] = $license_data; |
| 627 | 704 | } |
| @@ -628,8 +705,10 @@ | ||
| 628 | 705 | } catch ( Exception $e ) { |
| 629 | 706 | $response['status'] = $e->getMessage(); |
| 630 | 707 | } |
| 631 | 708 | |
| 709 | + $this->update_last_checked(); | |
| 710 | + $this->done_running(); | |
| 632 | 711 | return $response; |
| 633 | 712 | } |
| 634 | 713 | |
| 635 | 714 | private function get_messages() { |
| @@ -685,8 +764,9 @@ | ||
| 685 | 764 | $response['message'] = $e->getMessage(); |
| 686 | 765 | } |
| 687 | 766 | |
| 688 | 767 | $this_plugin->clear_license(); |
| 768 | + FrmInbox::clear_cache(); | |
| 689 | 769 | |
| 690 | 770 | wp_send_json( $response ); |
| 691 | 771 | } |
| 692 | 772 | |
| @@ -721,19 +801,20 @@ | ||
| 721 | 801 | 'timeout' => 25, |
| 722 | 802 | 'user-agent' => $this->plugin_slug . '/' . $this->version . '; ' . get_bloginfo( 'url' ), |
| 723 | 803 | ); |
| 724 | 804 | |
| 725 | - $resp = wp_remote_post( | |
| 805 | + $resp = wp_remote_post( | |
| 726 | 806 | $this->store_url . '?l=' . urlencode( base64_encode( $this->license ) ), |
| 727 | 807 | $arg_array |
| 728 | 808 | ); |
| 729 | - $body = wp_remote_retrieve_body( $resp ); | |
| 809 | + $body = wp_remote_retrieve_body( $resp ); | |
| 810 | + $this->save_status = array( 'response_code' => wp_remote_retrieve_response_code( $resp ) ); | |
| 730 | 811 | |
| 731 | 812 | $message = __( 'Your License Key was invalid', 'formidable' ); |
| 732 | 813 | if ( is_wp_error( $resp ) ) { |
| 733 | 814 | $link = FrmAppHelper::admin_upgrade_link( 'api', 'knowledgebase/why-cant-i-activate-formidable-pro/' ); |
| 734 | 815 | /* translators: %1$s: Start link HTML, %2$s: End link HTML */ |
| 735 | - $message = sprintf( __( 'You had an error communicating with the Formidable API. %1$sClick here%2$s for more information.', 'formidable' ), '<a href="' . esc_url( $link ) . '" target="_blank">', '</a>' ); | |
| 816 | + $message = sprintf( __( 'You had an error communicating with the Formidable API. %1$sClick here%2$s for more information.', 'formidable' ), '<a href="' . esc_url( $link ) . '" target="_blank">', '</a>' ); | |
| 736 | 817 | $message .= ' ' . $resp->get_error_message(); |
| 737 | 818 | } elseif ( 'error' === $body || is_wp_error( $body ) ) { |
| 738 | 819 | $message = __( 'You had an HTTP error connecting to the Formidable API', 'formidable' ); |
| 739 | 820 | } else { |
| @@ -743,9 +824,9 @@ | ||
| 743 | 824 | $message = $json_res['error']; |
| 744 | 825 | } else { |
| 745 | 826 | $message = $json_res; |
| 746 | 827 | } |
| 747 | - } elseif ( isset( $resp['response'] ) && isset( $resp['response']['code'] ) ) { | |
| 828 | + } elseif ( ! empty( $resp['response'] ) && ! empty( $resp['response']['code'] ) ) { | |
| 748 | 829 | $resp['body'] = wp_strip_all_tags( $resp['body'] ); |
| 749 | 830 | |
| 750 | 831 | $message = sprintf( |
| 751 | 832 | /* translators: %1$s: Error code, %2$s: Error message */ |
| @@ -753,9 +834,9 @@ | ||
| 753 | 834 | esc_html( $resp['response']['code'] ), |
| 754 | 835 | $resp['response']['message'] . ' ' . $resp['body'] |
| 755 | 836 | ); |
| 756 | 837 | } |
| 757 | - } | |
| 838 | + }//end if | |
| 758 | 839 | |
| 759 | 840 | return $message; |
| 760 | 841 | } |
| 761 | 842 | |
| @@ -766,6 +847,46 @@ | ||
| 766 | 847 | $updates->translations = array(); |
| 767 | 848 | $updates->no_update = array(); |
| 768 | 849 | $updates->checked = array(); |
| 769 | 850 | set_site_transient( 'update_plugins', $updates ); |
| 851 | + } | |
| 852 | + | |
| 853 | + /** | |
| 854 | + * Set the transient key for the lock. It should be unique to the license. | |
| 855 | + * | |
| 856 | + * @since 6.8.3 | |
| 857 | + * | |
| 858 | + * @return bool | |
| 859 | + */ | |
| 860 | + protected function lock_key() { | |
| 861 | + return $this->transient_lock_key . '_' . $this->license; | |
| 862 | + } | |
| 863 | + | |
| 864 | + /** | |
| 865 | + * Prevent multiple requests from running at the same time. | |
| 866 | + * | |
| 867 | + * @since 6.8.3 | |
| 868 | + * | |
| 869 | + * @return bool | |
| 870 | + */ | |
| 871 | + protected function is_running() { | |
| 872 | + return get_transient( $this->lock_key() ); | |
| 873 | + } | |
| 874 | + | |
| 875 | + /** | |
| 876 | + * @since 6.8.3 | |
| 877 | + * | |
| 878 | + * @return void | |
| 879 | + */ | |
| 880 | + protected function set_running() { | |
| 881 | + set_transient( $this->lock_key(), true, 2 * MINUTE_IN_SECONDS ); | |
| 882 | + } | |
| 883 | + | |
| 884 | + /** | |
| 885 | + * @since 6.8.3 | |
| 886 | + * | |
| 887 | + * @return void | |
| 888 | + */ | |
| 889 | + protected function done_running() { | |
| 890 | + delete_transient( $this->lock_key() ); | |
| 770 | 891 | } |
| 771 | 892 | } |