diff --git src/wp-cron.php src/wp-cron.php
index 2b965dd..a3d757e 100644
|
|
|
9 | 9 | * @package WordPress |
10 | 10 | */ |
11 | 11 | |
12 | | ignore_user_abort(true); |
| 12 | ignore_user_abort( true ); |
13 | 13 | |
14 | | if ( !empty($_POST) || defined('DOING_AJAX') || defined('DOING_CRON') ) |
15 | | die(); |
| 14 | if ( defined( 'DOING_AJAX' ) || defined( 'DOING_CRON' ) ) { |
| 15 | die( 'doing_ajax_or_doing_cron' ); |
| 16 | } |
16 | 17 | |
17 | 18 | /** |
18 | 19 | * Tell WordPress we are doing the CRON task. |
19 | 20 | * |
20 | 21 | * @var bool |
21 | 22 | */ |
22 | | define('DOING_CRON', true); |
| 23 | define( 'DOING_CRON', true ); |
23 | 24 | |
24 | | if ( !defined('ABSPATH') ) { |
| 25 | if ( ! defined( 'ABSPATH' ) ) { |
25 | 26 | /** Set up WordPress environment */ |
26 | 27 | require_once( dirname( __FILE__ ) . '/wp-load.php' ); |
27 | 28 | } |
28 | 29 | |
| 30 | /** |
| 31 | * Default handler for a WP Cron exit |
| 32 | * |
| 33 | * @param string $code |
| 34 | */ |
| 35 | function wp_cron_default_exit_handler( $code ) { |
| 36 | do_action( 'wp_cron_response_close', $code ); |
| 37 | if ( 'bad_post_request_or_doing_ajax_or_doing_cron' === $code ) { |
| 38 | $response = 400; |
| 39 | } elseif ( 'empty_cron_array' === $code ) { |
| 40 | $response = 204; |
| 41 | } elseif ( 'no_scheduled_actions_due' === $code ) { |
| 42 | $response = 204; |
| 43 | } elseif ( 'cron_locked' === $code ) { |
| 44 | $response = 403; |
| 45 | } elseif ( 'cron_lock_check_fail' === $code ) { |
| 46 | $response = 400; |
| 47 | } elseif ( 'ok_exit_prematurely' === $code ) { |
| 48 | $response = 200; |
| 49 | } elseif ( 'ok' === $code ) { |
| 50 | $response = 200; |
| 51 | } else { |
| 52 | $response = 500; |
| 53 | } |
| 54 | if ( ! headers_sent() ) { |
| 55 | header( 'Content-Type: text/plain' ); |
| 56 | status_header( $response ); |
| 57 | } |
| 58 | wp_die( $code, '', compact( $response ) ); |
| 59 | } |
| 60 | |
| 61 | /** |
| 62 | * @var string $wp_cron_exit_handler |
| 63 | * @todo docs |
| 64 | */ |
| 65 | $wp_cron_exit_handler = apply_filters( 'wp_cron_exit_handler', 'wp_cron_default_exit_handler' ); |
| 66 | |
| 67 | if ( ! empty( $_POST ) ) { |
| 68 | $wp_cron_exit_handler( 'bad_post_request' ); |
| 69 | } |
| 70 | |
29 | 71 | // Uncached doing_cron transient fetch |
30 | 72 | function _get_cron_lock() { |
31 | 73 | global $wpdb; |
… |
… |
function _get_cron_lock() { |
39 | 81 | $value = wp_cache_get( 'doing_cron', 'transient', true ); |
40 | 82 | } else { |
41 | 83 | $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 ) ) |
| 84 | if ( is_object( $row ) ) { |
43 | 85 | $value = $row->option_value; |
| 86 | } |
44 | 87 | } |
45 | 88 | |
46 | 89 | return $value; |
47 | 90 | } |
48 | 91 | |
49 | | if ( false === $crons = _get_cron_array() ) |
50 | | die(); |
| 92 | if ( false === $crons = _get_cron_array() ) { |
| 93 | $wp_cron_exit_handler( 'empty_cron_array' ); |
| 94 | } |
51 | 95 | |
52 | 96 | $keys = array_keys( $crons ); |
53 | 97 | $gmt_time = microtime( true ); |
54 | 98 | |
55 | | if ( isset($keys[0]) && $keys[0] > $gmt_time ) |
56 | | die(); |
| 99 | if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) { |
| 100 | $wp_cron_exit_handler( 'no_scheduled_actions_due' ); |
| 101 | } |
57 | 102 | |
58 | | $doing_cron_transient = get_transient( 'doing_cron'); |
| 103 | $doing_cron_transient = get_transient( 'doing_cron' ); |
59 | 104 | |
60 | 105 | // Use global $doing_wp_cron lock otherwise use the GET lock. If no lock, trying grabbing a new lock. |
61 | 106 | if ( empty( $doing_wp_cron ) ) { |
62 | | if ( empty( $_GET[ 'doing_wp_cron' ] ) ) { |
| 107 | if ( empty( $_GET['doing_wp_cron'] ) ) { |
63 | 108 | // 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; |
| 109 | if ( $doing_cron_transient && ( $doing_cron_transient + WP_CRON_LOCK_TIMEOUT > $gmt_time ) ) { |
| 110 | $wp_cron_exit_handler( 'cron_locked' ); |
| 111 | } |
66 | 112 | $doing_cron_transient = $doing_wp_cron = sprintf( '%.22F', microtime( true ) ); |
67 | 113 | set_transient( 'doing_cron', $doing_wp_cron ); |
68 | 114 | } else { |
69 | | $doing_wp_cron = $_GET[ 'doing_wp_cron' ]; |
| 115 | $doing_wp_cron = $_GET['doing_wp_cron']; |
70 | 116 | } |
71 | 117 | } |
72 | 118 | |
73 | 119 | // Check lock |
74 | | if ( $doing_cron_transient != $doing_wp_cron ) |
75 | | return; |
| 120 | if ( $doing_cron_transient != $doing_wp_cron ) { |
| 121 | $wp_cron_exit_handler( 'cron_lock_check_fail' ); |
| 122 | } |
76 | 123 | |
| 124 | /** |
| 125 | * @todo docs |
| 126 | */ |
| 127 | do_action( 'wp_cron_before_crons_loop', $crons ); |
77 | 128 | foreach ( $crons as $timestamp => $cronhooks ) { |
78 | | if ( $timestamp > $gmt_time ) |
| 129 | if ( $timestamp > $gmt_time ) { |
79 | 130 | break; |
| 131 | } |
80 | 132 | |
| 133 | /** |
| 134 | * @todo docs |
| 135 | */ |
| 136 | do_action( 'wp_cron_before_cronhooks_loop', $cronhooks ); |
81 | 137 | foreach ( $cronhooks as $hook => $keys ) { |
82 | 138 | |
| 139 | /** |
| 140 | * @todo docs |
| 141 | */ |
| 142 | do_action( 'wp_cron_before_keys_loop', $keys ); |
83 | 143 | foreach ( $keys as $k => $v ) { |
84 | 144 | |
85 | 145 | $schedule = $v['schedule']; |
86 | 146 | |
87 | 147 | if ( $schedule != false ) { |
88 | | $new_args = array($timestamp, $schedule, $hook, $v['args']); |
89 | | call_user_func_array('wp_reschedule_event', $new_args); |
| 148 | $new_args = array( $timestamp, $schedule, $hook, $v['args'] ); |
| 149 | call_user_func_array( 'wp_reschedule_event', $new_args ); |
90 | 150 | } |
91 | 151 | |
92 | 152 | wp_unschedule_event( $timestamp, $hook, $v['args'] ); |
93 | 153 | |
94 | 154 | /** |
| 155 | * @todo docs |
| 156 | */ |
| 157 | do_action( 'wp_cron_before_hook', $hook, $v['args'], $schedule, $timestamp ); |
| 158 | |
| 159 | /** |
95 | 160 | * Fires scheduled events. |
96 | 161 | * |
97 | 162 | * @internal |
… |
… |
foreach ( $crons as $timestamp => $cronhooks ) { |
100 | 165 | * @param string $hook Name of the hook that was scheduled to be fired. |
101 | 166 | * @param array $args The arguments to be passed to the hook. |
102 | 167 | */ |
103 | | do_action_ref_array( $hook, $v['args'] ); |
| 168 | do_action_ref_array( $hook, $v['args'] ); |
| 169 | |
| 170 | /** |
| 171 | * @todo docs |
| 172 | */ |
| 173 | do_action( 'wp_cron_after_hook', $hook, $v['args'], $schedule, $timestamp ); |
104 | 174 | |
105 | 175 | // If the hook ran too long and another cron process stole the lock, quit. |
106 | | if ( _get_cron_lock() != $doing_wp_cron ) |
107 | | return; |
| 176 | if ( _get_cron_lock() != $doing_wp_cron ) { |
| 177 | $wp_cron_exit_handler( 'ok_exit_prematurely' ); |
| 178 | } |
108 | 179 | } |
| 180 | |
| 181 | /** |
| 182 | * @todo docs |
| 183 | */ |
| 184 | do_action( 'wp_cron_after_keys_loop', $keys ); |
109 | 185 | } |
| 186 | |
| 187 | /** |
| 188 | * @todo docs |
| 189 | */ |
| 190 | do_action( 'wp_cron_after_cronhooks_loop', $cronhooks ); |
110 | 191 | } |
| 192 | /** |
| 193 | * @todo docs |
| 194 | */ |
| 195 | do_action( 'wp_cron_after_crons_loop', $crons ); |
111 | 196 | |
112 | | if ( _get_cron_lock() == $doing_wp_cron ) |
| 197 | if ( _get_cron_lock() == $doing_wp_cron ) { |
113 | 198 | delete_transient( 'doing_cron' ); |
| 199 | } |
114 | 200 | |
115 | | die(); |
| 201 | $wp_cron_exit_handler( 'ok' ); |
diff --git src/wp-includes/functions.php src/wp-includes/functions.php
index 33ace42..bc305e8 100644
|
|
function wp_nonce_ays( $action ) { |
2242 | 2242 | * @param string|array $args Optional arguments to control behavior. |
2243 | 2243 | */ |
2244 | 2244 | function wp_die( $message = '', $title = '', $args = array() ) { |
2245 | | if ( defined( 'DOING_AJAX' ) && DOING_AJAX ) { |
| 2245 | if ( ( defined( 'DOING_AJAX' ) && DOING_AJAX ) || ( defined( 'DOING_CRON' ) && DOING_CRON ) ) { |
2246 | 2246 | /** |
2247 | 2247 | * Filter callback for killing WordPress execution for AJAX requests. |
2248 | 2248 | * |