diff --git src/wp-includes/formatting.php src/wp-includes/formatting.php
index 7398d57249..9e4e97dc72 100644
|
|
function wp_trim_excerpt( $text = '', $post = null ) { |
3760 | 3760 | * to the number of individual characters. |
3761 | 3761 | * |
3762 | 3762 | * @since 3.3.0 |
| 3763 | * @since 5.3.0 `$num_words` will default to 55 if non-numeric argument is passed. |
3763 | 3764 | * |
3764 | 3765 | * @param string $text Text to trim. |
3765 | 3766 | * @param int $num_words Number of words. Default 55. |
… |
… |
function wp_trim_words( $text, $num_words = 55, $more = null ) { |
3774 | 3775 | $original_text = $text; |
3775 | 3776 | $text = wp_strip_all_tags( $text ); |
3776 | 3777 | |
| 3778 | if ( ! is_numeric( $num_words ) ) { |
| 3779 | $num_words = 55; |
| 3780 | } |
| 3781 | |
3777 | 3782 | /* |
3778 | 3783 | * translators: If your word count is based on single characters (e.g. East Asian characters), |
3779 | 3784 | * enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'. |
diff --git tests/phpunit/tests/formatting/WPTrimWords.php tests/phpunit/tests/formatting/WPTrimWords.php
index fab62372f7..e56749998b 100644
|
|
class Tests_Formatting_WPTrimWords extends WP_UnitTestCase { |
35 | 35 | $this->assertEquals( $trimmed, wp_trim_words( $text, 5 ) ); |
36 | 36 | } |
37 | 37 | |
| 38 | /** |
| 39 | * @ticket 47867 |
| 40 | */ |
| 41 | function test_wp_trim_words_should_work_with_non_numeric_num_words() { |
| 42 | $text = 'This is more than 55 word data to test that wp_trim_words() should be returning the default 55 words if the give $num_words argument is non-numeric. ' |
| 43 | . 'Numeric data even if they are passed as string still works. So the only concern are empty string or character non-numeric strings. ' |
| 44 | . 'Please see ticket #47867 for more information regarding this test.'; |
| 45 | $trimmed = 'This is more than 55 word data to test that wp_trim_words() should be returning the default 55 words if the give $num_words argument is non-numeric. ' |
| 46 | . 'Numeric data even if they are passed as string still works. So the only concern are empty string or character non-numeric strings. ' |
| 47 | . 'Please see ticket #47867 for more information regarding'; |
| 48 | $trimmed_3_words = 'This is more'; |
| 49 | |
| 50 | $this->assertEquals( $trimmed, wp_trim_words( $text, '', '' ) ); |
| 51 | $this->assertEquals( $trimmed, wp_trim_words( $text, 'abc', '' ) ); |
| 52 | $this->assertEquals( $trimmed_3_words, wp_trim_words( $text, '3', '' ) ); |
| 53 | } |
| 54 | |
38 | 55 | // #18726 |
39 | 56 | function test_strips_script_and_style_content() { |
40 | 57 | $trimmed = 'This text contains. It should go.'; |