PluginProbe
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization / trunk
Optimole – Optimize Images | Convert WebP & AVIF | CDN & Lazy Load | Image Optimization vtrunk
4.2.14 4.2.13 4.2.12 4.2.11 4.2.10 4.2.9 4.2.8 4.2.7 4.2.6 4.2.5 2.5.5 2.5.6 2.5.7 3.0.0 3.0.1 3.1.0 3.1.1 3.1.2 3.1.3 3.10.0 3.11.0 3.11.1 3.11.2 3.11.3 3.12.0 All 135 releases
← All changes | inc/traits/dam_offload_utils.php +110 -29 3.12.0 → trunk View file →
@@ -1,8 +1,20 @@
1 1 <?php
2 2
3 3 trait Optml_Dam_Offload_Utils {
4 + use Optml_Normalizer;
5 +
4 6 /**
7 + * Check if this contains the DAM flag.
8 + *
9 + * @param string $url The URL to check.
10 + *
11 + * @return bool
12 + */
13 + private function is_dam_url( $url ) {
14 + return strpos( $url, Optml_Dam::URL_DAM_FLAG ) !== false;
15 + }
16 + /**
5 17 * Checks that the attachment is a DAM image.
6 18 *
7 19 * @param int $post_id The attachment ID.
8 20 *
@@ -46,9 +58,9 @@
46 58
47 59 /**
48 60 * Get all registered image sizes.
49 61 *
50 - * @return array
62 + * @return array<string, array{width: int, height: int, crop: bool|array<int, string>}>
51 63 */
52 64 private function get_all_image_sizes() {
53 65 $additional_sizes = wp_get_additional_image_sizes();
54 66 $intermediate = get_intermediate_image_sizes();
@@ -84,14 +96,14 @@
84 96 * Get the dimension from optimized url.
85 97 *
86 98 * @param string $url The image url.
87 99 *
88 - * @return array Contains the width and height values in this order.
100 + * @return array{0: string|false, 1: string|false} Contains the width and height values in this order.
89 101 */
90 102 private function parse_dimension_from_optimized_url( $url ) {
91 103 $catch = [];
92 - $height = 'auto';
93 - $width = 'auto';
104 + $height = false;
105 + $width = false;
94 106 preg_match( '/\/w:(.*)\/h:(.*)\/q:/', $url, $catch );
95 107 if ( isset( $catch[1] ) && isset( $catch[2] ) ) {
96 108 $width = $catch[1];
97 109 $height = $catch[2];
@@ -150,15 +162,14 @@
150 162
151 163 /**
152 164 * Used to filter the image metadata. Adds optimized image url for all image sizes.
153 165 *
154 - * @param array $metadata The attachment metadata.
155 - * @param int $id The attachment id.
166 + * @param array<string, mixed> $metadata The attachment metadata.
167 + * @param int $id The attachment id.
156 168 *
157 - * @return mixed
169 + * @return array<string, mixed>
158 170 */
159 171 private function get_altered_metadata_for_remote_images( $metadata, $id ) {
160 - $sizes = $this->get_all_image_sizes();
161 172
162 173 $post = get_post( $id );
163 174
164 175 $sizes_meta = [];
@@ -173,26 +184,16 @@
173 184
174 185 if ( ! isset( $metadata['height'] ) || ! isset( $metadata['width'] ) ) {
175 186 return $metadata;
176 187 }
177 -
188 + $sizes = Optml_App_Replacer::image_sizes();
178 189 foreach ( $sizes as $size => $args ) {
179 190 // check if the image is portrait or landscape using attachment metadata.
180 - $is_portrait = $metadata['height'] > $metadata['width'];
181 -
182 - // proportionally set the width/height based on this if image is uncropped.
183 - if ( ! (bool) $args['crop'] ) {
184 - if ( $is_portrait ) {
185 - $args['width'] = (int) ( $args['height'] * round( $metadata['width'] / $metadata['height'] ) );
186 - } else {
187 - $args['height'] = (int) ( $args['width'] * round( $metadata['height'] / $metadata['width'] ) );
188 - }
189 - }
190 -
191 + $dimensions = $this->size_to_dimension( $size, $metadata );
191 192 $sizes_meta[ $size ] = [
192 193 'file' => $metadata['file'],
193 - 'width' => $args['width'],
194 - 'height' => $args['height'],
194 + 'width' => $dimensions['width'],
195 + 'height' => $dimensions['height'],
195 196 'mime-type' => $post->post_mime_type,
196 197 ];
197 198 }
198 199
@@ -222,24 +223,87 @@
222 223 */
223 224 private function is_scaled_url( $url ) {
224 225 return strpos( $url, '-scaled.' ) !== false;
225 226 }
227 +
226 228 /**
229 + * Check if the image has been offloaded completely.
230 + *
231 + * @param int $id The attachment ID.
232 + *
233 + * @return bool
234 + */
235 + private function is_completed_offload( $id ) {
236 + // This is the flag that is used to mark the image as offloaded at the end.
237 + $completed = ! empty( get_post_meta( $id, Optml_Media_Offload::OM_OFFLOADED_FLAG, true ) );
238 + if ( $completed ) {
239 + return true;
240 + }
241 + // In some rare cases the image is offloaded but the flag is not set so we can alternatively check this using the file path.
242 + $meta = wp_get_attachment_metadata( $id );
243 + if ( ! isset( $meta['file'] ) ) {
244 + return false;
245 + }
246 + if ( Optml_Media_Offload::is_uploaded_image( $meta['file'] ) ) {
247 + return true;
248 + }
249 +
250 + return false;
251 + }
252 + /**
253 + * Get the attachment ID from optimole ID.
254 + *
255 + * @param string $optimole_id The optimole ID.
256 + *
257 + * @return int
258 + */
259 + private function get_attachement_id_from_optimole_id( string $optimole_id ): int {
260 + global $wpdb;
261 +
262 + $id = $wpdb->get_var(
263 + $wpdb->prepare(
264 + "
265 + SELECT post_id
266 + FROM {$wpdb->postmeta}
267 + WHERE meta_key = %s
268 + AND meta_value = %s
269 + LIMIT 1
270 + ",
271 + Optml_Dam::OM_DAM_IMPORTED_FLAG,
272 + $optimole_id
273 + )
274 + );
275 + return empty( $id ) ? 0 : (int) $id;
276 + }
277 + /**
227 278 * Get the attachment ID from URL.
228 279 *
229 - * @param string $url The attachment URL.
280 + * @param string $input_url The attachment URL.
230 281 *
231 282 * @return int
232 283 */
233 - private function attachment_url_to_post_id( $url ) {
234 - $cached = Optml_Attachment_Cache::get_cached_attachment_id( $url );
284 + private function attachment_url_to_post_id( $input_url ) {
285 + $cached = Optml_Attachment_Cache::get_cached_attachment_id( $input_url );
235 286
236 287 if ( $cached !== false ) {
237 - return $cached;
288 + return (int) $cached;
238 289 }
239 290
240 - $url = $this->strip_image_size( $url );
291 + if ( Optml_Media_Offload::is_uploaded_image( $input_url ) ) {
292 + // The DAM are stored as attachments of format /id:<attachment_id>/<original_url>
293 + $pattern = '#/' . Optml_Media_Offload::KEYS['uploaded_flag'] . '([^/]+)#';
294 + if ( preg_match( $pattern, $input_url, $m ) ) {
295 + $attachment_id = $this->get_attachement_id_from_optimole_id( $m[1] );
296 + if ( $attachment_id !== 0 ) {
297 + Optml_Attachment_Cache::set_cached_attachment_id( $input_url, $attachment_id );
241 298
299 + return $attachment_id;
300 + }
301 + }
302 + }
303 +
304 + $url = $this->strip_image_size( $input_url );
305 +
242 306 $attachment_id = attachment_url_to_postid( $url );
243 307
244 308 if ( $attachment_id === 0 && ! $this->is_scaled_url( $url ) ) {
245 309 $scaled_url = $this->get_scaled_url( $url );
@@ -246,10 +310,28 @@
246 310
247 311 $attachment_id = attachment_url_to_postid( $scaled_url );
248 312 }
249 313
250 - Optml_Attachment_Cache::set_cached_attachment_id( $url, $attachment_id );
314 + /*
315 + * TODO: The logic is a mess, we need to refactor at some point.
316 + * Websites may transition between 'www' subdomains and apex domains, potentially breaking references to hosted images. This can cause issues when attempting to match attachment IDs if images are linked using outdated domains. The logic is checking for alternative domains and consider the use of 'scaled' prefixes in image URLs for large images, which might affect ID matching.
317 + */
318 + if ( $attachment_id === 0 ) {
319 + if ( strpos( $url, 'www.' ) !== false ) {
320 + $variant_url = str_replace( 'www.', '', $url );
321 + $attachment_id = attachment_url_to_postid( $variant_url );
322 + } else {
323 + $variant_url = str_replace( '://', '://www.', $url );
324 + $attachment_id = attachment_url_to_postid( $variant_url );
325 + }
326 + if ( $attachment_id === 0 && ! $this->is_scaled_url( $variant_url ) ) {
327 + $scaled_url = $this->get_scaled_url( $variant_url );
251 328
329 + $attachment_id = attachment_url_to_postid( $scaled_url );
330 + }
331 + }
332 + Optml_Attachment_Cache::set_cached_attachment_id( $input_url, $attachment_id );
333 +
252 334 return $attachment_id;
253 335 }
254 336
255 337 /**
@@ -264,7 +346,6 @@
264 346 $url = str_replace( $src_parts[1], '', $url );
265 347 }
266 348
267 349 return $url;
268 -
269 350 }
270 351 }