diff --git a/src/wp-admin/js/word-count.js b/src/wp-admin/js/word-count.js index c8cfb2f52d..c0255fd295 100644 --- a/src/wp-admin/js/word-count.js +++ b/src/wp-admin/js/word-count.js @@ -61,8 +61,8 @@ ].join( '' ), 'g' ), astralRegExp: /[\uD800-\uDBFF][\uDC00-\uDFFF]/g, wordsRegExp: /\S\s+/g, - charactersRegExp: /\S/g, - allRegExp: /[^\f\n\r\t\v\u00AD\u2028\u2029]/g, + characters_excluding_spacesRegExp: /\S/g, + characters_including_spacesRegExp: /[^\f\n\r\t\v\u00AD\u2028\u2029]/g, l10n: window.wordCountL10n || {} }; @@ -71,7 +71,7 @@ type = type || this.settings.l10n.type; - if ( type !== 'characters' && type !== 'all' ) { + if ( type !== 'characters_excluding_spaces' && type !== 'characters_including_spaces' ) { type = 'words'; } diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php index a2cda11b94..8765d3d0b6 100644 --- a/src/wp-includes/formatting.php +++ b/src/wp-includes/formatting.php @@ -2812,13 +2812,17 @@ function wp_trim_excerpt( $text = '' ) { * @return string Trimmed text. */ function wp_trim_words( $text, $num_words = 55, $more = null ) { - if ( null === $more ) + if ( null === $more ) { $more = __( '…' ); + } + $original_text = $text; $text = wp_strip_all_tags( $text ); - /* translators: If your word count is based on single characters (East Asian characters), - enter 'characters'. Otherwise, enter 'words'. Do not translate into your own language. */ - if ( 'characters' == _x( 'words', 'word count: words or characters?' ) && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) { + + /* translators: If your word count is based on single characters (e.g. East Asian characters), + enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'. + Do not translate into your own language. */ + if ( strpos( _x( 'words', 'Word count type. Do not translate!' ), 'characters' ) === 0 && preg_match( '/^utf\-?8$/i', get_option( 'blog_charset' ) ) ) { $text = trim( preg_replace( "/[\n\r\t ]+/", ' ', $text ), ' ' ); preg_match_all( '/./u', $text, $words_array ); $words_array = array_slice( $words_array[0], 0, $num_words + 1 ); @@ -2827,6 +2831,7 @@ function wp_trim_words( $text, $num_words = 55, $more = null ) { $words_array = preg_split( "/[\n\r\t ]+/", $text, $num_words + 1, PREG_SPLIT_NO_EMPTY ); $sep = ' '; } + if ( count( $words_array ) > $num_words ) { array_pop( $words_array ); $text = implode( $sep, $words_array ); @@ -2834,6 +2839,7 @@ function wp_trim_words( $text, $num_words = 55, $more = null ) { } else { $text = implode( $sep, $words_array ); } + /** * Filter the text content after words have been trimmed. * diff --git a/src/wp-includes/script-loader.php b/src/wp-includes/script-loader.php index 8862b764e6..67a43d7fea 100644 --- a/src/wp-includes/script-loader.php +++ b/src/wp-includes/script-loader.php @@ -401,10 +401,10 @@ function wp_default_scripts( &$scripts ) { $scripts->add( 'word-count', "/wp-admin/js/word-count$suffix.js", array(), false, 1 ); did_action( 'init' ) && $scripts->localize( 'word-count', 'wordCountL10n', array( - /* translators: If your word count is based on single characters (East Asian characters), - enter 'characters', or 'all' to include spaces. Otherwise, enter 'words'. + /* translators: If your word count is based on single characters (e.g. East Asian characters), + enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'. Do not translate into your own language. */ - 'type' => _x( 'words', 'word count: words, characters or all?' ), + 'type' => _x( 'words', 'Word count type. Do not translate!' ), 'shortcodes' => ! empty( $GLOBALS['shortcode_tags'] ) ? array_keys( $GLOBALS['shortcode_tags'] ) : array() ) ); diff --git a/tests/qunit/wp-admin/js/word-count.js b/tests/qunit/wp-admin/js/word-count.js index 07100e885f..5e5062a43c 100644 --- a/tests/qunit/wp-admin/js/word-count.js +++ b/tests/qunit/wp-admin/js/word-count.js @@ -7,74 +7,74 @@ message: 'Basic test.', string: 'one two three', words: 3, - characters: 11, - all: 13 + characters_excluding_spaces: 11, + characters_including_spaces: 13 }, { message: 'HTML tags.', string: 'one two
three', words: 3, - characters: 11, - all: 12 + characters_excluding_spaces: 11, + characters_including_spaces: 12 }, { message: 'Line breaks.', string: 'one\ntwo\nthree', words: 3, - characters: 11, - all: 11 + characters_excluding_spaces: 11, + characters_including_spaces: 11 }, { message: 'Encoded spaces.', string: 'one two three', words: 3, - characters: 11, - all: 13 + characters_excluding_spaces: 11, + characters_including_spaces: 13 }, { message: 'Punctuation.', string: 'It\'s two three \u2026 4?', words: 3, - characters: 15, - all: 19 + characters_excluding_spaces: 15, + characters_including_spaces: 19 }, { message: 'Em dash.', string: 'one\u2014two--three', words: 3, - characters: 14, - all: 14 + characters_excluding_spaces: 14, + characters_including_spaces: 14 }, { message: 'Shortcodes.', string: 'one [shortcode attribute="value"]two[/shortcode]three', words: 3, - characters: 11, - all: 12 + characters_excluding_spaces: 11, + characters_including_spaces: 12 }, { message: 'Astrals.', string: '\uD83D\uDCA9', words: 1, - characters: 1, - all: 1 + characters_excluding_spaces: 1, + characters_including_spaces: 1 }, { message: 'HTML comment.', string: 'onetwo three', words: 2, - characters: 11, - all: 12 + characters_excluding_spaces: 11, + characters_including_spaces: 12 }, { message: 'HTML entity.', string: '> test', words: 1, - characters: 5, - all: 6 + characters_excluding_spaces: 5, + characters_including_spaces: 6 } ], function( test ) { - _.each( [ 'words', 'characters', 'all' ], function( type ) { + _.each( [ 'words', 'characters_excluding_spaces', 'characters_including_spaces' ], function( type ) { assert.equal( wordCounter.count( test.string, type ), test[ type ], test.message + ' (' + type + ')' ); } ); } );