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