PluginProbe ʕ •ᴥ•ʔ
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz / 2.12.1
SureForms – Contact Form Builder, AI Forms, Payment Form, Survey & Quiz v2.12.1
2.12.2 2.12.1 2.12.0 2.11.1 2.11.0 2.10.1 2.10.0 2.9.1 2.9.0 2.8.2 2.8.1 2.7.0 2.7.1 2.8.0 trunk 0.0.10 0.0.11 0.0.12 0.0.13 0.0.2 0.0.3 0.0.4 0.0.5 0.0.6 0.0.7 0.0.8 0.0.9 1.0.0 1.0.1 1.0.2 1.0.3 1.0.4 1.0.5 1.0.6 1.0.7 1.1.0 1.1.1 1.1.2 1.10.0 1.10.1 1.11.0 1.12.0 1.12.1 1.12.2 1.12.3 1.13.0 1.13.1 1.13.2 1.2.0 1.2.1 1.2.2 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.4.0 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.5.0 1.5.1 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 1.7.0 1.7.1 1.7.2 1.7.3 1.7.4 1.8.0 1.9.0 1.9.1 2.0.0 2.0.1 2.0.2 2.1.0 2.1.1 2.2.0 2.2.1 2.2.2 2.3.0 2.4.0 2.5.0 2.5.2 2.6.0
sureforms / inc / lib / action-scheduler / functions.php
sureforms / inc / lib / action-scheduler Last commit date
classes 4 months ago deprecated 2 years ago lib 2 years ago action-scheduler.php 2 years ago changelog.txt 2 years ago functions.php 2 years ago license.txt 2 years ago readme.txt 2 years ago
functions.php
494 lines
1 <?php
2 /**
3 * General API functions for scheduling actions
4 *
5 * @package ActionScheduler.
6 */
7
8 /**
9 * Enqueue an action to run one time, as soon as possible
10 *
11 * @param string $hook The hook to trigger.
12 * @param array $args Arguments to pass when the hook triggers.
13 * @param string $group The group to assign this job to.
14 * @param bool $unique Whether the action should be unique.
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.
18 */
19 function as_enqueue_async_action( $hook, $args = array(), $group = '', $unique = false, $priority = 10 ) {
20 if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) {
21 return 0;
22 }
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 */
39 $pre = apply_filters( 'pre_as_enqueue_async_action', null, $hook, $args, $group, $priority );
40 if ( null !== $pre ) {
41 return is_int( $pre ) ? $pre : 0;
42 }
43
44 return ActionScheduler::factory()->create(
45 array(
46 'type' => 'async',
47 'hook' => $hook,
48 'arguments' => $args,
49 'group' => $group,
50 'unique' => $unique,
51 'priority' => $priority,
52 )
53 );
54 }
55
56 /**
57 * Schedule an action to run one time
58 *
59 * @param int $timestamp When the job will run.
60 * @param string $hook The hook to trigger.
61 * @param array $args Arguments to pass when the hook triggers.
62 * @param string $group The group to assign this job to.
63 * @param bool $unique Whether the action should be unique.
64 * @param int $priority Lower values take precedence over higher values. Defaults to 10, with acceptable values falling in the range 0-255.
65 *
66 * @return int The action ID. Zero if there was an error scheduling the action.
67 */
68 function as_schedule_single_action( $timestamp, $hook, $args = array(), $group = '', $unique = false, $priority = 10 ) {
69 if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) {
70 return 0;
71 }
72
73 /**
74 * Provides an opportunity to short-circuit the default process for enqueuing single
75 * actions.
76 *
77 * Returning a value other than null from the filter will short-circuit the normal
78 * process. The expectation in such a scenario is that callbacks will return an integer
79 * representing the scheduled action ID (scheduled using some alternative process) or else
80 * zero.
81 *
82 * @param int|null $pre_option The value to return instead of the option value.
83 * @param int $timestamp When the action will run.
84 * @param string $hook Action hook.
85 * @param array $args Action arguments.
86 * @param string $group Action group.
87 * @param int $priorities Action priority.
88 */
89 $pre = apply_filters( 'pre_as_schedule_single_action', null, $timestamp, $hook, $args, $group, $priority );
90 if ( null !== $pre ) {
91 return is_int( $pre ) ? $pre : 0;
92 }
93
94 return ActionScheduler::factory()->create(
95 array(
96 'type' => 'single',
97 'hook' => $hook,
98 'arguments' => $args,
99 'when' => $timestamp,
100 'group' => $group,
101 'unique' => $unique,
102 'priority' => $priority,
103 )
104 );
105 }
106
107 /**
108 * Schedule a recurring action
109 *
110 * @param int $timestamp When the first instance of the job will run.
111 * @param int $interval_in_seconds How long to wait between runs.
112 * @param string $hook The hook to trigger.
113 * @param array $args Arguments to pass when the hook triggers.
114 * @param string $group The group to assign this job to.
115 * @param bool $unique Whether the action should be unique.
116 * @param int $priority Lower values take precedence over higher values. Defaults to 10, with acceptable values falling in the range 0-255.
117 *
118 * @return int The action ID. Zero if there was an error scheduling the action.
119 */
120 function as_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args = array(), $group = '', $unique = false, $priority = 10 ) {
121 if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) {
122 return 0;
123 }
124
125 $interval = (int) $interval_in_seconds;
126
127 // We expect an integer and allow it to be passed using float and string types, but otherwise
128 // should reject unexpected values.
129 if ( ! is_numeric( $interval_in_seconds ) || $interval_in_seconds != $interval ) {
130 _doing_it_wrong(
131 __METHOD__,
132 sprintf(
133 /* translators: 1: provided value 2: provided type. */
134 esc_html__( 'An integer was expected but "%1$s" (%2$s) was received.', 'action-scheduler' ),
135 esc_html( $interval_in_seconds ),
136 esc_html( gettype( $interval_in_seconds ) )
137 ),
138 '3.6.0'
139 );
140
141 return 0;
142 }
143
144 /**
145 * Provides an opportunity to short-circuit the default process for enqueuing recurring
146 * actions.
147 *
148 * Returning a value other than null from the filter will short-circuit the normal
149 * process. The expectation in such a scenario is that callbacks will return an integer
150 * representing the scheduled action ID (scheduled using some alternative process) or else
151 * zero.
152 *
153 * @param int|null $pre_option The value to return instead of the option value.
154 * @param int $timestamp When the action will run.
155 * @param int $interval_in_seconds How long to wait between runs.
156 * @param string $hook Action hook.
157 * @param array $args Action arguments.
158 * @param string $group Action group.
159 * @param int $priority Action priority.
160 */
161 $pre = apply_filters( 'pre_as_schedule_recurring_action', null, $timestamp, $interval_in_seconds, $hook, $args, $group, $priority );
162 if ( null !== $pre ) {
163 return is_int( $pre ) ? $pre : 0;
164 }
165
166 return ActionScheduler::factory()->create(
167 array(
168 'type' => 'recurring',
169 'hook' => $hook,
170 'arguments' => $args,
171 'when' => $timestamp,
172 'pattern' => $interval_in_seconds,
173 'group' => $group,
174 'unique' => $unique,
175 'priority' => $priority,
176 )
177 );
178 }
179
180 /**
181 * Schedule an action that recurs on a cron-like schedule.
182 *
183 * @param int $timestamp The first instance of the action will be scheduled
184 * to run at a time calculated after this timestamp matching the cron
185 * expression. This can be used to delay the first instance of the action.
186 * @param string $schedule A cron-link schedule string.
187 * @see http://en.wikipedia.org/wiki/Cron
188 * * * * * * *
189 * ┬ ┬ ┬ ┬ ┬ ┬
190 * | | | | | |
191 * | | | | | + year [optional]
192 * | | | | +----- day of week (0 - 7) (Sunday=0 or 7)
193 * | | | +---------- month (1 - 12)
194 * | | +--------------- day of month (1 - 31)
195 * | +-------------------- hour (0 - 23)
196 * +------------------------- min (0 - 59)
197 * @param string $hook The hook to trigger.
198 * @param array $args Arguments to pass when the hook triggers.
199 * @param string $group The group to assign this job to.
200 * @param bool $unique Whether the action should be unique.
201 * @param int $priority Lower values take precedence over higher values. Defaults to 10, with acceptable values falling in the range 0-255.
202 *
203 * @return int The action ID. Zero if there was an error scheduling the action.
204 */
205 function as_schedule_cron_action( $timestamp, $schedule, $hook, $args = array(), $group = '', $unique = false, $priority = 10 ) {
206 if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) {
207 return 0;
208 }
209
210 /**
211 * Provides an opportunity to short-circuit the default process for enqueuing cron
212 * actions.
213 *
214 * Returning a value other than null from the filter will short-circuit the normal
215 * process. The expectation in such a scenario is that callbacks will return an integer
216 * representing the scheduled action ID (scheduled using some alternative process) or else
217 * zero.
218 *
219 * @param int|null $pre_option The value to return instead of the option value.
220 * @param int $timestamp When the action will run.
221 * @param string $schedule Cron-like schedule string.
222 * @param string $hook Action hook.
223 * @param array $args Action arguments.
224 * @param string $group Action group.
225 * @param int $priority Action priority.
226 */
227 $pre = apply_filters( 'pre_as_schedule_cron_action', null, $timestamp, $schedule, $hook, $args, $group, $priority );
228 if ( null !== $pre ) {
229 return is_int( $pre ) ? $pre : 0;
230 }
231
232 return ActionScheduler::factory()->create(
233 array(
234 'type' => 'cron',
235 'hook' => $hook,
236 'arguments' => $args,
237 'when' => $timestamp,
238 'pattern' => $schedule,
239 'group' => $group,
240 'unique' => $unique,
241 'priority' => $priority,
242 )
243 );
244 }
245
246 /**
247 * Cancel the next occurrence of a scheduled action.
248 *
249 * While only the next instance of a recurring or cron action is unscheduled by this method, that will also prevent
250 * all future instances of that recurring or cron action from being run. Recurring and cron actions are scheduled in
251 * a sequence instead of all being scheduled at once. Each successive occurrence of a recurring action is scheduled
252 * only after the former action is run. If the next instance is never run, because it's unscheduled by this function,
253 * then the following instance will never be scheduled (or exist), which is effectively the same as being unscheduled
254 * by this method also.
255 *
256 * @param string $hook The hook that the job will trigger.
257 * @param array $args Args that would have been passed to the job.
258 * @param string $group The group the job is assigned to.
259 *
260 * @return int|null The scheduled action ID if a scheduled action was found, or null if no matching action found.
261 */
262 function as_unschedule_action( $hook, $args = array(), $group = '' ) {
263 if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) {
264 return 0;
265 }
266 $params = array(
267 'hook' => $hook,
268 'status' => ActionScheduler_Store::STATUS_PENDING,
269 'orderby' => 'date',
270 'order' => 'ASC',
271 'group' => $group,
272 );
273 if ( is_array( $args ) ) {
274 $params['args'] = $args;
275 }
276
277 $action_id = ActionScheduler::store()->query_action( $params );
278
279 if ( $action_id ) {
280 try {
281 ActionScheduler::store()->cancel_action( $action_id );
282 } catch ( Exception $exception ) {
283 ActionScheduler::logger()->log(
284 $action_id,
285 sprintf(
286 /* translators: %1$s is the name of the hook to be cancelled, %2$s is the exception message. */
287 __( 'Caught exception while cancelling action "%1$s": %2$s', 'action-scheduler' ),
288 $hook,
289 $exception->getMessage()
290 )
291 );
292
293 $action_id = null;
294 }
295 }
296
297 return $action_id;
298 }
299
300 /**
301 * Cancel all occurrences of a scheduled action.
302 *
303 * @param string $hook The hook that the job will trigger.
304 * @param array $args Args that would have been passed to the job.
305 * @param string $group The group the job is assigned to.
306 */
307 function as_unschedule_all_actions( $hook, $args = array(), $group = '' ) {
308 if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) {
309 return;
310 }
311 if ( empty( $args ) ) {
312 if ( ! empty( $hook ) && empty( $group ) ) {
313 ActionScheduler_Store::instance()->cancel_actions_by_hook( $hook );
314 return;
315 }
316 if ( ! empty( $group ) && empty( $hook ) ) {
317 ActionScheduler_Store::instance()->cancel_actions_by_group( $group );
318 return;
319 }
320 }
321 do {
322 $unscheduled_action = as_unschedule_action( $hook, $args, $group );
323 } while ( ! empty( $unscheduled_action ) );
324 }
325
326 /**
327 * Check if there is an existing action in the queue with a given hook, args and group combination.
328 *
329 * An action in the queue could be pending, in-progress or async. If the is pending for a time in
330 * future, its scheduled date will be returned as a timestamp. If it is currently being run, or an
331 * async action sitting in the queue waiting to be processed, in which case boolean true will be
332 * returned. Or there may be no async, in-progress or pending action for this hook, in which case,
333 * boolean false will be the return value.
334 *
335 * @param string $hook Name of the hook to search for.
336 * @param array $args Arguments of the action to be searched.
337 * @param string $group Group of the action to be searched.
338 *
339 * @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.
340 */
341 function as_next_scheduled_action( $hook, $args = null, $group = '' ) {
342 if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) {
343 return false;
344 }
345
346 $params = array(
347 'hook' => $hook,
348 'orderby' => 'date',
349 'order' => 'ASC',
350 'group' => $group,
351 );
352
353 if ( is_array( $args ) ) {
354 $params['args'] = $args;
355 }
356
357 $params['status'] = ActionScheduler_Store::STATUS_RUNNING;
358 $action_id = ActionScheduler::store()->query_action( $params );
359 if ( $action_id ) {
360 return true;
361 }
362
363 $params['status'] = ActionScheduler_Store::STATUS_PENDING;
364 $action_id = ActionScheduler::store()->query_action( $params );
365 if ( null === $action_id ) {
366 return false;
367 }
368
369 $action = ActionScheduler::store()->fetch_action( $action_id );
370 $scheduled_date = $action->get_schedule()->get_date();
371 if ( $scheduled_date ) {
372 return (int) $scheduled_date->format( 'U' );
373 } elseif ( null === $scheduled_date ) { // pending async action with NullSchedule.
374 return true;
375 }
376
377 return false;
378 }
379
380 /**
381 * Check if there is a scheduled action in the queue but more efficiently than as_next_scheduled_action().
382 *
383 * It's recommended to use this function when you need to know whether a specific action is currently scheduled
384 * (pending or in-progress).
385 *
386 * @since 3.3.0
387 *
388 * @param string $hook The hook of the action.
389 * @param array $args Args that have been passed to the action. Null will matches any args.
390 * @param string $group The group the job is assigned to.
391 *
392 * @return bool True if a matching action is pending or in-progress, false otherwise.
393 */
394 function as_has_scheduled_action( $hook, $args = null, $group = '' ) {
395 if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) {
396 return false;
397 }
398
399 $query_args = array(
400 'hook' => $hook,
401 'status' => array( ActionScheduler_Store::STATUS_RUNNING, ActionScheduler_Store::STATUS_PENDING ),
402 'group' => $group,
403 'orderby' => 'none',
404 );
405
406 if ( null !== $args ) {
407 $query_args['args'] = $args;
408 }
409
410 $action_id = ActionScheduler::store()->query_action( $query_args );
411
412 return null !== $action_id;
413 }
414
415 /**
416 * Find scheduled actions
417 *
418 * @param array $args Possible arguments, with their default values.
419 * 'hook' => '' - the name of the action that will be triggered.
420 * 'args' => NULL - the args array that will be passed with the action.
421 * '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.
422 * 'date_compare' => '<=' - operator for testing "date". accepted values are '!=', '>', '>=', '<', '<=', '='.
423 * '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.
424 * 'modified_compare' => '<=' - operator for testing "modified". accepted values are '!=', '>', '>=', '<', '<=', '='.
425 * 'group' => '' - the group the action belongs to.
426 * 'status' => '' - ActionScheduler_Store::STATUS_COMPLETE or ActionScheduler_Store::STATUS_PENDING.
427 * 'claimed' => NULL - TRUE to find claimed actions, FALSE to find unclaimed actions, a string to find a specific claim ID.
428 * 'per_page' => 5 - Number of results to return.
429 * 'offset' => 0.
430 * 'orderby' => 'date' - accepted values are 'hook', 'group', 'modified', 'date' or 'none'.
431 * 'order' => 'ASC'.
432 *
433 * @param string $return_format OBJECT, ARRAY_A, or ids.
434 *
435 * @return array
436 */
437 function as_get_scheduled_actions( $args = array(), $return_format = OBJECT ) {
438 if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) {
439 return array();
440 }
441 $store = ActionScheduler::store();
442 foreach ( array( 'date', 'modified' ) as $key ) {
443 if ( isset( $args[ $key ] ) ) {
444 $args[ $key ] = as_get_datetime_object( $args[ $key ] );
445 }
446 }
447 $ids = $store->query_actions( $args );
448
449 if ( 'ids' === $return_format || 'int' === $return_format ) {
450 return $ids;
451 }
452
453 $actions = array();
454 foreach ( $ids as $action_id ) {
455 $actions[ $action_id ] = $store->fetch_action( $action_id );
456 }
457
458 if ( ARRAY_A == $return_format ) {
459 foreach ( $actions as $action_id => $action_object ) {
460 $actions[ $action_id ] = get_object_vars( $action_object );
461 }
462 }
463
464 return $actions;
465 }
466
467 /**
468 * Helper function to create an instance of DateTime based on a given
469 * string and timezone. By default, will return the current date/time
470 * in the UTC timezone.
471 *
472 * Needed because new DateTime() called without an explicit timezone
473 * will create a date/time in PHP's timezone, but we need to have
474 * assurance that a date/time uses the right timezone (which we almost
475 * always want to be UTC), which means we need to always include the
476 * timezone when instantiating datetimes rather than leaving it up to
477 * the PHP default.
478 *
479 * @param mixed $date_string A date/time string. Valid formats are explained in http://php.net/manual/en/datetime.formats.php.
480 * @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.
481 *
482 * @return ActionScheduler_DateTime
483 */
484 function as_get_datetime_object( $date_string = null, $timezone = 'UTC' ) {
485 if ( is_object( $date_string ) && $date_string instanceof DateTime ) {
486 $date = new ActionScheduler_DateTime( $date_string->format( 'Y-m-d H:i:s' ), new DateTimeZone( $timezone ) );
487 } elseif ( is_numeric( $date_string ) ) {
488 $date = new ActionScheduler_DateTime( '@' . $date_string, new DateTimeZone( $timezone ) );
489 } else {
490 $date = new ActionScheduler_DateTime( null === $date_string ? 'now' : $date_string, new DateTimeZone( $timezone ) );
491 }
492 return $date;
493 }
494