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