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') )
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);
}
}
?>

View File

@ -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;
}
?>

View File

@ -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);