PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.7.4
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.7.4
1.7.7 1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
image-optimization / vendor / woocommerce / action-scheduler / classes / abstracts / ActionScheduler_Store.php

ActionScheduler_Store.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.7.4, at vendor/woocommerce/action-scheduler/classes/abstracts/ActionScheduler_Store.php

507 lines 14.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * Class ActionScheduler_Store
5 *
6 * @codeCoverageIgnore
7 */
8 abstract class ActionScheduler_Store extends ActionScheduler_Store_Deprecated {
9 const STATUS_COMPLETE = 'complete';
10 const STATUS_PENDING = 'pending';
11 const STATUS_RUNNING = 'in-progress';
12 const STATUS_FAILED = 'failed';
13 const STATUS_CANCELED = 'canceled';
14 const DEFAULT_CLASS = 'ActionScheduler_wpPostStore';
15
16 /**
17 * ActionScheduler_Store instance.
18 *
19 * @var ActionScheduler_Store
20 */
21 private static $store = null;
22
23 /**
24 * Maximum length of args.
25 *
26 * @var int
27 */
28 protected static $max_args_length = 191;
29
30 /**
31 * Save action.
32 *
33 * @param ActionScheduler_Action $action Action to save.
34 * @param null|DateTime $scheduled_date Optional Date of the first instance
35 * to store. Otherwise uses the first date of the action's
36 * schedule.
37 *
38 * @return int The action ID
39 */
40 abstract public function save_action( ActionScheduler_Action $action, ?DateTime $scheduled_date = null );
41
42 /**
43 * Get action.
44 *
45 * @param string $action_id Action ID.
46 *
47 * @return ActionScheduler_Action
48 */
49 abstract public function fetch_action( $action_id );
50
51 /**
52 * Find an action.
53 *
54 * Note: the query ordering changes based on the passed 'status' value.
55 *
56 * @param string $hook Action hook.
57 * @param array $params Parameters of the action to find.
58 *
59 * @return string|null ID of the next action matching the criteria or NULL if not found.
60 */
61 public function find_action( $hook, $params = array() ) {
62 $params = wp_parse_args(
63 $params,
64 array(
65 'args' => null,
66 'status' => self::STATUS_PENDING,
67 'group' => '',
68 )
69 );
70
71 // These params are fixed for this method.
72 $params['hook'] = $hook;
73 $params['orderby'] = 'date';
74 $params['per_page'] = 1;
75
76 if ( ! empty( $params['status'] ) ) {
77 if ( self::STATUS_PENDING === $params['status'] ) {
78 $params['order'] = 'ASC'; // Find the next action that matches.
79 } else {
80 $params['order'] = 'DESC'; // Find the most recent action that matches.
81 }
82 }
83
84 $results = $this->query_actions( $params );
85
86 return empty( $results ) ? null : $results[0];
87 }
88
89 /**
90 * Query for action count or list of action IDs.
91 *
92 * @since 3.3.0 $query['status'] accepts array of statuses instead of a single status.
93 *
94 * @param array $query {
95 * Query filtering options.
96 *
97 * @type string $hook The name of the actions. Optional.
98 * @type string|array $status The status or statuses of the actions. Optional.
99 * @type array $args The args array of the actions. Optional.
100 * @type DateTime $date The scheduled date of the action. Used in UTC timezone. Optional.
101 * @type string $date_compare Operator for selecting by $date param. Accepted values are '!=', '>', '>=', '<', '<=', '='. Defaults to '<='.
102 * @type DateTime $modified The last modified date of the action. Used in UTC timezone. Optional.
103 * @type string $modified_compare Operator for comparing $modified param. Accepted values are '!=', '>', '>=', '<', '<=', '='. Defaults to '<='.
104 * @type string $group The group the action belongs to. Optional.
105 * @type bool|int $claimed TRUE to find claimed actions, FALSE to find unclaimed actions, an int to find a specific claim ID. Optional.
106 * @type int $per_page Number of results to return. Defaults to 5.
107 * @type int $offset The query pagination offset. Defaults to 0.
108 * @type int $orderby Accepted values are 'hook', 'group', 'modified', 'date' or 'none'. Defaults to 'date'.
109 * @type string $order Accepted values are 'ASC' or 'DESC'. Defaults to 'ASC'.
110 * }
111 * @param string $query_type Whether to select or count the results. Default, select.
112 *
113 * @return string|array|null The IDs of actions matching the query. Null on failure.
114 */
115 abstract public function query_actions( $query = array(), $query_type = 'select' );
116
117 /**
118 * Run query to get a single action ID.
119 *
120 * @since 3.3.0
121 *
122 * @see ActionScheduler_Store::query_actions for $query arg usage but 'per_page' and 'offset' can't be used.
123 *
124 * @param array $query Query parameters.
125 *
126 * @return int|null
127 */
128 public function query_action( $query ) {
129 $query['per_page'] = 1;
130 $query['offset'] = 0;
131 $results = $this->query_actions( $query );
132
133 if ( empty( $results ) ) {
134 return null;
135 } else {
136 return (int) $results[0];
137 }
138 }
139
140 /**
141 * Get a count of all actions in the store, grouped by status
142 *
143 * @return array
144 */
145 abstract public function action_counts();
146
147 /**
148 * Get additional action counts.
149 *
150 * - add past-due actions
151 *
152 * @return array
153 */
154 public function extra_action_counts() {
155 $extra_actions = array();
156
157 $pastdue_action_counts = (int) $this->query_actions(
158 array(
159 'status' => self::STATUS_PENDING,
160 'date' => as_get_datetime_object(),
161 ),
162 'count'
163 );
164
165 if ( $pastdue_action_counts ) {
166 $extra_actions['past-due'] = $pastdue_action_counts;
167 }
168
169 /**
170 * Allows 3rd party code to add extra action counts (used in filters in the list table).
171 *
172 * @since 3.5.0
173 * @param $extra_actions array Array with format action_count_identifier => action count.
174 */
175 return apply_filters( 'action_scheduler_extra_action_counts', $extra_actions );
176 }
177
178 /**
179 * Cancel action.
180 *
181 * @param string $action_id Action ID.
182 */
183 abstract public function cancel_action( $action_id );
184
185 /**
186 * Delete action.
187 *
188 * @param string $action_id Action ID.
189 */
190 abstract public function delete_action( $action_id );
191
192 /**
193 * Get action's schedule or run timestamp.
194 *
195 * @param string $action_id Action ID.
196 *
197 * @return DateTime The date the action is schedule to run, or the date that it ran.
198 */
199 abstract public function get_date( $action_id );
200
201
202 /**
203 * Make a claim.
204 *
205 * @param int $max_actions Maximum number of actions to claim.
206 * @param DateTime|null $before_date Claim only actions schedule before the given date. Defaults to now.
207 * @param array $hooks Claim only actions with a hook or hooks.
208 * @param string $group Claim only actions in the given group.
209 *
210 * @return ActionScheduler_ActionClaim
211 */
212 abstract public function stake_claim( $max_actions = 10, ?DateTime $before_date = null, $hooks = array(), $group = '' );
213
214 /**
215 * Get claim count.
216 *
217 * @return int
218 */
219 abstract public function get_claim_count();
220
221 /**
222 * Release the claim.
223 *
224 * @param ActionScheduler_ActionClaim $claim Claim object.
225 */
226 abstract public function release_claim( ActionScheduler_ActionClaim $claim );
227
228 /**
229 * Un-claim the action.
230 *
231 * @param string $action_id Action ID.
232 */
233 abstract public function unclaim_action( $action_id );
234
235 /**
236 * Mark action as failed.
237 *
238 * @param string $action_id Action ID.
239 */
240 abstract public function mark_failure( $action_id );
241
242 /**
243 * Log action's execution.
244 *
245 * @param string $action_id Actoin ID.
246 */
247 abstract public function log_execution( $action_id );
248
249 /**
250 * Mark action as complete.
251 *
252 * @param string $action_id Action ID.
253 */
254 abstract public function mark_complete( $action_id );
255
256 /**
257 * Get action's status.
258 *
259 * @param string $action_id Action ID.
260 * @return string
261 */
262 abstract public function get_status( $action_id );
263
264 /**
265 * Get action's claim ID.
266 *
267 * @param string $action_id Action ID.
268 * @return mixed
269 */
270 abstract public function get_claim_id( $action_id );
271
272 /**
273 * Find actions by claim ID.
274 *
275 * @param string $claim_id Claim ID.
276 * @return array
277 */
278 abstract public function find_actions_by_claim_id( $claim_id );
279
280 /**
281 * Validate SQL operator.
282 *
283 * @param string $comparison_operator Operator.
284 * @return string
285 */
286 protected function validate_sql_comparator( $comparison_operator ) {
287 if ( in_array( $comparison_operator, array( '!=', '>', '>=', '<', '<=', '=' ), true ) ) {
288 return $comparison_operator;
289 }
290
291 return '=';
292 }
293
294 /**
295 * Get the time MySQL formatted date/time string for an action's (next) scheduled date.
296 *
297 * @param ActionScheduler_Action $action Action.
298 * @param null|DateTime $scheduled_date Action's schedule date (optional).
299 * @return string
300 */
301 protected function get_scheduled_date_string( ActionScheduler_Action $action, ?DateTime $scheduled_date = null ) {
302 $next = is_null( $scheduled_date ) ? $action->get_schedule()->get_date() : $scheduled_date;
303
304 if ( ! $next ) {
305 $next = date_create();
306 }
307
308 $next->setTimezone( new DateTimeZone( 'UTC' ) );
309
310 return $next->format( 'Y-m-d H:i:s' );
311 }
312
313 /**
314 * Get the time MySQL formatted date/time string for an action's (next) scheduled date.
315 *
316 * @param ActionScheduler_Action|null $action Action.
317 * @param null|DateTime $scheduled_date Action's scheduled date (optional).
318 * @return string
319 */
320 protected function get_scheduled_date_string_local( ActionScheduler_Action $action, ?DateTime $scheduled_date = null ) {
321 $next = is_null( $scheduled_date ) ? $action->get_schedule()->get_date() : $scheduled_date;
322
323 if ( ! $next ) {
324 $next = date_create();
325 }
326
327 ActionScheduler_TimezoneHelper::set_local_timezone( $next );
328 return $next->format( 'Y-m-d H:i:s' );
329 }
330
331 /**
332 * Validate that we could decode action arguments.
333 *
334 * @param mixed $args The decoded arguments.
335 * @param int $action_id The action ID.
336 *
337 * @throws ActionScheduler_InvalidActionException When the decoded arguments are invalid.
338 */
339 protected function validate_args( $args, $action_id ) {
340 // Ensure we have an array of args.
341 if ( ! is_array( $args ) ) {
342 throw ActionScheduler_InvalidActionException::from_decoding_args( $action_id );
343 }
344
345 // Validate JSON decoding if possible.
346 if ( function_exists( 'json_last_error' ) && JSON_ERROR_NONE !== json_last_error() ) {
347 throw ActionScheduler_InvalidActionException::from_decoding_args( $action_id, $args );
348 }
349 }
350
351 /**
352 * Validate a ActionScheduler_Schedule object.
353 *
354 * @param mixed $schedule The unserialized ActionScheduler_Schedule object.
355 * @param int $action_id The action ID.
356 *
357 * @throws ActionScheduler_InvalidActionException When the schedule is invalid.
358 */
359 protected function validate_schedule( $schedule, $action_id ) {
360 if ( empty( $schedule ) || ! is_a( $schedule, 'ActionScheduler_Schedule' ) ) {
361 throw ActionScheduler_InvalidActionException::from_schedule( $action_id, $schedule );
362 }
363 }
364
365 /**
366 * InnoDB indexes have a maximum size of 767 bytes by default, which is only 191 characters with utf8mb4.
367 *
368 * Previously, AS wasn't concerned about args length, as we used the (unindex) post_content column. However,
369 * with custom tables, we use an indexed VARCHAR column instead.
370 *
371 * @param ActionScheduler_Action $action Action to be validated.
372 * @throws InvalidArgumentException When json encoded args is too long.
373 */
374 protected function validate_action( ActionScheduler_Action $action ) {
375 if ( strlen( wp_json_encode( $action->get_args() ) ) > static::$max_args_length ) {
376 // translators: %d is a number (maximum length of action arguments).
377 throw new InvalidArgumentException( sprintf( __( 'ActionScheduler_Action::$args too long. To ensure the args column can be indexed, action args should not be more than %d characters when encoded as JSON.', 'action-scheduler' ), static::$max_args_length ) );
378 }
379 }
380
381 /**
382 * Cancel pending actions by hook.
383 *
384 * @since 3.0.0
385 *
386 * @param string $hook Hook name.
387 *
388 * @return void
389 */
390 public function cancel_actions_by_hook( $hook ) {
391 $action_ids = true;
392 while ( ! empty( $action_ids ) ) {
393 $action_ids = $this->query_actions(
394 array(
395 'hook' => $hook,
396 'status' => self::STATUS_PENDING,
397 'per_page' => 1000,
398 'orderby' => 'none',
399 )
400 );
401
402 $this->bulk_cancel_actions( $action_ids );
403 }
404 }
405
406 /**
407 * Cancel pending actions by group.
408 *
409 * @since 3.0.0
410 *
411 * @param string $group Group slug.
412 *
413 * @return void
414 */
415 public function cancel_actions_by_group( $group ) {
416 $action_ids = true;
417 while ( ! empty( $action_ids ) ) {
418 $action_ids = $this->query_actions(
419 array(
420 'group' => $group,
421 'status' => self::STATUS_PENDING,
422 'per_page' => 1000,
423 'orderby' => 'none',
424 )
425 );
426
427 $this->bulk_cancel_actions( $action_ids );
428 }
429 }
430
431 /**
432 * Cancel a set of action IDs.
433 *
434 * @since 3.0.0
435 *
436 * @param int[] $action_ids List of action IDs.
437 *
438 * @return void
439 */
440 private function bulk_cancel_actions( $action_ids ) {
441 foreach ( $action_ids as $action_id ) {
442 $this->cancel_action( $action_id );
443 }
444
445 do_action( 'action_scheduler_bulk_cancel_actions', $action_ids );
446 }
447
448 /**
449 * Get status labels.
450 *
451 * @return array<string, string>
452 */
453 public function get_status_labels() {
454 return array(
455 self::STATUS_COMPLETE => __( 'Complete', 'action-scheduler' ),
456 self::STATUS_PENDING => __( 'Pending', 'action-scheduler' ),
457 self::STATUS_RUNNING => __( 'In-progress', 'action-scheduler' ),
458 self::STATUS_FAILED => __( 'Failed', 'action-scheduler' ),
459 self::STATUS_CANCELED => __( 'Canceled', 'action-scheduler' ),
460 );
461 }
462
463 /**
464 * Check if there are any pending scheduled actions due to run.
465 *
466 * @return string
467 */
468 public function has_pending_actions_due() {
469 $pending_actions = $this->query_actions(
470 array(
471 'per_page' => 1,
472 'date' => as_get_datetime_object(),
473 'status' => self::STATUS_PENDING,
474 'orderby' => 'none',
475 ),
476 'count'
477 );
478
479 return ! empty( $pending_actions );
480 }
481
482 /**
483 * Callable initialization function optionally overridden in derived classes.
484 */
485 public function init() {}
486
487 /**
488 * Callable function to mark an action as migrated optionally overridden in derived classes.
489 *
490 * @param int $action_id Action ID.
491 */
492 public function mark_migrated( $action_id ) {}
493
494 /**
495 * Get instance.
496 *
497 * @return ActionScheduler_Store
498 */
499 public static function instance() {
500 if ( empty( self::$store ) ) {
501 $class = apply_filters( 'action_scheduler_store_class', self::DEFAULT_CLASS );
502 self::$store = new $class();
503 }
504 return self::$store;
505 }
506 }
507