From 7421a1239bdf4390b2d219b8c7da92c14841ef90 Mon Sep 17 00:00:00 2001 From: Eugene Manuilov Date: Fri, 13 May 2022 23:51:23 +0300 Subject: [PATCH 1/5] Update the webp_uploads_pre_generate_additional_image_source filter to allow returing file size. --- modules/images/webp-uploads/helper.php | 20 +++++++++----------- 1 file changed, 9 insertions(+), 11 deletions(-) diff --git a/modules/images/webp-uploads/helper.php b/modules/images/webp-uploads/helper.php index d5f6adb6e..9457fb562 100644 --- a/modules/images/webp-uploads/helper.php +++ b/modules/images/webp-uploads/helper.php @@ -67,7 +67,6 @@ function webp_uploads_get_upload_image_mime_transforms() { * @return array|WP_Error An array with the file and filesize if the image was created correctly otherwise a WP_Error */ function webp_uploads_generate_additional_image_source( $attachment_id, $image_size, array $size_data, $mime, $destination_file_name = null ) { - /** * Filter to allow the generation of additional image sources, in which a defined mime type * can be transformed and create additional mime types for the file. @@ -84,19 +83,23 @@ function webp_uploads_generate_additional_image_source( $attachment_id, $image_s * @return array|null|WP_Error An array with the file and filesize if the image was created correctly otherwise a WP_Error */ $image = apply_filters( 'webp_uploads_pre_generate_additional_image_source', null, $attachment_id, $image_size, $size_data, $mime ); - if ( is_wp_error( $image ) ) { return $image; } if ( - is_array( $image ) - && ! empty( $image['file'] ) - && ! empty( $image['path'] ) + is_array( $image ) && + ! empty( $image['file'] ) && + ( + ! empty( $image['path'] ) || + ! empty( $image['filesize'] ) + ) ) { return array( 'file' => $image['file'], - 'filesize' => filesize( $image['path'] ), + 'filesize' => ! empty( $image['filesize'] ) + ? $image['filesize'] + : filesize( $image['path'] ), ); } @@ -110,14 +113,11 @@ function webp_uploads_generate_additional_image_source( $attachment_id, $image_s } $image_path = wp_get_original_image_path( $attachment_id ); - - // File does not exist. if ( ! file_exists( $image_path ) ) { return new WP_Error( 'original_image_file_not_found', __( 'The original image file does not exists, subsizes are created out of the original image.', 'performance-lab' ) ); } $editor = wp_get_image_editor( $image_path, array( 'mime_type' => $mime ) ); - if ( is_wp_error( $editor ) ) { return $editor; } @@ -125,7 +125,6 @@ function webp_uploads_generate_additional_image_source( $attachment_id, $image_s $height = isset( $size_data['height'] ) ? (int) $size_data['height'] : 0; $width = isset( $size_data['width'] ) ? (int) $size_data['width'] : 0; $crop = isset( $size_data['crop'] ) && $size_data['crop']; - if ( $width <= 0 && $height <= 0 ) { return new WP_Error( 'image_wrong_dimensions', __( 'At least one of the dimensions must be a positive number.', 'performance-lab' ) ); } @@ -144,7 +143,6 @@ function webp_uploads_generate_additional_image_source( $attachment_id, $image_s } $image = $editor->save( $destination_file_name, $mime ); - if ( is_wp_error( $image ) ) { return $image; } From 4cfce3c9b6f1dc746374af2e1ed7f0847b2bcb34 Mon Sep 17 00:00:00 2001 From: Eugene Manuilov Date: Sat, 14 May 2022 00:21:25 +0300 Subject: [PATCH 2/5] Update phpunit tests. --- .../images/webp-uploads/helper-tests.php | 39 ++++++++++++++----- 1 file changed, 29 insertions(+), 10 deletions(-) diff --git a/tests/modules/images/webp-uploads/helper-tests.php b/tests/modules/images/webp-uploads/helper-tests.php index 53b6108fc..a50eebbe7 100644 --- a/tests/modules/images/webp-uploads/helper-tests.php +++ b/tests/modules/images/webp-uploads/helper-tests.php @@ -270,7 +270,33 @@ function () { $this->assertArrayHasKey( 'filesize', $result ); $this->assertArrayHasKey( 'file', $result ); $this->assertStringEndsWith( 'image.webp', $result['file'] ); - $this->assertFileExists( '/tmp/image.webp' ); + } + + /** + * Tests the webp_uploads_pre_generate_additional_image_source filter returning filesize property. + * + * @test + */ + public function it_should_return_an_error_when_filter_webp_uploads_pre_generate_additional_image_source_returns_filesize() { + remove_all_filters( 'webp_uploads_pre_generate_additional_image_source' ); + + add_filter( + 'webp_uploads_pre_generate_additional_image_source', + function () { + return array( + 'file' => 'image.webp', + 'filesize' => 777, + ); + } + ); + + $result = webp_uploads_generate_additional_image_source( 0, 'medium', array(), 'image/webp', '/tmp/image.jpg' ); + + $this->assertIsArray( $result ); + $this->assertArrayHasKey( 'filesize', $result ); + $this->assertEquals( 777, $result['filesize'] ); + $this->assertArrayHasKey( 'file', $result ); + $this->assertStringEndsWith( 'image.webp', $result['file'] ); } /** @@ -281,8 +307,6 @@ function () { public function it_should_return_an_error_when_filter_webp_uploads_pre_generate_additional_image_source_returns_wp_error() { remove_all_filters( 'webp_uploads_pre_generate_additional_image_source' ); - $attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/car.jpeg' ); - add_filter( 'webp_uploads_pre_generate_additional_image_source', function () { @@ -290,14 +314,9 @@ function () { } ); - $size_data = array( - 'width' => 300, - 'height' => 300, - 'crop' => true, - ); - - $result = webp_uploads_generate_additional_image_source( $attachment_id, 'medium', $size_data, 'image/webp', '/tmp/image.jpg' ); + $result = webp_uploads_generate_additional_image_source( 0, 'medium', array(), 'image/webp', '/tmp/image.jpg' ); $this->assertWPError( $result ); $this->assertSame( 'image_additional_generated_error', $result->get_error_code() ); } + } From 6380957baa5155edd7b335208eeb11df26a5a182 Mon Sep 17 00:00:00 2001 From: Eugene Manuilov Date: Tue, 17 May 2022 17:36:04 +0300 Subject: [PATCH 3/5] Address code review feedback. --- modules/images/webp-uploads/helper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/modules/images/webp-uploads/helper.php b/modules/images/webp-uploads/helper.php index 00d8ab821..4bb8bc1d8 100644 --- a/modules/images/webp-uploads/helper.php +++ b/modules/images/webp-uploads/helper.php @@ -92,12 +92,12 @@ function webp_uploads_generate_additional_image_source( $attachment_id, $image_s ! empty( $image['file'] ) && ( ! empty( $image['path'] ) || - ! empty( $image['filesize'] ) + array_key_exists( 'filesize', $image ) ) ) { return array( 'file' => $image['file'], - 'filesize' => ! empty( $image['filesize'] ) + 'filesize' => array_key_exists( 'filesize', $image ) ? $image['filesize'] : filesize( $image['path'] ), ); From a082af9fa85bd7ebf0fdf78894a6da126be16c46 Mon Sep 17 00:00:00 2001 From: Eugene Manuilov Date: Wed, 18 May 2022 17:59:08 +0300 Subject: [PATCH 4/5] Fix incorrect test name. --- tests/modules/images/webp-uploads/helper-tests.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/modules/images/webp-uploads/helper-tests.php b/tests/modules/images/webp-uploads/helper-tests.php index a50eebbe7..b03c58150 100644 --- a/tests/modules/images/webp-uploads/helper-tests.php +++ b/tests/modules/images/webp-uploads/helper-tests.php @@ -277,7 +277,7 @@ function () { * * @test */ - public function it_should_return_an_error_when_filter_webp_uploads_pre_generate_additional_image_source_returns_filesize() { + public function it_should_use_filesize_when_filter_webp_uploads_pre_generate_additional_image_source_returns_filesize() { remove_all_filters( 'webp_uploads_pre_generate_additional_image_source' ); add_filter( From 5a313377717c30f9a0cd15ce98c2b85b453808c6 Mon Sep 17 00:00:00 2001 From: Eugene Manuilov Date: Thu, 19 May 2022 19:33:23 +0300 Subject: [PATCH 5/5] Update tests. --- .../images/webp-uploads/helper-tests.php | 20 +++++++++++++++++-- 1 file changed, 18 insertions(+), 2 deletions(-) diff --git a/tests/modules/images/webp-uploads/helper-tests.php b/tests/modules/images/webp-uploads/helper-tests.php index b03c58150..c990d386d 100644 --- a/tests/modules/images/webp-uploads/helper-tests.php +++ b/tests/modules/images/webp-uploads/helper-tests.php @@ -280,6 +280,8 @@ function () { public function it_should_use_filesize_when_filter_webp_uploads_pre_generate_additional_image_source_returns_filesize() { remove_all_filters( 'webp_uploads_pre_generate_additional_image_source' ); + $attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/car.jpeg' ); + add_filter( 'webp_uploads_pre_generate_additional_image_source', function () { @@ -290,7 +292,13 @@ function () { } ); - $result = webp_uploads_generate_additional_image_source( 0, 'medium', array(), 'image/webp', '/tmp/image.jpg' ); + $size_data = array( + 'width' => 300, + 'height' => 300, + 'crop' => true, + ); + + $result = webp_uploads_generate_additional_image_source( $attachment_id, 'medium', $size_data, 'image/webp', '/tmp/image.jpg' ); $this->assertIsArray( $result ); $this->assertArrayHasKey( 'filesize', $result ); @@ -307,6 +315,8 @@ function () { public function it_should_return_an_error_when_filter_webp_uploads_pre_generate_additional_image_source_returns_wp_error() { remove_all_filters( 'webp_uploads_pre_generate_additional_image_source' ); + $attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/car.jpeg' ); + add_filter( 'webp_uploads_pre_generate_additional_image_source', function () { @@ -314,7 +324,13 @@ function () { } ); - $result = webp_uploads_generate_additional_image_source( 0, 'medium', array(), 'image/webp', '/tmp/image.jpg' ); + $size_data = array( + 'width' => 300, + 'height' => 300, + 'crop' => true, + ); + + $result = webp_uploads_generate_additional_image_source( $attachment_id, 'medium', $size_data, 'image/webp', '/tmp/image.jpg' ); $this->assertWPError( $result ); $this->assertSame( 'image_additional_generated_error', $result->get_error_code() ); }