PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.5.4
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.5.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.5.4, at vendor/woocommerce/action-scheduler/classes/abstracts/ActionScheduler_Store.php

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