diff --git a/wp-includes/comment-template.php b/wp-includes/comment-template.php index c44514738c..7b8a79d2c6 100644 --- a/wp-includes/comment-template.php +++ b/wp-includes/comment-template.php @@ -569,42 +569,40 @@ function comment_date( $d = '', $comment_ID = 0 ) { } /** - * Retrieve the excerpt of the current comment. + * Retrieves the excerpt of the given comment. * - * Will cut each word and only output the first 20 words with '…' at the end. - * If the word count is less than 20, then no truncating is done and no '…' - * will appear. + * Returns a maximum of 20 words with an ellipsis appended if necessary. * * @since 1.5.0 * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object. * * @param int|WP_Comment $comment_ID WP_Comment or ID of the comment for which to get the excerpt. * Default current comment. - * @return string The maybe truncated comment with 20 words or less. + * @return string The possibly truncated comment excerpt. */ function get_comment_excerpt( $comment_ID = 0 ) { $comment = get_comment( $comment_ID ); - $comment_text = strip_tags( str_replace( array( "\n", "\r" ), ' ', $comment->comment_content ) ); - $words = explode( ' ', $comment_text ); + + if ( ! post_password_required( $comment->comment_post_ID ) ) { + $comment_text = strip_tags( str_replace( array( "\n", "\r" ), ' ', $comment->comment_content ) ); + } else { + $comment_text = __( 'Password protected' ); + } + + /* translators: Maximum number of words used in a comment excerpt. */ + $comment_excerpt_length = intval( _x( '20', 'comment_excerpt_length' ) ); /** - * Filters the amount of words used in the comment excerpt. + * Filters the maximum number 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 ); + $comment_excerpt_length = apply_filters( 'comment_excerpt_length', $comment_excerpt_length ); - $use_ellipsis = count( $words ) > $comment_excerpt_length; - if ( $use_ellipsis ) { - $words = array_slice( $words, 0, $comment_excerpt_length ); - } + $excerpt = wp_trim_words( $comment_text, $comment_excerpt_length, '…' ); - $excerpt = trim( join( ' ', $words ) ); - if ( $use_ellipsis ) { - $excerpt .= '…'; - } /** * Filters the retrieved comment excerpt. *