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 5 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
25 changes: 17 additions & 8 deletions plugins/webp-uploads/hooks.php
Original file line number Diff line number Diff line change
Expand Up @@ -52,21 +52,30 @@
* } 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.
$file = get_attached_file( $attachment_id, true );
// File does not exist.
if ( false === $file || ! file_exists( $file ) ) {
return $metadata;
}

/*
* 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 );
if ( isset( $filetype['type'] ) ) {
$mime_type = $filetype['type'];
} else {
$mime_type = get_post_mime_type( $attachment_id );
}
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved

$valid_mime_transforms = webp_uploads_get_upload_image_mime_transforms();

// 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 ] ) ) {
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
return $metadata;
}

$file = get_attached_file( $attachment_id, true );
// File does not exist.
if ( false === $file || ! file_exists( $file ) ) {
return $metadata;
}

// Make sure the top level `sources` key is a valid array.
if ( ! isset( $metadata['sources'] ) || ! is_array( $metadata['sources'] ) ) {
$metadata['sources'] = array();
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 $apply_threshold = false ): void {
mukeshpanchal27 marked this conversation as resolved.
Show resolved Hide resolved
$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 ( $apply_threshold ) {
// 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
Loading