Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 7e2ab1c

Browse files
pfefferlematticbot
andauthoredMay 28, 2025··
Strip shortcodes from the excerpt (#1730)
Co-authored-by: Automattic Bot <[email protected]>
1 parent d5ace2b commit 7e2ab1c

File tree

3 files changed

+150
-17
lines changed

3 files changed

+150
-17
lines changed
 
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: patch
2+
Type: fixed
3+
4+
Improved excerpt handling by removing shortcodes from summaries.

‎includes/functions.php

Lines changed: 18 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,16 +1183,6 @@ function generate_post_summary( $post, $length = 500 ) {
11831183
return '';
11841184
}
11851185

1186-
$content = \sanitize_post_field( 'post_excerpt', $post->post_excerpt, $post->ID );
1187-
1188-
if ( $content ) {
1189-
/** This filter is documented in wp-includes/post-template.php */
1190-
return \apply_filters( 'the_excerpt', $content );
1191-
}
1192-
1193-
$content = \sanitize_post_field( 'post_content', $post->post_content, $post->ID );
1194-
$content_parts = \get_extended( $content );
1195-
11961186
/**
11971187
* Filters the excerpt more value.
11981188
*
@@ -1201,15 +1191,26 @@ function generate_post_summary( $post, $length = 500 ) {
12011191
$excerpt_more = \apply_filters( 'activitypub_excerpt_more', '[…]' );
12021192
$length = $length - strlen( $excerpt_more );
12031193

1204-
// Check for the <!--more--> tag.
1205-
if (
1206-
! empty( $content_parts['extended'] ) &&
1207-
! empty( $content_parts['main'] )
1208-
) {
1209-
$content = $content_parts['main'] . ' ' . $excerpt_more;
1210-
$length = null;
1194+
$content = \sanitize_post_field( 'post_excerpt', $post->post_excerpt, $post->ID );
1195+
1196+
if ( $content ) {
1197+
// Ignore length if excerpt is set.
1198+
$length = null;
1199+
} else {
1200+
$content = \sanitize_post_field( 'post_content', $post->post_content, $post->ID );
1201+
$content_parts = \get_extended( $content );
1202+
1203+
// Check for the <!--more--> tag.
1204+
if (
1205+
! empty( $content_parts['extended'] ) &&
1206+
! empty( $content_parts['main'] )
1207+
) {
1208+
$content = \trim( $content_parts['main'] ) . ' ' . $excerpt_more;
1209+
$length = null;
1210+
}
12111211
}
12121212

1213+
$content = \strip_shortcodes( $content );
12131214
$content = \html_entity_decode( $content );
12141215
$content = \wp_strip_all_tags( $content );
12151216
$content = \trim( $content );

‎tests/includes/class-test-functions.php

Lines changed: 128 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -547,4 +547,132 @@ public function provide_wp_versions() {
547547
),
548548
);
549549
}
550+
551+
/**
552+
* Test generate_post_summary function.
553+
*
554+
* @covers \Activitypub\generate_post_summary
555+
* @dataProvider get_post_summary_data
556+
*
557+
* @param string $desc The description of the test.
558+
* @param object $post The post object.
559+
* @param string $expected The expected summary.
560+
* @param int $length The length of the summary.
561+
*/
562+
public function test_generate_post_summary( $desc, $post, $expected, $length = 500 ) {
563+
\add_shortcode(
564+
'activitypub_test_shortcode',
565+
function () {
566+
return 'mighty short code';
567+
}
568+
);
569+
570+
$post_id = \wp_insert_post( $post );
571+
572+
$this->assertEquals(
573+
$expected,
574+
\Activitypub\generate_post_summary( $post_id, $length ),
575+
$desc
576+
);
577+
578+
\wp_delete_post( $post_id, true );
579+
\remove_shortcode( 'activitypub_test_shortcode' );
580+
}
581+
582+
/**
583+
* Data provider for test_generate_post_summary.
584+
*
585+
* @return array[]
586+
*/
587+
public function get_post_summary_data() {
588+
return array(
589+
array(
590+
'Excerpt',
591+
array(
592+
'post_excerpt' => 'Hello World',
593+
),
594+
'<p>Hello World</p>' . PHP_EOL,
595+
),
596+
array(
597+
'Content',
598+
array(
599+
'post_content' => 'Hello World',
600+
),
601+
'<p>Hello World</p>' . PHP_EOL,
602+
),
603+
array(
604+
'Content with more tag',
605+
array(
606+
'post_content' => 'Hello World <!--more--> More',
607+
),
608+
'<p>Hello World […]</p>' . PHP_EOL,
609+
),
610+
array(
611+
'Excerpt with shortcode',
612+
array(
613+
'post_excerpt' => 'Hello World [activitypub_test_shortcode]',
614+
),
615+
'<p>Hello World</p>' . PHP_EOL,
616+
),
617+
array(
618+
'Content with shortcode',
619+
array(
620+
'post_content' => 'Hello World [activitypub_test_shortcode]',
621+
),
622+
'<p>Hello World</p>' . PHP_EOL,
623+
),
624+
array(
625+
'Excerpt more than limit',
626+
array(
627+
'post_excerpt' => 'Hello World Hello World Hello World Hello World Hello World',
628+
),
629+
'<p>Hello World Hello World Hello World Hello World Hello World</p>' . PHP_EOL,
630+
10,
631+
),
632+
array(
633+
'Content more than limit',
634+
array(
635+
'post_content' => 'Hello World Hello World Hello World Hello World Hello World',
636+
),
637+
'<p>Hello […]</p>' . PHP_EOL,
638+
10,
639+
),
640+
array(
641+
'Content more than limit with more tag',
642+
array(
643+
'post_content' => 'Hello World Hello <!--more--> World Hello World Hello World Hello World',
644+
),
645+
'<p>Hello World Hello […]</p>' . PHP_EOL,
646+
1,
647+
),
648+
array(
649+
'Test HTML content',
650+
array(
651+
'post_content' => '<p>Hello World</p>',
652+
),
653+
'<p>Hello World</p>' . PHP_EOL,
654+
),
655+
array(
656+
'Test HTML content with anchor',
657+
array(
658+
'post_content' => 'Hello <a href="https://example.com">World</a>',
659+
),
660+
'<p>Hello World</p>' . PHP_EOL,
661+
),
662+
array(
663+
'Test HTML excerpt',
664+
array(
665+
'post_excerpt' => '<p>Hello World</p>',
666+
),
667+
'<p>Hello World</p>' . PHP_EOL,
668+
),
669+
array(
670+
'Test HTML excerpt with anchor',
671+
array(
672+
'post_excerpt' => 'Hello <a href="https://example.com">World</a>',
673+
),
674+
'<p>Hello World</p>' . PHP_EOL,
675+
),
676+
);
677+
}
550678
}

0 commit comments

Comments
 (0)
Please sign in to comment.