From 7cccf527fa0558a2f1ee8502e72dfe12b48f9fa6 Mon Sep 17 00:00:00 2001 From: Igor Rozum Date: Fri, 23 Oct 2026 11:04:43 -0400 Subject: [PATCH] Comments: Pass an $is_trimmed argument to the get_comment_excerpt filter --- src/wp-includes/comment-template.php | 5 +- .../tests/comment/getCommentExcerpt.php | 48 +++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php index cc2608d4220e1..1187242c3bc3f 100644 --- a/src/wp-includes/comment-template.php +++ b/src/wp-includes/comment-template.php @@ -678,18 +678,21 @@ function get_comment_excerpt( $comment_id = 0 ) { $comment_excerpt_length = apply_filters( 'comment_excerpt_length', $comment_excerpt_length ); $comment_excerpt = wp_trim_words( $comment_text, $comment_excerpt_length, '…' ); + $is_trimmed = wp_trim_words( $comment_text, $comment_excerpt_length, '' ) !== $comment_excerpt; /** * Filters the retrieved comment excerpt. * * @since 1.5.0 * @since 4.1.0 The `$comment_id` and `$comment` parameters were added. + * @since 7.2.0 The `$is_trimmed` parameter was added. * * @param string $comment_excerpt The comment excerpt text. * @param string $comment_id The comment ID as a numeric string. * @param WP_Comment $comment The comment object. + * @param bool $is_trimmed Whether the comment text was trimmed to create the excerpt. */ - return apply_filters( 'get_comment_excerpt', $comment_excerpt, $comment->comment_ID, $comment ); + return apply_filters( 'get_comment_excerpt', $comment_excerpt, $comment->comment_ID, $comment, $is_trimmed ); } /** diff --git a/tests/phpunit/tests/comment/getCommentExcerpt.php b/tests/phpunit/tests/comment/getCommentExcerpt.php index 0a6455054a1b9..06f5063379613 100644 --- a/tests/phpunit/tests/comment/getCommentExcerpt.php +++ b/tests/phpunit/tests/comment/getCommentExcerpt.php @@ -43,4 +43,52 @@ public function _filter_comment_excerpt_length() { return 10; } + + /** + * @ticket 43792 + * + * @dataProvider data_get_comment_excerpt_is_trimmed + * + * @param string $content Comment content. + * @param int $length Excerpt length in words. + * @param bool $expected Expected value of the `$is_trimmed` filter argument. + */ + public function test_get_comment_excerpt_filter_receives_is_trimmed( $content, $length, $expected ) { + $comment_id = self::factory()->comment->create( + array( + 'comment_content' => $content, + ) + ); + + $is_trimmed = null; + add_filter( + 'get_comment_excerpt', + static function ( $excerpt, $id, $comment, $trimmed ) use ( &$is_trimmed ) { + $is_trimmed = $trimmed; + return $excerpt; + }, + 10, + 4 + ); + add_filter( + 'comment_excerpt_length', + static function () use ( $length ) { + return $length; + } + ); + + get_comment_excerpt( $comment_id ); + + $this->assertSame( $expected, $is_trimmed ); + } + + public function data_get_comment_excerpt_is_trimmed() { + return array( + 'long comment' => array( self::$bacon_comment, 20, true ), + 'short comment' => array( 'Short comment.', 20, false ), + 'word count equals the limit' => array( 'one two three four five', 5, false ), + 'word count exceeds the limit' => array( 'one two three four five six', 5, true ), + 'short comment ending in an ellipsis' => array( 'Wait for it…', 20, false ), + ); + } }