| 1 |
<?php |
| 2 |
|
| 3 |
namespace ImageOptimization\Classes\Image; |
| 4 |
|
| 5 |
use WP_Query; |
| 6 |
|
| 7 |
if ( ! defined( 'ABSPATH' ) ) { |
| 8 |
exit; // Exit if accessed directly. |
| 9 |
} |
| 10 |
|
| 11 |
class Image_Query_Builder { |
| 12 |
private array $query; |
| 13 |
|
| 14 |
public function return_images_only_with_backups(): self { |
| 15 |
$this->query['meta_query'][] = [ |
| 16 |
'compare' => 'NOT LIKE', |
| 17 |
'value' => '"backups";a:0', // Serialized empty array of backups |
| 18 |
'key' => Image_Meta::IMAGE_OPTIMIZER_METADATA_KEY, |
| 19 |
]; |
| 20 |
|
| 21 |
return $this; |
| 22 |
} |
| 23 |
|
| 24 |
public function return_not_optimized_images(): self { |
| 25 |
$this->query['meta_query'][] = [ |
| 26 |
'relation' => 'OR', |
| 27 |
[ |
| 28 |
'key' => Image_Meta::IMAGE_OPTIMIZER_METADATA_KEY, |
| 29 |
'compare' => 'NOT EXISTS', |
| 30 |
], |
| 31 |
[ |
| 32 |
'compare' => 'LIKE', |
| 33 |
'value' => '-failed";', |
| 34 |
'key' => Image_Meta::IMAGE_OPTIMIZER_METADATA_KEY, |
| 35 |
], |
| 36 |
[ |
| 37 |
'compare' => 'LIKE', |
| 38 |
'value' => ':"not-optimized";', |
| 39 |
'key' => Image_Meta::IMAGE_OPTIMIZER_METADATA_KEY, |
| 40 |
], |
| 41 |
]; |
| 42 |
|
| 43 |
return $this; |
| 44 |
} |
| 45 |
|
| 46 |
public function return_optimized_images(): self { |
| 47 |
$this->query['meta_query'][] = [ |
| 48 |
'compare' => 'LIKE', |
| 49 |
'value' => '"status";s:9:"optimized"', |
| 50 |
'key' => Image_Meta::IMAGE_OPTIMIZER_METADATA_KEY, |
| 51 |
]; |
| 52 |
|
| 53 |
return $this; |
| 54 |
} |
| 55 |
|
| 56 |
public function return_images_with_non_empty_meta(): self { |
| 57 |
$this->query['meta_query'][] = [ |
| 58 |
'key' => Image_Meta::IMAGE_OPTIMIZER_METADATA_KEY, |
| 59 |
'compare' => 'EXISTS', |
| 60 |
]; |
| 61 |
|
| 62 |
return $this; |
| 63 |
} |
| 64 |
|
| 65 |
public function set_paging_size( int $paging_size ): self { |
| 66 |
$this->query['posts_per_page'] = $paging_size; |
| 67 |
|
| 68 |
return $this; |
| 69 |
} |
| 70 |
|
| 71 |
public function set_current_page( int $current_page ): self { |
| 72 |
$this->query['paged'] = $current_page; |
| 73 |
|
| 74 |
return $this; |
| 75 |
} |
| 76 |
|
| 77 |
public function set_image_ids( array $image_ids ): self { |
| 78 |
$this->query['post__in'] = $image_ids; |
| 79 |
|
| 80 |
return $this; |
| 81 |
} |
| 82 |
|
| 83 |
public function set_mime_types( array $mime_types ): self { |
| 84 |
$this->query['post_mime_type'] = $mime_types; |
| 85 |
|
| 86 |
return $this; |
| 87 |
} |
| 88 |
|
| 89 |
public function execute(): WP_Query { |
| 90 |
return new WP_Query( $this->query ); |
| 91 |
} |
| 92 |
|
| 93 |
public function __construct() { |
| 94 |
$basic_query = [ |
| 95 |
'post_type' => 'attachment', |
| 96 |
'post_mime_type' => Image::get_supported_mime_types(), |
| 97 |
'post_status' => 'any', |
| 98 |
'fields' => 'ids', |
| 99 |
'posts_per_page' => -1, |
| 100 |
'meta_query' => [ |
| 101 |
'relation' => 'AND', |
| 102 |
[ |
| 103 |
'key' => '_wp_attachment_metadata', // Images without this field considered invalid |
| 104 |
'compare' => 'EXISTS', |
| 105 |
], |
| 106 |
], |
| 107 |
]; |
| 108 |
|
| 109 |
$this->query = $basic_query; |
| 110 |
} |
| 111 |
} |
| 112 |
|