Changeset 55850
- Timestamp:
- 05/23/2023 06:23:59 PM (16 months ago)
- Location:
- trunk
- Files:
-
- 3 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/formatting.php
r55844 r55850 3938 3938 $text = excerpt_remove_blocks( $text ); 3939 3939 3940 /* 3941 * Temporarily unhook wp_filter_content_tags() since any tags 3942 * within the excerpt are stripped out. Modifying the tags here 3943 * is wasteful and can lead to bugs in the image counting logic. 3944 */ 3945 $filter_removed = remove_filter( 'the_content', 'wp_filter_content_tags' ); 3946 3940 3947 /** This filter is documented in wp-includes/post-template.php */ 3941 3948 $text = apply_filters( 'the_content', $text ); 3942 3949 $text = str_replace( ']]>', ']]>', $text ); 3950 3951 /** 3952 * Only restore the filter callback if it was removed above. The logic 3953 * to unhook and restore only applies on the default priority of 10, 3954 * which is generally used for the filter callback in WordPress core. 3955 */ 3956 if ( $filter_removed ) { 3957 add_filter( 'the_content', 'wp_filter_content_tags' ); 3958 } 3943 3959 3944 3960 /* translators: Maximum number of words used in a post excerpt. */ -
trunk/tests/phpunit/tests/formatting/wpTrimExcerpt.php
r53562 r55850 93 93 $this->assertSame( 'Post content', wp_trim_excerpt( false, $post ) ); 94 94 } 95 96 /** 97 * Tests that `wp_trim_excerpt()` unhooks `wp_filter_content_tags()` from 'the_content' filter. 98 * 99 * @ticket 56588 100 */ 101 public function test_wp_trim_excerpt_unhooks_wp_filter_content_tags() { 102 $post = self::factory()->post->create(); 103 104 /* 105 * Record that during 'the_content' filter run by wp_trim_excerpt() the 106 * wp_filter_content_tags() callback is not used. 107 */ 108 $has_filter = true; 109 add_filter( 110 'the_content', 111 static function( $content ) use ( &$has_filter ) { 112 $has_filter = has_filter( 'the_content', 'wp_filter_content_tags' ); 113 return $content; 114 } 115 ); 116 117 wp_trim_excerpt( '', $post ); 118 119 $this->assertFalse( $has_filter, 'wp_filter_content_tags() was not unhooked in wp_trim_excerpt()' ); 120 } 121 122 /** 123 * Tests that `wp_trim_excerpt()` doesn't permanently unhook `wp_filter_content_tags()` from 'the_content' filter. 124 * 125 * @ticket 56588 126 */ 127 public function test_wp_trim_excerpt_should_not_permanently_unhook_wp_filter_content_tags() { 128 $post = self::factory()->post->create(); 129 130 wp_trim_excerpt( '', $post ); 131 132 $this->assertSame( 10, has_filter( 'the_content', 'wp_filter_content_tags' ), 'wp_filter_content_tags() was not restored in wp_trim_excerpt()' ); 133 } 134 135 /** 136 * Tests that `wp_trim_excerpt()` doesn't restore `wp_filter_content_tags()` if it was previously unhooked. 137 * 138 * @ticket 56588 139 */ 140 public function test_wp_trim_excerpt_does_not_restore_wp_filter_content_tags_if_previously_unhooked() { 141 $post = self::factory()->post->create(); 142 143 // Remove wp_filter_content_tags() from 'the_content' filter generally. 144 remove_filter( 'the_content', 'wp_filter_content_tags' ); 145 146 wp_trim_excerpt( '', $post ); 147 148 // Assert that the filter callback was not restored after running 'the_content'. 149 $this->assertFalse( has_filter( 'the_content', 'wp_filter_content_tags' ) ); 150 } 95 151 } -
trunk/tests/phpunit/tests/media.php
r55847 r55850 74 74 wp_delete_attachment( self::$large_id, true ); 75 75 parent::tear_down_after_class(); 76 } 77 78 /** 79 * Ensures that the static content media count and related filter are reset between tests. 80 */ 81 public function set_up() { 82 parent::set_up(); 83 84 $this->reset_content_media_count(); 85 $this->reset_omit_loading_attr_filter(); 76 86 } 77 87 … … 3568 3578 3569 3579 $query = $this->get_new_wp_query_for_published_post(); 3570 $this->reset_content_media_count();3571 $this->reset_omit_loading_attr_filter();3572 3580 3573 3581 while ( have_posts() ) { … … 3614 3622 $query = $this->get_new_wp_query_for_published_post(); 3615 3623 $this->set_main_query( $query ); 3616 $this->reset_content_media_count();3617 $this->reset_omit_loading_attr_filter();3618 3624 3619 3625 // Use the filter to alter the threshold for not lazy-loading to the first five elements. … … 3656 3662 $query = $this->get_new_wp_query_for_published_post(); 3657 3663 $this->set_main_query( $query ); 3658 $this->reset_content_media_count();3659 $this->reset_omit_loading_attr_filter();3660 3664 3661 3665 while ( have_posts() ) { … … 3708 3712 3709 3713 $wp_query = $this->get_new_wp_query_for_published_post(); 3710 $this->reset_content_media_count();3711 $this->reset_omit_loading_attr_filter();3712 3714 3713 3715 do_action( 'get_header' ); … … 3733 3735 $wp_query = $this->get_new_wp_query_for_published_post(); 3734 3736 $this->set_main_query( $wp_query ); 3735 $this->reset_content_media_count();3736 $this->reset_omit_loading_attr_filter();3737 3737 3738 3738 // Lazy if header not called. … … 3756 3756 $wp_query = $this->get_new_wp_query_for_published_post(); 3757 3757 $this->set_main_query( $wp_query ); 3758 $this->reset_content_media_count();3759 $this->reset_omit_loading_attr_filter();3760 3758 3761 3759 do_action( 'get_header' ); … … 3779 3777 $wp_query = $this->get_new_wp_query_for_published_post(); 3780 3778 $this->set_main_query( $wp_query ); 3781 $this->reset_content_media_count();3782 $this->reset_omit_loading_attr_filter();3783 3779 3784 3780 do_action( 'get_header' ); … … 3806 3802 $wp_query = $this->get_new_wp_query_for_published_post(); 3807 3803 $this->set_main_query( $wp_query ); 3808 $this->reset_content_media_count();3809 $this->reset_omit_loading_attr_filter();3810 3804 3811 3805 // Ensure header and footer is called. … … 3866 3860 $wp_the_query = $wp_query; 3867 3861 $post = get_post( self::$post_ids['publish'] ); 3868 $this->reset_content_media_count();3869 $this->reset_omit_loading_attr_filter();3870 3862 3871 3863 $_wp_current_template_content = '<!-- wp:post-content /-->'; … … 3923 3915 $wp_the_query = $wp_query; 3924 3916 $post = get_post( self::$post_ids['publish'] ); 3925 $this->reset_content_media_count();3926 $this->reset_omit_loading_attr_filter();3927 3917 3928 3918 $_wp_current_template_content = '<!-- wp:post-featured-image /--> <!-- wp:post-content /-->'; … … 4014 4004 $wp_query = new WP_Query( array( 'post__in' => array( self::$post_ids['publish'] ) ) ); 4015 4005 $wp_the_query = $wp_query; 4016 $this->reset_content_media_count(); 4017 $this->reset_omit_loading_attr_filter(); 4006 4018 4007 $content = ''; 4019 4008 while ( have_posts() ) { … … 4077 4066 'wp_get_attachment_image' => array( 'context' => 'wp_get_attachment_image' ), 4078 4067 ); 4068 } 4069 4070 /** 4071 * Tests that the content media count is not affected by `the_excerpt()` calls for posts that contain images. 4072 * 4073 * @ticket 56588 4074 * 4075 * @covers ::wp_trim_excerpt 4076 */ 4077 public function test_the_excerpt_does_not_affect_content_media_count() { 4078 global $wp_query, $wp_the_query; 4079 4080 /* 4081 * Use the filter to alter the threshold for not lazy-loading to the first 2 elements, 4082 * then use a post that contains exactly 2 images. 4083 */ 4084 $this->force_omit_loading_attr_threshold( 2 ); 4085 $post_content = '<img src="example.jpg" width="800" height="600">'; 4086 $post_content .= '<p>Some text.</p>'; 4087 $post_content .= '<img src="example2.jpg" width="800" height="600">'; 4088 4089 $post_id = self::factory()->post->create( 4090 array( 4091 'post_content' => $post_content, 4092 'post_excerpt' => '', 4093 ) 4094 ); 4095 4096 $wp_query = new WP_Query( array( 'post__in' => array( $post_id ) ) ); 4097 $wp_the_query = $wp_query; 4098 4099 while ( have_posts() ) { 4100 the_post(); 4101 4102 // Call `the_excerpt()` without generating output. 4103 get_echo( 'the_excerpt' ); 4104 } 4105 4106 // The only way to access the value is by calling this function without increasing the value. 4107 $content_media_count = wp_increase_content_media_count( 0 ); 4108 4109 // Assert that the media count was not increased even though there are 3 images in the post's content. 4110 $this->assertSame( 0, $content_media_count ); 4111 } 4112 4113 /** 4114 * Tests that the lazy-loading result is not affected by `the_excerpt()` calls for posts that 4115 * contain images. 4116 * 4117 * Printing the excerpt for a post that contains images in its content prior to its featured image should result in 4118 * that featured image not being lazy-loaded, since the images in the post content aren't displayed in the excerpt. 4119 * 4120 * @ticket 56588 4121 * 4122 * @covers ::wp_trim_excerpt 4123 */ 4124 public function test_the_excerpt_does_not_affect_omit_lazy_loading_logic() { 4125 global $wp_query, $wp_the_query; 4126 4127 /* 4128 * Use the filter to alter the threshold for not lazy-loading to the first 2 elements, 4129 * then use a post that contains exactly 2 images. 4130 */ 4131 $this->force_omit_loading_attr_threshold( 2 ); 4132 $post_content = '<img src="example.jpg" width="800" height="600">'; 4133 $post_content .= '<p>Some text.</p>'; 4134 $post_content .= '<img src="example2.jpg" width="800" height="600">'; 4135 4136 $post_id = self::factory()->post->create( 4137 array( 4138 'post_content' => $post_content, 4139 'post_excerpt' => '', 4140 ) 4141 ); 4142 $featured_image_id = self::$large_id; 4143 update_post_meta( $post_id, '_thumbnail_id', $featured_image_id ); 4144 4145 $expected_image_tag = get_the_post_thumbnail( $post_id, 'post-thumbnail', array( 'loading' => false ) ); 4146 4147 $wp_query = new WP_Query( array( 'post__in' => array( $post_id ) ) ); 4148 $wp_the_query = $wp_query; 4149 4150 $output = ''; 4151 while ( have_posts() ) { 4152 the_post(); 4153 4154 // Print excerpt first, then the featured image. 4155 $output .= get_echo( 'the_excerpt' ); 4156 $output .= get_echo( 'the_post_thumbnail' ); 4157 } 4158 4159 $this->assertStringContainsString( $expected_image_tag, $output ); 4079 4160 } 4080 4161
Note: See TracChangeset
for help on using the changeset viewer.