Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix bug that would prevent uploaded images from being converted to the intended output format when having fallback formats enabled #1635

Merged
merged 7 commits into from
Nov 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 16 additions & 8 deletions plugins/webp-uploads/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
64 changes: 26 additions & 38 deletions plugins/webp-uploads/tests/test-load.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,16 +43,26 @@ 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 ) ) {
$this->markTestSkipped( "Mime type $mime_type is not supported." );
}
$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' );
Expand Down Expand Up @@ -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.
*
Expand Down Expand Up @@ -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<string, array<int, string|true>> 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.
*
Expand Down