Skip to content
Permalink
Browse files

Posts, Post Types: In `wp_trim_words()` make sure the `$num_words` pa…

…rameter is always an integer, as documented, to avoid a PHP warning.

Props donmhico, pikamander2.
Fixes #47867.

git-svn-id: https://develop.svn.wordpress.org/trunk@45796 602fd350-edb4-49c9-b593-d223f7449a82
  • Loading branch information...
SergeyBiryukov committed Aug 14, 2019
1 parent d4a3052 commit a58eb86d5ee673d9ae47468be5b84ba44f5b7e8c
Showing with 12 additions and 1 deletion.
  1. +2 −1 src/wp-includes/formatting.php
  2. +10 −0 tests/phpunit/tests/formatting/WPTrimWords.php
@@ -3728,7 +3728,7 @@ function wp_trim_excerpt( $text = '', $post = null ) {
*
* @param int $number The maximum number of words. Default 55.
*/
$excerpt_length = apply_filters( 'excerpt_length', $excerpt_length );
$excerpt_length = (int) apply_filters( 'excerpt_length', $excerpt_length );
/**
* Filters the string in the "more" link displayed after a trimmed excerpt.
@@ -3773,6 +3773,7 @@ function wp_trim_words( $text, $num_words = 55, $more = null ) {
$original_text = $text;
$text = wp_strip_all_tags( $text );
$num_words = (int) $num_words;
/*
* translators: If your word count is based on single characters (e.g. East Asian characters),
@@ -73,4 +73,14 @@ function test_trims_to_20_counted_by_chars_with_double_width_chars() {
restore_previous_locale();
$this->assertEquals( $expected, $actual );
}
/**
* @ticket 47867
*/
function test_works_with_non_numeric_num_words() {
$this->assertEquals( '', wp_trim_words( $this->long_text, '', '' ) );
$this->assertEquals( '', wp_trim_words( $this->long_text, 'abc', '' ) );
$this->assertEquals( '', wp_trim_words( $this->long_text, null, '' ) );
$this->assertEquals( 'Lorem ipsum dolor', wp_trim_words( $this->long_text, '3', '' ) );
}
}

0 comments on commit a58eb86

Please sign in to comment.
You can’t perform that action at this time.