PluginProbe
WANotifier for Forms and Actions / 3.1.1
WANotifier for Forms and Actions v3.1.1
3.1.1 3.1.0 3.0.4 2.7.10 2.7.11 2.7.12 2.7.13 2.7.2 2.7.3 2.7.4 2.7.5 2.7.6 2.7.7 2.7.8 2.7.9 3.0.0 3.0.1 3.0.2 3.0.3 trunk 0.1.0 0.1.1 1.0.0 1.0.1 1.0.2 All 67 releases
← All changes | libraries/action-scheduler/functions.php +239 -59 1.0.23.1.1 View file →
@@ -1,8 +1,9 @@
1 1 <?php
2 -
3 2 /**
4 3 * General API functions for scheduling actions
4 + *
5 + * @package ActionScheduler.
5 6 */
6 7
7 8 /**
8 9 * Enqueue an action to run one time, as soon as possible
@@ -9,59 +10,185 @@
9 10 *
10 11 * @param string $hook The hook to trigger.
11 12 * @param array $args Arguments to pass when the hook triggers.
12 13 * @param string $group The group to assign this job to.
13 - * @return int The action ID.
14 + * @param bool $unique Whether the action should be unique. It will not be scheduled if another pending or running action has the same hook and group parameters.
15 + * @param int $priority Lower values take precedence over higher values. Defaults to 10, with acceptable values falling in the range 0-255.
16 + *
17 + * @return int The action ID. Zero if there was an error scheduling the action.
14 18 */
15 -function as_enqueue_async_action( $hook, $args = array(), $group = '' ) {
19 +function as_enqueue_async_action( $hook, $args = array(), $group = '', $unique = false, $priority = 10 ) {
16 20 if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) {
17 21 return 0;
18 22 }
19 - return ActionScheduler::factory()->async( $hook, $args, $group );
23 +
24 + /**
25 + * Provides an opportunity to short-circuit the default process for enqueuing async
26 + * actions.
27 + *
28 + * Returning a value other than null from the filter will short-circuit the normal
29 + * process. The expectation in such a scenario is that callbacks will return an integer
30 + * representing the enqueued action ID (enqueued using some alternative process) or else
31 + * zero.
32 + *
33 + * @param int|null $pre_option The value to return instead of the option value.
34 + * @param string $hook Action hook.
35 + * @param array $args Action arguments.
36 + * @param string $group Action group.
37 + * @param int $priority Action priority.
38 + * @param bool $unique Unique action.
39 + */
40 + $pre = apply_filters( 'pre_as_enqueue_async_action', null, $hook, $args, $group, $priority, $unique );
41 + if ( null !== $pre ) {
42 + return is_int( $pre ) ? $pre : 0;
43 + }
44 +
45 + return ActionScheduler::factory()->create(
46 + array(
47 + 'type' => 'async',
48 + 'hook' => $hook,
49 + 'arguments' => $args,
50 + 'group' => $group,
51 + 'unique' => $unique,
52 + 'priority' => $priority,
53 + )
54 + );
20 55 }
21 56
22 57 /**
23 58 * Schedule an action to run one time
24 59 *
25 - * @param int $timestamp When the job will run.
60 + * @param int $timestamp When the job will run.
26 61 * @param string $hook The hook to trigger.
27 - * @param array $args Arguments to pass when the hook triggers.
62 + * @param array $args Arguments to pass when the hook triggers.
28 63 * @param string $group The group to assign this job to.
64 + * @param bool $unique Whether the action should be unique. It will not be scheduled if another pending or running action has the same hook and group parameters.
65 + * @param int $priority Lower values take precedence over higher values. Defaults to 10, with acceptable values falling in the range 0-255.
29 66 *
30 - * @return int The action ID.
67 + * @return int The action ID. Zero if there was an error scheduling the action.
31 68 */
32 -function as_schedule_single_action( $timestamp, $hook, $args = array(), $group = '' ) {
69 +function as_schedule_single_action( $timestamp, $hook, $args = array(), $group = '', $unique = false, $priority = 10 ) {
33 70 if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) {
34 71 return 0;
35 72 }
36 - return ActionScheduler::factory()->single( $hook, $args, $timestamp, $group );
73 +
74 + /**
75 + * Provides an opportunity to short-circuit the default process for enqueuing single
76 + * actions.
77 + *
78 + * Returning a value other than null from the filter will short-circuit the normal
79 + * process. The expectation in such a scenario is that callbacks will return an integer
80 + * representing the scheduled action ID (scheduled using some alternative process) or else
81 + * zero.
82 + *
83 + * @param int|null $pre_option The value to return instead of the option value.
84 + * @param int $timestamp When the action will run.
85 + * @param string $hook Action hook.
86 + * @param array $args Action arguments.
87 + * @param string $group Action group.
88 + * @param int $priorities Action priority.
89 + * @param bool $unique Unique action.
90 + */
91 + $pre = apply_filters( 'pre_as_schedule_single_action', null, $timestamp, $hook, $args, $group, $priority, $unique );
92 + if ( null !== $pre ) {
93 + return is_int( $pre ) ? $pre : 0;
94 + }
95 +
96 + return ActionScheduler::factory()->create(
97 + array(
98 + 'type' => 'single',
99 + 'hook' => $hook,
100 + 'arguments' => $args,
101 + 'when' => $timestamp,
102 + 'group' => $group,
103 + 'unique' => $unique,
104 + 'priority' => $priority,
105 + )
106 + );
37 107 }
38 108
39 109 /**
40 110 * Schedule a recurring action
41 111 *
42 - * @param int $timestamp When the first instance of the job will run.
43 - * @param int $interval_in_seconds How long to wait between runs.
112 + * @param int $timestamp When the first instance of the job will run.
113 + * @param int $interval_in_seconds How long to wait between runs.
44 114 * @param string $hook The hook to trigger.
45 - * @param array $args Arguments to pass when the hook triggers.
115 + * @param array $args Arguments to pass when the hook triggers.
46 116 * @param string $group The group to assign this job to.
117 + * @param bool $unique Whether the action should be unique. It will not be scheduled if another pending or running action has the same hook and group parameters.
118 + * @param int $priority Lower values take precedence over higher values. Defaults to 10, with acceptable values falling in the range 0-255.
47 119 *
48 - * @return int The action ID.
120 + * @return int The action ID. Zero if there was an error scheduling the action.
49 121 */
50 -function as_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args = array(), $group = '' ) {
122 +function as_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args = array(), $group = '', $unique = false, $priority = 10 ) {
51 123 if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) {
52 124 return 0;
53 125 }
54 - return ActionScheduler::factory()->recurring( $hook, $args, $timestamp, $interval_in_seconds, $group );
126 +
127 + $interval = (int) $interval_in_seconds;
128 +
129 + // We expect an integer and allow it to be passed using float and string types, but otherwise
130 + // should reject unexpected values.
131 + // phpcs:ignore WordPress.PHP.StrictComparisons.LooseComparison
132 + if ( ! is_numeric( $interval_in_seconds ) || $interval_in_seconds != $interval ) {
133 + _doing_it_wrong(
134 + __METHOD__,
135 + sprintf(
136 + /* translators: 1: provided value 2: provided type. */
137 + esc_html__( 'An integer was expected but "%1$s" (%2$s) was received.', 'action-scheduler' ),
138 + esc_html( $interval_in_seconds ),
139 + esc_html( gettype( $interval_in_seconds ) )
140 + ),
141 + '3.6.0'
142 + );
143 +
144 + return 0;
145 + }
146 +
147 + /**
148 + * Provides an opportunity to short-circuit the default process for enqueuing recurring
149 + * actions.
150 + *
151 + * Returning a value other than null from the filter will short-circuit the normal
152 + * process. The expectation in such a scenario is that callbacks will return an integer
153 + * representing the scheduled action ID (scheduled using some alternative process) or else
154 + * zero.
155 + *
156 + * @param int|null $pre_option The value to return instead of the option value.
157 + * @param int $timestamp When the action will run.
158 + * @param int $interval_in_seconds How long to wait between runs.
159 + * @param string $hook Action hook.
160 + * @param array $args Action arguments.
161 + * @param string $group Action group.
162 + * @param int $priority Action priority.
163 + * @param bool $unique Unique action.
164 + */
165 + $pre = apply_filters( 'pre_as_schedule_recurring_action', null, $timestamp, $interval_in_seconds, $hook, $args, $group, $priority, $unique );
166 + if ( null !== $pre ) {
167 + return is_int( $pre ) ? $pre : 0;
168 + }
169 +
170 + return ActionScheduler::factory()->create(
171 + array(
172 + 'type' => 'recurring',
173 + 'hook' => $hook,
174 + 'arguments' => $args,
175 + 'when' => $timestamp,
176 + 'pattern' => $interval_in_seconds,
177 + 'group' => $group,
178 + 'unique' => $unique,
179 + 'priority' => $priority,
180 + )
181 + );
55 182 }
56 183
57 184 /**
58 185 * Schedule an action that recurs on a cron-like schedule.
59 186 *
60 - * @param int $base_timestamp The first instance of the action will be scheduled
61 - * to run at a time calculated after this timestamp matching the cron
62 - * expression. This can be used to delay the first instance of the action.
63 - * @param string $schedule A cron-link schedule string
187 + * @param int $timestamp The first instance of the action will be scheduled
188 + * to run at a time calculated after this timestamp matching the cron
189 + * expression. This can be used to delay the first instance of the action.
190 + * @param string $schedule A cron-link schedule string.
64 191 * @see http://en.wikipedia.org/wiki/Cron
65 192 * * * * * * *
66 193 * ┬ ┬ ┬ ┬ ┬ ┬
67 194 * | | | | | |
@@ -71,18 +198,55 @@
71 198 * | | +--------------- day of month (1 - 31)
72 199 * | +-------------------- hour (0 - 23)
73 200 * +------------------------- min (0 - 59)
74 201 * @param string $hook The hook to trigger.
75 - * @param array $args Arguments to pass when the hook triggers.
202 + * @param array $args Arguments to pass when the hook triggers.
76 203 * @param string $group The group to assign this job to.
204 + * @param bool $unique Whether the action should be unique. It will not be scheduled if another pending or running action has the same hook and group parameters.
205 + * @param int $priority Lower values take precedence over higher values. Defaults to 10, with acceptable values falling in the range 0-255.
77 206 *
78 - * @return int The action ID.
207 + * @return int The action ID. Zero if there was an error scheduling the action.
79 208 */
80 -function as_schedule_cron_action( $timestamp, $schedule, $hook, $args = array(), $group = '' ) {
209 +function as_schedule_cron_action( $timestamp, $schedule, $hook, $args = array(), $group = '', $unique = false, $priority = 10 ) {
81 210 if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) {
82 211 return 0;
83 212 }
84 - return ActionScheduler::factory()->cron( $hook, $args, $timestamp, $schedule, $group );
213 +
214 + /**
215 + * Provides an opportunity to short-circuit the default process for enqueuing cron
216 + * actions.
217 + *
218 + * Returning a value other than null from the filter will short-circuit the normal
219 + * process. The expectation in such a scenario is that callbacks will return an integer
220 + * representing the scheduled action ID (scheduled using some alternative process) or else
221 + * zero.
222 + *
223 + * @param int|null $pre_option The value to return instead of the option value.
224 + * @param int $timestamp When the action will run.
225 + * @param string $schedule Cron-like schedule string.
226 + * @param string $hook Action hook.
227 + * @param array $args Action arguments.
228 + * @param string $group Action group.
229 + * @param int $priority Action priority.
230 + * @param bool $unique Unique action.
231 + */
232 + $pre = apply_filters( 'pre_as_schedule_cron_action', null, $timestamp, $schedule, $hook, $args, $group, $priority, $unique );
233 + if ( null !== $pre ) {
234 + return is_int( $pre ) ? $pre : 0;
235 + }
236 +
237 + return ActionScheduler::factory()->create(
238 + array(
239 + 'type' => 'cron',
240 + 'hook' => $hook,
241 + 'arguments' => $args,
242 + 'when' => $timestamp,
243 + 'pattern' => $schedule,
244 + 'group' => $group,
245 + 'unique' => $unique,
246 + 'priority' => $priority,
247 + )
248 + );
85 249 }
86 250
87 251 /**
88 252 * Cancel the next occurrence of a scheduled action.
@@ -94,9 +258,9 @@
94 258 * then the following instance will never be scheduled (or exist), which is effectively the same as being unscheduled
95 259 * by this method also.
96 260 *
97 261 * @param string $hook The hook that the job will trigger.
98 - * @param array $args Args that would have been passed to the job.
262 + * @param array $args Args that would have been passed to the job.
99 263 * @param string $group The group the job is assigned to.
100 264 *
101 265 * @return int|null The scheduled action ID if a scheduled action was found, or null if no matching action found.
102 266 */
@@ -123,11 +287,12 @@
123 287 } catch ( Exception $exception ) {
124 288 ActionScheduler::logger()->log(
125 289 $action_id,
126 290 sprintf(
127 - /* translators: %s is the name of the hook to be cancelled. */
128 - __( 'Caught exception while cancelling action: %s', 'action-scheduler' ),
129 - esc_attr( $hook )
291 + /* translators: %1$s is the name of the hook to be cancelled, %2$s is the exception message. */
292 + __( 'Caught exception while cancelling action "%1$s": %2$s', 'action-scheduler' ),
293 + $hook,
294 + $exception->getMessage()
130 295 )
131 296 );
132 297
133 298 $action_id = null;
@@ -140,9 +305,9 @@
140 305 /**
141 306 * Cancel all occurrences of a scheduled action.
142 307 *
143 308 * @param string $hook The hook that the job will trigger.
144 - * @param array $args Args that would have been passed to the job.
309 + * @param array $args Args that would have been passed to the job.
145 310 * @param string $group The group the job is assigned to.
146 311 */
147 312 function as_unschedule_all_actions( $hook, $args = array(), $group = '' ) {
148 313 if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) {
@@ -171,11 +336,11 @@
171 336 * async action sitting in the queue waiting to be processed, in which case boolean true will be
172 337 * returned. Or there may be no async, in-progress or pending action for this hook, in which case,
173 338 * boolean false will be the return value.
174 339 *
175 - * @param string $hook
176 - * @param array $args
177 - * @param string $group
340 + * @param string $hook Name of the hook to search for.
341 + * @param array $args Arguments of the action to be searched.
342 + * @param string $group Group of the action to be searched.
178 343 *
179 344 * @return int|bool The timestamp for the next occurrence of a pending scheduled action, true for an async or in-progress action or false if there is no matching action.
180 345 */
181 346 function as_next_scheduled_action( $hook, $args = null, $group = '' ) {
@@ -209,9 +374,9 @@
209 374 $action = ActionScheduler::store()->fetch_action( $action_id );
210 375 $scheduled_date = $action->get_schedule()->get_date();
211 376 if ( $scheduled_date ) {
212 377 return (int) $scheduled_date->format( 'U' );
213 - } elseif ( null === $scheduled_date ) { // pending async action with NullSchedule
378 + } elseif ( null === $scheduled_date ) { // pending async action with NullSchedule.
214 379 return true;
215 380 }
216 381
217 382 return false;
@@ -236,12 +401,12 @@
236 401 return false;
237 402 }
238 403
239 404 $query_args = array(
240 - 'hook' => $hook,
241 - 'status' => array( ActionScheduler_Store::STATUS_RUNNING, ActionScheduler_Store::STATUS_PENDING ),
242 - 'group' => $group,
243 - 'orderby' => 'none',
405 + 'hook' => $hook,
406 + 'status' => array( ActionScheduler_Store::STATUS_RUNNING, ActionScheduler_Store::STATUS_PENDING ),
407 + 'group' => $group,
408 + 'orderby' => 'none',
244 409 );
245 410
246 411 if ( null !== $args ) {
247 412 $query_args['args'] = $args;
@@ -248,28 +413,28 @@
248 413 }
249 414
250 415 $action_id = ActionScheduler::store()->query_action( $query_args );
251 416
252 - return $action_id !== null;
417 + return null !== $action_id;
253 418 }
254 419
255 420 /**
256 421 * Find scheduled actions
257 422 *
258 - * @param array $args Possible arguments, with their default values:
259 - * 'hook' => '' - the name of the action that will be triggered
260 - * 'args' => NULL - the args array that will be passed with the action
261 - * 'date' => NULL - the scheduled date of the action. Expects a DateTime object, a unix timestamp, or a string that can parsed with strtotime(). Used in UTC timezone.
262 - * 'date_compare' => '<=' - operator for testing "date". accepted values are '!=', '>', '>=', '<', '<=', '='
263 - * 'modified' => NULL - the date the action was last updated. Expects a DateTime object, a unix timestamp, or a string that can parsed with strtotime(). Used in UTC timezone.
264 - * 'modified_compare' => '<=' - operator for testing "modified". accepted values are '!=', '>', '>=', '<', '<=', '='
265 - * 'group' => '' - the group the action belongs to
266 - * 'status' => '' - ActionScheduler_Store::STATUS_COMPLETE or ActionScheduler_Store::STATUS_PENDING
267 - * 'claimed' => NULL - TRUE to find claimed actions, FALSE to find unclaimed actions, a string to find a specific claim ID
268 - * 'per_page' => 5 - Number of results to return
269 - * 'offset' => 0
270 - * 'orderby' => 'date' - accepted values are 'hook', 'group', 'modified', 'date' or 'none'
271 - * 'order' => 'ASC'
423 + * @param array $args Possible arguments, with their default values.
424 + * 'hook' => '' - the name of the action that will be triggered.
425 + * 'args' => NULL - the args array that will be passed with the action.
426 + * 'date' => NULL - the scheduled date of the action. Expects a DateTime object, a unix timestamp, or a string that can parsed with strtotime(). Used in UTC timezone.
427 + * 'date_compare' => '<=' - operator for testing "date". accepted values are '!=', '>', '>=', '<', '<=', '='.
428 + * 'modified' => NULL - the date the action was last updated. Expects a DateTime object, a unix timestamp, or a string that can parsed with strtotime(). Used in UTC timezone.
429 + * 'modified_compare' => '<=' - operator for testing "modified". accepted values are '!=', '>', '>=', '<', '<=', '='.
430 + * 'group' => '' - the group the action belongs to.
431 + * 'status' => '' - ActionScheduler_Store::STATUS_COMPLETE or ActionScheduler_Store::STATUS_PENDING.
432 + * 'claimed' => NULL - TRUE to find claimed actions, FALSE to find unclaimed actions, a string to find a specific claim ID.
433 + * 'per_page' => 5 - Number of results to return.
434 + * 'offset' => 0.
435 + * 'orderby' => 'date' - accepted values are 'hook', 'group', 'modified', 'date' or 'none'.
436 + * 'order' => 'ASC'.
272 437 *
273 438 * @param string $return_format OBJECT, ARRAY_A, or ids.
274 439 *
275 440 * @return array
@@ -278,27 +443,27 @@
278 443 if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) {
279 444 return array();
280 445 }
281 446 $store = ActionScheduler::store();
282 - foreach ( array('date', 'modified') as $key ) {
283 - if ( isset($args[$key]) ) {
284 - $args[$key] = as_get_datetime_object($args[$key]);
447 + foreach ( array( 'date', 'modified' ) as $key ) {
448 + if ( isset( $args[ $key ] ) ) {
449 + $args[ $key ] = as_get_datetime_object( $args[ $key ] );
285 450 }
286 451 }
287 452 $ids = $store->query_actions( $args );
288 453
289 - if ( $return_format == 'ids' || $return_format == 'int' ) {
454 + if ( 'ids' === $return_format || 'int' === $return_format ) {
290 455 return $ids;
291 456 }
292 457
293 458 $actions = array();
294 459 foreach ( $ids as $action_id ) {
295 - $actions[$action_id] = $store->fetch_action( $action_id );
460 + $actions[ $action_id ] = $store->fetch_action( $action_id );
296 461 }
297 462
298 - if ( $return_format == ARRAY_A ) {
463 + if ( ARRAY_A === $return_format ) {
299 464 foreach ( $actions as $action_id => $action_object ) {
300 - $actions[$action_id] = get_object_vars($action_object);
465 + $actions[ $action_id ] = get_object_vars( $action_object );
301 466 }
302 467 }
303 468
304 469 return $actions;
@@ -315,9 +480,9 @@
315 480 * always want to be UTC), which means we need to always include the
316 481 * timezone when instantiating datetimes rather than leaving it up to
317 482 * the PHP default.
318 483 *
319 - * @param mixed $date_string A date/time string. Valid formats are explained in http://php.net/manual/en/datetime.formats.php.
484 + * @param mixed $date_string A date/time string. Valid formats are explained in http://php.net/manual/en/datetime.formats.php.
320 485 * @param string $timezone A timezone identifier, like UTC or Europe/Lisbon. The list of valid identifiers is available http://php.net/manual/en/timezones.php.
321 486 *
322 487 * @return ActionScheduler_DateTime
323 488 */
@@ -329,5 +494,20 @@
329 494 } else {
330 495 $date = new ActionScheduler_DateTime( null === $date_string ? 'now' : $date_string, new DateTimeZone( $timezone ) );
331 496 }
332 497 return $date;
498 +}
499 +
500 +/**
501 + * Check if a specific feature is supported by the current version of Action Scheduler.
502 + *
503 + * @since 3.9.3
504 + *
505 + * @param string $feature The feature to check support for.
506 + *
507 + * @return bool True if the feature is supported, false otherwise.
508 + */
509 +function as_supports( string $feature ): bool {
510 + $supported_features = array( 'ensure_recurring_actions_hook' );
511 +
512 + return in_array( $feature, $supported_features, true );
333 513 }