| 1 |
<?php |
| 2 |
/** |
| 3 |
* WordPress to Buffer general plugin functions. |
| 4 |
* |
| 5 |
* @package WP_To_Buffer |
| 6 |
* @author WP Zinc |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Saves the new access token, refresh token and its expiry against |
| 11 |
* all accounts that have the existing access token. |
| 12 |
* |
| 13 |
* @since 6.0.0 |
| 14 |
* |
| 15 |
* @param array $result New Access Token, Refresh Token and Expiry timestamp. |
| 16 |
* @param string $client_id OAuth Client ID used for the Access and Refresh Tokens. |
| 17 |
* @param string $existing_access_token Existing Access Token. |
| 18 |
*/ |
| 19 |
function wp_to_buffer_update_credentials( $result, $client_id, $existing_access_token ) { |
| 20 |
|
| 21 |
// Get Plugin instance. |
| 22 |
$wp_to_buffer = WP_To_Buffer::get_instance(); |
| 23 |
|
| 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 |
|
| 27 |
// Bail if no accounts are found. |
| 28 |
if ( count( $account_ids ) === 0 ) { |
| 29 |
return; |
| 30 |
} |
| 31 |
|
| 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 |
} |
| 41 |
|
| 42 |
} |
| 43 |
|
| 44 |
// Update Access Token when refreshed by the API class. |
| 45 |
add_action( 'wp_to_buffer_api_refresh_token', 'wp_to_buffer_update_credentials', 10, 3 ); |
| 46 |
|