]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-cron.php
WordPress 4.1
[autoinstalls/wordpress.git] / wp-cron.php
1 <?php
2 /**
3  * WordPress Cron Implementation for hosts, which do not offer CRON or for which
4  * the user has not set up a CRON job pointing to this file.
5  *
6  * The HTTP request to this file will not slow down the visitor who happens to
7  * visit when the cron job is needed to run.
8  *
9  * @package WordPress
10  */
11
12 ignore_user_abort(true);
13
14 if ( !empty($_POST) || defined('DOING_AJAX') || defined('DOING_CRON') )
15         die();
16
17 /**
18  * Tell WordPress we are doing the CRON task.
19  *
20  * @var bool
21  */
22 define('DOING_CRON', true);
23
24 if ( !defined('ABSPATH') ) {
25         /** Set up WordPress environment */
26         require_once( dirname( __FILE__ ) . '/wp-load.php' );
27 }
28
29 // Uncached doing_cron transient fetch
30 function _get_cron_lock() {
31         global $wpdb;
32
33         $value = 0;
34         if ( wp_using_ext_object_cache() ) {
35                 /*
36                  * Skip local cache and force re-fetch of doing_cron transient
37                  * in case another process updated the cache.
38                  */
39                 $value = wp_cache_get( 'doing_cron', 'transient', true );
40         } else {
41                 $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", '_transient_doing_cron' ) );
42                 if ( is_object( $row ) )
43                         $value = $row->option_value;
44         }
45
46         return $value;
47 }
48
49 if ( false === $crons = _get_cron_array() )
50         die();
51
52 $keys = array_keys( $crons );
53 $gmt_time = microtime( true );
54
55 if ( isset($keys[0]) && $keys[0] > $gmt_time )
56         die();
57
58 $doing_cron_transient = get_transient( 'doing_cron');
59
60 // Use global $doing_wp_cron lock otherwise use the GET lock. If no lock, trying grabbing a new lock.
61 if ( empty( $doing_wp_cron ) ) {
62         if ( empty( $_GET[ 'doing_wp_cron' ] ) ) {
63                 // Called from external script/job. Try setting a lock.
64                 if ( $doing_cron_transient && ( $doing_cron_transient + WP_CRON_LOCK_TIMEOUT > $gmt_time ) )
65                         return;
66                 $doing_cron_transient = $doing_wp_cron = sprintf( '%.22F', microtime( true ) );
67                 set_transient( 'doing_cron', $doing_wp_cron );
68         } else {
69                 $doing_wp_cron = $_GET[ 'doing_wp_cron' ];
70         }
71 }
72
73 // Check lock
74 if ( $doing_cron_transient != $doing_wp_cron )
75         return;
76
77 foreach ( $crons as $timestamp => $cronhooks ) {
78         if ( $timestamp > $gmt_time )
79                 break;
80
81         foreach ( $cronhooks as $hook => $keys ) {
82
83                 foreach ( $keys as $k => $v ) {
84
85                         $schedule = $v['schedule'];
86
87                         if ( $schedule != false ) {
88                                 $new_args = array($timestamp, $schedule, $hook, $v['args']);
89                                 call_user_func_array('wp_reschedule_event', $new_args);
90                         }
91
92                         wp_unschedule_event( $timestamp, $hook, $v['args'] );
93
94                         /**
95                          * Fires scheduled events.
96                          *
97                          * @internal
98                          * @since 2.1.0
99                          *
100                          * @param string $hook Name of the hook that was scheduled to be fired.
101                          * @param array  $args The arguments to be passed to the hook.
102                          */
103                         do_action_ref_array( $hook, $v['args'] );
104
105                         // If the hook ran too long and another cron process stole the lock, quit.
106                         if ( _get_cron_lock() != $doing_wp_cron )
107                                 return;
108                 }
109         }
110 }
111
112 if ( _get_cron_lock() == $doing_wp_cron )
113         delete_transient( 'doing_cron' );
114
115 die();