WordPress.org

Make WordPress Core

Ticket #32656: 32656.diff

File 32656.diff, 8.2 KB (added by rmccue, 4 years ago)
  • wp-includes/cron.php

    diff --git a/wp-includes/cron.php b/wp-includes/cron.php
    index 8b88c12..305d2bb 100644
    a b  
    2020 * @param array $args Optional. Arguments to pass to the hook's callback function.
    2121 * @return void|false
    2222 */
    23 function wp_schedule_single_event( $timestamp, $hook, $args = array()) {
     23function wp_schedule_single_event( $timestamp, $hook, $args = array() ) {
     24        $event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => false, 'args' => $args );
     25
     26        /**
     27         * Filter to preflight or hijack scheduling an event.
     28         *
     29         * Passing a non-null value will short-circuit adding the event to the cron
     30         * array, returning the passed value instead.
     31         *
     32         * Both single events and recurring events are passed through this filter;
     33         * single events have `$event->schedule` as false, whereas recurring events
     34         * have this set to a recurrence from {@see wp_get_schedules}. Recurring
     35         * events also have the integer recurrence interval set as `$event->interval`
     36         *
     37         * @param null|mixed $pre Value to return instead. Default null to continue adding the event.
     38         * @param object $event An object containing the event's data.
     39         */
     40        $pre = apply_filters( 'pre_schedule_event', null, $event );
     41        if ( null !== $pre ) {
     42                return $pre;
     43        }
     44
    2445        // don't schedule a duplicate if there's already an identical event due within 10 minutes of it
    2546        $next = wp_next_scheduled($hook, $args);
    2647        if ( $next && abs( $next - $timestamp ) <= 10 * MINUTE_IN_SECONDS ) {
    2748                return;
    2849        }
    2950
    30         $crons = _get_cron_array();
    31         $event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => false, 'args' => $args );
    3251        /**
    3352         * Filter a single event before it is scheduled.
    3453         *
    function wp_schedule_single_event( $timestamp, $hook, $args = array()) { 
    4463
    4564        $key = md5(serialize($event->args));
    4665
     66        $crons = _get_cron_array();
    4767        $crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args );
    4868        uksort( $crons, "strnatcasecmp" );
    4969        _set_cron_array( $crons );
    function wp_schedule_single_event( $timestamp, $hook, $args = array()) { 
    7090 * @return false|void False when does not schedule event.
    7191 */
    7292function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array()) {
    73         $crons = _get_cron_array();
    7493        $schedules = wp_get_schedules();
    7594
    7695        if ( !isset( $schedules[$recurrence] ) )
    7796                return false;
    7897
    7998        $event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => $recurrence, 'args' => $args, 'interval' => $schedules[$recurrence]['interval'] );
     99
     100        /** This filter is documented in wp-includes/cron.php */
     101        $pre = apply_filters( 'pre_schedule_event', null, $event );
     102        if ( null !== $pre ) {
     103                return $pre;
     104        }
     105
    80106        /** This filter is documented in wp-includes/cron.php */
    81107        $event = apply_filters( 'schedule_event', $event );
    82108
    function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array()) { 
    86112
    87113        $key = md5(serialize($event->args));
    88114
     115        $crons = _get_cron_array();
    89116        $crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args, 'interval' => $event->interval );
    90117        uksort( $crons, "strnatcasecmp" );
    91118        _set_cron_array( $crons );
    function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array()) { 
    103130 * @return false|void False when does not schedule event.
    104131 */
    105132function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array() ) {
     133        /**
     134         * Filter to preflight or hijack rescheduling of events.
     135         *
     136         * Passing a non-null value will short-circuit the normal rescheduling
     137         * process, returning the passed value instead.
     138         *
     139         * Note that `$recurrence` is the original stored value. This is not
     140         * guaranteed to exist when the event is rescheduled, so plugins should
     141         * store the interval value and fall back to this if needed.
     142         *
     143         * @param null|mixed $pre Value to return instead. Default null to continue rescheduling the event.
     144         * @param int $timestamp Timestamp for when the event originally ran.
     145         * @param string $recurrence How often the event should recur.
     146         * @param string $hook Action hook to execute when cron is run.
     147         * @param array $args Optional. Arguments to pass to the hook's callback function.
     148         */
     149        $pre = apply_filters( 'pre_reschedule_event', null, $timestamp, $recurrence, $hook, $args );
     150        if ( $pre !== null ) {
     151                return $pre;
     152        }
     153
    106154        $crons = _get_cron_array();
    107155        $schedules = wp_get_schedules();
    108156        $key = md5( serialize( $args ) );
    function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array() ) 
    148196 * as those used when originally scheduling the event.
    149197 */
    150198function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
     199        /**
     200         * Filter to preflight or hijack unscheduling of events.
     201         *
     202         * Passing a non-null value will short-circuit the normal unscheduling
     203         * process, returning the passed value instead.
     204         *
     205         * @param null|mixed $pre Value to return instead. Default null to continue unscheduling the event.
     206         * @param int $timestamp Timestamp for when to run the event.
     207         * @param string $hook Action hook, the execution of which will be unscheduled.
     208         * @param array $args Arguments to pass to the hook's callback function.
     209         */
     210        $pre = apply_filters( 'pre_unschedule_event', null, $timestamp, $hook, $args );
     211        if ( $pre !== null ) {
     212                return $pre;
     213        }
     214
    151215        $crons = _get_cron_array();
    152216        $key = md5(serialize($args));
    153217        unset( $crons[$timestamp][$hook][$key] );
    function wp_clear_scheduled_hook( $hook, $args = array() ) { 
    174238                $args = array_slice( func_get_args(), 1 );
    175239        }
    176240
     241        /**
     242         * Filter to preflight or hijack clearing a scheduled hook.
     243         *
     244         * Passing a non-null value will short-circuit the normal unscheduling
     245         * process, returning the passed value instead.
     246         *
     247         * @param null|mixed $pre Value to return instead. Default null to continue unscheduling the event.
     248         * @param string $hook Action hook, the execution of which will be unscheduled.
     249         * @param array $args Arguments to pass to the hook's callback function.
     250         */
     251        $pre = apply_filters( 'pre_clear_scheduled_hook', null, $hook, $args );
     252        if ( $pre !== null ) {
     253                return $pre;
     254        }
     255
    177256        // This logic duplicates wp_next_scheduled()
    178257        // It's required due to a scenario where wp_unschedule_event() fails due to update_option() failing,
    179258        // and, wp_next_scheduled() returns the same schedule in an infinite loop.
    function wp_clear_scheduled_hook( $hook, $args = array() ) { 
    201280function wp_next_scheduled( $hook, $args = array() ) {
    202281        $crons = _get_cron_array();
    203282        $key = md5(serialize($args));
    204         if ( empty($crons) )
    205                 return false;
    206         foreach ( $crons as $timestamp => $cron ) {
    207                 if ( isset( $cron[$hook][$key] ) )
    208                         return $timestamp;
     283        $next = false;
     284
     285        if ( ! empty($crons) ) {
     286                foreach ( $crons as $timestamp => $cron ) {
     287                        if ( isset( $cron[$hook][$key] ) ) {
     288                                $next = $timestamp;
     289                                break;
     290                        }
     291                }
    209292        }
    210         return false;
     293
     294        /**
     295         * Filter the next scheduled event timestamp.
     296         *
     297         * @param int|boolean The UNIX timestamp when the scheduled event will next occur, or false if not found.
     298         * @param string $hook Action hook to execute when cron is run.
     299         * @param array $args Arguments to be passed to the callback function. Used for deduplicating events.
     300         */
     301        return apply_filters( 'next_scheduled', $next, $hook, $args );
    211302}
    212303
    213304/**
    function wp_get_schedules() { 
    392483function wp_get_schedule($hook, $args = array()) {
    393484        $crons = _get_cron_array();
    394485        $key = md5(serialize($args));
    395         if ( empty($crons) )
    396                 return false;
    397         foreach ( $crons as $timestamp => $cron ) {
    398                 if ( isset( $cron[$hook][$key] ) )
    399                         return $cron[$hook][$key]['schedule'];
     486        $schedule = false;
     487        if ( ! empty($crons) ) {
     488                foreach ( $crons as $timestamp => $cron ) {
     489                        if ( isset( $cron[$hook][$key] ) ) {
     490                                $schedule = $cron[$hook][$key]['schedule'];
     491                                break;
     492                        }
     493                }
    400494        }
    401         return false;
     495
     496        /**
     497         * Filter the schedule for a hook.
     498         *
     499         * @param string|boolean $schedule Schedule for the hook. False if not found.
     500         * @param string $hook Action hook to execute when cron is run.
     501         * @param array $args Optional. Arguments to pass to the hook's callback function.
     502         */
     503        return apply_filters( 'get_schedule', $schedule, $hook, $args );
    402504}
    403505
    404506//