Skip to content

Commit b32ee73

Browse files
committed
Introduce support for target thumbnailo
when the only modified image size is the `thumbnail` make sure the changes are applied appropiately to the specifeid image size only with the file from that particular image size.
1 parent 6d30ccb commit b32ee73

File tree

3 files changed

+100
-30
lines changed

3 files changed

+100
-30
lines changed

‎modules/images/webp-uploads/load.php‎

Lines changed: 64 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id,
3939
}
4040

4141
$is_update = 'update' === $context;
42+
$target = isset( $_REQUEST['target'] ) ? $_REQUEST['target'] : 'all';
4243
if ( $is_update ) {
4344
if ( empty( $metadata['file'] ) ) {
4445
return $metadata;
@@ -53,8 +54,8 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id,
5354
$file = get_attached_file( $attachment_id, true );
5455
}
5556

56-
// File does not exist.
57-
if ( ! file_exists( $file ) ) {
57+
// File does not exist and we are not editing only the thumbnail.
58+
if ( 'thumbnail' !== $target && ! file_exists( $file ) ) {
5859
return $metadata;
5960
}
6061

@@ -68,7 +69,7 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id,
6869
}
6970

7071
if ( in_array( $mime_type, $valid_mime_transforms[ $mime_type ], true ) ) {
71-
if ( empty( $metadata['sources'][ $mime_type ] ) || $is_update ) {
72+
if ( empty( $metadata['sources'][ $mime_type ] ) || ( $is_update && 'thumbnail' !== $target ) ) {
7273
$metadata['sources'][ $mime_type ] = array(
7374
'file' => wp_basename( $file ),
7475
'filesize' => filesize( $file ),
@@ -87,28 +88,30 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id,
8788
$filename = pathinfo( $file, PATHINFO_FILENAME );
8889
$allowed_mimes = array_flip( wp_get_mime_types() );
8990

90-
// Create the sources for the full sized image.
91-
foreach ( $valid_mime_transforms[ $mime_type ] as $targeted_mime ) {
92-
// If this property exists no need to create the image again unless is an update.
93-
if ( ! empty( $metadata['sources'][ $targeted_mime ] ) && ! $is_update ) {
94-
continue;
95-
}
91+
// Create the sources for the full sized image only if the target is not the thumbnail only.
92+
if ( 'thumbnail' !== $target ) {
93+
foreach ( $valid_mime_transforms[ $mime_type ] as $targeted_mime ) {
94+
// If this property exists no need to create the image again unless is an update.
95+
if ( ! empty( $metadata['sources'][ $targeted_mime ] ) && ! $is_update ) {
96+
continue;
97+
}
9698

97-
// The targeted mime is not allowed in the current installation.
98-
if ( empty( $allowed_mimes[ $targeted_mime ] ) ) {
99-
continue;
100-
}
99+
// The targeted mime is not allowed in the current installation.
100+
if ( empty( $allowed_mimes[ $targeted_mime ] ) ) {
101+
continue;
102+
}
101103

102-
$extension = explode( '|', $allowed_mimes[ $targeted_mime ] );
103-
$destination = trailingslashit( $original_directory ) . "{$filename}.{$extension[0]}";
104-
$image = webp_uploads_generate_additional_image_source( $attachment_id, $original_size_data, $targeted_mime, $destination );
104+
$extension = explode( '|', $allowed_mimes[ $targeted_mime ] );
105+
$destination = trailingslashit( $original_directory ) . "{$filename}.{$extension[0]}";
106+
$image = webp_uploads_generate_additional_image_source( $attachment_id, $original_size_data, $targeted_mime, $destination );
105107

106-
if ( is_wp_error( $image ) ) {
107-
continue;
108-
}
108+
if ( is_wp_error( $image ) ) {
109+
continue;
110+
}
109111

110-
$metadata['sources'][ $targeted_mime ] = $image;
111-
wp_update_attachment_metadata( $attachment_id, $metadata );
112+
$metadata['sources'][ $targeted_mime ] = $image;
113+
wp_update_attachment_metadata( $attachment_id, $metadata );
114+
}
112115
}
113116

114117
// Make sure we have some sizes to work with, otherwise avoid any work.
@@ -122,6 +125,11 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id,
122125
}
123126

124127
foreach ( $metadata['sizes'] as $size_name => $properties ) {
128+
129+
if ( 'thumbnail' === $target && 'thumbnail' !== $size_name ) {
130+
continue;
131+
}
132+
125133
// This image size is not defined or not an array.
126134
if ( ! is_array( $properties ) ) {
127135
continue;
@@ -160,12 +168,32 @@ function webp_uploads_create_sources_property( array $metadata, $attachment_id,
160168
}
161169

162170
foreach ( $valid_mime_transforms[ $mime_type ] as $mime ) {
163-
// If this property exists no need to create the image again.
171+
// If this property exists no need to create the image again unless is an update with a different mime.
164172
if ( ! empty( $properties['sources'][ $mime ] ) && ! $is_update ) {
165173
continue;
166174
}
167175

168-
$source = webp_uploads_generate_image_size( $attachment_id, $size_name, $mime );
176+
if ( $mime === $current_mime ) {
177+
continue;
178+
}
179+
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 );
193+
} else {
194+
$source = webp_uploads_generate_image_size( $attachment_id, $size_name, $mime );
195+
}
196+
169197
if ( is_wp_error( $source ) ) {
170198
continue;
171199
}
@@ -229,13 +257,15 @@ function webp_uploads_filter_image_editor_output_format( $output_format, $filena
229257
*
230258
* @see wp_create_image_subsizes()
231259
*
232-
* @param int $attachment_id The ID of the attachment we are going to use as a reference to create the image.
233-
* @param string $size The size name that would be used to create this image, out of the registered subsizes.
234-
* @param string $mime A mime type we are looking to use to create this image.
260+
* @param int $attachment_id The ID of the attachment we are going to use as a reference to create the image.
261+
* @param string $size The size name that would be used to create this image, out of the registered subsizes.
262+
* @param string $mime A mime type we are looking to use to create this image.
263+
* @param string $file_path The path to the file used to create the image with.
264+
* @param string $destination_file_name The name used to store the created file it should include the full path.
235265
*
236266
* @return array|WP_Error
237267
*/
238-
function webp_uploads_generate_image_size( $attachment_id, $size, $mime ) {
268+
function webp_uploads_generate_image_size( $attachment_id, $size, $mime, $file_path = null, $destination_file_name = null ) {
239269
$sizes = wp_get_registered_image_subsizes();
240270
$metadata = wp_get_attachment_metadata( $attachment_id );
241271

@@ -251,6 +281,7 @@ function webp_uploads_generate_image_size( $attachment_id, $size, $mime ) {
251281
'width' => 0,
252282
'height' => 0,
253283
'crop' => false,
284+
'file' => $file_path,
254285
);
255286

256287
if ( isset( $sizes[ $size ]['width'] ) ) {
@@ -269,7 +300,7 @@ function webp_uploads_generate_image_size( $attachment_id, $size, $mime ) {
269300
$size_data['crop'] = (bool) $sizes[ $size ]['crop'];
270301
}
271302

272-
return webp_uploads_generate_additional_image_source( $attachment_id, $size_data, $mime );
303+
return webp_uploads_generate_additional_image_source( $attachment_id, $size_data, $mime, $destination_file_name );
273304
}
274305

275306
/**
@@ -340,7 +371,11 @@ function webp_uploads_generate_additional_image_source( $attachment_id, array $s
340371
return new WP_Error( 'image_mime_type_not_supported', __( 'The provided mime type is not supported.', 'performance-lab' ) );
341372
}
342373

343-
$image_path = wp_get_original_image_path( $attachment_id );
374+
if ( empty( $size_data['file'] ) ) {
375+
$image_path = wp_get_original_image_path( $attachment_id );
376+
} else {
377+
$image_path = $size_data['file'];
378+
}
344379

345380
// File does not exist.
346381
if ( ! file_exists( $image_path ) ) {

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

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -954,7 +954,30 @@ public function it_should_prevent_to_backup_the_full_size_image_if_only_the_thum
954954
$this->assertArrayHasKey( 'sources', $backup_sizes['thumbnail-orig'] );
955955

956956
$metadata = wp_get_attachment_metadata( $attachment_id );
957-
$this->assertArrayHasKey( 'sources', $metadata );
957+
958+
$this->assertImageHasSource( $attachment_id, 'image/jpeg' );
959+
$this->assertImageHasSource( $attachment_id, 'image/webp' );
960+
961+
$this->assertImageHasSizeSource( $attachment_id, 'thumbnail', 'image/jpeg' );
962+
$this->assertImageHasSizeSource( $attachment_id, 'thumbnail', 'image/webp' );
963+
964+
$this->assertFileNameIsNotEdited( $metadata['sources']['image/jpeg']['file'] );
965+
$this->assertFileNameIsNotEdited( $metadata['sources']['image/webp']['file'] );
966+
967+
foreach ( $metadata['sizes'] as $size_name => $properties ) {
968+
$this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/jpeg' );
969+
$this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/webp' );
970+
971+
if ( 'thumbnail' === $size_name ) {
972+
$this->assertSame( $properties['file'], $properties['sources']['image/jpeg']['file'] );
973+
$this->assertSame( str_replace( '.jpg', '.webp', $properties['file'] ), $properties['sources']['image/webp']['file'] );
974+
$this->assertFileNameIsEdited( $properties['sources']['image/jpeg']['file'] );
975+
$this->assertFileNameIsEdited( $properties['sources']['image/webp']['file'] );
976+
} else {
977+
$this->assertFileNameIsNotEdited( $properties['sources']['image/jpeg']['file'] );
978+
$this->assertFileNameIsNotEdited( $properties['sources']['image/webp']['file'] );
979+
}
980+
}
958981
}
959982

960983
/**

‎tests/utils/TestCase/ImagesTestCase.php‎

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
* @method void assertImageNotHasSource( $attachment_id, $mime_type, $message ) Asserts that the image doesn't have the appropriate source.
1515
* @method void assertImageNotHasSizeSource( $attachment_id, $size, $mime_type, $message ) Asserts that the image doesn't have the appropriate source for the subsize.
1616
* @method void assertFileNameIsEdited( string $filename, string $message = '' ) Asserts that the provided file name was edited by WordPress contains an e{WITH_13_DIGITS} on the filename.
17+
* @method void assertFileNameIsNotEdited( string $filename, string $message = '' ) Asserts that the provided file name was edited by WordPress contains an e{WITH_13_DIGITS} on the filename.
1718
* @method void assertSizeNameIsHashed( string $size_name, string $hashed_size_name, string $message = '' ) Asserts that the provided size name is an edited name that contains a hash with digits.
1819
*/
1920
abstract class ImagesTestCase extends WP_UnitTestCase {
@@ -89,6 +90,17 @@ public static function assertFileNameIsEdited( $filename, $message = '' ) {
8990
self::assertRegExp( '/e\d{13}/', $filename, $message );
9091
}
9192

93+
/**
94+
* Asserts that the provided file name was edited by WordPress contains an e{WITH_13_DIGITS} on the filename.
95+
*
96+
* @param string $filename The name of the filename to be asserted.
97+
* @param string $message The Error message used to display when the assertion fails.
98+
* @return void
99+
*/
100+
public static function assertFileNameIsNotEdited( $filename, $message = '' ) {
101+
self::assertNotRegExp( '/e\d{13}/', $filename, $message );
102+
}
103+
92104
/**
93105
* Asserts that the provided size name is an edited name that contains a hash with digits.
94106
*

0 commit comments

Comments
 (0)