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