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.


Built from https://develop.svn.wordpress.org/trunk@44483


git-svn-id: http://core.svn.wordpress.org/trunk@44314 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Peter Wilson 2019-01-08 21:49:49 +00:00
parent cd7b916d55
commit bf3c97de77
3 changed files with 57 additions and 14 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
//

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.1-alpha-44482';
$wp_version = '5.1-alpha-44483';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.