Skip to content

Commit 4ac0a91

Browse files
committed
Use the attached image on subsequent image edits
When an edit takes place make sure that the source to create the image is the attached image and not the original image if present. Due every edit uses the attached version of the media library if attached and original image are different.
1 parent b32ee73 commit 4ac0a91

File tree

2 files changed

+81
-17
lines changed

2 files changed

+81
-17
lines changed

modules/images/webp-uploads/load.php

Lines changed: 27 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -38,8 +38,9 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id,
3838
return $metadata;
3939
}
4040

41-
$is_update = 'update' === $context;
42-
$target = isset( $_REQUEST['target'] ) ? $_REQUEST['target'] : 'all';
41+
$is_update = 'update' === $context;
42+
$target = isset( $_REQUEST['target'] ) ? $_REQUEST['target'] : 'all';
43+
$attached_file = get_attached_file( $attachment_id, true );
4344
if ( $is_update ) {
4445
if ( empty( $metadata['file'] ) ) {
4546
return $metadata;
@@ -51,7 +52,7 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id,
5152
return $metadata;
5253
}
5354
} else {
54-
$file = get_attached_file( $attachment_id, true );
55+
$file = $attached_file;
5556
}
5657

5758
// File does not exist and we are not editing only the thumbnail.
@@ -125,11 +126,16 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id,
125126
}
126127

127128
foreach ( $metadata['sizes'] as $size_name => $properties ) {
128-
129+
// WHen only the thumbnail is selected to be edited any other size is dismissed.
129130
if ( 'thumbnail' === $target && 'thumbnail' !== $size_name ) {
130131
continue;
131132
}
132133

134+
// When all the sizes except the thumbnail are edited the thumbnail is dismissed.
135+
if ( 'nothumb' === $target && 'thumbnail' === $size_name ) {
136+
continue;
137+
}
138+
133139
// This image size is not defined or not an array.
134140
if ( ! is_array( $properties ) ) {
135141
continue;
@@ -177,19 +183,23 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id,
177183
continue;
178184
}
179185

180-
if ( 'update' === $context && 'thumbnail' === $target ) {
181-
/**
182-
* When only the thumbnail requires additional image, make sure that the base image to create additional
183-
* mime types is the thumbnail with the original mime type due this image is the only one that was modified
184-
* using the attached image or original image would be. The filename should match the original image with
185-
* the only difference of the extension on the filename instead, so the new created image does not have multiple
186-
* suffix like filename-150x150-150x150.webp and instead matches filename-150x150.webp
187-
*/
188-
$original_extension = explode( '|', $allowed_mimes[ $current_mime ] );
189-
$target_extension = explode( '|', $allowed_mimes[ $mime ] );
190-
$file_path = path_join( $original_directory, $properties['file'] );
191-
$destination = preg_replace( "/\.{$original_extension[0]}$/", ".{$target_extension[0]}", $file_path );
192-
$source = webp_uploads_generate_image_size( $attachment_id, $size_name, $mime, $file_path, $destination );
186+
if ( 'update' === $context ) {
187+
if ( 'thumbnail' === $target ) {
188+
/**
189+
* When only the thumbnail requires additional image, make sure that the base image to create additional
190+
* mime types is the thumbnail with the original mime type due this image is the only one that was modified
191+
* using the attached image or original image would be. The filename should match the original image with
192+
* the only difference of the extension on the filename instead, so the new created image does not have multiple
193+
* suffix like filename-150x150-150x150.webp and instead matches filename-150x150.webp
194+
*/
195+
$original_extension = explode( '|', $allowed_mimes[ $current_mime ] );
196+
$target_extension = explode( '|', $allowed_mimes[ $mime ] );
197+
$file_path = path_join( $original_directory, $properties['file'] );
198+
$destination = preg_replace( "/\.{$original_extension[0]}$/", ".{$target_extension[0]}", $file_path );
199+
$source = webp_uploads_generate_image_size( $attachment_id, $size_name, $mime, $file_path, $destination );
200+
} else {
201+
$source = webp_uploads_generate_image_size( $attachment_id, $size_name, $mime, $attached_file );
202+
}
193203
} else {
194204
$source = webp_uploads_generate_image_size( $attachment_id, $size_name, $mime );
195205
}

tests/modules/images/webp-uploads/webp-uploads-test.php

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1021,6 +1021,60 @@ public function it_should_backup_the_image_when_all_images_except_the_thumbnail_
10211021
}
10221022
}
10231023

1024+
/**
1025+
* Use the attached image when updating subsequent images not the original version
1026+
*
1027+
* @test
1028+
*/
1029+
public function it_should_use_the_attached_image_when_updating_subsequent_images_not_the_original_version() {
1030+
// The leafs image is 1080 pixels wide with this filter we ensure a -scaled version is created for this test.
1031+
add_filter(
1032+
'big_image_size_threshold',
1033+
function () {
1034+
// Due to the largest image size is 1024 and the image is 1080x720, 1050 is a good spot to create a scaled size for all images sizes.
1035+
return 1050;
1036+
}
1037+
);
1038+
1039+
$attachment_id = $this->factory->attachment->create_upload_object(
1040+
TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg'
1041+
);
1042+
1043+
$this->assertNotSame( wp_get_original_image_path( $attachment_id ), get_attached_file( $attachment_id ) );
1044+
1045+
$editor = new WP_Image_Edit( $attachment_id );
1046+
$editor->flip_right()->all_except_thumbnail()->save();
1047+
1048+
$this->assertTrue( $editor->success() );
1049+
1050+
$metadata = wp_get_attachment_metadata( $attachment_id );
1051+
1052+
$this->assertArrayHasKey( 'original_image', $metadata );
1053+
$this->assertArrayHasKey( 'sources', $metadata );
1054+
$this->assertImageHasSource( $attachment_id, 'image/jpeg' );
1055+
$this->assertImageHasSource( $attachment_id, 'image/webp' );
1056+
1057+
foreach ( $metadata['sources'] as $properties ) {
1058+
$this->assertFileNameIsEdited( $properties['file'] );
1059+
$this->assertStringContainsString( '-scaled-', $properties['file'] );
1060+
}
1061+
1062+
$this->assertArrayHasKey( 'sizes', $metadata );
1063+
foreach ( $metadata['sizes'] as $size_name => $properties ) {
1064+
$this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/jpeg' );
1065+
$this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/webp' );
1066+
foreach ( $properties['sources'] as $mime_type => $values ) {
1067+
if ( 'thumbnail' === $size_name ) {
1068+
$this->assertFileNameIsNotEdited( $values['file'], "'{$size_name}' is not valid." );
1069+
$this->assertStringNotContainsString( '-scaled-', $values['file'] );
1070+
} else {
1071+
$this->assertFileNameIsEdited( $values['file'], "'{$size_name}' is not valid." );
1072+
$this->assertStringContainsString( '-scaled-', $values['file'] );
1073+
}
1074+
}
1075+
}
1076+
}
1077+
10241078
/**
10251079
* Update source attributes when webp is edited.
10261080
*

0 commit comments

Comments
 (0)