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/src/wp-includes/cron.php
+++ b/src/wp-includes/cron.php
@@ -18,7 +18,7 @@
  * @param int $timestamp Timestamp for when to run the event.
  * @param string $hook Action hook to execute when cron is run.
  * @param array $args Optional. Arguments to pass to the hook's callback function.
- * @return false|void False when an event is not scheduled.
+ * @return bool False on failure, true if the event has been scheduled.
  */
 function wp_schedule_single_event( $timestamp, $hook, $args = array()) {
 	// Make sure timestamp is a positive integer
@@ -44,14 +44,15 @@ function wp_schedule_single_event( $timestamp, $hook, $args = array()) {
 	$event = apply_filters( 'schedule_event', $event );
 
 	// A plugin disallowed this event
-	if ( ! $event )
+	if ( ! $event ) {
 		return false;
+	}
 
 	$key = md5(serialize($event->args));
 
 	$crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args );
 	uksort( $crons, "strnatcasecmp" );
-	_set_cron_array( $crons );
+	return _set_cron_array( $crons );
 }
 
 /**
@@ -72,7 +73,7 @@ function wp_schedule_single_event( $timestamp, $hook, $args = array()) {
  * @param string $recurrence How often the event should recur.
  * @param string $hook Action hook to execute when cron is run.
  * @param array $args Optional. Arguments to pass to the hook's callback function.
- * @return false|void False when an event is not scheduled.
+ * @return bool False on failure, true if the event has been scheduled.
  */
 function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array()) {
 	// Make sure timestamp is a positive integer
@@ -83,22 +84,24 @@ function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array()) {
 	$crons = _get_cron_array();
 	$schedules = wp_get_schedules();
 
-	if ( !isset( $schedules[$recurrence] ) )
+	if ( !isset( $schedules[$recurrence] ) ) {
 		return false;
+	}
 
 	$event = (object) array( 'hook' => $hook, 'timestamp' => $timestamp, 'schedule' => $recurrence, 'args' => $args, 'interval' => $schedules[$recurrence]['interval'] );
 	/** This filter is documented in wp-includes/cron.php */
 	$event = apply_filters( 'schedule_event', $event );
 
 	// A plugin disallowed this event
-	if ( ! $event )
+	if ( ! $event ) {
 		return false;
+	}
 
 	$key = md5(serialize($event->args));
 
 	$crons[$event->timestamp][$event->hook][$key] = array( 'schedule' => $event->schedule, 'args' => $event->args, 'interval' => $event->interval );
 	uksort( $crons, "strnatcasecmp" );
-	_set_cron_array( $crons );
+	return _set_cron_array( $crons );
 }
 
 /**
@@ -110,7 +113,7 @@ function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array()) {
  * @param string $recurrence How often the event should recur.
  * @param string $hook Action hook to execute when cron is run.
  * @param array $args Optional. Arguments to pass to the hook's callback function.
- * @return false|void False when an event is not scheduled.
+ * @return bool False on failure, true if the event has been rescheduled.
  */
 function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array() ) {
 	// Make sure timestamp is a positive integer
@@ -144,7 +147,7 @@ function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array() )
 		$timestamp = $now + ( $interval - ( ( $now - $timestamp ) % $interval ) );
 	}
 
-	wp_schedule_event( $timestamp, $recurrence, $hook, $args );
+	return wp_schedule_event( $timestamp, $recurrence, $hook, $args );
 }
 
 /**
@@ -161,7 +164,7 @@ function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array() )
  * Although not passed to a callback function, these arguments are used
  * to uniquely identify the scheduled event, so they should be the same
  * as those used when originally scheduling the event.
- * @return false|void False when an event is not unscheduled.
+ * @return bool False on failure, true if the event has been unscheduled.
  */
 function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
 	// Make sure timestamp is a positive integer
@@ -172,11 +175,13 @@ function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
 	$crons = _get_cron_array();
 	$key = md5(serialize($args));
 	unset( $crons[$timestamp][$hook][$key] );
-	if ( empty($crons[$timestamp][$hook]) )
+	if ( empty($crons[$timestamp][$hook]) ) {
 		unset( $crons[$timestamp][$hook] );
-	if ( empty($crons[$timestamp]) )
+	}
+	if ( empty($crons[$timestamp]) ) {
 		unset( $crons[$timestamp] );
-	_set_cron_array( $crons );
+	}
+	return _set_cron_array( $crons );
 }
 
 /**
@@ -186,6 +191,7 @@ function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
  *
  * @param string $hook Action hook, the execution of which will be unscheduled.
  * @param array $args Optional. Arguments that were to be pass to the hook's callback function.
+ * @return array Boolean values, for each unscheduled event with timestamps as keys.
  */
 function wp_clear_scheduled_hook( $hook, $args = array() ) {
 	// Backward compatibility
@@ -199,15 +205,18 @@ function wp_clear_scheduled_hook( $hook, $args = array() ) {
 	// It's required due to a scenario where wp_unschedule_event() fails due to update_option() failing,
 	// and, wp_next_scheduled() returns the same schedule in an infinite loop.
 	$crons = _get_cron_array();
-	if ( empty( $crons ) )
+	if ( empty( $crons ) ) {
 		return;
+	}
 
+	$results = array();
 	$key = md5( serialize( $args ) );
 	foreach ( $crons as $timestamp => $cron ) {
 		if ( isset( $cron[ $hook ][ $key ] ) ) {
-			wp_unschedule_event( $timestamp, $hook, $args );
+			$results[ $timestamp ] = wp_unschedule_event( $timestamp, $hook, $args );
 		}
 	}
+	return $results;
 }
 
 /**
@@ -235,13 +244,18 @@ function wp_next_scheduled( $hook, $args = array() ) {
  * Send request to run cron through HTTP request that doesn't halt page loading.
  *
  * @since 2.1.0
+ *
+ * @return null|WP_Error|array Null when cron could not be spawned, because it is not needed to run.
+ * When cron runs, return the result of wp_remote_post().
  */
 function spawn_cron( $gmt_time = 0 ) {
-	if ( ! $gmt_time )
+	if ( ! $gmt_time ) {
 		$gmt_time = microtime( true );
+	}
 
-	if ( defined('DOING_CRON') || isset($_GET['doing_wp_cron']) )
+	if ( defined('DOING_CRON') || isset($_GET['doing_wp_cron']) ) {
 		return;
+	}
 
 	/*
 	 * Get the cron lock, which is a unix timestamp of when the last cron was spawned
@@ -252,21 +266,25 @@ function spawn_cron( $gmt_time = 0 ) {
 	 */
 	$lock = get_transient('doing_cron');
 
-	if ( $lock > $gmt_time + 10 * MINUTE_IN_SECONDS )
+	if ( $lock > $gmt_time + 10 * MINUTE_IN_SECONDS ) {
 		$lock = 0;
+	}
 
 	// don't run if another process is currently running it or more than once every 60 sec.
-	if ( $lock + WP_CRON_LOCK_TIMEOUT > $gmt_time )
+	if ( $lock + WP_CRON_LOCK_TIMEOUT > $gmt_time ) {
 		return;
+	}
 
 	//sanity check
 	$crons = _get_cron_array();
-	if ( !is_array($crons) )
+	if ( !is_array($crons) ) {
 		return;
+	}
 
 	$keys = array_keys( $crons );
-	if ( isset($keys[0]) && $keys[0] > $gmt_time )
+	if ( isset($keys[0]) && $keys[0] > $gmt_time ) {
 		return;
+	}
 
 	if ( defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) {
 		if ( 'GET' !== $_SERVER['REQUEST_METHOD'] || defined( 'DOING_AJAX' ) ||  defined( 'XMLRPC_REQUEST' ) ) {
@@ -322,37 +340,45 @@ function spawn_cron( $gmt_time = 0 ) {
 		)
 	) );
 
-	wp_remote_post( $cron_request['url'], $cron_request['args'] );
+	return wp_remote_post( $cron_request['url'], $cron_request['args'] );
 }
 
 /**
  * Run scheduled callbacks or spawn cron for all scheduled events.
  *
  * @since 2.1.0
+ *
+ * @return null|array Null when doesn't need to run Cron. Array of spawn_cron() results when cron does run.
  */
 function wp_cron() {
 	// Prevent infinite loops caused by lack of wp-cron.php
-	if ( strpos($_SERVER['REQUEST_URI'], '/wp-cron.php') !== false || ( defined('DISABLE_WP_CRON') && DISABLE_WP_CRON ) )
+	if ( strpos($_SERVER['REQUEST_URI'], '/wp-cron.php') !== false || ( defined('DISABLE_WP_CRON') && DISABLE_WP_CRON ) ) {
 		return;
+	}
 
-	if ( false === $crons = _get_cron_array() )
+	if ( false === $crons = _get_cron_array() ) {
 		return;
+	}
 
 	$gmt_time = microtime( true );
 	$keys = array_keys( $crons );
-	if ( isset($keys[0]) && $keys[0] > $gmt_time )
+	if ( isset($keys[0]) && $keys[0] > $gmt_time ) {
 		return;
+	}
 
+	$results = array();
 	$schedules = wp_get_schedules();
 	foreach ( $crons as $timestamp => $cronhooks ) {
 		if ( $timestamp > $gmt_time ) break;
 		foreach ( (array) $cronhooks as $hook => $args ) {
-			if ( isset($schedules[$hook]['callback']) && !call_user_func( $schedules[$hook]['callback'] ) )
+			if ( isset($schedules[$hook]['callback']) && !call_user_func( $schedules[$hook]['callback'] ) ) {
 				continue;
-			spawn_cron( $gmt_time );
+			}
+			$results[] = spawn_cron( $gmt_time );
 			break 2;
 		}
 	}
+	return $results;
 }
 
 /**
@@ -454,10 +480,12 @@ function _get_cron_array()  {
  * @access private
  *
  * @param array $cron Cron info array from {@link _get_cron_array()}.
+ *
+ * @return bool Whether the update of the cron option succeeded.
  */
 function _set_cron_array($cron) {
 	$cron['version'] = 2;
-	update_option( 'cron', $cron );
+	return update_option( 'cron', $cron );
 }
 
 /**
-- 
1.9.4.msysgit.2

