]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-includes/cron.php
WordPress 4.2
[autoinstalls/wordpress.git] / wp-includes / cron.php
1 <?php
2 /**
3  * WordPress CRON API
4  *
5  * @package WordPress
6  */
7
8 /**
9  * Schedules a hook to run only once.
10  *
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.
14  *
15  * @since 2.1.0
16  * @link https://codex.wordpress.org/Function_Reference/wp_schedule_single_event
17  *
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.
21  */
22 function wp_schedule_single_event( $timestamp, $hook, $args = array()) {
23         // don't schedule a duplicate if there's already an identical event due within 10 minutes of it
24         $next = wp_next_scheduled($hook, $args);
25         if ( $next && abs( $next - $timestamp ) <= 10 * MINUTE_IN_SECONDS ) {
26                 return;
27         }
28
29         $crons = _get_cron_array();
30         $event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => false, 'args' => $args );
31         /**
32          * Filter a single event before it is scheduled.
33          *
34          * @since 3.1.0
35          *
36          * @param object $event An object containing an event's data.
37          */
38         $event = apply_filters( 'schedule_event', $event );
39
40         // A plugin disallowed this event
41         if ( ! $event )
42                 return false;
43
44         $key = md5(serialize($event->args));
45
46         $crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args );
47         uksort( $crons, "strnatcasecmp" );
48         _set_cron_array( $crons );
49 }
50
51 /**
52  * Schedule a periodic event.
53  *
54  * Schedules a hook which will be executed by the WordPress actions core on a
55  * specific interval, specified by you. The action will trigger when someone
56  * visits your WordPress site, if the scheduled time has passed.
57  *
58  * Valid values for the recurrence are hourly, daily and twicedaily. These can
59  * be extended using the cron_schedules filter in wp_get_schedules().
60  *
61  * Use wp_next_scheduled() to prevent duplicates
62  *
63  * @since 2.1.0
64  *
65  * @param int $timestamp Timestamp for when to run the event.
66  * @param string $recurrence How often the event should recur.
67  * @param string $hook Action hook to execute when cron is run.
68  * @param array $args Optional. Arguments to pass to the hook's callback function.
69  * @return false|null False on failure, null when complete with scheduling event.
70  */
71 function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array()) {
72         $crons = _get_cron_array();
73         $schedules = wp_get_schedules();
74
75         if ( !isset( $schedules[$recurrence] ) )
76                 return false;
77
78         $event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => $recurrence, 'args' => $args, 'interval' => $schedules[$recurrence]['interval'] );
79         /** This filter is documented in wp-includes/cron.php */
80         $event = apply_filters( 'schedule_event', $event );
81
82         // A plugin disallowed this event
83         if ( ! $event )
84                 return false;
85
86         $key = md5(serialize($event->args));
87
88         $crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args, 'interval' => $event->interval );
89         uksort( $crons, "strnatcasecmp" );
90         _set_cron_array( $crons );
91 }
92
93 /**
94  * Reschedule a recurring event.
95  *
96  * @since 2.1.0
97  *
98  * @param int $timestamp Timestamp for when to run the event.
99  * @param string $recurrence How often the event should recur.
100  * @param string $hook Action hook to execute when cron is run.
101  * @param array $args Optional. Arguments to pass to the hook's callback function.
102  * @return false|null False on failure. Null when event is rescheduled.
103  */
104 function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array() ) {
105         $crons = _get_cron_array();
106         $schedules = wp_get_schedules();
107         $key = md5( serialize( $args ) );
108         $interval = 0;
109
110         // First we try to get it from the schedule
111         if ( isset( $schedules[ $recurrence ] ) ) {
112                 $interval = $schedules[ $recurrence ]['interval'];
113         }
114         // Now we try to get it from the saved interval in case the schedule disappears
115         if ( 0 == $interval ) {
116                 $interval = $crons[ $timestamp ][ $hook ][ $key ]['interval'];
117         }
118         // Now we assume something is wrong and fail to schedule
119         if ( 0 == $interval ) {
120                 return false;
121         }
122
123         $now = time();
124
125         if ( $timestamp >= $now ) {
126                 $timestamp = $now + $interval;
127         } else {
128                 $timestamp = $now + ( $interval - ( ( $now - $timestamp ) % $interval ) );
129         }
130
131         wp_schedule_event( $timestamp, $recurrence, $hook, $args );
132 }
133
134 /**
135  * Unschedule a previously scheduled cron job.
136  *
137  * The $timestamp and $hook parameters are required, so that the event can be
138  * identified.
139  *
140  * @since 2.1.0
141  *
142  * @param int $timestamp Timestamp for when to run the event.
143  * @param string $hook Action hook, the execution of which will be unscheduled.
144  * @param array $args Arguments to pass to the hook's callback function.
145  * Although not passed to a callback function, these arguments are used
146  * to uniquely identify the scheduled event, so they should be the same
147  * as those used when originally scheduling the event.
148  */
149 function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
150         $crons = _get_cron_array();
151         $key = md5(serialize($args));
152         unset( $crons[$timestamp][$hook][$key] );
153         if ( empty($crons[$timestamp][$hook]) )
154                 unset( $crons[$timestamp][$hook] );
155         if ( empty($crons[$timestamp]) )
156                 unset( $crons[$timestamp] );
157         _set_cron_array( $crons );
158 }
159
160 /**
161  * Unschedule all cron jobs attached to a specific hook.
162  *
163  * @since 2.1.0
164  *
165  * @param string $hook Action hook, the execution of which will be unscheduled.
166  * @param array $args Optional. Arguments that were to be pass to the hook's callback function.
167  */
168 function wp_clear_scheduled_hook( $hook, $args = array() ) {
169         // Backward compatibility
170         // Previously this function took the arguments as discrete vars rather than an array like the rest of the API
171         if ( !is_array($args) ) {
172                 _deprecated_argument( __FUNCTION__, '3.0', __('This argument has changed to an array to match the behavior of the other cron functions.') );
173                 $args = array_slice( func_get_args(), 1 );
174         }
175
176         // This logic duplicates wp_next_scheduled()
177         // It's required due to a scenario where wp_unschedule_event() fails due to update_option() failing,
178         // and, wp_next_scheduled() returns the same schedule in an infinite loop.
179         $crons = _get_cron_array();
180         if ( empty( $crons ) )
181                 return;
182
183         $key = md5( serialize( $args ) );
184         foreach ( $crons as $timestamp => $cron ) {
185                 if ( isset( $cron[ $hook ][ $key ] ) ) {
186                         wp_unschedule_event( $timestamp, $hook, $args );
187                 }
188         }
189 }
190
191 /**
192  * Retrieve the next timestamp for a cron event.
193  *
194  * @since 2.1.0
195  *
196  * @param string $hook Action hook to execute when cron is run.
197  * @param array $args Optional. Arguments to pass to the hook's callback function.
198  * @return bool|int The UNIX timestamp of the next time the scheduled event will occur.
199  */
200 function wp_next_scheduled( $hook, $args = array() ) {
201         $crons = _get_cron_array();
202         $key = md5(serialize($args));
203         if ( empty($crons) )
204                 return false;
205         foreach ( $crons as $timestamp => $cron ) {
206                 if ( isset( $cron[$hook][$key] ) )
207                         return $timestamp;
208         }
209         return false;
210 }
211
212 /**
213  * Send request to run cron through HTTP request that doesn't halt page loading.
214  *
215  * @since 2.1.0
216  *
217  * @return null Cron could not be spawned, because it is not needed to run.
218  */
219 function spawn_cron( $gmt_time = 0 ) {
220
221         if ( ! $gmt_time )
222                 $gmt_time = microtime( true );
223
224         if ( defined('DOING_CRON') || isset($_GET['doing_wp_cron']) )
225                 return;
226
227         /*
228         * multiple processes on multiple web servers can run this code concurrently
229         * try to make this as atomic as possible by setting doing_cron switch
230         */
231         $lock = get_transient('doing_cron');
232
233         if ( $lock > $gmt_time + 10 * MINUTE_IN_SECONDS )
234                 $lock = 0;
235
236         // don't run if another process is currently running it or more than once every 60 sec.
237         if ( $lock + WP_CRON_LOCK_TIMEOUT > $gmt_time )
238                 return;
239
240         //sanity check
241         $crons = _get_cron_array();
242         if ( !is_array($crons) )
243                 return;
244
245         $keys = array_keys( $crons );
246         if ( isset($keys[0]) && $keys[0] > $gmt_time )
247                 return;
248
249         if ( defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) {
250                 if ( ! empty( $_POST ) || defined( 'DOING_AJAX' ) ||  defined( 'XMLRPC_REQUEST' ) ) {
251                         return;
252                 }
253
254                 $doing_wp_cron = sprintf( '%.22F', $gmt_time );
255                 set_transient( 'doing_cron', $doing_wp_cron );
256
257                 ob_start();
258                 wp_redirect( add_query_arg( 'doing_wp_cron', $doing_wp_cron, wp_unslash( $_SERVER['REQUEST_URI'] ) ) );
259                 echo ' ';
260
261                 // flush any buffers and send the headers
262                 while ( @ob_end_flush() );
263                 flush();
264
265                 WP_DEBUG ? include_once( ABSPATH . 'wp-cron.php' ) : @include_once( ABSPATH . 'wp-cron.php' );
266                 return;
267         }
268
269         $doing_wp_cron = sprintf( '%.22F', $gmt_time );
270         set_transient( 'doing_cron', $doing_wp_cron );
271
272         /**
273          * Filter the cron request arguments.
274          *
275          * @since 3.5.0
276          *
277          * @param array $cron_request_array {
278          *     An array of cron request URL arguments.
279          *
280          *     @type string $url  The cron request URL.
281          *     @type int    $key  The 22 digit GMT microtime.
282          *     @type array  $args {
283          *         An array of cron request arguments.
284          *
285          *         @type int  $timeout   The request timeout in seconds. Default .01 seconds.
286          *         @type bool $blocking  Whether to set blocking for the request. Default false.
287          *         @type bool $sslverify Whether SSL should be verified for the request. Default false.
288          *     }
289          * }
290          */
291         $cron_request = apply_filters( 'cron_request', array(
292                 'url'  => add_query_arg( 'doing_wp_cron', $doing_wp_cron, site_url( 'wp-cron.php' ) ),
293                 'key'  => $doing_wp_cron,
294                 'args' => array(
295                         'timeout'   => 0.01,
296                         'blocking'  => false,
297                         /** This filter is documented in wp-includes/class-http.php */
298                         'sslverify' => apply_filters( 'https_local_ssl_verify', false )
299                 )
300         ) );
301
302         wp_remote_post( $cron_request['url'], $cron_request['args'] );
303 }
304
305 /**
306  * Run scheduled callbacks or spawn cron for all scheduled events.
307  *
308  * @since 2.1.0
309  *
310  * @return null When doesn't need to run Cron.
311  */
312 function wp_cron() {
313
314         // Prevent infinite loops caused by lack of wp-cron.php
315         if ( strpos($_SERVER['REQUEST_URI'], '/wp-cron.php') !== false || ( defined('DISABLE_WP_CRON') && DISABLE_WP_CRON ) )
316                 return;
317
318         if ( false === $crons = _get_cron_array() )
319                 return;
320
321         $gmt_time = microtime( true );
322         $keys = array_keys( $crons );
323         if ( isset($keys[0]) && $keys[0] > $gmt_time )
324                 return;
325
326         $schedules = wp_get_schedules();
327         foreach ( $crons as $timestamp => $cronhooks ) {
328                 if ( $timestamp > $gmt_time ) break;
329                 foreach ( (array) $cronhooks as $hook => $args ) {
330                         if ( isset($schedules[$hook]['callback']) && !call_user_func( $schedules[$hook]['callback'] ) )
331                                 continue;
332                         spawn_cron( $gmt_time );
333                         break 2;
334                 }
335         }
336 }
337
338 /**
339  * Retrieve supported and filtered Cron recurrences.
340  *
341  * The supported recurrences are 'hourly' and 'daily'. A plugin may add more by
342  * hooking into the 'cron_schedules' filter. The filter accepts an array of
343  * arrays. The outer array has a key that is the name of the schedule or for
344  * example 'weekly'. The value is an array with two keys, one is 'interval' and
345  * the other is 'display'.
346  *
347  * The 'interval' is a number in seconds of when the cron job should run. So for
348  * 'hourly', the time is 3600 or 60*60. For weekly, the value would be
349  * 60*60*24*7 or 604800. The value of 'interval' would then be 604800.
350  *
351  * The 'display' is the description. For the 'weekly' key, the 'display' would
352  * be `__( 'Once Weekly' )`.
353  *
354  * For your plugin, you will be passed an array. you can easily add your
355  * schedule by doing the following.
356  *
357  *     // Filter parameter variable name is 'array'.
358  *     $array['weekly'] = array(
359  *         'interval' => 604800,
360  *         'display'  => __( 'Once Weekly' )
361  *     );
362  *
363  *
364  * @since 2.1.0
365  *
366  * @return array
367  */
368 function wp_get_schedules() {
369         $schedules = array(
370                 'hourly'     => array( 'interval' => HOUR_IN_SECONDS,      'display' => __( 'Once Hourly' ) ),
371                 'twicedaily' => array( 'interval' => 12 * HOUR_IN_SECONDS, 'display' => __( 'Twice Daily' ) ),
372                 'daily'      => array( 'interval' => DAY_IN_SECONDS,       'display' => __( 'Once Daily' ) ),
373         );
374         /**
375          * Filter the non-default cron schedules.
376          *
377          * @since 2.1.0
378          *
379          * @param array $new_schedules An array of non-default cron schedules. Default empty.
380          */
381         return array_merge( apply_filters( 'cron_schedules', array() ), $schedules );
382 }
383
384 /**
385  * Retrieve Cron schedule for hook with arguments.
386  *
387  * @since 2.1.0
388  *
389  * @param string $hook Action hook to execute when cron is run.
390  * @param array $args Optional. Arguments to pass to the hook's callback function.
391  * @return string|bool False, if no schedule. Schedule on success.
392  */
393 function wp_get_schedule($hook, $args = array()) {
394         $crons = _get_cron_array();
395         $key = md5(serialize($args));
396         if ( empty($crons) )
397                 return false;
398         foreach ( $crons as $timestamp => $cron ) {
399                 if ( isset( $cron[$hook][$key] ) )
400                         return $cron[$hook][$key]['schedule'];
401         }
402         return false;
403 }
404
405 //
406 // Private functions
407 //
408
409 /**
410  * Retrieve cron info array option.
411  *
412  * @since 2.1.0
413  * @access private
414  *
415  * @return array CRON info array.
416  */
417 function _get_cron_array()  {
418         $cron = get_option('cron');
419         if ( ! is_array($cron) )
420                 return false;
421
422         if ( !isset($cron['version']) )
423                 $cron = _upgrade_cron_array($cron);
424
425         unset($cron['version']);
426
427         return $cron;
428 }
429
430 /**
431  * Updates the CRON option with the new CRON array.
432  *
433  * @since 2.1.0
434  * @access private
435  *
436  * @param array $cron Cron info array from {@link _get_cron_array()}.
437  */
438 function _set_cron_array($cron) {
439         $cron['version'] = 2;
440         update_option( 'cron', $cron );
441 }
442
443 /**
444  * Upgrade a Cron info array.
445  *
446  * This function upgrades the Cron info array to version 2.
447  *
448  * @since 2.1.0
449  * @access private
450  *
451  * @param array $cron Cron info array from {@link _get_cron_array()}.
452  * @return array An upgraded Cron info array.
453  */
454 function _upgrade_cron_array($cron) {
455         if ( isset($cron['version']) && 2 == $cron['version'])
456                 return $cron;
457
458         $new_cron = array();
459
460         foreach ( (array) $cron as $timestamp => $hooks) {
461                 foreach ( (array) $hooks as $hook => $args ) {
462                         $key = md5(serialize($args['args']));
463                         $new_cron[$timestamp][$hook][$key] = $args;
464                 }
465         }
466
467         $new_cron['version'] = 2;
468         update_option( 'cron', $new_cron );
469         return $new_cron;
470 }