-
Notifications
You must be signed in to change notification settings - Fork 0
OAuth: Automatically Refresh Token on Expiry #76
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
a50d82e
8af61a0
5d10848
374cc0e
56b60c1
57ca7af
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -137,7 +137,7 @@ private function maybe_get_and_store_access_token() { | |
| array( | ||
| 'access_token' => $result['access_token'], | ||
| 'refresh_token' => $result['refresh_token'], | ||
| 'token_expires' => ( $result['created_at'] + $result['expires_in'] ), | ||
| 'token_expires' => ( time() + $result['expires_in'] ), | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why are you switching to this? Wouldn't this set the expiration to longer than it actually is?
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @noelherrick Let me know if this needs another review. It's the same principle as the main Kit Plugin: Kit/convertkit-wordpress#840 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Thanks for the reminder! |
||
| ) | ||
| ); | ||
|
|
||
|
|
||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,58 @@ | ||
| <?php | ||
| /** | ||
| * WordPress Cron functions. | ||
| * | ||
| * @package ConvertKit_MM | ||
| * @author ConvertKit | ||
| */ | ||
|
|
||
| /** | ||
| * Refresh the OAuth access token, triggered by WordPress' Cron. | ||
| * | ||
| * @since 1.3.3 | ||
| */ | ||
| function convertkit_mm_refresh_token() { | ||
|
|
||
| // Initialize settings class. | ||
| $settings = new ConvertKit_MM_Settings(); | ||
|
|
||
| // Bail if no existing access and refresh token exists. | ||
| if ( ! $settings->has_access_token() ) { | ||
| return; | ||
| } | ||
| if ( ! $settings->has_refresh_token() ) { | ||
| return; | ||
| } | ||
|
|
||
| // Initialize the API. | ||
| $api = new ConvertKit_MM_API( | ||
| CONVERTKIT_OAUTH_CLIENT_ID, | ||
| CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI, | ||
| $settings->get_access_token(), | ||
| $settings->get_refresh_token(), | ||
| $settings->debug_enabled(), | ||
| 'cron_refresh_token' | ||
| ); | ||
|
|
||
| // Refresh the token. | ||
| $result = $api->refresh_token(); | ||
|
|
||
| // If an error occured, don't save the new tokens. | ||
| // Logging is handled by the ConvertKit_API_V4 class. | ||
| if ( is_wp_error( $result ) ) { | ||
| return; | ||
| } | ||
|
|
||
| $settings->save( | ||
| array( | ||
| 'access_token' => $result['access_token'], | ||
| 'refresh_token' => $result['refresh_token'], | ||
| 'token_expires' => ( time() + $result['expires_in'] ), | ||
| ) | ||
| ); | ||
|
|
||
| } | ||
|
|
||
| // Register action to run above function; this action is created by WordPress' wp_schedule_event() function | ||
| // in update_credentials() in the ConvertKit_Settings class. | ||
| add_action( 'convertkit_mm_refresh_token', 'convertkit_mm_refresh_token' ); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Integration tests weren't running on GitHub actions; this ensures they run.