PluginProbe ʕ •ᴥ•ʔ
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments / 4.6.4
SureCart – Ecommerce Made Easy For Selling Physical Products, Digital Downloads, Subscriptions, Donations, & Payments v4.6.4
4.7.0 4.6.6 4.6.5 4.6.4 4.6.3 4.6.2 4.6.1 4.6.0 4.5.1 4.5.0 4.4.2 4.4.1 4.4.0 4.3.3 4.3.2 4.3.1 4.3.0 4.2.3 4.2.2 4.2.1 1.0.3 1.0.4 1.0.5 1.0.6 1.1.0 1.1.1 1.1.10 1.1.11 1.1.12 1.1.13 1.1.14 1.1.15 1.1.16 1.1.17 1.1.18 1.1.19 1.1.2 1.1.3 1.1.4 1.1.5 1.1.6 1.1.7 1.1.8 1.1.9 1.10.0 1.10.1 1.10.2 1.10.3 1.10.4 1.11.0 1.11.1 1.11.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.3.3 1.3.4 1.4.0 1.4.1 1.4.2 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.5.5 1.5.6 1.5.7 1.5.8 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.7.0 1.7.1 1.7.2 1.8.0 1.8.1 1.8.2 1.8.3 1.8.4 1.8.5 1.9.0 1.9.1 1.9.2 1.9.3 1.9.4 1.9.5 2.0.0 2.0.1 2.1.0 2.1.1 2.1.2 2.1.3 2.1.4 2.10.0 2.10.1 2.11.0 2.11.1 2.11.2 2.11.3 2.11.4 2.12.0 2.13.0 2.14.0 2.14.1 2.15.0 2.15.1 2.16.0 2.16.1 2.16.2 2.16.3 2.17.0 2.17.1 2.17.2 2.18.0 2.19.0 2.19.2 2.19.3 2.19.4 2.2.0 2.2.1 2.20.0 2.20.1 2.20.2 2.20.3 2.20.4 2.20.5 2.20.6 2.21.0 2.22.0 2.22.1 2.23.0 2.24.0 2.25.0 2.25.1 2.25.2 2.26.0 2.27.0 2.27.1 2.28.0 2.29.0 2.29.1 2.29.2 2.29.3 2.29.4 2.3.0 2.3.1 2.30.0 2.31.0 2.31.1 2.31.2 2.31.3 2.4.0 2.4.1 2.4.2 2.4.3 2.4.4 2.40.0 2.40.1 2.5.0 2.5.1 2.5.2 2.6.0 2.6.1 2.6.2 2.7.0 2.7.1 2.7.2 2.7.3 2.7.4 2.7.5 2.8.0 2.8.1 2.8.2 2.8.3 2.8.4 2.9.0 3.0.0 3.0.0-RC1 3.0.0-RC2 3.0.0-beta1 3.0.0-beta2 3.0.1 3.0.2 3.0.3 3.0.4 3.0.5 3.1.0 3.1.1 3.1.2 3.1.3 3.1.4 3.1.5 3.1.6 3.10.0 3.10.1 3.11.0 3.12.0 3.13.0 3.13.1 3.13.2 3.13.3 3.13.4 3.14.0 3.15.0 3.15.1 3.15.2 3.15.3 3.15.4 3.15.5 3.16.0 3.16.1 3.16.2 3.16.3 3.16.4 3.16.5 3.16.6 3.16.7 3.16.8 3.17.0 3.17.1 3.17.2 3.17.3 3.17.4 3.17.5 3.17.6 3.18.0 3.19.0 3.19.1 3.19.2 3.2.0 3.2.1 3.2.2 3.20.0 3.20.1 3.3.0 3.3.1 3.4.0 3.4.1 3.4.2 3.4.3 3.5.0 3.5.1 3.5.2 3.5.3 3.6.0 3.6.1 3.6.2 3.7.0 3.7.1 3.7.2 3.7.3 3.8.0 3.8.1 3.8.2 3.8.3 3.8.4 3.8.5 3.9.0 4.0.0 4.0.1 4.0.2 4.0.3 trunk 4.1.0 0.2.19.1 4.1.1 1.0.0 4.2.0 1.0.1 1.0.2
surecart / vendor / woocommerce / action-scheduler / classes / ActionScheduler_ActionFactory.php
surecart / vendor / woocommerce / action-scheduler / classes Last commit date
WP_CLI 3 years ago abstracts 1 year ago actions 3 years ago data-stores 1 year ago migration 1 year ago schedules 3 years ago schema 2 years ago ActionScheduler_ActionClaim.php 3 years ago ActionScheduler_ActionFactory.php 1 year ago ActionScheduler_AdminView.php 1 year ago ActionScheduler_AsyncRequest_QueueRunner.php 3 years ago ActionScheduler_Compatibility.php 3 years ago ActionScheduler_DataController.php 3 years ago ActionScheduler_DateTime.php 3 years ago ActionScheduler_Exception.php 3 years ago ActionScheduler_FatalErrorMonitor.php 3 years ago ActionScheduler_InvalidActionException.php 3 years ago ActionScheduler_ListTable.php 1 year ago ActionScheduler_LogEntry.php 3 years ago ActionScheduler_NullLogEntry.php 3 years ago ActionScheduler_OptionLock.php 2 years ago ActionScheduler_QueueCleaner.php 3 years ago ActionScheduler_QueueRunner.php 2 years ago ActionScheduler_Versions.php 3 years ago ActionScheduler_WPCommentCleaner.php 3 years ago ActionScheduler_wcSystemStatus.php 3 years ago
ActionScheduler_ActionFactory.php
358 lines
1 <?php
2
3 /**
4 * Class ActionScheduler_ActionFactory
5 */
6 class ActionScheduler_ActionFactory {
7
8 /**
9 * Return stored actions for given params.
10 *
11 * @param string $status The action's status in the data store.
12 * @param string $hook The hook to trigger when this action runs.
13 * @param array $args Args to pass to callbacks when the hook is triggered.
14 * @param ActionScheduler_Schedule $schedule The action's schedule.
15 * @param string $group A group to put the action in.
16 * phpcs:ignore Squiz.Commenting.FunctionComment.ExtraParamComment
17 * @param int $priority The action priority.
18 *
19 * @return ActionScheduler_Action An instance of the stored action.
20 */
21 public function get_stored_action( $status, $hook, array $args = array(), ActionScheduler_Schedule $schedule = null, $group = '' ) {
22 // The 6th parameter ($priority) is not formally declared in the method signature to maintain compatibility with
23 // third-party subclasses created before this param was added.
24 $priority = func_num_args() >= 6 ? (int) func_get_arg( 5 ) : 10;
25
26 switch ( $status ) {
27 case ActionScheduler_Store::STATUS_PENDING:
28 $action_class = 'ActionScheduler_Action';
29 break;
30 case ActionScheduler_Store::STATUS_CANCELED:
31 $action_class = 'ActionScheduler_CanceledAction';
32 if ( ! is_null( $schedule ) && ! is_a( $schedule, 'ActionScheduler_CanceledSchedule' ) && ! is_a( $schedule, 'ActionScheduler_NullSchedule' ) ) {
33 $schedule = new ActionScheduler_CanceledSchedule( $schedule->get_date() );
34 }
35 break;
36 default:
37 $action_class = 'ActionScheduler_FinishedAction';
38 break;
39 }
40
41 $action_class = apply_filters( 'action_scheduler_stored_action_class', $action_class, $status, $hook, $args, $schedule, $group );
42
43 $action = new $action_class( $hook, $args, $schedule, $group );
44 $action->set_priority( $priority );
45
46 /**
47 * Allow 3rd party code to change the instantiated action for a given hook, args, schedule and group.
48 *
49 * @param ActionScheduler_Action $action The instantiated action.
50 * @param string $hook The instantiated action's hook.
51 * @param array $args The instantiated action's args.
52 * @param ActionScheduler_Schedule $schedule The instantiated action's schedule.
53 * @param string $group The instantiated action's group.
54 * @param int $priority The action priority.
55 */
56 return apply_filters( 'action_scheduler_stored_action_instance', $action, $hook, $args, $schedule, $group, $priority );
57 }
58
59 /**
60 * Enqueue an action to run one time, as soon as possible (rather a specific scheduled time).
61 *
62 * This method creates a new action using the NullSchedule. In practice, this results in an action scheduled to
63 * execute "now". Therefore, it will generally run as soon as possible but is not prioritized ahead of other actions
64 * that are already past-due.
65 *
66 * @param string $hook The hook to trigger when this action runs.
67 * @param array $args Args to pass when the hook is triggered.
68 * @param string $group A group to put the action in.
69 *
70 * @return int The ID of the stored action.
71 */
72 public function async( $hook, $args = array(), $group = '' ) {
73 return $this->async_unique( $hook, $args, $group, false );
74 }
75
76 /**
77 * Same as async, but also supports $unique param.
78 *
79 * @param string $hook The hook to trigger when this action runs.
80 * @param array $args Args to pass when the hook is triggered.
81 * @param string $group A group to put the action in.
82 * @param bool $unique Whether to ensure the action is unique.
83 *
84 * @return int The ID of the stored action.
85 */
86 public function async_unique( $hook, $args = array(), $group = '', $unique = true ) {
87 $schedule = new ActionScheduler_NullSchedule();
88 $action = new ActionScheduler_Action( $hook, $args, $schedule, $group );
89 return $unique ? $this->store_unique_action( $action, $unique ) : $this->store( $action );
90 }
91
92 /**
93 * Create single action.
94 *
95 * @param string $hook The hook to trigger when this action runs.
96 * @param array $args Args to pass when the hook is triggered.
97 * @param int $when Unix timestamp when the action will run.
98 * @param string $group A group to put the action in.
99 *
100 * @return int The ID of the stored action.
101 */
102 public function single( $hook, $args = array(), $when = null, $group = '' ) {
103 return $this->single_unique( $hook, $args, $when, $group, false );
104 }
105
106 /**
107 * Create single action only if there is no pending or running action with same name and params.
108 *
109 * @param string $hook The hook to trigger when this action runs.
110 * @param array $args Args to pass when the hook is triggered.
111 * @param int $when Unix timestamp when the action will run.
112 * @param string $group A group to put the action in.
113 * @param bool $unique Whether action scheduled should be unique.
114 *
115 * @return int The ID of the stored action.
116 */
117 public function single_unique( $hook, $args = array(), $when = null, $group = '', $unique = true ) {
118 $date = as_get_datetime_object( $when );
119 $schedule = new ActionScheduler_SimpleSchedule( $date );
120 $action = new ActionScheduler_Action( $hook, $args, $schedule, $group );
121 return $unique ? $this->store_unique_action( $action ) : $this->store( $action );
122 }
123
124 /**
125 * Create the first instance of an action recurring on a given interval.
126 *
127 * @param string $hook The hook to trigger when this action runs.
128 * @param array $args Args to pass when the hook is triggered.
129 * @param int $first Unix timestamp for the first run.
130 * @param int $interval Seconds between runs.
131 * @param string $group A group to put the action in.
132 *
133 * @return int The ID of the stored action.
134 */
135 public function recurring( $hook, $args = array(), $first = null, $interval = null, $group = '' ) {
136 return $this->recurring_unique( $hook, $args, $first, $interval, $group, false );
137 }
138
139 /**
140 * Create the first instance of an action recurring on a given interval only if there is no pending or running action with same name and params.
141 *
142 * @param string $hook The hook to trigger when this action runs.
143 * @param array $args Args to pass when the hook is triggered.
144 * @param int $first Unix timestamp for the first run.
145 * @param int $interval Seconds between runs.
146 * @param string $group A group to put the action in.
147 * @param bool $unique Whether action scheduled should be unique.
148 *
149 * @return int The ID of the stored action.
150 */
151 public function recurring_unique( $hook, $args = array(), $first = null, $interval = null, $group = '', $unique = true ) {
152 if ( empty( $interval ) ) {
153 return $this->single_unique( $hook, $args, $first, $group, $unique );
154 }
155 $date = as_get_datetime_object( $first );
156 $schedule = new ActionScheduler_IntervalSchedule( $date, $interval );
157 $action = new ActionScheduler_Action( $hook, $args, $schedule, $group );
158 return $unique ? $this->store_unique_action( $action ) : $this->store( $action );
159 }
160
161 /**
162 * Create the first instance of an action recurring on a Cron schedule.
163 *
164 * @param string $hook The hook to trigger when this action runs.
165 * @param array $args Args to pass when the hook is triggered.
166 * @param int $base_timestamp The first instance of the action will be scheduled
167 * to run at a time calculated after this timestamp matching the cron
168 * expression. This can be used to delay the first instance of the action.
169 * @param int $schedule A cron definition string.
170 * @param string $group A group to put the action in.
171 *
172 * @return int The ID of the stored action.
173 */
174 public function cron( $hook, $args = array(), $base_timestamp = null, $schedule = null, $group = '' ) {
175 return $this->cron_unique( $hook, $args, $base_timestamp, $schedule, $group, false );
176 }
177
178
179 /**
180 * Create the first instance of an action recurring on a Cron schedule only if there is no pending or running action with same name and params.
181 *
182 * @param string $hook The hook to trigger when this action runs.
183 * @param array $args Args to pass when the hook is triggered.
184 * @param int $base_timestamp The first instance of the action will be scheduled
185 * to run at a time calculated after this timestamp matching the cron
186 * expression. This can be used to delay the first instance of the action.
187 * @param int $schedule A cron definition string.
188 * @param string $group A group to put the action in.
189 * @param bool $unique Whether action scheduled should be unique.
190 *
191 * @return int The ID of the stored action.
192 **/
193 public function cron_unique( $hook, $args = array(), $base_timestamp = null, $schedule = null, $group = '', $unique = true ) {
194 if ( empty( $schedule ) ) {
195 return $this->single_unique( $hook, $args, $base_timestamp, $group, $unique );
196 }
197 $date = as_get_datetime_object( $base_timestamp );
198 $cron = CronExpression::factory( $schedule );
199 $schedule = new ActionScheduler_CronSchedule( $date, $cron );
200 $action = new ActionScheduler_Action( $hook, $args, $schedule, $group );
201 return $unique ? $this->store_unique_action( $action ) : $this->store( $action );
202 }
203
204 /**
205 * Create a successive instance of a recurring or cron action.
206 *
207 * Importantly, the action will be rescheduled to run based on the current date/time.
208 * That means when the action is scheduled to run in the past, the next scheduled date
209 * will be pushed forward. For example, if a recurring action set to run every hour
210 * was scheduled to run 5 seconds ago, it will be next scheduled for 1 hour in the
211 * future, which is 1 hour and 5 seconds from when it was last scheduled to run.
212 *
213 * Alternatively, if the action is scheduled to run in the future, and is run early,
214 * likely via manual intervention, then its schedule will change based on the time now.
215 * For example, if a recurring action set to run every day, and is run 12 hours early,
216 * it will run again in 24 hours, not 36 hours.
217 *
218 * This slippage is less of an issue with Cron actions, as the specific run time can
219 * be set for them to run, e.g. 1am each day. In those cases, and entire period would
220 * need to be missed before there was any change is scheduled, e.g. in the case of an
221 * action scheduled for 1am each day, the action would need to run an entire day late.
222 *
223 * @param ActionScheduler_Action $action The existing action.
224 *
225 * @return string The ID of the stored action
226 * @throws InvalidArgumentException If $action is not a recurring action.
227 */
228 public function repeat( $action ) {
229 $schedule = $action->get_schedule();
230 $next = $schedule->get_next( as_get_datetime_object() );
231
232 if ( is_null( $next ) || ! $schedule->is_recurring() ) {
233 throw new InvalidArgumentException( __( 'Invalid action - must be a recurring action.', 'action-scheduler' ) );
234 }
235
236 $schedule_class = get_class( $schedule );
237 $new_schedule = new $schedule( $next, $schedule->get_recurrence(), $schedule->get_first_date() );
238 $new_action = new ActionScheduler_Action( $action->get_hook(), $action->get_args(), $new_schedule, $action->get_group() );
239 $new_action->set_priority( $action->get_priority() );
240 return $this->store( $new_action );
241 }
242
243 /**
244 * Creates a scheduled action.
245 *
246 * This general purpose method can be used in place of specific methods such as async(),
247 * async_unique(), single() or single_unique(), etc.
248 *
249 * @internal Not intended for public use, should not be overriden by subclasses.
250 *
251 * @param array $options {
252 * Describes the action we wish to schedule.
253 *
254 * @type string $type Must be one of 'async', 'cron', 'recurring', or 'single'.
255 * @type string $hook The hook to be executed.
256 * @type array $arguments Arguments to be passed to the callback.
257 * @type string $group The action group.
258 * @type bool $unique If the action should be unique.
259 * @type int $when Timestamp. Indicates when the action, or first instance of the action in the case
260 * of recurring or cron actions, becomes due.
261 * @type int|string $pattern Recurrence pattern. This is either an interval in seconds for recurring actions
262 * or a cron expression for cron actions.
263 * @type int $priority Lower values means higher priority. Should be in the range 0-255.
264 * }
265 *
266 * @return int The action ID. Zero if there was an error scheduling the action.
267 */
268 public function create( array $options = array() ) {
269 $defaults = array(
270 'type' => 'single',
271 'hook' => '',
272 'arguments' => array(),
273 'group' => '',
274 'unique' => false,
275 'when' => time(),
276 'pattern' => null,
277 'priority' => 10,
278 );
279
280 $options = array_merge( $defaults, $options );
281
282 // Cron/recurring actions without a pattern are treated as single actions (this gives calling code the ability
283 // to use functions like as_schedule_recurring_action() to schedule recurring as well as single actions).
284 if ( ( 'cron' === $options['type'] || 'recurring' === $options['type'] ) && empty( $options['pattern'] ) ) {
285 $options['type'] = 'single';
286 }
287
288 switch ( $options['type'] ) {
289 case 'async':
290 $schedule = new ActionScheduler_NullSchedule();
291 break;
292
293 case 'cron':
294 $date = as_get_datetime_object( $options['when'] );
295 $cron = CronExpression::factory( $options['pattern'] );
296 $schedule = new ActionScheduler_CronSchedule( $date, $cron );
297 break;
298
299 case 'recurring':
300 $date = as_get_datetime_object( $options['when'] );
301 $schedule = new ActionScheduler_IntervalSchedule( $date, $options['pattern'] );
302 break;
303
304 case 'single':
305 $date = as_get_datetime_object( $options['when'] );
306 $schedule = new ActionScheduler_SimpleSchedule( $date );
307 break;
308
309 default:
310 error_log( "Unknown action type '{$options['type']}' specified when trying to create an action for '{$options['hook']}'." );
311 return 0;
312 }
313
314 $action = new ActionScheduler_Action( $options['hook'], $options['arguments'], $schedule, $options['group'] );
315 $action->set_priority( $options['priority'] );
316
317 $action_id = 0;
318 try {
319 $action_id = $options['unique'] ? $this->store_unique_action( $action ) : $this->store( $action );
320 } catch ( Exception $e ) {
321 error_log(
322 sprintf(
323 /* translators: %1$s is the name of the hook to be enqueued, %2$s is the exception message. */
324 __( 'Caught exception while enqueuing action "%1$s": %2$s', 'action-scheduler' ),
325 $options['hook'],
326 $e->getMessage()
327 )
328 );
329 }
330 return $action_id;
331 }
332
333 /**
334 * Save action to database.
335 *
336 * @param ActionScheduler_Action $action Action object to save.
337 *
338 * @return int The ID of the stored action
339 */
340 protected function store( ActionScheduler_Action $action ) {
341 $store = ActionScheduler_Store::instance();
342 return $store->save_action( $action );
343 }
344
345 /**
346 * Store action if it's unique.
347 *
348 * @param ActionScheduler_Action $action Action object to store.
349 *
350 * @return int ID of the created action. Will be 0 if action was not created.
351 */
352 protected function store_unique_action( ActionScheduler_Action $action ) {
353 $store = ActionScheduler_Store::instance();
354 return method_exists( $store, 'save_unique_action' ) ?
355 $store->save_unique_action( $action ) : $store->save_action( $action );
356 }
357 }
358