Cron API: Add function and filter to return ready cron jobs.

Add the function `wp_get_ready_cron_jobs()` to return a modified version of the cron array limited to jobs ready to be run, ie with a timestamp of `time()` or earlier.

The new function includes the filter `pre_get_ready_cron_jobs` to allow for custom cron storage systems. This rounds out the functionality added in #32656.

Props Pento for code review.
Fixes #45797.



git-svn-id: https://develop.svn.wordpress.org/trunk@44483 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Peter Wilson 2019-01-08 21:48:07 +00:00
parent 5778bffd42
commit 0bc20594c7
2 changed files with 56 additions and 13 deletions

View File

@ -66,18 +66,11 @@ function _get_cron_lock() {
return $value;
}
if ( false === $crons = _get_cron_array() ) {
$crons = wp_get_ready_cron_jobs();
if ( empty( $crons ) ) {
die();
}
$keys = array_keys( $crons );
$gmt_time = microtime( true );
if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) {
die();
}
// The cron lock: a unix timestamp from when the cron was spawned.
$doing_cron_transient = get_transient( 'doing_cron' );

View File

@ -641,8 +641,8 @@ function spawn_cron( $gmt_time = 0 ) {
}
//sanity check
$crons = _get_cron_array();
if ( ! is_array( $crons ) ) {
$crons = wp_get_ready_cron_jobs();
if ( empty( $crons ) ) {
return false;
}
@ -736,8 +736,8 @@ function wp_cron() {
return 0;
}
$crons = _get_cron_array();
if ( false === $crons ) {
$crons = wp_get_ready_cron_jobs();
if ( empty( $crons ) ) {
return 0;
}
@ -854,6 +854,56 @@ function wp_get_schedule( $hook, $args = array() ) {
return apply_filters( 'get_schedule', $schedule, $hook, $args );
}
/**
* Retrieve cron jobs ready to be run.
*
* Returns the results of _get_cron_array() limited to events ready to be run,
* ie, with a timestamp in the past.
*
* @since 5.1.0
*
* @return array Cron jobs ready to be run.
*/
function wp_get_ready_cron_jobs() {
/**
* Filter to preflight or hijack retrieving ready cron jobs.
*
* Returning an array will short-circuit the normal retrieval of ready
* cron jobs, causing the function to return the filtered value instead.
*
* @since 5.1.0
*
* @param null|array $pre Array of ready cron tasks to return instead. Default null
* to continue using results from _get_cron_array().
*/
$pre = apply_filters( 'pre_get_ready_cron_jobs', null );
if ( null !== $pre ) {
return $pre;
}
$crons = _get_cron_array();
if ( false === $crons ) {
return array();
}
$gmt_time = microtime( true );
$keys = array_keys( $crons );
if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) {
return array();
}
$results = array();
foreach ( $crons as $timestamp => $cronhooks ) {
if ( $timestamp > $gmt_time ) {
break;
}
$results[ $timestamp ] = $cronhooks;
}
return $results;
}
//
// Private functions
//