close
Skip to content

Commit 105b69e

Browse files
authored
Merge pull request #230 from WordPress/feature/187-content-images-format
Allow developers to select which image format to use for images in the content
2 parents cd2f4e8 + 35eb759 commit 105b69e

2 files changed

Lines changed: 51 additions & 3 deletions

File tree

‎modules/images/webp-uploads/load.php‎

Lines changed: 26 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -599,11 +599,34 @@ function webp_uploads_img_tag_update_mime_type( $image, $context, $attachment_id
599599
return $image;
600600
}
601601

602-
$urls = $matches[0];
603-
// TODO: Add a filterable option to change the selected mime type. See https://github.com/WordPress/performance/issues/187.
604-
$target_mime = 'image/webp';
602+
/**
603+
* Filters mime types that should be used to update all images in the content. The order of
604+
* mime types matters. The last mime type in the list will be used if it is supported by an image.
605+
*
606+
* @since n.e.x.t
607+
*
608+
* @param array $target_mimes The list of mime types that can be used to update images in the content.
609+
* @param int $attachment_id The attachment ID.
610+
* @param string $context The current context.
611+
*/
612+
$target_mimes = apply_filters( 'webp_uploads_content_image_mimes', array( 'image/jpeg', 'image/webp' ), $attachment_id, $context );
613+
614+
$target_mime = null;
615+
// Look for the most progressive image format first.
616+
$target_mimes = array_reverse( $target_mimes );
617+
foreach ( $target_mimes as $mime ) {
618+
if ( isset( $metadata['sources'][ $mime ] ) ) {
619+
$target_mime = $mime;
620+
break;
621+
}
622+
}
623+
624+
if ( null === $target_mime ) {
625+
return $image;
626+
}
605627

606628
$basename = wp_basename( $metadata['file'] );
629+
$urls = $matches[0];
607630
foreach ( $urls as $url ) {
608631
$src_filename = wp_basename( $url );
609632

‎tests/modules/images/webp-uploads/webp-uploads-test.php‎

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -513,6 +513,31 @@ public function it_should_replace_the_references_to_a_jpg_image_to_a_webp_versio
513513
$this->assertSame( $expected_tag, webp_uploads_img_tag_update_mime_type( $tag, 'the_content', $attachment_id ) );
514514
}
515515

516+
/**
517+
* Should not replace jpeg images in the content if other mime types are disabled via filter.
518+
*
519+
* @dataProvider provider_replace_images_with_different_extensions
520+
* @group webp_uploads_update_image_references
521+
*
522+
* @test
523+
*/
524+
public function it_should_not_replace_the_references_to_a_jpg_image_when_disabled_via_filter( $image_path ) {
525+
remove_all_filters( 'webp_uploads_content_image_mimes' );
526+
527+
add_filter(
528+
'webp_uploads_content_image_mimes',
529+
function( $mime_types ) {
530+
unset( $mime_types[ array_search( 'image/webp', $mime_types, true ) ] );
531+
return $mime_types;
532+
}
533+
);
534+
535+
$attachment_id = $this->factory->attachment->create_upload_object( $image_path );
536+
$tag = wp_get_attachment_image( $attachment_id, 'medium', false, array( 'class' => "wp-image-{$attachment_id}" ) );
537+
538+
$this->assertSame( $tag, webp_uploads_img_tag_update_mime_type( $tag, 'the_content', $attachment_id ) );
539+
}
540+
516541
public function provider_replace_images_with_different_extensions() {
517542
yield 'An image with a .jpg extension' => array( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg' );
518543
yield 'An image with a .jpeg extension' => array( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/car.jpeg' );

0 commit comments

Comments
 (0)