From a3dee98dc0925675232245d139b6867be7838b6b Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Wed, 13 Apr 2022 19:47:02 -0500 Subject: [PATCH 01/10] Add assertions for target type of edits When an edit with a target occurrs make sure that only the specified images are modified, and the metadata reflects this changes based on the specified target from the media section --- .../images/webp-uploads/webp-uploads-test.php | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/tests/modules/images/webp-uploads/webp-uploads-test.php b/tests/modules/images/webp-uploads/webp-uploads-test.php index 8a624ced51..b9ea7d4018 100644 --- a/tests/modules/images/webp-uploads/webp-uploads-test.php +++ b/tests/modules/images/webp-uploads/webp-uploads-test.php @@ -961,9 +961,22 @@ public function it_should_prevent_to_backup_the_full_size_image_if_only_the_thum $this->assertImageHasSizeSource( $attachment_id, 'thumbnail', 'image/jpeg' ); $this->assertImageHasSizeSource( $attachment_id, 'thumbnail', 'image/webp' ); + $this->assertFileNameIsNotEdited( $metadata['sources']['image/jpeg']['file'] ); + $this->assertFileNameIsNotEdited( $metadata['sources']['image/webp']['file'] ); + foreach ( $metadata['sizes'] as $size_name => $properties ) { $this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/jpeg' ); $this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/webp' ); + + if ( 'thumbnail' === $size_name ) { + $this->assertSame( $properties['file'], $properties['sources']['image/jpeg']['file'] ); + $this->assertSame( str_replace( '.jpg', '.webp', $properties['file'] ), $properties['sources']['image/webp']['file'] ); + $this->assertFileNameIsEdited( $properties['sources']['image/jpeg']['file'] ); + $this->assertFileNameIsEdited( $properties['sources']['image/webp']['file'] ); + } else { + $this->assertFileNameIsNotEdited( $properties['sources']['image/jpeg']['file'] ); + $this->assertFileNameIsNotEdited( $properties['sources']['image/webp']['file'] ); + } } } @@ -992,6 +1005,10 @@ public function it_should_backup_the_image_when_all_images_except_the_thumbnail_ $this->assertImageHasSource( $attachment_id, 'image/jpeg' ); $this->assertImageHasSource( $attachment_id, 'image/webp' ); + foreach ( $updated_metadata['sources'] as $properties ) { + $this->assertFileNameIsEdited( $properties['file'] ); + } + $backup_sizes = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true ); $this->assertIsArray( $backup_sizes ); $this->assertArrayNotHasKey( 'thumbnail-orig', $backup_sizes, 'The thumbnail-orig was stored in the back up' ); @@ -1004,6 +1021,60 @@ public function it_should_backup_the_image_when_all_images_except_the_thumbnail_ } } + /** + * Use the attached image when updating subsequent images not the original version + * + * @test + */ + public function it_should_use_the_attached_image_when_updating_subsequent_images_not_the_original_version() { + // The leafs image is 1080 pixels wide with this filter we ensure a -scaled version is created for this test. + add_filter( + 'big_image_size_threshold', + function () { + // 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. + return 1050; + } + ); + + $attachment_id = $this->factory->attachment->create_upload_object( + TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' + ); + + $this->assertNotSame( wp_get_original_image_path( $attachment_id ), get_attached_file( $attachment_id ) ); + + $editor = new WP_Image_Edit( $attachment_id ); + $editor->flip_right()->all_except_thumbnail()->save(); + + $this->assertTrue( $editor->success() ); + + $metadata = wp_get_attachment_metadata( $attachment_id ); + + $this->assertArrayHasKey( 'original_image', $metadata ); + $this->assertArrayHasKey( 'sources', $metadata ); + $this->assertImageHasSource( $attachment_id, 'image/jpeg' ); + $this->assertImageHasSource( $attachment_id, 'image/webp' ); + + foreach ( $metadata['sources'] as $properties ) { + $this->assertFileNameIsEdited( $properties['file'] ); + $this->assertStringContainsString( '-scaled-', $properties['file'] ); + } + + $this->assertArrayHasKey( 'sizes', $metadata ); + foreach ( $metadata['sizes'] as $size_name => $properties ) { + $this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/jpeg' ); + $this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/webp' ); + foreach ( $properties['sources'] as $mime_type => $values ) { + if ( 'thumbnail' === $size_name ) { + $this->assertFileNameIsNotEdited( $values['file'], "'{$size_name}' is not valid." ); + $this->assertStringNotContainsString( '-scaled-', $values['file'] ); + } else { + $this->assertFileNameIsEdited( $values['file'], "'{$size_name}' is not valid." ); + $this->assertStringContainsString( '-scaled-', $values['file'] ); + } + } + } + } + /** * Update source attributes when webp is edited. * From 7a36d1de98382a8726c0ead9b760f7e17ae8a220 Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Fri, 15 Apr 2022 21:28:32 -0500 Subject: [PATCH 02/10] Migrate tests to verify image edits to new file location Move the tests from the single location into the correct file where the image edits are located. --- .../images/webp-uploads/image-edit-tests.php | 79 +++++++++++++++++-- 1 file changed, 72 insertions(+), 7 deletions(-) diff --git a/tests/modules/images/webp-uploads/image-edit-tests.php b/tests/modules/images/webp-uploads/image-edit-tests.php index a9cee9705a..9e247ea250 100644 --- a/tests/modules/images/webp-uploads/image-edit-tests.php +++ b/tests/modules/images/webp-uploads/image-edit-tests.php @@ -3,7 +3,7 @@ * Tests for webp-uploads module image-edit.php. * * @package performance-lab - * @group webp-uploads + * @group image-edit */ use PerformanceLab\Tests\TestCase\ImagesTestCase; @@ -138,31 +138,38 @@ public function it_should_prevent_to_backup_the_full_size_image_if_only_the_thum $attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' ); $metadata = wp_get_attachment_metadata( $attachment_id ); $this->assertArrayHasKey( 'sources', $metadata ); - $editor = new WP_Image_Edit( $attachment_id ); $editor->flip_vertical()->only_thumbnail()->save(); $this->assertTrue( $editor->success() ); - $backup_sources = get_post_meta( $attachment_id, '_wp_attachment_backup_sources', true ); $this->assertEmpty( $backup_sources ); - $backup_sizes = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true ); $this->assertIsArray( $backup_sizes ); $this->assertCount( 1, $backup_sizes ); $this->assertArrayHasKey( 'thumbnail-orig', $backup_sizes ); $this->assertArrayHasKey( 'sources', $backup_sizes['thumbnail-orig'] ); - $metadata = wp_get_attachment_metadata( $attachment_id ); - $this->assertImageHasSource( $attachment_id, 'image/jpeg' ); $this->assertImageHasSource( $attachment_id, 'image/webp' ); - $this->assertImageHasSizeSource( $attachment_id, 'thumbnail', 'image/jpeg' ); $this->assertImageHasSizeSource( $attachment_id, 'thumbnail', 'image/webp' ); + $this->assertFileNameIsNotEdited( $metadata['sources']['image/jpeg']['file'] ); + $this->assertFileNameIsNotEdited( $metadata['sources']['image/webp']['file'] ); + foreach ( $metadata['sizes'] as $size_name => $properties ) { $this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/jpeg' ); $this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/webp' ); + + if ( 'thumbnail' === $size_name ) { + $this->assertSame( $properties['file'], $properties['sources']['image/jpeg']['file'] ); + $this->assertSame( str_replace( '.jpg', '.webp', $properties['file'] ), $properties['sources']['image/webp']['file'] ); + $this->assertFileNameIsEdited( $properties['sources']['image/jpeg']['file'] ); + $this->assertFileNameIsEdited( $properties['sources']['image/webp']['file'] ); + } else { + $this->assertFileNameIsNotEdited( $properties['sources']['image/jpeg']['file'] ); + $this->assertFileNameIsNotEdited( $properties['sources']['image/webp']['file'] ); + } } } @@ -191,6 +198,10 @@ public function it_should_backup_the_image_when_all_images_except_the_thumbnail_ $this->assertImageHasSource( $attachment_id, 'image/jpeg' ); $this->assertImageHasSource( $attachment_id, 'image/webp' ); + foreach ( $updated_metadata['sources'] as $properties ) { + $this->assertFileNameIsEdited( $properties['file'] ); + } + $backup_sizes = get_post_meta( $attachment_id, '_wp_attachment_backup_sizes', true ); $this->assertIsArray( $backup_sizes ); $this->assertArrayNotHasKey( 'thumbnail-orig', $backup_sizes, 'The thumbnail-orig was stored in the back up' ); @@ -203,6 +214,60 @@ public function it_should_backup_the_image_when_all_images_except_the_thumbnail_ } } + /** + * Use the attached image when updating subsequent images not the original version + * + * @test + */ + public function it_should_use_the_attached_image_when_updating_subsequent_images_not_the_original_version() { + // The leafs image is 1080 pixels wide with this filter we ensure a -scaled version is created for this test. + add_filter( + 'big_image_size_threshold', + function () { + // 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. + return 1050; + } + ); + + $attachment_id = $this->factory->attachment->create_upload_object( + TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' + ); + + $this->assertNotSame( wp_get_original_image_path( $attachment_id ), get_attached_file( $attachment_id ) ); + + $editor = new WP_Image_Edit( $attachment_id ); + $editor->flip_right()->all_except_thumbnail()->save(); + + $this->assertTrue( $editor->success() ); + + $metadata = wp_get_attachment_metadata( $attachment_id ); + + $this->assertArrayHasKey( 'original_image', $metadata ); + $this->assertArrayHasKey( 'sources', $metadata ); + $this->assertImageHasSource( $attachment_id, 'image/jpeg' ); + $this->assertImageHasSource( $attachment_id, 'image/webp' ); + + foreach ( $metadata['sources'] as $properties ) { + $this->assertFileNameIsEdited( $properties['file'] ); + $this->assertStringContainsString( '-scaled-', $properties['file'] ); + } + + $this->assertArrayHasKey( 'sizes', $metadata ); + foreach ( $metadata['sizes'] as $size_name => $properties ) { + $this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/jpeg' ); + $this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/webp' ); + foreach ( $properties['sources'] as $mime_type => $values ) { + if ( 'thumbnail' === $size_name ) { + $this->assertFileNameIsNotEdited( $values['file'], "'{$size_name}' is not valid." ); + $this->assertStringNotContainsString( '-scaled-', $values['file'] ); + } else { + $this->assertFileNameIsEdited( $values['file'], "'{$size_name}' is not valid." ); + $this->assertStringContainsString( '-scaled-', $values['file'] ); + } + } + } + } + /** * Update source attributes when webp is edited. * From 5ecffc893528724b9f86c055c0af657fdf03b073 Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Sat, 16 Apr 2022 16:18:37 -0500 Subject: [PATCH 03/10] Handle the scenario when a target is selected. When an image is selected for a target make sure that only the selected target images are processed instead of processing all the images when is not required or desired. Fixes #299 --- modules/images/webp-uploads/image-edit.php | 119 +++++++++++++++------ 1 file changed, 89 insertions(+), 30 deletions(-) diff --git a/modules/images/webp-uploads/image-edit.php b/modules/images/webp-uploads/image-edit.php index 456d6c844c..ec851f3423 100644 --- a/modules/images/webp-uploads/image-edit.php +++ b/modules/images/webp-uploads/image-edit.php @@ -19,18 +19,41 @@ */ function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $main_images, $subsized_images ) { foreach ( $valid_mime_transforms as $targeted_mime ) { - // Make sure the path and file exists as those values are being accessed. - if ( ! isset( $main_images[ $targeted_mime ]['path'], $main_images[ $targeted_mime ]['file'] ) || ! file_exists( $main_images[ $targeted_mime ]['path'] ) ) { - continue; + // Make sure the path and file exists as those values are required. + $image_directory = null; + if ( isset( $main_images[ $targeted_mime ]['path'], $main_images[ $targeted_mime ]['file'] ) && file_exists( $main_images[ $targeted_mime ]['path'] ) ) { + // Add sources to original image metadata. + $metadata['sources'][ $targeted_mime ] = array( + 'file' => $main_images[ $targeted_mime ]['file'], + 'filesize' => filesize( $main_images[ $targeted_mime ]['path'] ), + ); + $image_directory = pathinfo( $main_images[ $targeted_mime ]['path'], PATHINFO_DIRNAME ); } - $image_directory = pathinfo( $main_images[ $targeted_mime ]['path'], PATHINFO_DIRNAME ); + /** + * If no original image was provided the image_directory can't be determined, in that scenario try to + * find it from the `file` property. + * + * @see get_attached_file() + */ + if ( + null === $image_directory + && isset( $metadata['file'] ) + && 0 !== strpos( $metadata['file'], '/' ) + && ! preg_match( '|^.:\\\|', $metadata['file'] ) + ) { + $uploads = wp_get_upload_dir(); + if ( false === $uploads['error'] && isset( $uploads['basedir'] ) ) { + $file = path_join( $uploads['basedir'], $metadata['file'] ); + if ( file_exists( $file ) ) { + $image_directory = pathinfo( $file, PATHINFO_DIRNAME ); + } + } + } - // Add sources to original image metadata. - $metadata['sources'][ $targeted_mime ] = array( - 'file' => $main_images[ $targeted_mime ]['file'], - 'filesize' => filesize( $main_images[ $targeted_mime ]['path'] ), - ); + if ( null === $image_directory ) { + continue; + } foreach ( $metadata['sizes'] as $size_name => $size_details ) { if ( empty( $subsized_images[ $targeted_mime ][ $size_name ]['file'] ) ) { @@ -39,7 +62,6 @@ function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $main_i // Add sources to resized image metadata. $subsize_path = path_join( $image_directory, $subsized_images[ $targeted_mime ][ $size_name ]['file'] ); - if ( ! file_exists( $subsize_path ) ) { continue; } @@ -59,29 +81,29 @@ function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $main_i * * @since n.e.x.t * - * @param bool|null $override Value to return instead of saving. Default null. - * @param string $file_path Name of the file to be saved. - * @param WP_Image_Editor $editor The image editor instance. - * @param string $mime_type The mime type of the image. - * @param int $post_id Attachment post ID. + * @param bool|null $override Value to return instead of saving. Default null. + * @param string $file_path Name of the file to be saved. + * @param WP_Image_Editor $editor The image editor instance. + * @param string $original_mime_type The mime type of the image. + * @param int $post_id Attachment post ID. * @return bool|null Potentially modified $override value. */ -function webp_uploads_update_image_onchange( $override, $file_path, $editor, $mime_type, $post_id ) { +function webp_uploads_update_image_onchange( $override, $file_path, $editor, $original_mime_type, $post_id ) { if ( null !== $override ) { return $override; } $transforms = webp_uploads_get_upload_image_mime_transforms(); - if ( empty( $transforms[ $mime_type ] ) ) { + if ( empty( $transforms[ $original_mime_type ] ) ) { return $override; } - $mime_transforms = $transforms[ $mime_type ]; + $mime_transforms = $transforms[ $original_mime_type ]; // This variable allows to unhook the logic from within the closure without the need fo a function name. $callback_executed = false; add_filter( 'wp_update_attachment_metadata', - function ( $metadata, $post_meta_id ) use ( $post_id, $file_path, $mime_type, $editor, $mime_transforms, &$callback_executed ) { + function ( $metadata, $post_meta_id ) use ( $post_id, $file_path, $original_mime_type, $editor, $mime_transforms, &$callback_executed ) { if ( $post_meta_id !== $post_id ) { return $metadata; } @@ -91,7 +113,6 @@ function ( $metadata, $post_meta_id ) use ( $post_id, $file_path, $mime_type, $e return $metadata; } $callback_executed = true; - // No sizes to be created. if ( empty( $metadata['sizes'] ) ) { return $metadata; @@ -99,7 +120,13 @@ function ( $metadata, $post_meta_id ) use ( $post_id, $file_path, $mime_type, $e $old_metadata = wp_get_attachment_metadata( $post_id ); $resize_sizes = array(); + $target = isset( $_REQUEST['target'] ) ? $_REQUEST['target'] : 'all'; + foreach ( $old_metadata['sizes'] as $size_name => $size_details ) { + if ( 'nothumb' === $target && 'thumbnail' === $size_name ) { + continue; + } + if ( isset( $metadata['sizes'][ $size_name ] ) && ! empty( $metadata['sizes'][ $size_name ] ) && $metadata['sizes'][ $size_name ]['file'] !== $old_metadata['sizes'][ $size_name ]['file'] ) { $resize_sizes[ $size_name ] = $metadata['sizes'][ $size_name ]; @@ -111,8 +138,18 @@ function ( $metadata, $post_meta_id ) use ( $post_id, $file_path, $mime_type, $e $filename = pathinfo( $file_path, PATHINFO_FILENAME ); $main_images = array(); $subsized_images = array(); + foreach ( $mime_transforms as $targeted_mime ) { - if ( $targeted_mime === $mime_type ) { + if ( $targeted_mime === $original_mime_type ) { + // If the target is `thumbnail` make sure is the only selected size. + if ( 'thumbnail' === $target ) { + if ( isset( $metadata['sizes']['thumbnail'] ) ) { + $subsized_images[ $targeted_mime ] = array( 'thumbnail' => $metadata['sizes']['thumbnail'] ); + } + // When the targeted thumbnail is selected no additional size and subsize is set. + continue; + } + $main_images[ $targeted_mime ] = array( 'path' => $file_path, 'file' => pathinfo( $file_path, PATHINFO_BASENAME ), @@ -129,16 +166,38 @@ function ( $metadata, $post_meta_id ) use ( $post_id, $file_path, $mime_type, $e continue; } - $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); - $destination = trailingslashit( $original_directory ) . "{$filename}.{$extension[0]}"; - $result = $editor->save( $destination, $targeted_mime ); - - if ( is_wp_error( $result ) ) { - continue; + $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); + $extension = $extension[0]; + + if ( 'thumbnail' === $target ) { + if ( ! isset( $subsized_images[ $original_mime_type ]['thumbnail']['file'] ) ) { + continue; + } + $thumbnail_file = $subsized_images[ $original_mime_type ]['thumbnail']['file']; + $image_path = path_join( $original_directory, $thumbnail_file ); + $editor = wp_get_image_editor( $image_path, array( 'mime_type' => $targeted_mime ) ); + + if ( is_wp_error( $editor ) ) { + continue; + } + + $current_extension = pathinfo( $thumbnail_file, PATHINFO_EXTENSION ); + // Create a file with then new extension out of the targeted file. + $target_file_name = preg_replace( "/\.$current_extension$/", ".$extension", $thumbnail_file ); + $target_file_location = path_join( $original_directory, $target_file_name ); + $result = $editor->save( $target_file_location, $targeted_mime ); + $subsized_images[ $targeted_mime ] = array( 'thumbnail' => $result ); + } else { + $destination = trailingslashit( $original_directory ) . "{$filename}.{$extension}"; + $result = $editor->save( $destination, $targeted_mime ); + + if ( is_wp_error( $result ) ) { + continue; + } + + $main_images[ $targeted_mime ] = $result; + $subsized_images[ $targeted_mime ] = $editor->multi_resize( $resize_sizes ); } - - $main_images[ $targeted_mime ] = $result; - $subsized_images[ $targeted_mime ] = $editor->multi_resize( $resize_sizes ); } return webp_uploads_update_sources( $metadata, $mime_transforms, $main_images, $subsized_images ); From 20c45d102d0c0c046939269e1611e46b3b24e4df Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Crisoforo=20Gaspar=20Hern=C3=A1ndez?= Date: Sat, 23 Apr 2022 12:42:18 -0500 Subject: [PATCH 04/10] Replace regular expression with `substr` call instead. Co-authored-by: Eugene Manuilov --- modules/images/webp-uploads/image-edit.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/images/webp-uploads/image-edit.php b/modules/images/webp-uploads/image-edit.php index ec851f3423..3e06a00f29 100644 --- a/modules/images/webp-uploads/image-edit.php +++ b/modules/images/webp-uploads/image-edit.php @@ -40,7 +40,7 @@ function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $main_i null === $image_directory && isset( $metadata['file'] ) && 0 !== strpos( $metadata['file'], '/' ) - && ! preg_match( '|^.:\\\|', $metadata['file'] ) + && ':\' !== substr( $metadata['file'], 1, 2 ) ) { $uploads = wp_get_upload_dir(); if ( false === $uploads['error'] && isset( $uploads['basedir'] ) ) { From 10256c646adad5c91a2c574c27511ed279c2f759 Mon Sep 17 00:00:00 2001 From: Crisoforo Gaspar Date: Sat, 23 Apr 2022 12:51:08 -0500 Subject: [PATCH 05/10] Escape correctly the back slash `\` --- modules/images/webp-uploads/image-edit.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/images/webp-uploads/image-edit.php b/modules/images/webp-uploads/image-edit.php index 3e06a00f29..1f65ed0a77 100644 --- a/modules/images/webp-uploads/image-edit.php +++ b/modules/images/webp-uploads/image-edit.php @@ -40,7 +40,7 @@ function webp_uploads_update_sources( $metadata, $valid_mime_transforms, $main_i null === $image_directory && isset( $metadata['file'] ) && 0 !== strpos( $metadata['file'], '/' ) - && ':\' !== substr( $metadata['file'], 1, 2 ) + && ':\\' !== substr( $metadata['file'], 1, 2 ) ) { $uploads = wp_get_upload_dir(); if ( false === $uploads['error'] && isset( $uploads['basedir'] ) ) { From 91427d2e080bde261f27dfe8c71ea0b5315e725c Mon Sep 17 00:00:00 2001 From: Mehul Kaklotar Date: Thu, 12 May 2022 13:35:53 +0530 Subject: [PATCH 06/10] Update modules/images/webp-uploads/image-edit.php Co-authored-by: Felix Arntz --- modules/images/webp-uploads/image-edit.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/modules/images/webp-uploads/image-edit.php b/modules/images/webp-uploads/image-edit.php index ea3fcf8ed0..c14f73a64c 100644 --- a/modules/images/webp-uploads/image-edit.php +++ b/modules/images/webp-uploads/image-edit.php @@ -141,7 +141,7 @@ function ( $metadata, $post_meta_id ) use ( $post_id, $file_path, $original_mime foreach ( $mime_transforms as $targeted_mime ) { if ( $targeted_mime === $original_mime_type ) { - // If the target is `thumbnail` make sure is the only selected size. + // If the target is `thumbnail` make sure it is the only selected size. if ( 'thumbnail' === $target ) { if ( isset( $metadata['sizes']['thumbnail'] ) ) { $subsized_images[ $targeted_mime ] = array( 'thumbnail' => $metadata['sizes']['thumbnail'] ); From fb8d9881d9abd574106360372e99d13d0d18d533 Mon Sep 17 00:00:00 2001 From: Mehul Kaklotar Date: Thu, 12 May 2022 13:36:05 +0530 Subject: [PATCH 07/10] Update modules/images/webp-uploads/image-edit.php Co-authored-by: Felix Arntz --- modules/images/webp-uploads/image-edit.php | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/images/webp-uploads/image-edit.php b/modules/images/webp-uploads/image-edit.php index c14f73a64c..c711379801 100644 --- a/modules/images/webp-uploads/image-edit.php +++ b/modules/images/webp-uploads/image-edit.php @@ -169,6 +169,7 @@ function ( $metadata, $post_meta_id ) use ( $post_id, $file_path, $original_mime $extension = explode( '|', $allowed_mimes[ $targeted_mime ] ); $extension = $extension[0]; + // If the target is `thumbnail` make sure only that size is generated. if ( 'thumbnail' === $target ) { if ( ! isset( $subsized_images[ $original_mime_type ]['thumbnail']['file'] ) ) { continue; From d71045f8212b9dd59154c41866b1a077e24e4352 Mon Sep 17 00:00:00 2001 From: Mehul Kaklotar Date: Thu, 12 May 2022 13:36:14 +0530 Subject: [PATCH 08/10] Update modules/images/webp-uploads/image-edit.php Co-authored-by: Felix Arntz --- modules/images/webp-uploads/image-edit.php | 1 + 1 file changed, 1 insertion(+) diff --git a/modules/images/webp-uploads/image-edit.php b/modules/images/webp-uploads/image-edit.php index c711379801..69bbad94ac 100644 --- a/modules/images/webp-uploads/image-edit.php +++ b/modules/images/webp-uploads/image-edit.php @@ -123,6 +123,7 @@ function ( $metadata, $post_meta_id ) use ( $post_id, $file_path, $original_mime $target = isset( $_REQUEST['target'] ) ? $_REQUEST['target'] : 'all'; foreach ( $old_metadata['sizes'] as $size_name => $size_details ) { + // If the target is 'nothumb', skip generating the 'thumbnail' size. if ( 'nothumb' === $target && 'thumbnail' === $size_name ) { continue; } From fa7edad86f4a6733c57ee7e69d8a07613d0b1211 Mon Sep 17 00:00:00 2001 From: Mehul Kaklotar Date: Thu, 12 May 2022 15:51:05 +0530 Subject: [PATCH 09/10] PR feedback - check for WP_Error returned added --- modules/images/webp-uploads/image-edit.php | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/modules/images/webp-uploads/image-edit.php b/modules/images/webp-uploads/image-edit.php index 4ee16fe8bc..3fb9357e98 100644 --- a/modules/images/webp-uploads/image-edit.php +++ b/modules/images/webp-uploads/image-edit.php @@ -185,9 +185,14 @@ function ( $metadata, $post_meta_id ) use ( $post_id, $file_path, $original_mime $current_extension = pathinfo( $thumbnail_file, PATHINFO_EXTENSION ); // Create a file with then new extension out of the targeted file. - $target_file_name = preg_replace( "/\.$current_extension$/", ".$extension", $thumbnail_file ); - $target_file_location = path_join( $original_directory, $target_file_name ); - $result = $editor->save( $target_file_location, $targeted_mime ); + $target_file_name = preg_replace( "/\.$current_extension$/", ".$extension", $thumbnail_file ); + $target_file_location = path_join( $original_directory, $target_file_name ); + $result = $editor->save( $target_file_location, $targeted_mime ); + + if ( is_wp_error( $result ) ) { + continue; + } + $subsized_images[ $targeted_mime ] = array( 'thumbnail' => $result ); } else { $destination = trailingslashit( $original_directory ) . "{$filename}.{$extension}"; From 3643b942507da8c40e05c338b5e3eccc6c9dc97c Mon Sep 17 00:00:00 2001 From: Mehul Kaklotar Date: Thu, 12 May 2022 17:21:28 +0530 Subject: [PATCH 10/10] test case fixed, to converted --- modules/images/webp-uploads/image-edit.php | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/modules/images/webp-uploads/image-edit.php b/modules/images/webp-uploads/image-edit.php index 3fb9357e98..c3b8e9621d 100644 --- a/modules/images/webp-uploads/image-edit.php +++ b/modules/images/webp-uploads/image-edit.php @@ -103,7 +103,7 @@ function webp_uploads_update_image_onchange( $override, $file_path, $editor, $mi $callback_executed = false; add_filter( 'wp_update_attachment_metadata', - function ( $metadata, $post_meta_id ) use ( $post_id, $file_path, $original_mime_type, $editor, $mime_transforms, &$callback_executed ) { + function ( $metadata, $post_meta_id ) use ( $post_id, $file_path, $mime_type, $editor, $mime_transforms, &$callback_executed ) { if ( $post_meta_id !== $post_id ) { return $metadata; } @@ -141,7 +141,7 @@ function ( $metadata, $post_meta_id ) use ( $post_id, $file_path, $original_mime $subsized_images = array(); foreach ( $mime_transforms as $targeted_mime ) { - if ( $targeted_mime === $original_mime_type ) { + if ( $targeted_mime === $mime_type ) { // If the target is `thumbnail` make sure it is the only selected size. if ( 'thumbnail' === $target ) { if ( isset( $metadata['sizes']['thumbnail'] ) ) { @@ -172,10 +172,10 @@ function ( $metadata, $post_meta_id ) use ( $post_id, $file_path, $original_mime // If the target is `thumbnail` make sure only that size is generated. if ( 'thumbnail' === $target ) { - if ( ! isset( $subsized_images[ $original_mime_type ]['thumbnail']['file'] ) ) { + if ( ! isset( $subsized_images[ $mime_type ]['thumbnail']['file'] ) ) { continue; } - $thumbnail_file = $subsized_images[ $original_mime_type ]['thumbnail']['file']; + $thumbnail_file = $subsized_images[ $mime_type ]['thumbnail']['file']; $image_path = path_join( $original_directory, $thumbnail_file ); $editor = wp_get_image_editor( $image_path, array( 'mime_type' => $targeted_mime ) );