| @@ -1,12 +1,14 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | |
| 3 | -namespace ImageOptimizer\Classes\Async_Operation; | |
| 3 | +namespace ImageOptimization\Classes\Async_Operation; | |
| 4 | 4 | |
| 5 | 5 | use ActionScheduler; |
| 6 | -use ImageOptimizer\Classes\Async_Operation\Exceptions\Async_Operation_Exception; | |
| 7 | -use ImageOptimizer\Classes\Async_Operation\Interfaces\Operation_Query_Interface; | |
| 8 | -use ImageOptimizer\Classes\Logger; | |
| 6 | +use ImageOptimization\Classes\Async_Operation\{ | |
| 7 | + Exceptions\Async_Operation_Exception, | |
| 8 | + Interfaces\Operation_Query_Interface | |
| 9 | +}; | |
| 10 | +use ImageOptimization\Classes\Logger; | |
| 9 | 11 | use Throwable; |
| 10 | 12 | use TypeError; |
| 11 | 13 | |
| 12 | 14 | if ( ! defined( 'ABSPATH' ) ) { |
| @@ -23,21 +25,21 @@ | ||
| 23 | 25 | |
| 24 | 26 | /** |
| 25 | 27 | * @throws Async_Operation_Exception |
| 26 | 28 | */ |
| 27 | - public static function create( string $hook, array $args, string $queue, int $priority = 10 ): int { | |
| 29 | + public static function create( string $hook, array $args, string $queue, int $priority = 10, $unique = false ): int { | |
| 28 | 30 | self::check_library_is_registered(); |
| 29 | 31 | |
| 30 | 32 | if ( ! in_array( $hook, Async_Operation_Hook::get_values(), true ) ) { |
| 31 | 33 | Logger::log( Logger::LEVEL_ERROR, "Hook $hook is not a part of Async_Operation_Hook values" ); |
| 32 | 34 | |
| 33 | - throw new TypeError( "Hook $hook is not a part of Async_Operation_Hook values" ); | |
| 35 | + throw new TypeError( esc_html( "Hook $hook is not a part of Async_Operation_Hook values" ) ); | |
| 34 | 36 | } |
| 35 | 37 | |
| 36 | 38 | if ( ! in_array( $queue, Async_Operation_Queue::get_values(), true ) ) { |
| 37 | 39 | Logger::log( Logger::LEVEL_ERROR, "Queue $queue is not a part of Async_Operation_Queue values" ); |
| 38 | 40 | |
| 39 | - throw new TypeError( "Queue $queue is not a part of Async_Operation_Queue values" ); | |
| 41 | + throw new TypeError( esc_html( "Queue $queue is not a part of Async_Operation_Queue values" ) ); | |
| 40 | 42 | } |
| 41 | 43 | |
| 42 | 44 | return as_enqueue_async_action( |
| 43 | 45 | $hook, |
| @@ -42,13 +44,39 @@ | ||
| 42 | 44 | return as_enqueue_async_action( |
| 43 | 45 | $hook, |
| 44 | 46 | $args, |
| 45 | 47 | $queue, |
| 46 | - false, | |
| 48 | + $unique, | |
| 47 | 49 | $priority |
| 48 | 50 | ); |
| 49 | 51 | } |
| 50 | 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 | + | |
| 51 | 79 | /** |
| 52 | 80 | * @param Operation_Query_Interface $query |
| 53 | 81 | * |
| 54 | 82 | * @return Async_Operation_Item[]|int[] |
| @@ -100,8 +128,44 @@ | ||
| 100 | 128 | ->set_logs( $logger->get_logs( $action_id ) ); |
| 101 | 129 | } |
| 102 | 130 | |
| 103 | 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 ) ); | |
| 104 | 168 | } |
| 105 | 169 | |
| 106 | 170 | /** |
| 107 | 171 | * @throws Async_Operation_Exception |