| @@ -6,8 +6,59 @@ | ||
| 6 | 6 | * @author ConvertKit |
| 7 | 7 | */ |
| 8 | 8 | |
| 9 | 9 | /** |
| 10 | + * Refresh the OAuth access token, triggered by WordPress' Cron. | |
| 11 | + * | |
| 12 | + * @since 2.8.3 | |
| 13 | + */ | |
| 14 | +function convertkit_refresh_token() { | |
| 15 | + | |
| 16 | + // Get Settings and Log classes. | |
| 17 | + $settings = new ConvertKit_Settings(); | |
| 18 | + | |
| 19 | + // Bail if no existing access and refresh token exists. | |
| 20 | + if ( ! $settings->has_access_token() ) { | |
| 21 | + return; | |
| 22 | + } | |
| 23 | + if ( ! $settings->has_refresh_token() ) { | |
| 24 | + return; | |
| 25 | + } | |
| 26 | + | |
| 27 | + // Initialize the API. | |
| 28 | + $api = new ConvertKit_API_V4( | |
| 29 | + CONVERTKIT_OAUTH_CLIENT_ID, | |
| 30 | + CONVERTKIT_OAUTH_CLIENT_REDIRECT_URI, | |
| 31 | + $settings->get_access_token(), | |
| 32 | + $settings->get_refresh_token(), | |
| 33 | + $settings->debug_enabled(), | |
| 34 | + 'cron_refresh_token' | |
| 35 | + ); | |
| 36 | + | |
| 37 | + // Refresh the token. | |
| 38 | + $result = $api->refresh_token(); | |
| 39 | + | |
| 40 | + // If an error occurred, don't save the new tokens. | |
| 41 | + // Logging is handled by the ConvertKit_API_V4 class. | |
| 42 | + if ( is_wp_error( $result ) ) { | |
| 43 | + return; | |
| 44 | + } | |
| 45 | + | |
| 46 | + $settings->save( | |
| 47 | + array( | |
| 48 | + 'access_token' => $result['access_token'], | |
| 49 | + 'refresh_token' => $result['refresh_token'], | |
| 50 | + 'token_expires' => ( time() + $result['expires_in'] ), | |
| 51 | + ) | |
| 52 | + ); | |
| 53 | + | |
| 54 | +} | |
| 55 | + | |
| 56 | +// Register action to run above function; this action is created by WordPress' wp_schedule_event() function | |
| 57 | +// in update_credentials() in the ConvertKit_Settings class. | |
| 58 | +add_action( 'convertkit_refresh_token', 'convertkit_refresh_token' ); | |
| 59 | + | |
| 60 | +/** | |
| 10 | 61 | * Refresh the Posts Resource cache, triggered by WordPress' Cron. |
| 11 | 62 | * |
| 12 | 63 | * @since 1.9.7.4 |
| 13 | 64 | */ |
| @@ -16,8 +67,17 @@ | ||
| 16 | 67 | // Get Settings and Log classes. |
| 17 | 68 | $settings = new ConvertKit_Settings(); |
| 18 | 69 | $log = new ConvertKit_Log( CONVERTKIT_PLUGIN_PATH ); |
| 19 | 70 | |
| 71 | + // Bail if no access and refresh token exists. | |
| 72 | + if ( ! $settings->has_access_and_refresh_token() ) { | |
| 73 | + if ( $settings->debug_enabled() ) { | |
| 74 | + $log->add( 'CRON: convertkit_resource_refresh_posts(): No access and refresh token exists.' ); | |
| 75 | + } | |
| 76 | + | |
| 77 | + return; | |
| 78 | + } | |
| 79 | + | |
| 20 | 80 | // If debug logging is enabled, write to it now. |
| 21 | 81 | if ( $settings->debug_enabled() ) { |
| 22 | 82 | $log->add( 'CRON: convertkit_resource_refresh_posts(): Started' ); |
| 23 | 83 | } |
| @@ -25,18 +85,20 @@ | ||
| 25 | 85 | // Refresh Posts Resource. |
| 26 | 86 | $posts = new ConvertKit_Resource_Posts( 'cron' ); |
| 27 | 87 | $result = $posts->refresh(); |
| 28 | 88 | |
| 89 | + // Bail if an error occurred. | |
| 90 | + if ( is_wp_error( $result ) ) { | |
| 91 | + // Delete credentials if the error is a 401. | |
| 92 | + convertkit_maybe_delete_credentials( $result, CONVERTKIT_OAUTH_CLIENT_ID ); | |
| 93 | + | |
| 94 | + $log->add( 'CRON: convertkit_resource_refresh_posts(): Error: ' . $result->get_error_message() ); | |
| 95 | + return; | |
| 96 | + } | |
| 97 | + | |
| 29 | 98 | // If debug logging is enabled, write to it now. |
| 30 | 99 | if ( $settings->debug_enabled() ) { |
| 31 | - // If an error occured, log it. | |
| 32 | - if ( is_wp_error( $result ) ) { | |
| 33 | - $log->add( 'CRON: convertkit_resource_refresh_posts(): Error: ' . $result->get_error_message() ); | |
| 34 | - } | |
| 35 | - if ( is_array( $result ) ) { | |
| 36 | - $log->add( 'CRON: convertkit_resource_refresh_posts(): Success: ' . count( $result ) . ' broadcasts fetched from API and cached.' ); | |
| 37 | - } | |
| 38 | - | |
| 100 | + $log->add( 'CRON: convertkit_resource_refresh_posts(): Success: ' . count( $result ) . ' broadcasts fetched from API and cached.' ); | |
| 39 | 101 | $log->add( 'CRON: convertkit_resource_refresh_posts(): Finished' ); |
| 40 | 102 | } |
| 41 | 103 | |
| 42 | 104 | } |