WordPress.org

Make WordPress Core

Ticket #32656: 32656-30apr2016.diff

File 32656-30apr2016.diff, 7.3 KB (added by DavidAnderson, 3 years ago)

Update 32656.diff so that it applies to trunk as at 2016-Apr-30 (no other changes yet)

  • wp-includes/cron.php

     
    2626                return false;
    2727        }
    2828
     29        $event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => false, 'args' => $args );
     30
     31        /**
     32         * Filter to preflight or hijack scheduling an event.
     33         *
     34         * Passing a non-null value will short-circuit adding the event to the cron
     35         * array, returning the passed value instead.
     36         *
     37         * Both single events and recurring events are passed through this filter;
     38         * single events have `$event->schedule` as false, whereas recurring events
     39         * have this set to a recurrence from {@see wp_get_schedules}. Recurring
     40         * events also have the integer recurrence interval set as `$event->interval`
     41         *
     42         * @param null|mixed $pre Value to return instead. Default null to continue adding the event.
     43         * @param object $event An object containing the event's data.
     44         */
     45        $pre = apply_filters( 'pre_schedule_event', null, $event );
     46        if ( null !== $pre ) {
     47                return $pre;
     48        }
     49       
    2950        // Don't schedule a duplicate if there's already an identical event due within 10 minutes of it
    3051        $next = wp_next_scheduled($hook, $args);
    3152        if ( $next && abs( $next - $timestamp ) <= 10 * MINUTE_IN_SECONDS ) {
     
    3253                return false;
    3354        }
    3455
    35         $crons = _get_cron_array();
    36         $event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => false, 'args' => $args );
    3756        /**
    3857         * Filter a single event before it is scheduled.
    3958         *
     
    4968
    5069        $key = md5(serialize($event->args));
    5170
     71        $crons = _get_cron_array();
    5272        $crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args );
    5373        uksort( $crons, "strnatcasecmp" );
    5474        _set_cron_array( $crons );
     
    80100                return false;
    81101        }
    82102
    83         $crons = _get_cron_array();
    84103        $schedules = wp_get_schedules();
    85104
    86105        if ( !isset( $schedules[$recurrence] ) )
     
    87106                return false;
    88107
    89108        $event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => $recurrence, 'args' => $args, 'interval' => $schedules[$recurrence]['interval'] );
     109       
    90110        /** This filter is documented in wp-includes/cron.php */
     111        $pre = apply_filters( 'pre_schedule_event', null, $event );
     112        if ( null !== $pre ) {
     113                return $pre;
     114        }
     115
     116        /** This filter is documented in wp-includes/cron.php */
    91117        $event = apply_filters( 'schedule_event', $event );
    92118
    93119        // A plugin disallowed this event
     
    96122
    97123        $key = md5(serialize($event->args));
    98124
     125        $crons = _get_cron_array();
    99126        $crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args, 'interval' => $event->interval );
    100127        uksort( $crons, "strnatcasecmp" );
    101128        _set_cron_array( $crons );
     
    118145                return false;
    119146        }
    120147
     148        /**
     149         * Filter to preflight or hijack rescheduling of events.
     150         *
     151         * Passing a non-null value will short-circuit the normal rescheduling
     152         * process, returning the passed value instead.
     153         *
     154         * Note that `$recurrence` is the original stored value. This is not
     155         * guaranteed to exist when the event is rescheduled, so plugins should
     156         * store the interval value and fall back to this if needed.
     157         *
     158         * @param null|mixed $pre Value to return instead. Default null to continue rescheduling the event.
     159         * @param int $timestamp Timestamp for when the event originally ran.
     160         * @param string $recurrence How often the event should recur.
     161         * @param string $hook Action hook to execute when cron is run.
     162         * @param array $args Optional. Arguments to pass to the hook's callback function.
     163         */
     164        $pre = apply_filters( 'pre_reschedule_event', null, $timestamp, $recurrence, $hook, $args );
     165        if ( $pre !== null ) {
     166                return $pre;
     167        }
     168       
    121169        $crons = _get_cron_array();
    122170        $schedules = wp_get_schedules();
    123171        $key = md5( serialize( $args ) );
     
    169217                return false;
    170218        }
    171219
     220        /**
     221         * Filter to preflight or hijack unscheduling of events.
     222         *
     223         * Passing a non-null value will short-circuit the normal unscheduling
     224         * process, returning the passed value instead.
     225         *
     226         * @param null|mixed $pre Value to return instead. Default null to continue unscheduling the event.
     227         * @param int $timestamp Timestamp for when to run the event.
     228         * @param string $hook Action hook, the execution of which will be unscheduled.
     229         * @param array $args Arguments to pass to the hook's callback function.
     230         */
     231        $pre = apply_filters( 'pre_unschedule_event', null, $timestamp, $hook, $args );
     232        if ( $pre !== null ) {
     233                return $pre;
     234        }
     235       
    172236        $crons = _get_cron_array();
    173237        $key = md5(serialize($args));
    174238        unset( $crons[$timestamp][$hook][$key] );
     
    195259                $args = array_slice( func_get_args(), 1 );
    196260        }
    197261
     262        /**
     263         * Filter to preflight or hijack clearing a scheduled hook.
     264         *
     265         * Passing a non-null value will short-circuit the normal unscheduling
     266         * process, returning the passed value instead.
     267         *
     268         * @param null|mixed $pre Value to return instead. Default null to continue unscheduling the event.
     269         * @param string $hook Action hook, the execution of which will be unscheduled.
     270         * @param array $args Arguments to pass to the hook's callback function.
     271         */
     272        $pre = apply_filters( 'pre_clear_scheduled_hook', null, $hook, $args );
     273        if ( $pre !== null ) {
     274                return $pre;
     275        }
     276
    198277        // This logic duplicates wp_next_scheduled()
    199278        // It's required due to a scenario where wp_unschedule_event() fails due to update_option() failing,
    200279        // and, wp_next_scheduled() returns the same schedule in an infinite loop.
     
    222301function wp_next_scheduled( $hook, $args = array() ) {
    223302        $crons = _get_cron_array();
    224303        $key = md5(serialize($args));
    225         if ( empty($crons) )
    226                 return false;
    227         foreach ( $crons as $timestamp => $cron ) {
    228                 if ( isset( $cron[$hook][$key] ) )
    229                         return $timestamp;
     304        $next = false;
     305
     306        if ( ! empty($crons) ) {
     307                foreach ( $crons as $timestamp => $cron ) {
     308                        if ( isset( $cron[$hook][$key] ) ) {
     309                                $next = $timestamp;
     310                                break;
     311                        }
     312                }
    230313        }
    231         return false;
     314
     315        /**
     316         * Filter the next scheduled event timestamp.
     317         *
     318         * @param int|boolean The UNIX timestamp when the scheduled event will next occur, or false if not found.
     319         * @param string $hook Action hook to execute when cron is run.
     320         * @param array $args Arguments to be passed to the callback function. Used for deduplicating events.
     321         */
     322        return apply_filters( 'next_scheduled', $next, $hook, $args );
    232323}
    233324
    234325/**
     
    417508function wp_get_schedule($hook, $args = array()) {
    418509        $crons = _get_cron_array();
    419510        $key = md5(serialize($args));
    420         if ( empty($crons) )
    421                 return false;
    422         foreach ( $crons as $timestamp => $cron ) {
    423                 if ( isset( $cron[$hook][$key] ) )
    424                         return $cron[$hook][$key]['schedule'];
     511        $schedule = false;
     512        if ( ! empty($crons) ) {
     513                foreach ( $crons as $timestamp => $cron ) {
     514                        if ( isset( $cron[$hook][$key] ) ) {
     515                                $schedule = $cron[$hook][$key]['schedule'];
     516                                break;
     517                        }
     518                }
    425519        }
    426         return false;
     520
     521        /**
     522         * Filter the schedule for a hook.
     523         *
     524         * @param string|boolean $schedule Schedule for the hook. False if not found.
     525         * @param string $hook Action hook to execute when cron is run.
     526         * @param array $args Optional. Arguments to pass to the hook's callback function.
     527         */
     528        return apply_filters( 'get_schedule', $schedule, $hook, $args );
    427529}
    428530
    429531//