<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[50143] trunk: Cron API: Introduce a `$wp_error` parameter to functions that write to the cron array.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { white-space: pre-line; overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="https://core.trac.wordpress.org/changeset/50143">50143</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"https://core.trac.wordpress.org/changeset/50143","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>johnbillion</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2021-02-02 13:47:46 +0000 (Tue, 02 Feb 2021)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Cron API: Introduce a `$wp_error` parameter to functions that write to the cron array.

This allows the functions to return a `WP_Error` object containing more information in case of a problem, instead of just boolean false.

The various `pre_` filters in these functions are also updated so they can return or be passed a `WP_Error` object.

Fixes <a href="https://core.trac.wordpress.org/ticket/49961">#49961</a></pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#trunksrcwpincludescronphp">trunk/src/wp-includes/cron.php</a></li>
<li><a href="#trunktestsphpunittestscronphp">trunk/tests/phpunit/tests/cron.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunksrcwpincludescronphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/src/wp-includes/cron.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/src/wp-includes/cron.php    2021-02-02 13:03:37 UTC (rev 50142)
+++ trunk/src/wp-includes/cron.php      2021-02-02 13:47:46 UTC (rev 50143)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -23,6 +23,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 2.1.0
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 5.1.0 Return value modified to boolean indicating success or failure,
</span><span class="cx" style="display: block; padding: 0 10px">  *              {@see 'pre_schedule_event'} filter added to short-circuit the function.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.7.0 The `$wp_error` parameter was added.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @link https://developer.wordpress.org/reference/functions/wp_schedule_single_event/
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -32,11 +33,19 @@
</span><span class="cx" style="display: block; padding: 0 10px">  *                           hook's callback function. Each value in the array is passed
</span><span class="cx" style="display: block; padding: 0 10px">  *                           to the callback as an individual parameter. The array keys
</span><span class="cx" style="display: block; padding: 0 10px">  *                           are ignored. Default: empty array.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return bool True if event successfully scheduled. False for failure.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @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.
</ins><span class="cx" style="display: block; padding: 0 10px">  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function wp_schedule_single_event( $timestamp, $hook, $args = array() ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function wp_schedule_single_event( $timestamp, $hook, $args = array(), $wp_error = false ) {
</ins><span class="cx" style="display: block; padding: 0 10px">         // Make sure timestamp is a positive integer.
</span><span class="cx" style="display: block; padding: 0 10px">        if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( $wp_error ) {
+                       return new WP_Error(
+                               'invalid_timestamp',
+                               __( 'Event timestamp must be a valid Unix timestamp.' )
+                       );
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 return false;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -62,12 +71,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * identical event within ten minutes and apply the {@see 'schedule_event'}
</span><span class="cx" style="display: block; padding: 0 10px">         * filter to check if another plugin has disallowed the event before scheduling.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * Return true if the event was scheduled, false if not.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * Return true if the event was scheduled, false or a WP_Error if not.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 5.1.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @since 5.7.0 The `$wp_error` parameter was added, and a `WP_Error` object can now be returned.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param null|bool $pre   Value to return instead. Default null to continue adding the event.
-        * @param stdClass  $event {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @param null|bool|WP_Error $pre      Value to return instead. Default null to continue adding the event.
+        * @param stdClass           $event    {
</ins><span class="cx" style="display: block; padding: 0 10px">          *     An object containing an event's data.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         *     @type string       $hook      Action hook to execute when the event is run.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -76,9 +86,22 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *     @type array        $args      Array containing each separate argument to pass to the hook's callback function.
</span><span class="cx" style="display: block; padding: 0 10px">         *     @type int          $interval  The interval time in seconds for the schedule. Only present for recurring events.
</span><span class="cx" style="display: block; padding: 0 10px">         * }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @param bool               $wp_error Whether to return a WP_Error on failure.
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $pre = apply_filters( 'pre_schedule_event', null, $event );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $pre = apply_filters( 'pre_schedule_event', null, $event, $wp_error );
+
</ins><span class="cx" style="display: block; padding: 0 10px">         if ( null !== $pre ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                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;
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 return $pre;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -125,6 +148,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( $duplicate ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( $wp_error ) {
+                       return new WP_Error(
+                               'duplicate_event',
+                               __( 'A duplicate event already exists.' )
+                       );
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 return false;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -147,6 +177,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        // A plugin disallowed this event.
</span><span class="cx" style="display: block; padding: 0 10px">        if ( ! $event ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( $wp_error ) {
+                       return new WP_Error(
+                               'schedule_event_false',
+                               __( 'A plugin disallowed this event.' )
+                       );
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 return false;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -155,7 +192,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                'args'     => $event->args,
</span><span class="cx" style="display: block; padding: 0 10px">        );
</span><span class="cx" style="display: block; padding: 0 10px">        uksort( $crons, 'strnatcasecmp' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        return _set_cron_array( $crons );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       return _set_cron_array( $crons, $wp_error );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -179,6 +217,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 2.1.0
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 5.1.0 Return value modified to boolean indicating success or failure,
</span><span class="cx" style="display: block; padding: 0 10px">  *              {@see 'pre_schedule_event'} filter added to short-circuit the function.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.7.0 The `$wp_error` parameter was added.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @link https://developer.wordpress.org/reference/functions/wp_schedule_event/
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -190,11 +229,19 @@
</span><span class="cx" style="display: block; padding: 0 10px">  *                           hook's callback function. Each value in the array is passed
</span><span class="cx" style="display: block; padding: 0 10px">  *                           to the callback as an individual parameter. The array keys
</span><span class="cx" style="display: block; padding: 0 10px">  *                           are ignored. Default: empty array.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return bool True if event successfully scheduled. False for failure.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @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.
</ins><span class="cx" style="display: block; padding: 0 10px">  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array() ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array(), $wp_error = false ) {
</ins><span class="cx" style="display: block; padding: 0 10px">         // Make sure timestamp is a positive integer.
</span><span class="cx" style="display: block; padding: 0 10px">        if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( $wp_error ) {
+                       return new WP_Error(
+                               'invalid_timestamp',
+                               __( 'Event timestamp must be a valid Unix timestamp.' )
+                       );
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 return false;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -201,6 +248,13 @@
</span><span class="cx" style="display: block; padding: 0 10px">        $schedules = wp_get_schedules();
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        if ( ! isset( $schedules[ $recurrence ] ) ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( $wp_error ) {
+                       return new WP_Error(
+                               'invalid_schedule',
+                               __( 'Event schedule does not exist.' )
+                       );
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 return false;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -213,8 +267,20 @@
</span><span class="cx" style="display: block; padding: 0 10px">        );
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /** This filter is documented in wp-includes/cron.php */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $pre = apply_filters( 'pre_schedule_event', null, $event );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $pre = apply_filters( 'pre_schedule_event', null, $event, $wp_error );
+
</ins><span class="cx" style="display: block; padding: 0 10px">         if ( null !== $pre ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                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;
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 return $pre;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -223,6 +289,13 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        // A plugin disallowed this event.
</span><span class="cx" style="display: block; padding: 0 10px">        if ( ! $event ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( $wp_error ) {
+                       return new WP_Error(
+                               'schedule_event_false',
+                               __( 'A plugin disallowed this event.' )
+                       );
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 return false;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -235,7 +308,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">                'interval' => $event->interval,
</span><span class="cx" style="display: block; padding: 0 10px">        );
</span><span class="cx" style="display: block; padding: 0 10px">        uksort( $crons, 'strnatcasecmp' );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        return _set_cron_array( $crons );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       return _set_cron_array( $crons, $wp_error );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -250,6 +324,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 2.1.0
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 5.1.0 Return value modified to boolean indicating success or failure,
</span><span class="cx" style="display: block; padding: 0 10px">  *              {@see 'pre_reschedule_event'} filter added to short-circuit the function.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.7.0 The `$wp_error` parameter was added.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @param int    $timestamp  Unix timestamp (UTC) for when the event was scheduled.
</span><span class="cx" style="display: block; padding: 0 10px">  * @param string $recurrence How often the event should subsequently recur.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -259,11 +334,19 @@
</span><span class="cx" style="display: block; padding: 0 10px">  *                           hook's callback function. Each value in the array is passed
</span><span class="cx" style="display: block; padding: 0 10px">  *                           to the callback as an individual parameter. The array keys
</span><span class="cx" style="display: block; padding: 0 10px">  *                           are ignored. Default: empty array.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return bool True if event successfully rescheduled. False for failure.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @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.
</ins><span class="cx" style="display: block; padding: 0 10px">  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array() ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array(), $wp_error = false ) {
</ins><span class="cx" style="display: block; padding: 0 10px">         // Make sure timestamp is a positive integer.
</span><span class="cx" style="display: block; padding: 0 10px">        if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( $wp_error ) {
+                       return new WP_Error(
+                               'invalid_timestamp',
+                               __( 'Event timestamp must be a valid Unix timestamp.' )
+                       );
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 return false;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -301,9 +384,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * rescheduled, false if not.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 5.1.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @since 5.7.0 The `$wp_error` parameter was added, and a `WP_Error` object can now be returned.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @param null|bool $pre   Value to return instead. Default null to continue adding the event.
-        * @param stdClass  $event {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @param null|bool|WP_Error $pre      Value to return instead. Default null to continue adding the event.
+        * @param stdClass           $event    {
</ins><span class="cx" style="display: block; padding: 0 10px">          *     An object containing an event's data.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         *     @type string       $hook      Action hook to execute when the event is run.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -312,14 +396,34 @@
</span><span class="cx" style="display: block; padding: 0 10px">         *     @type array        $args      Array containing each separate argument to pass to the hook's callback function.
</span><span class="cx" style="display: block; padding: 0 10px">         *     @type int          $interval  The interval time in seconds for the schedule. Only present for recurring events.
</span><span class="cx" style="display: block; padding: 0 10px">         * }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @param bool               $wp_error Whether to return a WP_Error on failure.
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><span class="cx" style="display: block; padding: 0 10px">        $pre = apply_filters( 'pre_reschedule_event', null, $event );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px">         if ( null !== $pre ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                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;
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 return $pre;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        // Now we assume something is wrong and fail to schedule.
</span><span class="cx" style="display: block; padding: 0 10px">        if ( 0 == $interval ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( $wp_error ) {
+                       return new WP_Error(
+                               'invalid_schedule',
+                               __( 'Event schedule does not exist.' )
+                       );
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 return false;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -331,7 +435,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $timestamp = $now + ( $interval - ( ( $now - $timestamp ) % $interval ) );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        return wp_schedule_event( $timestamp, $recurrence, $hook, $args );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ return wp_schedule_event( $timestamp, $recurrence, $hook, $args, $wp_error );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -343,6 +447,7 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 2.1.0
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 5.1.0 Return value modified to boolean indicating success or failure,
</span><span class="cx" style="display: block; padding: 0 10px">  *              {@see 'pre_unschedule_event'} filter added to short-circuit the function.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.7.0 The `$wp_error` parameter was added.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @param int    $timestamp Unix timestamp (UTC) of the event.
</span><span class="cx" style="display: block; padding: 0 10px">  * @param string $hook      Action hook of the event.
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -349,11 +454,19 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @param array  $args      Optional. Array containing each separate argument to pass to the hook's callback function.
</span><span class="cx" style="display: block; padding: 0 10px">  *                          Although not passed to a callback, these arguments are used to uniquely identify the
</span><span class="cx" style="display: block; padding: 0 10px">  *                          event, so they should be the same as those used when originally scheduling the event.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @return bool True if event successfully unscheduled. False for failure.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @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.
</ins><span class="cx" style="display: block; padding: 0 10px">  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function wp_unschedule_event( $timestamp, $hook, $args = array(), $wp_error = false ) {
</ins><span class="cx" style="display: block; padding: 0 10px">         // Make sure timestamp is a positive integer.
</span><span class="cx" style="display: block; padding: 0 10px">        if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                if ( $wp_error ) {
+                       return new WP_Error(
+                               'invalid_timestamp',
+                               __( 'Event timestamp must be a valid Unix timestamp.' )
+                       );
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 return false;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -367,14 +480,28 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * unscheduled, false if not.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 5.1.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @since 5.7.0 The `$wp_error` parameter was added, and a `WP_Error` object can now be returned.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @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.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @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.
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $pre = apply_filters( 'pre_unschedule_event', null, $timestamp, $hook, $args );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $pre = apply_filters( 'pre_unschedule_event', null, $timestamp, $hook, $args, $wp_error );
+
</ins><span class="cx" style="display: block; padding: 0 10px">         if ( null !== $pre ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                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;
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 return $pre;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -387,7 +514,8 @@
</span><span class="cx" style="display: block; padding: 0 10px">        if ( empty( $crons[ $timestamp ] ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                unset( $crons[ $timestamp ] );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        return _set_cron_array( $crons );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       return _set_cron_array( $crons, $wp_error );
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -406,16 +534,18 @@
</span><span class="cx" style="display: block; padding: 0 10px">  * @param array  $args Optional. Array containing each separate argument to pass to the hook's callback function.
</span><span class="cx" style="display: block; padding: 0 10px">  *                     Although not passed to a callback, these arguments are used to uniquely identify the
</span><span class="cx" style="display: block; padding: 0 10px">  *                     event, so they should be the same as those used when originally scheduling the event.
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @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.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @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.
</ins><span class="cx" style="display: block; padding: 0 10px">  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function wp_clear_scheduled_hook( $hook, $args = array() ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function wp_clear_scheduled_hook( $hook, $args = array(), $wp_error = false ) {
</ins><span class="cx" style="display: block; padding: 0 10px">         // Backward compatibility.
</span><span class="cx" style="display: block; padding: 0 10px">        // Previously, this function took the arguments as discrete vars rather than an array like the rest of the API.
</span><span class="cx" style="display: block; padding: 0 10px">        if ( ! is_array( $args ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                _deprecated_argument( __FUNCTION__, '3.0.0', __( 'This argument has changed to an array to match the behavior of the other cron functions.' ) );
</span><span class="cx" style="display: block; padding: 0 10px">                $args = array_slice( func_get_args(), 1 ); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $wp_error = false;
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -429,13 +559,27 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * if unscheduling one or more events fails.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 5.1.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @since 5.7.0 The `$wp_error` parameter was added, and a `WP_Error` object can now be returned.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @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.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @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.
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $pre = apply_filters( 'pre_clear_scheduled_hook', null, $hook, $args );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $pre = apply_filters( 'pre_clear_scheduled_hook', null, $hook, $args, $wp_error );
+
</ins><span class="cx" style="display: block; padding: 0 10px">         if ( null !== $pre ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                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;
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 return $pre;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -451,14 +595,26 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        $results = array();
</span><span class="cx" style="display: block; padding: 0 10px">        $key     = md5( serialize( $args ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px">         foreach ( $crons as $timestamp => $cron ) {
</span><span class="cx" style="display: block; padding: 0 10px">                if ( isset( $cron[ $hook ][ $key ] ) ) {
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                        $results[] = wp_unschedule_event( $timestamp, $hook, $args );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                 $results[] = wp_unschedule_event( $timestamp, $hook, $args, true );
</ins><span class="cx" style="display: block; padding: 0 10px">                 }
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( in_array( false, $results, true ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       $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;
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 return false;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
</ins><span class="cx" style="display: block; padding: 0 10px">         return count( $results );
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -474,12 +630,14 @@
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 4.9.0
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 5.1.0 Return value added to indicate success or failure.
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.7.0 The `$wp_error` parameter was added.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @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.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @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.
</ins><span class="cx" style="display: block; padding: 0 10px">  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function wp_unschedule_hook( $hook ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function wp_unschedule_hook( $hook, $wp_error = false ) {
</ins><span class="cx" style="display: block; padding: 0 10px">         /**
</span><span class="cx" style="display: block; padding: 0 10px">         * Filter to preflight or hijack clearing all events attached to the hook.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -491,12 +649,26 @@
</span><span class="cx" style="display: block; padding: 0 10px">         * if unscheduling one or more events fails.
</span><span class="cx" style="display: block; padding: 0 10px">         *
</span><span class="cx" style="display: block; padding: 0 10px">         * @since 5.1.0
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+         * @since 5.7.0 The `$wp_error` parameter was added, and a `WP_Error` object can now be returned.
</ins><span class="cx" style="display: block; padding: 0 10px">          *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-         * @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.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+  * @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.
</ins><span class="cx" style="display: block; padding: 0 10px">          */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        $pre = apply_filters( 'pre_unschedule_hook', null, $hook );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $pre = apply_filters( 'pre_unschedule_hook', null, $hook, $wp_error );
+
</ins><span class="cx" style="display: block; padding: 0 10px">         if ( null !== $pre ) {
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                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;
+               }
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 return $pre;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -524,10 +696,14 @@
</span><span class="cx" style="display: block; padding: 0 10px">        if ( empty( $results ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                return 0;
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        if ( _set_cron_array( $crons ) ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       $set = _set_cron_array( $crons, $wp_error );
+
+       if ( true === $set ) {
</ins><span class="cx" style="display: block; padding: 0 10px">                 return array_sum( $results );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        return false;
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       return $set;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -992,19 +1168,30 @@
</span><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * Updates the CRON option with the new CRON array.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * Updates the cron option with the new cron array.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 2.1.0
</span><span class="cx" style="display: block; padding: 0 10px">  * @since 5.1.0 Return value modified to outcome of update_option().
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @since 5.7.0 The `$wp_error` parameter was added.
</ins><span class="cx" style="display: block; padding: 0 10px">  *
</span><span class="cx" style="display: block; padding: 0 10px">  * @access private
</span><span class="cx" style="display: block; padding: 0 10px">  *
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">- * @param array $cron Cron info array from _get_cron_array().
- * @return bool True if cron array updated, false on failure.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ * @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.
</ins><span class="cx" style="display: block; padding: 0 10px">  */
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-function _set_cron_array( $cron ) {
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+function _set_cron_array( $cron, $wp_error = false ) {
</ins><span class="cx" style="display: block; padding: 0 10px">         $cron['version'] = 2;
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-        return update_option( 'cron', $cron );
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+ $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;
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px"> /**
</span></span></pre></div>
<a id="trunktestsphpunittestscronphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: trunk/tests/phpunit/tests/cron.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- trunk/tests/phpunit/tests/cron.php        2021-02-02 13:03:37 UTC (rev 50142)
+++ trunk/tests/phpunit/tests/cron.php  2021-02-02 13:47:46 UTC (rev 50143)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -271,8 +271,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // First one works.
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertTrue( wp_schedule_single_event( $ts1, $hook, $args ) );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                // Second one is ignored.
</del><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               // Subsequent ones are ignored.
</ins><span class="cx" style="display: block; padding: 0 10px">                 $this->assertFalse( wp_schedule_single_event( $ts2, $hook, $args ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $subsequent = wp_schedule_single_event( $ts2, $hook, $args, true );
+               $this->assertWPError( $subsequent );
+               $this->assertSame( 'duplicate_event', $subsequent->get_error_code() );
</ins><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // The next event should be at +5 minutes, not +3.
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertSame( $ts1, wp_next_scheduled( $hook, $args ) );
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -617,6 +621,11 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Third event fails.
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertFalse( wp_schedule_single_event( $ts3, $hook, $args ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               // Fourth event fails.
+               $subsequent = wp_schedule_single_event( $ts3, $hook, $args, true );
+               $this->assertWPError( $subsequent );
+               $this->assertSame( 'duplicate_event', $subsequent->get_error_code() );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -639,6 +648,12 @@
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">                // Third event fails.
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertFalse( wp_schedule_single_event( $ts3, $hook, $args ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               // Fourth event fails.
+               $subsequent = wp_schedule_single_event( $ts3, $hook, $args, true );
+               $this->assertWPError( $subsequent );
+               $this->assertSame( 'duplicate_event', $subsequent->get_error_code() );
+
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -659,6 +674,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                // Events within ten minutes should fail.
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertFalse( wp_schedule_single_event( $ts2, $hook, $args ) );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertFalse( wp_schedule_single_event( $ts3, $hook, $args ) );
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+               $subsequent = wp_schedule_single_event( $ts3, $hook, $args, true );
+               $this->assertWPError( $subsequent );
+               $this->assertSame( 'duplicate_event', $subsequent->get_error_code() );
</ins><span class="cx" style="display: block; padding: 0 10px">         }
</span><span class="cx" style="display: block; padding: 0 10px"> 
</span><span class="cx" style="display: block; padding: 0 10px">        /**
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -680,4 +699,411 @@
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertTrue( wp_schedule_single_event( $ts2, $hook, $args ) );
</span><span class="cx" style="display: block; padding: 0 10px">                $this->assertTrue( wp_schedule_single_event( $ts3, $hook, $args ) );
</span><span class="cx" style="display: block; padding: 0 10px">        }
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+
+       /**
+        * @ticket 49961
+        */
+       public function test_invalid_timestamp_for_event_returns_error() {
+               $single_event      = wp_schedule_single_event( -50, 'hook', array(), true );
+               $event             = wp_schedule_event( -50, 'daily', 'hook', array(), true );
+               $rescheduled_event = wp_reschedule_event( -50, 'daily', 'hook', array(), true );
+               $unscheduled_event = wp_unschedule_event( -50, 'hook', array(), true );
+
+               $this->assertWPError( $single_event );
+               $this->assertSame( 'invalid_timestamp', $single_event->get_error_code() );
+
+               $this->assertWPError( $event );
+               $this->assertSame( 'invalid_timestamp', $event->get_error_code() );
+
+               $this->assertWPError( $rescheduled_event );
+               $this->assertSame( 'invalid_timestamp', $rescheduled_event->get_error_code() );
+
+               $this->assertWPError( $unscheduled_event );
+               $this->assertSame( 'invalid_timestamp', $unscheduled_event->get_error_code() );
+       }
+
+       /**
+        * @ticket 49961
+        */
+       public function test_invalid_recurrence_for_event_returns_error() {
+               $event             = wp_schedule_event( time(), 'invalid', 'hook', array(), true );
+               $rescheduled_event = wp_reschedule_event( time(), 'invalid', 'hook', array(), true );
+
+               $this->assertWPError( $event );
+               $this->assertSame( 'invalid_schedule', $event->get_error_code() );
+
+               $this->assertWPError( $rescheduled_event );
+               $this->assertSame( 'invalid_schedule', $rescheduled_event->get_error_code() );
+       }
+
+       /**
+        * @ticket 49961
+        */
+       public function test_disallowed_event_returns_false_when_wp_error_is_set_to_false() {
+               add_filter( 'schedule_event', '__return_false' );
+
+               $single_event      = wp_schedule_single_event( time(), 'hook', array() );
+               $event             = wp_schedule_event( time(), 'daily', 'hook', array() );
+               $rescheduled_event = wp_reschedule_event( time(), 'daily', 'hook', array() );
+
+               $this->assertFalse( $single_event );
+               $this->assertFalse( $event );
+               $this->assertFalse( $rescheduled_event );
+       }
+
+       /**
+        * @ticket 49961
+        */
+       public function test_disallowed_event_returns_error_when_wp_error_is_set_to_true() {
+               add_filter( 'schedule_event', '__return_false' );
+
+               $single_event      = wp_schedule_single_event( time(), 'hook', array(), true );
+               $event             = wp_schedule_event( time(), 'daily', 'hook', array(), true );
+               $rescheduled_event = wp_reschedule_event( time(), 'daily', 'hook', array(), true );
+
+               $this->assertWPError( $single_event );
+               $this->assertSame( 'schedule_event_false', $single_event->get_error_code() );
+
+               $this->assertWPError( $event );
+               $this->assertSame( 'schedule_event_false', $event->get_error_code() );
+
+               $this->assertWPError( $rescheduled_event );
+               $this->assertSame( 'schedule_event_false', $rescheduled_event->get_error_code() );
+       }
+
+       /**
+        * @ticket 49961
+        */
+       public function test_schedule_short_circuit_with_error_returns_false_when_wp_error_is_set_to_false() {
+               $return_error = function() {
+                       return new WP_Error(
+                               'my_error',
+                               'An error ocurred'
+                       );
+               };
+
+               // Add filters which return a WP_Error:
+               add_filter( 'pre_schedule_event', $return_error );
+               add_filter( 'pre_reschedule_event', $return_error );
+
+               // Schedule events without the `$wp_error` parameter:
+               $single_event      = wp_schedule_single_event( time(), 'hook', array() );
+               $event             = wp_schedule_event( time(), 'daily', 'hook', array() );
+               $rescheduled_event = wp_reschedule_event( time(), 'daily', 'hook', array() );
+
+               // Ensure boolean false is returned:
+               $this->assertFalse( $single_event );
+               $this->assertFalse( $event );
+               $this->assertFalse( $rescheduled_event );
+       }
+
+       /**
+        * @ticket 49961
+        */
+       public function test_schedule_short_circuit_with_error_returns_error_when_wp_error_is_set_to_true() {
+               $return_error = function() {
+                       return new WP_Error(
+                               'my_error',
+                               'An error ocurred'
+                       );
+               };
+
+               // Add filters which return a WP_Error:
+               add_filter( 'pre_schedule_event', $return_error );
+               add_filter( 'pre_reschedule_event', $return_error );
+
+               // Schedule events with the `$wp_error` parameter:
+               $single_event      = wp_schedule_single_event( time(), 'hook', array(), true );
+               $event             = wp_schedule_event( time(), 'daily', 'hook', array(), true );
+               $rescheduled_event = wp_reschedule_event( time(), 'daily', 'hook', array(), true );
+
+               // Ensure the error object is returned:
+               $this->assertWPError( $single_event );
+               $this->assertSame( 'my_error', $single_event->get_error_code() );
+
+               $this->assertWPError( $event );
+               $this->assertSame( 'my_error', $event->get_error_code() );
+
+               $this->assertWPError( $rescheduled_event );
+               $this->assertSame( 'my_error', $rescheduled_event->get_error_code() );
+       }
+
+       /**
+        * @ticket 49961
+        */
+       public function test_schedule_short_circuit_with_false_returns_false_when_wp_error_is_set_to_false() {
+               // Add filters which return false:
+               add_filter( 'pre_schedule_event', '__return_false' );
+               add_filter( 'pre_reschedule_event', '__return_false' );
+
+               // Schedule events without the `$wp_error` parameter:
+               $single_event      = wp_schedule_single_event( time(), 'hook', array() );
+               $event             = wp_schedule_event( time(), 'daily', 'hook', array() );
+               $rescheduled_event = wp_reschedule_event( time(), 'daily', 'hook', array() );
+
+               // Ensure false is returned:
+               $this->assertFalse( $single_event );
+               $this->assertFalse( $event );
+               $this->assertFalse( $rescheduled_event );
+       }
+
+       /**
+        * @ticket 49961
+        */
+       public function test_schedule_short_circuit_with_false_returns_error_when_wp_error_is_set_to_true() {
+               // Add filters which return false:
+               add_filter( 'pre_schedule_event', '__return_false' );
+               add_filter( 'pre_reschedule_event', '__return_false' );
+
+               // Schedule events with the `$wp_error` parameter:
+               $single_event      = wp_schedule_single_event( time(), 'hook', array(), true );
+               $event             = wp_schedule_event( time(), 'daily', 'hook', array(), true );
+               $rescheduled_event = wp_reschedule_event( time(), 'daily', 'hook', array(), true );
+
+               // Ensure an error object is returned:
+               $this->assertWPError( $single_event );
+               $this->assertSame( 'pre_schedule_event_false', $single_event->get_error_code() );
+
+               $this->assertWPError( $event );
+               $this->assertSame( 'pre_schedule_event_false', $event->get_error_code() );
+
+               $this->assertWPError( $rescheduled_event );
+               $this->assertSame( 'pre_reschedule_event_false', $rescheduled_event->get_error_code() );
+       }
+
+       /**
+        * @ticket 49961
+        * @expectedDeprecated wp_clear_scheduled_hook
+        */
+       public function test_deprecated_argument_usage_of_wp_clear_scheduled_hook() {
+               add_filter(
+                       'pre_clear_scheduled_hook',
+                       function( $pre, $hook, $args, $wp_error ) {
+                               $this->assertSame( array( 1, 2, 3 ), $args );
+                               $this->assertFalse( $wp_error );
+
+                               return $pre;
+                       },
+                       10,
+                       4
+               );
+
+               $cleared = wp_clear_scheduled_hook( 'hook', 1, 2, 3 );
+
+               $this->assertSame( 0, $cleared );
+       }
+
+       /**
+        * @ticket 49961
+        */
+       public function test_clear_scheduled_hook_returns_default_pre_filter_error_when_wp_error_is_set_to_true() {
+               add_filter( 'pre_unschedule_event', '__return_false' );
+
+               wp_schedule_single_event( strtotime( '+1 hour' ), 'test_hook' );
+               wp_schedule_single_event( strtotime( '+2 hours' ), 'test_hook' );
+
+               $cleared = wp_clear_scheduled_hook( 'test_hook', array(), true );
+
+               $this->assertWPError( $cleared );
+               $this->assertSame(
+                       array(
+                               'pre_unschedule_event_false',
+                       ),
+                       $cleared->get_error_codes()
+               );
+               $this->assertCount( 2, $cleared->get_error_messages() );
+       }
+
+       /**
+        * @ticket 49961
+        */
+       public function test_clear_scheduled_hook_returns_custom_pre_filter_error_when_wp_error_is_set_to_true() {
+               add_filter(
+                       'pre_unschedule_event',
+                       function( $pre ) {
+                               return new WP_Error( 'error_code', 'error message' );
+                       }
+               );
+
+               wp_schedule_single_event( strtotime( '+1 hour' ), 'test_hook' );
+               wp_schedule_single_event( strtotime( '+2 hours' ), 'test_hook' );
+
+               $cleared = wp_clear_scheduled_hook( 'test_hook', array(), true );
+
+               $this->assertWPError( $cleared );
+               $this->assertSame(
+                       array(
+                               'error_code',
+                       ),
+                       $cleared->get_error_codes()
+               );
+               $this->assertSame(
+                       array(
+                               'error message',
+                               'error message',
+                       ),
+                       $cleared->get_error_messages()
+               );
+       }
+
+       /**
+        * @ticket 49961
+        */
+       public function test_unschedule_short_circuit_with_error_returns_false_when_wp_error_is_set_to_false() {
+               $return_error = function() {
+                       return new WP_Error(
+                               'my_error',
+                               'An error ocurred'
+                       );
+               };
+
+               // Add a filter which returns a WP_Error:
+               add_filter( 'pre_unschedule_hook', $return_error );
+
+               // Unschedule a hook without the `$wp_error` parameter:
+               $result = wp_unschedule_hook( 'hook' );
+
+               // Ensure boolean false is returned:
+               $this->assertFalse( $result );
+       }
+
+       /**
+        * @ticket 49961
+        */
+       public function test_unschedule_short_circuit_with_error_returns_error_when_wp_error_is_set_to_true() {
+               $return_error = function() {
+                       return new WP_Error(
+                               'my_error',
+                               'An error ocurred'
+                       );
+               };
+
+               // Add a filter which returns a WP_Error:
+               add_filter( 'pre_unschedule_hook', $return_error );
+
+               // Unschedule a hook with the `$wp_error` parameter:
+               $result = wp_unschedule_hook( 'hook', true );
+
+               // Ensure the error object is returned:
+               $this->assertWPError( $result );
+               $this->assertSame( 'my_error', $result->get_error_code() );
+       }
+
+       /**
+        * @ticket 49961
+        */
+       public function test_unschedule_short_circuit_with_false_returns_false_when_wp_error_is_set_to_false() {
+               // Add a filter which returns false:
+               add_filter( 'pre_unschedule_hook', '__return_false' );
+
+               // Unschedule a hook without the `$wp_error` parameter:
+               $result = wp_unschedule_hook( 'hook' );
+
+               // Ensure false is returned:
+               $this->assertFalse( $result );
+       }
+
+       /**
+        * @ticket 49961
+        */
+       public function test_unschedule_short_circuit_with_false_returns_error_when_wp_error_is_set_to_true() {
+               // Add a filter which returns false:
+               add_filter( 'pre_unschedule_hook', '__return_false' );
+
+               // Unchedule a hook with the `$wp_error` parameter:
+               $result = wp_unschedule_hook( 'hook', true );
+
+               // Ensure an error object is returned:
+               $this->assertWPError( $result );
+               $this->assertSame( 'pre_unschedule_hook_false', $result->get_error_code() );
+       }
+
+       /**
+        * @ticket 49961
+        */
+       public function test_cron_array_error_is_returned_when_scheduling_single_event() {
+               // Force update_option() to fail by setting the new value to match the existing:
+               add_filter(
+                       'pre_update_option_cron',
+                       function() {
+                               return get_option( 'cron' );
+                       }
+               );
+
+               // Attempt to schedule a valid event:
+               $event = wp_schedule_single_event( time(), 'hook', array(), true );
+
+               // Ensure an error object is returned:
+               $this->assertWPError( $event );
+               $this->assertSame( 'could_not_set', $event->get_error_code() );
+       }
+
+       /**
+        * @ticket 49961
+        */
+       public function test_cron_array_error_is_returned_when_scheduling_event() {
+               // Force update_option() to fail by setting the new value to match the existing:
+               add_filter(
+                       'pre_update_option_cron',
+                       function() {
+                               return get_option( 'cron' );
+                       }
+               );
+
+               // Attempt to schedule a valid event:
+               $event = wp_schedule_event( time(), 'daily', 'hook', array(), true );
+
+               // Ensure an error object is returned:
+               $this->assertWPError( $event );
+               $this->assertSame( 'could_not_set', $event->get_error_code() );
+       }
+
+       /**
+        * @ticket 49961
+        */
+       public function test_cron_array_error_is_returned_when_unscheduling_hook() {
+               // Schedule a valid event:
+               $event = wp_schedule_event( strtotime( '+1 hour' ), 'daily', 'hook', array(), true );
+
+               // Force update_option() to fail by setting the new value to match the existing:
+               add_filter(
+                       'pre_update_option_cron',
+                       function() {
+                               return get_option( 'cron' );
+                       }
+               );
+
+               // Attempt to unschedule the hook:
+               $unscheduled = wp_unschedule_hook( 'hook', true );
+
+               // Ensure an error object is returned:
+               $this->assertTrue( $event );
+               $this->assertWPError( $unscheduled );
+               $this->assertSame( 'could_not_set', $unscheduled->get_error_code() );
+       }
+
+       /**
+        * @ticket 49961
+        */
+       public function test_cron_array_error_is_returned_when_unscheduling_event() {
+               // Schedule a valid event:
+               $event = wp_schedule_event( strtotime( '+1 hour' ), 'daily', 'hook', array(), true );
+
+               // Force update_option() to fail by setting the new value to match the existing:
+               add_filter(
+                       'pre_update_option_cron',
+                       function() {
+                               return get_option( 'cron' );
+                       }
+               );
+
+               // Attempt to unschedule the event:
+               $unscheduled = wp_unschedule_event( wp_next_scheduled( 'hook' ), 'hook', array(), true );
+
+               // Ensure an error object is returned:
+               $this->assertTrue( $event );
+               $this->assertWPError( $unscheduled );
+               $this->assertSame( 'could_not_set', $unscheduled->get_error_code() );
+       }
+
</ins><span class="cx" style="display: block; padding: 0 10px"> }
</span></span></pre>
</div>
</div>

</body>
</html>