WordPress.org

Make WordPress Core

Ticket #18997: #cron.patch

File #cron.patch, 4.2 KB (added by arena, 6 years ago)
  • wp-includes/cron.php

     
    2525        if ( $next && $next <= $timestamp + 600 )
    2626                return;
    2727
    28         $crons = _get_cron_array();
    2928        $event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => false, 'args' => $args );
    3029        $event = apply_filters('schedule_event', $event);
    3130
     
    3534
    3635        $key = md5(serialize($event->args));
    3736
     37        $crons = _get_cron_array();
    3838        $crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args );
    39         uksort( $crons, "strnatcasecmp" );
    4039        _set_cron_array( $crons );
    4140}
    4241
     
    6160 * @return bool|null False on failure, null when complete with scheduling event.
    6261 */
    6362function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array()) {
    64         $crons = _get_cron_array();
    6563        $schedules = wp_get_schedules();
    6664
    6765        if ( !isset( $schedules[$recurrence] ) )
     
    7674
    7775        $key = md5(serialize($event->args));
    7876
     77        $crons = _get_cron_array();
    7978        $crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args, 'interval' => $event->interval );
    80         uksort( $crons, "strnatcasecmp" );
    8179        _set_cron_array( $crons );
    8280}
    8381
     
    9391 * @return bool|null False on failure. Null when event is rescheduled.
    9492 */
    9593function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array()) {
    96         $crons = _get_cron_array();
    97         $schedules = wp_get_schedules();
    98         $key = md5(serialize($args));
    9994        $interval = 0;
    10095
    10196        // First we try to get it from the schedule
    102         if ( 0 == $interval )
     97        $schedules = wp_get_schedules();
     98        if ( !empty( $schedules ) )
    10399                $interval = $schedules[$recurrence]['interval'];
     100
    104101        // Now we try to get it from the saved interval in case the schedule disappears
    105         if ( 0 == $interval )
    106                 $interval = $crons[$timestamp][$hook][$key]['interval'];
     102        if ( 0 == $interval ) {
     103                $crons = _get_cron_array();
     104                if ( !empty( $crons ) ) {
     105                        $key = md5( serialize( $args ) );
     106                        $interval = $crons[$timestamp][$hook][$key]['interval'];
     107                }
     108        }
    107109        // Now we assume something is wrong and fail to schedule
    108110        if ( 0 == $interval )
    109111                return false;
     
    135137 */
    136138function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
    137139        $crons = _get_cron_array();
     140        if ( empty( $crons ) )
     141                return;
    138142        $key = md5(serialize($args));
    139143        unset( $crons[$timestamp][$hook][$key] );
    140144        if ( empty($crons[$timestamp][$hook]) )
    141145                unset( $crons[$timestamp][$hook] );
    142         if ( empty($crons[$timestamp]) )
    143                 unset( $crons[$timestamp] );
    144         _set_cron_array( $crons );
     146        _set_cron_array( $crons, 'removed' );
    145147}
    146148
    147149/**
     
    164166                wp_unschedule_event( $timestamp, $hook, $args );
    165167}
    166168
     169/**
     170* Unschedule all previously scheduled cron job for a hook.
     171*
     172* Can be usefull for plugins when deactivating to clean up the cron queue
     173*
     174* The $hook parameter is required, so that the events can be
     175* identified.
     176*
     177* @since 3.4
     178*
     179* @param string $hook Action hook, the execution of which will be unscheduled.
     180*/
     181function wp_unschedule_hook( $hook ) {
     182        $crons = _get_cron_array();
     183        if ( empty( $crons ) )
     184                return;
     185        foreach($crons as $timestamp => $args)
     186                unset( $crons[$timestamp][$hook] );
     187        _set_cron_array( $crons, 'removed' );
     188}
     189
    167190/**
    168191 * Retrieve the next timestamp for a cron event.
    169192 *
     
    175198 */
    176199function wp_next_scheduled( $hook, $args = array() ) {
    177200        $crons = _get_cron_array();
     201        if ( empty( $crons ) )
     202                return false;
    178203        $key = md5(serialize($args));
    179         if ( empty($crons) )
    180                 return false;
    181204        foreach ( $crons as $timestamp => $cron ) {
    182205                if ( isset( $cron[$hook][$key] ) )
    183206                        return $timestamp;
     
    373396 * @access private
    374397 *
    375398 * @param array $cron Cron info array from {@link _get_cron_array()}.
     399 * @param string $action Default value 'added'. Other possible values : 'removed'.
    376400 */
    377 function _set_cron_array($cron) {
     401function _set_cron_array($cron, $action = 'added') {
     402        if ('added' == $action)
     403                uksort( $crons, "strnatcasecmp" );
     404        if ('removed' == $action)
     405                $crons = array_filter( $crons );
    378406        $cron['version'] = 2;
    379407        update_option( 'cron', $cron );
    380408}