WordPress.org

Make WordPress Core

Ticket #47867: 47867.diff

File 47867.diff, 2.5 KB (added by donmhico, 2 years ago)

Patch + unit tests.

  • src/wp-includes/formatting.php

    diff --git src/wp-includes/formatting.php src/wp-includes/formatting.php
    index 7398d57249..9e4e97dc72 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 default to 55 if 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 = 55;
     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..e56749998b 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 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
    3855        // #18726
    3956        function test_strips_script_and_style_content() {
    4057                $trimmed = 'This text contains. It should go.';