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
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