diff --git a/plugins/webp-uploads/hooks.php b/plugins/webp-uploads/hooks.php index 8354eea92c..07167cfc4f 100644 --- a/plugins/webp-uploads/hooks.php +++ b/plugins/webp-uploads/hooks.php @@ -52,18 +52,26 @@ * } An array with the updated structure for the metadata before is stored in the database. */ function webp_uploads_create_sources_property( array $metadata, int $attachment_id ): array { - // This should take place only on the JPEG image. - $valid_mime_transforms = webp_uploads_get_upload_image_mime_transforms(); + $file = get_attached_file( $attachment_id, true ); + // File does not exist. + if ( false === $file || ! file_exists( $file ) ) { + return $metadata; + } - // Not a supported mime type to create the sources property. - $mime_type = get_post_mime_type( $attachment_id ); - if ( ! is_string( $mime_type ) || ! isset( $valid_mime_transforms[ $mime_type ] ) ) { + /* + * We need to get the MIME type ideally from the file, as WordPress Core may have already altered it. + * The post MIME type is typically not updated during that process. + */ + $filetype = wp_check_filetype( $file ); + $mime_type = $filetype['type'] ?? get_post_mime_type( $attachment_id ); + if ( ! is_string( $mime_type ) ) { return $metadata; } - $file = get_attached_file( $attachment_id, true ); - // File does not exist. - if ( false === $file || ! file_exists( $file ) ) { + $valid_mime_transforms = webp_uploads_get_upload_image_mime_transforms(); + + // Not a supported mime type to create the sources property. + if ( ! isset( $valid_mime_transforms[ $mime_type ] ) ) { return $metadata; } diff --git a/plugins/webp-uploads/tests/test-load.php b/plugins/webp-uploads/tests/test-load.php index f70fd19e9f..c9c8f0d32f 100644 --- a/plugins/webp-uploads/tests/test-load.php +++ b/plugins/webp-uploads/tests/test-load.php @@ -43,9 +43,9 @@ static function ( string $filename ) { /** * Don't create the original mime type for JPEG images. * - * @dataProvider data_provider_supported_image_types + * @dataProvider data_provider_supported_image_types_with_threshold */ - public function test_it_should_not_create_the_original_mime_type_for_jpeg_images( string $image_type ): void { + public function test_it_should_not_create_the_original_mime_type_for_jpeg_images( string $image_type, bool $above_big_image_size = false ): void { $mime_type = 'image/' . $image_type; $this->set_image_output_type( $image_type ); if ( ! webp_uploads_mime_type_supported( $mime_type ) ) { @@ -53,6 +53,16 @@ public function test_it_should_not_create_the_original_mime_type_for_jpeg_images } $attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/leaves.jpg' ); + if ( $above_big_image_size ) { + // Add threshold to create a `-scaled` output image for testing. + add_filter( + 'big_image_size_threshold', + static function () { + return 850; + } + ); + } + // There should be an image_type source, but no JPEG source for the full image. $this->assertImageHasSource( $attachment_id, $mime_type ); $this->assertImageNotHasSource( $attachment_id, 'image/jpeg' ); @@ -106,42 +116,6 @@ public function test_it_should_create_the_original_mime_type_as_well_with_all_th } } - /** - * Create JPEG and output type for JPEG images, if opted in. - * - * @dataProvider data_provider_supported_image_types - */ - public function test_it_should_create_jpeg_and_webp_for_jpeg_images_if_opted_in( string $image_type ): void { - $mime_type = 'image/' . $image_type; - if ( ! webp_uploads_mime_type_supported( $mime_type ) ) { - $this->markTestSkipped( "Mime type $mime_type is not supported." ); - } - $this->set_image_output_type( $image_type ); - - update_option( 'perflab_generate_webp_and_jpeg', true ); - $attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/leaves.jpg' ); - - // There should be JPEG and mime_type sources for the full image. - $this->assertImageHasSource( $attachment_id, 'image/jpeg' ); - $this->assertImageHasSource( $attachment_id, $mime_type ); - - $metadata = wp_get_attachment_metadata( $attachment_id ); - - // The full image should be a JPEG. - $this->assertArrayHasKey( 'file', $metadata ); - $this->assertStringEndsWith( $metadata['sources']['image/jpeg']['file'], $metadata['file'] ); - $this->assertStringEndsWith( $metadata['sources']['image/jpeg']['file'], get_attached_file( $attachment_id ) ); - - // The post MIME type should be JPEG. - $this->assertSame( 'image/jpeg', get_post_mime_type( $attachment_id ) ); - - // There should be JPEG and WebP sources for all sizes. - foreach ( array_keys( $metadata['sizes'] ) as $size_name ) { - $this->assertImageHasSizeSource( $attachment_id, $size_name, 'image/jpeg' ); - $this->assertImageHasSizeSource( $attachment_id, $size_name, $mime_type ); - } - } - /** * Create JPEG and output format for JPEG images, if perflab_generate_webp_and_jpeg option set. * @@ -749,6 +723,20 @@ public function data_provider_supported_image_types(): array { ); } + /** + * Data provider for tests returns the supported image types to run the tests with and without threshold check. + * + * @return array> An array of valid image types. + */ + public function data_provider_supported_image_types_with_threshold(): array { + return array( + 'webp' => array( 'webp' ), + 'webp with 850 threshold' => array( 'webp', true ), + 'avif' => array( 'avif' ), + 'avif with 850 threshold' => array( 'avif', true ), + ); + } + /** * Prevent replacing an image if image was uploaded via external source or plugin. *