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 +58 -5 2.3.0 → 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;
@@ -286,9 +292,10 @@
286 292 }
287 293 if ( ! isset( $mime ) && empty( $mime ) ) {
288 294 $mime = 'image/webp';
289 295 }
290 - $mime_types = str_replace( ",'" . $mime . "'", '', $mime_types );
296 + $mime = trim( $mime );
297 + $mime_types = str_replace( [ ", '" . $mime . "'", ",'" . $mime . "'" ], '', $mime_types );
291 298 $statuses = Imagify_DB::get_post_statuses();
292 299 $nodata_join = '';
293 300 $nodata_where = '';
294 301 if ( ! imagify_has_attachments_without_required_metadata() ) {
@@ -313,10 +320,11 @@
313 320 LEFT JOIN $wpdb->postmeta AS mt2
314 321 ON ( p.ID = mt2.post_id AND mt2.meta_key = '_imagify_data' )
315 322 WHERE
316 323 p.post_mime_type IN ( $mime_types )
317 - AND (mt1.meta_key IS NULL OR mt1.meta_value = 'success' OR mt1.meta_value = 'already_optimized' )
324 + AND ( mt1.meta_value = 'success' OR mt1.meta_value = 'already_optimized' )
318 325 AND mt2.meta_value NOT LIKE %s
326 + AND mt2.meta_value NOT LIKE %s
319 327 AND p.post_type = 'attachment'
320 328 AND p.post_status IN ( $statuses )
321 329 $nodata_where
322 330 ORDER BY p.ID DESC
@@ -321,8 +329,17 @@
321 329 $nodata_where
322 330 ORDER BY p.ID DESC
323 331 LIMIT 0, %d",
324 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;' ) . '%',
325 342 imagify_get_unoptimized_attachment_limit()
326 343 )
327 344 );
328 345
@@ -346,8 +363,10 @@
346 363 $metas = Imagify_DB::get_metas(
347 364 [
348 365 // Get attachments filename.
349 366 'filenames' => '_wp_attached_file',
367 + // Get attachments metadata, to detect a WP-scaled original.
368 + 'metadata' => '_wp_attachment_metadata',
350 369 ],
351 370 $ids
352 371 );
353 372
@@ -376,10 +395,17 @@
376 395 $data['errors']['no_file_path'][] = $id;
377 396 continue;
378 397 }
379 398
380 - $backup_path = get_imagify_attachment_backup_path( $file_path );
399 + // Skip files whose extension already matches the target format
400 + // (e.g. a .webp file stored with incorrect post_mime_type).
401 + if ( strtolower( pathinfo( $file_path, PATHINFO_EXTENSION ) ) === $format ) {
402 + continue;
403 + }
381 404
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 );
407 +
382 408 if ( ! $this->filesystem->exists( $backup_path ) ) {
383 409 // No backup, no WebP.
384 410 $data['errors']['no_backup'][] = $id;
385 411 continue;
@@ -388,8 +414,35 @@
388 414 $data['ids'][] = $id;
389 415 }
390 416
391 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'];
392 445 }
393 446
394 447 /**
395 448 * Get the context data.