PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / 2.8.16
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell v2.8.16
3.13.3 3.13.2 3.13.1 3.13.0 3.12.13 3.12.12 3.12.11 3.12.10 3.12.9 3.12.8 3.12.7 3.12.6 3.12.5 3.12.4 3.12.3 3.12.1 3.12.2 3.12.0 3.11.1 3.11.0 3.10.9 3.10.8 3.10.7 3.10.6 2.8.16 All 261 releases
wpfunnels / packages / action-scheduler / functions.php

functions.php in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell 2.8.16, at packages/action-scheduler/functions.php

320 lines 11.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * General API functions for scheduling actions
5 */
6
7 /**
8 * Enqueue an action to run one time, as soon as possible
9 *
10 * @param string $hook The hook to trigger.
11 * @param array $args Arguments to pass when the hook triggers.
12 * @param string $group The group to assign this job to.
13 * @return int The action ID.
14 */
15 function as_enqueue_async_action( $hook, $args = array(), $group = '' ) {
16 if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) {
17 return 0;
18 }
19 return ActionScheduler::factory()->async( $hook, $args, $group );
20 }
21
22 /**
23 * Schedule an action to run one time
24 *
25 * @param int $timestamp When the job will run.
26 * @param string $hook The hook to trigger.
27 * @param array $args Arguments to pass when the hook triggers.
28 * @param string $group The group to assign this job to.
29 *
30 * @return int The action ID.
31 */
32 function as_schedule_single_action( $timestamp, $hook, $args = array(), $group = '' ) {
33 if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) {
34 return 0;
35 }
36 return ActionScheduler::factory()->single( $hook, $args, $timestamp, $group );
37 }
38
39 /**
40 * Schedule a recurring action
41 *
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.
44 * @param string $hook The hook to trigger.
45 * @param array $args Arguments to pass when the hook triggers.
46 * @param string $group The group to assign this job to.
47 *
48 * @return int The action ID.
49 */
50 function as_schedule_recurring_action( $timestamp, $interval_in_seconds, $hook, $args = array(), $group = '' ) {
51 if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) {
52 return 0;
53 }
54 return ActionScheduler::factory()->recurring( $hook, $args, $timestamp, $interval_in_seconds, $group );
55 }
56
57 /**
58 * Schedule an action that recurs on a cron-like schedule.
59 *
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
64 * @see http://en.wikipedia.org/wiki/Cron
65 * * * * * * *
66 * ┬ ┬ ┬ ┬ ┬ ┬
67 * | | | | | |
68 * | | | | | + year [optional]
69 * | | | | +----- day of week (0 - 7) (Sunday=0 or 7)
70 * | | | +---------- month (1 - 12)
71 * | | +--------------- day of month (1 - 31)
72 * | +-------------------- hour (0 - 23)
73 * +------------------------- min (0 - 59)
74 * @param string $hook The hook to trigger.
75 * @param array $args Arguments to pass when the hook triggers.
76 * @param string $group The group to assign this job to.
77 *
78 * @return int The action ID.
79 */
80 function as_schedule_cron_action( $timestamp, $schedule, $hook, $args = array(), $group = '' ) {
81 if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) {
82 return 0;
83 }
84 return ActionScheduler::factory()->cron( $hook, $args, $timestamp, $schedule, $group );
85 }
86
87 /**
88 * Cancel the next occurrence of a scheduled action.
89 *
90 * While only the next instance of a recurring or cron action is unscheduled by this method, that will also prevent
91 * all future instances of that recurring or cron action from being run. Recurring and cron actions are scheduled in
92 * a sequence instead of all being scheduled at once. Each successive occurrence of a recurring action is scheduled
93 * only after the former action is run. If the next instance is never run, because it's unscheduled by this function,
94 * then the following instance will never be scheduled (or exist), which is effectively the same as being unscheduled
95 * by this method also.
96 *
97 * @param string $hook The hook that the job will trigger.
98 * @param array $args Args that would have been passed to the job.
99 * @param string $group The group the job is assigned to.
100 *
101 * @return string|null The scheduled action ID if a scheduled action was found, or null if no matching action found.
102 */
103 function as_unschedule_action( $hook, $args = array(), $group = '' ) {
104 if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) {
105 return 0;
106 }
107 $params = array(
108 'hook' => $hook,
109 'status' => ActionScheduler_Store::STATUS_PENDING,
110 'orderby' => 'date',
111 'order' => 'ASC',
112 'group' => $group,
113 );
114 if ( is_array( $args ) ) {
115 $params['args'] = $args;
116 }
117
118 $action_id = ActionScheduler::store()->query_action( $params );
119 if ( $action_id ) {
120 ActionScheduler::store()->cancel_action( $action_id );
121 }
122
123 return $action_id;
124 }
125
126 /**
127 * Cancel all occurrences of a scheduled action.
128 *
129 * @param string $hook The hook that the job will trigger.
130 * @param array $args Args that would have been passed to the job.
131 * @param string $group The group the job is assigned to.
132 */
133 function as_unschedule_all_actions( $hook, $args = array(), $group = '' ) {
134 if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) {
135 return;
136 }
137 if ( empty( $args ) ) {
138 if ( ! empty( $hook ) && empty( $group ) ) {
139 ActionScheduler_Store::instance()->cancel_actions_by_hook( $hook );
140 return;
141 }
142 if ( ! empty( $group ) && empty( $hook ) ) {
143 ActionScheduler_Store::instance()->cancel_actions_by_group( $group );
144 return;
145 }
146 }
147 do {
148 $unscheduled_action = as_unschedule_action( $hook, $args, $group );
149 } while ( ! empty( $unscheduled_action ) );
150 }
151
152 /**
153 * Check if there is an existing action in the queue with a given hook, args and group combination.
154 *
155 * An action in the queue could be pending, in-progress or async. If the is pending for a time in
156 * future, its scheduled date will be returned as a timestamp. If it is currently being run, or an
157 * async action sitting in the queue waiting to be processed, in which case boolean true will be
158 * returned. Or there may be no async, in-progress or pending action for this hook, in which case,
159 * boolean false will be the return value.
160 *
161 * @param string $hook
162 * @param array $args
163 * @param string $group
164 *
165 * @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.
166 */
167 function as_next_scheduled_action( $hook, $args = null, $group = '' ) {
168 if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) {
169 return false;
170 }
171
172 $params = array(
173 'hook' => $hook,
174 'orderby' => 'date',
175 'order' => 'ASC',
176 'group' => $group,
177 );
178
179 if ( is_array( $args ) ) {
180 $params['args'] = $args;
181 }
182
183 $params['status'] = ActionScheduler_Store::STATUS_RUNNING;
184 $action_id = ActionScheduler::store()->query_action( $params );
185 if ( $action_id ) {
186 return true;
187 }
188
189 $params['status'] = ActionScheduler_Store::STATUS_PENDING;
190 $action_id = ActionScheduler::store()->query_action( $params );
191 if ( null === $action_id ) {
192 return false;
193 }
194
195 $action = ActionScheduler::store()->fetch_action( $action_id );
196 $scheduled_date = $action->get_schedule()->get_date();
197 if ( $scheduled_date ) {
198 return (int) $scheduled_date->format( 'U' );
199 } elseif ( null === $scheduled_date ) { // pending async action with NullSchedule
200 return true;
201 }
202
203 return false;
204 }
205
206 /**
207 * Check if there is a scheduled action in the queue but more efficiently than as_next_scheduled_action().
208 *
209 * It's recommended to use this function when you need to know whether a specific action is currently scheduled
210 * (pending or in-progress).
211 *
212 * @since x.x.x
213 *
214 * @param string $hook The hook of the action.
215 * @param array $args Args that have been passed to the action. Null will matches any args.
216 * @param string $group The group the job is assigned to.
217 *
218 * @return bool True if a matching action is pending or in-progress, false otherwise.
219 */
220 function as_has_scheduled_action( $hook, $args = null, $group = '' ) {
221 if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) {
222 return false;
223 }
224
225 $query_args = array(
226 'hook' => $hook,
227 'status' => array( ActionScheduler_Store::STATUS_RUNNING, ActionScheduler_Store::STATUS_PENDING ),
228 'group' => $group,
229 'orderby' => 'none',
230 );
231
232 if ( null !== $args ) {
233 $query_args['args'] = $args;
234 }
235
236 $action_id = ActionScheduler::store()->query_action( $query_args );
237
238 return $action_id !== null;
239 }
240
241 /**
242 * Find scheduled actions
243 *
244 * @param array $args Possible arguments, with their default values:
245 * 'hook' => '' - the name of the action that will be triggered
246 * 'args' => NULL - the args array that will be passed with the action
247 * '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.
248 * 'date_compare' => '<=' - operator for testing "date". accepted values are '!=', '>', '>=', '<', '<=', '='
249 * '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.
250 * 'modified_compare' => '<=' - operator for testing "modified". accepted values are '!=', '>', '>=', '<', '<=', '='
251 * 'group' => '' - the group the action belongs to
252 * 'status' => '' - ActionScheduler_Store::STATUS_COMPLETE or ActionScheduler_Store::STATUS_PENDING
253 * 'claimed' => NULL - TRUE to find claimed actions, FALSE to find unclaimed actions, a string to find a specific claim ID
254 * 'per_page' => 5 - Number of results to return
255 * 'offset' => 0
256 * 'orderby' => 'date' - accepted values are 'hook', 'group', 'modified', 'date' or 'none'
257 * 'order' => 'ASC'
258 *
259 * @param string $return_format OBJECT, ARRAY_A, or ids.
260 *
261 * @return array
262 */
263 function as_get_scheduled_actions( $args = array(), $return_format = OBJECT ) {
264 if ( ! ActionScheduler::is_initialized( __FUNCTION__ ) ) {
265 return array();
266 }
267 $store = ActionScheduler::store();
268 foreach ( array('date', 'modified') as $key ) {
269 if ( isset($args[$key]) ) {
270 $args[$key] = as_get_datetime_object($args[$key]);
271 }
272 }
273 $ids = $store->query_actions( $args );
274
275 if ( $return_format == 'ids' || $return_format == 'int' ) {
276 return $ids;
277 }
278
279 $actions = array();
280 foreach ( $ids as $action_id ) {
281 $actions[$action_id] = $store->fetch_action( $action_id );
282 }
283
284 if ( $return_format == ARRAY_A ) {
285 foreach ( $actions as $action_id => $action_object ) {
286 $actions[$action_id] = get_object_vars($action_object);
287 }
288 }
289
290 return $actions;
291 }
292
293 /**
294 * Helper function to create an instance of DateTime based on a given
295 * string and timezone. By default, will return the current date/time
296 * in the UTC timezone.
297 *
298 * Needed because new DateTime() called without an explicit timezone
299 * will create a date/time in PHP's timezone, but we need to have
300 * assurance that a date/time uses the right timezone (which we almost
301 * always want to be UTC), which means we need to always include the
302 * timezone when instantiating datetimes rather than leaving it up to
303 * the PHP default.
304 *
305 * @param mixed $date_string A date/time string. Valid formats are explained in http://php.net/manual/en/datetime.formats.php.
306 * @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.
307 *
308 * @return ActionScheduler_DateTime
309 */
310 function as_get_datetime_object( $date_string = null, $timezone = 'UTC' ) {
311 if ( is_object( $date_string ) && $date_string instanceof DateTime ) {
312 $date = new ActionScheduler_DateTime( $date_string->format( 'Y-m-d H:i:s' ), new DateTimeZone( $timezone ) );
313 } elseif ( is_numeric( $date_string ) ) {
314 $date = new ActionScheduler_DateTime( '@' . $date_string, new DateTimeZone( $timezone ) );
315 } else {
316 $date = new ActionScheduler_DateTime( $date_string, new DateTimeZone( $timezone ) );
317 }
318 return $date;
319 }
320