WordPress.org

Make WordPress Core

Changeset 44483


Ignore:
Timestamp:
01/08/2019 09:48:07 PM (5 months ago)
Author:
peterwilsoncc
Message:

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.

Location:
trunk/src
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-cron.php

    r42343 r44483  
    6767}
    6868
    69 if ( false === $crons = _get_cron_array() ) {
     69$crons = wp_get_ready_cron_jobs();
     70if ( empty( $crons ) ) {
    7071    die();
    7172}
    72 
    73 $keys     = array_keys( $crons );
    74 $gmt_time = microtime( true );
    75 
    76 if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) {
    77     die();
    78 }
    79 
    8073
    8174// The cron lock: a unix timestamp from when the cron was spawned.
  • trunk/src/wp-includes/cron.php

    r44375 r44483  
    642642
    643643    //sanity check
    644     $crons = _get_cron_array();
    645     if ( ! is_array( $crons ) ) {
     644    $crons = wp_get_ready_cron_jobs();
     645    if ( empty( $crons ) ) {
    646646        return false;
    647647    }
     
    737737    }
    738738
    739     $crons = _get_cron_array();
    740     if ( false === $crons ) {
     739    $crons = wp_get_ready_cron_jobs();
     740    if ( empty( $crons ) ) {
    741741        return 0;
    742742    }
     
    855855}
    856856
     857/**
     858 * Retrieve cron jobs ready to be run.
     859 *
     860 * Returns the results of _get_cron_array() limited to events ready to be run,
     861 * ie, with a timestamp in the past.
     862 *
     863 * @since 5.1.0
     864 *
     865 * @return array Cron jobs ready to be run.
     866 */
     867function wp_get_ready_cron_jobs() {
     868    /**
     869     * Filter to preflight or hijack retrieving ready cron jobs.
     870     *
     871     * Returning an array will short-circuit the normal retrieval of ready
     872     * cron jobs, causing the function to return the filtered value instead.
     873     *
     874     * @since 5.1.0
     875     *
     876     * @param null|array $pre Array of ready cron tasks to return instead. Default null
     877     *                        to continue using results from _get_cron_array().
     878     */
     879    $pre = apply_filters( 'pre_get_ready_cron_jobs', null );
     880    if ( null !== $pre ) {
     881        return $pre;
     882    }
     883
     884    $crons = _get_cron_array();
     885
     886    if ( false === $crons ) {
     887        return array();
     888    }
     889
     890    $gmt_time = microtime( true );
     891    $keys     = array_keys( $crons );
     892    if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) {
     893        return array();
     894    }
     895
     896    $results = array();
     897    foreach ( $crons as $timestamp => $cronhooks ) {
     898        if ( $timestamp > $gmt_time ) {
     899            break;
     900        }
     901        $results[ $timestamp ] = $cronhooks;
     902    }
     903
     904    return $results;
     905}
     906
    857907//
    858908// Private functions
Note: See TracChangeset for help on using the changeset viewer.