Make WordPress Core


Ignore:
Timestamp:
06/08/2019 06:41:08 PM (5 years ago)
Author:
johnbillion
Message:

I18N: Allow the length of automatically generated excerpts to be localized.

This introduces three new strings that can be used to control the maximum length of automatically generated excerpts for posts, comments, and draft post previews in the dashboard. Optionally combined with the existing word count type control this allows languages which include many multibyte characters to specify more appropriate maximum excerpt lengths.

Props miyauchi, birgire, johnbillion

Fixes #44541

File:
1 edited

Legend:

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

    r45496 r45505  
    578578
    579579/**
    580  * Retrieve the excerpt of the current comment.
    581  *
    582  * Will cut each word and only output the first 20 words with '…' at the end.
    583  * If the word count is less than 20, then no truncating is done and no '…'
    584  * will appear.
     580 * Retrieves the excerpt of the given comment.
     581 *
     582 * Returns a maximum of 20 words with an ellipsis appended if necessary.
    585583 *
    586584 * @since 1.5.0
     
    589587 * @param int|WP_Comment $comment_ID  WP_Comment or ID of the comment for which to get the excerpt.
    590588 *                                    Default current comment.
    591  * @return string The maybe truncated comment with 20 words or less.
     589 * @return string The possibly truncated comment excerpt.
    592590 */
    593591function get_comment_excerpt( $comment_ID = 0 ) {
    594592    $comment      = get_comment( $comment_ID );
    595593    $comment_text = strip_tags( str_replace( array( "\n", "\r" ), ' ', $comment->comment_content ) );
    596     $words        = explode( ' ', $comment_text );
    597 
    598     /**
    599      * Filters the amount of words used in the comment excerpt.
     594
     595    /* translators: Maximum number of words used in a comment excerpt. */
     596    $comment_excerpt_length = intval( _x( '20', 'comment_excerpt_length' ) );
     597
     598    /**
     599     * Filters the maximum number of words used in the comment excerpt.
    600600     *
    601601     * @since 4.4.0
     
    603603     * @param int $comment_excerpt_length The amount of words you want to display in the comment excerpt.
    604604     */
    605     $comment_excerpt_length = apply_filters( 'comment_excerpt_length', 20 );
    606 
    607     $use_ellipsis = count( $words ) > $comment_excerpt_length;
    608     if ( $use_ellipsis ) {
    609         $words = array_slice( $words, 0, $comment_excerpt_length );
    610     }
    611 
    612     $excerpt = trim( join( ' ', $words ) );
    613     if ( $use_ellipsis ) {
    614         $excerpt .= '…';
    615     }
     605    $comment_excerpt_length = apply_filters( 'comment_excerpt_length', $comment_excerpt_length );
     606
     607    $excerpt = wp_trim_words( $comment_text, $comment_excerpt_length, '…' );
     608
    616609    /**
    617610     * Filters the retrieved comment excerpt.
Note: See TracChangeset for help on using the changeset viewer.