WordPress.org

Make WordPress Core

Ticket #47867: 47867.2.diff

File 47867.2.diff, 2.2 KB (added by donmhico, 2 years ago)

Set $num_words to 0 if non-numeric argument is passed.

  • src/wp-includes/formatting.php

    diff --git src/wp-includes/formatting.php src/wp-includes/formatting.php
    index 7398d57249..6973bce84b 100644
    function wp_trim_excerpt( $text = '', $post = null ) { 
    37603760 * to the number of individual characters.
    37613761 *
    37623762 * @since 3.3.0
     3763 * @since 5.3.0 `$num_words` will be set to 0 if null or non-numeric argument is passed.
    37633764 *
    37643765 * @param string $text      Text to trim.
    37653766 * @param int    $num_words Number of words. Default 55.
    function wp_trim_words( $text, $num_words = 55, $more = null ) { 
    37743775        $original_text = $text;
    37753776        $text          = wp_strip_all_tags( $text );
    37763777
     3778        if ( ! is_numeric( $num_words ) ) {
     3779                $num_words = 0;
     3780        }
     3781
    37773782        /*
    37783783         * translators: If your word count is based on single characters (e.g. East Asian characters),
    37793784         * enter 'characters_excluding_spaces' or 'characters_including_spaces'. Otherwise, enter 'words'.
  • tests/phpunit/tests/formatting/WPTrimWords.php

    diff --git tests/phpunit/tests/formatting/WPTrimWords.php tests/phpunit/tests/formatting/WPTrimWords.php
    index fab62372f7..67466eff87 100644
    class Tests_Formatting_WPTrimWords extends WP_UnitTestCase { 
    3535                $this->assertEquals( $trimmed, wp_trim_words( $text, 5 ) );
    3636        }
    3737
     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 0 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 = '';
     46                $trimmed_3_words = 'This is more';
     47
     48                $this->assertEquals( $trimmed, wp_trim_words( $text, '', '' ) );
     49                $this->assertEquals( $trimmed, wp_trim_words( $text, 'abc', '' ) );
     50                $this->assertEquals( $trimmed, wp_trim_words( $text, null, '' ) );
     51                $this->assertEquals( $trimmed_3_words, wp_trim_words( $text, '3', '' ) );
     52        }
     53
    3854        // #18726
    3955        function test_strips_script_and_style_content() {
    4056                $trimmed = 'This text contains. It should go.';