2006-03-07 22:43:59 +01:00
|
|
|
<?php
|
2008-09-04 21:12:40 +02:00
|
|
|
/**
|
2016-08-26 11:22:30 +02:00
|
|
|
* WordPress Cron API
|
2008-09-04 21:12:40 +02:00
|
|
|
*
|
|
|
|
* @package WordPress
|
|
|
|
*/
|
2006-04-06 07:25:50 +02:00
|
|
|
|
2008-09-04 21:12:40 +02:00
|
|
|
/**
|
2016-08-26 11:22:30 +02:00
|
|
|
* Schedules an event to run only once.
|
2008-09-04 21:12:40 +02:00
|
|
|
*
|
2022-10-11 11:02:14 +02:00
|
|
|
* Schedules a hook which will be triggered by WordPress at the specified UTC time.
|
2017-11-22 16:16:47 +01:00
|
|
|
* The action will trigger when someone visits your WordPress site if the scheduled
|
|
|
|
* time has passed.
|
2008-09-04 21:12:40 +02:00
|
|
|
*
|
2016-07-25 14:23:30 +02:00
|
|
|
* Note that scheduling an event to occur within 10 minutes of an existing event
|
2016-08-26 11:22:30 +02:00
|
|
|
* with the same action hook will be ignored unless you pass unique `$args` values
|
2016-07-25 14:23:30 +02:00
|
|
|
* for each scheduled event.
|
|
|
|
*
|
2017-11-22 16:16:47 +01:00
|
|
|
* Use wp_next_scheduled() to prevent duplicate events.
|
|
|
|
*
|
|
|
|
* Use wp_schedule_event() to schedule a recurring event.
|
|
|
|
*
|
2008-09-04 21:12:40 +02:00
|
|
|
* @since 2.1.0
|
2018-12-19 04:41:49 +01:00
|
|
|
* @since 5.1.0 Return value modified to boolean indicating success or failure,
|
2018-12-30 05:51:51 +01:00
|
|
|
* {@see 'pre_schedule_event'} filter added to short-circuit the function.
|
2021-02-02 14:49:03 +01:00
|
|
|
* @since 5.7.0 The `$wp_error` parameter was added.
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
*
|
2019-07-26 00:45:57 +02:00
|
|
|
* @link https://developer.wordpress.org/reference/functions/wp_schedule_single_event/
|
2008-09-04 21:12:40 +02:00
|
|
|
*
|
2017-11-22 16:16:47 +01:00
|
|
|
* @param int $timestamp Unix timestamp (UTC) for when to next run the event.
|
|
|
|
* @param string $hook Action hook to execute when the event is run.
|
2020-10-28 19:37:07 +01:00
|
|
|
* @param array $args Optional. Array containing arguments to pass to the
|
2021-02-03 12:08:04 +01:00
|
|
|
* 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.
|
2021-02-02 14:49:03 +01:00
|
|
|
* @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.
|
2008-09-04 21:12:40 +02:00
|
|
|
*/
|
2021-02-02 14:49:03 +01:00
|
|
|
function wp_schedule_single_event( $timestamp, $hook, $args = array(), $wp_error = false ) {
|
2020-01-29 01:45:18 +01:00
|
|
|
// Make sure timestamp is a positive integer.
|
2015-09-07 04:39:23 +02:00
|
|
|
if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
|
2021-02-02 14:49:03 +01:00
|
|
|
if ( $wp_error ) {
|
|
|
|
return new WP_Error(
|
|
|
|
'invalid_timestamp',
|
|
|
|
__( 'Event timestamp must be a valid Unix timestamp.' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-09-07 04:39:23 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
$event = (object) array(
|
|
|
|
'hook' => $hook,
|
|
|
|
'timestamp' => $timestamp,
|
|
|
|
'schedule' => false,
|
|
|
|
'args' => $args,
|
|
|
|
);
|
2017-11-22 16:16:47 +01:00
|
|
|
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
/**
|
|
|
|
* Filter to preflight or hijack scheduling an event.
|
|
|
|
*
|
|
|
|
* Returning a non-null value will short-circuit adding the event to the
|
|
|
|
* cron array, causing the function to return the filtered value instead.
|
|
|
|
*
|
|
|
|
* Both single events and recurring events are passed through this filter;
|
|
|
|
* single events have `$event->schedule` as false, whereas recurring events
|
2018-12-30 05:51:51 +01:00
|
|
|
* have this set to a recurrence from wp_get_schedules(). Recurring
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
* events also have the integer recurrence interval set as `$event->interval`.
|
|
|
|
*
|
|
|
|
* For plugins replacing wp-cron, it is recommended you check for an
|
2018-12-30 05:51:51 +01:00
|
|
|
* identical event within ten minutes and apply the {@see 'schedule_event'}
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
* filter to check if another plugin has disallowed the event before scheduling.
|
|
|
|
*
|
2021-02-02 14:49:03 +01:00
|
|
|
* Return true if the event was scheduled, false or a WP_Error if not.
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
*
|
2018-12-30 06:06:48 +01:00
|
|
|
* @since 5.1.0
|
2021-02-02 14:49:03 +01:00
|
|
|
* @since 5.7.0 The `$wp_error` parameter was added, and a `WP_Error` object can now be returned.
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
*
|
Code Modernization: Rename parameters that use reserved keywords in `phpunit/tests/cron.php`.
While using reserved PHP keywords as parameter name labels is allowed, in the context of function calls using named parameters in PHP 8.0+, this will easily lead to confusion. To avoid that, it is recommended not to use reserved keywords as function parameter names.
This commit:
* Renames the `$null` parameter to `$result` in `Tests_Cron::filter_pre_schedule_event_filter()`.
* Amends the `$pre` parameter of the `pre_schedule_event` filter for consistency.
Follow-up to [52946], [52996], [52997], [52998], [53003], [53014], [53029], [53039], [53116], [53117], [53137], [53174], [53184], [53185], [53192], [53193], [53198], [53203], [53207], [53215], [53216], [53220], [53230], [53232], [53236], [53239], [53240], [53242], [53243], [53245], [53246], [53257], [53269], [53270], [53271], [53272], [53273], [53274], [53275], [53276], [53277], [53281], [53283], [53284], [53285], [53287], [53364], [53365], [54927], [54929], [54930], [54931], [54932], [54933], [54938], [54943], [54944], [54945], [54946], [54947], [54948], [54950], [54951], [54952], [54956], [54959], [54960], [54961], [54962], [54964], [54965], [54969], [54970], [54971], [54972], [54996], [55000], [55011], [55013], [55014], [55015], [55016], [55017], [55020], [55021], [55023], [55027], [55028], [55034], [55036], [55037].
Props jrf, aristath, poena, justinahinon, SergeyBiryukov.
See #56788.
Built from https://develop.svn.wordpress.org/trunk@55038
git-svn-id: http://core.svn.wordpress.org/trunk@54571 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2023-01-09 01:42:15 +01:00
|
|
|
* @param null|bool|WP_Error $result The value to return instead. Default null to continue adding the event.
|
2021-02-02 14:49:03 +01:00
|
|
|
* @param stdClass $event {
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
* An object containing an event's data.
|
|
|
|
*
|
|
|
|
* @type string $hook Action hook to execute when the event is run.
|
|
|
|
* @type int $timestamp Unix timestamp (UTC) for when to next run the event.
|
|
|
|
* @type string|false $schedule How often the event should subsequently recur.
|
|
|
|
* @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.
|
|
|
|
* }
|
2021-02-02 14:49:03 +01:00
|
|
|
* @param bool $wp_error Whether to return a WP_Error on failure.
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
*/
|
2021-02-02 14:49:03 +01:00
|
|
|
$pre = apply_filters( 'pre_schedule_event', null, $event, $wp_error );
|
|
|
|
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
if ( null !== $pre ) {
|
2021-02-02 14:49:03 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
return $pre;
|
|
|
|
}
|
|
|
|
|
2019-03-16 07:42:50 +01:00
|
|
|
/*
|
|
|
|
* Check for a duplicated event.
|
|
|
|
*
|
|
|
|
* Don't schedule an event if there's already an identical event
|
|
|
|
* within 10 minutes.
|
|
|
|
*
|
|
|
|
* When scheduling events within ten minutes of the current time,
|
|
|
|
* all past identical events are considered duplicates.
|
|
|
|
*
|
|
|
|
* When scheduling an event with a past timestamp (ie, before the
|
|
|
|
* current time) all events scheduled within the next ten minutes
|
|
|
|
* are considered duplicates.
|
|
|
|
*/
|
2021-10-18 14:51:58 +02:00
|
|
|
$crons = _get_cron_array();
|
|
|
|
|
2019-03-16 07:42:50 +01:00
|
|
|
$key = md5( serialize( $event->args ) );
|
|
|
|
$duplicate = false;
|
|
|
|
|
|
|
|
if ( $event->timestamp < time() + 10 * MINUTE_IN_SECONDS ) {
|
|
|
|
$min_timestamp = 0;
|
|
|
|
} else {
|
|
|
|
$min_timestamp = $event->timestamp - 10 * MINUTE_IN_SECONDS;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $event->timestamp < time() ) {
|
|
|
|
$max_timestamp = time() + 10 * MINUTE_IN_SECONDS;
|
|
|
|
} else {
|
|
|
|
$max_timestamp = $event->timestamp + 10 * MINUTE_IN_SECONDS;
|
|
|
|
}
|
|
|
|
|
|
|
|
foreach ( $crons as $event_timestamp => $cron ) {
|
|
|
|
if ( $event_timestamp < $min_timestamp ) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
if ( $event_timestamp > $max_timestamp ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if ( isset( $cron[ $event->hook ][ $key ] ) ) {
|
|
|
|
$duplicate = true;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $duplicate ) {
|
2021-02-02 14:49:03 +01:00
|
|
|
if ( $wp_error ) {
|
|
|
|
return new WP_Error(
|
|
|
|
'duplicate_event',
|
|
|
|
__( 'A duplicate event already exists.' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2013-11-19 06:01:11 +01:00
|
|
|
/**
|
2019-01-24 00:23:52 +01:00
|
|
|
* Modify an event before it is scheduled.
|
2013-11-19 06:01:11 +01:00
|
|
|
*
|
|
|
|
* @since 3.1.0
|
|
|
|
*
|
2020-05-03 21:38:08 +02:00
|
|
|
* @param stdClass|false $event {
|
|
|
|
* An object containing an event's data, or boolean false to prevent the event from being scheduled.
|
2016-08-26 11:22:30 +02:00
|
|
|
*
|
2017-11-22 16:16:47 +01:00
|
|
|
* @type string $hook Action hook to execute when the event is run.
|
|
|
|
* @type int $timestamp Unix timestamp (UTC) for when to next run the event.
|
|
|
|
* @type string|false $schedule How often the event should subsequently recur.
|
|
|
|
* @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.
|
2016-08-26 11:22:30 +02:00
|
|
|
* }
|
2013-11-19 06:01:11 +01:00
|
|
|
*/
|
|
|
|
$event = apply_filters( 'schedule_event', $event );
|
2010-10-18 22:50:35 +02:00
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// A plugin disallowed this event.
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( ! $event ) {
|
2021-02-02 14:49:03 +01:00
|
|
|
if ( $wp_error ) {
|
|
|
|
return new WP_Error(
|
|
|
|
'schedule_event_false',
|
|
|
|
__( 'A plugin disallowed this event.' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-10-18 22:50:35 +02:00
|
|
|
return false;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2010-10-18 22:50:35 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
$crons[ $event->timestamp ][ $event->hook ][ $key ] = array(
|
|
|
|
'schedule' => $event->schedule,
|
|
|
|
'args' => $event->args,
|
|
|
|
);
|
|
|
|
uksort( $crons, 'strnatcasecmp' );
|
2021-02-02 14:49:03 +01:00
|
|
|
|
|
|
|
return _set_cron_array( $crons, $wp_error );
|
2006-03-07 22:43:59 +01:00
|
|
|
}
|
2006-04-06 07:25:50 +02:00
|
|
|
|
2008-09-04 21:12:40 +02:00
|
|
|
/**
|
2017-11-22 16:16:47 +01:00
|
|
|
* Schedules a recurring event.
|
2008-09-04 21:12:40 +02:00
|
|
|
*
|
2017-11-22 16:16:47 +01:00
|
|
|
* Schedules a hook which will be triggered by WordPress at the specified interval.
|
|
|
|
* The action will trigger when someone visits your WordPress site if the scheduled
|
|
|
|
* time has passed.
|
2008-09-04 21:12:40 +02:00
|
|
|
*
|
2017-11-22 16:16:47 +01:00
|
|
|
* Valid values for the recurrence are 'hourly', 'daily', and 'twicedaily'. These can
|
2016-05-23 20:59:27 +02:00
|
|
|
* be extended using the {@see 'cron_schedules'} filter in wp_get_schedules().
|
2009-04-08 22:16:59 +02:00
|
|
|
*
|
2017-11-22 16:16:47 +01:00
|
|
|
* Use wp_next_scheduled() to prevent duplicate events.
|
|
|
|
*
|
|
|
|
* Use wp_schedule_single_event() to schedule a non-recurring event.
|
2010-12-02 17:27:16 +01:00
|
|
|
*
|
2008-09-04 21:12:40 +02:00
|
|
|
* @since 2.1.0
|
2018-12-19 04:41:49 +01:00
|
|
|
* @since 5.1.0 Return value modified to boolean indicating success or failure,
|
2018-12-30 05:51:51 +01:00
|
|
|
* {@see 'pre_schedule_event'} filter added to short-circuit the function.
|
2021-02-02 14:49:03 +01:00
|
|
|
* @since 5.7.0 The `$wp_error` parameter was added.
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
*
|
2019-07-26 00:45:57 +02:00
|
|
|
* @link https://developer.wordpress.org/reference/functions/wp_schedule_event/
|
2008-09-04 21:12:40 +02:00
|
|
|
*
|
2017-11-22 16:16:47 +01:00
|
|
|
* @param int $timestamp Unix timestamp (UTC) for when to next run the event.
|
2020-10-28 19:37:07 +01:00
|
|
|
* @param string $recurrence How often the event should subsequently recur.
|
|
|
|
* See wp_get_schedules() for accepted values.
|
2017-11-22 16:16:47 +01:00
|
|
|
* @param string $hook Action hook to execute when the event is run.
|
2020-10-28 19:37:07 +01:00
|
|
|
* @param array $args Optional. Array containing arguments to pass to the
|
2021-02-03 12:08:04 +01:00
|
|
|
* 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.
|
2021-02-02 14:49:03 +01:00
|
|
|
* @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.
|
2008-09-04 21:12:40 +02:00
|
|
|
*/
|
2021-02-02 14:49:03 +01:00
|
|
|
function wp_schedule_event( $timestamp, $recurrence, $hook, $args = array(), $wp_error = false ) {
|
2020-01-29 01:45:18 +01:00
|
|
|
// Make sure timestamp is a positive integer.
|
2015-09-07 04:39:23 +02:00
|
|
|
if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
|
2021-02-02 14:49:03 +01:00
|
|
|
if ( $wp_error ) {
|
|
|
|
return new WP_Error(
|
|
|
|
'invalid_timestamp',
|
|
|
|
__( 'Event timestamp must be a valid Unix timestamp.' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-09-07 04:39:23 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-03-07 22:43:59 +01:00
|
|
|
$schedules = wp_get_schedules();
|
2010-10-18 22:50:35 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( ! isset( $schedules[ $recurrence ] ) ) {
|
2021-02-02 14:49:03 +01:00
|
|
|
if ( $wp_error ) {
|
|
|
|
return new WP_Error(
|
|
|
|
'invalid_schedule',
|
|
|
|
__( 'Event schedule does not exist.' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2006-03-07 22:43:59 +01:00
|
|
|
return false;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2010-10-18 22:50:35 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
$event = (object) array(
|
|
|
|
'hook' => $hook,
|
|
|
|
'timestamp' => $timestamp,
|
|
|
|
'schedule' => $recurrence,
|
|
|
|
'args' => $args,
|
|
|
|
'interval' => $schedules[ $recurrence ]['interval'],
|
|
|
|
);
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
|
|
|
|
/** This filter is documented in wp-includes/cron.php */
|
2021-02-02 14:49:03 +01:00
|
|
|
$pre = apply_filters( 'pre_schedule_event', null, $event, $wp_error );
|
|
|
|
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
if ( null !== $pre ) {
|
2021-02-02 14:49:03 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
return $pre;
|
|
|
|
}
|
|
|
|
|
2013-11-19 06:01:11 +01:00
|
|
|
/** This filter is documented in wp-includes/cron.php */
|
|
|
|
$event = apply_filters( 'schedule_event', $event );
|
2010-10-18 22:50:35 +02:00
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// A plugin disallowed this event.
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( ! $event ) {
|
2021-02-02 14:49:03 +01:00
|
|
|
if ( $wp_error ) {
|
|
|
|
return new WP_Error(
|
|
|
|
'schedule_event_false',
|
|
|
|
__( 'A plugin disallowed this event.' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2010-10-18 22:50:35 +02:00
|
|
|
return false;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2010-10-18 22:50:35 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
$key = md5( serialize( $event->args ) );
|
2010-10-18 22:50:35 +02:00
|
|
|
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
$crons = _get_cron_array();
|
Code Modernization: Check the return type of `_get_cron_array()` in `wp_schedule_event()`.
This fixes a "Deprecated: Automatic conversion of false to array is deprecated" warning on PHP 8.1.
In `wp_schedule_event()`, the cron info array is retrieved via a call to `_get_cron_array()`, but as the documentation (correctly) states, the return type of that function is `array|false`, where `false` is returned for a virgin site, with no cron jobs scheduled yet.
However, no type check is done on the return value, and the `wp_schedule_event()` function just blindly continues by assigning a value to a subkey of the `$crons` "array".
Fixed by adding validation for the returned value from `_get_cron_array()` and initializing an empty array if `false` was returned.
Reference: [https://developer.wordpress.org/reference/functions/_get_cron_array/ WordPress Developer Resources: _get_cron_array()]
Props jrf, hellofromTonya, lucatume, pbearne, iluy, pedromendonca, SergeyBiryukov.
See #53635.
Built from https://develop.svn.wordpress.org/trunk@51619
git-svn-id: http://core.svn.wordpress.org/trunk@51225 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-08-16 17:23:57 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
$crons[ $event->timestamp ][ $event->hook ][ $key ] = array(
|
|
|
|
'schedule' => $event->schedule,
|
|
|
|
'args' => $event->args,
|
|
|
|
'interval' => $event->interval,
|
|
|
|
);
|
|
|
|
uksort( $crons, 'strnatcasecmp' );
|
2021-02-02 14:49:03 +01:00
|
|
|
|
|
|
|
return _set_cron_array( $crons, $wp_error );
|
2006-03-07 22:43:59 +01:00
|
|
|
}
|
|
|
|
|
2008-09-04 21:12:40 +02:00
|
|
|
/**
|
2017-11-22 16:16:47 +01:00
|
|
|
* Reschedules a recurring event.
|
2008-09-04 21:12:40 +02:00
|
|
|
*
|
2022-10-11 11:02:14 +02:00
|
|
|
* Mainly for internal use, this takes the UTC timestamp of a previously run
|
2018-09-03 06:04:25 +02:00
|
|
|
* recurring event and reschedules it for its next run.
|
|
|
|
*
|
2018-12-30 05:51:51 +01:00
|
|
|
* To change upcoming scheduled events, use wp_schedule_event() to
|
2018-09-03 06:04:25 +02:00
|
|
|
* change the recurrence frequency.
|
|
|
|
*
|
2008-09-04 21:12:40 +02:00
|
|
|
* @since 2.1.0
|
2018-12-19 04:41:49 +01:00
|
|
|
* @since 5.1.0 Return value modified to boolean indicating success or failure,
|
2018-12-30 05:51:51 +01:00
|
|
|
* {@see 'pre_reschedule_event'} filter added to short-circuit the function.
|
2021-02-02 14:49:03 +01:00
|
|
|
* @since 5.7.0 The `$wp_error` parameter was added.
|
2008-09-04 21:12:40 +02:00
|
|
|
*
|
2018-09-03 06:04:25 +02:00
|
|
|
* @param int $timestamp Unix timestamp (UTC) for when the event was scheduled.
|
2020-10-28 19:37:07 +01:00
|
|
|
* @param string $recurrence How often the event should subsequently recur.
|
|
|
|
* See wp_get_schedules() for accepted values.
|
2017-11-22 16:16:47 +01:00
|
|
|
* @param string $hook Action hook to execute when the event is run.
|
2020-10-28 19:37:07 +01:00
|
|
|
* @param array $args Optional. Array containing arguments to pass to the
|
2021-02-03 12:08:04 +01:00
|
|
|
* 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.
|
2021-02-02 14:49:03 +01:00
|
|
|
* @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.
|
2008-09-04 21:12:40 +02:00
|
|
|
*/
|
2021-02-02 14:49:03 +01:00
|
|
|
function wp_reschedule_event( $timestamp, $recurrence, $hook, $args = array(), $wp_error = false ) {
|
2020-01-29 01:45:18 +01:00
|
|
|
// Make sure timestamp is a positive integer.
|
2015-09-07 04:39:23 +02:00
|
|
|
if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
|
2021-02-02 14:49:03 +01:00
|
|
|
if ( $wp_error ) {
|
|
|
|
return new WP_Error(
|
|
|
|
'invalid_timestamp',
|
|
|
|
__( 'Event timestamp must be a valid Unix timestamp.' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-09-07 04:39:23 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2006-03-07 22:43:59 +01:00
|
|
|
$schedules = wp_get_schedules();
|
2017-12-01 00:11:00 +01:00
|
|
|
$interval = 0;
|
2006-04-06 07:25:50 +02:00
|
|
|
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
// First we try to get the interval from the schedule.
|
2014-08-01 22:57:16 +02:00
|
|
|
if ( isset( $schedules[ $recurrence ] ) ) {
|
|
|
|
$interval = $schedules[ $recurrence ]['interval'];
|
|
|
|
}
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
|
|
|
|
// Now we try to get it from the saved interval in case the schedule disappears.
|
|
|
|
if ( 0 === $interval ) {
|
|
|
|
$scheduled_event = wp_get_scheduled_event( $hook, $args, $timestamp );
|
|
|
|
if ( $scheduled_event && isset( $scheduled_event->interval ) ) {
|
|
|
|
$interval = $scheduled_event->interval;
|
|
|
|
}
|
2014-08-01 22:57:16 +02:00
|
|
|
}
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
|
|
|
|
$event = (object) array(
|
|
|
|
'hook' => $hook,
|
|
|
|
'timestamp' => $timestamp,
|
|
|
|
'schedule' => $recurrence,
|
|
|
|
'args' => $args,
|
|
|
|
'interval' => $interval,
|
|
|
|
);
|
|
|
|
|
|
|
|
/**
|
2022-09-20 22:32:09 +02:00
|
|
|
* Filter to preflight or hijack rescheduling of a recurring event.
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
*
|
|
|
|
* Returning a non-null value will short-circuit the normal rescheduling
|
|
|
|
* process, causing the function to return the filtered value instead.
|
|
|
|
*
|
|
|
|
* For plugins replacing wp-cron, return true if the event was successfully
|
2022-10-11 11:02:14 +02:00
|
|
|
* rescheduled, false or a WP_Error if not.
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
*
|
2018-12-30 06:06:48 +01:00
|
|
|
* @since 5.1.0
|
2021-02-02 14:49:03 +01:00
|
|
|
* @since 5.7.0 The `$wp_error` parameter was added, and a `WP_Error` object can now be returned.
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
*
|
2021-02-02 14:49:03 +01:00
|
|
|
* @param null|bool|WP_Error $pre Value to return instead. Default null to continue adding the event.
|
|
|
|
* @param stdClass $event {
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
* An object containing an event's data.
|
|
|
|
*
|
2022-09-20 22:32:09 +02:00
|
|
|
* @type string $hook Action hook to execute when the event is run.
|
|
|
|
* @type int $timestamp Unix timestamp (UTC) for when to next run the event.
|
|
|
|
* @type string $schedule How often the event should subsequently recur.
|
|
|
|
* @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.
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
* }
|
2021-02-02 14:49:03 +01:00
|
|
|
* @param bool $wp_error Whether to return a WP_Error on failure.
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
*/
|
2021-02-20 13:11:03 +01:00
|
|
|
$pre = apply_filters( 'pre_reschedule_event', null, $event, $wp_error );
|
2021-02-02 14:49:03 +01:00
|
|
|
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
if ( null !== $pre ) {
|
2021-02-02 14:49:03 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
return $pre;
|
|
|
|
}
|
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// Now we assume something is wrong and fail to schedule.
|
2014-08-01 22:57:16 +02:00
|
|
|
if ( 0 == $interval ) {
|
2021-02-02 14:49:03 +01:00
|
|
|
if ( $wp_error ) {
|
|
|
|
return new WP_Error(
|
|
|
|
'invalid_schedule',
|
|
|
|
__( 'Event schedule does not exist.' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2006-03-07 22:43:59 +01:00
|
|
|
return false;
|
2014-08-01 22:57:16 +02:00
|
|
|
}
|
2006-03-07 22:43:59 +01:00
|
|
|
|
2009-04-17 02:23:48 +02:00
|
|
|
$now = time();
|
|
|
|
|
2014-08-01 22:57:16 +02:00
|
|
|
if ( $timestamp >= $now ) {
|
2010-06-30 02:05:18 +02:00
|
|
|
$timestamp = $now + $interval;
|
2014-08-01 22:57:16 +02:00
|
|
|
} else {
|
|
|
|
$timestamp = $now + ( $interval - ( ( $now - $timestamp ) % $interval ) );
|
|
|
|
}
|
2006-04-06 07:25:50 +02:00
|
|
|
|
2021-02-02 14:49:03 +01:00
|
|
|
return wp_schedule_event( $timestamp, $recurrence, $hook, $args, $wp_error );
|
2006-03-07 22:43:59 +01:00
|
|
|
}
|
|
|
|
|
2008-09-04 21:12:40 +02:00
|
|
|
/**
|
2016-08-26 11:22:30 +02:00
|
|
|
* Unschedule a previously scheduled event.
|
2008-09-04 21:12:40 +02:00
|
|
|
*
|
2016-08-26 11:22:30 +02:00
|
|
|
* The $timestamp and $hook parameters are required so that the event can be
|
2008-09-04 21:12:40 +02:00
|
|
|
* identified.
|
|
|
|
*
|
|
|
|
* @since 2.1.0
|
2018-12-19 04:41:49 +01:00
|
|
|
* @since 5.1.0 Return value modified to boolean indicating success or failure,
|
2018-12-30 05:51:51 +01:00
|
|
|
* {@see 'pre_unschedule_event'} filter added to short-circuit the function.
|
2021-02-02 14:49:03 +01:00
|
|
|
* @since 5.7.0 The `$wp_error` parameter was added.
|
2008-09-04 21:12:40 +02:00
|
|
|
*
|
2017-11-22 16:16:47 +01:00
|
|
|
* @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.
|
2021-02-03 12:08:04 +01:00
|
|
|
* Default empty array.
|
2021-02-02 14:49:03 +01:00
|
|
|
* @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.
|
2008-09-04 21:12:40 +02:00
|
|
|
*/
|
2021-02-02 14:49:03 +01:00
|
|
|
function wp_unschedule_event( $timestamp, $hook, $args = array(), $wp_error = false ) {
|
2020-01-29 01:45:18 +01:00
|
|
|
// Make sure timestamp is a positive integer.
|
2015-09-07 04:39:23 +02:00
|
|
|
if ( ! is_numeric( $timestamp ) || $timestamp <= 0 ) {
|
2021-02-02 14:49:03 +01:00
|
|
|
if ( $wp_error ) {
|
|
|
|
return new WP_Error(
|
|
|
|
'invalid_timestamp',
|
|
|
|
__( 'Event timestamp must be a valid Unix timestamp.' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2015-09-07 04:39:23 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
/**
|
|
|
|
* Filter to preflight or hijack unscheduling of events.
|
|
|
|
*
|
|
|
|
* Returning a non-null value will short-circuit the normal unscheduling
|
|
|
|
* process, causing the function to return the filtered value instead.
|
|
|
|
*
|
|
|
|
* For plugins replacing wp-cron, return true if the event was successfully
|
2022-10-11 11:02:14 +02:00
|
|
|
* unscheduled, false or a WP_Error if not.
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
*
|
2018-12-30 06:06:48 +01:00
|
|
|
* @since 5.1.0
|
2021-02-02 14:49:03 +01:00
|
|
|
* @since 5.7.0 The `$wp_error` parameter was added, and a `WP_Error` object can now be returned.
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
*
|
2021-02-02 14:49:03 +01:00
|
|
|
* @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.
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
*/
|
2021-02-02 14:49:03 +01:00
|
|
|
$pre = apply_filters( 'pre_unschedule_event', null, $timestamp, $hook, $args, $wp_error );
|
|
|
|
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
if ( null !== $pre ) {
|
2021-02-02 14:49:03 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
return $pre;
|
|
|
|
}
|
|
|
|
|
2006-09-14 01:54:15 +02:00
|
|
|
$crons = _get_cron_array();
|
2017-12-01 00:11:00 +01:00
|
|
|
$key = md5( serialize( $args ) );
|
|
|
|
unset( $crons[ $timestamp ][ $hook ][ $key ] );
|
|
|
|
if ( empty( $crons[ $timestamp ][ $hook ] ) ) {
|
|
|
|
unset( $crons[ $timestamp ][ $hook ] );
|
|
|
|
}
|
|
|
|
if ( empty( $crons[ $timestamp ] ) ) {
|
|
|
|
unset( $crons[ $timestamp ] );
|
|
|
|
}
|
2021-02-02 14:49:03 +01:00
|
|
|
|
|
|
|
return _set_cron_array( $crons, $wp_error );
|
2006-03-07 22:43:59 +01:00
|
|
|
}
|
|
|
|
|
2008-09-04 21:12:40 +02:00
|
|
|
/**
|
2017-06-30 06:54:41 +02:00
|
|
|
* Unschedules all events attached to the hook with the specified arguments.
|
2008-09-04 21:12:40 +02:00
|
|
|
*
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
* Warning: This function may return Boolean FALSE, but may also return a non-Boolean
|
|
|
|
* value which evaluates to FALSE. For information about casting to booleans see the
|
2020-01-20 04:14:06 +01:00
|
|
|
* {@link https://www.php.net/manual/en/language.types.boolean.php PHP documentation}. Use
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
* the `===` operator for testing the return value of this function.
|
|
|
|
*
|
2008-09-04 21:12:40 +02:00
|
|
|
* @since 2.1.0
|
2018-12-19 04:41:49 +01:00
|
|
|
* @since 5.1.0 Return value modified to indicate success or failure,
|
2018-12-30 05:51:51 +01:00
|
|
|
* {@see 'pre_clear_scheduled_hook'} filter added to short-circuit the function.
|
2021-02-03 11:58:07 +01:00
|
|
|
* @since 5.7.0 The `$wp_error` parameter was added.
|
2008-09-04 21:12:40 +02:00
|
|
|
*
|
2021-02-03 11:58:07 +01:00
|
|
|
* @param string $hook Action hook, the execution of which will be unscheduled.
|
|
|
|
* @param array $args Optional. Array containing each separate argument to pass to the hook's callback function.
|
|
|
|
* Although not passed to a callback, these arguments are used to uniquely identify the
|
|
|
|
* event, so they should be the same as those used when originally scheduling the event.
|
2021-02-03 12:08:04 +01:00
|
|
|
* Default empty array.
|
2021-02-03 11:58:07 +01:00
|
|
|
* @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false.
|
2021-02-02 14:49:03 +01:00
|
|
|
* @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.
|
2008-09-04 21:12:40 +02:00
|
|
|
*/
|
2021-02-02 14:49:03 +01:00
|
|
|
function wp_clear_scheduled_hook( $hook, $args = array(), $wp_error = false ) {
|
2020-01-29 01:45:18 +01:00
|
|
|
// Backward compatibility.
|
|
|
|
// Previously, this function took the arguments as discrete vars rather than an array like the rest of the API.
|
2017-12-01 00:11:00 +01:00
|
|
|
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.' ) );
|
2021-02-02 19:04:03 +01:00
|
|
|
$args = array_slice( func_get_args(), 1 ); // phpcs:ignore PHPCompatibility.FunctionUse.ArgumentFunctionsReportCurrentValue.NeedsInspection
|
2021-02-02 14:49:03 +01:00
|
|
|
$wp_error = false;
|
2009-12-24 12:12:04 +01:00
|
|
|
}
|
2007-02-27 16:24:54 +01:00
|
|
|
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
/**
|
|
|
|
* Filter to preflight or hijack clearing a scheduled hook.
|
|
|
|
*
|
|
|
|
* Returning a non-null value will short-circuit the normal unscheduling
|
|
|
|
* process, causing the function to return the filtered value instead.
|
|
|
|
*
|
|
|
|
* For plugins replacing wp-cron, return the number of events successfully
|
|
|
|
* unscheduled (zero if no events were registered with the hook) or false
|
2022-10-11 11:02:14 +02:00
|
|
|
* or a WP_Error if unscheduling one or more events fails.
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
*
|
2018-12-30 06:06:48 +01:00
|
|
|
* @since 5.1.0
|
2021-02-02 14:49:03 +01:00
|
|
|
* @since 5.7.0 The `$wp_error` parameter was added, and a `WP_Error` object can now be returned.
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
*
|
2021-02-02 14:49:03 +01:00
|
|
|
* @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.
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
*/
|
2021-02-02 14:49:03 +01:00
|
|
|
$pre = apply_filters( 'pre_clear_scheduled_hook', null, $hook, $args, $wp_error );
|
|
|
|
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
if ( null !== $pre ) {
|
2021-02-02 14:49:03 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
return $pre;
|
|
|
|
}
|
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
/*
|
|
|
|
* This logic duplicates wp_next_scheduled().
|
|
|
|
* It's required due to a scenario where wp_unschedule_event() fails due to update_option() failing,
|
|
|
|
* and, wp_next_scheduled() returns the same schedule in an infinite loop.
|
|
|
|
*/
|
2013-12-07 10:13:11 +01:00
|
|
|
$crons = _get_cron_array();
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( empty( $crons ) ) {
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
return 0;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2013-12-07 10:13:11 +01:00
|
|
|
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
$results = array();
|
|
|
|
$key = md5( serialize( $args ) );
|
2021-02-02 14:49:03 +01:00
|
|
|
|
2013-12-07 10:13:11 +01:00
|
|
|
foreach ( $crons as $timestamp => $cron ) {
|
|
|
|
if ( isset( $cron[ $hook ][ $key ] ) ) {
|
2021-02-02 14:49:03 +01:00
|
|
|
$results[] = wp_unschedule_event( $timestamp, $hook, $args, true );
|
2013-12-07 10:13:11 +01:00
|
|
|
}
|
|
|
|
}
|
2021-02-02 14:49:03 +01:00
|
|
|
|
|
|
|
$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;
|
|
|
|
}
|
|
|
|
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
return false;
|
|
|
|
}
|
2021-02-02 14:49:03 +01:00
|
|
|
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
return count( $results );
|
2006-03-07 22:43:59 +01:00
|
|
|
}
|
|
|
|
|
2017-06-30 02:22:44 +02:00
|
|
|
/**
|
2017-06-30 06:54:41 +02:00
|
|
|
* Unschedules all events attached to the hook.
|
2017-06-30 02:22:44 +02:00
|
|
|
*
|
|
|
|
* Can be useful for plugins when deactivating to clean up the cron queue.
|
|
|
|
*
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
* Warning: This function may return Boolean FALSE, but may also return a non-Boolean
|
|
|
|
* value which evaluates to FALSE. For information about casting to booleans see the
|
2020-01-20 04:14:06 +01:00
|
|
|
* {@link https://www.php.net/manual/en/language.types.boolean.php PHP documentation}. Use
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
* the `===` operator for testing the return value of this function.
|
|
|
|
*
|
2017-06-30 02:22:44 +02:00
|
|
|
* @since 4.9.0
|
2018-12-19 04:41:49 +01:00
|
|
|
* @since 5.1.0 Return value added to indicate success or failure.
|
2021-02-02 14:49:03 +01:00
|
|
|
* @since 5.7.0 The `$wp_error` parameter was added.
|
2017-06-30 02:22:44 +02:00
|
|
|
*
|
2021-02-02 14:49:03 +01:00
|
|
|
* @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.
|
2017-06-30 02:22:44 +02:00
|
|
|
*/
|
2021-02-02 14:49:03 +01:00
|
|
|
function wp_unschedule_hook( $hook, $wp_error = false ) {
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
/**
|
|
|
|
* Filter to preflight or hijack clearing all events attached to the hook.
|
|
|
|
*
|
|
|
|
* Returning a non-null value will short-circuit the normal unscheduling
|
|
|
|
* process, causing the function to return the filtered value instead.
|
|
|
|
*
|
|
|
|
* For plugins replacing wp-cron, return the number of events successfully
|
|
|
|
* unscheduled (zero if no events were registered with the hook) or false
|
|
|
|
* if unscheduling one or more events fails.
|
|
|
|
*
|
2018-12-30 06:06:48 +01:00
|
|
|
* @since 5.1.0
|
2021-02-02 14:49:03 +01:00
|
|
|
* @since 5.7.0 The `$wp_error` parameter was added, and a `WP_Error` object can now be returned.
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
*
|
2021-02-02 14:49:03 +01:00
|
|
|
* @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.
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
*/
|
2021-02-02 14:49:03 +01:00
|
|
|
$pre = apply_filters( 'pre_unschedule_hook', null, $hook, $wp_error );
|
|
|
|
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
if ( null !== $pre ) {
|
2021-02-02 14:49:03 +01:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
return $pre;
|
|
|
|
}
|
|
|
|
|
2017-06-30 02:22:44 +02:00
|
|
|
$crons = _get_cron_array();
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
if ( empty( $crons ) ) {
|
|
|
|
return 0;
|
|
|
|
}
|
2017-06-30 02:22:44 +02:00
|
|
|
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
$results = array();
|
2017-12-01 00:11:00 +01:00
|
|
|
foreach ( $crons as $timestamp => $args ) {
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
if ( ! empty( $crons[ $timestamp ][ $hook ] ) ) {
|
|
|
|
$results[] = count( $crons[ $timestamp ][ $hook ] );
|
|
|
|
}
|
2017-06-30 02:22:44 +02:00
|
|
|
unset( $crons[ $timestamp ][ $hook ] );
|
|
|
|
|
|
|
|
if ( empty( $crons[ $timestamp ] ) ) {
|
|
|
|
unset( $crons[ $timestamp ] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
/*
|
|
|
|
* If the results are empty (zero events to unschedule), no attempt
|
|
|
|
* to update the cron array is required.
|
|
|
|
*/
|
|
|
|
if ( empty( $results ) ) {
|
|
|
|
return 0;
|
|
|
|
}
|
2021-02-02 14:49:03 +01:00
|
|
|
|
|
|
|
$set = _set_cron_array( $crons, $wp_error );
|
|
|
|
|
|
|
|
if ( true === $set ) {
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
return array_sum( $results );
|
|
|
|
}
|
2021-02-02 14:49:03 +01:00
|
|
|
|
|
|
|
return $set;
|
2017-06-30 02:22:44 +02:00
|
|
|
}
|
|
|
|
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
/**
|
|
|
|
* Retrieve a scheduled event.
|
|
|
|
*
|
2019-01-24 00:08:51 +01:00
|
|
|
* Retrieve the full event object for a given event, if no timestamp is specified the next
|
|
|
|
* scheduled event is returned.
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
*
|
2018-12-30 06:06:48 +01:00
|
|
|
* @since 5.1.0
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
*
|
|
|
|
* @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.
|
2021-02-03 12:08:04 +01:00
|
|
|
* Default empty array.
|
|
|
|
* @param int|null $timestamp Optional. Unix timestamp (UTC) of the event. If not specified, the next scheduled event
|
|
|
|
* is returned. Default null.
|
2020-01-11 19:32:05 +01:00
|
|
|
* @return object|false The event object. False if the event does not exist.
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
*/
|
|
|
|
function wp_get_scheduled_event( $hook, $args = array(), $timestamp = null ) {
|
|
|
|
/**
|
|
|
|
* Filter to preflight or hijack retrieving a scheduled event.
|
|
|
|
*
|
|
|
|
* Returning a non-null value will short-circuit the normal process,
|
|
|
|
* returning the filtered value instead.
|
|
|
|
*
|
|
|
|
* Return false if the event does not exist, otherwise an event object
|
|
|
|
* should be returned.
|
|
|
|
*
|
2018-12-30 06:06:48 +01:00
|
|
|
* @since 5.1.0
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
*
|
2019-09-21 19:41:57 +02:00
|
|
|
* @param null|false|object $pre Value to return instead. Default null to continue retrieving the event.
|
|
|
|
* @param string $hook Action hook of the event.
|
|
|
|
* @param array $args 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.
|
2019-01-24 00:08:51 +01:00
|
|
|
* @param int|null $timestamp Unix timestamp (UTC) of the event. Null to retrieve next scheduled event.
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
*/
|
|
|
|
$pre = apply_filters( 'pre_get_scheduled_event', null, $hook, $args, $timestamp );
|
|
|
|
if ( null !== $pre ) {
|
|
|
|
return $pre;
|
|
|
|
}
|
|
|
|
|
2019-01-24 00:08:51 +01:00
|
|
|
if ( null !== $timestamp && ! is_numeric( $timestamp ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
|
2019-01-24 00:08:51 +01:00
|
|
|
$crons = _get_cron_array();
|
|
|
|
if ( empty( $crons ) ) {
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2019-01-24 00:08:51 +01:00
|
|
|
$key = md5( serialize( $args ) );
|
|
|
|
|
|
|
|
if ( ! $timestamp ) {
|
|
|
|
// Get next event.
|
|
|
|
$next = false;
|
|
|
|
foreach ( $crons as $timestamp => $cron ) {
|
|
|
|
if ( isset( $cron[ $hook ][ $key ] ) ) {
|
|
|
|
$next = $timestamp;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if ( ! $next ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$timestamp = $next;
|
|
|
|
} elseif ( ! isset( $crons[ $timestamp ][ $hook ][ $key ] ) ) {
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$event = (object) array(
|
|
|
|
'hook' => $hook,
|
|
|
|
'timestamp' => $timestamp,
|
|
|
|
'schedule' => $crons[ $timestamp ][ $hook ][ $key ]['schedule'],
|
|
|
|
'args' => $args,
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( isset( $crons[ $timestamp ][ $hook ][ $key ]['interval'] ) ) {
|
|
|
|
$event->interval = $crons[ $timestamp ][ $hook ][ $key ]['interval'];
|
|
|
|
}
|
|
|
|
|
|
|
|
return $event;
|
|
|
|
}
|
|
|
|
|
2008-09-04 21:12:40 +02:00
|
|
|
/**
|
2016-08-26 11:22:30 +02:00
|
|
|
* Retrieve the next timestamp for an event.
|
2008-09-04 21:12:40 +02:00
|
|
|
*
|
|
|
|
* @since 2.1.0
|
|
|
|
*
|
2017-11-22 16:16:47 +01:00
|
|
|
* @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.
|
2021-02-03 12:08:04 +01:00
|
|
|
* Default empty array.
|
2020-01-11 19:32:05 +01:00
|
|
|
* @return int|false The Unix timestamp of the next time the event will occur. False if the event doesn't exist.
|
2008-09-04 21:12:40 +02:00
|
|
|
*/
|
2006-09-14 01:54:15 +02:00
|
|
|
function wp_next_scheduled( $hook, $args = array() ) {
|
2019-01-24 00:08:51 +01:00
|
|
|
$next_event = wp_get_scheduled_event( $hook, $args );
|
|
|
|
if ( ! $next_event ) {
|
|
|
|
return false;
|
2006-09-14 01:54:15 +02:00
|
|
|
}
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
|
2019-01-24 00:08:51 +01:00
|
|
|
return $next_event->timestamp;
|
2006-03-07 22:43:59 +01:00
|
|
|
}
|
|
|
|
|
2008-08-01 07:00:07 +02:00
|
|
|
/**
|
2015-12-19 09:20:27 +01:00
|
|
|
* Sends a request to run cron through HTTP request that doesn't halt page loading.
|
2008-08-01 07:00:07 +02:00
|
|
|
*
|
|
|
|
* @since 2.1.0
|
2018-12-19 04:41:49 +01:00
|
|
|
* @since 5.1.0 Return values added.
|
2015-12-19 09:20:27 +01:00
|
|
|
*
|
2016-08-26 11:22:30 +02:00
|
|
|
* @param int $gmt_time Optional. Unix timestamp (UTC). Default 0 (current time is used).
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
* @return bool True if spawned, false if no events spawned.
|
2008-08-01 07:00:07 +02:00
|
|
|
*/
|
2012-09-23 18:57:21 +02:00
|
|
|
function spawn_cron( $gmt_time = 0 ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( ! $gmt_time ) {
|
2012-09-23 18:57:21 +02:00
|
|
|
$gmt_time = microtime( true );
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2009-02-07 14:32:34 +01:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( defined( 'DOING_CRON' ) || isset( $_GET['doing_wp_cron'] ) ) {
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
return false;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2008-09-18 09:09:38 +02:00
|
|
|
|
2009-02-07 14:32:34 +01:00
|
|
|
/*
|
2016-08-26 11:22:30 +02:00
|
|
|
* Get the cron lock, which is a Unix timestamp of when the last cron was spawned
|
2015-05-25 08:25:25 +02:00
|
|
|
* and has not finished running.
|
|
|
|
*
|
|
|
|
* Multiple processes on multiple web servers can run this code concurrently,
|
|
|
|
* this lock attempts to make spawning as atomic as possible.
|
|
|
|
*/
|
2017-12-01 00:11:00 +01:00
|
|
|
$lock = get_transient( 'doing_cron' );
|
2009-02-07 14:32:34 +01:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( $lock > $gmt_time + 10 * MINUTE_IN_SECONDS ) {
|
2011-09-09 21:59:44 +02:00
|
|
|
$lock = 0;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2009-02-07 14:32:34 +01:00
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// Don't run if another process is currently running it or more than once every 60 sec.
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( $lock + WP_CRON_LOCK_TIMEOUT > $gmt_time ) {
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
return false;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2009-02-07 14:32:34 +01:00
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// Sanity check.
|
2019-01-08 22:49:49 +01:00
|
|
|
$crons = wp_get_ready_cron_jobs();
|
|
|
|
if ( empty( $crons ) ) {
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
return false;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2007-02-27 16:24:54 +01:00
|
|
|
|
2006-06-27 08:04:27 +02:00
|
|
|
$keys = array_keys( $crons );
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) {
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
return false;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2006-04-06 07:25:50 +02:00
|
|
|
|
2014-09-11 16:02:15 +02:00
|
|
|
if ( defined( 'ALTERNATE_WP_CRON' ) && ALTERNATE_WP_CRON ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( 'GET' !== $_SERVER['REQUEST_METHOD'] || defined( 'DOING_AJAX' ) || defined( 'XMLRPC_REQUEST' ) ) {
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
return false;
|
2014-09-11 16:02:15 +02:00
|
|
|
}
|
2008-09-18 09:09:38 +02:00
|
|
|
|
2012-09-23 18:57:21 +02:00
|
|
|
$doing_wp_cron = sprintf( '%.22F', $gmt_time );
|
2011-09-09 21:59:44 +02:00
|
|
|
set_transient( 'doing_cron', $doing_wp_cron );
|
2009-02-07 14:32:34 +01:00
|
|
|
|
|
|
|
ob_start();
|
2013-03-03 17:30:38 +01:00
|
|
|
wp_redirect( add_query_arg( 'doing_wp_cron', $doing_wp_cron, wp_unslash( $_SERVER['REQUEST_URI'] ) ) );
|
2009-02-07 14:32:34 +01:00
|
|
|
echo ' ';
|
2008-09-18 09:09:38 +02:00
|
|
|
|
2020-01-29 01:45:18 +01:00
|
|
|
// Flush any buffers and send the headers.
|
2019-07-09 07:45:58 +02:00
|
|
|
wp_ob_end_flush_all();
|
2009-02-07 14:32:34 +01:00
|
|
|
flush();
|
|
|
|
|
2020-02-06 07:33:11 +01:00
|
|
|
include_once ABSPATH . 'wp-cron.php';
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
return true;
|
2009-02-07 14:32:34 +01:00
|
|
|
}
|
2008-09-18 09:09:38 +02:00
|
|
|
|
2015-05-25 08:25:25 +02:00
|
|
|
// Set the cron lock with the current unix timestamp, when the cron is being spawned.
|
2012-09-23 18:57:21 +02:00
|
|
|
$doing_wp_cron = sprintf( '%.22F', $gmt_time );
|
2011-09-09 21:59:44 +02:00
|
|
|
set_transient( 'doing_cron', $doing_wp_cron );
|
2007-06-14 04:25:30 +02:00
|
|
|
|
2013-11-19 06:01:11 +01:00
|
|
|
/**
|
2016-05-22 20:50:28 +02:00
|
|
|
* Filters the cron request arguments.
|
2013-11-19 06:01:11 +01:00
|
|
|
*
|
|
|
|
* @since 3.5.0
|
2016-01-14 18:30:28 +01:00
|
|
|
* @since 4.5.0 The `$doing_wp_cron` parameter was added.
|
2013-11-19 06:01:11 +01:00
|
|
|
*
|
|
|
|
* @param array $cron_request_array {
|
|
|
|
* An array of cron request URL arguments.
|
|
|
|
*
|
|
|
|
* @type string $url The cron request URL.
|
|
|
|
* @type int $key The 22 digit GMT microtime.
|
|
|
|
* @type array $args {
|
|
|
|
* An array of cron request arguments.
|
|
|
|
*
|
|
|
|
* @type int $timeout The request timeout in seconds. Default .01 seconds.
|
|
|
|
* @type bool $blocking Whether to set blocking for the request. Default false.
|
2014-06-20 00:53:19 +02:00
|
|
|
* @type bool $sslverify Whether SSL should be verified for the request. Default false.
|
2013-11-19 06:01:11 +01:00
|
|
|
* }
|
|
|
|
* }
|
2016-01-09 00:54:26 +01:00
|
|
|
* @param string $doing_wp_cron The unix timestamp of the cron lock.
|
2013-11-19 06:01:11 +01:00
|
|
|
*/
|
2017-12-01 00:11:00 +01:00
|
|
|
$cron_request = apply_filters(
|
2018-08-17 03:51:36 +02:00
|
|
|
'cron_request',
|
|
|
|
array(
|
2017-12-01 00:11:00 +01:00
|
|
|
'url' => add_query_arg( 'doing_wp_cron', $doing_wp_cron, site_url( 'wp-cron.php' ) ),
|
|
|
|
'key' => $doing_wp_cron,
|
|
|
|
'args' => array(
|
|
|
|
'timeout' => 0.01,
|
|
|
|
'blocking' => false,
|
|
|
|
/** This filter is documented in wp-includes/class-wp-http-streams.php */
|
|
|
|
'sslverify' => apply_filters( 'https_local_ssl_verify', false ),
|
|
|
|
),
|
2018-08-17 03:51:36 +02:00
|
|
|
),
|
|
|
|
$doing_wp_cron
|
2017-12-01 00:11:00 +01:00
|
|
|
);
|
2012-07-20 17:15:22 +02:00
|
|
|
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
$result = wp_remote_post( $cron_request['url'], $cron_request['args'] );
|
|
|
|
return ! is_wp_error( $result );
|
2006-03-07 22:43:59 +01:00
|
|
|
}
|
|
|
|
|
2008-09-04 21:12:40 +02:00
|
|
|
/**
|
Cron API: Run alternative wp-cron later, do not run on archived blogs.
Runs cron jobs later on sites using alternative cron, ie the `ALTERNATE_WP_CRON` constant is true, to more closely match when standard cron jobs are run. Jobs now run on the `wp_loaded` hook at priority `20`. Prior to this change they would run on the `init` hook. This ensures custom post types and taxonomies are registered prior to the jobs running.
This change also prevents alternative wp-cron from running on archived or suspended multisite blogs as these are shut down prior to the `wp_loaded` hook from running.
Moves the existing functionality of `wp_cron()` in to a new private function `_wp_cron()`.
Props flixos90, jeremyfelt, johnbillion, jrf, kurtpayne, nacin, peterwilsoncc, prettyboymp, r-a-y, ryan, stevenkword, swissspidy.
Fixes #20537, #24160.
Built from https://develop.svn.wordpress.org/trunk@50135
git-svn-id: http://core.svn.wordpress.org/trunk@49814 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-02-02 04:10:08 +01:00
|
|
|
* Register _wp_cron() to run on the {@see 'wp_loaded'} action.
|
|
|
|
*
|
|
|
|
* If the {@see 'wp_loaded'} action has already fired, this function calls
|
|
|
|
* _wp_cron() directly.
|
2008-09-04 21:12:40 +02:00
|
|
|
*
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
* Warning: This function may return Boolean FALSE, but may also return a non-Boolean
|
|
|
|
* value which evaluates to FALSE. For information about casting to booleans see the
|
2020-01-20 04:14:06 +01:00
|
|
|
* {@link https://www.php.net/manual/en/language.types.boolean.php PHP documentation}. Use
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
* the `===` operator for testing the return value of this function.
|
|
|
|
*
|
2008-09-04 21:12:40 +02:00
|
|
|
* @since 2.1.0
|
2018-12-19 04:41:49 +01:00
|
|
|
* @since 5.1.0 Return value added to indicate success or failure.
|
Cron API: Run alternative wp-cron later, do not run on archived blogs.
Runs cron jobs later on sites using alternative cron, ie the `ALTERNATE_WP_CRON` constant is true, to more closely match when standard cron jobs are run. Jobs now run on the `wp_loaded` hook at priority `20`. Prior to this change they would run on the `init` hook. This ensures custom post types and taxonomies are registered prior to the jobs running.
This change also prevents alternative wp-cron from running on archived or suspended multisite blogs as these are shut down prior to the `wp_loaded` hook from running.
Moves the existing functionality of `wp_cron()` in to a new private function `_wp_cron()`.
Props flixos90, jeremyfelt, johnbillion, jrf, kurtpayne, nacin, peterwilsoncc, prettyboymp, r-a-y, ryan, stevenkword, swissspidy.
Fixes #20537, #24160.
Built from https://develop.svn.wordpress.org/trunk@50135
git-svn-id: http://core.svn.wordpress.org/trunk@49814 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-02-02 04:10:08 +01:00
|
|
|
* @since 5.7.0 Functionality moved to _wp_cron() to which this becomes a wrapper.
|
|
|
|
*
|
|
|
|
* @return bool|int|void On success an integer indicating number of events spawned (0 indicates no
|
|
|
|
* events needed to be spawned), false if spawning fails for one or more events or
|
|
|
|
* void if the function registered _wp_cron() to run on the action.
|
|
|
|
*/
|
|
|
|
function wp_cron() {
|
|
|
|
if ( did_action( 'wp_loaded' ) ) {
|
|
|
|
return _wp_cron();
|
|
|
|
}
|
|
|
|
|
|
|
|
add_action( 'wp_loaded', '_wp_cron', 20 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Run scheduled callbacks or spawn cron for all scheduled events.
|
|
|
|
*
|
|
|
|
* Warning: This function may return Boolean FALSE, but may also return a non-Boolean
|
|
|
|
* value which evaluates to FALSE. For information about casting to booleans see the
|
|
|
|
* {@link https://www.php.net/manual/en/language.types.boolean.php PHP documentation}. Use
|
|
|
|
* the `===` operator for testing the return value of this function.
|
|
|
|
*
|
|
|
|
* @since 5.7.0
|
|
|
|
* @access private
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
*
|
2021-01-04 18:18:04 +01:00
|
|
|
* @return int|false On success an integer indicating number of events spawned (0 indicates no
|
2021-01-03 23:04:04 +01:00
|
|
|
* events needed to be spawned), false if spawning fails for one or more events.
|
2008-09-04 21:12:40 +02:00
|
|
|
*/
|
Cron API: Run alternative wp-cron later, do not run on archived blogs.
Runs cron jobs later on sites using alternative cron, ie the `ALTERNATE_WP_CRON` constant is true, to more closely match when standard cron jobs are run. Jobs now run on the `wp_loaded` hook at priority `20`. Prior to this change they would run on the `init` hook. This ensures custom post types and taxonomies are registered prior to the jobs running.
This change also prevents alternative wp-cron from running on archived or suspended multisite blogs as these are shut down prior to the `wp_loaded` hook from running.
Moves the existing functionality of `wp_cron()` in to a new private function `_wp_cron()`.
Props flixos90, jeremyfelt, johnbillion, jrf, kurtpayne, nacin, peterwilsoncc, prettyboymp, r-a-y, ryan, stevenkword, swissspidy.
Fixes #20537, #24160.
Built from https://develop.svn.wordpress.org/trunk@50135
git-svn-id: http://core.svn.wordpress.org/trunk@49814 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2021-02-02 04:10:08 +01:00
|
|
|
function _wp_cron() {
|
2020-01-29 01:45:18 +01:00
|
|
|
// Prevent infinite loops caused by lack of wp-cron.php.
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( strpos( $_SERVER['REQUEST_URI'], '/wp-cron.php' ) !== false || ( defined( 'DISABLE_WP_CRON' ) && DISABLE_WP_CRON ) ) {
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
return 0;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2007-01-30 14:06:56 +01:00
|
|
|
|
2019-01-08 22:49:49 +01:00
|
|
|
$crons = wp_get_ready_cron_jobs();
|
|
|
|
if ( empty( $crons ) ) {
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
return 0;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2006-06-10 22:43:49 +02:00
|
|
|
|
2012-09-23 18:57:21 +02:00
|
|
|
$gmt_time = microtime( true );
|
2017-12-01 00:11:00 +01:00
|
|
|
$keys = array_keys( $crons );
|
|
|
|
if ( isset( $keys[0] ) && $keys[0] > $gmt_time ) {
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
return 0;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2006-03-07 22:43:59 +01:00
|
|
|
|
|
|
|
$schedules = wp_get_schedules();
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
$results = array();
|
2006-04-06 07:25:50 +02:00
|
|
|
foreach ( $crons as $timestamp => $cronhooks ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( $timestamp > $gmt_time ) {
|
|
|
|
break;
|
|
|
|
}
|
2008-08-06 22:31:54 +02:00
|
|
|
foreach ( (array) $cronhooks as $hook => $args ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( isset( $schedules[ $hook ]['callback'] ) && ! call_user_func( $schedules[ $hook ]['callback'] ) ) {
|
2006-03-07 22:43:59 +01:00
|
|
|
continue;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
$results[] = spawn_cron( $gmt_time );
|
2006-03-07 22:43:59 +01:00
|
|
|
break 2;
|
|
|
|
}
|
|
|
|
}
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
|
|
|
|
if ( in_array( false, $results, true ) ) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
return count( $results );
|
2006-03-07 22:43:59 +01:00
|
|
|
}
|
|
|
|
|
2008-09-04 21:12:40 +02:00
|
|
|
/**
|
2016-08-26 11:22:30 +02:00
|
|
|
* Retrieve supported event recurrence schedules.
|
2008-09-04 21:12:40 +02:00
|
|
|
*
|
2020-01-12 12:20:04 +01:00
|
|
|
* The default supported recurrences are 'hourly', 'twicedaily', 'daily', and 'weekly'.
|
|
|
|
* A plugin may add more by hooking into the {@see 'cron_schedules'} filter.
|
|
|
|
* The filter accepts an array of arrays. The outer array has a key that is the name
|
|
|
|
* of the schedule, for example 'monthly'. The value is an array with two keys,
|
|
|
|
* one is 'interval' and the other is 'display'.
|
2008-09-04 21:12:40 +02:00
|
|
|
*
|
2020-01-12 12:20:04 +01:00
|
|
|
* The 'interval' is a number in seconds of when the cron job should run.
|
|
|
|
* So for 'hourly' the time is `HOUR_IN_SECONDS` (60 * 60 or 3600). For 'monthly',
|
|
|
|
* the value would be `MONTH_IN_SECONDS` (30 * 24 * 60 * 60 or 2592000).
|
2008-09-04 21:12:40 +02:00
|
|
|
*
|
2020-01-12 12:20:04 +01:00
|
|
|
* The 'display' is the description. For the 'monthly' key, the 'display'
|
|
|
|
* would be `__( 'Once Monthly' )`.
|
2008-09-04 21:12:40 +02:00
|
|
|
*
|
2020-01-12 12:20:04 +01:00
|
|
|
* For your plugin, you will be passed an array. You can easily add your
|
2008-09-04 21:12:40 +02:00
|
|
|
* schedule by doing the following.
|
2014-11-24 05:58:22 +01:00
|
|
|
*
|
|
|
|
* // Filter parameter variable name is 'array'.
|
2020-01-12 12:20:04 +01:00
|
|
|
* $array['monthly'] = array(
|
|
|
|
* 'interval' => MONTH_IN_SECONDS,
|
|
|
|
* 'display' => __( 'Once Monthly' )
|
2014-11-24 05:58:22 +01:00
|
|
|
* );
|
|
|
|
*
|
2008-09-04 21:12:40 +02:00
|
|
|
* @since 2.1.0
|
2020-01-12 12:20:04 +01:00
|
|
|
* @since 5.4.0 The 'weekly' schedule was added.
|
2008-09-04 21:12:40 +02:00
|
|
|
*
|
2021-10-30 01:13:05 +02:00
|
|
|
* @return array[]
|
2008-09-04 21:12:40 +02:00
|
|
|
*/
|
2006-03-07 22:43:59 +01:00
|
|
|
function wp_get_schedules() {
|
|
|
|
$schedules = array(
|
2017-12-01 00:11:00 +01:00
|
|
|
'hourly' => array(
|
|
|
|
'interval' => HOUR_IN_SECONDS,
|
|
|
|
'display' => __( 'Once Hourly' ),
|
|
|
|
),
|
|
|
|
'twicedaily' => array(
|
|
|
|
'interval' => 12 * HOUR_IN_SECONDS,
|
|
|
|
'display' => __( 'Twice Daily' ),
|
|
|
|
),
|
|
|
|
'daily' => array(
|
|
|
|
'interval' => DAY_IN_SECONDS,
|
|
|
|
'display' => __( 'Once Daily' ),
|
|
|
|
),
|
2020-01-12 12:20:04 +01:00
|
|
|
'weekly' => array(
|
2020-01-13 18:05:07 +01:00
|
|
|
'interval' => WEEK_IN_SECONDS,
|
2020-01-12 12:20:04 +01:00
|
|
|
'display' => __( 'Once Weekly' ),
|
|
|
|
),
|
2006-03-07 22:43:59 +01:00
|
|
|
);
|
2020-01-12 12:20:04 +01:00
|
|
|
|
2013-11-19 06:01:11 +01:00
|
|
|
/**
|
2016-05-22 20:50:28 +02:00
|
|
|
* Filters the non-default cron schedules.
|
2013-11-19 06:01:11 +01:00
|
|
|
*
|
|
|
|
* @since 2.1.0
|
|
|
|
*
|
2021-10-30 01:13:05 +02:00
|
|
|
* @param array[] $new_schedules An array of non-default cron schedule arrays. Default empty.
|
2013-11-19 06:01:11 +01:00
|
|
|
*/
|
2006-04-06 07:25:50 +02:00
|
|
|
return array_merge( apply_filters( 'cron_schedules', array() ), $schedules );
|
2006-03-07 22:43:59 +01:00
|
|
|
}
|
2006-04-06 07:25:50 +02:00
|
|
|
|
2008-09-04 21:12:40 +02:00
|
|
|
/**
|
2016-08-26 11:22:30 +02:00
|
|
|
* Retrieve the recurrence schedule for an event.
|
|
|
|
*
|
|
|
|
* @see wp_get_schedules() for available schedules.
|
2008-09-04 21:12:40 +02:00
|
|
|
*
|
|
|
|
* @since 2.1.0
|
2018-12-30 06:06:48 +01:00
|
|
|
* @since 5.1.0 {@see 'get_schedule'} filter added.
|
2008-09-04 21:12:40 +02:00
|
|
|
*
|
2016-08-26 11:22:30 +02:00
|
|
|
* @param string $hook Action hook to identify the event.
|
2020-07-23 23:11:05 +02:00
|
|
|
* @param array $args Optional. Arguments passed to the event's callback function.
|
2021-02-03 12:08:04 +01:00
|
|
|
* Default empty array.
|
2021-01-15 21:08:07 +01:00
|
|
|
* @return string|false Schedule name on success, false if no schedule.
|
2008-09-04 21:12:40 +02:00
|
|
|
*/
|
2017-12-01 00:11:00 +01:00
|
|
|
function wp_get_schedule( $hook, $args = array() ) {
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
$schedule = false;
|
|
|
|
$event = wp_get_scheduled_event( $hook, $args );
|
|
|
|
|
|
|
|
if ( $event ) {
|
|
|
|
$schedule = $event->schedule;
|
2006-09-26 03:14:49 +02:00
|
|
|
}
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
|
|
|
|
/**
|
2020-08-11 02:34:08 +02:00
|
|
|
* Filters the schedule for a hook.
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
*
|
2018-12-30 06:06:48 +01:00
|
|
|
* @since 5.1.0
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
*
|
2021-01-03 23:04:04 +01:00
|
|
|
* @param string|false $schedule Schedule for the hook. False if not found.
|
|
|
|
* @param string $hook Action hook to execute when cron is run.
|
2021-02-03 12:08:04 +01:00
|
|
|
* @param array $args Arguments to pass to the hook's callback function.
|
Cron: Add hooks and a function to allow hijacking cron implementation.
This allows sites with a large cron option or a custom cron implementation to hijack the cron option to store cron data using custom functionality.
`wp_get_scheduled_event()` is new function to retrieve the event object for a given event based on the hook name, arguments and timestamp. If no timestamp is specified the next occurence is returned.
Preflight filters are added to all functions that read from or modify the cron option: `pre_schedule_event`, `pre_reschedule_event`, `pre_unschedule_event`, `pre_clear_scheduled_hook`, `pre_unschedule_hook`, `pre_get_scheduled_event` and `pre_next_scheduled`.
Additionally, the post scheduling hooks `next_scheduled` and `get_schedule` to allow plugins to modify an event after retrieving it from WordPress.
Props rmccue, DavidAnderson, ethitter, peterwilsoncc.
Fixes #32656.
Built from https://develop.svn.wordpress.org/trunk@43540
git-svn-id: http://core.svn.wordpress.org/trunk@43369 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-07-27 04:23:25 +02:00
|
|
|
*/
|
|
|
|
return apply_filters( 'get_schedule', $schedule, $hook, $args );
|
2006-09-26 03:14:49 +02:00
|
|
|
}
|
|
|
|
|
2019-01-08 22:49:49 +01:00
|
|
|
/**
|
|
|
|
* Retrieve cron jobs ready to be run.
|
|
|
|
*
|
|
|
|
* Returns the results of _get_cron_array() limited to events ready to be run,
|
|
|
|
* ie, with a timestamp in the past.
|
|
|
|
*
|
|
|
|
* @since 5.1.0
|
|
|
|
*
|
2021-11-23 22:37:01 +01:00
|
|
|
* @return array[] Array of cron job arrays ready to be run.
|
2019-01-08 22:49:49 +01:00
|
|
|
*/
|
|
|
|
function wp_get_ready_cron_jobs() {
|
|
|
|
/**
|
|
|
|
* Filter to preflight or hijack retrieving ready cron jobs.
|
|
|
|
*
|
|
|
|
* Returning an array will short-circuit the normal retrieval of ready
|
|
|
|
* cron jobs, causing the function to return the filtered value instead.
|
|
|
|
*
|
|
|
|
* @since 5.1.0
|
|
|
|
*
|
2021-11-23 22:37:01 +01:00
|
|
|
* @param null|array[] $pre Array of ready cron tasks to return instead. Default null
|
|
|
|
* to continue using results from _get_cron_array().
|
2019-01-08 22:49:49 +01:00
|
|
|
*/
|
|
|
|
$pre = apply_filters( 'pre_get_ready_cron_jobs', null );
|
2022-09-09 03:53:11 +02:00
|
|
|
|
2019-01-08 22:49:49 +01:00
|
|
|
if ( null !== $pre ) {
|
|
|
|
return $pre;
|
|
|
|
}
|
|
|
|
|
Coding Standards: Various alignment fixes from `composer format`.
Follow up to [53874], [54097], [54110], [54155], [54162], [54184].
See #39210, #55443, #56288, #56092, #56408, #56467, #55881.
Built from https://develop.svn.wordpress.org/trunk@54210
git-svn-id: http://core.svn.wordpress.org/trunk@53769 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-19 21:51:09 +02:00
|
|
|
$crons = _get_cron_array();
|
2019-01-08 22:49:49 +01:00
|
|
|
$gmt_time = microtime( true );
|
Coding Standards: Various alignment fixes from `composer format`.
Follow up to [53874], [54097], [54110], [54155], [54162], [54184].
See #39210, #55443, #56288, #56092, #56408, #56467, #55881.
Built from https://develop.svn.wordpress.org/trunk@54210
git-svn-id: http://core.svn.wordpress.org/trunk@53769 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2022-09-19 21:51:09 +02:00
|
|
|
$results = array();
|
2022-09-09 03:53:11 +02:00
|
|
|
|
2019-01-08 22:49:49 +01:00
|
|
|
foreach ( $crons as $timestamp => $cronhooks ) {
|
|
|
|
if ( $timestamp > $gmt_time ) {
|
|
|
|
break;
|
|
|
|
}
|
2022-09-09 03:53:11 +02:00
|
|
|
|
2019-01-08 22:49:49 +01:00
|
|
|
$results[ $timestamp ] = $cronhooks;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $results;
|
|
|
|
}
|
|
|
|
|
2006-09-14 01:54:15 +02:00
|
|
|
//
|
2020-01-29 01:45:18 +01:00
|
|
|
// Private functions.
|
2006-09-14 01:54:15 +02:00
|
|
|
//
|
|
|
|
|
2008-09-04 21:12:40 +02:00
|
|
|
/**
|
|
|
|
* Retrieve cron info array option.
|
|
|
|
*
|
|
|
|
* @since 2.1.0
|
2022-10-12 13:53:15 +02:00
|
|
|
* @since 6.1.0 Return type modified to consistently return an array.
|
2008-09-04 21:12:40 +02:00
|
|
|
* @access private
|
|
|
|
*
|
2022-07-29 05:34:13 +02:00
|
|
|
* @return array[] Array of cron events.
|
2008-09-04 21:12:40 +02:00
|
|
|
*/
|
2017-12-01 00:11:00 +01:00
|
|
|
function _get_cron_array() {
|
|
|
|
$cron = get_option( 'cron' );
|
|
|
|
if ( ! is_array( $cron ) ) {
|
2022-07-29 05:34:13 +02:00
|
|
|
return array();
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2006-09-14 01:54:15 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
if ( ! isset( $cron['version'] ) ) {
|
|
|
|
$cron = _upgrade_cron_array( $cron );
|
|
|
|
}
|
2006-09-14 01:54:15 +02:00
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
unset( $cron['version'] );
|
2006-09-14 01:54:15 +02:00
|
|
|
|
|
|
|
return $cron;
|
|
|
|
}
|
|
|
|
|
2008-09-04 21:12:40 +02:00
|
|
|
/**
|
2021-02-02 14:49:03 +01:00
|
|
|
* Updates the cron option with the new cron array.
|
2008-09-04 21:12:40 +02:00
|
|
|
*
|
|
|
|
* @since 2.1.0
|
2018-12-30 05:51:51 +01:00
|
|
|
* @since 5.1.0 Return value modified to outcome of update_option().
|
2021-02-02 14:49:03 +01:00
|
|
|
* @since 5.7.0 The `$wp_error` parameter was added.
|
Cron API: Return meaningful values from cron functions.
Return values added to Cron API functions to indicate outcome:
* `wp_schedule_single_event()`, `wp_schedule_event()`, `wp_reschedule_event()` and `wp_unschedule_event()`: boolean indicating success or failure,
* `wp_clear_scheduled_hook()`: integer indicating number of jobs cleared (zero or more), `false` if one or more jobs fail to clear,
* `wp_unschedule_hook()`: integer indicating number of jobs cleared (zero or more), `false` if the jobs fail to clear,
* `spawn_cron()`: boolean indicating whether job spawned,
* `wp_cron()`: integer indicating number of jobs spawned (zero or more), `false` if one or more jobs fail to spawned,
* `_set_cron_array()`: boolean outcome of `update_option()`.
Props evansolomon, jrf, peterwilsoncc, pento for code review.
Fixes #21072.
Built from https://develop.svn.wordpress.org/trunk@43050
git-svn-id: http://core.svn.wordpress.org/trunk@42879 1a063a9b-81f0-0310-95a4-ce76da25c4cd
2018-05-01 04:05:23 +02:00
|
|
|
*
|
2008-09-04 21:12:40 +02:00
|
|
|
* @access private
|
|
|
|
*
|
2021-11-23 22:37:01 +01:00
|
|
|
* @param array[] $cron Array of cron info arrays from _get_cron_array().
|
|
|
|
* @param bool $wp_error Optional. Whether to return a WP_Error on failure. Default false.
|
2021-02-02 14:49:03 +01:00
|
|
|
* @return bool|WP_Error True if cron array updated. False or WP_Error on failure.
|
2008-09-04 21:12:40 +02:00
|
|
|
*/
|
2021-02-02 14:49:03 +01:00
|
|
|
function _set_cron_array( $cron, $wp_error = false ) {
|
2021-08-30 17:20:56 +02:00
|
|
|
if ( ! is_array( $cron ) ) {
|
|
|
|
$cron = array();
|
|
|
|
}
|
|
|
|
|
2006-09-14 01:54:15 +02:00
|
|
|
$cron['version'] = 2;
|
2021-02-02 19:04:03 +01:00
|
|
|
$result = update_option( 'cron', $cron );
|
2021-02-02 14:49:03 +01:00
|
|
|
|
|
|
|
if ( $wp_error && ! $result ) {
|
|
|
|
return new WP_Error(
|
|
|
|
'could_not_set',
|
|
|
|
__( 'The cron event list could not be saved.' )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
return $result;
|
2006-09-14 01:54:15 +02:00
|
|
|
}
|
|
|
|
|
2008-09-04 21:12:40 +02:00
|
|
|
/**
|
2022-08-11 16:11:08 +02:00
|
|
|
* Upgrade a cron info array.
|
2008-09-04 21:12:40 +02:00
|
|
|
*
|
2022-08-11 16:11:08 +02:00
|
|
|
* This function upgrades the cron info array to version 2.
|
2008-09-04 21:12:40 +02:00
|
|
|
*
|
|
|
|
* @since 2.1.0
|
|
|
|
* @access private
|
|
|
|
*
|
2016-05-22 19:39:28 +02:00
|
|
|
* @param array $cron Cron info array from _get_cron_array().
|
2022-08-11 16:11:08 +02:00
|
|
|
* @return array An upgraded cron info array.
|
2008-09-04 21:12:40 +02:00
|
|
|
*/
|
2017-12-01 00:11:00 +01:00
|
|
|
function _upgrade_cron_array( $cron ) {
|
|
|
|
if ( isset( $cron['version'] ) && 2 == $cron['version'] ) {
|
2006-09-14 01:54:15 +02:00
|
|
|
return $cron;
|
2017-12-01 00:11:00 +01:00
|
|
|
}
|
2006-09-14 01:54:15 +02:00
|
|
|
|
|
|
|
$new_cron = array();
|
|
|
|
|
2017-12-01 00:11:00 +01:00
|
|
|
foreach ( (array) $cron as $timestamp => $hooks ) {
|
2008-08-06 22:31:54 +02:00
|
|
|
foreach ( (array) $hooks as $hook => $args ) {
|
2017-12-01 00:11:00 +01:00
|
|
|
$key = md5( serialize( $args['args'] ) );
|
|
|
|
$new_cron[ $timestamp ][ $hook ][ $key ] = $args;
|
2006-09-14 01:54:15 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$new_cron['version'] = 2;
|
|
|
|
update_option( 'cron', $new_cron );
|
|
|
|
return $new_cron;
|
2010-12-02 17:27:16 +01:00
|
|
|
}
|