| @@ -6,10 +6,10 @@ | ||
| 6 | 6 | * @author WP Zinc |
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | /** |
| 10 | - * Saves the new access token, refresh token and its expiry, and schedules | |
| 11 | - * a WordPress Cron event to refresh the token on expiry. | |
| 10 | + * Saves the new access token, refresh token and its expiry against | |
| 11 | + * all accounts that have the existing access token. | |
| 12 | 12 | * |
| 13 | 13 | * @since 6.0.0 |
| 14 | 14 | * |
| 15 | 15 | * @param array $result New Access Token, Refresh Token and Expiry timestamp. |
| @@ -20,24 +20,50 @@ | ||
| 20 | 20 | |
| 21 | 21 | // Get Plugin instance. |
| 22 | 22 | $wp_to_buffer = WP_To_Buffer::get_instance(); |
| 23 | 23 | |
| 24 | - // Get the account ID based on the existing access token. | |
| 25 | - $account_id = $wp_to_buffer->get_class( 'settings' )->get_account_id_by_access_token( $existing_access_token ); | |
| 24 | + // Get the account IDs based on the existing access token. | |
| 25 | + $account_ids = $wp_to_buffer->get_class( 'settings' )->get_account_ids_by_access_token( $existing_access_token ); | |
| 26 | 26 | |
| 27 | - // Bail if the account ID is not found. | |
| 28 | - if ( empty( $account_id ) ) { | |
| 27 | + // Bail if no accounts are found. | |
| 28 | + if ( count( $account_ids ) === 0 ) { | |
| 29 | 29 | return; |
| 30 | 30 | } |
| 31 | 31 | |
| 32 | - // Update the access and refresh tokens in the Plugin settings. | |
| 33 | - $wp_to_buffer->get_class( 'settings' )->update_account_credentials( | |
| 34 | - $result['access_token'], | |
| 35 | - $result['refresh_token'], | |
| 36 | - $result['token_expires'], | |
| 37 | - $account_id | |
| 38 | - ); | |
| 32 | + // Update the access and refresh tokens for each account. | |
| 33 | + foreach ( $account_ids as $account_id ) { | |
| 34 | + $wp_to_buffer->get_class( 'settings' )->update_account_credentials( | |
| 35 | + $result['access_token'], | |
| 36 | + $result['refresh_token'], | |
| 37 | + $result['token_expires'], | |
| 38 | + $account_id | |
| 39 | + ); | |
| 40 | + } | |
| 39 | 41 | |
| 40 | 42 | } |
| 41 | 43 | |
| 42 | 44 | // Update Access Token when refreshed by the API class. |
| 43 | -add_action( 'wp_to_buffer_pro_api_refresh_token', 'wp_to_buffer_update_credentials', 10, 3 ); | |
| 45 | +add_action( 'wp_to_buffer_api_refresh_token', 'wp_to_buffer_update_credentials', 10, 3 ); | |
| 46 | + | |
| 47 | +/** | |
| 48 | + * Clears stored credentials when a refresh returns invalid_grant, which is terminal. | |
| 49 | + * | |
| 50 | + * @since 6.2.5 | |
| 51 | + * | |
| 52 | + * @param \WP_Error $result Error from API. | |
| 53 | + * @param string $client_id OAuth Client ID. | |
| 54 | + * @param string $existing_access_token Existing Access Token. | |
| 55 | + * @param string $existing_refresh_token Existing Refresh Token. | |
| 56 | + */ | |
| 57 | +function wp_to_buffer_revoke_credentials( $result, $client_id, $existing_access_token, $existing_refresh_token ) { | |
| 58 | + | |
| 59 | + // Don't revoke credentials if the error is not invalid_grant. | |
| 60 | + if ( $result->get_error_code() !== 'invalid_grant' ) { | |
| 61 | + return; | |
| 62 | + } | |
| 63 | + | |
| 64 | + // Revoke credentials by removing them from the account. | |
| 65 | + $wp_to_buffer = WP_To_Buffer::get_instance(); | |
| 66 | + $wp_to_buffer->get_class( 'settings' )->clear_account_credentials_by_refresh_token( $existing_refresh_token ); | |
| 67 | + | |
| 68 | +} | |
| 69 | +add_action( 'wp_to_buffer_api_refresh_token_error', 'wp_to_buffer_revoke_credentials', 10, 4 ); | |