| @@ -44,8 +44,16 @@ | ||
| 44 | 44 | } else { |
| 45 | 45 | $this->settings = array_merge( $this->get_defaults(), $settings ); |
| 46 | 46 | } |
| 47 | 47 | |
| 48 | + // Update Access Token when refreshed by the API class. | |
| 49 | + add_action( 'convertkit_api_get_access_token', array( $this, 'update_credentials' ), 10, 2 ); | |
| 50 | + add_action( 'convertkit_api_refresh_token', array( $this, 'update_credentials' ), 10, 2 ); | |
| 51 | + | |
| 52 | + // Delete credentials if the API class uses a invalid access token. | |
| 53 | + // This prevents the Plugin making repetitive API requests that will 401. | |
| 54 | + add_action( 'convertkit_api_access_token_invalid', array( $this, 'maybe_delete_credentials' ), 10, 2 ); | |
| 55 | + | |
| 48 | 56 | } |
| 49 | 57 | |
| 50 | 58 | /** |
| 51 | 59 | * Returns Plugin settings. |
| @@ -174,11 +182,8 @@ | ||
| 174 | 182 | * @return string |
| 175 | 183 | */ |
| 176 | 184 | public function get_access_token() { |
| 177 | 185 | |
| 178 | - // Reload settings from options table, to ensure we have the latest tokens. | |
| 179 | - $this->refresh_settings(); | |
| 180 | - | |
| 181 | 186 | // Return Access Token from settings. |
| 182 | 187 | return $this->settings['access_token']; |
| 183 | 188 | |
| 184 | 189 | } |
| @@ -204,11 +209,8 @@ | ||
| 204 | 209 | * @return string |
| 205 | 210 | */ |
| 206 | 211 | public function get_refresh_token() { |
| 207 | 212 | |
| 208 | - // Reload settings from options table, to ensure we have the latest tokens. | |
| 209 | - $this->refresh_settings(); | |
| 210 | - | |
| 211 | 213 | // Return Refresh Token from settings. |
| 212 | 214 | return $this->settings['refresh_token']; |
| 213 | 215 | |
| 214 | 216 | } |
| @@ -542,21 +544,8 @@ | ||
| 542 | 544 | |
| 543 | 545 | } |
| 544 | 546 | |
| 545 | 547 | /** |
| 546 | - * Returns whether the Add New Landing Page / Member Content button is disabled in the Plugin settings. | |
| 547 | - * | |
| 548 | - * @since 3.2.0 | |
| 549 | - * | |
| 550 | - * @return bool | |
| 551 | - */ | |
| 552 | - public function add_new_button_disabled() { | |
| 553 | - | |
| 554 | - return ( $this->settings['no_add_new_button'] === 'on' ? true : false ); | |
| 555 | - | |
| 556 | - } | |
| 557 | - | |
| 558 | - /** | |
| 559 | 548 | * Returns whether usage tracking is enabled in the Plugin settings. |
| 560 | 549 | * |
| 561 | 550 | * @since 3.0.4 |
| 562 | 551 | * |
| @@ -601,9 +590,8 @@ | ||
| 601 | 590 | // Advanced. |
| 602 | 591 | 'debug' => '', // blank|on. |
| 603 | 592 | 'no_scripts' => '', // blank|on. |
| 604 | 593 | 'no_css' => '', // blank|on. |
| 605 | - 'no_add_new_button' => '', // blank|on. | |
| 606 | 594 | 'usage_tracking' => '', // blank|on. |
| 607 | 595 | ); |
| 608 | 596 | |
| 609 | 597 | // Add Post Type Default Forms. |
| @@ -633,12 +621,19 @@ | ||
| 633 | 621 | * a WordPress Cron event to refresh the token on expiry. |
| 634 | 622 | * |
| 635 | 623 | * @since 2.8.3 |
| 636 | 624 | * |
| 637 | - * @param array $result New Access Token, Refresh Token and Expiry. | |
| 625 | + * @param array $result New Access Token, Refresh Token and Expiry. | |
| 626 | + * @param string $client_id OAuth Client ID used for the Access and Refresh Tokens. | |
| 638 | 627 | */ |
| 639 | - public function update_credentials( $result ) { | |
| 628 | + public function update_credentials( $result, $client_id ) { | |
| 640 | 629 | |
| 630 | + // Don't save these credentials if they're not for this Client ID. | |
| 631 | + // They're for another Kit Plugin that uses OAuth. | |
| 632 | + if ( $client_id !== CONVERTKIT_OAUTH_CLIENT_ID ) { | |
| 633 | + return; | |
| 634 | + } | |
| 635 | + | |
| 641 | 636 | // Remove any existing persistent notice. |
| 642 | 637 | WP_ConvertKit()->get_class( 'admin_notices' )->delete( 'authorization_failed' ); |
| 643 | 638 | |
| 644 | 639 | $this->save( |
| @@ -657,8 +652,36 @@ | ||
| 657 | 652 | |
| 658 | 653 | } |
| 659 | 654 | |
| 660 | 655 | /** |
| 656 | + * Deletes the stored access token, refresh token and its expiry from the Plugin settings, | |
| 657 | + * and clears any existing scheduled WordPress Cron event to refresh the token on expiry, | |
| 658 | + * when either: | |
| 659 | + * - The access token is invalid | |
| 660 | + * - The access token expired, and refreshing failed | |
| 661 | + * | |
| 662 | + * @since 3.1.0 | |
| 663 | + * | |
| 664 | + * @param WP_Error $result Error result. | |
| 665 | + * @param string $client_id OAuth Client ID used for the Access and Refresh Tokens. | |
| 666 | + */ | |
| 667 | + public function maybe_delete_credentials( $result, $client_id ) { | |
| 668 | + | |
| 669 | + // Don't delete these credentials if they're not for this Client ID. | |
| 670 | + // They're for another Kit Plugin that uses OAuth. | |
| 671 | + if ( $client_id !== CONVERTKIT_OAUTH_CLIENT_ID ) { | |
| 672 | + return; | |
| 673 | + } | |
| 674 | + | |
| 675 | + // Persist an error notice in the WordPress Administration until the user fixes the problem. | |
| 676 | + WP_ConvertKit()->get_class( 'admin_notices' )->add( 'authorization_failed' ); | |
| 677 | + | |
| 678 | + // Delete the credentials from the Plugin settings. | |
| 679 | + $this->delete_credentials(); | |
| 680 | + | |
| 681 | + } | |
| 682 | + | |
| 683 | + /** | |
| 661 | 684 | * Deletes any existing access token, refresh token and its expiry from the Plugin settings, |
| 662 | 685 | * and clears any existing scheduled WordPress Cron event to refresh the token on expiry. |
| 663 | 686 | * |
| 664 | 687 | * @since 2.5.0 |
| @@ -666,16 +689,11 @@ | ||
| 666 | 689 | public function delete_credentials() { |
| 667 | 690 | |
| 668 | 691 | $this->save( |
| 669 | 692 | array( |
| 670 | - // OAuth. | |
| 671 | 693 | 'access_token' => '', |
| 672 | 694 | 'refresh_token' => '', |
| 673 | 695 | 'token_expires' => '', |
| 674 | - | |
| 675 | - // API Key. | |
| 676 | - 'api_key' => '', | |
| 677 | - 'api_secret' => '', | |
| 678 | 696 | ) |
| 679 | 697 | ); |
| 680 | 698 | |
| 681 | 699 | // Clear any existing scheduled WordPress Cron event. |
| @@ -694,27 +712,9 @@ | ||
| 694 | 712 | |
| 695 | 713 | update_option( self::SETTINGS_NAME, array_merge( $this->get(), $settings ) ); |
| 696 | 714 | |
| 697 | 715 | // Reload settings in class, to reflect changes. |
| 698 | - $this->refresh_settings(); | |
| 699 | - | |
| 700 | - } | |
| 701 | - | |
| 702 | - /** | |
| 703 | - * Reloads settings from the options table so this instance has the latest values. | |
| 704 | - * | |
| 705 | - * @since 3.1.1 | |
| 706 | - */ | |
| 707 | - private function refresh_settings() { | |
| 708 | - | |
| 709 | - $settings = get_option( self::SETTINGS_NAME ); | |
| 710 | - | |
| 711 | - if ( ! $settings ) { | |
| 712 | - $this->settings = $this->get_defaults(); | |
| 713 | - return; | |
| 714 | - } | |
| 715 | - | |
| 716 | - $this->settings = array_merge( $this->get_defaults(), $settings ); | |
| 716 | + $this->settings = get_option( self::SETTINGS_NAME ); | |
| 717 | 717 | |
| 718 | 718 | } |
| 719 | 719 | |
| 720 | 720 | } |