| 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 ): 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 |
false, |
| 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 |
|