diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index 28b65135a0..45922d6f65 100644 --- a/src/wp-includes/formatting.php +++ b/src/wp-includes/formatting.php @@ -3808,7 +3808,7 @@ function human_time_diff( $from, $to = 0 ) { function wp_trim_excerpt( $text = '', $post = null ) { $raw_excerpt = $text; - if ( '' === $text ) { + if ( '' === trim( $text ) ) { $post = get_post( $post ); $text = get_the_content( '', false, $post ); diff --git a/tests/phpunit/tests/formatting/WpTrimExcerpt.php b/tests/phpunit/tests/formatting/WpTrimExcerpt.php index b2c026a957..01353c8e22 100644 --- a/tests/phpunit/tests/formatting/WpTrimExcerpt.php +++ b/tests/phpunit/tests/formatting/WpTrimExcerpt.php @@ -66,4 +66,19 @@ class Tests_Formatting_WpTrimExcerpt extends WP_UnitTestCase { } } } + + /** + * @ticket 51042 + */ + public function test_should_generate_excerpt_for_empty_values() { + $post = self::factory()->post->create( + array( + 'post_content' => 'Post content', + ) + ); + + $this->assertSame( 'Post content', wp_trim_excerpt( '', $post ) ); + $this->assertSame( 'Post content', wp_trim_excerpt( null, $post ) ); + $this->assertSame( 'Post content', wp_trim_excerpt( false, $post ) ); + } }