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

Built from https://develop.svn.wordpress.org/trunk@45505


git-svn-id: http://core.svn.wordpress.org/trunk@45316 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
John Blackbourn 2019-06-08 18:42:52 +00:00
parent eeba1c1244
commit c9b51df3d0
4 changed files with 23 additions and 24 deletions

View File

@ -584,6 +584,9 @@ function wp_dashboard_recent_drafts( $drafts = false ) {
}
echo '<h2 class="hide-if-no-js">' . __( 'Your Recent Drafts' ) . "</h2>\n<ul>";
/* translators: Maximum number of words used in a preview of a draft on the dashboard. */
$draft_length = intval( _x( '10', 'draft_length' ) );
$drafts = array_slice( $drafts, 0, 3 );
foreach ( $drafts as $draft ) {
$url = get_edit_post_link( $draft->ID );
@ -592,7 +595,7 @@ function wp_dashboard_recent_drafts( $drafts = false ) {
/* translators: %s: post title */
echo '<div class="draft-title"><a href="' . esc_url( $url ) . '" aria-label="' . esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $title ) ) . '">' . esc_html( $title ) . '</a>';
echo '<time datetime="' . get_the_time( 'c', $draft ) . '">' . get_the_time( __( 'F j, Y' ), $draft ) . '</time></div>';
if ( $the_content = wp_trim_words( $draft->post_content, 10 ) ) {
if ( $the_content = wp_trim_words( $draft->post_content, $draft_length ) ) {
echo '<p>' . $the_content . '</p>';
}
echo "</li>\n";

View File

@ -577,42 +577,35 @@ 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 '&hellip;' at the end.
* If the word count is less than 20, then no truncating is done and no '&hellip;'
* 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 );
/* 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, '&hellip;' );
$excerpt = trim( join( ' ', $words ) );
if ( $use_ellipsis ) {
$excerpt .= '&hellip;';
}
/**
* Filters the retrieved comment excerpt.
*

View File

@ -3680,9 +3680,7 @@ function human_time_diff( $from, $to = '' ) {
/**
* Generates an excerpt from the content, if needed.
*
* The excerpt word amount will be 55 words and if the amount is greater than
* that, then the string ' [&hellip;]' will be appended to the excerpt. If the string
* is less than 55 words, then the content will be returned as is.
* Returns a maximum of 55 words with an ellipsis appended if necessary.
*
* The 55 word limit can be modified by plugins/themes using the {@see 'excerpt_length'} filter
* The ' [&hellip;]' string can be modified by plugins/themes using the {@see 'excerpt_more'} filter
@ -3707,14 +3705,18 @@ function wp_trim_excerpt( $text = '', $post = null ) {
$text = apply_filters( 'the_content', $text );
$text = str_replace( ']]>', ']]&gt;', $text );
/* translators: Maximum number of words used in a post excerpt. */
$excerpt_length = intval( _x( '55', 'excerpt_length' ) );
/**
* Filters the number of words in an excerpt.
* Filters the maximum number of words in a post excerpt.
*
* @since 2.7.0
*
* @param int $number The number of words. Default 55.
* @param int $number The maximum number of words. Default 55.
*/
$excerpt_length = apply_filters( 'excerpt_length', 55 );
$excerpt_length = apply_filters( 'excerpt_length', $excerpt_length );
/**
* Filters the string in the "more" link displayed after a trimmed excerpt.
*
@ -3725,6 +3727,7 @@ function wp_trim_excerpt( $text = '', $post = null ) {
$excerpt_more = apply_filters( 'excerpt_more', ' ' . '[&hellip;]' );
$text = wp_trim_words( $text, $excerpt_length, $excerpt_more );
}
/**
* Filters the trimmed excerpt string.
*

View File

@ -13,7 +13,7 @@
*
* @global string $wp_version
*/
$wp_version = '5.3-alpha-45504';
$wp_version = '5.3-alpha-45505';
/**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.