Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome: * `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure, * `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear, * `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear, * `spawn_cron()`: boolean indicating whether job spawned, * `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned, * `_set_cron_array()`: boolean outcome of `update_option()`. Props evansolomon, jrf, peterwilsoncc, pento for code review. Fixes #21072. git-svn-id: https://develop.svn.wordpress.org/trunk@43050 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
parent
4c36079299
commit
e73af26e92
|
@ -21,12 +21,14 @@
|
||||||
* Use wp_schedule_event() to schedule a recurring event.
|
* Use wp_schedule_event() to schedule a recurring event.
|
||||||
*
|
*
|
||||||
* @since 2.1.0
|
* @since 2.1.0
|
||||||
|
* @since 5.0.0 Return value modified to boolean indicating success or failure.
|
||||||
|
*
|
||||||
* @link https://codex.wordpress.org/Function_Reference/wp_schedule_single_event
|
* @link https://codex.wordpress.org/Function_Reference/wp_schedule_single_event
|
||||||
*
|
*
|
||||||
* @param int $timestamp Unix timestamp (UTC) for when to next run the event.
|
* @param int $timestamp Unix timestamp (UTC) for when to next run the event.
|
||||||
* @param string $hook Action hook to execute when the event is run.
|
* @param string $hook Action hook to execute when the event is run.
|
||||||
* @param array $args Optional. Array containing each separate argument to pass to the hook's callback function.
|
* @param array $args Optional. Array containing each separate argument to pass to the hook's callback function.
|
||||||
* @return false|void False if the event did not get scheduled.
|
* @return bool True if event successfully scheduled. False for failure.
|
||||||
*/
|
*/
|
||||||
function wp_schedule_single_event( $timestamp, $hook, $args = array() ) {
|
function wp_schedule_single_event( $timestamp, $hook, $args = array() ) {
|
||||||
// Make sure timestamp is a positive integer
|
// Make sure timestamp is a positive integer
|
||||||
|
@ -77,7 +79,7 @@ function wp_schedule_single_event( $timestamp, $hook, $args = array() ) {
|
||||||
'args' => $event->args,
|
'args' => $event->args,
|
||||||
);
|
);
|
||||||
uksort( $crons, 'strnatcasecmp' );
|
uksort( $crons, 'strnatcasecmp' );
|
||||||
_set_cron_array( $crons );
|
return _set_cron_array( $crons );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -99,13 +101,15 @@ function wp_schedule_single_event( $timestamp, $hook, $args = array() ) {
|
||||||
* Use wp_schedule_single_event() to schedule a non-recurring event.
|
* Use wp_schedule_single_event() to schedule a non-recurring event.
|
||||||
*
|
*
|
||||||
* @since 2.1.0
|
* @since 2.1.0
|
||||||
|
* @since 5.0.0 Return value modified to boolean indicating success or failure.
|
||||||
|
*
|
||||||
* @link https://codex.wordpress.org/Function_Reference/wp_schedule_event
|
* @link https://codex.wordpress.org/Function_Reference/wp_schedule_event
|
||||||
*
|
*
|
||||||
* @param int $timestamp Unix timestamp (UTC) for when to next run the event.
|
* @param int $timestamp Unix timestamp (UTC) for when to next run the event.
|
||||||
* @param string $recurrence How often the event should subsequently recur. See wp_get_schedules() for accepted values.
|
* @param string $recurrence How often the event should subsequently recur. See wp_get_schedules() for accepted values.
|
||||||
* @param string $hook Action hook to execute when the event is run.
|
* @param string $hook Action hook to execute when the event is run.
|
||||||
* @param array $args Optional. Array containing each separate argument to pass to the hook's callback function.
|
* @param array $args Optional. Array containing each separate argument to pass to the hook's callback function.
|
||||||
* @return false|void False if the event did not get scheduled.
|
* @return bool True if event successfully scheduled. False for failure.
|
||||||
*/
|
*/
|
||||||
function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array() ) {
|
function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array() ) {
|
||||||
// Make sure timestamp is a positive integer
|
// Make sure timestamp is a positive integer
|
||||||
|
@ -143,19 +147,20 @@ function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array() ) {
|
||||||
'interval' => $event->interval,
|
'interval' => $event->interval,
|
||||||
);
|
);
|
||||||
uksort( $crons, 'strnatcasecmp' );
|
uksort( $crons, 'strnatcasecmp' );
|
||||||
_set_cron_array( $crons );
|
return _set_cron_array( $crons );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Reschedules a recurring event.
|
* Reschedules a recurring event.
|
||||||
*
|
*
|
||||||
* @since 2.1.0
|
* @since 2.1.0
|
||||||
|
* @since 5.0.0 Return value modified to boolean indicating success or failure.
|
||||||
*
|
*
|
||||||
* @param int $timestamp Unix timestamp (UTC) for when to next run the event.
|
* @param int $timestamp Unix timestamp (UTC) for when to next run the event.
|
||||||
* @param string $recurrence How often the event should subsequently recur. See wp_get_schedules() for accepted values.
|
* @param string $recurrence How often the event should subsequently recur. See wp_get_schedules() for accepted values.
|
||||||
* @param string $hook Action hook to execute when the event is run.
|
* @param string $hook Action hook to execute when the event is run.
|
||||||
* @param array $args Optional. Array containing each separate argument to pass to the hook's callback function.
|
* @param array $args Optional. Array containing each separate argument to pass to the hook's callback function.
|
||||||
* @return false|void False if the event did not get rescheduled.
|
* @return bool True if event successfully rescheduled. False for failure.
|
||||||
*/
|
*/
|
||||||
function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array() ) {
|
function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array() ) {
|
||||||
// Make sure timestamp is a positive integer
|
// Make sure timestamp is a positive integer
|
||||||
|
@ -189,7 +194,7 @@ function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array() )
|
||||||
$timestamp = $now + ( $interval - ( ( $now - $timestamp ) % $interval ) );
|
$timestamp = $now + ( $interval - ( ( $now - $timestamp ) % $interval ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
wp_schedule_event( $timestamp, $recurrence, $hook, $args );
|
return wp_schedule_event( $timestamp, $recurrence, $hook, $args );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -199,13 +204,14 @@ function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array() )
|
||||||
* identified.
|
* identified.
|
||||||
*
|
*
|
||||||
* @since 2.1.0
|
* @since 2.1.0
|
||||||
|
* @since 5.0.0 Return value modified to boolean indicating success or failure.
|
||||||
*
|
*
|
||||||
* @param int $timestamp Unix timestamp (UTC) of the event.
|
* @param int $timestamp Unix timestamp (UTC) of the event.
|
||||||
* @param string $hook Action hook of the event.
|
* @param string $hook Action hook of the event.
|
||||||
* @param array $args Optional. Array containing each separate argument to pass to the hook's callback function.
|
* @param array $args Optional. Array containing each separate argument to pass to the hook's callback function.
|
||||||
* Although not passed to a callback, these arguments are used to uniquely identify the
|
* Although not passed to a callback, these arguments are used to uniquely identify the
|
||||||
* event, so they should be the same as those used when originally scheduling the event.
|
* event, so they should be the same as those used when originally scheduling the event.
|
||||||
* @return false|void False if the event did not get unscheduled.
|
* @return bool True if event successfully unscheduled. False for failure.
|
||||||
*/
|
*/
|
||||||
function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
|
function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
|
||||||
// Make sure timestamp is a positive integer
|
// Make sure timestamp is a positive integer
|
||||||
|
@ -222,16 +228,25 @@ function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
|
||||||
if ( empty( $crons[ $timestamp ] ) ) {
|
if ( empty( $crons[ $timestamp ] ) ) {
|
||||||
unset( $crons[ $timestamp ] );
|
unset( $crons[ $timestamp ] );
|
||||||
}
|
}
|
||||||
_set_cron_array( $crons );
|
return _set_cron_array( $crons );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Unschedules all events attached to the hook with the specified arguments.
|
* Unschedules all events attached to the hook with the specified arguments.
|
||||||
*
|
*
|
||||||
|
* Warning: This function may return Boolean FALSE, but may also return a non-Boolean
|
||||||
|
* value which evaluates to FALSE. For information about casting to booleans see the
|
||||||
|
* {@link https://php.net/manual/en/language.types.boolean.php PHP documentation}. Use
|
||||||
|
* the `===` operator for testing the return value of this function.
|
||||||
|
*
|
||||||
* @since 2.1.0
|
* @since 2.1.0
|
||||||
|
* @since 5.0.0 Return value modified to indicate success or failure.
|
||||||
*
|
*
|
||||||
* @param string $hook Action hook, the execution of which will be unscheduled.
|
* @param string $hook Action hook, the execution of which will be unscheduled.
|
||||||
* @param array $args Optional. Arguments that were to be passed to the hook's callback function.
|
* @param array $args Optional. Arguments that were to be passed to the hook's callback function.
|
||||||
|
* @return bool|int On success an integer indicating number of events unscheduled (0 indicates no
|
||||||
|
* events were registered with the hook and arguments combination), false if
|
||||||
|
* unscheduling one or more events fail.
|
||||||
*/
|
*/
|
||||||
function wp_clear_scheduled_hook( $hook, $args = array() ) {
|
function wp_clear_scheduled_hook( $hook, $args = array() ) {
|
||||||
// Backward compatibility
|
// Backward compatibility
|
||||||
|
@ -246,15 +261,20 @@ function wp_clear_scheduled_hook( $hook, $args = array() ) {
|
||||||
// and, wp_next_scheduled() returns the same schedule in an infinite loop.
|
// and, wp_next_scheduled() returns the same schedule in an infinite loop.
|
||||||
$crons = _get_cron_array();
|
$crons = _get_cron_array();
|
||||||
if ( empty( $crons ) ) {
|
if ( empty( $crons ) ) {
|
||||||
return;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
$key = md5( serialize( $args ) );
|
$results = array();
|
||||||
|
$key = md5( serialize( $args ) );
|
||||||
foreach ( $crons as $timestamp => $cron ) {
|
foreach ( $crons as $timestamp => $cron ) {
|
||||||
if ( isset( $cron[ $hook ][ $key ] ) ) {
|
if ( isset( $cron[ $hook ][ $key ] ) ) {
|
||||||
wp_unschedule_event( $timestamp, $hook, $args );
|
$results[] = wp_unschedule_event( $timestamp, $hook, $args );
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
if ( in_array( false, $results, true ) ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return count( $results );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -262,14 +282,29 @@ function wp_clear_scheduled_hook( $hook, $args = array() ) {
|
||||||
*
|
*
|
||||||
* Can be useful for plugins when deactivating to clean up the cron queue.
|
* Can be useful for plugins when deactivating to clean up the cron queue.
|
||||||
*
|
*
|
||||||
|
* Warning: This function may return Boolean FALSE, but may also return a non-Boolean
|
||||||
|
* value which evaluates to FALSE. For information about casting to booleans see the
|
||||||
|
* {@link https://php.net/manual/en/language.types.boolean.php PHP documentation}. Use
|
||||||
|
* the `===` operator for testing the return value of this function.
|
||||||
|
*
|
||||||
* @since 4.9.0
|
* @since 4.9.0
|
||||||
|
* @since 5.0.0 Return value added to indicate success or failure.
|
||||||
*
|
*
|
||||||
* @param string $hook Action hook, the execution of which will be unscheduled.
|
* @param string $hook Action hook, the execution of which will be unscheduled.
|
||||||
|
* @return bool|int On success an integer indicating number of events unscheduled (0 indicates no
|
||||||
|
* events were registered on the hook), false if unscheduling fails.
|
||||||
*/
|
*/
|
||||||
function wp_unschedule_hook( $hook ) {
|
function wp_unschedule_hook( $hook ) {
|
||||||
$crons = _get_cron_array();
|
$crons = _get_cron_array();
|
||||||
|
if ( empty( $crons ) ) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
|
||||||
|
$results = array();
|
||||||
foreach ( $crons as $timestamp => $args ) {
|
foreach ( $crons as $timestamp => $args ) {
|
||||||
|
if ( ! empty( $crons[ $timestamp ][ $hook ] ) ) {
|
||||||
|
$results[] = count( $crons[ $timestamp ][ $hook ] );
|
||||||
|
}
|
||||||
unset( $crons[ $timestamp ][ $hook ] );
|
unset( $crons[ $timestamp ][ $hook ] );
|
||||||
|
|
||||||
if ( empty( $crons[ $timestamp ] ) ) {
|
if ( empty( $crons[ $timestamp ] ) ) {
|
||||||
|
@ -277,7 +312,17 @@ function wp_unschedule_hook( $hook ) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
_set_cron_array( $crons );
|
/*
|
||||||
|
* If the results are empty (zero events to unschedule), no attempt
|
||||||
|
* to update the cron array is required.
|
||||||
|
*/
|
||||||
|
if ( empty( $results ) ) {
|
||||||
|
return 0;
|
||||||
|
}
|
||||||
|
if ( _set_cron_array( $crons ) ) {
|
||||||
|
return array_sum( $results );
|
||||||
|
}
|
||||||
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -309,8 +354,10 @@ function wp_next_scheduled( $hook, $args = array() ) {
|
||||||
* Sends a request to run cron through HTTP request that doesn't halt page loading.
|
* Sends a request to run cron through HTTP request that doesn't halt page loading.
|
||||||
*
|
*
|
||||||
* @since 2.1.0
|
* @since 2.1.0
|
||||||
|
* @since 5.0.0 Return values added.
|
||||||
*
|
*
|
||||||
* @param int $gmt_time Optional. Unix timestamp (UTC). Default 0 (current time is used).
|
* @param int $gmt_time Optional. Unix timestamp (UTC). Default 0 (current time is used).
|
||||||
|
* @return bool True if spawned, false if no events spawned.
|
||||||
*/
|
*/
|
||||||
function spawn_cron( $gmt_time = 0 ) {
|
function spawn_cron( $gmt_time = 0 ) {
|
||||||
if ( ! $gmt_time ) {
|
if ( ! $gmt_time ) {
|
||||||
|
@ -318,7 +365,7 @@ function spawn_cron( $gmt_time = 0 ) {
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( defined( 'DOING_CRON' ) || isset( $_GET['doing_wp_cron'] ) ) {
|
if ( defined( 'DOING_CRON' ) || isset( $_GET['doing_wp_cron'] ) ) {
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
/*
|
/*
|
||||||
|
@ -336,23 +383,23 @@ function spawn_cron( $gmt_time = 0 ) {
|
||||||
|
|
||||||
// don't run if another process is currently running it or more than once every 60 sec.
|
// don't run if another process is currently running it or more than once every 60 sec.
|
||||||
if ( $lock + WP_CRON_LOCK_TIMEOUT > $gmt_time ) {
|
if ( $lock + WP_CRON_LOCK_TIMEOUT > $gmt_time ) {
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
//sanity check
|
//sanity check
|
||||||
$crons = _get_cron_array();
|
$crons = _get_cron_array();
|
||||||
if ( ! is_array( $crons ) ) {
|
if ( ! is_array( $crons ) ) {
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$keys = array_keys( $crons );
|
$keys = array_keys( $crons );
|
||||||
if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) {
|
if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) {
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) {
|
if ( defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) {
|
||||||
if ( 'GET' !== $_SERVER['REQUEST_METHOD'] || defined( 'DOING_AJAX' ) || defined( 'XMLRPC_REQUEST' ) ) {
|
if ( 'GET' !== $_SERVER['REQUEST_METHOD'] || defined( 'DOING_AJAX' ) || defined( 'XMLRPC_REQUEST' ) ) {
|
||||||
return;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
$doing_wp_cron = sprintf( '%.22F', $gmt_time );
|
$doing_wp_cron = sprintf( '%.22F', $gmt_time );
|
||||||
|
@ -368,7 +415,7 @@ function spawn_cron( $gmt_time = 0 ) {
|
||||||
flush();
|
flush();
|
||||||
|
|
||||||
WP_DEBUG ? include_once( ABSPATH . 'wp-cron.php' ) : @include_once( ABSPATH . 'wp-cron.php' );
|
WP_DEBUG ? include_once( ABSPATH . 'wp-cron.php' ) : @include_once( ABSPATH . 'wp-cron.php' );
|
||||||
return;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Set the cron lock with the current unix timestamp, when the cron is being spawned.
|
// Set the cron lock with the current unix timestamp, when the cron is being spawned.
|
||||||
|
@ -409,31 +456,43 @@ function spawn_cron( $gmt_time = 0 ) {
|
||||||
), $doing_wp_cron
|
), $doing_wp_cron
|
||||||
);
|
);
|
||||||
|
|
||||||
wp_remote_post( $cron_request['url'], $cron_request['args'] );
|
$result = wp_remote_post( $cron_request['url'], $cron_request['args'] );
|
||||||
|
return ! is_wp_error( $result );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* Run scheduled callbacks or spawn cron for all scheduled events.
|
* Run scheduled callbacks or spawn cron for all scheduled events.
|
||||||
*
|
*
|
||||||
|
* Warning: This function may return Boolean FALSE, but may also return a non-Boolean
|
||||||
|
* value which evaluates to FALSE. For information about casting to booleans see the
|
||||||
|
* {@link https://php.net/manual/en/language.types.boolean.php PHP documentation}. Use
|
||||||
|
* the `===` operator for testing the return value of this function.
|
||||||
|
*
|
||||||
* @since 2.1.0
|
* @since 2.1.0
|
||||||
|
* @since 5.0.0 Return value added to indicate success or failure.
|
||||||
|
*
|
||||||
|
* @return bool|int On success an integer indicating number of events spawned (0 indicates no
|
||||||
|
* events needed to be spawned), false if spawning fails for one or more events.
|
||||||
*/
|
*/
|
||||||
function wp_cron() {
|
function wp_cron() {
|
||||||
// Prevent infinite loops caused by lack of wp-cron.php
|
// Prevent infinite loops caused by lack of wp-cron.php
|
||||||
if ( strpos( $_SERVER['REQUEST_URI'], '/wp-cron.php' ) !== false || ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) ) {
|
if ( strpos( $_SERVER['REQUEST_URI'], '/wp-cron.php' ) !== false || ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) ) {
|
||||||
return;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
if ( false === $crons = _get_cron_array() ) {
|
$crons = _get_cron_array();
|
||||||
return;
|
if ( false === $crons ) {
|
||||||
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
$gmt_time = microtime( true );
|
$gmt_time = microtime( true );
|
||||||
$keys = array_keys( $crons );
|
$keys = array_keys( $crons );
|
||||||
if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) {
|
if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) {
|
||||||
return;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
$schedules = wp_get_schedules();
|
$schedules = wp_get_schedules();
|
||||||
|
$results = array();
|
||||||
foreach ( $crons as $timestamp => $cronhooks ) {
|
foreach ( $crons as $timestamp => $cronhooks ) {
|
||||||
if ( $timestamp > $gmt_time ) {
|
if ( $timestamp > $gmt_time ) {
|
||||||
break;
|
break;
|
||||||
|
@ -442,10 +501,15 @@ function wp_cron() {
|
||||||
if ( isset( $schedules[ $hook ]['callback'] ) && ! call_user_func( $schedules[ $hook ]['callback'] ) ) {
|
if ( isset( $schedules[ $hook ]['callback'] ) && ! call_user_func( $schedules[ $hook ]['callback'] ) ) {
|
||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
spawn_cron( $gmt_time );
|
$results[] = spawn_cron( $gmt_time );
|
||||||
break 2;
|
break 2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if ( in_array( false, $results, true ) ) {
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
return count( $results );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
@ -558,13 +622,16 @@ function _get_cron_array() {
|
||||||
* Updates the CRON option with the new CRON array.
|
* Updates the CRON option with the new CRON array.
|
||||||
*
|
*
|
||||||
* @since 2.1.0
|
* @since 2.1.0
|
||||||
|
* @since 5.0.0 Return value modified to outcome of {@see update_option}.
|
||||||
|
*
|
||||||
* @access private
|
* @access private
|
||||||
*
|
*
|
||||||
* @param array $cron Cron info array from _get_cron_array().
|
* @param array $cron Cron info array from _get_cron_array().
|
||||||
|
* @return bool True if cron array updated, false on failure.
|
||||||
*/
|
*/
|
||||||
function _set_cron_array( $cron ) {
|
function _set_cron_array( $cron ) {
|
||||||
$cron['version'] = 2;
|
$cron['version'] = 2;
|
||||||
update_option( 'cron', $cron );
|
return update_option( 'cron', $cron );
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
@ -29,7 +29,8 @@ class Tests_Cron extends WP_UnitTestCase {
|
||||||
$hook = __FUNCTION__;
|
$hook = __FUNCTION__;
|
||||||
$timestamp = strtotime( '+1 hour' );
|
$timestamp = strtotime( '+1 hour' );
|
||||||
|
|
||||||
wp_schedule_single_event( $timestamp, $hook );
|
$scheduled = wp_schedule_single_event( $timestamp, $hook );
|
||||||
|
$this->assertTrue( $scheduled );
|
||||||
$this->assertEquals( $timestamp, wp_next_scheduled( $hook ) );
|
$this->assertEquals( $timestamp, wp_next_scheduled( $hook ) );
|
||||||
|
|
||||||
// it's a non recurring event
|
// it's a non recurring event
|
||||||
|
@ -43,7 +44,8 @@ class Tests_Cron extends WP_UnitTestCase {
|
||||||
$timestamp = strtotime( '+1 hour' );
|
$timestamp = strtotime( '+1 hour' );
|
||||||
$args = array( 'foo' );
|
$args = array( 'foo' );
|
||||||
|
|
||||||
wp_schedule_single_event( $timestamp, $hook, $args );
|
$scheduled = wp_schedule_single_event( $timestamp, $hook, $args );
|
||||||
|
$this->assertTrue( $scheduled );
|
||||||
// this returns the timestamp only if we provide matching args
|
// this returns the timestamp only if we provide matching args
|
||||||
$this->assertEquals( $timestamp, wp_next_scheduled( $hook, $args ) );
|
$this->assertEquals( $timestamp, wp_next_scheduled( $hook, $args ) );
|
||||||
// these don't match so return nothing
|
// these don't match so return nothing
|
||||||
|
@ -60,7 +62,8 @@ class Tests_Cron extends WP_UnitTestCase {
|
||||||
$recur = 'hourly';
|
$recur = 'hourly';
|
||||||
$timestamp = strtotime( '+1 hour' );
|
$timestamp = strtotime( '+1 hour' );
|
||||||
|
|
||||||
wp_schedule_event( $timestamp, $recur, $hook );
|
$scheduled = wp_schedule_event( $timestamp, $recur, $hook );
|
||||||
|
$this->assertTrue( $scheduled );
|
||||||
// it's scheduled for the right time
|
// it's scheduled for the right time
|
||||||
$this->assertEquals( $timestamp, wp_next_scheduled( $hook ) );
|
$this->assertEquals( $timestamp, wp_next_scheduled( $hook ) );
|
||||||
// it's a recurring event
|
// it's a recurring event
|
||||||
|
@ -74,7 +77,8 @@ class Tests_Cron extends WP_UnitTestCase {
|
||||||
$recur = 'hourly';
|
$recur = 'hourly';
|
||||||
$args = array( 'foo' );
|
$args = array( 'foo' );
|
||||||
|
|
||||||
wp_schedule_event( $timestamp, 'hourly', $hook, $args );
|
$scheduled = wp_schedule_event( $timestamp, 'hourly', $hook, $args );
|
||||||
|
$this->assertTrue( $scheduled );
|
||||||
// this returns the timestamp only if we provide matching args
|
// this returns the timestamp only if we provide matching args
|
||||||
$this->assertEquals( $timestamp, wp_next_scheduled( $hook, $args ) );
|
$this->assertEquals( $timestamp, wp_next_scheduled( $hook, $args ) );
|
||||||
// these don't match so return nothing
|
// these don't match so return nothing
|
||||||
|
@ -94,7 +98,8 @@ class Tests_Cron extends WP_UnitTestCase {
|
||||||
$this->assertEquals( $timestamp, wp_next_scheduled( $hook ) );
|
$this->assertEquals( $timestamp, wp_next_scheduled( $hook ) );
|
||||||
|
|
||||||
// now unschedule it and make sure it's gone
|
// now unschedule it and make sure it's gone
|
||||||
wp_unschedule_event( $timestamp, $hook );
|
$unscheduled = wp_unschedule_event( $timestamp, $hook );
|
||||||
|
$this->assertTrue( $unscheduled );
|
||||||
$this->assertEquals( false, wp_next_scheduled( $hook ) );
|
$this->assertEquals( false, wp_next_scheduled( $hook ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -113,7 +118,8 @@ class Tests_Cron extends WP_UnitTestCase {
|
||||||
$this->assertTrue( wp_next_scheduled( $hook, $args ) > 0 );
|
$this->assertTrue( wp_next_scheduled( $hook, $args ) > 0 );
|
||||||
|
|
||||||
// clear the schedule for the no args events and make sure it's gone
|
// clear the schedule for the no args events and make sure it's gone
|
||||||
wp_clear_scheduled_hook( $hook );
|
$hook_unscheduled = wp_clear_scheduled_hook( $hook );
|
||||||
|
$this->assertSame( 2, $hook_unscheduled );
|
||||||
$this->assertFalse( wp_next_scheduled( $hook ) );
|
$this->assertFalse( wp_next_scheduled( $hook ) );
|
||||||
// the args events should still be there
|
// the args events should still be there
|
||||||
$this->assertTrue( wp_next_scheduled( $hook, $args ) > 0 );
|
$this->assertTrue( wp_next_scheduled( $hook, $args ) > 0 );
|
||||||
|
@ -124,6 +130,18 @@ class Tests_Cron extends WP_UnitTestCase {
|
||||||
$this->assertFalse( wp_next_scheduled( $hook, $args ) );
|
$this->assertFalse( wp_next_scheduled( $hook, $args ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function test_clear_undefined_schedule() {
|
||||||
|
$hook = __FUNCTION__;
|
||||||
|
$args = array( 'arg1' );
|
||||||
|
|
||||||
|
wp_schedule_single_event( strtotime( '+1 hour' ), $hook, $args );
|
||||||
|
wp_schedule_single_event( strtotime( '+2 hour' ), $hook, $args );
|
||||||
|
|
||||||
|
// clear the schedule for no args events and ensure no events are cleared.
|
||||||
|
$hook_unscheduled = wp_clear_scheduled_hook( $hook );
|
||||||
|
$this->assertSame( 0, $hook_unscheduled );
|
||||||
|
}
|
||||||
|
|
||||||
function test_clear_schedule_multiple_args() {
|
function test_clear_schedule_multiple_args() {
|
||||||
$hook = __FUNCTION__;
|
$hook = __FUNCTION__;
|
||||||
$args = array( 'arg1', 'arg2' );
|
$args = array( 'arg1', 'arg2' );
|
||||||
|
@ -206,7 +224,26 @@ class Tests_Cron extends WP_UnitTestCase {
|
||||||
$this->assertTrue( wp_next_scheduled( $hook, $args ) > 0 );
|
$this->assertTrue( wp_next_scheduled( $hook, $args ) > 0 );
|
||||||
|
|
||||||
// clear the schedule and make sure it's gone.
|
// clear the schedule and make sure it's gone.
|
||||||
wp_unschedule_hook( $hook );
|
$unschedule_hook = wp_unschedule_hook( $hook );
|
||||||
|
$this->assertSame( 4, $unschedule_hook );
|
||||||
|
$this->assertFalse( wp_next_scheduled( $hook ) );
|
||||||
|
}
|
||||||
|
|
||||||
|
function test_unschedule_undefined_hook() {
|
||||||
|
$hook = __FUNCTION__;
|
||||||
|
$unrelated_hook = __FUNCTION__ . '_two';
|
||||||
|
|
||||||
|
// Attempt to clear schedule on non-existant hook.
|
||||||
|
$unschedule_hook = wp_unschedule_hook( $hook );
|
||||||
|
$this->assertSame( 0, $unschedule_hook );
|
||||||
|
$this->assertFalse( wp_next_scheduled( $hook ) );
|
||||||
|
|
||||||
|
// Repeat tests with populated cron array.
|
||||||
|
wp_schedule_single_event( strtotime( '+1 hour' ), $unrelated_hook );
|
||||||
|
wp_schedule_single_event( strtotime( '+2 hour' ), $unrelated_hook );
|
||||||
|
|
||||||
|
$unschedule_hook = wp_unschedule_hook( $hook );
|
||||||
|
$this->assertSame( 0, $unschedule_hook );
|
||||||
$this->assertFalse( wp_next_scheduled( $hook ) );
|
$this->assertFalse( wp_next_scheduled( $hook ) );
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -221,9 +258,9 @@ class Tests_Cron extends WP_UnitTestCase {
|
||||||
$ts2 = strtotime( '+3 minutes' );
|
$ts2 = strtotime( '+3 minutes' );
|
||||||
|
|
||||||
// first one works
|
// first one works
|
||||||
wp_schedule_single_event( $ts1, $hook, $args );
|
$this->assertTrue( wp_schedule_single_event( $ts1, $hook, $args ) );
|
||||||
// second one is ignored
|
// second one is ignored
|
||||||
wp_schedule_single_event( $ts2, $hook, $args );
|
$this->assertFalse( wp_schedule_single_event( $ts2, $hook, $args ) );
|
||||||
|
|
||||||
// the next event should be at +5 minutes, not +3
|
// the next event should be at +5 minutes, not +3
|
||||||
$this->assertEquals( $ts1, wp_next_scheduled( $hook, $args ) );
|
$this->assertEquals( $ts1, wp_next_scheduled( $hook, $args ) );
|
||||||
|
@ -240,9 +277,9 @@ class Tests_Cron extends WP_UnitTestCase {
|
||||||
$ts2 = strtotime( '+3 minutes' );
|
$ts2 = strtotime( '+3 minutes' );
|
||||||
|
|
||||||
// first one works
|
// first one works
|
||||||
wp_schedule_single_event( $ts1, $hook, $args );
|
$this->assertTrue( wp_schedule_single_event( $ts1, $hook, $args ) );
|
||||||
// second works too
|
// second works too
|
||||||
wp_schedule_single_event( $ts2, $hook, $args );
|
$this->assertTrue( wp_schedule_single_event( $ts2, $hook, $args ) );
|
||||||
|
|
||||||
// the next event should be at +3 minutes, even though that one was scheduled second
|
// the next event should be at +3 minutes, even though that one was scheduled second
|
||||||
$this->assertEquals( $ts2, wp_next_scheduled( $hook, $args ) );
|
$this->assertEquals( $ts2, wp_next_scheduled( $hook, $args ) );
|
||||||
|
@ -259,9 +296,9 @@ class Tests_Cron extends WP_UnitTestCase {
|
||||||
$ts2 = strtotime( '+30 minutes' );
|
$ts2 = strtotime( '+30 minutes' );
|
||||||
|
|
||||||
// first one works
|
// first one works
|
||||||
wp_schedule_single_event( $ts1, $hook, $args );
|
$this->assertTrue( wp_schedule_single_event( $ts1, $hook, $args ) );
|
||||||
// second works too
|
// second works too
|
||||||
wp_schedule_single_event( $ts2, $hook, $args );
|
$this->assertTrue( wp_schedule_single_event( $ts2, $hook, $args ) );
|
||||||
|
|
||||||
// the next event should be at +3 minutes
|
// the next event should be at +3 minutes
|
||||||
$this->assertEquals( $ts1, wp_next_scheduled( $hook, $args ) );
|
$this->assertEquals( $ts1, wp_next_scheduled( $hook, $args ) );
|
||||||
|
|
Loading…
Reference in New Issue