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

Replace the full size image in the_content with additional MIME type if available #195

Merged
merged 10 commits into from
Mar 10, 2022
11 changes: 7 additions & 4 deletions modules/images/webp-uploads/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -544,17 +544,19 @@ function webp_uploads_img_tag_update_mime_type( $image, $context, $attachment_id

$basename = wp_basename( $metadata['file'] );
felixarntz marked this conversation as resolved.
Show resolved Hide resolved
foreach ( $urls as $url ) {
if ( isset( $metadata['file'] ) && strpos( $url, $basename ) !== false ) {
// TODO: we don't have a replacement for full image yet, issue. See: https://github.com/WordPress/performance/issues/174.
$src_filename = wp_basename( $url );

// Replace the full size image if present.
if ( isset( $metadata['sources'][ $target_mime ]['file'] ) && strpos( $url, $basename ) !== false ) {
$image = str_replace( $src_filename, $metadata['sources'][ $target_mime ]['file'], $image );
mitogh marked this conversation as resolved.
Show resolved Hide resolved
continue;
}

if ( empty( $metadata['sizes'] ) ) {
continue;
}

$src_filename = wp_basename( $url );
$extension = wp_check_filetype( $src_filename );
$extension = wp_check_filetype( $src_filename );
// Extension was not set properly no action possible or extension is already in the expected mime.
if ( empty( $extension['type'] ) || $extension['type'] === $target_mime ) {
continue;
Expand All @@ -577,6 +579,7 @@ function webp_uploads_img_tag_update_mime_type( $image, $context, $attachment_id
}

$image = str_replace( $src_filename, $size_data['sources'][ $target_mime ]['file'], $image );
break;
}
}

Expand Down
12 changes: 7 additions & 5 deletions tests/modules/images/webp-uploads/webp-uploads-test.php
Original file line number Diff line number Diff line change
Expand Up @@ -547,6 +547,8 @@ public function it_should_replace_the_references_to_a_jpg_image_to_a_webp_versio
$expected_tag = str_replace( $properties['sources']['image/jpeg']['file'], $properties['sources']['image/webp']['file'], $expected_tag );
}

$expected_tag = str_replace( $metadata['sources']['image/jpeg']['file'], $metadata['sources']['image/webp']['file'], $expected_tag );

$this->assertNotEmpty( $expected_tag );
$this->assertNotSame( $tag, $expected_tag );
$this->assertSame( $expected_tag, webp_uploads_img_tag_update_mime_type( $tag, 'the_content', $attachment_id ) );
Expand All @@ -558,13 +560,11 @@ public function provider_replace_images_with_different_extensions() {
}

/**
* Contain the full image size from the original mime
*
* @group webp_uploads_update_image_references
* Replace all the images including the full size image
*
* @test
*/
public function it_should_contain_the_full_image_size_from_the_original_mime() {
public function it_should_replace_all_the_images_including_the_full_size_image() {
$attachment_id = $this->factory->attachment->create_upload_object(
TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/leafs.jpg'
);
Expand All @@ -575,8 +575,10 @@ public function it_should_contain_the_full_image_size_from_the_original_mime() {
'ext' => 'jpg',
'type' => 'image/jpeg',
);
$metadata = wp_get_attachment_metadata( $attachment_id );
$this->assertSame( $expected, wp_check_filetype( get_attached_file( $attachment_id ) ) );
$this->assertContains( wp_basename( get_attached_file( $attachment_id ) ), webp_uploads_img_tag_update_mime_type( $tag, 'the_content', $attachment_id ) );
$this->assertNotContains( wp_basename( get_attached_file( $attachment_id ) ), webp_uploads_img_tag_update_mime_type( $tag, 'the_content', $attachment_id ) );
$this->assertContains( $metadata['sources']['image/webp']['file'], webp_uploads_img_tag_update_mime_type( $tag, 'the_content', $attachment_id ) );
}

/**
Expand Down