Skip to content

Commit

Permalink
Merge pull request #316 from WordPress/fix/288-featured-image-in-webp
Browse files Browse the repository at this point in the history
Replace the featured image with WebP version when available
  • Loading branch information
felixarntz committed May 2, 2022
2 parents 1b208da + 0046f65 commit ea67f4d
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 0 deletions.
16 changes: 16 additions & 0 deletions modules/images/webp-uploads/load.php
Original file line number Diff line number Diff line change
Expand Up @@ -502,3 +502,19 @@ function webp_uploads_img_tag_update_mime_type( $image, $context, $attachment_id

return $image;
}

/**
* Updates the references of the featured image to the a new image format if available, in the same way it
* occurs in the_content of a post.
*
* @since n.e.x.t
*
* @param string $html The current HTML markup of the featured image.
* @param int $post_id The current post ID where the featured image is requested.
* @param int $attachment_id The ID of the attachment image.
* @return string The updated HTML markup.
*/
function webp_uploads_update_featured_image( $html, $post_id, $attachment_id ) {
return webp_uploads_img_tag_update_mime_type( $html, 'post_thumbnail_html', $attachment_id );
}
add_filter( 'post_thumbnail_html', 'webp_uploads_update_featured_image', 10, 3 );
17 changes: 17 additions & 0 deletions tests/modules/images/webp-uploads/load-tests.php
Original file line number Diff line number Diff line change
Expand Up @@ -662,4 +662,21 @@ function( $data, $attachment_id ) {
$this->assertNotContains( 'image/invalid', $mime_types );
$this->assertSame( array( 'image/jpeg', 'image/webp' ), $mime_types );
}

/**
* Replace the featured image to WebP when requesting the featured image
*
* @test
*/
public function it_should_replace_the_featured_image_to_web_p_when_requesting_the_featured_image() {
$attachment_id = $this->factory->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/testdata/modules/images/paint.jpeg' );
$post_id = $this->factory()->post->create();
set_post_thumbnail( $post_id, $attachment_id );

$featured_image = get_the_post_thumbnail( $post_id );

$this->assertTrue( has_post_thumbnail( $post_id ) );
$this->assertStringContainsString( '.webp', $featured_image );
$this->assertStringNotContainsString( '.jpeg', $featured_image );
}
}

0 comments on commit ea67f4d

Please sign in to comment.