From 6e05d118ac9c587b2456a86aad25207ca81dd816 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 25 Feb 2026 22:42:05 +0000 Subject: [PATCH 1/5] Initial plan From c1f1fa0dba0f7690c7c4f73d378f680806e884eb Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 25 Feb 2026 22:48:36 +0000 Subject: [PATCH 2/5] Fix: preserve user-edited images during regeneration of big images Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> --- features/media-regenerate.feature | 48 +++++++++++++++++++++++++++++++ src/Media_Command.php | 5 +++- 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/features/media-regenerate.feature b/features/media-regenerate.feature index 181cd9d8..5d06ec4e 100644 --- a/features/media-regenerate.feature +++ b/features/media-regenerate.feature @@ -1686,6 +1686,54 @@ Feature: Regenerate WordPress attachments """ And the return code should be 1 + @require-wp-5.3 + Scenario: Regenerate a large image that was edited by the user preserves the edits + Given download: + | path | url | + | {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg | + And I run `wp option update uploads_use_yearmonth_folders 0` + + When I run `wp media import {CACHE_DIR}/large-image.jpg --title="My imported large attachment" --porcelain` + Then save STDOUT as {ATTACHMENT_ID} + And the wp-content/uploads/large-image.jpg file should exist + And the wp-content/uploads/large-image-scaled.jpg file should exist + + # Simulate a user edit by copying the scaled file as an "edited" version, + # updating the attached file metadata, and setting backup sizes. + When I run `wp eval ' + $id = {ATTACHMENT_ID}; + $meta = wp_get_attachment_metadata( $id ); + $old_file = get_attached_file( $id ); + $edited_file = preg_replace( "/(\.[^.]+)$/", "-e0000000000000$1", $old_file ); + copy( $old_file, $edited_file ); + $edited_relative = _wp_relative_upload_path( $edited_file ); + update_post_meta( $id, "_wp_attached_file", $edited_relative ); + $backup = array(); + foreach ( $meta["sizes"] as $size => $size_data ) { + $backup[ $size . "-orig" ] = $size_data; + } + update_post_meta( $id, "_wp_attachment_backup_sizes", $backup ); + '` + Then the wp-content/uploads/large-image-scaled-e0000000000000.jpg file should exist + + When I run `wp post meta get {ATTACHMENT_ID} _wp_attached_file` + Then STDOUT should contain: + """ + large-image-scaled-e0000000000000.jpg + """ + + When I run `wp media regenerate {ATTACHMENT_ID} --yes` + Then STDOUT should contain: + """ + Regenerated thumbnails for "My imported large attachment" + """ + + When I run `wp post meta get {ATTACHMENT_ID} _wp_attached_file` + Then STDOUT should contain: + """ + large-image-scaled-e0000000000000.jpg + """ + Scenario: Only delete missing image sizes Given download: | path | url | diff --git a/src/Media_Command.php b/src/Media_Command.php index ef4a7d40..1ce9b878 100644 --- a/src/Media_Command.php +++ b/src/Media_Command.php @@ -1351,7 +1351,10 @@ private function calculate_transformation( $orientation ) { * @return string|false Filepath of the attachment, or false if not found. */ private function get_attached_file( $attachment_id ) { - if ( function_exists( 'wp_get_original_image_path' ) ) { + // If the image has been edited by the user, use the edited file (tracked + // via _wp_attachment_backup_sizes) rather than the original pre-scaled image. + $backup_sizes = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true ); + if ( empty( $backup_sizes ) && function_exists( 'wp_get_original_image_path' ) ) { $filepath = wp_get_original_image_path( $attachment_id ); if ( false !== $filepath ) { From d944e4c628cf580095ff236b8de91d1f3a08287e Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Wed, 25 Feb 2026 23:57:37 +0100 Subject: [PATCH 3/5] Update features/media-regenerate.feature --- features/media-regenerate.feature | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/features/media-regenerate.feature b/features/media-regenerate.feature index 5d06ec4e..5b3eb2e9 100644 --- a/features/media-regenerate.feature +++ b/features/media-regenerate.feature @@ -1709,8 +1709,10 @@ Feature: Regenerate WordPress attachments $edited_relative = _wp_relative_upload_path( $edited_file ); update_post_meta( $id, "_wp_attached_file", $edited_relative ); $backup = array(); - foreach ( $meta["sizes"] as $size => $size_data ) { - $backup[ $size . "-orig" ] = $size_data; + if ( ! empty( $meta['sizes'] ) ) { + foreach ( $meta['sizes'] as $size => $size_data ) { + $backup[ $size . '-orig' ] = $size_data; + } } update_post_meta( $id, "_wp_attachment_backup_sizes", $backup ); '` From c61a9f744e40e2245aacf7c66aef2f22f64f0d25 Mon Sep 17 00:00:00 2001 From: "copilot-swe-agent[bot]" <198982749+Copilot@users.noreply.github.com> Date: Wed, 25 Feb 2026 23:32:46 +0000 Subject: [PATCH 4/5] refactor: move inline eval code to a PHP file, use eval-file in test Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> --- features/media-regenerate.feature | 13 ++++++++----- 1 file changed, 8 insertions(+), 5 deletions(-) diff --git a/features/media-regenerate.feature b/features/media-regenerate.feature index 5799b6e5..d8b21a43 100644 --- a/features/media-regenerate.feature +++ b/features/media-regenerate.feature @@ -1737,22 +1737,25 @@ Feature: Regenerate WordPress attachments # Simulate a user edit by copying the scaled file as an "edited" version, # updating the attached file metadata, and setting backup sizes. - When I run `wp eval ' - $id = {ATTACHMENT_ID}; + Given a simulate-edit.php file: + """ + $size_data ) { $backup[ $size . '-orig' ] = $size_data; } } - update_post_meta( $id, "_wp_attachment_backup_sizes", $backup ); - '` + update_post_meta( $id, '_wp_attachment_backup_sizes', $backup ); + """ + When I run `wp eval-file simulate-edit.php {ATTACHMENT_ID}` Then the wp-content/uploads/large-image-scaled-e0000000000000.jpg file should exist When I run `wp post meta get {ATTACHMENT_ID} _wp_attached_file` From 5a49fe705ab74e34bed1841b4d852445f8ee3f15 Mon Sep 17 00:00:00 2001 From: Pascal Birchler Date: Thu, 26 Feb 2026 01:02:32 +0100 Subject: [PATCH 5/5] Update features/media-regenerate.feature Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> --- features/media-regenerate.feature | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/features/media-regenerate.feature b/features/media-regenerate.feature index d8b21a43..46222862 100644 --- a/features/media-regenerate.feature +++ b/features/media-regenerate.feature @@ -1753,6 +1753,14 @@ Feature: Regenerate WordPress attachments $backup[ $size . '-orig' ] = $size_data; } } + if ( empty( $backup ) ) { + $backup['full-orig'] = array( + 'file' => wp_basename( $old_file ), + 'width' => isset( $meta['width'] ) ? $meta['width'] : 0, + 'height' => isset( $meta['height'] ) ? $meta['height'] : 0, + 'mime-type' => get_post_mime_type( $id ), + ); + } update_post_meta( $id, '_wp_attachment_backup_sizes', $backup ); """ When I run `wp eval-file simulate-edit.php {ATTACHMENT_ID}`