Make WordPress Core


Ignore:
Timestamp:
09/13/2006 11:54:15 PM (19 years ago)
Author:
ryan
Message:

cron and future post publishing fixes. fixes #3058

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-includes/cron.php

    r4144 r4189  
    33function wp_schedule_single_event( $timestamp, $hook ) {
    44    $args = array_slice( func_get_args(), 2 );
    5     $crons = get_option( 'cron' );
    6     $crons[$timestamp][$hook] = array( 'schedule' => false, 'args' => $args );
     5    $crons = _get_cron_array();
     6    $key = md5(serialize($args));
     7    $crons[$timestamp][$hook][$key] = array( 'schedule' => false, 'args' => $args );
    78    ksort( $crons );
    8     update_option( 'cron', $crons );
     9    _set_cron_array( $crons );
    910}
    1011
    1112function wp_schedule_event( $timestamp, $recurrence, $hook ) {
    1213    $args = array_slice( func_get_args(), 3 );
    13     $crons = get_option( 'cron' );
     14    $crons = _get_cron_array();
    1415    $schedules = wp_get_schedules();
     16    $key = md5(serialize($args));
    1517    if ( !isset( $schedules[$recurrence] ) )
    1618        return false;
    17     $crons[$timestamp][$hook] = array( 'schedule' => $recurrence, 'args' => $args, 'interval' => $schedules[$recurrence]['interval'] );
     19    $crons[$timestamp][$hook][$key] = array( 'schedule' => $recurrence, 'args' => $args, 'interval' => $schedules[$recurrence]['interval'] );
    1820    ksort( $crons );
    19     update_option( 'cron', $crons );
     21    _set_cron_array( $crons );
    2022}
    2123
    2224function wp_reschedule_event( $timestamp, $recurrence, $hook ) {
    2325    $args = array_slice( func_get_args(), 3 );
    24     $crons = get_option( 'cron' );
     26    $crons = _get_cron_array();
    2527    $schedules = wp_get_schedules();
     28    $key = md5(serialize($args));
    2629    $interval = 0;
    2730
     
    3134    // Now we try to get it from the saved interval in case the schedule disappears
    3235    if ( 0 == $interval )
    33         $interval = $crons[$timestamp][$hook]['interval'];
     36        $interval = $crons[$timestamp][$hook][$key]['interval'];
    3437    // Now we assume something is wrong and fail to schedule
    3538    if ( 0 == $interval )
     
    3942        $timestamp += $interval;
    4043
    41     wp_schedule_event( $timestamp, $recurrence, $hook );
     44    wp_schedule_event( $timestamp, $recurrence, $hook, $args );
    4245}
    4346
    44 function wp_unschedule_event( $timestamp, $hook ) {
    45     $crons = get_option( 'cron' );
    46     unset( $crons[$timestamp][$hook] );
     47function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
     48    $crons = _get_cron_array();
     49    $key = md5(serialize($args));
     50    unset( $crons[$timestamp][$hook][$key] );
     51    if ( empty($crons[$timestamp][$hook]) )
     52        unset( $crons[$timestamp][$hook] );
    4753    if ( empty($crons[$timestamp]) )
    4854        unset( $crons[$timestamp] );
    49     update_option( 'cron', $crons );
     55    _set_cron_array( $crons );
    5056}
    5157
     
    5460   
    5561    while ( $timestamp = wp_next_scheduled( $hook, $args ) )
    56         wp_unschedule_event( $timestamp, $hook );
     62        wp_unschedule_event( $timestamp, $hook, $args );
    5763}
    5864
    59 function wp_next_scheduled( $hook, $args = '' ) {
    60     $crons = get_option( 'cron' );
     65function wp_next_scheduled( $hook, $args = array() ) {
     66    $crons = _get_cron_array();
     67    $key = md5(serialize($args));
    6168    if ( empty($crons) )
    6269        return false;
    63     foreach ( $crons as $timestamp => $cron )
    64         if ( isset( $cron[$hook] ) ) {
    65             if ( empty($args) )
    66                 return $timestamp;
    67             if ( $args == $cron[$hook]['args'] )
    68                 return $timestamp;
    69         }
     70    foreach ( $crons as $timestamp => $cron ) {
     71        if ( isset( $cron[$hook][$key] ) )
     72            return $timestamp;
     73    }
    7074    return false;
    7175}
    7276
    7377function spawn_cron() {
    74     $crons = get_option( 'cron' );
     78    $crons = _get_cron_array();
    7579   
    7680    if ( !is_array($crons) )
     
    9397
    9498function wp_cron() {
    95     $crons = get_option( 'cron' );
     99    $crons = _get_cron_array();
    96100   
    97101    if ( !is_array($crons) )
     
    122126}
    123127
     128//
     129// Private functions
     130//
     131
     132function _get_cron_array()  {
     133    $cron = get_option('cron');
     134    if ( ! is_array($cron) )
     135        return false;
     136
     137    if ( !isset($cron['version']) )
     138        $cron = _upgrade_cron_array($cron);
     139
     140    unset($cron['version']);
     141
     142    return $cron;
     143}
     144
     145function _set_cron_array($cron) {
     146    $cron['version'] = 2;
     147    update_option( 'cron', $cron );
     148}
     149
     150function _upgrade_cron_array($cron) {
     151    if ( isset($cron['version']) && 2 == $cron['version'])
     152        return $cron;
     153
     154    $new_cron = array();
     155
     156    foreach ($cron as $timestamp => $hooks) {
     157        foreach ( $hooks as $hook => $args ) {
     158            $key = md5(serialize($args['args']));
     159            $new_cron[$timestamp][$hook][$key] = $args;
     160        }
     161    }
     162
     163    $new_cron['version'] = 2;
     164    update_option( 'cron', $new_cron );
     165    return $new_cron;
     166}
     167
    124168?>
Note: See TracChangeset for help on using the changeset viewer.