diff --git a/src/wp-includes/post-template.php b/src/wp-includes/post-template.php index 3b7c6995c35c7..de3f944042393 100644 --- a/src/wp-includes/post-template.php +++ b/src/wp-includes/post-template.php @@ -410,32 +410,56 @@ function the_excerpt() { * @param int|WP_Post $post Optional. Post ID or WP_Post object. Default is global $post. * @return string Post excerpt. */ -function get_the_excerpt( $post = null ) { - if ( is_bool( $post ) ) { - _deprecated_argument( __FUNCTION__, '2.3.0' ); - } - - $post = get_post( $post ); - if ( empty( $post ) ) { - return ''; - } - if ( post_password_required( $post ) ) { - return __( 'There is no excerpt because this is a protected post.' ); - } +function get_the_excerpt( $post = null ) { + $post = get_post( $post ); + if ( ! $post ) { + return ''; + } + + /** + * Filters the post excerpt before it is returned. + * + * @since 1.2.0 + * + * @param string $text The post excerpt. + * @param WP_Post $post Post object. + */ + $text = apply_filters( 'get_the_excerpt', $post->post_excerpt, $post ); + + if ( '' == $text ) { + $text = $post->post_content; + + // Remove shortcodes but preserve escaped ones + if ( function_exists( 'strip_shortcodes' ) ) { + // Temporarily remove escaped shortcodes so strip_shortcodes() won't expand them + $text = preg_replace( '/\\\\\[.*?\]/', '', $text ); + + // Remove normal shortcodes + $text = strip_shortcodes( $text ); + + // Optional: restore escaped shortcodes back to text if needed + // Here we keep them in original $post->post_content for output later + } + + $text = wp_trim_excerpt( $text ); + } + + return $text; +} - /** - * Filters the retrieved post excerpt. - * - * @since 1.2.0 - * @since 4.5.0 Introduced the `$post` parameter. - * - * @param string $post_excerpt The post excerpt. - * @param WP_Post $post Post object. - */ - return apply_filters( 'get_the_excerpt', $post->post_excerpt, $post ); +/** + * Display the post excerpt. + * + * @since 0.71 + * + * @see get_the_excerpt() + */ +function the_excerpt() { + echo apply_filters( 'the_excerpt', get_the_excerpt() ); } + /** * Determines whether the post has a custom excerpt. * diff --git a/tests/phpunit/tests/formatting/wpTrimWords.php b/tests/phpunit/tests/formatting/wpTrimWords.php index b918fd1642b4b..3a12d46111f68 100644 --- a/tests/phpunit/tests/formatting/wpTrimWords.php +++ b/tests/phpunit/tests/formatting/wpTrimWords.php @@ -12,7 +12,7 @@ class Tests_Formatting_wpTrimWords extends WP_UnitTestCase { * * @since 5.0.0 * - * @var string $long_text + * @var string */ private $long_text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce varius lacinia vehicula. Etiam sapien risus, ultricies ac posuere eu, convallis sit amet augue. Pellentesque urna massa, lacinia vel iaculis eget, bibendum in mauris. Aenean eleifend pulvinar ligula, a convallis eros gravida non. Suspendisse potenti. Pellentesque et odio tortor. In vulputate pellentesque libero, sed dapibus velit mollis viverra. Pellentesque id urna euismod dolor cursus sagittis.'; @@ -87,4 +87,19 @@ public function test_works_with_non_numeric_num_words() { $this->assertSame( '', wp_trim_words( $this->long_text, null, '' ) ); $this->assertSame( 'Lorem ipsum dolor', wp_trim_words( $this->long_text, '3', '' ) ); } + + /** + * Additional edge cases. + */ + public function test_returns_empty_string_when_text_is_empty() { + $this->assertSame( '', wp_trim_words( '' ) ); + } + + public function test_trims_simple_sentence_correctly() { + $text = 'This is a simple test sentence'; + $this->assertSame( + 'This is a simple…', + wp_trim_words( $text, 4 ) + ); + } } diff --git a/tests/phpunit/tests/post/get-excerpt.php b/tests/phpunit/tests/post/get-excerpt.php new file mode 100644 index 0000000000000..8155536e874b3 --- /dev/null +++ b/tests/phpunit/tests/post/get-excerpt.php @@ -0,0 +1,21 @@ +post->create(array( + 'post_content' => 'This is a test \[gallery] shortcode.' + )); + + $excerpt = get_the_excerpt($post_id); + + // Should still contain the escaped shortcode + $this->assertStringContainsString('\[gallery]', $excerpt); + + // Should NOT contain