From 85ef68165ec7044ecd04d00e40e9df6e3ddc430c Mon Sep 17 00:00:00 2001
From: jrfnl <github_nospam@adviesenzo.nl>
Date: Tue, 8 Dec 2015 11:23:04 +0100
Subject: [PATCH] Refresh of @evansolomon's patch for issue #20172.
Boyscouting: get rid of inline control structures in touched functions.
---
src/wp-includes/cron.php | 84 ++++++++++++++++++++++++++++++++----------------
1 file changed, 56 insertions(+), 28 deletions(-)
diff --git a/src/wp-includes/cron.php b/src/wp-includes/cron.php
index 60492c5..c8852f9 100644
|
a
|
b
|
|
| 18 | 18 | * @param int $timestamp Timestamp for when to run the event. |
| 19 | 19 | * @param string $hook Action hook to execute when cron is run. |
| 20 | 20 | * @param array $args Optional. Arguments to pass to the hook's callback function. |
| 21 | | * @return false|void False when an event is not scheduled. |
| | 21 | * @return bool False on failure, true if the event has been scheduled. |
| 22 | 22 | */ |
| 23 | 23 | function wp_schedule_single_event( $timestamp, $hook, $args = array()) { |
| 24 | 24 | // Make sure timestamp is a positive integer |
| … |
… |
function wp_schedule_single_event( $timestamp, $hook, $args = array()) {
|
| 44 | 44 | $event = apply_filters( 'schedule_event', $event ); |
| 45 | 45 | |
| 46 | 46 | // A plugin disallowed this event |
| 47 | | if ( ! $event ) |
| | 47 | if ( ! $event ) { |
| 48 | 48 | return false; |
| | 49 | } |
| 49 | 50 | |
| 50 | 51 | $key = md5(serialize($event->args)); |
| 51 | 52 | |
| 52 | 53 | $crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args ); |
| 53 | 54 | uksort( $crons, "strnatcasecmp" ); |
| 54 | | _set_cron_array( $crons ); |
| | 55 | return _set_cron_array( $crons ); |
| 55 | 56 | } |
| 56 | 57 | |
| 57 | 58 | /** |
| … |
… |
function wp_schedule_single_event( $timestamp, $hook, $args = array()) {
|
| 72 | 73 | * @param string $recurrence How often the event should recur. |
| 73 | 74 | * @param string $hook Action hook to execute when cron is run. |
| 74 | 75 | * @param array $args Optional. Arguments to pass to the hook's callback function. |
| 75 | | * @return false|void False when an event is not scheduled. |
| | 76 | * @return bool False on failure, true if the event has been scheduled. |
| 76 | 77 | */ |
| 77 | 78 | function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array()) { |
| 78 | 79 | // Make sure timestamp is a positive integer |
| … |
… |
function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array()) {
|
| 83 | 84 | $crons = _get_cron_array(); |
| 84 | 85 | $schedules = wp_get_schedules(); |
| 85 | 86 | |
| 86 | | if ( !isset( $schedules[$recurrence] ) ) |
| | 87 | if ( !isset( $schedules[$recurrence] ) ) { |
| 87 | 88 | return false; |
| | 89 | } |
| 88 | 90 | |
| 89 | 91 | $event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => $recurrence, 'args' => $args, 'interval' => $schedules[$recurrence]['interval'] ); |
| 90 | 92 | /** This filter is documented in wp-includes/cron.php */ |
| 91 | 93 | $event = apply_filters( 'schedule_event', $event ); |
| 92 | 94 | |
| 93 | 95 | // A plugin disallowed this event |
| 94 | | if ( ! $event ) |
| | 96 | if ( ! $event ) { |
| 95 | 97 | return false; |
| | 98 | } |
| 96 | 99 | |
| 97 | 100 | $key = md5(serialize($event->args)); |
| 98 | 101 | |
| 99 | 102 | $crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args, 'interval' => $event->interval ); |
| 100 | 103 | uksort( $crons, "strnatcasecmp" ); |
| 101 | | _set_cron_array( $crons ); |
| | 104 | return _set_cron_array( $crons ); |
| 102 | 105 | } |
| 103 | 106 | |
| 104 | 107 | /** |
| … |
… |
function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array()) {
|
| 110 | 113 | * @param string $recurrence How often the event should recur. |
| 111 | 114 | * @param string $hook Action hook to execute when cron is run. |
| 112 | 115 | * @param array $args Optional. Arguments to pass to the hook's callback function. |
| 113 | | * @return false|void False when an event is not scheduled. |
| | 116 | * @return bool False on failure, true if the event has been rescheduled. |
| 114 | 117 | */ |
| 115 | 118 | function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array() ) { |
| 116 | 119 | // Make sure timestamp is a positive integer |
| … |
… |
function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array() )
|
| 144 | 147 | $timestamp = $now + ( $interval - ( ( $now - $timestamp ) % $interval ) ); |
| 145 | 148 | } |
| 146 | 149 | |
| 147 | | wp_schedule_event( $timestamp, $recurrence, $hook, $args ); |
| | 150 | return wp_schedule_event( $timestamp, $recurrence, $hook, $args ); |
| 148 | 151 | } |
| 149 | 152 | |
| 150 | 153 | /** |
| … |
… |
function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array() )
|
| 161 | 164 | * Although not passed to a callback function, these arguments are used |
| 162 | 165 | * to uniquely identify the scheduled event, so they should be the same |
| 163 | 166 | * as those used when originally scheduling the event. |
| 164 | | * @return false|void False when an event is not unscheduled. |
| | 167 | * @return bool False on failure, true if the event has been unscheduled. |
| 165 | 168 | */ |
| 166 | 169 | function wp_unschedule_event( $timestamp, $hook, $args = array() ) { |
| 167 | 170 | // Make sure timestamp is a positive integer |
| … |
… |
function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
|
| 172 | 175 | $crons = _get_cron_array(); |
| 173 | 176 | $key = md5(serialize($args)); |
| 174 | 177 | unset( $crons[$timestamp][$hook][$key] ); |
| 175 | | if ( empty($crons[$timestamp][$hook]) ) |
| | 178 | if ( empty($crons[$timestamp][$hook]) ) { |
| 176 | 179 | unset( $crons[$timestamp][$hook] ); |
| 177 | | if ( empty($crons[$timestamp]) ) |
| | 180 | } |
| | 181 | if ( empty($crons[$timestamp]) ) { |
| 178 | 182 | unset( $crons[$timestamp] ); |
| 179 | | _set_cron_array( $crons ); |
| | 183 | } |
| | 184 | return _set_cron_array( $crons ); |
| 180 | 185 | } |
| 181 | 186 | |
| 182 | 187 | /** |
| … |
… |
function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
|
| 186 | 191 | * |
| 187 | 192 | * @param string $hook Action hook, the execution of which will be unscheduled. |
| 188 | 193 | * @param array $args Optional. Arguments that were to be pass to the hook's callback function. |
| | 194 | * @return array Boolean values, for each unscheduled event with timestamps as keys. |
| 189 | 195 | */ |
| 190 | 196 | function wp_clear_scheduled_hook( $hook, $args = array() ) { |
| 191 | 197 | // Backward compatibility |
| … |
… |
function wp_clear_scheduled_hook( $hook, $args = array() ) {
|
| 199 | 205 | // It's required due to a scenario where wp_unschedule_event() fails due to update_option() failing, |
| 200 | 206 | // and, wp_next_scheduled() returns the same schedule in an infinite loop. |
| 201 | 207 | $crons = _get_cron_array(); |
| 202 | | if ( empty( $crons ) ) |
| | 208 | if ( empty( $crons ) ) { |
| 203 | 209 | return; |
| | 210 | } |
| 204 | 211 | |
| | 212 | $results = array(); |
| 205 | 213 | $key = md5( serialize( $args ) ); |
| 206 | 214 | foreach ( $crons as $timestamp => $cron ) { |
| 207 | 215 | if ( isset( $cron[ $hook ][ $key ] ) ) { |
| 208 | | wp_unschedule_event( $timestamp, $hook, $args ); |
| | 216 | $results[ $timestamp ] = wp_unschedule_event( $timestamp, $hook, $args ); |
| 209 | 217 | } |
| 210 | 218 | } |
| | 219 | return $results; |
| 211 | 220 | } |
| 212 | 221 | |
| 213 | 222 | /** |
| … |
… |
function wp_next_scheduled( $hook, $args = array() ) {
|
| 235 | 244 | * Send request to run cron through HTTP request that doesn't halt page loading. |
| 236 | 245 | * |
| 237 | 246 | * @since 2.1.0 |
| | 247 | * |
| | 248 | * @return null|WP_Error|array Null when cron could not be spawned, because it is not needed to run. |
| | 249 | * When cron runs, return the result of wp_remote_post(). |
| 238 | 250 | */ |
| 239 | 251 | function spawn_cron( $gmt_time = 0 ) { |
| 240 | | if ( ! $gmt_time ) |
| | 252 | if ( ! $gmt_time ) { |
| 241 | 253 | $gmt_time = microtime( true ); |
| | 254 | } |
| 242 | 255 | |
| 243 | | if ( defined('DOING_CRON') || isset($_GET['doing_wp_cron']) ) |
| | 256 | if ( defined('DOING_CRON') || isset($_GET['doing_wp_cron']) ) { |
| 244 | 257 | return; |
| | 258 | } |
| 245 | 259 | |
| 246 | 260 | /* |
| 247 | 261 | * Get the cron lock, which is a unix timestamp of when the last cron was spawned |
| … |
… |
function spawn_cron( $gmt_time = 0 ) {
|
| 252 | 266 | */ |
| 253 | 267 | $lock = get_transient('doing_cron'); |
| 254 | 268 | |
| 255 | | if ( $lock > $gmt_time + 10 * MINUTE_IN_SECONDS ) |
| | 269 | if ( $lock > $gmt_time + 10 * MINUTE_IN_SECONDS ) { |
| 256 | 270 | $lock = 0; |
| | 271 | } |
| 257 | 272 | |
| 258 | 273 | // don't run if another process is currently running it or more than once every 60 sec. |
| 259 | | if ( $lock + WP_CRON_LOCK_TIMEOUT > $gmt_time ) |
| | 274 | if ( $lock + WP_CRON_LOCK_TIMEOUT > $gmt_time ) { |
| 260 | 275 | return; |
| | 276 | } |
| 261 | 277 | |
| 262 | 278 | //sanity check |
| 263 | 279 | $crons = _get_cron_array(); |
| 264 | | if ( !is_array($crons) ) |
| | 280 | if ( !is_array($crons) ) { |
| 265 | 281 | return; |
| | 282 | } |
| 266 | 283 | |
| 267 | 284 | $keys = array_keys( $crons ); |
| 268 | | if ( isset($keys[0]) && $keys[0] > $gmt_time ) |
| | 285 | if ( isset($keys[0]) && $keys[0] > $gmt_time ) { |
| 269 | 286 | return; |
| | 287 | } |
| 270 | 288 | |
| 271 | 289 | if ( defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) { |
| 272 | 290 | if ( 'GET' !== $_SERVER['REQUEST_METHOD'] || defined( 'DOING_AJAX' ) || defined( 'XMLRPC_REQUEST' ) ) { |
| … |
… |
function spawn_cron( $gmt_time = 0 ) {
|
| 322 | 340 | ) |
| 323 | 341 | ) ); |
| 324 | 342 | |
| 325 | | wp_remote_post( $cron_request['url'], $cron_request['args'] ); |
| | 343 | return wp_remote_post( $cron_request['url'], $cron_request['args'] ); |
| 326 | 344 | } |
| 327 | 345 | |
| 328 | 346 | /** |
| 329 | 347 | * Run scheduled callbacks or spawn cron for all scheduled events. |
| 330 | 348 | * |
| 331 | 349 | * @since 2.1.0 |
| | 350 | * |
| | 351 | * @return null|array Null when doesn't need to run Cron. Array of spawn_cron() results when cron does run. |
| 332 | 352 | */ |
| 333 | 353 | function wp_cron() { |
| 334 | 354 | // Prevent infinite loops caused by lack of wp-cron.php |
| 335 | | if ( strpos($_SERVER['REQUEST_URI'], '/wp-cron.php') !== false || ( defined('DISABLE_WP_CRON') && DISABLE_WP_CRON ) ) |
| | 355 | if ( strpos($_SERVER['REQUEST_URI'], '/wp-cron.php') !== false || ( defined('DISABLE_WP_CRON') && DISABLE_WP_CRON ) ) { |
| 336 | 356 | return; |
| | 357 | } |
| 337 | 358 | |
| 338 | | if ( false === $crons = _get_cron_array() ) |
| | 359 | if ( false === $crons = _get_cron_array() ) { |
| 339 | 360 | return; |
| | 361 | } |
| 340 | 362 | |
| 341 | 363 | $gmt_time = microtime( true ); |
| 342 | 364 | $keys = array_keys( $crons ); |
| 343 | | if ( isset($keys[0]) && $keys[0] > $gmt_time ) |
| | 365 | if ( isset($keys[0]) && $keys[0] > $gmt_time ) { |
| 344 | 366 | return; |
| | 367 | } |
| 345 | 368 | |
| | 369 | $results = array(); |
| 346 | 370 | $schedules = wp_get_schedules(); |
| 347 | 371 | foreach ( $crons as $timestamp => $cronhooks ) { |
| 348 | 372 | if ( $timestamp > $gmt_time ) break; |
| 349 | 373 | foreach ( (array) $cronhooks as $hook => $args ) { |
| 350 | | if ( isset($schedules[$hook]['callback']) && !call_user_func( $schedules[$hook]['callback'] ) ) |
| | 374 | if ( isset($schedules[$hook]['callback']) && !call_user_func( $schedules[$hook]['callback'] ) ) { |
| 351 | 375 | continue; |
| 352 | | spawn_cron( $gmt_time ); |
| | 376 | } |
| | 377 | $results[] = spawn_cron( $gmt_time ); |
| 353 | 378 | break 2; |
| 354 | 379 | } |
| 355 | 380 | } |
| | 381 | return $results; |
| 356 | 382 | } |
| 357 | 383 | |
| 358 | 384 | /** |
| … |
… |
function _get_cron_array() {
|
| 454 | 480 | * @access private |
| 455 | 481 | * |
| 456 | 482 | * @param array $cron Cron info array from {@link _get_cron_array()}. |
| | 483 | * |
| | 484 | * @return bool Whether the update of the cron option succeeded. |
| 457 | 485 | */ |
| 458 | 486 | function _set_cron_array($cron) { |
| 459 | 487 | $cron['version'] = 2; |
| 460 | | update_option( 'cron', $cron ); |
| | 488 | return update_option( 'cron', $cron ); |
| 461 | 489 | } |
| 462 | 490 | |
| 463 | 491 | /** |