PluginProbe
Image Optimizer – Compress Images and Convert to WebP or AVIF / 1.7.7
Image Optimizer – Compress Images and Convert to WebP or AVIF v1.7.7
1.7.7 1.7.6 1.7.5 1.7.4 trunk 1.0.0 1.0.1 1.0.2 1.1.0 1.2.0 1.2.1 1.3.0 1.4.0 1.4.1 1.5.0 1.5.1 1.5.2 1.5.3 1.5.4 1.6.0 1.6.1 1.6.2 1.6.3 1.6.4 1.6.5 All 33 releases
image-optimization / classes / image / image-query-builder.php

image-query-builder.php in Image Optimizer – Compress Images and Convert to WebP or AVIF 1.7.7, at classes/image/image-query-builder.php

138 lines 3.0 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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_optimization_in_progress_images(): self {
47 $this->query['meta_query'][] = [
48 'relation' => 'AND',
49 [
50 'key' => '_wp_attachment_metadata',
51 'compare' => 'EXISTS',
52 ],
53 [
54 'relation' => 'OR',
55 [
56 'compare' => 'LIKE',
57 'value' => 'optimization-in-progress";',
58 'key' => 'image_optimizer_metadata',
59 ],
60 [
61 'compare' => 'LIKE',
62 'value' => 'reoptimizing-in-progress";',
63 'key' => 'image_optimizer_metadata',
64 ],
65 ],
66 ];
67
68 return $this;
69 }
70
71 public function return_optimized_images(): self {
72 $this->query['meta_query'][] = [
73 'compare' => 'LIKE',
74 'value' => '"status";s:9:"optimized"',
75 'key' => Image_Meta::IMAGE_OPTIMIZER_METADATA_KEY,
76 ];
77
78 return $this;
79 }
80
81 public function return_images_with_non_empty_meta(): self {
82 $this->query['meta_query'][] = [
83 'key' => Image_Meta::IMAGE_OPTIMIZER_METADATA_KEY,
84 'compare' => 'EXISTS',
85 ];
86
87 return $this;
88 }
89
90 public function set_paging_size( int $paging_size ): self {
91 $this->query['posts_per_page'] = $paging_size;
92
93 return $this;
94 }
95
96 public function set_current_page( int $current_page ): self {
97 $this->query['paged'] = $current_page;
98
99 return $this;
100 }
101
102 public function set_image_ids( array $image_ids ): self {
103 // Passing an empty array to post__in will return the last posts instead of an empty result.
104 $this->query['post__in'] = count( $image_ids ) ? $image_ids : [ 0 ];
105
106 return $this;
107 }
108
109 public function set_mime_types( array $mime_types ): self {
110 $this->query['post_mime_type'] = $mime_types;
111
112 return $this;
113 }
114
115 public function execute(): WP_Query {
116 return new WP_Query( $this->query );
117 }
118
119 public function __construct() {
120 $basic_query = [
121 'post_type' => 'attachment',
122 'post_mime_type' => Image::get_supported_mime_types(),
123 'post_status' => 'any',
124 'fields' => 'ids',
125 'posts_per_page' => -1,
126 'meta_query' => [
127 'relation' => 'AND',
128 [
129 'key' => '_wp_attachment_metadata', // Images without this field considered invalid
130 'compare' => 'EXISTS',
131 ],
132 ],
133 ];
134
135 $this->query = $basic_query;
136 }
137 }
138