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.
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.
12 ignore_user_abort(true);
14 if ( !empty($_POST) || defined('DOING_AJAX') || defined('DOING_CRON') )
18 * Tell WordPress we are doing the CRON task.
22 define('DOING_CRON', true);
24 if ( !defined('ABSPATH') ) {
25 /** Set up WordPress environment */
26 require_once( dirname( __FILE__ ) . '/wp-load.php' );
29 // Uncached doing_cron transient fetch
30 function _get_cron_lock() {
34 if ( wp_using_ext_object_cache() ) {
35 // Skip local cache and force refetch of doing_cron transient in case
36 // another processs updated the cache
37 $value = wp_cache_get( 'doing_cron', 'transient', true );
39 $row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", '_transient_doing_cron' ) );
40 if ( is_object( $row ) )
41 $value = $row->option_value;
47 if ( false === $crons = _get_cron_array() )
50 $keys = array_keys( $crons );
51 $gmt_time = microtime( true );
53 if ( isset($keys[0]) && $keys[0] > $gmt_time )
56 $doing_cron_transient = get_transient( 'doing_cron');
58 // Use global $doing_wp_cron lock otherwise use the GET lock. If no lock, trying grabbing a new lock.
59 if ( empty( $doing_wp_cron ) ) {
60 if ( empty( $_GET[ 'doing_wp_cron' ] ) ) {
61 // Called from external script/job. Try setting a lock.
62 if ( $doing_cron_transient && ( $doing_cron_transient + WP_CRON_LOCK_TIMEOUT > $gmt_time ) )
64 $doing_cron_transient = $doing_wp_cron = sprintf( '%.22F', microtime( true ) );
65 set_transient( 'doing_cron', $doing_wp_cron );
67 $doing_wp_cron = $_GET[ 'doing_wp_cron' ];
72 if ( $doing_cron_transient != $doing_wp_cron )
75 foreach ( $crons as $timestamp => $cronhooks ) {
76 if ( $timestamp > $gmt_time )
79 foreach ( $cronhooks as $hook => $keys ) {
81 foreach ( $keys as $k => $v ) {
83 $schedule = $v['schedule'];
85 if ( $schedule != false ) {
86 $new_args = array($timestamp, $schedule, $hook, $v['args']);
87 call_user_func_array('wp_reschedule_event', $new_args);
90 wp_unschedule_event( $timestamp, $hook, $v['args'] );
93 * Fires scheduled events.
97 * @param string $hook Name of the hook that was scheduled to be fired.
98 * @param array $v['args'] The arguments to be passed to the hook.
100 do_action_ref_array( $hook, $v['args'] );
102 // If the hook ran too long and another cron process stole the lock, quit.
103 if ( _get_cron_lock() != $doing_wp_cron )
109 if ( _get_cron_lock() == $doing_wp_cron )
110 delete_transient( 'doing_cron' );