From f32e16d3b53a8a74267e25f26474b2674bfb6160 Mon Sep 17 00:00:00 2001 From: AhmarZaidi Date: Wed, 12 Feb 2025 18:08:38 +0530 Subject: [PATCH 1/3] Add pdf support to transforms - Add application/pdf as default transforms - Fix image_path issue to handle files other than images - Add unit test to check pdf is included in transforms --- plugins/webp-uploads/helper.php | 11 +++++---- plugins/webp-uploads/tests/test-helper.php | 28 +++++++++++++++------- 2 files changed, 26 insertions(+), 13 deletions(-) diff --git a/plugins/webp-uploads/helper.php b/plugins/webp-uploads/helper.php index a4913cdcf9..a6f9e13573 100644 --- a/plugins/webp-uploads/helper.php +++ b/plugins/webp-uploads/helper.php @@ -29,10 +29,11 @@ function webp_uploads_get_upload_image_mime_transforms(): array { $output_format = webp_uploads_mime_type_supported( 'image/avif' ) ? webp_uploads_get_image_output_format() : 'webp'; $default_transforms = array( - 'image/jpeg' => array( 'image/' . $output_format ), - 'image/webp' => array( 'image/' . $output_format ), - 'image/avif' => array( 'image/avif' ), - 'image/png' => array( 'image/' . $output_format ), + 'image/jpeg' => array( 'image/' . $output_format ), + 'image/webp' => array( 'image/' . $output_format ), + 'image/avif' => array( 'image/avif' ), + 'image/png' => array( 'image/' . $output_format ), + 'application/pdf' => array( 'image/' . $output_format ), ); // Check setting for whether to generate both JPEG and the modern output format. @@ -144,7 +145,7 @@ function webp_uploads_generate_additional_image_source( int $attachment_id, stri return new WP_Error( 'image_mime_type_not_supported', __( 'The provided mime type is not supported.', 'webp-uploads' ) ); } - $image_path = wp_get_original_image_path( $attachment_id ); + $image_path = 'application/pdf' !== get_post_mime_type( $attachment_id ) ? wp_get_original_image_path( $attachment_id ) : get_attached_file( $attachment_id ); if ( false === $image_path || ! 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.', 'webp-uploads' ) ); } diff --git a/plugins/webp-uploads/tests/test-helper.php b/plugins/webp-uploads/tests/test-helper.php index 5165aaa21a..a4fb3aad82 100644 --- a/plugins/webp-uploads/tests/test-helper.php +++ b/plugins/webp-uploads/tests/test-helper.php @@ -364,17 +364,19 @@ public function test_it_should_return_default_transforms_when_filter_returns_non if ( webp_uploads_mime_type_supported( 'image/avif' ) ) { $this->set_image_output_type( 'avif' ); $default_transforms = array( - 'image/jpeg' => array( 'image/avif' ), - 'image/webp' => array( 'image/avif' ), - 'image/avif' => array( 'image/avif' ), - 'image/png' => array( 'image/avif' ), + 'image/jpeg' => array( 'image/avif' ), + 'image/webp' => array( 'image/avif' ), + 'image/avif' => array( 'image/avif' ), + 'image/png' => array( 'image/avif' ), + 'application/pdf' => array( 'image/avif' ), ); } else { $default_transforms = array( - 'image/jpeg' => array( 'image/webp' ), - 'image/webp' => array( 'image/webp' ), - 'image/avif' => array( 'image/avif' ), - 'image/png' => array( 'image/webp' ), + 'image/jpeg' => array( 'image/webp' ), + 'image/webp' => array( 'image/webp' ), + 'image/avif' => array( 'image/avif' ), + 'image/png' => array( 'image/webp' ), + 'application/pdf' => array( 'image/webp' ), ); } @@ -450,6 +452,16 @@ public function test_it_should_return_jpeg_and_webp_transforms_when_option_gener } } + /** + * Returns true if 'application/pdf' is included in the MIME transforms array. + */ + public function test_it_should_include_pdf_in_mime_transforms(): void { + $transforms = webp_uploads_get_upload_image_mime_transforms(); + + $this->assertArrayHasKey( 'application/pdf', $transforms ); + $this->assertContains( 'image/webp', $transforms['application/pdf'] ); + } + /** * @dataProvider data_provider_image_filesize * From ce0895d012692a8ad40ec948c8b5138c09ad71de Mon Sep 17 00:00:00 2001 From: AhmarZaidi Date: Thu, 13 Feb 2025 17:50:45 +0530 Subject: [PATCH 2/3] Update image path logic --- plugins/webp-uploads/helper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/plugins/webp-uploads/helper.php b/plugins/webp-uploads/helper.php index a6f9e13573..1d1207b4a1 100644 --- a/plugins/webp-uploads/helper.php +++ b/plugins/webp-uploads/helper.php @@ -145,7 +145,7 @@ function webp_uploads_generate_additional_image_source( int $attachment_id, stri return new WP_Error( 'image_mime_type_not_supported', __( 'The provided mime type is not supported.', 'webp-uploads' ) ); } - $image_path = 'application/pdf' !== get_post_mime_type( $attachment_id ) ? wp_get_original_image_path( $attachment_id ) : get_attached_file( $attachment_id ); + $image_path = wp_attachment_is_image( $attachment_id ) ? wp_get_original_image_path( $attachment_id ) : get_attached_file( $attachment_id ); if ( false === $image_path || ! 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.', 'webp-uploads' ) ); } From 8209e684bd07cee5784864b459ce6da68fcbef05 Mon Sep 17 00:00:00 2001 From: AhmarZaidi Date: Fri, 14 Feb 2025 19:42:41 +0530 Subject: [PATCH 3/3] Remove ternary operator for readability --- plugins/webp-uploads/helper.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/plugins/webp-uploads/helper.php b/plugins/webp-uploads/helper.php index 1d1207b4a1..4e89f3c522 100644 --- a/plugins/webp-uploads/helper.php +++ b/plugins/webp-uploads/helper.php @@ -145,7 +145,11 @@ function webp_uploads_generate_additional_image_source( int $attachment_id, stri return new WP_Error( 'image_mime_type_not_supported', __( 'The provided mime type is not supported.', 'webp-uploads' ) ); } - $image_path = wp_attachment_is_image( $attachment_id ) ? wp_get_original_image_path( $attachment_id ) : get_attached_file( $attachment_id ); + if ( wp_attachment_is_image( $attachment_id ) ) { + $image_path = wp_get_original_image_path( $attachment_id ); + } else { + $image_path = get_attached_file( $attachment_id ); + } if ( false === $image_path || ! 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.', 'webp-uploads' ) ); }