Media: Run the wp_content_img_tag filter once per image.

Prevent multiple identical `img` tags in a block of content causing the `wp_content_img_tag` filter to fire multiple times for that image.

Follow up to [53028].

Props superpoincare, flixos90, pbearne, peterwilsoncc.
Fixes #55510.
See #55347.


git-svn-id: https://develop.svn.wordpress.org/trunk@53149 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Peter Wilson
2022-04-12 05:16:46 +00:00
parent b9556e457f
commit a2b13439ea
2 changed files with 79 additions and 0 deletions
+12
View File
@@ -1857,6 +1857,12 @@ function wp_filter_content_tags( $content, $context = null ) {
if ( $filtered_image !== $match[0] ) {
$content = str_replace( $match[0], $filtered_image, $content );
}
/*
* Unset image lookup to not run the same logic again unnecessarily if the same image tag is used more than
* once in the same blob of content.
*/
unset( $images[ $match[0] ] );
}
// Filter an iframe match.
@@ -1871,6 +1877,12 @@ function wp_filter_content_tags( $content, $context = null ) {
if ( $filtered_iframe !== $match[0] ) {
$content = str_replace( $match[0], $filtered_iframe, $content );
}
/*
* Unset iframe lookup to not run the same logic again unnecessarily if the same iframe tag is used more
* than once in the same blob of content.
*/
unset( $iframes[ $match[0] ] );
}
}
+67
View File
@@ -2304,6 +2304,73 @@ EOF;
wp_filter_content_tags( $img_tag_1 );
$this->assertSame( 1, $filter->get_call_count() );
}
/**
* @ticket 55510
* @covers ::wp_filter_content_tags
*/
public function test_wp_filter_content_tags_handles_duplicate_img_and_iframe_tags_once() {
$img = get_image_tag( self::$large_id, '', '', '', 'large' );
$iframe = '<iframe src="https://www.example.com" width="640" height="360"></iframe>';
$content = "$img\n$img\n$iframe\n$iframe";
// Record how often one of the available img and iframe filters is run.
// Both images and iframes support lazy-loading, so that's why this is used here.
$img_filter = new MockAction();
add_filter( 'wp_img_tag_add_loading_attr', array( &$img_filter, 'filter' ) );
$iframe_filter = new MockAction();
add_filter( 'wp_iframe_tag_add_loading_attr', array( &$iframe_filter, 'filter' ) );
// Ensure the img and iframe filters only ran once because the content is a single duplicated img tag and a
// single duplicate iframe tag.
wp_filter_content_tags( $content );
$this->assertSame( 1, $img_filter->get_call_count() );
$this->assertSame( 1, $iframe_filter->get_call_count() );
}
/**
* @ticket 55510
* @covers ::wp_filter_content_tags
*/
public function test_wp_filter_content_tags_filter_with_identical_image_tags_custom_attributes() {
$img = get_image_tag( self::$large_id, '', '', '', 'large' );
$img = str_replace( '<img ', '<img srcset="custom" sizes="custom" loading="custom" ', $img );
$content = "$img\n$img";
add_filter(
'wp_content_img_tag',
function( $filtered_image ) {
return "<span>$filtered_image</span>";
}
);
// Ensure there is no duplicate <span> wrapping the image.
$this->assertStringNotContainsString( '<span><span><img ', wp_filter_content_tags( $content ) );
}
/**
* @ticket 55510
* @covers ::wp_filter_content_tags
*/
public function test_wp_filter_content_tags_filter_with_identical_image_tags_disabled_core_filters() {
$img = get_image_tag( self::$large_id, '', '', '', 'large' );
$content = "$img\n$img";
add_filter( 'wp_img_tag_add_loading_attr', '__return_false' );
add_filter( 'wp_img_tag_add_width_and_height_attr', '__return_false' );
add_filter( 'wp_img_tag_add_srcset_and_sizes_attr', '__return_false' );
add_filter(
'wp_content_img_tag',
function( $filtered_image ) {
return "<span>$filtered_image</span>";
}
);
// Ensure the output has both instances of the image wrapped with a single <span>.
$this->assertSame( "<span>$img</span>\n<span>$img</span>", wp_filter_content_tags( $content ) );
}
/**
* @ticket 33641
* @ticket 34528