Skip to content

Commit 4f17dd7

Browse files
westonruterb1ink0adamsilverstein1ucay
authored
Merge pull request #2179 from b1ink0/fix/post-thumbnail-missing-picture-element
Fix missing `PICTURE` element support for post thumbnail and add missing Modern Image Formats support for Widget Block Co-authored-by: b1ink0 <[email protected]> Co-authored-by: adamsilverstein <[email protected]> Co-authored-by: westonruter <[email protected]> Co-authored-by: 1ucay <[email protected]>
2 parents 4db03c2 + 65b198c commit 4f17dd7

File tree

4 files changed

+110
-1
lines changed

4 files changed

+110
-1
lines changed

plugins/webp-uploads/hooks.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -673,6 +673,9 @@ function webp_uploads_img_tag_update_mime_type( string $original_image, string $
673673
* @return string The updated HTML markup.
674674
*/
675675
function webp_uploads_update_featured_image( string $html, int $post_id, int $attachment_id ): string {
676+
if ( webp_uploads_is_picture_element_enabled() ) {
677+
return webp_uploads_wrap_image_in_picture( $html, 'post_thumbnail_html', $attachment_id );
678+
}
676679
return webp_uploads_img_tag_update_mime_type( $html, 'post_thumbnail_html', $attachment_id );
677680
}
678681
add_filter( 'post_thumbnail_html', 'webp_uploads_update_featured_image', 10, 3 );

plugins/webp-uploads/picture-element.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
* @return string The new image tag.
2020
*/
2121
function webp_uploads_wrap_image_in_picture( string $image, string $context, int $attachment_id ): string {
22-
if ( 'the_content' !== $context ) {
22+
if ( ! in_array( $context, array( 'the_content', 'post_thumbnail_html', 'widget_block_content' ), true ) ) {
2323
return $image;
2424
}
2525

plugins/webp-uploads/tests/test-load.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1268,4 +1268,44 @@ static function () {
12681268
);
12691269
$this->assertSameSets( $file, webp_uploads_convert_palette_png_to_truecolor( $file ) );
12701270
}
1271+
1272+
/**
1273+
* Test that the webp_uploads_update_featured_image function is hooked to the post_thumbnail_html filter.
1274+
*/
1275+
public function test_webp_uploads_update_featured_image_hooked_into_post_thumbnail_html(): void {
1276+
$this->assertSame( 10, has_filter( 'post_thumbnail_html', 'webp_uploads_update_featured_image' ) );
1277+
}
1278+
1279+
/**
1280+
* Test that the featured image is not wrapped in a picture element.
1281+
*
1282+
* @covers ::webp_uploads_update_featured_image
1283+
* @covers ::webp_uploads_img_tag_update_mime_type
1284+
*/
1285+
public function test_webp_uploads_update_featured_image_picture_element_disabled(): void {
1286+
$attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/car.jpeg' );
1287+
$post_id = self::factory()->post->create();
1288+
set_post_thumbnail( $post_id, $attachment_id );
1289+
1290+
$featured_image = get_the_post_thumbnail( $post_id );
1291+
$this->assertStringStartsWith( '<img ', $featured_image );
1292+
}
1293+
1294+
/**
1295+
* Test that the featured image is wrapped in a picture element.
1296+
*
1297+
* @covers ::webp_uploads_update_featured_image
1298+
* @covers ::webp_uploads_wrap_image_in_picture
1299+
*/
1300+
public function test_webp_uploads_update_featured_image_picture_element_enabled(): void {
1301+
update_option( 'perflab_generate_webp_and_jpeg', '1' );
1302+
$this->opt_in_to_picture_element();
1303+
1304+
$attachment_id = self::factory()->attachment->create_upload_object( TESTS_PLUGIN_DIR . '/tests/data/images/car.jpeg' );
1305+
$post_id = self::factory()->post->create();
1306+
set_post_thumbnail( $post_id, $attachment_id );
1307+
1308+
$featured_image = get_the_post_thumbnail( $post_id );
1309+
$this->assertStringStartsWith( '<picture ', $featured_image );
1310+
}
12711311
}

plugins/webp-uploads/tests/test-picture-element.php

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -526,4 +526,70 @@ public function test_wrap_image_in_picture_with_false_image_src(): void {
526526

527527
$this->assertSame( $image, $filtered_image );
528528
}
529+
530+
/**
531+
* Test that images are not wrapped in picture element for unsupported contexts.
532+
*
533+
* @dataProvider data_provider_webp_uploads_wrap_image_in_picture_with_different_context
534+
*
535+
* @param string $context The context to test.
536+
* @param bool $expected Whether the image should be wrapped in a picture element.
537+
*
538+
* @covers ::webp_uploads_wrap_image_in_picture
539+
*/
540+
public function test_webp_uploads_wrap_image_in_picture_with_different_context( string $context, bool $expected ): void {
541+
$image = wp_get_attachment_image(
542+
self::$image_id,
543+
'large',
544+
false,
545+
array(
546+
'class' => 'wp-image-' . self::$image_id,
547+
'alt' => 'Green Leaves',
548+
)
549+
);
550+
551+
$this->opt_in_to_picture_element();
552+
$filtered_image = apply_filters( 'wp_content_img_tag', $image, $context, self::$image_id );
553+
if ( $expected ) {
554+
$processor = new WP_HTML_Tag_Processor( $filtered_image );
555+
$this->assertTrue( $processor->next_tag() );
556+
$this->assertSame( 'PICTURE', $processor->get_tag() );
557+
$this->assertTrue( $processor->next_tag() );
558+
$this->assertSame( 'SOURCE', $processor->get_tag() );
559+
$this->assertTrue( $processor->next_tag() );
560+
$this->assertSame( 'IMG', $processor->get_tag() );
561+
} else {
562+
$this->assertSame( $image, $filtered_image );
563+
}
564+
}
565+
566+
/**
567+
* Data provider for test_webp_uploads_wrap_image_in_picture_with_different_context.
568+
*
569+
* @return array<string, array{ context: string, expected: bool }>
570+
*/
571+
public function data_provider_webp_uploads_wrap_image_in_picture_with_different_context(): array {
572+
return array(
573+
'the_content' =>
574+
array(
575+
'context' => 'the_content',
576+
'expected' => true,
577+
),
578+
'post_thumbnail_html' =>
579+
array(
580+
'context' => 'post_thumbnail_html',
581+
'expected' => true,
582+
),
583+
'widget_block_content' =>
584+
array(
585+
'context' => 'widget_block_content',
586+
'expected' => true,
587+
),
588+
'invalid_context' =>
589+
array(
590+
'context' => 'invalid_context',
591+
'expected' => false,
592+
),
593+
);
594+
}
529595
}

0 commit comments

Comments
 (0)