From 0e12c136c5fcd24b5e9dbf01bc416aee4a3482d2 Mon Sep 17 00:00:00 2001 From: Wojtek Naruniec Date: Thu, 15 Dec 2022 10:12:10 +0100 Subject: [PATCH 1/4] Fix tests to account for current core behavior --- features/media-regenerate.feature | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/features/media-regenerate.feature b/features/media-regenerate.feature index 51d4bd3b..d9c0c933 100644 --- a/features/media-regenerate.feature +++ b/features/media-regenerate.feature @@ -40,7 +40,7 @@ Feature: Regenerate WordPress attachments When I run `wp media import {CACHE_DIR}/white-150-square.jpg --title="My imported small attachment" --porcelain` Then save STDOUT as {SMALL_ATTACHMENT_ID} And the wp-content/uploads/white-150-square.jpg file should exist - And the wp-content/uploads/white-150-square-150x150.jpg file should exist + And the wp-content/uploads/white-150-square-150x150.jpg file should not exist And the wp-content/uploads/white-150-square-300x300.jpg file should not exist And the wp-content/uploads/white-150-square-1024x1024.jpg file should not exist @@ -76,7 +76,7 @@ Feature: Regenerate WordPress attachments And the wp-content/uploads/canola-300x225.jpg file should exist And the wp-content/uploads/canola-1024x768.jpg file should not exist And the wp-content/uploads/white-150-square.jpg file should exist - And the wp-content/uploads/white-150-square-150x150.jpg file should exist + And the wp-content/uploads/white-150-square-150x150.jpg file should not exist And the wp-content/uploads/white-150-square-300x300.jpg file should not exist And the wp-content/uploads/white-150-square-1024x1024.jpg file should not exist From 24a3c2bcf5c1199ef7843da5be3d3e89c0e1709a Mon Sep 17 00:00:00 2001 From: Wojtek Naruniec Date: Thu, 15 Dec 2022 12:16:47 +0100 Subject: [PATCH 2/4] Fix regenerate command to not delete other image sizes from meta --- features/media-regenerate.feature | 19 +++++++++++++++++++ src/Media_Command.php | 7 ++++--- 2 files changed, 23 insertions(+), 3 deletions(-) diff --git a/features/media-regenerate.feature b/features/media-regenerate.feature index d9c0c933..a8e1d0d5 100644 --- a/features/media-regenerate.feature +++ b/features/media-regenerate.feature @@ -30,6 +30,25 @@ Feature: Regenerate WordPress attachments And the wp-content/uploads/large-image-1024x768.jpg file should exist And the wp-content/uploads/large-image-2048x1536.jpg file should exist + When I run `wp option update medium_size_w 256` + And I run `wp option update medium_size_h 256` + And I run `wp media regenerate {LARGE_ATTACHMENT_ID} --image_size=medium --skip-delete --only-missing` + And I run `wp post meta get {LARGE_ATTACHMENT_ID} _wp_attachment_metadata` + Then STDOUT should contain: + """ + 'medium' + """ + And STDOUT should contain: + """ + 'large' + """ + And STDOUT should contain: + """ + 'thumbnail' + """ + And I run `wp option update medium_size_w 300` + And I run `wp option update medium_size_h 300` + When I run `wp media import {CACHE_DIR}/canola.jpg --title="My imported medium attachment" --porcelain` Then save STDOUT as {MEDIUM_ATTACHMENT_ID} And the wp-content/uploads/canola.jpg file should exist diff --git a/src/Media_Command.php b/src/Media_Command.php index 17f3c11a..80f405c8 100644 --- a/src/Media_Command.php +++ b/src/Media_Command.php @@ -580,6 +580,8 @@ private function process_regeneration( $id, $skip_delete, $only_missing, $image_ $is_pdf = 'application/pdf' === get_post_mime_type( $id ); + $original_meta = wp_get_attachment_metadata( $id ); + $needs_regeneration = $this->needs_regeneration( $id, $fullsizepath, $is_pdf, $image_size, $skip_delete, $skip_it ); if ( $skip_it ) { @@ -619,7 +621,7 @@ private function process_regeneration( $id, $skip_delete, $only_missing, $image_ } if ( $image_size ) { - if ( $this->update_attachment_metadata_for_image_size( $id, $metadata, $image_size ) ) { + if ( $this->update_attachment_metadata_for_image_size( $id, $metadata, $image_size, $original_meta ) ) { WP_CLI::log( "$progress Regenerated $thumbnail_desc for $att_desc." ); } else { WP_CLI::log( "$progress No $thumbnail_desc regeneration needed for $att_desc." ); @@ -886,8 +888,7 @@ private function remove_image_size_filters( $image_size_filters ) { } // Update attachment sizes metadata just for a particular intermediate image size. - private function update_attachment_metadata_for_image_size( $id, $new_metadata, $image_size ) { - $metadata = wp_get_attachment_metadata( $id ); + private function update_attachment_metadata_for_image_size( $id, $new_metadata, $image_size, $metadata ) { if ( ! is_array( $metadata ) ) { return false; From 8bf206e765193a39a523d1652320b824a3286245 Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Fri, 16 Dec 2022 05:30:10 -0800 Subject: [PATCH 3/4] Revert unrelated test changes --- features/media-regenerate.feature | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/features/media-regenerate.feature b/features/media-regenerate.feature index a8e1d0d5..108bc37d 100644 --- a/features/media-regenerate.feature +++ b/features/media-regenerate.feature @@ -59,7 +59,7 @@ Feature: Regenerate WordPress attachments When I run `wp media import {CACHE_DIR}/white-150-square.jpg --title="My imported small attachment" --porcelain` Then save STDOUT as {SMALL_ATTACHMENT_ID} And the wp-content/uploads/white-150-square.jpg file should exist - And the wp-content/uploads/white-150-square-150x150.jpg file should not exist + And the wp-content/uploads/white-150-square-150x150.jpg file should exist And the wp-content/uploads/white-150-square-300x300.jpg file should not exist And the wp-content/uploads/white-150-square-1024x1024.jpg file should not exist @@ -95,7 +95,7 @@ Feature: Regenerate WordPress attachments And the wp-content/uploads/canola-300x225.jpg file should exist And the wp-content/uploads/canola-1024x768.jpg file should not exist And the wp-content/uploads/white-150-square.jpg file should exist - And the wp-content/uploads/white-150-square-150x150.jpg file should not exist + And the wp-content/uploads/white-150-square-150x150.jpg file should exist And the wp-content/uploads/white-150-square-300x300.jpg file should not exist And the wp-content/uploads/white-150-square-1024x1024.jpg file should not exist From b2432370cb8463acbc40b2658f663dc0e898caec Mon Sep 17 00:00:00 2001 From: Daniel Bachhuber Date: Fri, 16 Dec 2022 05:30:35 -0800 Subject: [PATCH 4/4] Move tests to a more sensible location --- features/media-regenerate.feature | 52 ++++++++++++++++++++----------- 1 file changed, 33 insertions(+), 19 deletions(-) diff --git a/features/media-regenerate.feature b/features/media-regenerate.feature index 108bc37d..5731fa76 100644 --- a/features/media-regenerate.feature +++ b/features/media-regenerate.feature @@ -30,25 +30,6 @@ Feature: Regenerate WordPress attachments And the wp-content/uploads/large-image-1024x768.jpg file should exist And the wp-content/uploads/large-image-2048x1536.jpg file should exist - When I run `wp option update medium_size_w 256` - And I run `wp option update medium_size_h 256` - And I run `wp media regenerate {LARGE_ATTACHMENT_ID} --image_size=medium --skip-delete --only-missing` - And I run `wp post meta get {LARGE_ATTACHMENT_ID} _wp_attachment_metadata` - Then STDOUT should contain: - """ - 'medium' - """ - And STDOUT should contain: - """ - 'large' - """ - And STDOUT should contain: - """ - 'thumbnail' - """ - And I run `wp option update medium_size_w 300` - And I run `wp option update medium_size_h 300` - When I run `wp media import {CACHE_DIR}/canola.jpg --title="My imported medium attachment" --porcelain` Then save STDOUT as {MEDIUM_ATTACHMENT_ID} And the wp-content/uploads/canola.jpg file should exist @@ -695,6 +676,17 @@ Feature: Regenerate WordPress attachments "file":"canola-400x400.jpg" """ + # Check remaining metadata is present + When I run `wp post meta get {ATTACHMENT_ID} _wp_attachment_metadata` + Then STDOUT should contain: + """ + 'medium' + """ + And STDOUT should contain: + """ + 'thumbnail' + """ + # Regenerate "medium" image size removed above - should be regenerated. When I run `wp media regenerate --image_size=medium --only-missing --yes` Then STDOUT should contain: @@ -718,6 +710,17 @@ Feature: Regenerate WordPress attachments "file":"canola-300x225.jpg" """ + # Check remaining metadata is present + When I run `wp post meta get {ATTACHMENT_ID} _wp_attachment_metadata` + Then STDOUT should contain: + """ + 'medium' + """ + And STDOUT should contain: + """ + 'thumbnail' + """ + # Regenerate "medium" image size whether missing or not - should be regenerated. When I run `wp media regenerate --image_size=medium --yes` Then STDOUT should contain: @@ -734,6 +737,17 @@ Feature: Regenerate WordPress attachments """ And the wp-content/uploads/canola-300x225.jpg file should exist + # Check remaining metadata is present + When I run `wp post meta get {ATTACHMENT_ID} _wp_attachment_metadata` + Then STDOUT should contain: + """ + 'medium' + """ + And STDOUT should contain: + """ + 'thumbnail' + """ + # Change "test1" image size. Given a wp-content/mu-plugins/media-settings.php file: """