Skip to content

Commit

Permalink
Merge pull request #195 from WordPress/feature/174-full-image-size-in…
Browse files Browse the repository at this point in the history
…-content

Replace `the_content` with the full size image in new mime types if available
  • Loading branch information
mitogh authored Mar 10, 2022
2 parents 6b48480 + 9bc5e29 commit d70c915
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
11 changes: 7 additions & 4 deletions modules/images/webp-uploads/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -554,17 +554,19 @@ function webp_uploads_img_tag_update_mime_type( $image, $context, $attachment_id

$basename = wp_basename( $metadata['file'] );
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 );
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 @@ -587,6 +589,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

0 comments on commit d70c915

Please sign in to comment.