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

Allow developers to select which image format to use for images in the content #230

Merged
merged 9 commits into from Mar 18, 2022
29 changes: 26 additions & 3 deletions modules/images/webp-uploads/load.php
Expand Up @@ -599,11 +599,34 @@ function webp_uploads_img_tag_update_mime_type( $image, $context, $attachment_id
return $image;
}

$urls = $matches[0];
// TODO: Add a filterable option to change the selected mime type. See https://github.com/WordPress/performance/issues/187.
$target_mime = 'image/webp';
/**
* Filters mime types that should be used to update all images in the content. The order of
* mime types matters. The last mime type in the list will be used if it is supported by an image.
adamsilverstein marked this conversation as resolved.
Show resolved Hide resolved
*
* @since n.e.x.t
*
* @param array $target_mimes The list of mime types that can be used to update images in the content.
* @param int $attachment_id The attachment ID.
* @param string $context The current context.
*/
$target_mimes = apply_filters( 'webp_uploads_content_image_mimes', array( 'image/jpeg', 'image/webp' ), $attachment_id, $context );

$target_mime = null;
// Look for the most progressive image format first.
$target_mimes = array_reverse( $target_mimes );
eugene-manuilov marked this conversation as resolved.
Show resolved Hide resolved
foreach ( $target_mimes as $mime ) {
eugene-manuilov marked this conversation as resolved.
Show resolved Hide resolved
if ( isset( $metadata['sources'][ $mime ] ) ) {
$target_mime = $mime;
break;
}
}

if ( null === $target_mime ) {
return $image;
}
felixarntz marked this conversation as resolved.
Show resolved Hide resolved

$basename = wp_basename( $metadata['file'] );
$urls = $matches[0];
foreach ( $urls as $url ) {
$src_filename = wp_basename( $url );

Expand Down
25 changes: 25 additions & 0 deletions tests/modules/images/webp-uploads/webp-uploads-test.php
Expand Up @@ -513,6 +513,31 @@ public function it_should_replace_the_references_to_a_jpg_image_to_a_webp_versio
$this->assertSame( $expected_tag, webp_uploads_img_tag_update_mime_type( $tag, 'the_content', $attachment_id ) );
}

/**
* Should not replace jpeg images in the content if other mime types are disabled via filter.
*
* @dataProvider provider_replace_images_with_different_extensions
* @group webp_uploads_update_image_references
*
* @test
*/
public function it_should_not_replace_the_references_to_a_jpg_image_when_disabled_via_filter( $image_path ) {
remove_all_filters( 'webp_uploads_content_image_mimes' );

add_filter(
'webp_uploads_content_image_mimes',
function( $mime_types ) {
unset( $mime_types[ array_search( 'image/webp', $mime_types, true ) ] );
return $mime_types;
}
);

$attachment_id = $this->factory->attachment->create_upload_object( $image_path );
$tag = wp_get_attachment_image( $attachment_id, 'medium', false, array( 'class' => "wp-image-{$attachment_id}" ) );

$this->assertSame( $tag, webp_uploads_img_tag_update_mime_type( $tag, 'the_content', $attachment_id ) );
}

public function provider_replace_images_with_different_extensions() {
yield 'An image with a .jpg extension' => array( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' );
yield 'An image with a .jpeg extension' => array( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/car.jpeg' );
Expand Down