Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
68 changes: 46 additions & 22 deletions src/wp-includes/post-template.php
Original file line number Diff line number Diff line change
Expand Up @@ -410,32 +410,56 @@
* @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 );

Check failure on line 415 in src/wp-includes/post-template.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Tabs must be used to indent lines; spaces are not allowed
if ( ! $post ) {

Check failure on line 416 in src/wp-includes/post-template.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Tabs must be used to indent lines; spaces are not allowed
return '';

Check failure on line 417 in src/wp-includes/post-template.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Tabs must be used to indent lines; spaces are not allowed
}

Check failure on line 418 in src/wp-includes/post-template.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Tabs must be used to indent lines; spaces are not allowed

/**

Check failure on line 420 in src/wp-includes/post-template.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Tabs must be used to indent lines; spaces are not allowed
* Filters the post excerpt before it is returned.

Check failure on line 421 in src/wp-includes/post-template.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Tabs must be used to indent lines; spaces are not allowed
*

Check failure on line 422 in src/wp-includes/post-template.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Tabs must be used to indent lines; spaces are not allowed
* @since 1.2.0

Check failure on line 423 in src/wp-includes/post-template.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Tabs must be used to indent lines; spaces are not allowed
*

Check failure on line 424 in src/wp-includes/post-template.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Tabs must be used to indent lines; spaces are not allowed
* @param string $text The post excerpt.

Check failure on line 425 in src/wp-includes/post-template.php

View workflow job for this annotation

GitHub Actions / Coding standards / PHP checks

Tabs must be used to indent lines; spaces are not allowed
* @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.
*
Expand Down
17 changes: 16 additions & 1 deletion tests/phpunit/tests/formatting/wpTrimWords.php
Original file line number Diff line number Diff line change
Expand Up @@ -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.';

Expand Down Expand Up @@ -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 )
);
}
}
21 changes: 21 additions & 0 deletions tests/phpunit/tests/post/get-excerpt.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Test that escaped shortcodes do not expand in excerpts.
*/

class Tests_Post_GetExcerpt extends WP_UnitTestCase {

function test_escaped_shortcode_not_expanded() {
$post_id = self::factory()->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 <div class="gallery">
$this->assertStringNotContainsString('<div class="gallery">', $excerpt);
}
}
Loading