PluginProbe
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF / trunk
Imagify Image Optimization: Optimize Images | Compress & Convert to WebP/AVIF vtrunk
2.3.4 2.3.3 2.3.2 2.3.1 2.3.0 2.2.9 2.2.8 trunk 1.10 1.3.3 1.3.4 1.3.5 1.3.5.1 1.3.5.2 1.3.6 1.3.6.1 1.4 1.4.1 1.4.2 1.4.3 1.4.4 1.4.5 1.4.6 1.4.7 1.5 All 103 releases
← All changes | classes/Bulk/WP.php +49 -3 2.3.1 → trunk View file →
@@ -90,8 +90,10 @@
90 90 // Get attachments optimization level.
91 91 'optimization_levels' => '_imagify_optimization_level',
92 92 // Get attachments status.
93 93 'statuses' => '_imagify_status',
94 + // Get attachments metadata, to detect a WP-scaled original.
95 + 'metadata' => '_wp_attachment_metadata',
94 96 ],
95 97 $ids
96 98 );
97 99
@@ -155,9 +157,10 @@
155 157 if ( ! $file_path || ! $this->filesystem->exists( $file_path ) ) {
156 158 continue;
157 159 }
158 160
159 - $attachment_backup_path = get_imagify_attachment_backup_path( $file_path );
161 + $original_path = $this->get_original_file_path_from_metadata( $file_path, isset( $metas['metadata'][ $id ] ) ? $metas['metadata'][ $id ] : null );
162 + $attachment_backup_path = get_imagify_attachment_backup_path( $original_path );
160 163 $attachment_status = isset( $metas['statuses'][ $id ] ) ? $metas['statuses'][ $id ] : false;
161 164 $attachment_optimization_level = isset( $metas['optimization_levels'][ $id ] ) ? $metas['optimization_levels'][ $id ] : false;
162 165
163 166 // Don't try to re-optimize if there is no backup file.
@@ -220,8 +223,10 @@
220 223 $metas = Imagify_DB::get_metas(
221 224 [
222 225 // Get attachments filename.
223 226 'filenames' => '_wp_attached_file',
227 + // Get attachments metadata, to detect a WP-scaled original.
228 + 'metadata' => '_wp_attachment_metadata',
224 229 ],
225 230 $ids
226 231 );
227 232
@@ -238,9 +243,10 @@
238 243 if ( ! $file_path ) {
239 244 continue;
240 245 }
241 246
242 - $attachment_backup_path = get_imagify_attachment_backup_path( $file_path );
247 + $original_path = $this->get_original_file_path_from_metadata( $file_path, isset( $metas['metadata'][ $id ] ) ? $metas['metadata'][ $id ] : null );
248 + $attachment_backup_path = get_imagify_attachment_backup_path( $original_path );
243 249
244 250 if ( ! $this->filesystem->exists( $attachment_backup_path ) ) {
245 251 // No backup, cannot restore.
246 252 continue;
@@ -316,8 +322,9 @@
316 322 WHERE
317 323 p.post_mime_type IN ( $mime_types )
318 324 AND ( mt1.meta_value = 'success' OR mt1.meta_value = 'already_optimized' )
319 325 AND mt2.meta_value NOT LIKE %s
326 + AND mt2.meta_value NOT LIKE %s
320 327 AND p.post_type = 'attachment'
321 328 AND p.post_status IN ( $statuses )
322 329 $nodata_where
323 330 ORDER BY p.ID DESC
@@ -322,8 +329,17 @@
322 329 $nodata_where
323 330 ORDER BY p.ID DESC
324 331 LIMIT 0, %d",
325 332 '%' . $wpdb->esc_like( $nextgen_suffix . '";a:4:{s:7:"success";b:1;' ) . '%',
333 + /**
334 + * Second predicate: skip media the API permanently refused to convert (the next-gen file
335 + * would be heavier than the original, or the file is already compressed). Those are stored
336 + * as `<size><suffix>";a:3:{s:15:"permanent_error";b:1;s:7:"success";b:0;s:5:"error";…`.
337 + * Transient failures (network, quota, timeout) serialize as a 2-element array without the
338 + * `permanent_error` key, so they keep being retried. Matching serialized data with LIKE is
339 + * fragile: the key order written in Optimization\Data\WP must not change.
340 + */
341 + '%' . $wpdb->esc_like( $nextgen_suffix . '";a:3:{s:15:"permanent_error";b:1;' ) . '%',
326 342 imagify_get_unoptimized_attachment_limit()
327 343 )
328 344 );
329 345
@@ -347,8 +363,10 @@
347 363 $metas = Imagify_DB::get_metas(
348 364 [
349 365 // Get attachments filename.
350 366 'filenames' => '_wp_attached_file',
367 + // Get attachments metadata, to detect a WP-scaled original.
368 + 'metadata' => '_wp_attachment_metadata',
351 369 ],
352 370 $ids
353 371 );
354 372
@@ -383,9 +401,10 @@
383 401 if ( strtolower( pathinfo( $file_path, PATHINFO_EXTENSION ) ) === $format ) {
384 402 continue;
385 403 }
386 404
387 - $backup_path = get_imagify_attachment_backup_path( $file_path );
405 + $original_path = $this->get_original_file_path_from_metadata( $file_path, isset( $metas['metadata'][ $id ] ) ? $metas['metadata'][ $id ] : null );
406 + $backup_path = get_imagify_attachment_backup_path( $original_path );
388 407
389 408 if ( ! $this->filesystem->exists( $backup_path ) ) {
390 409 // No backup, no WebP.
391 410 $data['errors']['no_backup'][] = $id;
@@ -395,8 +414,35 @@
395 414 $data['ids'][] = $id;
396 415 }
397 416
398 417 return $data;
418 + }
419 +
420 + /**
421 + * Get the original (pre-scaling) file path from batched `_wp_attachment_metadata`.
422 + *
423 + * When WordPress scales down an image on upload, `_wp_attached_file` (and therefore
424 + * `$file_path`) points to the scaled copy, but Imagify backups are always stored at the
425 + * path derived from the true original file (see `Imagify\Media\WP::get_raw_backup_path()`,
426 + * which relies on `wp_get_original_image_path()`). This mirrors that derivation from data
427 + * already fetched in a single batched query, instead of instantiating a media object (and
428 + * firing its own uncached meta reads) for every attachment in the loop.
429 + *
430 + * @since 2.4
431 + *
432 + * @param string $file_path Path derived from `_wp_attached_file`.
433 + * @param mixed $metadata The unserialized `_wp_attachment_metadata` value for this attachment,
434 + * or null/garbage if it couldn't be fetched or decoded.
435 + *
436 + * @return string The original-derived file path, or $file_path unchanged when there is no
437 + * scaled original to account for.
438 + */
439 + private function get_original_file_path_from_metadata( $file_path, $metadata ) {
440 + if ( ! is_array( $metadata ) || empty( $metadata['original_image'] ) || ! is_string( $metadata['original_image'] ) ) {
441 + return $file_path;
442 + }
443 +
444 + return trailingslashit( dirname( $file_path ) ) . $metadata['original_image'];
399 445 }
400 446
401 447 /**
402 448 * Get the context data.