From 0bc20594c78369a6778635cd58f2b90c57428d6d Mon Sep 17 00:00:00 2001 From: Peter Wilson Date: Tue, 8 Jan 2019 21:48:07 +0000 Subject: [PATCH] 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 --- src/wp-cron.php | 11 ++------ src/wp-includes/cron.php | 58 +++++++++++++++++++++++++++++++++++++--- 2 files changed, 56 insertions(+), 13 deletions(-) diff --git a/src/wp-cron.php b/src/wp-cron.php index acf5ded2d4..d704ceaf84 100644 --- a/src/wp-cron.php +++ b/src/wp-cron.php @@ -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' ); diff --git a/src/wp-includes/cron.php b/src/wp-includes/cron.php index 046c0dca73..c6bb637092 100644 --- a/src/wp-includes/cron.php +++ b/src/wp-includes/cron.php @@ -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 //