Make WordPress Core

Changeset 34520


Ignore:
Timestamp:
09/24/2015 09:04:16 PM (9 years ago)
Author:
wonderboymusic
Message:

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.

Location:
trunk
Files:
1 added
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/comment-template.php

    r34503 r34520  
    566566function get_comment_excerpt( $comment_ID = 0 ) {
    567567    $comment = get_comment( $comment_ID );
    568     $comment_text = strip_tags($comment->comment_content);
    569     $blah = explode(' ', $comment_text);
    570 
    571     if (count($blah) > 20) {
    572         $k = 20;
    573         $use_dotdotdot = 1;
    574     } else {
    575         $k = count($blah);
    576         $use_dotdotdot = 0;
    577     }
    578 
    579     $excerpt = '';
    580     for ($i=0; $i<$k; $i++) {
    581         $excerpt .= $blah[$i] . ' ';
    582     }
    583     $excerpt .= ($use_dotdotdot) ? '&hellip;' : '';
    584 
     568    $comment_text = strip_tags( str_replace( array( "\n", "\r" ), ' ', $comment->comment_content ) );
     569    $words = explode( ' ', $comment_text );
     570
     571    /**
     572     * Filter the amount of words used in the comment excerpt.
     573     *
     574     * @since 4.4.0
     575     *
     576     * @param int $comment_excerpt_length The amount of words you want to display in the comment excerpt.
     577     */
     578    $comment_excerpt_length = apply_filters( 'comment_excerpt_length', 20 );
     579
     580    $use_ellipsis = count( $words ) > $comment_excerpt_length;
     581    if ( $use_ellipsis ) {
     582        $words = array_slice( $words, 0, $comment_excerpt_length );
     583    }
     584
     585    $excerpt = trim( join( ' ', $words ) );
     586    if ( $use_ellipsis ) {
     587        $excerpt .= '&hellip;';
     588    }
    585589    /**
    586590     * Filter the retrieved comment excerpt.
Note: See TracChangeset for help on using the changeset viewer.