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 / classes / async-operation / async-operation.php

async-operation.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.5.4, at classes/async-operation/async-operation.php

146 lines 3.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace ImageOptimization\Classes\Async_Operation;
4
5 use ActionScheduler;
6 use ImageOptimization\Classes\Async_Operation\{
7 Exceptions\Async_Operation_Exception,
8 Interfaces\Operation_Query_Interface
9 };
10 use ImageOptimization\Classes\Logger;
11 use Throwable;
12 use TypeError;
13
14 if ( ! defined( 'ABSPATH' ) ) {
15 exit; // Exit if accessed directly.
16 }
17
18 class Async_Operation {
19 public const OPERATION_STATUS_NOT_STARTED = 'not-started';
20 public const OPERATION_STATUS_COMPLETE = 'complete';
21 public const OPERATION_STATUS_PENDING = 'pending';
22 public const OPERATION_STATUS_RUNNING = 'in-progress';
23 public const OPERATION_STATUS_FAILED = 'failed';
24 public const OPERATION_STATUS_CANCELED = 'canceled';
25
26 /**
27 * @throws Async_Operation_Exception
28 */
29 public static function create( string $hook, array $args, string $queue, int $priority = 10, $unique = false ): int {
30 self::check_library_is_registered();
31
32 if ( ! in_array( $hook, Async_Operation_Hook::get_values(), true ) ) {
33 Logger::log( Logger::LEVEL_ERROR, "Hook $hook is not a part of Async_Operation_Hook values" );
34
35 throw new TypeError( "Hook $hook is not a part of Async_Operation_Hook values" );
36 }
37
38 if ( ! in_array( $queue, Async_Operation_Queue::get_values(), true ) ) {
39 Logger::log( Logger::LEVEL_ERROR, "Queue $queue is not a part of Async_Operation_Queue values" );
40
41 throw new TypeError( "Queue $queue is not a part of Async_Operation_Queue values" );
42 }
43
44 return as_enqueue_async_action(
45 $hook,
46 $args,
47 $queue,
48 $unique,
49 $priority
50 );
51 }
52
53 /**
54 * @param Operation_Query_Interface $query
55 *
56 * @return Async_Operation_Item[]|int[]
57 * @throws Async_Operation_Exception
58 */
59 public static function get( Operation_Query_Interface $query ): array {
60 self::check_library_is_registered();
61
62 $actions = [];
63
64 $store = ActionScheduler::store();
65 $logger = ActionScheduler::logger();
66
67 $action_ids = $store->query_actions( $query->get_query() );
68
69 if ( 'ids' === $query->get_return_type() ) {
70 return $action_ids ?? [];
71 }
72
73 foreach ( $action_ids as $action_id ) {
74 try {
75 $action = $store->fetch_action( $action_id );
76 } catch ( Throwable $t ) {
77 Logger::log(
78 Logger::LEVEL_ERROR,
79 "Unable to fetch an action `$action_id`. Reason: " . $t->getMessage()
80 );
81
82 continue;
83 }
84
85 if ( is_a( $action, 'ActionScheduler_NullAction' ) ) {
86 Logger::log(
87 Logger::LEVEL_WARN,
88 'ActionScheduler_NullAction found'
89 );
90
91 continue;
92 }
93
94 $item = new Async_Operation_Item();
95
96 $actions[] = $item->set_id( $action_id )
97 ->set_hook( $action->get_hook() )
98 ->set_status( $store->get_status( $action_id ) )
99 ->set_args( $action->get_args() )
100 ->set_queue( $action->get_group() )
101 ->set_date( $store->get_date( $action_id ) )
102 ->set_logs( $logger->get_logs( $action_id ) );
103 }
104
105 return $actions;
106 }
107
108 /**
109 * @throws Async_Operation_Exception
110 */
111 public static function cancel( int $operation_id ): void {
112 self::check_library_is_registered();
113
114 $store = ActionScheduler::store();
115
116 $store->cancel_action( $operation_id );
117 }
118
119 /**
120 * @throws Async_Operation_Exception
121 */
122 public static function remove( array $operation_ids ): void {
123 self::check_library_is_registered();
124
125 $store = ActionScheduler::store();
126
127 foreach ( $operation_ids as $operation_id ) {
128 $store->delete_action( $operation_id );
129 }
130 }
131
132 /**
133 * @throws Async_Operation_Exception
134 */
135 private static function check_library_is_registered(): void {
136 if ( ! ActionScheduler::is_initialized() ) {
137 Logger::log(
138 Logger::LEVEL_ERROR,
139 'ActionScheduler is not initialised though its method was called'
140 );
141
142 throw new Async_Operation_Exception();
143 }
144 }
145 }
146