wp-cron.php 3.76 KB
Newer Older
godog's avatar
godog committed
1 2
<?php
/**
lucha's avatar
lucha committed
3 4 5 6 7 8 9 10 11
 * A pseudo-CRON daemon for scheduling WordPress tasks
 *
 * WP Cron is triggered when the site receives a visit. In the scenario
 * where a site may not receive enough visits to execute scheduled tasks
 * in a timely manner, this file can be called directly or via a server
 * CRON daemon for X number of times.
 *
 * Defining DISABLE_WP_CRON as true and calling this file directly are
 * mutually exclusive and the latter does not rely on the former to work.
godog's avatar
godog committed
12 13 14 15 16 17 18
 *
 * The HTTP request to this file will not slow down the visitor who happens to
 * visit when the cron job is needed to run.
 *
 * @package WordPress
 */

samba's avatar
samba committed
19
ignore_user_abort( true );
godog's avatar
godog committed
20

samba's avatar
samba committed
21 22 23 24 25 26
/* Don't make the request block till we finish, if possible. */
if ( function_exists( 'fastcgi_finish_request' ) && version_compare( phpversion(), '7.0.16', '>=' ) ) {
	fastcgi_finish_request();
}

if ( ! empty( $_POST ) || defined( 'DOING_AJAX' ) || defined( 'DOING_CRON' ) ) {
godog's avatar
godog committed
27
	die();
samba's avatar
samba committed
28
}
godog's avatar
godog committed
29 30 31 32 33 34

/**
 * Tell WordPress we are doing the CRON task.
 *
 * @var bool
 */
samba's avatar
samba committed
35
define( 'DOING_CRON', true );
godog's avatar
godog committed
36

samba's avatar
samba committed
37
if ( ! defined( 'ABSPATH' ) ) {
godog's avatar
godog committed
38
	/** Set up WordPress environment */
lucha's avatar
lucha committed
39
	require_once( dirname( __FILE__ ) . '/wp-load.php' );
godog's avatar
godog committed
40 41
}

ale's avatar
ale committed
42 43 44 45 46 47 48 49
/**
 * Retrieves the cron lock.
 *
 * Returns the uncached `doing_cron` transient.
 *
 * @ignore
 * @since 3.3.0
 *
lucha's avatar
lucha committed
50 51
 * @global wpdb $wpdb WordPress database abstraction object.
 *
ale's avatar
ale committed
52 53
 * @return string|false Value of the `doing_cron` transient, 0|false otherwise.
 */
root's avatar
root committed
54
function _get_cron_lock() {
lucha's avatar
lucha committed
55
	global $wpdb;
root's avatar
root committed
56 57

	$value = 0;
lucha's avatar
lucha committed
58
	if ( wp_using_ext_object_cache() ) {
lucha's avatar
lucha committed
59 60 61 62
		/*
		 * Skip local cache and force re-fetch of doing_cron transient
		 * in case another process updated the cache.
		 */
root's avatar
root committed
63 64 65
		$value = wp_cache_get( 'doing_cron', 'transient', true );
	} else {
		$row = $wpdb->get_row( $wpdb->prepare( "SELECT option_value FROM $wpdb->options WHERE option_name = %s LIMIT 1", '_transient_doing_cron' ) );
samba's avatar
samba committed
66
		if ( is_object( $row ) ) {
root's avatar
root committed
67
			$value = $row->option_value;
samba's avatar
samba committed
68
		}
root's avatar
root committed
69 70 71 72 73
	}

	return $value;
}

samba's avatar
samba committed
74 75
$crons = wp_get_ready_cron_jobs();
if ( empty( $crons ) ) {
godog's avatar
godog committed
76
	die();
samba's avatar
samba committed
77
}
godog's avatar
godog committed
78

lechuck's avatar
lechuck committed
79
$gmt_time = microtime( true );
godog's avatar
godog committed
80

lechuck's avatar
lechuck committed
81 82
// The cron lock: a unix timestamp from when the cron was spawned.
$doing_cron_transient = get_transient( 'doing_cron' );
root's avatar
root committed
83 84 85

// Use global $doing_wp_cron lock otherwise use the GET lock. If no lock, trying grabbing a new lock.
if ( empty( $doing_wp_cron ) ) {
samba's avatar
samba committed
86
	if ( empty( $_GET['doing_wp_cron'] ) ) {
root's avatar
root committed
87
		// Called from external script/job. Try setting a lock.
samba's avatar
samba committed
88
		if ( $doing_cron_transient && ( $doing_cron_transient + WP_CRON_LOCK_TIMEOUT > $gmt_time ) ) {
root's avatar
root committed
89
			return;
samba's avatar
samba committed
90
		}
lechuck's avatar
lechuck committed
91
		$doing_cron_transient = $doing_wp_cron = sprintf( '%.22F', microtime( true ) );
root's avatar
root committed
92 93
		set_transient( 'doing_cron', $doing_wp_cron );
	} else {
samba's avatar
samba committed
94
		$doing_wp_cron = $_GET['doing_wp_cron'];
root's avatar
root committed
95 96 97
	}
}

lechuck's avatar
lechuck committed
98 99 100 101
/*
 * The cron lock (a unix timestamp set when the cron was spawned),
 * must match $doing_wp_cron (the "key").
 */
samba's avatar
samba committed
102
if ( $doing_cron_transient != $doing_wp_cron ) {
root's avatar
root committed
103
	return;
samba's avatar
samba committed
104
}
root's avatar
root committed
105 106

foreach ( $crons as $timestamp => $cronhooks ) {
samba's avatar
samba committed
107
	if ( $timestamp > $gmt_time ) {
godog's avatar
godog committed
108
		break;
samba's avatar
samba committed
109
	}
godog's avatar
godog committed
110

root's avatar
root committed
111
	foreach ( $cronhooks as $hook => $keys ) {
godog's avatar
godog committed
112

root's avatar
root committed
113
		foreach ( $keys as $k => $v ) {
godog's avatar
godog committed
114 115 116

			$schedule = $v['schedule'];

root's avatar
root committed
117
			if ( $schedule != false ) {
samba's avatar
samba committed
118 119
				$new_args = array( $timestamp, $schedule, $hook, $v['args'] );
				call_user_func_array( 'wp_reschedule_event', $new_args );
godog's avatar
godog committed
120 121
			}

root's avatar
root committed
122 123
			wp_unschedule_event( $timestamp, $hook, $v['args'] );

lucha's avatar
lucha committed
124 125 126
			/**
			 * Fires scheduled events.
			 *
ale's avatar
ale committed
127
			 * @ignore
lucha's avatar
lucha committed
128 129 130
			 * @since 2.1.0
			 *
			 * @param string $hook Name of the hook that was scheduled to be fired.
lucha's avatar
lucha committed
131
			 * @param array  $args The arguments to be passed to the hook.
lucha's avatar
lucha committed
132
			 */
samba's avatar
samba committed
133
			do_action_ref_array( $hook, $v['args'] );
godog's avatar
godog committed
134

root's avatar
root committed
135
			// If the hook ran too long and another cron process stole the lock, quit.
samba's avatar
samba committed
136
			if ( _get_cron_lock() != $doing_wp_cron ) {
root's avatar
root committed
137
				return;
samba's avatar
samba committed
138
			}
godog's avatar
godog committed
139 140 141 142
		}
	}
}

samba's avatar
samba committed
143
if ( _get_cron_lock() == $doing_wp_cron ) {
root's avatar
root committed
144
	delete_transient( 'doing_cron' );
samba's avatar
samba committed
145
}
root's avatar
root committed
146

godog's avatar
godog committed
147
die();