| @@ -41,5 +41,29 @@ | ||
| 41 | 41 | |
| 42 | 42 | } |
| 43 | 43 | |
| 44 | 44 | // Update Access Token when refreshed by the API class. |
| 45 | -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 ); | |