root/tags/2.1.1/wp-includes/cron.php

Revision 4850, 4.6 kB (checked in by markjaquith, 2 years ago)

Prevent a missing wp-cron.php from causing an infinite loop. Props jan. fixes #3713

  • Property svn:eol-style set to native
Line 
1 <?php
2
3 function wp_schedule_single_event( $timestamp, $hook, $args = array()) {
4     $crons = _get_cron_array();
5     $key = md5(serialize($args));
6     $crons[$timestamp][$hook][$key] = array( 'schedule' => false, 'args' => $args );
7     uksort( $crons, "strnatcasecmp" );
8     _set_cron_array( $crons );
9 }
10
11 function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array()) {
12     $crons = _get_cron_array();
13     $schedules = wp_get_schedules();
14     $key = md5(serialize($args));
15     if ( !isset( $schedules[$recurrence] ) )
16         return false;
17     $crons[$timestamp][$hook][$key] = array( 'schedule' => $recurrence, 'args' => $args, 'interval' => $schedules[$recurrence]['interval'] );
18     uksort( $crons, "strnatcasecmp" );
19     _set_cron_array( $crons );
20 }
21
22 function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array()) {
23     $crons = _get_cron_array();
24     $schedules = wp_get_schedules();
25     $key = md5(serialize($args));
26     $interval = 0;
27
28     // First we try to get it from the schedule
29     if ( 0 == $interval )
30         $interval = $schedules[$recurrence]['interval'];
31     // Now we try to get it from the saved interval in case the schedule disappears
32     if ( 0 == $interval )
33         $interval = $crons[$timestamp][$hook][$key]['interval'];
34     // Now we assume something is wrong and fail to schedule
35     if ( 0 == $interval )
36         return false;
37
38     while ( $timestamp < time() + 1 )
39         $timestamp += $interval;
40
41     wp_schedule_event( $timestamp, $recurrence, $hook, $args );
42 }
43
44 function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
45     $crons = _get_cron_array();
46     $key = md5(serialize($args));
47     unset( $crons[$timestamp][$hook][$key] );
48     if ( empty($crons[$timestamp][$hook]) )
49         unset( $crons[$timestamp][$hook] );
50     if ( empty($crons[$timestamp]) )
51         unset( $crons[$timestamp] );
52     _set_cron_array( $crons );
53 }
54
55 function wp_clear_scheduled_hook( $hook ) {
56     $args = array_slice( func_get_args(), 1 );
57     
58     while ( $timestamp = wp_next_scheduled( $hook, $args ) )
59         wp_unschedule_event( $timestamp, $hook, $args );
60 }
61
62 function wp_next_scheduled( $hook, $args = array() ) {
63     $crons = _get_cron_array();
64     $key = md5(serialize($args));
65     if ( empty($crons) )
66         return false;
67     foreach ( $crons as $timestamp => $cron ) {
68         if ( isset( $cron[$hook][$key] ) )
69             return $timestamp;
70     }
71     return false;
72 }
73
74 function spawn_cron() {
75     $crons = _get_cron_array();
76     
77     if ( !is_array($crons) )
78         return;
79     
80     $keys = array_keys( $crons );
81     if ( array_shift( $keys ) > time() )
82         return;
83
84     $cron_url = get_option( 'siteurl' ) . '/wp-cron.php';
85     $parts = parse_url( $cron_url );
86
87     $argyle = @ fsockopen( $parts['host'], $_SERVER['SERVER_PORT'], $errno, $errstr, 0.01 );
88     if ( $argyle )
89         fputs( $argyle,
90               "GET {$parts['path']}?check=" . md5(DB_PASS . '187425') . " HTTP/1.0\r\n"
91             . "Host: {$_SERVER['HTTP_HOST']}\r\n\r\n"
92         );
93 }
94
95 function wp_cron() {
96     // Prevent infinite loops caused by lack of wp-cron.php
97     if ( strpos($_SERVER['REQUEST_URI'], '/wp-cron.php') !== false )
98         return;
99
100     $crons = _get_cron_array();
101     
102     if ( !is_array($crons) )
103         return;
104
105     $keys = array_keys( $crons );
106     if ( isset($keys[0]) && $keys[0] > time() )
107         return;
108
109     $schedules = wp_get_schedules();
110     foreach ( $crons as $timestamp => $cronhooks ) {
111         if ( $timestamp > time() ) break;
112         foreach ( $cronhooks as $hook => $args ) {
113             if ( isset($schedules[$hook]['callback']) && !call_user_func( $schedules[$hook]['callback'] ) )
114                 continue;
115             spawn_cron();
116             break 2;
117         }
118     }
119 }
120
121 function wp_get_schedules() {
122     $schedules = array(
123         'hourly' => array( 'interval' => 3600, 'display' => __('Once Hourly') ),
124         'daily' => array( 'interval' => 86400, 'display' => __('Once Daily') ),
125     );
126     return array_merge( apply_filters( 'cron_schedules', array() ), $schedules );
127 }
128
129 function wp_get_schedule($hook, $args = array()) {
130     $crons = _get_cron_array();
131     $key = md5(serialize($args));
132     if ( empty($crons) )
133         return false;
134     foreach ( $crons as $timestamp => $cron ) {
135         if ( isset( $cron[$hook][$key] ) )
136             return $cron[$hook][$key]['schedule'];
137     }
138     return false;
139 }
140
141 //
142 // Private functions
143 //
144
145 function _get_cron_array()  {
146     $cron = get_option('cron');
147     if ( ! is_array($cron) )
148         return false;
149
150     if ( !isset($cron['version']) )
151         $cron = _upgrade_cron_array($cron);
152
153     unset($cron['version']);
154
155     return $cron;
156 }
157
158 function _set_cron_array($cron) {
159     $cron['version'] = 2;
160     update_option( 'cron', $cron );
161 }
162
163 function _upgrade_cron_array($cron) {
164     if ( isset($cron['version']) && 2 == $cron['version'])
165         return $cron;
166
167     $new_cron = array();
168
169     foreach ($cron as $timestamp => $hooks) {
170         foreach ( $hooks as $hook => $args ) {
171             $key = md5(serialize($args['args']));
172             $new_cron[$timestamp][$hook][$key] = $args;
173         }
174     }
175
176     $new_cron['version'] = 2;
177     update_option( 'cron', $new_cron );
178     return $new_cron;
179 }
180
181 ?>
Note: See TracBrowser for help on using the browser.