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.5 2.12.4 2.12.3 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 / classes / ActionScheduler_ActionFactory.php
sureforms / inc / lib / action-scheduler / classes Last commit date
WP_CLI 2 years ago abstracts 5 months ago actions 2 years ago data-stores 2 years ago migration 2 years ago schedules 2 years ago schema 2 years ago ActionScheduler_ActionClaim.php 2 years ago ActionScheduler_ActionFactory.php 2 years ago ActionScheduler_AdminView.php 2 years ago ActionScheduler_AsyncRequest_QueueRunner.php 2 years ago ActionScheduler_Compatibility.php 2 years ago ActionScheduler_DataController.php 2 years ago ActionScheduler_DateTime.php 2 years ago ActionScheduler_Exception.php 2 years ago ActionScheduler_FatalErrorMonitor.php 2 years ago ActionScheduler_InvalidActionException.php 2 years ago ActionScheduler_ListTable.php 2 years ago ActionScheduler_LogEntry.php 2 years ago ActionScheduler_NullLogEntry.php 2 years ago ActionScheduler_OptionLock.php 2 years ago ActionScheduler_QueueCleaner.php 2 years ago ActionScheduler_QueueRunner.php 2 years ago ActionScheduler_Versions.php 2 years ago ActionScheduler_WPCommentCleaner.php 2 years ago ActionScheduler_wcSystemStatus.php 2 years ago
ActionScheduler_ActionFactory.php
359 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 *
17 * phpcs:ignore Squiz.Commenting.FunctionComment.ExtraParamComment
18 * @param int $priority The action priority.
19 *
20 * @return ActionScheduler_Action An instance of the stored action.
21 */
22 public function get_stored_action( $status, $hook, array $args = array(), ActionScheduler_Schedule $schedule = null, $group = '' ) {
23 // The 6th parameter ($priority) is not formally declared in the method signature to maintain compatibility with
24 // third-party subclasses created before this param was added.
25 $priority = func_num_args() >= 6 ? (int) func_get_arg( 5 ) : 10;
26
27 switch ( $status ) {
28 case ActionScheduler_Store::STATUS_PENDING:
29 $action_class = 'ActionScheduler_Action';
30 break;
31 case ActionScheduler_Store::STATUS_CANCELED:
32 $action_class = 'ActionScheduler_CanceledAction';
33 if ( ! is_null( $schedule ) && ! is_a( $schedule, 'ActionScheduler_CanceledSchedule' ) && ! is_a( $schedule, 'ActionScheduler_NullSchedule' ) ) {
34 $schedule = new ActionScheduler_CanceledSchedule( $schedule->get_date() );
35 }
36 break;
37 default:
38 $action_class = 'ActionScheduler_FinishedAction';
39 break;
40 }
41
42 $action_class = apply_filters( 'action_scheduler_stored_action_class', $action_class, $status, $hook, $args, $schedule, $group );
43
44 $action = new $action_class( $hook, $args, $schedule, $group );
45 $action->set_priority( $priority );
46
47 /**
48 * Allow 3rd party code to change the instantiated action for a given hook, args, schedule and group.
49 *
50 * @param ActionScheduler_Action $action The instantiated action.
51 * @param string $hook The instantiated action's hook.
52 * @param array $args The instantiated action's args.
53 * @param ActionScheduler_Schedule $schedule The instantiated action's schedule.
54 * @param string $group The instantiated action's group.
55 * @param int $priority The action priority.
56 */
57 return apply_filters( 'action_scheduler_stored_action_instance', $action, $hook, $args, $schedule, $group, $priority );
58 }
59
60 /**
61 * Enqueue an action to run one time, as soon as possible (rather a specific scheduled time).
62 *
63 * This method creates a new action using the NullSchedule. In practice, this results in an action scheduled to
64 * execute "now". Therefore, it will generally run as soon as possible but is not prioritized ahead of other actions
65 * that are already past-due.
66 *
67 * @param string $hook The hook to trigger when this action runs.
68 * @param array $args Args to pass when the hook is triggered.
69 * @param string $group A group to put the action in.
70 *
71 * @return int The ID of the stored action.
72 */
73 public function async( $hook, $args = array(), $group = '' ) {
74 return $this->async_unique( $hook, $args, $group, false );
75 }
76
77 /**
78 * Same as async, but also supports $unique param.
79 *
80 * @param string $hook The hook to trigger when this action runs.
81 * @param array $args Args to pass when the hook is triggered.
82 * @param string $group A group to put the action in.
83 * @param bool $unique Whether to ensure the action is unique.
84 *
85 * @return int The ID of the stored action.
86 */
87 public function async_unique( $hook, $args = array(), $group = '', $unique = true ) {
88 $schedule = new ActionScheduler_NullSchedule();
89 $action = new ActionScheduler_Action( $hook, $args, $schedule, $group );
90 return $unique ? $this->store_unique_action( $action, $unique ) : $this->store( $action );
91 }
92
93 /**
94 * Create single action.
95 *
96 * @param string $hook The hook to trigger when this action runs.
97 * @param array $args Args to pass when the hook is triggered.
98 * @param int $when Unix timestamp when the action will run.
99 * @param string $group A group to put the action in.
100 *
101 * @return int The ID of the stored action.
102 */
103 public function single( $hook, $args = array(), $when = null, $group = '' ) {
104 return $this->single_unique( $hook, $args, $when, $group, false );
105 }
106
107 /**
108 * Create single action only if there is no pending or running action with same name and params.
109 *
110 * @param string $hook The hook to trigger when this action runs.
111 * @param array $args Args to pass when the hook is triggered.
112 * @param int $when Unix timestamp when the action will run.
113 * @param string $group A group to put the action in.
114 * @param bool $unique Whether action scheduled should be unique.
115 *
116 * @return int The ID of the stored action.
117 */
118 public function single_unique( $hook, $args = array(), $when = null, $group = '', $unique = true ) {
119 $date = as_get_datetime_object( $when );
120 $schedule = new ActionScheduler_SimpleSchedule( $date );
121 $action = new ActionScheduler_Action( $hook, $args, $schedule, $group );
122 return $unique ? $this->store_unique_action( $action ) : $this->store( $action );
123 }
124
125 /**
126 * Create the first instance of an action recurring on a given interval.
127 *
128 * @param string $hook The hook to trigger when this action runs.
129 * @param array $args Args to pass when the hook is triggered.
130 * @param int $first Unix timestamp for the first run.
131 * @param int $interval Seconds between runs.
132 * @param string $group A group to put the action in.
133 *
134 * @return int The ID of the stored action.
135 */
136 public function recurring( $hook, $args = array(), $first = null, $interval = null, $group = '' ) {
137 return $this->recurring_unique( $hook, $args, $first, $interval, $group, false );
138 }
139
140 /**
141 * 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.
142 *
143 * @param string $hook The hook to trigger when this action runs.
144 * @param array $args Args to pass when the hook is triggered.
145 * @param int $first Unix timestamp for the first run.
146 * @param int $interval Seconds between runs.
147 * @param string $group A group to put the action in.
148 * @param bool $unique Whether action scheduled should be unique.
149 *
150 * @return int The ID of the stored action.
151 */
152 public function recurring_unique( $hook, $args = array(), $first = null, $interval = null, $group = '', $unique = true ) {
153 if ( empty( $interval ) ) {
154 return $this->single_unique( $hook, $args, $first, $group, $unique );
155 }
156 $date = as_get_datetime_object( $first );
157 $schedule = new ActionScheduler_IntervalSchedule( $date, $interval );
158 $action = new ActionScheduler_Action( $hook, $args, $schedule, $group );
159 return $unique ? $this->store_unique_action( $action ) : $this->store( $action );
160 }
161
162 /**
163 * Create the first instance of an action recurring on a Cron schedule.
164 *
165 * @param string $hook The hook to trigger when this action runs.
166 * @param array $args Args to pass when the hook is triggered.
167 * @param int $base_timestamp The first instance of the action will be scheduled
168 * to run at a time calculated after this timestamp matching the cron
169 * expression. This can be used to delay the first instance of the action.
170 * @param int $schedule A cron definition string.
171 * @param string $group A group to put the action in.
172 *
173 * @return int The ID of the stored action.
174 */
175 public function cron( $hook, $args = array(), $base_timestamp = null, $schedule = null, $group = '' ) {
176 return $this->cron_unique( $hook, $args, $base_timestamp, $schedule, $group, false );
177 }
178
179
180 /**
181 * 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.
182 *
183 * @param string $hook The hook to trigger when this action runs.
184 * @param array $args Args to pass when the hook is triggered.
185 * @param int $base_timestamp The first instance of the action will be scheduled
186 * to run at a time calculated after this timestamp matching the cron
187 * expression. This can be used to delay the first instance of the action.
188 * @param int $schedule A cron definition string.
189 * @param string $group A group to put the action in.
190 * @param bool $unique Whether action scheduled should be unique.
191 *
192 * @return int The ID of the stored action.
193 **/
194 public function cron_unique( $hook, $args = array(), $base_timestamp = null, $schedule = null, $group = '', $unique = true ) {
195 if ( empty( $schedule ) ) {
196 return $this->single_unique( $hook, $args, $base_timestamp, $group, $unique );
197 }
198 $date = as_get_datetime_object( $base_timestamp );
199 $cron = CronExpression::factory( $schedule );
200 $schedule = new ActionScheduler_CronSchedule( $date, $cron );
201 $action = new ActionScheduler_Action( $hook, $args, $schedule, $group );
202 return $unique ? $this->store_unique_action( $action ) : $this->store( $action );
203 }
204
205 /**
206 * Create a successive instance of a recurring or cron action.
207 *
208 * Importantly, the action will be rescheduled to run based on the current date/time.
209 * That means when the action is scheduled to run in the past, the next scheduled date
210 * will be pushed forward. For example, if a recurring action set to run every hour
211 * was scheduled to run 5 seconds ago, it will be next scheduled for 1 hour in the
212 * future, which is 1 hour and 5 seconds from when it was last scheduled to run.
213 *
214 * Alternatively, if the action is scheduled to run in the future, and is run early,
215 * likely via manual intervention, then its schedule will change based on the time now.
216 * For example, if a recurring action set to run every day, and is run 12 hours early,
217 * it will run again in 24 hours, not 36 hours.
218 *
219 * This slippage is less of an issue with Cron actions, as the specific run time can
220 * be set for them to run, e.g. 1am each day. In those cases, and entire period would
221 * need to be missed before there was any change is scheduled, e.g. in the case of an
222 * action scheduled for 1am each day, the action would need to run an entire day late.
223 *
224 * @param ActionScheduler_Action $action The existing action.
225 *
226 * @return string The ID of the stored action
227 * @throws InvalidArgumentException If $action is not a recurring action.
228 */
229 public function repeat( $action ) {
230 $schedule = $action->get_schedule();
231 $next = $schedule->get_next( as_get_datetime_object() );
232
233 if ( is_null( $next ) || ! $schedule->is_recurring() ) {
234 throw new InvalidArgumentException( __( 'Invalid action - must be a recurring action.', 'action-scheduler' ) );
235 }
236
237 $schedule_class = get_class( $schedule );
238 $new_schedule = new $schedule( $next, $schedule->get_recurrence(), $schedule->get_first_date() );
239 $new_action = new ActionScheduler_Action( $action->get_hook(), $action->get_args(), $new_schedule, $action->get_group() );
240 $new_action->set_priority( $action->get_priority() );
241 return $this->store( $new_action );
242 }
243
244 /**
245 * Creates a scheduled action.
246 *
247 * This general purpose method can be used in place of specific methods such as async(),
248 * async_unique(), single() or single_unique(), etc.
249 *
250 * @internal Not intended for public use, should not be overriden by subclasses.
251 *
252 * @param array $options {
253 * Describes the action we wish to schedule.
254 *
255 * @type string $type Must be one of 'async', 'cron', 'recurring', or 'single'.
256 * @type string $hook The hook to be executed.
257 * @type array $arguments Arguments to be passed to the callback.
258 * @type string $group The action group.
259 * @type bool $unique If the action should be unique.
260 * @type int $when Timestamp. Indicates when the action, or first instance of the action in the case
261 * of recurring or cron actions, becomes due.
262 * @type int|string $pattern Recurrence pattern. This is either an interval in seconds for recurring actions
263 * or a cron expression for cron actions.
264 * @type int $priority Lower values means higher priority. Should be in the range 0-255.
265 * }
266 *
267 * @return int The action ID. Zero if there was an error scheduling the action.
268 */
269 public function create( array $options = array() ) {
270 $defaults = array(
271 'type' => 'single',
272 'hook' => '',
273 'arguments' => array(),
274 'group' => '',
275 'unique' => false,
276 'when' => time(),
277 'pattern' => null,
278 'priority' => 10,
279 );
280
281 $options = array_merge( $defaults, $options );
282
283 // Cron/recurring actions without a pattern are treated as single actions (this gives calling code the ability
284 // to use functions like as_schedule_recurring_action() to schedule recurring as well as single actions).
285 if ( ( 'cron' === $options['type'] || 'recurring' === $options['type'] ) && empty( $options['pattern'] ) ) {
286 $options['type'] = 'single';
287 }
288
289 switch ( $options['type'] ) {
290 case 'async':
291 $schedule = new ActionScheduler_NullSchedule();
292 break;
293
294 case 'cron':
295 $date = as_get_datetime_object( $options['when'] );
296 $cron = CronExpression::factory( $options['pattern'] );
297 $schedule = new ActionScheduler_CronSchedule( $date, $cron );
298 break;
299
300 case 'recurring':
301 $date = as_get_datetime_object( $options['when'] );
302 $schedule = new ActionScheduler_IntervalSchedule( $date, $options['pattern'] );
303 break;
304
305 case 'single':
306 $date = as_get_datetime_object( $options['when'] );
307 $schedule = new ActionScheduler_SimpleSchedule( $date );
308 break;
309
310 default:
311 error_log( "Unknown action type '{$options['type']}' specified when trying to create an action for '{$options['hook']}'." );
312 return 0;
313 }
314
315 $action = new ActionScheduler_Action( $options['hook'], $options['arguments'], $schedule, $options['group'] );
316 $action->set_priority( $options['priority'] );
317
318 $action_id = 0;
319 try {
320 $action_id = $options['unique'] ? $this->store_unique_action( $action ) : $this->store( $action );
321 } catch ( Exception $e ) {
322 error_log(
323 sprintf(
324 /* translators: %1$s is the name of the hook to be enqueued, %2$s is the exception message. */
325 __( 'Caught exception while enqueuing action "%1$s": %2$s', 'action-scheduler' ),
326 $options['hook'],
327 $e->getMessage()
328 )
329 );
330 }
331 return $action_id;
332 }
333
334 /**
335 * Save action to database.
336 *
337 * @param ActionScheduler_Action $action Action object to save.
338 *
339 * @return int The ID of the stored action
340 */
341 protected function store( ActionScheduler_Action $action ) {
342 $store = ActionScheduler_Store::instance();
343 return $store->save_action( $action );
344 }
345
346 /**
347 * Store action if it's unique.
348 *
349 * @param ActionScheduler_Action $action Action object to store.
350 *
351 * @return int ID of the created action. Will be 0 if action was not created.
352 */
353 protected function store_unique_action( ActionScheduler_Action $action ) {
354 $store = ActionScheduler_Store::instance();
355 return method_exists( $store, 'save_unique_action' ) ?
356 $store->save_unique_action( $action ) : $store->save_action( $action );
357 }
358 }
359