cron and future post publishing fixes. fixes #3058

git-svn-id: http://svn.automattic.com/wordpress/trunk@4189 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
ryan 2006-09-13 23:54:15 +00:00
parent 986f88a690
commit 68aa6231a2
3 changed files with 81 additions and 34 deletions

View File

@ -6,20 +6,23 @@ require_once('wp-config.php');
if ( $_GET['check'] != md5(DB_PASS . '187425') ) if ( $_GET['check'] != md5(DB_PASS . '187425') )
exit; exit;
$crons = get_option('cron'); $crons = _get_cron_array();
$keys = array_keys($crons); $keys = array_keys($crons);
if (!is_array($crons) || $keys[0] > time()) if (!is_array($crons) || $keys[0] > time())
return; return;
foreach ($crons as $timestamp => $cronhooks) { foreach ($crons as $timestamp => $cronhooks) {
if ($timestamp > time()) break; if ($timestamp > time()) break;
foreach($cronhooks as $hook => $args) { foreach ($cronhooks as $hook => $keys) {
do_action($hook, $args['args']); foreach ($keys as $key => $args) {
$schedule = $args['schedule']; do_action_ref_array($hook, $args['args']);
if($schedule != false) { $schedule = $args['schedule'];
$args = array_merge( array($timestamp, $schedule, $hook), $args['args']); if ($schedule != false) {
call_user_func_array('wp_reschedule_event', $args); $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);
} }
} }
?> ?>

View File

@ -2,27 +2,30 @@
function wp_schedule_single_event( $timestamp, $hook ) { function wp_schedule_single_event( $timestamp, $hook ) {
$args = array_slice( func_get_args(), 2 ); $args = array_slice( func_get_args(), 2 );
$crons = get_option( 'cron' ); $crons = _get_cron_array();
$crons[$timestamp][$hook] = array( 'schedule' => false, 'args' => $args ); $key = md5(serialize($args));
$crons[$timestamp][$hook][$key] = array( 'schedule' => false, 'args' => $args );
ksort( $crons ); ksort( $crons );
update_option( 'cron', $crons ); _set_cron_array( $crons );
} }
function wp_schedule_event( $timestamp, $recurrence, $hook ) { function wp_schedule_event( $timestamp, $recurrence, $hook ) {
$args = array_slice( func_get_args(), 3 ); $args = array_slice( func_get_args(), 3 );
$crons = get_option( 'cron' ); $crons = _get_cron_array();
$schedules = wp_get_schedules(); $schedules = wp_get_schedules();
$key = md5(serialize($args));
if ( !isset( $schedules[$recurrence] ) ) if ( !isset( $schedules[$recurrence] ) )
return false; 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 ); ksort( $crons );
update_option( 'cron', $crons ); _set_cron_array( $crons );
} }
function wp_reschedule_event( $timestamp, $recurrence, $hook ) { function wp_reschedule_event( $timestamp, $recurrence, $hook ) {
$args = array_slice( func_get_args(), 3 ); $args = array_slice( func_get_args(), 3 );
$crons = get_option( 'cron' ); $crons = _get_cron_array();
$schedules = wp_get_schedules(); $schedules = wp_get_schedules();
$key = md5(serialize($args));
$interval = 0; $interval = 0;
// First we try to get it from the schedule // First we try to get it from the schedule
@ -30,7 +33,7 @@ function wp_reschedule_event( $timestamp, $recurrence, $hook ) {
$interval = $schedules[$recurrence]['interval']; $interval = $schedules[$recurrence]['interval'];
// Now we try to get it from the saved interval in case the schedule disappears // Now we try to get it from the saved interval in case the schedule disappears
if ( 0 == $interval ) if ( 0 == $interval )
$interval = $crons[$timestamp][$hook]['interval']; $interval = $crons[$timestamp][$hook][$key]['interval'];
// Now we assume something is wrong and fail to schedule // Now we assume something is wrong and fail to schedule
if ( 0 == $interval ) if ( 0 == $interval )
return false; return false;
@ -38,40 +41,41 @@ function wp_reschedule_event( $timestamp, $recurrence, $hook ) {
while ( $timestamp < time() + 1 ) while ( $timestamp < time() + 1 )
$timestamp += $interval; $timestamp += $interval;
wp_schedule_event( $timestamp, $recurrence, $hook ); wp_schedule_event( $timestamp, $recurrence, $hook, $args );
} }
function wp_unschedule_event( $timestamp, $hook ) { function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
$crons = get_option( 'cron' ); $crons = _get_cron_array();
unset( $crons[$timestamp][$hook] ); $key = md5(serialize($args));
unset( $crons[$timestamp][$hook][$key] );
if ( empty($crons[$timestamp][$hook]) )
unset( $crons[$timestamp][$hook] );
if ( empty($crons[$timestamp]) ) if ( empty($crons[$timestamp]) )
unset( $crons[$timestamp] ); unset( $crons[$timestamp] );
update_option( 'cron', $crons ); _set_cron_array( $crons );
} }
function wp_clear_scheduled_hook( $hook ) { function wp_clear_scheduled_hook( $hook ) {
$args = array_slice( func_get_args(), 1 ); $args = array_slice( func_get_args(), 1 );
while ( $timestamp = wp_next_scheduled( $hook, $args ) ) while ( $timestamp = wp_next_scheduled( $hook, $args ) )
wp_unschedule_event( $timestamp, $hook ); wp_unschedule_event( $timestamp, $hook, $args );
} }
function wp_next_scheduled( $hook, $args = '' ) { function wp_next_scheduled( $hook, $args = array() ) {
$crons = get_option( 'cron' ); $crons = _get_cron_array();
$key = md5(serialize($args));
if ( empty($crons) ) if ( empty($crons) )
return false; return false;
foreach ( $crons as $timestamp => $cron ) foreach ( $crons as $timestamp => $cron ) {
if ( isset( $cron[$hook] ) ) { if ( isset( $cron[$hook][$key] ) )
if ( empty($args) ) return $timestamp;
return $timestamp; }
if ( $args == $cron[$hook]['args'] )
return $timestamp;
}
return false; return false;
} }
function spawn_cron() { function spawn_cron() {
$crons = get_option( 'cron' ); $crons = _get_cron_array();
if ( !is_array($crons) ) if ( !is_array($crons) )
return; return;
@ -92,7 +96,7 @@ function spawn_cron() {
} }
function wp_cron() { function wp_cron() {
$crons = get_option( 'cron' ); $crons = _get_cron_array();
if ( !is_array($crons) ) if ( !is_array($crons) )
return; return;
@ -121,4 +125,44 @@ function wp_get_schedules() {
return array_merge( apply_filters( 'cron_schedules', array() ), $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;
}
?> ?>

View File

@ -698,7 +698,7 @@ function wp_insert_post($postarr = array()) {
// Schedule publication. // Schedule publication.
if ( 'future' == $post_status ) 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('save_post', $post_ID);
do_action('wp_insert_post', $post_ID); do_action('wp_insert_post', $post_ID);