From 339ada95372633dd801c680f196c048477140415 Mon Sep 17 00:00:00 2001 From: Ryan Boren Date: Wed, 13 Sep 2006 23:54:15 +0000 Subject: [PATCH] cron and future post publishing fixes. fixes #3058 git-svn-id: https://develop.svn.wordpress.org/trunk@4189 602fd350-edb4-49c9-b593-d223f7449a82 --- wp-cron.php | 19 +++++---- wp-includes/cron.php | 94 ++++++++++++++++++++++++++++++++------------ wp-includes/post.php | 2 +- 3 files changed, 81 insertions(+), 34 deletions(-) diff --git a/wp-cron.php b/wp-cron.php index 74d6dda2b3..dd52f2b3a9 100644 --- a/wp-cron.php +++ b/wp-cron.php @@ -6,20 +6,23 @@ require_once('wp-config.php'); if ( $_GET['check'] != md5(DB_PASS . '187425') ) exit; -$crons = get_option('cron'); +$crons = _get_cron_array(); $keys = array_keys($crons); if (!is_array($crons) || $keys[0] > time()) return; foreach ($crons as $timestamp => $cronhooks) { if ($timestamp > time()) break; - foreach($cronhooks as $hook => $args) { - do_action($hook, $args['args']); - $schedule = $args['schedule']; - if($schedule != false) { - $args = array_merge( array($timestamp, $schedule, $hook), $args['args']); - call_user_func_array('wp_reschedule_event', $args); + foreach ($cronhooks as $hook => $keys) { + foreach ($keys as $key => $args) { + do_action_ref_array($hook, $args['args']); + $schedule = $args['schedule']; + if ($schedule != false) { + $args = array_merge( array($timestamp, $schedule, $hook), $args['args']); + call_user_func_array('wp_reschedule_event', $args); + } + wp_unschedule_event($timestamp, $hook); } - wp_unschedule_event($timestamp, $hook); + wp_unschedule_event($timestamp, $hook, $args); } } ?> diff --git a/wp-includes/cron.php b/wp-includes/cron.php index dfc067669b..d57d30a453 100644 --- a/wp-includes/cron.php +++ b/wp-includes/cron.php @@ -2,27 +2,30 @@ function wp_schedule_single_event( $timestamp, $hook ) { $args = array_slice( func_get_args(), 2 ); - $crons = get_option( 'cron' ); - $crons[$timestamp][$hook] = array( 'schedule' => false, 'args' => $args ); + $crons = _get_cron_array(); + $key = md5(serialize($args)); + $crons[$timestamp][$hook][$key] = array( 'schedule' => false, 'args' => $args ); ksort( $crons ); - update_option( 'cron', $crons ); + _set_cron_array( $crons ); } function wp_schedule_event( $timestamp, $recurrence, $hook ) { $args = array_slice( func_get_args(), 3 ); - $crons = get_option( 'cron' ); + $crons = _get_cron_array(); $schedules = wp_get_schedules(); + $key = md5(serialize($args)); if ( !isset( $schedules[$recurrence] ) ) return false; - $crons[$timestamp][$hook] = array( 'schedule' => $recurrence, 'args' => $args, 'interval' => $schedules[$recurrence]['interval'] ); + $crons[$timestamp][$hook][$key] = array( 'schedule' => $recurrence, 'args' => $args, 'interval' => $schedules[$recurrence]['interval'] ); ksort( $crons ); - update_option( 'cron', $crons ); + _set_cron_array( $crons ); } function wp_reschedule_event( $timestamp, $recurrence, $hook ) { $args = array_slice( func_get_args(), 3 ); - $crons = get_option( 'cron' ); + $crons = _get_cron_array(); $schedules = wp_get_schedules(); + $key = md5(serialize($args)); $interval = 0; // First we try to get it from the schedule @@ -30,7 +33,7 @@ function wp_reschedule_event( $timestamp, $recurrence, $hook ) { $interval = $schedules[$recurrence]['interval']; // Now we try to get it from the saved interval in case the schedule disappears if ( 0 == $interval ) - $interval = $crons[$timestamp][$hook]['interval']; + $interval = $crons[$timestamp][$hook][$key]['interval']; // Now we assume something is wrong and fail to schedule if ( 0 == $interval ) return false; @@ -38,40 +41,41 @@ function wp_reschedule_event( $timestamp, $recurrence, $hook ) { while ( $timestamp < time() + 1 ) $timestamp += $interval; - wp_schedule_event( $timestamp, $recurrence, $hook ); + wp_schedule_event( $timestamp, $recurrence, $hook, $args ); } -function wp_unschedule_event( $timestamp, $hook ) { - $crons = get_option( 'cron' ); - unset( $crons[$timestamp][$hook] ); +function wp_unschedule_event( $timestamp, $hook, $args = array() ) { + $crons = _get_cron_array(); + $key = md5(serialize($args)); + unset( $crons[$timestamp][$hook][$key] ); + if ( empty($crons[$timestamp][$hook]) ) + unset( $crons[$timestamp][$hook] ); if ( empty($crons[$timestamp]) ) unset( $crons[$timestamp] ); - update_option( 'cron', $crons ); + _set_cron_array( $crons ); } function wp_clear_scheduled_hook( $hook ) { $args = array_slice( func_get_args(), 1 ); while ( $timestamp = wp_next_scheduled( $hook, $args ) ) - wp_unschedule_event( $timestamp, $hook ); + wp_unschedule_event( $timestamp, $hook, $args ); } -function wp_next_scheduled( $hook, $args = '' ) { - $crons = get_option( 'cron' ); +function wp_next_scheduled( $hook, $args = array() ) { + $crons = _get_cron_array(); + $key = md5(serialize($args)); if ( empty($crons) ) return false; - foreach ( $crons as $timestamp => $cron ) - if ( isset( $cron[$hook] ) ) { - if ( empty($args) ) - return $timestamp; - if ( $args == $cron[$hook]['args'] ) - return $timestamp; - } + foreach ( $crons as $timestamp => $cron ) { + if ( isset( $cron[$hook][$key] ) ) + return $timestamp; + } return false; } function spawn_cron() { - $crons = get_option( 'cron' ); + $crons = _get_cron_array(); if ( !is_array($crons) ) return; @@ -92,7 +96,7 @@ function spawn_cron() { } function wp_cron() { - $crons = get_option( 'cron' ); + $crons = _get_cron_array(); if ( !is_array($crons) ) return; @@ -121,4 +125,44 @@ function wp_get_schedules() { return array_merge( apply_filters( 'cron_schedules', array() ), $schedules ); } +// +// Private functions +// + +function _get_cron_array() { + $cron = get_option('cron'); + if ( ! is_array($cron) ) + return false; + + if ( !isset($cron['version']) ) + $cron = _upgrade_cron_array($cron); + + unset($cron['version']); + + return $cron; +} + +function _set_cron_array($cron) { + $cron['version'] = 2; + update_option( 'cron', $cron ); +} + +function _upgrade_cron_array($cron) { + if ( isset($cron['version']) && 2 == $cron['version']) + return $cron; + + $new_cron = array(); + + foreach ($cron as $timestamp => $hooks) { + foreach ( $hooks as $hook => $args ) { + $key = md5(serialize($args['args'])); + $new_cron[$timestamp][$hook][$key] = $args; + } + } + + $new_cron['version'] = 2; + update_option( 'cron', $new_cron ); + return $new_cron; +} + ?> \ No newline at end of file diff --git a/wp-includes/post.php b/wp-includes/post.php index 189422538c..b7c59f4949 100644 --- a/wp-includes/post.php +++ b/wp-includes/post.php @@ -698,7 +698,7 @@ function wp_insert_post($postarr = array()) { // Schedule publication. if ( 'future' == $post_status ) - wp_schedule_single_event(mysql2date('U', $post_date), 'publish_future_post', $post_ID); + wp_schedule_single_event(strtotime($post->post_date_gmt. ' GMT'), 'publish_future_post', $post_ID); do_action('save_post', $post_ID); do_action('wp_insert_post', $post_ID);