Skip to content

Strip shortcodes from the excerpt #1730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 8 commits into from
May 28, 2025
Merged
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
4 changes: 4 additions & 0 deletions .github/changelog/1730-from-description
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: patch
Type: fixed

Improved excerpt handling by removing shortcodes from summaries.
35 changes: 18 additions & 17 deletions includes/functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -1183,16 +1183,6 @@ function generate_post_summary( $post, $length = 500 ) {
return '';
}

$content = \sanitize_post_field( 'post_excerpt', $post->post_excerpt, $post->ID );

if ( $content ) {
/** This filter is documented in wp-includes/post-template.php */
return \apply_filters( 'the_excerpt', $content );
}

$content = \sanitize_post_field( 'post_content', $post->post_content, $post->ID );
$content_parts = \get_extended( $content );

/**
* Filters the excerpt more value.
*
Expand All @@ -1201,15 +1191,26 @@ function generate_post_summary( $post, $length = 500 ) {
$excerpt_more = \apply_filters( 'activitypub_excerpt_more', '[…]' );
$length = $length - strlen( $excerpt_more );

// Check for the <!--more--> tag.
if (
! empty( $content_parts['extended'] ) &&
! empty( $content_parts['main'] )
) {
$content = $content_parts['main'] . ' ' . $excerpt_more;
$length = null;
$content = \sanitize_post_field( 'post_excerpt', $post->post_excerpt, $post->ID );

if ( $content ) {
// Ignore length if excerpt is set.
$length = null;
} else {
$content = \sanitize_post_field( 'post_content', $post->post_content, $post->ID );
$content_parts = \get_extended( $content );

// Check for the <!--more--> tag.
if (
! empty( $content_parts['extended'] ) &&
! empty( $content_parts['main'] )
) {
$content = \trim( $content_parts['main'] ) . ' ' . $excerpt_more;
$length = null;
}
}

$content = \strip_shortcodes( $content );
$content = \html_entity_decode( $content );
$content = \wp_strip_all_tags( $content );
$content = \trim( $content );
Expand Down
128 changes: 128 additions & 0 deletions tests/includes/class-test-functions.php
Original file line number Diff line number Diff line change
Expand Up @@ -547,4 +547,132 @@ public function provide_wp_versions() {
),
);
}

/**
* Test generate_post_summary function.
*
* @covers \Activitypub\generate_post_summary
* @dataProvider get_post_summary_data
*
* @param string $desc The description of the test.
* @param object $post The post object.
* @param string $expected The expected summary.
* @param int $length The length of the summary.
*/
public function test_generate_post_summary( $desc, $post, $expected, $length = 500 ) {
\add_shortcode(
'activitypub_test_shortcode',
function () {
return 'mighty short code';
}
);

$post_id = \wp_insert_post( $post );

$this->assertEquals(
$expected,
\Activitypub\generate_post_summary( $post_id, $length ),
$desc
);

\wp_delete_post( $post_id, true );
\remove_shortcode( 'activitypub_test_shortcode' );
}

/**
* Data provider for test_generate_post_summary.
*
* @return array[]
*/
public function get_post_summary_data() {
return array(
array(
'Excerpt',
array(
'post_excerpt' => 'Hello World',
),
'<p>Hello World</p>' . PHP_EOL,
),
array(
'Content',
array(
'post_content' => 'Hello World',
),
'<p>Hello World</p>' . PHP_EOL,
),
array(
'Content with more tag',
array(
'post_content' => 'Hello World <!--more--> More',
),
'<p>Hello World […]</p>' . PHP_EOL,
),
array(
'Excerpt with shortcode',
array(
'post_excerpt' => 'Hello World [activitypub_test_shortcode]',
),
'<p>Hello World</p>' . PHP_EOL,
),
array(
'Content with shortcode',
array(
'post_content' => 'Hello World [activitypub_test_shortcode]',
),
'<p>Hello World</p>' . PHP_EOL,
),
array(
'Excerpt more than limit',
array(
'post_excerpt' => 'Hello World Hello World Hello World Hello World Hello World',
),
'<p>Hello World Hello World Hello World Hello World Hello World</p>' . PHP_EOL,
10,
),
array(
'Content more than limit',
array(
'post_content' => 'Hello World Hello World Hello World Hello World Hello World',
),
'<p>Hello […]</p>' . PHP_EOL,
10,
),
array(
'Content more than limit with more tag',
array(
'post_content' => 'Hello World Hello <!--more--> World Hello World Hello World Hello World',
),
'<p>Hello World Hello […]</p>' . PHP_EOL,
1,
),
array(
'Test HTML content',
array(
'post_content' => '<p>Hello World</p>',
),
'<p>Hello World</p>' . PHP_EOL,
),
array(
'Test HTML content with anchor',
array(
'post_content' => 'Hello <a href="https://example.com">World</a>',
),
'<p>Hello World</p>' . PHP_EOL,
),
array(
'Test HTML excerpt',
array(
'post_excerpt' => '<p>Hello World</p>',
),
'<p>Hello World</p>' . PHP_EOL,
),
array(
'Test HTML excerpt with anchor',
array(
'post_excerpt' => 'Hello <a href="https://example.com">World</a>',
),
'<p>Hello World</p>' . PHP_EOL,
),
);
}
}
Loading