Skip to content

Commit 457b4b5

Browse files
committed
Cron improvements from masquerade. WordPress#2425
git-svn-id: https://develop.svn.wordpress.org/trunk@3634 602fd350-edb4-49c9-b593-d223f7449a82
1 parent 20c6baa commit 457b4b5

5 files changed

Lines changed: 136 additions & 69 deletions

File tree

wp-cron.php

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
<?php
2+
ignore_user_abort(true);
3+
define('DOING_CRON', TRUE);
4+
require_once('wp-config.php');
5+
6+
$crons = get_option('cron');
7+
if (!is_array($crons) || array_shift(array_keys($crons)) > time())
8+
return;
9+
foreach ($crons as $timestamp => $cronhooks) {
10+
if ($timestamp > time()) break;
11+
foreach($cronhooks as $hook => $args) {
12+
do_action($hook, $args['args']);
13+
$schedule = $args['schedule'];
14+
if($schedule != false) {
15+
fwrite($fp, var_export($schedules[$schedule]));
16+
$args = array_merge( array($timestamp, $schedule, $hook), $args['args']);
17+
call_user_func_array('wp_reschedule_event', $args);
18+
}
19+
wp_unschedule_event($timestamp, $hook);
20+
}
21+
}
22+
?>

wp-includes/cron.php

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
<?php
2+
function wp_schedule_single_event($timestamp, $hook) {
3+
$args = array_slice(func_get_args(), 2);
4+
$crons = get_option('cron');
5+
$crons[$timestamp][$hook] = array('schedule' => false, 'args' => $args);
6+
ksort($crons);
7+
update_option('cron', $crons);
8+
}
9+
function wp_schedule_new_event($timestamp, $recurrence, $hook) {
10+
$args = array_slice(func_get_args(), 3);
11+
$crons = get_option('cron');
12+
$schedules = wp_get_schedules();
13+
if(!isset($schedules[$recurrence]))
14+
return false;
15+
$crons[$timestamp][$hook] = array('schedule' => $recurrence, 'args' => $args, 'interval' => $schedules[$recurrence]['interval']);
16+
ksort($crons);
17+
update_option('cron', $crons);
18+
}
19+
20+
21+
function wp_reschedule_event($timestamp, $recurrence, $hook) {
22+
$args = array_slice(func_get_args(), 3);
23+
$crons = get_option('cron');
24+
$schedules = wp_get_schedules();
25+
$interval = 0;
26+
27+
// First we try to get it from the schedule
28+
if( 0 == $interval )
29+
$interval = $schedules[$recurrence]['interval'];
30+
// Now we try to get it from the saved interval in case the schedule disappears
31+
if( 0 == $interval )
32+
$interval = $crons[$timestamp][$hook]['interval'];
33+
// Now we assume something is wrong and fail to schedule
34+
if( 0 == $interval )
35+
return false;
36+
37+
while($timestamp < time() + 1) {
38+
$timestamp += $interval;
39+
}
40+
wp_schedule_new_event($timestamp, $recurrence, $hook);
41+
}
42+
43+
function wp_unschedule_event($timestamp, $hook) {
44+
$crons = get_option('cron');
45+
unset($crons[$timestamp][$hook]);
46+
if ( empty($crons[$timestamp]) )
47+
unset($crons[$timestamp]);
48+
update_option('cron', $crons);
49+
}
50+
51+
function wp_clear_scheduled_hook($hook) {
52+
while($timestamp = next_scheduled($hook))
53+
wp_unschedule_event($timestamp, $hook);
54+
}
55+
56+
function wp_next_scheduled($hook) {
57+
$crons = get_option('cron');
58+
if ( empty($crons) )
59+
return false;
60+
foreach($crons as $timestamp => $cron) {
61+
//if($timestamp <= time()) continue;
62+
if(isset($cron[$hook])) return $timestamp;
63+
}
64+
return false;
65+
}
66+
67+
function spawn_cron() {
68+
if (array_shift(array_keys(get_option('cron'))) > time()) return;
69+
70+
//Since execute pings had CGI problems, but I'd like to test this without this code first
71+
// It seems to be working on CGI here, please report if you have issues
72+
/* if ( substr(php_sapi_name(), 0, 3) == 'cgi' ) {
73+
echo '<iframe src="' . $cron_url . '"></iframe>';
74+
}*/
75+
76+
$cron_url = get_settings('siteurl') . '/wp-cron.php';
77+
$parts = parse_url($cron_url);
78+
79+
$argyle = @ fsockopen($parts['host'], $_SERVER['SERVER_PORT'], $errno, $errstr, 0.01);
80+
if ( $argyle )
81+
fputs($argyle, "GET {$parts['path']}?time=" . time() . '&check='
82+
. md5(DB_PASS . '187425') . " HTTP/1.0\r\nHost: {$_SERVER['HTTP_HOST']}\r\n\r\n");
83+
}
84+
85+
function wp_cron() {
86+
$crons = get_option('cron');
87+
if (!is_array($crons) || array_shift(array_keys($crons)) > time())
88+
return;
89+
90+
$schedules = wp_get_schedules();
91+
foreach ($crons as $timestamp => $cronhooks) {
92+
if ($timestamp > time()) break;
93+
foreach($cronhooks as $hook => $args) {
94+
if(isset($schedules[$hook]['callback']) && !call_user_func($schedules[$hook]['callback']))
95+
continue;
96+
spawn_cron();
97+
break 2;
98+
}
99+
}
100+
}
101+
102+
function wp_get_schedules() {
103+
$schedules = array(
104+
'hourly' => array('interval' => 3600, 'display' => __('Once Hourly')),
105+
'daily' => array('interval' => 86400, 'display' => __('Once Daily')),
106+
);
107+
return array_merge(apply_filters('cron_schedules', array()), $schedules);
108+
}
109+
?>

wp-includes/functions-post.php

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -210,9 +210,10 @@ function wp_insert_post($postarr = array()) {
210210
add_post_meta($post_ID, '_wp_page_template', $page_template, true);
211211
}
212212

213-
if ( 'future' == $post_status )
214-
wp_schedule_event(mysql2date('U', $post_date), 'once', 'publish_future_post', $post_ID);
215-
213+
if ( 'future' == $post_status ) {
214+
wp_schedule_single_event(mysql2date('U', $post_date), 'publish_future_post', $post_ID);
215+
}
216+
216217
do_action('save_post', $post_ID);
217218
do_action('wp_insert_post', $post_ID);
218219

wp-includes/functions.php

Lines changed: 0 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -2423,72 +2423,6 @@ function get_num_queries() {
24232423
return $wpdb->num_queries;
24242424
}
24252425

2426-
function wp_schedule_event($timestamp, $recurrence, $hook) {
2427-
$args = array_slice(func_get_args(), 3);
2428-
$crons = get_option('cron');
2429-
$crons[$timestamp][$hook] = array('recur' => $recurrence, 'args' => $args);
2430-
ksort($crons);
2431-
update_option('cron', $crons);
2432-
}
2433-
2434-
function wp_unschedule_event($timestamp, $hook) {
2435-
$crons = get_option('cron');
2436-
unset($crons[$timestamp][$hook]);
2437-
if ( empty($crons[$timestamp]) )
2438-
unset($crons[$timestamp]);
2439-
update_option('cron', $crons);
2440-
}
2441-
2442-
function wp_clear_scheduled_hook($hook) {
2443-
while($timestamp = next_scheduled('scheduled_hook'))
2444-
wp_unschedule_event($timestamp, 'scheduled_hook');
2445-
}
2446-
2447-
function next_scheduled($hook) {
2448-
$crons = get_option('cron');
2449-
if ( empty($crons) )
2450-
return false;
2451-
foreach($crons as $timestamp => $cron) {
2452-
//if($timestamp <= time()) continue;
2453-
if(isset($cron[$hook])) return $timestamp;
2454-
}
2455-
return false;
2456-
}
2457-
2458-
function spawn_cron() {
2459-
if (array_shift(array_keys(get_option('cron'))) > time()) return;
2460-
2461-
$cron_url = get_settings('siteurl') . '/wp-cron.php';
2462-
$parts = parse_url($cron_url);
2463-
$argyle = @ fsockopen($parts['host'], $_SERVER['SERVER_PORT'], $errno, $errstr, 0.01);
2464-
if ( $argyle )
2465-
fputs($argyle, "GET {$parts['path']}?time=" . time() . '&check='
2466-
. md5(DB_PASS . '187425') . " HTTP/1.0\r\nHost: {$_SERVER['HTTP_HOST']}\r\n\r\n");
2467-
}
2468-
2469-
function wp_cron() {
2470-
$crons = get_option('cron');
2471-
if (!is_array($crons) || array_shift(array_keys($crons)) > time())
2472-
return;
2473-
2474-
foreach ($crons as $timestamp => $cronhooks) {
2475-
if ($timestamp > current_time( 'timestamp' )) break;
2476-
foreach($cronhooks as $hook => $args) {
2477-
do_action($hook, $args['args']);
2478-
$recurrence = $args['recur'];
2479-
if ( 'hourly' == $recurrence ) {
2480-
$args = array_merge( array($timestamp + 3600, $recurrence, $hook), $args['args']);
2481-
call_user_func_array('wp_schedule_event', $args);
2482-
} else if ( 'daily' == $recurrence ) {
2483-
$args = array_merge( array($timestamp + 86400, $recurrence, $hook), $args['args']);
2484-
call_user_func_array('wp_schedule_event', $args);
2485-
}
2486-
2487-
wp_unschedule_event($timestamp, $hook);
2488-
}
2489-
}
2490-
}
2491-
24922426
function privacy_ping_filter( $sites ) {
24932427
if ( get_option('blog_public') )
24942428
return $sites;

wp-settings.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -143,6 +143,7 @@ function timer_start() {
143143
require (ABSPATH . WPINC . '/feed-functions.php');
144144
require (ABSPATH . WPINC . '/template-functions-bookmarks.php');
145145
require (ABSPATH . WPINC . '/kses.php');
146+
require (ABSPATH . WPINC . '/cron.php');
146147
require (ABSPATH . WPINC . '/version.php');
147148
require (ABSPATH . WPINC . '/deprecated.php');
148149

0 commit comments

Comments
 (0)