WordPress.org

Make WordPress Core

Ticket #15148: 15148.2.diff

File 15148.2.diff, 12.2 KB (added by ryan, 8 years ago)

WP_Cron_Store

  • wp-includes/update.php

     
    334334        wp_update_themes();
    335335}
    336336
     337/**
     338 * Schedule core, theme, and plugin update checks.
     339 *
     340 * @since 3.1.0
     341 */
     342function wp_schedule_update_checks() {
     343        if ( !wp_next_scheduled('wp_version_check') && !defined('WP_INSTALLING') )
     344                wp_schedule_event(time(), 'twicedaily', 'wp_version_check');
     345
     346        if ( !wp_next_scheduled('wp_update_plugins') && !defined('WP_INSTALLING') )
     347                wp_schedule_event(time(), 'twicedaily', 'wp_update_plugins');
     348
     349        if ( !wp_next_scheduled('wp_update_themes') && !defined('WP_INSTALLING') )
     350                wp_schedule_event(time(), 'twicedaily', 'wp_update_themes');
     351}
     352
    337353if ( ! is_main_site() )
    338354        return;
    339355
     
    352368add_action( 'admin_init', '_maybe_update_themes' );
    353369add_action( 'wp_update_themes', 'wp_update_themes' );
    354370
    355 if ( !wp_next_scheduled('wp_version_check') && !defined('WP_INSTALLING') )
    356         wp_schedule_event(time(), 'twicedaily', 'wp_version_check');
     371add_action('init', 'wp_schedule_update_checks');
    357372
    358 if ( !wp_next_scheduled('wp_update_plugins') && !defined('WP_INSTALLING') )
    359         wp_schedule_event(time(), 'twicedaily', 'wp_update_plugins');
    360 
    361 if ( !wp_next_scheduled('wp_update_themes') && !defined('WP_INSTALLING') )
    362         wp_schedule_event(time(), 'twicedaily', 'wp_update_themes');
    363 
    364373?>
  • wp-includes/cron.php

     
    2020 * @param array $args Optional. Arguments to pass to the hook's callback function.
    2121 */
    2222function wp_schedule_single_event( $timestamp, $hook, $args = array()) {
     23        global $wp_cron_store;
     24
    2325        // don't schedule a duplicate if there's already an identical event due in the next 10 minutes
    2426        $next = wp_next_scheduled($hook, $args);
    2527        if ( $next && $next <= $timestamp + 600 )
     
    3335        if ( ! $event )
    3436                return false;
    3537
    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 );
     38        $wp_cron_store->insert_event($event);
    4139}
    4240
    4341/**
     
    5957 * @return bool|null False on failure, null when complete with scheduling event.
    6058 */
    6159function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array()) {
     60        global $wp_cron_store;
     61
    6262        $crons = _get_cron_array();
    6363        $schedules = wp_get_schedules();
    6464
     
    7272        if ( ! $event )
    7373                return false;
    7474
    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 );
     75        $wp_cron_store->insert_event($event);
    8076}
    8177
    8278/**
     
    9187 * @return bool|null False on failure. Null when event is rescheduled.
    9288 */
    9389function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array()) {
    94         $crons = _get_cron_array();
    95         $schedules = wp_get_schedules();
    96         $key = md5(serialize($args));
     90        global $wp_cron_store;
     91
    9792        $interval = 0;
    9893
    9994        // First we try to get it from the schedule
    100         if ( 0 == $interval )
     95        if ( isset($schedules[$recurrence]) )
    10196                $interval = $schedules[$recurrence]['interval'];
    10297        // 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'];
     98        if ( 0 == $interval ) {
     99                $event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'args' => $args );
     100                $event = $wp_cron_store->get_event( $event );
     101                if ( $event )
     102                        $interval = $event->interval;
     103        }
    105104        // Now we assume something is wrong and fail to schedule
    106105        if ( 0 == $interval )
    107106                return false;
     
    132131 * as those used when originally scheduling the event.
    133132 */
    134133function 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 );
     134        global $wp_cron_store;
     135
     136        $event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'args' => $args );
     137        $wp_cron_store->delete_event($event);
    143138}
    144139
    145140/**
     
    172167 * @return bool|int The UNIX timestamp of the next time the scheduled event will occur.
    173168 */
    174169function wp_next_scheduled( $hook, $args = array() ) {
    175         $crons = _get_cron_array();
    176         $key = md5(serialize($args));
    177         if ( empty($crons) )
     170        global $wp_cron_store;
     171
     172        $event = (object) array( 'hook' => $hook, 'timestamp' => 'next', 'args' => $args );
     173
     174        $event = $wp_cron_store->get_event( $event );
     175
     176        if ( !$event )
    178177                return false;
    179         foreach ( $crons as $timestamp => $cron ) {
    180                 if ( isset( $cron[$hook][$key] ) )
    181                         return $timestamp;
    182         }
    183         return false;
     178
     179        return $event->timestamp;
    184180}
    185181
    186182/**
     
    191187 * @return null Cron could not be spawned, because it is not needed to run.
    192188 */
    193189function spawn_cron( $local_time = 0 ) {
     190        global $wp_cron_store;
    194191
    195192        if ( !$local_time )
    196193                $local_time = time();
     
    220217                return;
    221218
    222219        //sanity check
    223         $crons = _get_cron_array();
     220        $crons = $wp_cron_store->_get_cron_array();
    224221        if ( !is_array($crons) )
    225222                return;
    226223
     
    260257 * @return null When doesn't need to run Cron.
    261258 */
    262259function wp_cron() {
     260        global $wp_cron_store;
    263261
    264262        // Prevent infinite loops caused by lack of wp-cron.php
    265263        if ( strpos($_SERVER['REQUEST_URI'], '/wp-cron.php') !== false || ( defined('DISABLE_WP_CRON') && DISABLE_WP_CRON ) )
    266264                return;
    267265
    268         if ( false === $crons = _get_cron_array() )
     266        if ( false === $crons = $wp_cron_store->_get_cron_array() )
    269267                return;
    270268
    271269        $local_time = time();
     
    334332 * @return string|bool False, if no schedule. Schedule on success.
    335333 */
    336334function wp_get_schedule($hook, $args = array()) {
    337         $crons = _get_cron_array();
    338         $key = md5(serialize($args));
    339         if ( empty($crons) )
     335        global $wp_cron_store;
     336
     337        $event = (object) array( 'hook' => $hook, 'timestamp' => 'next', 'args' => $args );
     338
     339        $event = $wp_cron_store->get_event( $event );
     340
     341        if ( !$event )
    340342                return false;
    341         foreach ( $crons as $timestamp => $cron ) {
    342                 if ( isset( $cron[$hook][$key] ) )
    343                         return $cron[$hook][$key]['schedule'];
    344         }
    345         return false;
     343
     344        return $event->schedule;
    346345}
    347346
    348347//
    349348// Private functions
    350349//
    351350
    352 /**
    353  * Retrieve cron info array option.
    354  *
    355  * @since 2.1.0
    356  * @access private
    357  *
    358  * @return array CRON info array.
    359  */
    360 function _get_cron_array()  {
    361         $cron = get_option('cron');
    362         if ( ! is_array($cron) )
    363                 return false;
     351class WP_Cron_Store {
     352        function delete_event( $event ) {
     353                $crons = $this->_get_cron_array();
     354                $key = md5(serialize($event->args));
     355                unset( $crons[$event->timestamp][$event->hook][$key] );
     356                if ( empty($crons[$event->timestamp][$event->hook]) )
     357                        unset( $crons[$event->timestamp][$event->hook] );
     358                if ( empty($crons[$event->timestamp]) )
     359                        unset( $crons[$event->timestamp] );
     360                $this->_set_cron_array( $crons );       
     361        }
    364362
    365         if ( !isset($cron['version']) )
    366                 $cron = _upgrade_cron_array($cron);
     363        function get_event( $event ) {
     364                $crons = $this->_get_cron_array();
     365                $key = md5(serialize($event->args));
     366                if ( empty($crons) )
     367                        return false;
     368       
     369                if ( empty($event->timestamp) || 'next' == $event->timestamp ) {
     370                        foreach ( $crons as $timestamp => $cron ) {
     371                                if ( isset( $cron[$event->hook][$key] ) )
     372                                        return (object) array( 'hook' => $event->hook, 'args' => $event->args, 'timestamp' => $timestamp, 'schedule' => $cron[$event->hook][$key]['schedule'], 'interval' => $cron[$event->hook][$key]['interval']);
     373                        }
     374                        return false;
     375                }
    367376
    368         unset($cron['version']);
     377                if ( isset($cron[$event->timestamp][$event->hook][$key]) )
     378                        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']);
    369379
    370         return $cron;
    371 }
    372 
    373 /**
    374  * Updates the CRON option with the new CRON array.
    375  *
    376  * @since 2.1.0
    377  * @access private
    378  *
    379  * @param array $cron Cron info array from {@link _get_cron_array()}.
    380  */
    381 function _set_cron_array($cron) {
    382         $cron['version'] = 2;
    383         update_option( 'cron', $cron );
    384 }
    385 
    386 /**
    387  * Upgrade a Cron info array.
    388  *
    389  * This function upgrades the Cron info array to version 2.
    390  *
    391  * @since 2.1.0
    392  * @access private
    393  *
    394  * @param array $cron Cron info array from {@link _get_cron_array()}.
    395  * @return array An upgraded Cron info array.
    396  */
    397 function _upgrade_cron_array($cron) {
    398         if ( isset($cron['version']) && 2 == $cron['version'])
     380                return false;
     381        }
     382       
     383        function get_events( $args ) {
     384                // @todo
     385                return null;
     386        }
     387       
     388        function insert_event( $event ) {
     389                $key = md5(serialize($event->args));
     390       
     391                $crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args, 'interval' => $event->interval );
     392                uksort( $crons, "strnatcasecmp" );
     393                $this->_set_cron_array( $crons );       
     394        }
     395       
     396        /**
     397         * Retrieve cron info array option.
     398         *
     399         * @since 2.1.0
     400         * @access private
     401         *
     402         * @return array CRON info array.
     403         */
     404        function _get_cron_array()  {
     405                $cron = get_option('cron');
     406                if ( ! is_array($cron) )
     407                        return false;
     408       
     409                if ( !isset($cron['version']) )
     410                        $cron = $this->_upgrade_cron_array($cron);
     411       
     412                unset($cron['version']);
     413       
    399414                return $cron;
    400 
    401         $new_cron = array();
    402 
    403         foreach ( (array) $cron as $timestamp => $hooks) {
    404                 foreach ( (array) $hooks as $hook => $args ) {
    405                         $key = md5(serialize($args['args']));
    406                         $new_cron[$timestamp][$hook][$key] = $args;
     415        }
     416       
     417        /**
     418         * Updates the CRON option with the new CRON array.
     419         *
     420         * @since 2.1.0
     421         * @access private
     422         *
     423         * @param array $cron Cron info array from {@link _get_cron_array()}.
     424         */
     425        function _set_cron_array($cron) {
     426                $cron['version'] = 2;
     427                update_option( 'cron', $cron );
     428        }
     429       
     430        /**
     431         * Upgrade a Cron info array.
     432         *
     433         * This function upgrades the Cron info array to version 2.
     434         *
     435         * @since 2.1.0
     436         * @access private
     437         *
     438         * @param array $cron Cron info array from {@link _get_cron_array()}.
     439         * @return array An upgraded Cron info array.
     440         */
     441        function _upgrade_cron_array($cron) {
     442                if ( isset($cron['version']) && 2 == $cron['version'])
     443                        return $cron;
     444       
     445                $new_cron = array();
     446       
     447                foreach ( (array) $cron as $timestamp => $hooks) {
     448                        foreach ( (array) $hooks as $hook => $args ) {
     449                                $key = md5(serialize($args['args']));
     450                                $new_cron[$timestamp][$hook][$key] = $args;
     451                        }
    407452                }
     453       
     454                $new_cron['version'] = 2;
     455                update_option( 'cron', $new_cron );
     456                return $new_cron;
    408457        }
    409 
    410         $new_cron['version'] = 2;
    411         update_option( 'cron', $new_cron );
    412         return $new_cron;
    413458}
    414459
    415460// stub for checking server timer accuracy, using outside standard time sources
  • wp-settings.php

     
    9999// Run the installer if WordPress is not installed.
    100100wp_not_installed();
    101101
     102// Load cron
     103require_once( ABSPATH . WPINC . '/cron.php' );
     104if ( file_exists( WP_CONTENT_DIR . '/cron.php' ) )
     105        require_once( WP_CONTENT_DIR . '/cron.php' );
     106
     107if ( !isset( $wp_cron_store ) )
     108        $wp_cron_store = new WP_Cron_Store;
     109
    102110// Load most of WordPress.
    103111require( ABSPATH . WPINC . '/formatting.php' );
    104112require( ABSPATH . WPINC . '/capabilities.php' );
     
    120128require( ABSPATH . WPINC . '/bookmark.php' );
    121129require( ABSPATH . WPINC . '/bookmark-template.php' );
    122130require( ABSPATH . WPINC . '/kses.php' );
    123 require( ABSPATH . WPINC . '/cron.php' );
    124131require( ABSPATH . WPINC . '/deprecated.php' );
    125132require( ABSPATH . WPINC . '/script-loader.php' );
    126133require( ABSPATH . WPINC . '/taxonomy.php' );
  • wp-cron.php

     
    2626        require_once('./wp-load.php');
    2727}
    2828
    29 if ( false === $crons = _get_cron_array() )
     29if ( false === $crons = $wp_cron_store->_get_cron_array() )
    3030        die();
    3131
    3232$keys = array_keys( $crons );