[wp-trac] [WordPress Trac] #55510: The `wp_content_img_tag` filter can appear to run twice when used to wrap image tags

WordPress Trac noreply at wordpress.org
Thu Mar 31 23:43:05 UTC 2022


#55510: The `wp_content_img_tag` filter can appear to run twice when used to wrap
image tags
------------------------------------------+-----------------------
 Reporter:  peterwilsoncc                 |       Owner:  flixos90
     Type:  defect (bug)                  |      Status:  assigned
 Priority:  normal                        |   Milestone:  6.0
Component:  Media                         |     Version:
 Severity:  normal                        |  Resolution:
 Keywords:  needs-patch needs-unit-tests  |     Focuses:
------------------------------------------+-----------------------

Comment (by peterwilsoncc):

 Three unit tests, two with the same image included twice under certain
 circumstances, one with the `wp_filter_content_tags()` function running on
 the content multiple times.

 Please check there is not a situation in WP in which the
 `wp_filter_content_tags()` function will run twice on the same blob of
 content. I do wonder if any sites run it on save to avoid the performance
 hit, but I'd expect them to disable it on render.

 {{{#!php
 <?php
 /**
  * Test an image included twice that already contains the attributes WP
 adds.
  *
  * @ticket 55347
  */
 public function
 test_wp_filter_content_tags_with_identical_images_custom_attributes() {
         $img         = get_image_tag( self::$large_id, '', '', '',
 'medium' );
         $img         = str_replace( '<img ', '<img srcset="custom"
 sizes="custom" loading="custom" ', $img );
         $the_content = "$img\n\n$img";

         add_filter(
                 'wp_content_img_tag',
                 function( $filtered_image ) {
                         return "<span>$filtered_image</span>";
                 }
         );

         $actual = wp_filter_content_tags( $the_content );
         $this->assertStringNotContainsString( '<span><span>', $actual );
 }

 /**
  * Test an image included twice when attribute modification is disabled.
  *
  * @ticket 55347
  */
 public function
 test_wp_filter_content_tags_with_identical_images_disabled_core_filters()
 {
         $img         = get_image_tag( self::$large_id, '', '', '',
 'medium' );
         $the_content = "$img\n\n$img";

         add_filter( 'wp_lazy_loading_enabled', '__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>";
                 }
         );

         $actual = wp_filter_content_tags( $the_content );
         $this->assertStringNotContainsString( '<span><span>', $actual );
 }

 /**
  * Test wrapping an image when the `wp_filter_content_tags()` function
 runs multiple times.
  *
  * @ticket 55347
  */
 public function test_wp_filter_content_tags_with_the_function_run_twice()
 {
         $the_content = get_image_tag( self::$large_id, '', '', '',
 'medium' );

         add_filter(
                 'wp_content_img_tag',
                 function( $filtered_image ) {
                         return "<span>$filtered_image</span>";
                 }
         );

         $actual = wp_filter_content_tags( wp_filter_content_tags(
 $the_content ) );
         $this->assertStringNotContainsString( '<span><span>', $actual );
 }

 }}}

-- 
Ticket URL: <https://core.trac.wordpress.org/ticket/55510#comment:1>
WordPress Trac <https://core.trac.wordpress.org/>
WordPress publishing platform


More information about the wp-trac mailing list