9 * Schedules a hook to run only once.
11 * Schedules a hook which will be executed once by the Wordpress actions core at
12 * a time which you specify. The action will fire off when someone visits your
13 * WordPress site, if the schedule time has passed.
16 * @link http://codex.wordpress.org/Function_Reference/wp_schedule_single_event
18 * @param int $timestamp Timestamp for when to run the event.
19 * @param string $hook Action hook to execute when cron is run.
20 * @param array $args Optional. Arguments to pass to the hook's callback function.
22 function wp_schedule_single_event( $timestamp, $hook, $args = array()) {
23 // don't schedule a duplicate if there's already an identical event due in the next 10 minutes
24 $next = wp_next_scheduled($hook, $args);
25 if ( $next && $next <= $timestamp + 600 )
28 $crons = _get_cron_array();
29 $key = md5(serialize($args));
30 $crons[$timestamp][$hook][$key] = array( 'schedule' => false, 'args' => $args );
31 uksort( $crons, "strnatcasecmp" );
32 _set_cron_array( $crons );
36 * Schedule a periodic event.
38 * Schedules a hook which will be executed by the WordPress actions core on a
39 * specific interval, specified by you. The action will trigger when someone
40 * visits your WordPress site, if the scheduled time has passed.
44 * @param int $timestamp Timestamp for when to run the event.
45 * @param string $recurrence How often the event should recur.
46 * @param string $hook Action hook to execute when cron is run.
47 * @param array $args Optional. Arguments to pass to the hook's callback function.
48 * @return bool|null False on failure, null when complete with scheduling event.
50 function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array()) {
51 $crons = _get_cron_array();
52 $schedules = wp_get_schedules();
53 $key = md5(serialize($args));
54 if ( !isset( $schedules[$recurrence] ) )
56 $crons[$timestamp][$hook][$key] = array( 'schedule' => $recurrence, 'args' => $args, 'interval' => $schedules[$recurrence]['interval'] );
57 uksort( $crons, "strnatcasecmp" );
58 _set_cron_array( $crons );
62 * Reschedule a recurring event.
66 * @param int $timestamp Timestamp for when to run the event.
67 * @param string $recurrence How often the event should recur.
68 * @param string $hook Action hook to execute when cron is run.
69 * @param array $args Optional. Arguments to pass to the hook's callback function.
70 * @return bool|null False on failure. Null when event is rescheduled.
72 function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array()) {
73 $crons = _get_cron_array();
74 $schedules = wp_get_schedules();
75 $key = md5(serialize($args));
78 // First we try to get it from the schedule
80 $interval = $schedules[$recurrence]['interval'];
81 // Now we try to get it from the saved interval in case the schedule disappears
83 $interval = $crons[$timestamp][$hook][$key]['interval'];
84 // Now we assume something is wrong and fail to schedule
88 while ( $timestamp < time() + 1 )
89 $timestamp += $interval;
91 wp_schedule_event( $timestamp, $recurrence, $hook, $args );
95 * Unschedule a previously scheduled cron job.
97 * The $timestamp and $hook parameters are required, so that the event can be
102 * @param int $timestamp Timestamp for when to run the event.
103 * @param string $hook Action hook, the execution of which will be unscheduled.
104 * @param array $args Arguments to pass to the hook's callback function.
105 * Although not passed to a callback function, these arguments are used
106 * to uniquely identify the scheduled event, so they should be the same
107 * as those used when originally scheduling the event.
109 function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
110 $crons = _get_cron_array();
111 $key = md5(serialize($args));
112 unset( $crons[$timestamp][$hook][$key] );
113 if ( empty($crons[$timestamp][$hook]) )
114 unset( $crons[$timestamp][$hook] );
115 if ( empty($crons[$timestamp]) )
116 unset( $crons[$timestamp] );
117 _set_cron_array( $crons );
121 * Unschedule all cron jobs attached to a specific hook.
125 * @param string $hook Action hook, the execution of which will be unscheduled.
126 * @param mixed $args,... Optional. Event arguments.
128 function wp_clear_scheduled_hook( $hook ) {
129 $args = array_slice( func_get_args(), 1 );
131 while ( $timestamp = wp_next_scheduled( $hook, $args ) )
132 wp_unschedule_event( $timestamp, $hook, $args );
136 * Retrieve the next timestamp for a cron event.
140 * @param string $hook Action hook to execute when cron is run.
141 * @param array $args Optional. Arguments to pass to the hook's callback function.
142 * @return bool|int The UNIX timestamp of the next time the scheduled event will occur.
144 function wp_next_scheduled( $hook, $args = array() ) {
145 $crons = _get_cron_array();
146 $key = md5(serialize($args));
149 foreach ( $crons as $timestamp => $cron ) {
150 if ( isset( $cron[$hook][$key] ) )
157 * Send request to run cron through HTTP request that doesn't halt page loading.
161 * @return null Cron could not be spawned, because it is not needed to run.
163 function spawn_cron( $local_time ) {
166 * do not even start the cron if local server timer has drifted
167 * such as due to power failure, or misconfiguration
169 $timer_accurate = check_server_timer( $local_time );
170 if ( !$timer_accurate )
174 $crons = _get_cron_array();
175 if ( !is_array($crons) )
178 $keys = array_keys( $crons );
179 $timestamp = $keys[0];
180 if ( $timestamp > $local_time )
183 $cron_url = get_option( 'siteurl' ) . '/wp-cron.php?check=' . wp_hash('187425');
185 * multiple processes on multiple web servers can run this code concurrently
186 * try to make this as atomic as possible by setting doing_cron switch
188 $flag = get_option('doing_cron');
190 // clean up potential invalid value resulted from various system chaos
192 if ( $flag > $local_time + 10*60 || $flag < $local_time - 10*60 ) {
193 update_option('doing_cron', 0);
198 //don't run if another process is currently running it
199 if ( $flag > $local_time )
202 update_option( 'doing_cron', $local_time + 30 );
204 wp_remote_post($cron_url, array('timeout' => 0.01, 'blocking' => false));
208 * Run scheduled callbacks or spawn cron for all scheduled events.
212 * @return null When doesn't need to run Cron.
216 // Prevent infinite loops caused by lack of wp-cron.php
217 if ( strpos($_SERVER['REQUEST_URI'], '/wp-cron.php') !== false )
220 $crons = _get_cron_array();
222 if ( !is_array($crons) )
225 $keys = array_keys( $crons );
226 if ( isset($keys[0]) && $keys[0] > time() )
229 $local_time = time();
230 $schedules = wp_get_schedules();
231 foreach ( $crons as $timestamp => $cronhooks ) {
232 if ( $timestamp > $local_time ) break;
233 foreach ( (array) $cronhooks as $hook => $args ) {
234 if ( isset($schedules[$hook]['callback']) && !call_user_func( $schedules[$hook]['callback'] ) )
236 spawn_cron( $local_time );
243 * Retrieve supported and filtered Cron recurrences.
245 * The supported recurrences are 'hourly' and 'daily'. A plugin may add more by
246 * hooking into the 'cron_schedules' filter. The filter accepts an array of
247 * arrays. The outer array has a key that is the name of the schedule or for
248 * example 'weekly'. The value is an array with two keys, one is 'interval' and
249 * the other is 'display'.
251 * The 'interval' is a number in seconds of when the cron job should run. So for
252 * 'hourly', the time is 3600 or 60*60. For weekly, the value would be
253 * 60*60*24*7 or 604800. The value of 'interval' would then be 604800.
255 * The 'display' is the description. For the 'weekly' key, the 'display' would
256 * be <code>__('Once Weekly')</code>.
258 * For your plugin, you will be passed an array. you can easily add your
259 * schedule by doing the following.
261 * // filter parameter variable name is 'array'
262 * $array['weekly'] = array(
263 * 'interval' => 604800,
264 * 'display' => __('Once Weekly')
272 function wp_get_schedules() {
274 'hourly' => array( 'interval' => 3600, 'display' => __('Once Hourly') ),
275 'twicedaily' => array( 'interval' => 43200, 'display' => __('Twice Daily') ),
276 'daily' => array( 'interval' => 86400, 'display' => __('Once Daily') ),
278 return array_merge( apply_filters( 'cron_schedules', array() ), $schedules );
282 * Retrieve Cron schedule for hook with arguments.
286 * @param string $hook Action hook to execute when cron is run.
287 * @param array $args Optional. Arguments to pass to the hook's callback function.
288 * @return string|bool False, if no schedule. Schedule on success.
290 function wp_get_schedule($hook, $args = array()) {
291 $crons = _get_cron_array();
292 $key = md5(serialize($args));
295 foreach ( $crons as $timestamp => $cron ) {
296 if ( isset( $cron[$hook][$key] ) )
297 return $cron[$hook][$key]['schedule'];
307 * Retrieve cron info array option.
312 * @return array CRON info array.
314 function _get_cron_array() {
315 $cron = get_option('cron');
316 if ( ! is_array($cron) )
319 if ( !isset($cron['version']) )
320 $cron = _upgrade_cron_array($cron);
322 unset($cron['version']);
328 * Updates the CRON option with the new CRON array.
333 * @param array $cron Cron info array from {@link _get_cron_array()}.
335 function _set_cron_array($cron) {
336 $cron['version'] = 2;
337 update_option( 'cron', $cron );
341 * Upgrade a Cron info array.
343 * This function upgrades the Cron info array to version 2.
348 * @param array $cron Cron info array from {@link _get_cron_array()}.
349 * @return array An upgraded Cron info array.
351 function _upgrade_cron_array($cron) {
352 if ( isset($cron['version']) && 2 == $cron['version'])
357 foreach ( (array) $cron as $timestamp => $hooks) {
358 foreach ( (array) $hooks as $hook => $args ) {
359 $key = md5(serialize($args['args']));
360 $new_cron[$timestamp][$hook][$key] = $args;
364 $new_cron['version'] = 2;
365 update_option( 'cron', $new_cron );
369 // stub for checking server timer accuracy, using outside standard time sources
370 function check_server_timer( $local_time ) {