Skip to content

Commit 561e577

Browse files
CopilotswissspidyCopilot
authored
Fix thumbnail regeneration reverting user-edited big images to pre-edit state (#225)
Co-authored-by: swissspidy <841956+swissspidy@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com> Co-authored-by: copilot-swe-agent[bot] <198982749+Copilot@users.noreply.github.com> Co-authored-by: Pascal Birchler <pascal.birchler@gmail.com>
1 parent 1e8e0f6 commit 561e577

2 files changed

Lines changed: 65 additions & 1 deletion

File tree

features/media-regenerate.feature

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1723,6 +1723,67 @@ Feature: Regenerate WordPress attachments
17231723
"""
17241724
And the return code should be 1
17251725

1726+
@require-wp-5.3
1727+
Scenario: Regenerate a large image that was edited by the user preserves the edits
1728+
Given download:
1729+
| path | url |
1730+
| {CACHE_DIR}/large-image.jpg | http://wp-cli.org/behat-data/large-image.jpg |
1731+
And I run `wp option update uploads_use_yearmonth_folders 0`
1732+
1733+
When I run `wp media import {CACHE_DIR}/large-image.jpg --title="My imported large attachment" --porcelain`
1734+
Then save STDOUT as {ATTACHMENT_ID}
1735+
And the wp-content/uploads/large-image.jpg file should exist
1736+
And the wp-content/uploads/large-image-scaled.jpg file should exist
1737+
1738+
# Simulate a user edit by copying the scaled file as an "edited" version,
1739+
# updating the attached file metadata, and setting backup sizes.
1740+
Given a simulate-edit.php file:
1741+
"""
1742+
<?php
1743+
$id = (int) $args[0];
1744+
$meta = wp_get_attachment_metadata( $id );
1745+
$old_file = get_attached_file( $id );
1746+
$edited_file = preg_replace( "/(\.[^.]+)$/", "-e0000000000000$1", $old_file );
1747+
copy( $old_file, $edited_file );
1748+
$edited_relative = _wp_relative_upload_path( $edited_file );
1749+
update_post_meta( $id, '_wp_attached_file', $edited_relative );
1750+
$backup = array();
1751+
if ( ! empty( $meta['sizes'] ) ) {
1752+
foreach ( $meta['sizes'] as $size => $size_data ) {
1753+
$backup[ $size . '-orig' ] = $size_data;
1754+
}
1755+
}
1756+
if ( empty( $backup ) ) {
1757+
$backup['full-orig'] = array(
1758+
'file' => wp_basename( $old_file ),
1759+
'width' => isset( $meta['width'] ) ? $meta['width'] : 0,
1760+
'height' => isset( $meta['height'] ) ? $meta['height'] : 0,
1761+
'mime-type' => get_post_mime_type( $id ),
1762+
);
1763+
}
1764+
update_post_meta( $id, '_wp_attachment_backup_sizes', $backup );
1765+
"""
1766+
When I run `wp eval-file simulate-edit.php {ATTACHMENT_ID}`
1767+
Then the wp-content/uploads/large-image-scaled-e0000000000000.jpg file should exist
1768+
1769+
When I run `wp post meta get {ATTACHMENT_ID} _wp_attached_file`
1770+
Then STDOUT should contain:
1771+
"""
1772+
large-image-scaled-e0000000000000.jpg
1773+
"""
1774+
1775+
When I run `wp media regenerate {ATTACHMENT_ID} --yes`
1776+
Then STDOUT should contain:
1777+
"""
1778+
Regenerated thumbnails for "My imported large attachment"
1779+
"""
1780+
1781+
When I run `wp post meta get {ATTACHMENT_ID} _wp_attached_file`
1782+
Then STDOUT should contain:
1783+
"""
1784+
large-image-scaled-e0000000000000.jpg
1785+
"""
1786+
17261787
Scenario: Only delete missing image sizes
17271788
Given download:
17281789
| path | url |

src/Media_Command.php

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1390,7 +1390,10 @@ private function calculate_transformation( $orientation ) {
13901390
* @return string|false Filepath of the attachment, or false if not found.
13911391
*/
13921392
private function get_attached_file( $attachment_id ) {
1393-
if ( function_exists( 'wp_get_original_image_path' ) ) {
1393+
// If the image has been edited by the user, use the edited file (tracked
1394+
// via _wp_attachment_backup_sizes) rather than the original pre-scaled image.
1395+
$backup_sizes = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true );
1396+
if ( empty( $backup_sizes ) && function_exists( 'wp_get_original_image_path' ) ) {
13941397
$filepath = wp_get_original_image_path( $attachment_id );
13951398

13961399
if ( false !== $filepath ) {

0 commit comments

Comments
 (0)