| 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( esc_html( "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( esc_html( "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 |
public static function create_recurring( int $timestamp, int $interval_in_seconds, string $hook, array $args, string $queue, int $priority = 10, $unique = false ): int { |
| 54 |
self::check_library_is_registered(); |
| 55 |
|
| 56 |
if ( ! in_array( $hook, Async_Operation_Hook::get_values(), true ) ) { |
| 57 |
Logger::log( Logger::LEVEL_ERROR, "Hook $hook is not a part of Async_Operation_Hook values" ); |
| 58 |
|
| 59 |
throw new TypeError( esc_html( "Hook $hook is not a part of Async_Operation_Hook values" ) ); |
| 60 |
} |
| 61 |
|
| 62 |
if ( ! in_array( $queue, Async_Operation_Queue::get_values(), true ) ) { |
| 63 |
Logger::log( Logger::LEVEL_ERROR, "Queue $queue is not a part of Async_Operation_Queue values" ); |
| 64 |
|
| 65 |
throw new TypeError( esc_html( "Queue $queue is not a part of Async_Operation_Queue values" ) ); |
| 66 |
} |
| 67 |
|
| 68 |
return as_schedule_recurring_action( |
| 69 |
$timestamp, |
| 70 |
$interval_in_seconds, |
| 71 |
$hook, |
| 72 |
$args, |
| 73 |
$queue, |
| 74 |
$unique, |
| 75 |
$priority |
| 76 |
); |
| 77 |
} |
| 78 |
|
| 79 |
/** |
| 80 |
* @param Operation_Query_Interface $query |
| 81 |
* |
| 82 |
* @return Async_Operation_Item[]|int[] |
| 83 |
* @throws Async_Operation_Exception |
| 84 |
*/ |
| 85 |
public static function get( Operation_Query_Interface $query ): array { |
| 86 |
self::check_library_is_registered(); |
| 87 |
|
| 88 |
$actions = []; |
| 89 |
|
| 90 |
$store = ActionScheduler::store(); |
| 91 |
$logger = ActionScheduler::logger(); |
| 92 |
|
| 93 |
$action_ids = $store->query_actions( $query->get_query() ); |
| 94 |
|
| 95 |
if ( 'ids' === $query->get_return_type() ) { |
| 96 |
return $action_ids ?? []; |
| 97 |
} |
| 98 |
|
| 99 |
foreach ( $action_ids as $action_id ) { |
| 100 |
try { |
| 101 |
$action = $store->fetch_action( $action_id ); |
| 102 |
} catch ( Throwable $t ) { |
| 103 |
Logger::log( |
| 104 |
Logger::LEVEL_ERROR, |
| 105 |
"Unable to fetch an action `$action_id`. Reason: " . $t->getMessage() |
| 106 |
); |
| 107 |
|
| 108 |
continue; |
| 109 |
} |
| 110 |
|
| 111 |
if ( is_a( $action, 'ActionScheduler_NullAction' ) ) { |
| 112 |
Logger::log( |
| 113 |
Logger::LEVEL_WARN, |
| 114 |
'ActionScheduler_NullAction found' |
| 115 |
); |
| 116 |
|
| 117 |
continue; |
| 118 |
} |
| 119 |
|
| 120 |
$item = new Async_Operation_Item(); |
| 121 |
|
| 122 |
$actions[] = $item->set_id( $action_id ) |
| 123 |
->set_hook( $action->get_hook() ) |
| 124 |
->set_status( $store->get_status( $action_id ) ) |
| 125 |
->set_args( $action->get_args() ) |
| 126 |
->set_queue( $action->get_group() ) |
| 127 |
->set_date( $store->get_date( $action_id ) ) |
| 128 |
->set_logs( $logger->get_logs( $action_id ) ); |
| 129 |
} |
| 130 |
|
| 131 |
return $actions; |
| 132 |
} |
| 133 |
|
| 134 |
public static function get_by_id( int $action_id ): ?Async_Operation_Item { |
| 135 |
self::check_library_is_registered(); |
| 136 |
|
| 137 |
$store = ActionScheduler::store(); |
| 138 |
$logger = ActionScheduler::logger(); |
| 139 |
|
| 140 |
try { |
| 141 |
$action = $store->fetch_action( $action_id ); |
| 142 |
} catch ( Throwable $t ) { |
| 143 |
Logger::log( |
| 144 |
Logger::LEVEL_ERROR, |
| 145 |
"Unable to fetch an action `$action_id`. Reason: " . $t->getMessage() |
| 146 |
); |
| 147 |
|
| 148 |
return null; |
| 149 |
} |
| 150 |
|
| 151 |
if ( is_a( $action, 'ActionScheduler_NullAction' ) ) { |
| 152 |
Logger::log( |
| 153 |
Logger::LEVEL_WARN, |
| 154 |
'ActionScheduler_NullAction found' |
| 155 |
); |
| 156 |
|
| 157 |
return null; |
| 158 |
} |
| 159 |
|
| 160 |
return ( new Async_Operation_Item() ) |
| 161 |
->set_id( $action_id ) |
| 162 |
->set_hook( $action->get_hook() ) |
| 163 |
->set_status( $store->get_status( $action_id ) ) |
| 164 |
->set_args( $action->get_args() ) |
| 165 |
->set_queue( $action->get_group() ) |
| 166 |
->set_date( $store->get_date( $action_id ) ) |
| 167 |
->set_logs( $logger->get_logs( $action_id ) ); |
| 168 |
} |
| 169 |
|
| 170 |
/** |
| 171 |
* @throws Async_Operation_Exception |
| 172 |
*/ |
| 173 |
public static function cancel( int $operation_id ): void { |
| 174 |
self::check_library_is_registered(); |
| 175 |
|
| 176 |
$store = ActionScheduler::store(); |
| 177 |
|
| 178 |
$store->cancel_action( $operation_id ); |
| 179 |
} |
| 180 |
|
| 181 |
/** |
| 182 |
* @throws Async_Operation_Exception |
| 183 |
*/ |
| 184 |
public static function remove( array $operation_ids ): void { |
| 185 |
self::check_library_is_registered(); |
| 186 |
|
| 187 |
$store = ActionScheduler::store(); |
| 188 |
|
| 189 |
foreach ( $operation_ids as $operation_id ) { |
| 190 |
$store->delete_action( $operation_id ); |
| 191 |
} |
| 192 |
} |
| 193 |
|
| 194 |
/** |
| 195 |
* @throws Async_Operation_Exception |
| 196 |
*/ |
| 197 |
private static function check_library_is_registered(): void { |
| 198 |
if ( ! ActionScheduler::is_initialized() ) { |
| 199 |
Logger::log( |
| 200 |
Logger::LEVEL_ERROR, |
| 201 |
'ActionScheduler is not initialised though its method was called' |
| 202 |
); |
| 203 |
|
| 204 |
throw new Async_Operation_Exception(); |
| 205 |
} |
| 206 |
} |
| 207 |
} |
| 208 |
|