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 #49961

Built from https://develop.svn.wordpress.org/trunk@50143


git-svn-id: http://core.svn.wordpress.org/trunk@49822 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
John Blackbourn 2021-02-02 13:49:03 +00:00
parent c504140c32
commit 06c4b334fa
2 changed files with 236 additions and 49 deletions

View File

@ -23,6 +23,7 @@
* @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/
*
@ -32,11 +33,19 @@
* 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.
* @return bool True if event successfully scheduled. False for failure.
* @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;
}
@ -62,12 +71,13 @@ function wp_schedule_single_event( $timestamp, $hook, $args = array() ) {
* 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.
@ -76,9 +86,22 @@ function wp_schedule_single_event( $timestamp, $hook, $args = array() ) {
* @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;
}
@ -125,6 +148,13 @@ function wp_schedule_single_event( $timestamp, $hook, $args = array() ) {
}
if ( $duplicate ) {
if ( $wp_error ) {
return new WP_Error(
'duplicate_event',
__( 'A duplicate event already exists.' )
);
}
return false;
}
@ -147,6 +177,13 @@ function wp_schedule_single_event( $timestamp, $hook, $args = array() ) {
// A plugin disallowed this event.
if ( ! $event ) {
if ( $wp_error ) {
return new WP_Error(
'schedule_event_false',
__( 'A plugin disallowed this event.' )
);
}
return false;
}
@ -155,7 +192,8 @@ function wp_schedule_single_event( $timestamp, $hook, $args = array() ) {
'args' => $event->args,
);
uksort( $crons, 'strnatcasecmp' );
return _set_cron_array( $crons );
return _set_cron_array( $crons, $wp_error );
}
/**
@ -179,6 +217,7 @@ function wp_schedule_single_event( $timestamp, $hook, $args = array() ) {
* @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/
*
@ -190,17 +229,32 @@ function wp_schedule_single_event( $timestamp, $hook, $args = array() ) {
* 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.
* @return bool True if event successfully scheduled. False for failure.
* @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;
}
@ -213,8 +267,20 @@ function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array() ) {
);
/** 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;
}
@ -223,6 +289,13 @@ function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array() ) {
// A plugin disallowed this event.
if ( ! $event ) {
if ( $wp_error ) {
return new WP_Error(
'schedule_event_false',
__( 'A plugin disallowed this event.' )
);
}
return false;
}
@ -235,7 +308,8 @@ function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array() ) {
'interval' => $event->interval,
);
uksort( $crons, 'strnatcasecmp' );
return _set_cron_array( $crons );
return _set_cron_array( $crons, $wp_error );
}
/**
@ -250,6 +324,7 @@ function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array() ) {
* @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.
@ -259,11 +334,19 @@ function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array() ) {
* 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.
* @return bool True if event successfully rescheduled. False for failure.
* @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;
}
@ -301,9 +384,10 @@ function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array() )
* 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.
@ -312,14 +396,34 @@ function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array() )
* @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 );
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;
}
@ -331,7 +435,7 @@ function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array() )
$timestamp = $now + ( $interval - ( ( $now - $timestamp ) % $interval ) );
}
return wp_schedule_event( $timestamp, $recurrence, $hook, $args );
return wp_schedule_event( $timestamp, $recurrence, $hook, $args, $wp_error );
}
/**
@ -343,17 +447,26 @@ function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array() )
* @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.
* @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;
}
@ -367,14 +480,28 @@ function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
* 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;
}
@ -387,7 +514,8 @@ function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
if ( empty( $crons[ $timestamp ] ) ) {
unset( $crons[ $timestamp ] );
}
return _set_cron_array( $crons );
return _set_cron_array( $crons, $wp_error );
}
/**
@ -406,16 +534,18 @@ function wp_unschedule_event( $timestamp, $hook, $args = array() ) {
* @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 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 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
$wp_error = false;
}
/**
@ -429,13 +559,27 @@ function wp_clear_scheduled_hook( $hook, $args = array() ) {
* 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;
}
@ -451,14 +595,26 @@ function wp_clear_scheduled_hook( $hook, $args = array() ) {
$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 );
}
@ -474,12 +630,14 @@ function wp_clear_scheduled_hook( $hook, $args = array() ) {
*
* @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.
*
@ -491,12 +649,26 @@ function wp_unschedule_hook( $hook ) {
* 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;
}
@ -524,10 +696,14 @@ function wp_unschedule_hook( $hook ) {
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;
}
/**
@ -992,19 +1168,30 @@ function _get_cron_array() {
}
/**
* 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;
}
/**

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.7-alpha-50142';
$wp_version = '5.7-alpha-50143';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.