| @@ -6,59 +6,8 @@ | ||
| 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 | -/** | |
| 61 | 10 | * Refresh the Posts Resource cache, triggered by WordPress' Cron. |
| 62 | 11 | * |
| 63 | 12 | * @since 1.9.7.4 |
| 64 | 13 | */ |
| @@ -67,17 +16,8 @@ | ||
| 67 | 16 | // Get Settings and Log classes. |
| 68 | 17 | $settings = new ConvertKit_Settings(); |
| 69 | 18 | $log = new ConvertKit_Log( CONVERTKIT_PLUGIN_PATH ); |
| 70 | 19 | |
| 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 | - | |
| 80 | 20 | // If debug logging is enabled, write to it now. |
| 81 | 21 | if ( $settings->debug_enabled() ) { |
| 82 | 22 | $log->add( 'CRON: convertkit_resource_refresh_posts(): Started' ); |
| 83 | 23 | } |
| @@ -85,20 +25,18 @@ | ||
| 85 | 25 | // Refresh Posts Resource. |
| 86 | 26 | $posts = new ConvertKit_Resource_Posts( 'cron' ); |
| 87 | 27 | $result = $posts->refresh(); |
| 88 | 28 | |
| 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 | - | |
| 98 | 29 | // If debug logging is enabled, write to it now. |
| 99 | 30 | if ( $settings->debug_enabled() ) { |
| 100 | - $log->add( 'CRON: convertkit_resource_refresh_posts(): Success: ' . count( $result ) . ' broadcasts fetched from API and cached.' ); | |
| 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 | + | |
| 101 | 39 | $log->add( 'CRON: convertkit_resource_refresh_posts(): Finished' ); |
| 102 | 40 | } |
| 103 | 41 | |
| 104 | 42 | } |