Editor: word count: better names for types.

Also fix it in wp_trim_words().

Fixes #30966.


git-svn-id: https://develop.svn.wordpress.org/trunk@33440 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Ella Iseulde Van Dorpe 2015-07-27 11:18:55 +00:00
parent 70dd339fa3
commit aed9a8c5bc
4 changed files with 37 additions and 31 deletions

View File

@ -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';
}

View File

@ -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.
*

View File

@ -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()
) );

View File

@ -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 <em class="test">two</em><br />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&nbsp;two&#160;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: 'one<!-- comment -->two three',
words: 2,
characters: 11,
all: 12
characters_excluding_spaces: 11,
characters_including_spaces: 12
},
{
message: 'HTML entity.',
string: '&gt; 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 + ')' );
} );
} );