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
This commit is contained in:
Scott Taylor 2015-09-24 21:04:16 +00:00
parent c5a336d664
commit 55bb8b8c0b
2 changed files with 60 additions and 13 deletions

View File

@ -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 .= '&hellip;';
}
$excerpt .= ($use_dotdotdot) ? '&hellip;' : '';
/**
* Filter the retrieved comment excerpt.
*

View File

@ -0,0 +1,43 @@
<?php
class Tests_Get_Comment_Excerpt extends WP_UnitTestCase {
protected static $bacon_comment;
public static function setUpBeforeClass() {
parent::setUpBeforeClass();
self::$bacon_comment = 'Bacon ipsum dolor amet porchetta capicola sirloin prosciutto brisket shankle jerky. Ham hock filet mignon boudin ground round, prosciutto alcatra spare ribs meatball turducken pork beef ribs ham beef. Bacon pastrami short loin, venison tri-tip ham short ribs doner swine. Tenderloin pig tongue pork jowl doner. Pork loin rump t-bone, beef strip steak flank drumstick tri-tip short loin capicola jowl. Cow filet mignon hamburger doner rump. Short loin jowl drumstick, tongue tail beef ribs pancetta flank brisket landjaeger chuck venison frankfurter turkey.
Brisket shank rump, tongue beef ribs swine fatback turducken capicola meatball picanha chicken cupim meatloaf turkey. Bacon biltong shoulder tail frankfurter boudin cupim turkey drumstick. Porchetta pig shoulder, jerky flank pork tail meatball hamburger. Doner ham hock ribeye tail jerky swine. Leberkas ribeye pancetta, tenderloin capicola doner turducken chicken venison ground round boudin pork chop. Tail pork loin pig spare ribs, biltong ribeye brisket pork chop cupim. Short loin leberkas spare ribs jowl landjaeger tongue kevin flank bacon prosciutto.
Shankle pork chop prosciutto ribeye ham hock pastrami. T-bone shank brisket bacon pork chop. Cupim hamburger pork loin short loin. Boudin ball tip cupim ground round ham shoulder. Sausage rump cow tongue bresaola pork pancetta biltong tail chicken turkey hamburger. Kevin flank pork loin salami biltong. Alcatra landjaeger pastrami andouille kielbasa ham tenderloin drumstick sausage turducken tongue corned beef.';
}
public function test_get_comment_excerpt() {
$comment_id = $this->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;
}
}