image-optimization
/
trunk
/
classes
/
async-operation
/
queries
/
image-optimization-operation-query.php
image-optimization-operation-query.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.6.5, at trunk/classes/async-operation/queries/image-optimization-operation-query.php
| 1 | <?php |
| 2 | |
| 3 | namespace ImageOptimization\Classes\Async_Operation\Queries; |
| 4 | |
| 5 | use ImageOptimization\Classes\Async_Operation\{ |
| 6 | Async_Operation_Hook, |
| 7 | Async_Operation_Queue, |
| 8 | Interfaces\Operation_Query_Interface, |
| 9 | }; |
| 10 | use TypeError; |
| 11 | |
| 12 | if ( ! defined( 'ABSPATH' ) ) { |
| 13 | exit; // Exit if accessed directly. |
| 14 | } |
| 15 | |
| 16 | class Image_Optimization_Operation_Query implements Operation_Query_Interface { |
| 17 | private array $query; |
| 18 | |
| 19 | public function set_hook( string $hook ): self { |
| 20 | if ( ! in_array( $hook, Async_Operation_Hook::get_values(), true ) ) { |
| 21 | throw new TypeError( "Hook $hook is not a part of Async_Operation_Hook values" ); |
| 22 | } |
| 23 | |
| 24 | $this->query['hook'] = $hook; |
| 25 | |
| 26 | return $this; |
| 27 | } |
| 28 | |
| 29 | /** |
| 30 | * @param string|array $status |
| 31 | * @return $this |
| 32 | */ |
| 33 | public function set_status( $status ): self { |
| 34 | $this->query['status'] = $status; |
| 35 | |
| 36 | return $this; |
| 37 | } |
| 38 | |
| 39 | public function set_image_id( int $image_id ): self { |
| 40 | if ( empty( $this->query['args'] ) ) { |
| 41 | $this->query['args'] = []; |
| 42 | } |
| 43 | |
| 44 | $this->query['args']['attachment_id'] = $image_id; |
| 45 | |
| 46 | return $this; |
| 47 | } |
| 48 | |
| 49 | public function set_bulk_operation_id( string $operation_id ): self { |
| 50 | if ( empty( $this->query['args'] ) ) { |
| 51 | $this->query['args'] = []; |
| 52 | } |
| 53 | |
| 54 | $this->query['args']['operation_id'] = $operation_id; |
| 55 | |
| 56 | return $this; |
| 57 | } |
| 58 | |
| 59 | public function set_limit( int $limit ): self { |
| 60 | $this->query['per_page'] = $limit; |
| 61 | |
| 62 | return $this; |
| 63 | } |
| 64 | |
| 65 | public function get_return_type(): string { |
| 66 | return $this->query['_return_type']; |
| 67 | } |
| 68 | |
| 69 | public function return_ids(): self { |
| 70 | $this->query['_return_type'] = 'ids'; |
| 71 | |
| 72 | return $this; |
| 73 | } |
| 74 | |
| 75 | public function get_query(): array { |
| 76 | return $this->query; |
| 77 | } |
| 78 | |
| 79 | public function __construct() { |
| 80 | $this->query = [ |
| 81 | 'group' => Async_Operation_Queue::OPTIMIZE, |
| 82 | 'per_page' => 10, |
| 83 | 'orderby' => 'date', |
| 84 | 'order' => 'DESC', |
| 85 | 'partial_args_matching' => 'json', |
| 86 | '_return_type' => 'object', |
| 87 | ]; |
| 88 | } |
| 89 | } |
| 90 |