Commit 11f844201c7 for woocommerce
commit 11f844201c76433e20a8c897dfc94b4566f609fd
Author: Tung Du <dinhtungdu@gmail.com>
Date: Tue Nov 17 21:49:08 2026 +0700
Keep product image sizes when a site filter skips the regenerated size (#71005)
* Keep image sizes when a site filter skips the regenerated size
When a site filter stops WordPress from making the WooCommerce size,
regeneration saved an empty sizes list and lost every stored size.
Now the stored record is always saved back after regeneration.
Fixes #60577.
Amp-Thread-ID: https://ampcode.com/threads/T-01a1e174-d4cc-75ea-9b42-4b88b324046e
Co-authored-by: Amp <amp@ampcode.com>
* Keep the main file keys in sync when regeneration replaces the image
Regeneration can scale or rotate the main file of an old unscaled image.
Take file, width, height and filesize from the new metadata, and keep a
stored original_image so the uploaded original can still be found.
Amp-Thread-ID: https://ampcode.com/threads/T-01a1e174-d4cc-75ea-9b42-4b88b324046e
Co-authored-by: Amp <amp@ampcode.com>
---------
Co-authored-by: Amp <amp@ampcode.com>
diff --git a/plugins/woocommerce/changelog/60577-keep-image-sizes-when-size-skipped b/plugins/woocommerce/changelog/60577-keep-image-sizes-when-size-skipped
new file mode 100644
index 00000000000..5746980526a
--- /dev/null
+++ b/plugins/woocommerce/changelog/60577-keep-image-sizes-when-size-skipped
@@ -0,0 +1,4 @@
+Significance: patch
+Type: fix
+
+Stop product images from losing their stored sizes on stores where a plugin or theme skips WooCommerce image sizes.
diff --git a/plugins/woocommerce/includes/class-wc-regenerate-images.php b/plugins/woocommerce/includes/class-wc-regenerate-images.php
index c5f822a6080..9b59854a2c4 100644
--- a/plugins/woocommerce/includes/class-wc-regenerate-images.php
+++ b/plugins/woocommerce/includes/class-wc-regenerate-images.php
@@ -451,11 +451,28 @@ class WC_Regenerate_Images {
return $image;
}
+ // wp_generate_attachment_metadata() can also replace the main file with a scaled or rotated copy.
+ // Take these keys from its result so they describe the file that is now attached.
+ foreach ( array( 'file', 'width', 'height', 'filesize' ) as $key ) {
+ if ( isset( $new_metadata[ $key ] ) ) {
+ $metadata[ $key ] = $new_metadata[ $key ];
+ }
+ }
+
+ // Keep a stored original_image, because it names the upload. When WordPress scales the
+ // -scaled copy again, it reports that copy as the original.
+ if ( empty( $metadata['original_image'] ) && isset( $new_metadata['original_image'] ) ) {
+ $metadata['original_image'] = $new_metadata['original_image'];
+ }
+
if ( isset( $new_metadata['sizes'][ self::$regenerate_size ] ) ) {
$metadata['sizes'][ self::$regenerate_size ] = $new_metadata['sizes'][ self::$regenerate_size ];
- wp_update_attachment_metadata( $attachment_id, $metadata );
}
+ // wp_generate_attachment_metadata() first saves a record with no sizes. Always save the stored
+ // record again, even when WordPress made no new size, for example because a site filter skipped it.
+ wp_update_attachment_metadata( $attachment_id, $metadata );
+
// Now we've done our regen, attempt to return the new size.
$new_image = self::unfiltered_image_downsize( $attachment_id, self::$regenerate_size );
diff --git a/plugins/woocommerce/tests/php/includes/class-wc-regenerate-images-test.php b/plugins/woocommerce/tests/php/includes/class-wc-regenerate-images-test.php
index 6d95eb646c3..5d240d521e5 100644
--- a/plugins/woocommerce/tests/php/includes/class-wc-regenerate-images-test.php
+++ b/plugins/woocommerce/tests/php/includes/class-wc-regenerate-images-test.php
@@ -68,4 +68,82 @@ class WC_Regenerate_Images_Test extends WC_Unit_Test_Case {
'Metadata hidden by a wp_get_attachment_metadata filter should survive on-the-fly regeneration'
);
}
+
+ /**
+ * @testdox Rendering an image should keep its stored sizes when a site filter skips the WooCommerce size.
+ */
+ public function test_render_keeps_sizes_when_site_filter_skips_requested_size(): void {
+ $generation_runs = 0;
+
+ // A site that keeps the size registered but never generates it.
+ add_filter(
+ 'intermediate_image_sizes_advanced',
+ function ( $sizes ) use ( &$generation_runs ) {
+ ++$generation_runs;
+ unset( $sizes['woocommerce_thumbnail'] );
+
+ return $sizes;
+ },
+ 20
+ );
+
+ // The image is wide because maybe_resize_image() regenerates only when the aspect ratio does not match.
+ $attachment_id = $this->create_image_attachment( 900, 300, 'wc-regen-skipped-size.jpg' );
+ $stored_sizes = wp_get_attachment_metadata( $attachment_id, true )['sizes'];
+
+ $this->assertNotEmpty( $stored_sizes, 'The upload should generate the sizes the site allows' );
+ $this->assertArrayNotHasKey( 'woocommerce_thumbnail', $stored_sizes, 'The site filter should stop the WooCommerce size at upload' );
+
+ $runs_after_upload = $generation_runs;
+
+ wp_get_attachment_image_src( $attachment_id, 'woocommerce_thumbnail' );
+
+ $this->assertGreaterThan( $runs_after_upload, $generation_runs, 'Rendering should try to regenerate the missing size' );
+ $this->assertSame(
+ $stored_sizes,
+ wp_get_attachment_metadata( $attachment_id, true )['sizes'],
+ 'A regeneration that creates no file should leave the stored sizes as they were'
+ );
+ }
+
+ /**
+ * @testdox Rendering a large unscaled image should keep its sizes and record both the scaled file and the original.
+ */
+ public function test_render_of_unscaled_large_image_keeps_file_keys_in_sync(): void {
+ // A site that keeps the size registered but never generates it.
+ add_filter(
+ 'intermediate_image_sizes_advanced',
+ function ( $sizes ) {
+ unset( $sizes['woocommerce_thumbnail'] );
+
+ return $sizes;
+ },
+ 20
+ );
+
+ // Store the image unscaled, like an upload from before WordPress 5.3 added the big image threshold.
+ add_filter( 'big_image_size_threshold', '__return_false' );
+ $attachment_id = $this->create_image_attachment( 3000, 2000, 'wc-regen-unscaled.jpg' );
+ remove_filter( 'big_image_size_threshold', '__return_false' );
+
+ $original_file = get_attached_file( $attachment_id );
+ $stored_sizes = wp_get_attachment_metadata( $attachment_id, true )['sizes'];
+
+ // Render twice. The second render regenerates from the scaled file, which WordPress does not scale again.
+ wp_get_attachment_image_src( $attachment_id, 'woocommerce_thumbnail' );
+ wp_get_attachment_image_src( $attachment_id, 'woocommerce_thumbnail' );
+
+ $metadata = wp_get_attachment_metadata( $attachment_id, true );
+ $attached_file = get_attached_file( $attachment_id );
+
+ $this->assertNotSame( $original_file, $attached_file, 'WordPress should have scaled the attached file down' );
+ $this->assertSame( basename( $attached_file ), basename( $metadata['file'] ), 'The stored file should be the attached file' );
+ $this->assertSame( array( 2560, 1707 ), array( $metadata['width'], $metadata['height'] ), 'The stored dimensions should be those of the scaled file' );
+ $this->assertSame( basename( $original_file ), $metadata['original_image'] ?? null, 'The stored record should point to the uploaded original' );
+ $this->assertSame( $original_file, wp_get_original_image_path( $attachment_id ), 'The original image path should be the uploaded file' );
+
+ foreach ( array_keys( $stored_sizes ) as $size ) {
+ $this->assertArrayHasKey( $size, $metadata['sizes'], "The $size size should be kept" );
+ }
+ }
}