From 55bb8b8c0bda751da28b7958568b80fef67a2a9d Mon Sep 17 00:00:00 2001 From: Scott Taylor Date: Thu, 24 Sep 2015 21:04:16 +0000 Subject: [PATCH] Comments: add a `'comment_excerpt_length'` filter to `get_comment_excerpt()`. Create the first ever unit tests for `get_comment_excerpt()`. Props dannydehaan, wonderboymusic. Fixes #27526. git-svn-id: https://develop.svn.wordpress.org/trunk@34520 602fd350-edb4-49c9-b593-d223f7449a82 --- src/wp-includes/comment-template.php | 30 +++++++------ .../tests/comment/getCommentExcerpt.php | 43 +++++++++++++++++++ 2 files changed, 60 insertions(+), 13 deletions(-) create mode 100644 tests/phpunit/tests/comment/getCommentExcerpt.php diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php index f3e55bf03e..06cc26e1f0 100644 --- a/src/wp-includes/comment-template.php +++ b/src/wp-includes/comment-template.php @@ -565,23 +565,27 @@ function comment_date( $d = '', $comment_ID = 0 ) { */ function get_comment_excerpt( $comment_ID = 0 ) { $comment = get_comment( $comment_ID ); - $comment_text = strip_tags($comment->comment_content); - $blah = explode(' ', $comment_text); + $comment_text = strip_tags( str_replace( array( "\n", "\r" ), ' ', $comment->comment_content ) ); + $words = explode( ' ', $comment_text ); - if (count($blah) > 20) { - $k = 20; - $use_dotdotdot = 1; - } else { - $k = count($blah); - $use_dotdotdot = 0; + /** + * Filter the amount of words used in the comment excerpt. + * + * @since 4.4.0 + * + * @param int $comment_excerpt_length The amount of words you want to display in the comment excerpt. + */ + $comment_excerpt_length = apply_filters( 'comment_excerpt_length', 20 ); + + $use_ellipsis = count( $words ) > $comment_excerpt_length; + if ( $use_ellipsis ) { + $words = array_slice( $words, 0, $comment_excerpt_length ); } - $excerpt = ''; - for ($i=0; $i<$k; $i++) { - $excerpt .= $blah[$i] . ' '; + $excerpt = trim( join( ' ', $words ) ); + if ( $use_ellipsis ) { + $excerpt .= '…'; } - $excerpt .= ($use_dotdotdot) ? '…' : ''; - /** * Filter the retrieved comment excerpt. * diff --git a/tests/phpunit/tests/comment/getCommentExcerpt.php b/tests/phpunit/tests/comment/getCommentExcerpt.php new file mode 100644 index 0000000000..0ab97b74f8 --- /dev/null +++ b/tests/phpunit/tests/comment/getCommentExcerpt.php @@ -0,0 +1,43 @@ +factory->comment->create( array( + 'comment_content' => self::$bacon_comment + ) ); + + $excerpt = get_comment_excerpt( $comment_id ); + + $this->assertEquals( 20, count( explode( ' ', $excerpt ) ) ); + } + + public function test_get_comment_excerpt_filtered() { + $comment_id = $this->factory->comment->create( array( + 'comment_content' => self::$bacon_comment + ) ); + + add_filter( 'comment_excerpt_length', array( $this, '_filter_comment_excerpt_length' ) ); + + $excerpt = get_comment_excerpt( $comment_id ); + + $this->assertEquals( 10, count( explode( ' ', $excerpt ) ) ); + } + + public function _filter_comment_excerpt_length() { + remove_filter( 'comment_excerpt_length', array( $this, __METHOD__ ) ); + + return 10; + } +}