diff --git a/plugins/webp-uploads/helper.php b/plugins/webp-uploads/helper.php index a089809361..7d2b1249b5 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,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_get_original_image_path( $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' ) ); } diff --git a/plugins/webp-uploads/tests/test-helper.php b/plugins/webp-uploads/tests/test-helper.php index 0eaa68e74a..79292dd6c3 100644 --- a/plugins/webp-uploads/tests/test-helper.php +++ b/plugins/webp-uploads/tests/test-helper.php @@ -414,17 +414,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' ), ); } @@ -500,6 +502,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 *