| 1 |
<?php |
| 2 |
/** |
| 3 |
* ConvertKit WordPress Cron functions. |
| 4 |
* |
| 5 |
* @package ConvertKit |
| 6 |
* @author ConvertKit |
| 7 |
*/ |
| 8 |
|
| 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 occured, 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 |
* Refresh the Posts Resource cache, triggered by WordPress' Cron. |
| 62 |
* |
| 63 |
* @since 1.9.7.4 |
| 64 |
*/ |
| 65 |
function convertkit_resource_refresh_posts() { |
| 66 |
|
| 67 |
// Get Settings and Log classes. |
| 68 |
$settings = new ConvertKit_Settings(); |
| 69 |
$log = new ConvertKit_Log( CONVERTKIT_PLUGIN_PATH ); |
| 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 |
|
| 80 |
// If debug logging is enabled, write to it now. |
| 81 |
if ( $settings->debug_enabled() ) { |
| 82 |
$log->add( 'CRON: convertkit_resource_refresh_posts(): Started' ); |
| 83 |
} |
| 84 |
|
| 85 |
// Refresh Posts Resource. |
| 86 |
$posts = new ConvertKit_Resource_Posts( 'cron' ); |
| 87 |
$result = $posts->refresh(); |
| 88 |
|
| 89 |
// Bail if an error occured. |
| 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 |
// If debug logging is enabled, write to it now. |
| 99 |
if ( $settings->debug_enabled() ) { |
| 100 |
$log->add( 'CRON: convertkit_resource_refresh_posts(): Success: ' . count( $result ) . ' broadcasts fetched from API and cached.' ); |
| 101 |
$log->add( 'CRON: convertkit_resource_refresh_posts(): Finished' ); |
| 102 |
} |
| 103 |
|
| 104 |
} |
| 105 |
|
| 106 |
// Register action to run above function; this action is created by WordPress' wp_schedule_event() function |
| 107 |
// in the ConvertKit_Resource_Posts class. |
| 108 |
add_action( 'convertkit_resource_refresh_posts', 'convertkit_resource_refresh_posts' ); |
| 109 |
|