WordPress.org

Make WordPress Core

Ticket #15148: 15148.diff

File 15148.diff, 4.7 KB (added by ryan, 8 years ago)
  • wp-includes/cron.php

     
    3333        if ( ! $event )
    3434                return false;
    3535
    36         $key = md5(serialize($event->args));
    37 
    38         $crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args );
    39         uksort( $crons, "strnatcasecmp" );
    40         _set_cron_array( $crons );
     36        wp_cron_insert_event($event);
    4137}
    4238
    4339/**
     
    7268        if ( ! $event )
    7369                return false;
    7470
    75         $key = md5(serialize($event->args));
    76 
    77         $crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args, 'interval' => $event->interval );
    78         uksort( $crons, "strnatcasecmp" );
    79         _set_cron_array( $crons );
     71        wp_cron_insert_event($event);
    8072}
    8173
    8274/**
     
    9789        $interval = 0;
    9890
    9991        // First we try to get it from the schedule
    100         if ( 0 == $interval )
     92        if ( isset($schedules[$recurrence]) )
    10193                $interval = $schedules[$recurrence]['interval'];
    10294        // Now we try to get it from the saved interval in case the schedule disappears
    103         if ( 0 == $interval )
    104                 $interval = $crons[$timestamp][$hook][$key]['interval'];
     95        if ( 0 == $interval ) {
     96                $event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'args' => $args );
     97                $event = wp_cron_get_event( $event );
     98                if ( $event )
     99                        $interval = $event->interval;
     100        }
    105101        // Now we assume something is wrong and fail to schedule
    106102        if ( 0 == $interval )
    107103                return false;
     
    132128 * as those used when originally scheduling the event.
    133129 */
    134130function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
    135         $crons = _get_cron_array();
    136         $key = md5(serialize($args));
    137         unset( $crons[$timestamp][$hook][$key] );
    138         if ( empty($crons[$timestamp][$hook]) )
    139                 unset( $crons[$timestamp][$hook] );
    140         if ( empty($crons[$timestamp]) )
    141                 unset( $crons[$timestamp] );
    142         _set_cron_array( $crons );
     131        $event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'args' => $args );
     132        wp_cron_delete_event($event);
    143133}
    144134
    145135/**
     
    172162 * @return bool|int The UNIX timestamp of the next time the scheduled event will occur.
    173163 */
    174164function wp_next_scheduled( $hook, $args = array() ) {
    175         $crons = _get_cron_array();
    176         $key = md5(serialize($args));
    177         if ( empty($crons) )
     165        $event = (object) array( 'hook' => $hook, 'timestamp' => 'next', 'args' => $args );
     166
     167        $event = wp_cron_get_event( $event );
     168
     169        if ( !$event )
    178170                return false;
    179         foreach ( $crons as $timestamp => $cron ) {
    180                 if ( isset( $cron[$hook][$key] ) )
    181                         return $timestamp;
    182         }
    183         return false;
     171
     172        return $event->timestamp;
    184173}
    185174
    186175/**
     
    349338// Private functions
    350339//
    351340
     341function wp_cron_delete_event( $event ) {
     342        if ( null !== $result = apply_filters('wp_cron_delete_event', null, $event) )
     343                return $result;
     344
     345        $crons = _get_cron_array();
     346        $key = md5(serialize($event->args));
     347        unset( $crons[$event->timestamp][$event->hook][$key] );
     348        if ( empty($crons[$event->timestamp][$event->hook]) )
     349                unset( $crons[$event->timestamp][$event->hook] );
     350        if ( empty($crons[$event->timestamp]) )
     351                unset( $crons[$event->timestamp] );
     352        _set_cron_array( $crons );     
     353}
     354
     355function wp_cron_get_event( $event ) {
     356        if ( null !== $result = apply_filters('wp_cron_get_event', null, $event) )
     357                return $result;
     358
     359        $crons = _get_cron_array();
     360        $key = md5(serialize($event->args));
     361        if ( empty($crons) )
     362                return false;
     363
     364        if ( empty($event->timestamp) || 'next' == $event->timestamp ) {
     365                foreach ( $crons as $timestamp => $cron ) {
     366                        if ( isset( $cron[$event->hook][$key] ) )
     367                                return (object) array( 'hook' => $event->hook, 'args' => $event->args, 'timestamp' => $timestamp, 'schedule' => $cron[$event->hook][$key]['schedule'], 'interval' => $cron[$event->hook][$key]['interval']);
     368                }
     369                return false;
     370        }
     371
     372        if ( isset($cron[$event->timestamp][$event->hook][$key]) )
     373                return (object) array( 'hook' => $event->hook, 'args' => $event->args, 'timestamp' => $event->timestamp, 'schedule' => $cron[$event->timestamp][$event->hook][$key]['schedule'], 'interval' => $cron[$event->timestamp][$event->hook][$key]['interval']);
     374
     375        return false;
     376}
     377
     378function wp_cron_get_events( $args ) {
     379        if ( null !== $result = apply_filters('wp_cron_get_events', null, $args) )
     380                return $result;
     381
     382        // @todo
     383        return null;
     384}
     385
     386function wp_cron_insert_event( $event ) {
     387        if ( null !== $result = apply_filters('wp_cron_insert_event', null, $event) )
     388                return $result;
     389
     390        $key = md5(serialize($event->args));
     391
     392        $crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args, 'interval' => $event->interval );
     393        uksort( $crons, "strnatcasecmp" );
     394        _set_cron_array( $crons );     
     395}
     396
    352397/**
    353398 * Retrieve cron info array option.
    354399 *