wp/wp-includes/cron.php
changeset 18 be944660c56a
parent 16 a86126ab1dd4
child 19 3d72ae0968f4
--- a/wp/wp-includes/cron.php	Tue Dec 15 15:52:01 2020 +0100
+++ b/wp/wp-includes/cron.php	Wed Sep 21 18:19:35 2022 +0200
@@ -23,17 +23,29 @@
  * @since 2.1.0
  * @since 5.1.0 Return value modified to boolean indicating success or failure,
  *              {@see 'pre_schedule_event'} filter added to short-circuit the function.
+ * @since 5.7.0 The `$wp_error` parameter was added.
  *
  * @link https://developer.wordpress.org/reference/functions/wp_schedule_single_event/
  *
  * @param int    $timestamp  Unix timestamp (UTC) for when to next run the event.
  * @param string $hook       Action hook to execute when the event is run.
- * @param array  $args       Optional. Array containing each separate argument to pass to the hook's callback function.
- * @return bool True if event successfully scheduled. False for failure.
+ * @param array  $args       Optional. Array containing arguments to pass to the
+ *                           hook's callback function. Each value in the array
+ *                           is passed to the callback as an individual parameter.
+ *                           The array keys are ignored. Default empty array.
+ * @param bool   $wp_error   Optional. Whether to return a WP_Error on failure. Default false.
+ * @return bool|WP_Error True if event successfully scheduled. False or WP_Error on failure.
  */
-function wp_schedule_single_event( $timestamp, $hook, $args = array() ) {
+function wp_schedule_single_event( $timestamp, $hook, $args = array(), $wp_error = false ) {
 	// Make sure timestamp is a positive integer.
 	if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
+		if ( $wp_error ) {
+			return new WP_Error(
+				'invalid_timestamp',
+				__( 'Event timestamp must be a valid Unix timestamp.' )
+			);
+		}
+
 		return false;
 	}
 
@@ -59,12 +71,13 @@
 	 * identical event within ten minutes and apply the {@see 'schedule_event'}
 	 * filter to check if another plugin has disallowed the event before scheduling.
 	 *
-	 * Return true if the event was scheduled, false if not.
+	 * Return true if the event was scheduled, false or a WP_Error if not.
 	 *
 	 * @since 5.1.0
+	 * @since 5.7.0 The `$wp_error` parameter was added, and a `WP_Error` object can now be returned.
 	 *
-	 * @param null|bool $pre   Value to return instead. Default null to continue adding the event.
-	 * @param stdClass  $event {
+	 * @param null|bool|WP_Error $pre      Value to return instead. Default null to continue adding the event.
+	 * @param stdClass           $event    {
 	 *     An object containing an event's data.
 	 *
 	 *     @type string       $hook      Action hook to execute when the event is run.
@@ -73,9 +86,22 @@
 	 *     @type array        $args      Array containing each separate argument to pass to the hook's callback function.
 	 *     @type int          $interval  The interval time in seconds for the schedule. Only present for recurring events.
 	 * }
+	 * @param bool               $wp_error Whether to return a WP_Error on failure.
 	 */
-	$pre = apply_filters( 'pre_schedule_event', null, $event );
+	$pre = apply_filters( 'pre_schedule_event', null, $event, $wp_error );
+
 	if ( null !== $pre ) {
+		if ( $wp_error && false === $pre ) {
+			return new WP_Error(
+				'pre_schedule_event_false',
+				__( 'A plugin prevented the event from being scheduled.' )
+			);
+		}
+
+		if ( ! $wp_error && is_wp_error( $pre ) ) {
+			return false;
+		}
+
 		return $pre;
 	}
 
@@ -122,6 +148,13 @@
 	}
 
 	if ( $duplicate ) {
+		if ( $wp_error ) {
+			return new WP_Error(
+				'duplicate_event',
+				__( 'A duplicate event already exists.' )
+			);
+		}
+
 		return false;
 	}
 
@@ -144,6 +177,13 @@
 
 	// A plugin disallowed this event.
 	if ( ! $event ) {
+		if ( $wp_error ) {
+			return new WP_Error(
+				'schedule_event_false',
+				__( 'A plugin disallowed this event.' )
+			);
+		}
+
 		return false;
 	}
 
@@ -152,7 +192,8 @@
 		'args'     => $event->args,
 	);
 	uksort( $crons, 'strnatcasecmp' );
-	return _set_cron_array( $crons );
+
+	return _set_cron_array( $crons, $wp_error );
 }
 
 /**
@@ -176,24 +217,44 @@
  * @since 2.1.0
  * @since 5.1.0 Return value modified to boolean indicating success or failure,
  *              {@see 'pre_schedule_event'} filter added to short-circuit the function.
+ * @since 5.7.0 The `$wp_error` parameter was added.
  *
  * @link https://developer.wordpress.org/reference/functions/wp_schedule_event/
  *
  * @param int    $timestamp  Unix timestamp (UTC) for when to next run the event.
- * @param string $recurrence How often the event should subsequently recur. See wp_get_schedules() for accepted values.
+ * @param string $recurrence How often the event should subsequently recur.
+ *                           See wp_get_schedules() for accepted values.
  * @param string $hook       Action hook to execute when the event is run.
- * @param array  $args       Optional. Array containing each separate argument to pass to the hook's callback function.
- * @return bool True if event successfully scheduled. False for failure.
+ * @param array  $args       Optional. Array containing arguments to pass to the
+ *                           hook's callback function. Each value in the array
+ *                           is passed to the callback as an individual parameter.
+ *                           The array keys are ignored. Default empty array.
+ * @param bool   $wp_error   Optional. Whether to return a WP_Error on failure. Default false.
+ * @return bool|WP_Error True if event successfully scheduled. False or WP_Error on failure.
  */
-function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array() ) {
+function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array(), $wp_error = false ) {
 	// Make sure timestamp is a positive integer.
 	if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
+		if ( $wp_error ) {
+			return new WP_Error(
+				'invalid_timestamp',
+				__( 'Event timestamp must be a valid Unix timestamp.' )
+			);
+		}
+
 		return false;
 	}
 
 	$schedules = wp_get_schedules();
 
 	if ( ! isset( $schedules[ $recurrence ] ) ) {
+		if ( $wp_error ) {
+			return new WP_Error(
+				'invalid_schedule',
+				__( 'Event schedule does not exist.' )
+			);
+		}
+
 		return false;
 	}
 
@@ -206,8 +267,20 @@
 	);
 
 	/** This filter is documented in wp-includes/cron.php */
-	$pre = apply_filters( 'pre_schedule_event', null, $event );
+	$pre = apply_filters( 'pre_schedule_event', null, $event, $wp_error );
+
 	if ( null !== $pre ) {
+		if ( $wp_error && false === $pre ) {
+			return new WP_Error(
+				'pre_schedule_event_false',
+				__( 'A plugin prevented the event from being scheduled.' )
+			);
+		}
+
+		if ( ! $wp_error && is_wp_error( $pre ) ) {
+			return false;
+		}
+
 		return $pre;
 	}
 
@@ -216,6 +289,13 @@
 
 	// A plugin disallowed this event.
 	if ( ! $event ) {
+		if ( $wp_error ) {
+			return new WP_Error(
+				'schedule_event_false',
+				__( 'A plugin disallowed this event.' )
+			);
+		}
+
 		return false;
 	}
 
@@ -228,7 +308,8 @@
 		'interval' => $event->interval,
 	);
 	uksort( $crons, 'strnatcasecmp' );
-	return _set_cron_array( $crons );
+
+	return _set_cron_array( $crons, $wp_error );
 }
 
 /**
@@ -243,16 +324,29 @@
  * @since 2.1.0
  * @since 5.1.0 Return value modified to boolean indicating success or failure,
  *              {@see 'pre_reschedule_event'} filter added to short-circuit the function.
+ * @since 5.7.0 The `$wp_error` parameter was added.
  *
  * @param int    $timestamp  Unix timestamp (UTC) for when the event was scheduled.
- * @param string $recurrence How often the event should subsequently recur. See wp_get_schedules() for accepted values.
+ * @param string $recurrence How often the event should subsequently recur.
+ *                           See wp_get_schedules() for accepted values.
  * @param string $hook       Action hook to execute when the event is run.
- * @param array  $args       Optional. Array containing each separate argument to pass to the hook's callback function.
- * @return bool True if event successfully rescheduled. False for failure.
+ * @param array  $args       Optional. Array containing arguments to pass to the
+ *                           hook's callback function. Each value in the array
+ *                           is passed to the callback as an individual parameter.
+ *                           The array keys are ignored. Default empty array.
+ * @param bool   $wp_error   Optional. Whether to return a WP_Error on failure. Default false.
+ * @return bool|WP_Error True if event successfully rescheduled. False or WP_Error on failure.
  */
-function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array() ) {
+function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array(), $wp_error = false ) {
 	// Make sure timestamp is a positive integer.
 	if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
+		if ( $wp_error ) {
+			return new WP_Error(
+				'invalid_timestamp',
+				__( 'Event timestamp must be a valid Unix timestamp.' )
+			);
+		}
+
 		return false;
 	}
 
@@ -290,9 +384,10 @@
 	 * rescheduled, false if not.
 	 *
 	 * @since 5.1.0
+	 * @since 5.7.0 The `$wp_error` parameter was added, and a `WP_Error` object can now be returned.
 	 *
-	 * @param null|bool $pre   Value to return instead. Default null to continue adding the event.
-	 * @param stdClass  $event {
+	 * @param null|bool|WP_Error $pre      Value to return instead. Default null to continue adding the event.
+	 * @param stdClass           $event    {
 	 *     An object containing an event's data.
 	 *
 	 *     @type string       $hook      Action hook to execute when the event is run.
@@ -301,14 +396,34 @@
 	 *     @type array        $args      Array containing each separate argument to pass to the hook's callback function.
 	 *     @type int          $interval  The interval time in seconds for the schedule. Only present for recurring events.
 	 * }
+	 * @param bool               $wp_error Whether to return a WP_Error on failure.
 	 */
-	$pre = apply_filters( 'pre_reschedule_event', null, $event );
+	$pre = apply_filters( 'pre_reschedule_event', null, $event, $wp_error );
+
 	if ( null !== $pre ) {
+		if ( $wp_error && false === $pre ) {
+			return new WP_Error(
+				'pre_reschedule_event_false',
+				__( 'A plugin prevented the event from being rescheduled.' )
+			);
+		}
+
+		if ( ! $wp_error && is_wp_error( $pre ) ) {
+			return false;
+		}
+
 		return $pre;
 	}
 
 	// Now we assume something is wrong and fail to schedule.
 	if ( 0 == $interval ) {
+		if ( $wp_error ) {
+			return new WP_Error(
+				'invalid_schedule',
+				__( 'Event schedule does not exist.' )
+			);
+		}
+
 		return false;
 	}
 
@@ -320,7 +435,7 @@
 		$timestamp = $now + ( $interval - ( ( $now - $timestamp ) % $interval ) );
 	}
 
-	return wp_schedule_event( $timestamp, $recurrence, $hook, $args );
+	return wp_schedule_event( $timestamp, $recurrence, $hook, $args, $wp_error );
 }
 
 /**
@@ -332,17 +447,27 @@
  * @since 2.1.0
  * @since 5.1.0 Return value modified to boolean indicating success or failure,
  *              {@see 'pre_unschedule_event'} filter added to short-circuit the function.
+ * @since 5.7.0 The `$wp_error` parameter was added.
  *
  * @param int    $timestamp Unix timestamp (UTC) of the event.
  * @param string $hook      Action hook of the event.
  * @param array  $args      Optional. Array containing each separate argument to pass to the hook's callback function.
  *                          Although not passed to a callback, these arguments are used to uniquely identify the
  *                          event, so they should be the same as those used when originally scheduling the event.
- * @return bool True if event successfully unscheduled. False for failure.
+ *                          Default empty array.
+ * @param bool   $wp_error  Optional. Whether to return a WP_Error on failure. Default false.
+ * @return bool|WP_Error True if event successfully unscheduled. False or WP_Error on failure.
  */
-function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
+function wp_unschedule_event( $timestamp, $hook, $args = array(), $wp_error = false ) {
 	// Make sure timestamp is a positive integer.
 	if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
+		if ( $wp_error ) {
+			return new WP_Error(
+				'invalid_timestamp',
+				__( 'Event timestamp must be a valid Unix timestamp.' )
+			);
+		}
+
 		return false;
 	}
 
@@ -356,14 +481,28 @@
 	 * unscheduled, false if not.
 	 *
 	 * @since 5.1.0
+	 * @since 5.7.0 The `$wp_error` parameter was added, and a `WP_Error` object can now be returned.
 	 *
-	 * @param null|bool $pre       Value to return instead. Default null to continue unscheduling the event.
-	 * @param int       $timestamp Timestamp for when to run the event.
-	 * @param string    $hook      Action hook, the execution of which will be unscheduled.
-	 * @param array     $args      Arguments to pass to the hook's callback function.
+	 * @param null|bool|WP_Error $pre       Value to return instead. Default null to continue unscheduling the event.
+	 * @param int                $timestamp Timestamp for when to run the event.
+	 * @param string             $hook      Action hook, the execution of which will be unscheduled.
+	 * @param array              $args      Arguments to pass to the hook's callback function.
+	 * @param bool               $wp_error  Whether to return a WP_Error on failure.
 	 */
-	$pre = apply_filters( 'pre_unschedule_event', null, $timestamp, $hook, $args );
+	$pre = apply_filters( 'pre_unschedule_event', null, $timestamp, $hook, $args, $wp_error );
+
 	if ( null !== $pre ) {
+		if ( $wp_error && false === $pre ) {
+			return new WP_Error(
+				'pre_unschedule_event_false',
+				__( 'A plugin prevented the event from being unscheduled.' )
+			);
+		}
+
+		if ( ! $wp_error && is_wp_error( $pre ) ) {
+			return false;
+		}
+
 		return $pre;
 	}
 
@@ -376,7 +515,8 @@
 	if ( empty( $crons[ $timestamp ] ) ) {
 		unset( $crons[ $timestamp ] );
 	}
-	return _set_cron_array( $crons );
+
+	return _set_cron_array( $crons, $wp_error );
 }
 
 /**
@@ -390,19 +530,25 @@
  * @since 2.1.0
  * @since 5.1.0 Return value modified to indicate success or failure,
  *              {@see 'pre_clear_scheduled_hook'} filter added to short-circuit the function.
+ * @since 5.7.0 The `$wp_error` parameter was added.
  *
- * @param string $hook Action hook, the execution of which will be unscheduled.
- * @param array  $args Optional. Arguments that were to be passed to the hook's callback function.
- * @return int|false On success an integer indicating number of events unscheduled (0 indicates no
- *                   events were registered with the hook and arguments combination), false if
- *                   unscheduling one or more events fail.
+ * @param string $hook     Action hook, the execution of which will be unscheduled.
+ * @param array  $args     Optional. Array containing each separate argument to pass to the hook's callback function.
+ *                         Although not passed to a callback, these arguments are used to uniquely identify the
+ *                         event, so they should be the same as those used when originally scheduling the event.
+ *                         Default empty array.
+ * @param bool   $wp_error Optional. Whether to return a WP_Error on failure. Default false.
+ * @return int|false|WP_Error On success an integer indicating number of events unscheduled (0 indicates no
+ *                            events were registered with the hook and arguments combination), false or WP_Error
+ *                            if unscheduling one or more events fail.
  */
-function wp_clear_scheduled_hook( $hook, $args = array() ) {
+function wp_clear_scheduled_hook( $hook, $args = array(), $wp_error = false ) {
 	// Backward compatibility.
 	// Previously, this function took the arguments as discrete vars rather than an array like the rest of the API.
 	if ( ! is_array( $args ) ) {
 		_deprecated_argument( __FUNCTION__, '3.0.0', __( 'This argument has changed to an array to match the behavior of the other cron functions.' ) );
-		$args = array_slice( func_get_args(), 1 ); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
+		$args     = array_slice( func_get_args(), 1 ); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
+		$wp_error = false;
 	}
 
 	/**
@@ -416,13 +562,27 @@
 	 * if unscheduling one or more events fails.
 	 *
 	 * @since 5.1.0
+	 * @since 5.7.0 The `$wp_error` parameter was added, and a `WP_Error` object can now be returned.
 	 *
-	 * @param null|int|false $pre  Value to return instead. Default null to continue unscheduling the event.
-	 * @param string         $hook Action hook, the execution of which will be unscheduled.
-	 * @param array          $args Arguments to pass to the hook's callback function.
+	 * @param null|int|false|WP_Error $pre      Value to return instead. Default null to continue unscheduling the event.
+	 * @param string                  $hook     Action hook, the execution of which will be unscheduled.
+	 * @param array                   $args     Arguments to pass to the hook's callback function.
+	 * @param bool                    $wp_error Whether to return a WP_Error on failure.
 	 */
-	$pre = apply_filters( 'pre_clear_scheduled_hook', null, $hook, $args );
+	$pre = apply_filters( 'pre_clear_scheduled_hook', null, $hook, $args, $wp_error );
+
 	if ( null !== $pre ) {
+		if ( $wp_error && false === $pre ) {
+			return new WP_Error(
+				'pre_clear_scheduled_hook_false',
+				__( 'A plugin prevented the hook from being cleared.' )
+			);
+		}
+
+		if ( ! $wp_error && is_wp_error( $pre ) ) {
+			return false;
+		}
+
 		return $pre;
 	}
 
@@ -438,14 +598,26 @@
 
 	$results = array();
 	$key     = md5( serialize( $args ) );
+
 	foreach ( $crons as $timestamp => $cron ) {
 		if ( isset( $cron[ $hook ][ $key ] ) ) {
-			$results[] = wp_unschedule_event( $timestamp, $hook, $args );
+			$results[] = wp_unschedule_event( $timestamp, $hook, $args, true );
 		}
 	}
-	if ( in_array( false, $results, true ) ) {
+
+	$errors = array_filter( $results, 'is_wp_error' );
+	$error  = new WP_Error();
+
+	if ( $errors ) {
+		if ( $wp_error ) {
+			array_walk( $errors, array( $error, 'merge_from' ) );
+
+			return $error;
+		}
+
 		return false;
 	}
+
 	return count( $results );
 }
 
@@ -461,12 +633,14 @@
  *
  * @since 4.9.0
  * @since 5.1.0 Return value added to indicate success or failure.
+ * @since 5.7.0 The `$wp_error` parameter was added.
  *
- * @param string $hook Action hook, the execution of which will be unscheduled.
- * @return int|false On success an integer indicating number of events unscheduled (0 indicates no
- *                   events were registered on the hook), false if unscheduling fails.
+ * @param string $hook     Action hook, the execution of which will be unscheduled.
+ * @param bool   $wp_error Optional. Whether to return a WP_Error on failure. Default false.
+ * @return int|false|WP_Error On success an integer indicating number of events unscheduled (0 indicates no
+ *                            events were registered on the hook), false or WP_Error if unscheduling fails.
  */
-function wp_unschedule_hook( $hook ) {
+function wp_unschedule_hook( $hook, $wp_error = false ) {
 	/**
 	 * Filter to preflight or hijack clearing all events attached to the hook.
 	 *
@@ -478,12 +652,26 @@
 	 * if unscheduling one or more events fails.
 	 *
 	 * @since 5.1.0
+	 * @since 5.7.0 The `$wp_error` parameter was added, and a `WP_Error` object can now be returned.
 	 *
-	 * @param null|int|false $pre  Value to return instead. Default null to continue unscheduling the hook.
-	 * @param string         $hook Action hook, the execution of which will be unscheduled.
+	 * @param null|int|false|WP_Error $pre      Value to return instead. Default null to continue unscheduling the hook.
+	 * @param string                  $hook     Action hook, the execution of which will be unscheduled.
+	 * @param bool                    $wp_error Whether to return a WP_Error on failure.
 	 */
-	$pre = apply_filters( 'pre_unschedule_hook', null, $hook );
+	$pre = apply_filters( 'pre_unschedule_hook', null, $hook, $wp_error );
+
 	if ( null !== $pre ) {
+		if ( $wp_error && false === $pre ) {
+			return new WP_Error(
+				'pre_unschedule_hook_false',
+				__( 'A plugin prevented the hook from being cleared.' )
+			);
+		}
+
+		if ( ! $wp_error && is_wp_error( $pre ) ) {
+			return false;
+		}
+
 		return $pre;
 	}
 
@@ -511,10 +699,14 @@
 	if ( empty( $results ) ) {
 		return 0;
 	}
-	if ( _set_cron_array( $crons ) ) {
+
+	$set = _set_cron_array( $crons, $wp_error );
+
+	if ( true === $set ) {
 		return array_sum( $results );
 	}
-	return false;
+
+	return $set;
 }
 
 /**
@@ -529,7 +721,9 @@
  * @param array    $args      Optional. Array containing each separate argument to pass to the hook's callback function.
  *                            Although not passed to a callback, these arguments are used to uniquely identify the
  *                            event, so they should be the same as those used when originally scheduling the event.
- * @param int|null $timestamp Optional. Unix timestamp (UTC) of the event. If not specified, the next scheduled event is returned.
+ *                            Default empty array.
+ * @param int|null $timestamp Optional. Unix timestamp (UTC) of the event. If not specified, the next scheduled event
+ *                            is returned. Default null.
  * @return object|false The event object. False if the event does not exist.
  */
 function wp_get_scheduled_event( $hook, $args = array(), $timestamp = null ) {
@@ -608,6 +802,7 @@
  * @param array  $args Optional. Array containing each separate argument to pass to the hook's callback function.
  *                     Although not passed to a callback, these arguments are used to uniquely identify the
  *                     event, so they should be the same as those used when originally scheduling the event.
+ *                     Default empty array.
  * @return int|false The Unix timestamp of the next time the event will occur. False if the event doesn't exist.
  */
 function wp_next_scheduled( $hook, $args = array() ) {
@@ -731,7 +926,10 @@
 }
 
 /**
- * Run scheduled callbacks or spawn cron for all scheduled events.
+ * Register _wp_cron() to run on the {@see 'wp_loaded'} action.
+ *
+ * If the {@see 'wp_loaded'} action has already fired, this function calls
+ * _wp_cron() directly.
  *
  * Warning: This function may return Boolean FALSE, but may also return a non-Boolean
  * value which evaluates to FALSE. For information about casting to booleans see the
@@ -740,11 +938,35 @@
  *
  * @since 2.1.0
  * @since 5.1.0 Return value added to indicate success or failure.
+ * @since 5.7.0 Functionality moved to _wp_cron() to which this becomes a wrapper.
  *
- * @return bool|int On success an integer indicating number of events spawned (0 indicates no
- *                  events needed to be spawned), false if spawning fails for one or more events.
+ * @return bool|int|void On success an integer indicating number of events spawned (0 indicates no
+ *                       events needed to be spawned), false if spawning fails for one or more events or
+ *                       void if the function registered _wp_cron() to run on the action.
  */
 function wp_cron() {
+	if ( did_action( 'wp_loaded' ) ) {
+		return _wp_cron();
+	}
+
+	add_action( 'wp_loaded', '_wp_cron', 20 );
+}
+
+/**
+ * Run scheduled callbacks or spawn cron for all scheduled events.
+ *
+ * Warning: This function may return Boolean FALSE, but may also return a non-Boolean
+ * value which evaluates to FALSE. For information about casting to booleans see the
+ * {@link https://www.php.net/manual/en/language.types.boolean.php PHP documentation}. Use
+ * the `===` operator for testing the return value of this function.
+ *
+ * @since 5.7.0
+ * @access private
+ *
+ * @return int|false On success an integer indicating number of events spawned (0 indicates no
+ *                   events needed to be spawned), false if spawning fails for one or more events.
+ */
+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 ) ) {
 		return 0;
@@ -852,7 +1074,8 @@
  *
  * @param string $hook Action hook to identify the event.
  * @param array  $args Optional. Arguments passed to the event's callback function.
- * @return string|false False, if no schedule. Schedule name on success.
+ *                     Default empty array.
+ * @return string|false Schedule name on success, false if no schedule.
  */
 function wp_get_schedule( $hook, $args = array() ) {
 	$schedule = false;
@@ -863,13 +1086,13 @@
 	}
 
 	/**
-	 * Filter the schedule for a hook.
+	 * Filters the schedule for a hook.
 	 *
 	 * @since 5.1.0
 	 *
-	 * @param string|bool $schedule Schedule for the hook. False if not found.
-	 * @param string      $hook     Action hook to execute when cron is run.
-	 * @param array       $args     Optional. Arguments to pass to the hook's callback function.
+	 * @param string|false $schedule Schedule for the hook. False if not found.
+	 * @param string       $hook     Action hook to execute when cron is run.
+	 * @param array        $args     Arguments to pass to the hook's callback function.
 	 */
 	return apply_filters( 'get_schedule', $schedule, $hook, $args );
 }
@@ -934,7 +1157,7 @@
  * @since 2.1.0
  * @access private
  *
- * @return array|false CRON info array.
+ * @return array|false Cron info array on success, false on failure.
  */
 function _get_cron_array() {
 	$cron = get_option( 'cron' );
@@ -952,19 +1175,30 @@
 }
 
 /**
- * Updates the CRON option with the new CRON array.
+ * Updates the cron option with the new cron array.
  *
  * @since 2.1.0
  * @since 5.1.0 Return value modified to outcome of update_option().
+ * @since 5.7.0 The `$wp_error` parameter was added.
  *
  * @access private
  *
- * @param array $cron Cron info array from _get_cron_array().
- * @return bool True if cron array updated, false on failure.
+ * @param array $cron     Cron info array from _get_cron_array().
+ * @param bool  $wp_error Optional. Whether to return a WP_Error on failure. Default false.
+ * @return bool|WP_Error True if cron array updated. False or WP_Error on failure.
  */
-function _set_cron_array( $cron ) {
+function _set_cron_array( $cron, $wp_error = false ) {
 	$cron['version'] = 2;
-	return update_option( 'cron', $cron );
+	$result          = update_option( 'cron', $cron );
+
+	if ( $wp_error && ! $result ) {
+		return new WP_Error(
+			'could_not_set',
+			__( 'The cron event list could not be saved.' )
+		);
+	}
+
+	return $result;
 }
 
 /**