Make WordPress Core

Ticket #44541: 44541.diff

File 44541.diff, 15.9 KB (added by johnbillion, 5 years ago)
  • src/wp-admin/includes/dashboard.php

    diff --git a/src/wp-admin/includes/dashboard.php b/src/wp-admin/includes/dashboard.php
    index ad02909c74..62cd5a014f 100644
    a b function wp_dashboard_recent_drafts( $drafts = false ) { 
    584584        }
    585585        echo '<h2 class="hide-if-no-js">' . __( 'Your Recent Drafts' ) . "</h2>\n<ul>";
    586586
     587        /* translators: Maximum number of words used in a preview of a draft on the dashboard. */
     588        $draft_length = intval( _x( '10', 'draft_length' ) );
     589
    587590        $drafts = array_slice( $drafts, 0, 3 );
    588591        foreach ( $drafts as $draft ) {
    589592                $url   = get_edit_post_link( $draft->ID );
    function wp_dashboard_recent_drafts( $drafts = false ) { 
    592595                /* translators: %s: post title */
    593596                echo '<div class="draft-title"><a href="' . esc_url( $url ) . '" aria-label="' . esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $title ) ) . '">' . esc_html( $title ) . '</a>';
    594597                echo '<time datetime="' . get_the_time( 'c', $draft ) . '">' . get_the_time( __( 'F j, Y' ), $draft ) . '</time></div>';
    595                 if ( $the_content = wp_trim_words( $draft->post_content, 10 ) ) {
     598                if ( $the_content = wp_trim_words( $draft->post_content, $draft_length ) ) {
    596599                        echo '<p>' . $the_content . '</p>';
    597600                }
    598601                echo "</li>\n";
  • src/wp-includes/comment-template.php

    diff --git a/src/wp-includes/comment-template.php b/src/wp-includes/comment-template.php
    index 41031cba93..300bdac992 100644
    a b function comment_date( $d = '', $comment_ID = 0 ) { 
    577577}
    578578
    579579/**
    580  * Retrieve the excerpt of the current comment.
     580 * Retrieves the excerpt of the given comment.
    581581 *
    582  * Will cut each word and only output the first 20 words with '&hellip;' at the end.
    583  * If the word count is less than 20, then no truncating is done and no '&hellip;'
    584  * will appear.
     582 * Returns a maximum of 20 words with an ellipsis appended if necessary.
    585583 *
    586584 * @since 1.5.0
    587585 * @since 4.4.0 Added the ability for `$comment_ID` to also accept a WP_Comment object.
    588586 *
    589587 * @param int|WP_Comment $comment_ID  WP_Comment or ID of the comment for which to get the excerpt.
    590588 *                                    Default current comment.
    591  * @return string The maybe truncated comment with 20 words or less.
     589 * @return string The possibly truncated comment excerpt.
    592590 */
    593591function get_comment_excerpt( $comment_ID = 0 ) {
    594592        $comment      = get_comment( $comment_ID );
    595593        $comment_text = strip_tags( str_replace( array( "\n", "\r" ), ' ', $comment->comment_content ) );
    596         $words        = explode( ' ', $comment_text );
     594
     595        /* translators: Maximum number of words used in a comment excerpt. */
     596        $comment_excerpt_length = intval( _x( '20', 'comment_excerpt_length' ) );
    597597
    598598        /**
    599          * Filters the amount of words used in the comment excerpt.
     599         * Filters the maximum number of words used in the comment excerpt.
    600600         *
    601601         * @since 4.4.0
    602602         *
    603603         * @param int $comment_excerpt_length The amount of words you want to display in the comment excerpt.
    604604         */
    605         $comment_excerpt_length = apply_filters( 'comment_excerpt_length', 20 );
     605        $comment_excerpt_length = apply_filters( 'comment_excerpt_length', $comment_excerpt_length );
    606606
    607         $use_ellipsis = count( $words ) > $comment_excerpt_length;
    608         if ( $use_ellipsis ) {
    609                 $words = array_slice( $words, 0, $comment_excerpt_length );
    610         }
     607        $excerpt = wp_trim_words( $comment_text, $comment_excerpt_length, '&hellip;' );
    611608
    612         $excerpt = trim( join( ' ', $words ) );
    613         if ( $use_ellipsis ) {
    614                 $excerpt .= '&hellip;';
    615         }
    616609        /**
    617610         * Filters the retrieved comment excerpt.
    618611         *
  • src/wp-includes/formatting.php

    diff --git a/src/wp-includes/formatting.php b/src/wp-includes/formatting.php
    index 7d73481eab..36ec85e0d1 100644
    a b function human_time_diff( $from, $to = '' ) { 
    36803680/**
    36813681 * Generates an excerpt from the content, if needed.
    36823682 *
    3683  * The excerpt word amount will be 55 words and if the amount is greater than
    3684  * that, then the string ' [&hellip;]' will be appended to the excerpt. If the string
    3685  * is less than 55 words, then the content will be returned as is.
     3683 * Returns a maximum of 55 words with an ellipsis appended if necessary.
    36863684 *
    36873685 * The 55 word limit can be modified by plugins/themes using the {@see 'excerpt_length'} filter
    36883686 * The ' [&hellip;]' string can be modified by plugins/themes using the {@see 'excerpt_more'} filter
    function wp_trim_excerpt( $text = '', $post = null ) { 
    37073705                $text = apply_filters( 'the_content', $text );
    37083706                $text = str_replace( ']]>', ']]&gt;', $text );
    37093707
     3708                /* translators: Maximum number of words used in a post excerpt. */
     3709                $excerpt_length = intval( _x( '55', 'excerpt_length' ) );
     3710
    37103711                /**
    3711                  * Filters the number of words in an excerpt.
     3712                 * Filters the maximum number of words in a post excerpt.
    37123713                 *
    37133714                 * @since 2.7.0
    37143715                 *
    3715                  * @param int $number The number of words. Default 55.
     3716                 * @param int $number The maximum number of words. Default 55.
    37163717                 */
    3717                 $excerpt_length = apply_filters( 'excerpt_length', 55 );
     3718                $excerpt_length = apply_filters( 'excerpt_length', $excerpt_length );
     3719
    37183720                /**
    37193721                 * Filters the string in the "more" link displayed after a trimmed excerpt.
    37203722                 *
    function wp_trim_excerpt( $text = '', $post = null ) { 
    37253727                $excerpt_more = apply_filters( 'excerpt_more', ' ' . '[&hellip;]' );
    37263728                $text         = wp_trim_words( $text, $excerpt_length, $excerpt_more );
    37273729        }
     3730
    37283731        /**
    37293732         * Filters the trimmed excerpt string.
    37303733         *
  • tests/phpunit/data/languages/ja_JP.po

    diff --git a/tests/phpunit/data/languages/ja_JP.po b/tests/phpunit/data/languages/ja_JP.po
    index f71a737714..bd9cd17a95 100644
    a b msgstr "number_format_thousands_sep" 
    4040#: wp-includes/script-loader.php:620
    4141msgid "Update %s now"
    4242msgstr "今すぐ %s を更新"
     43
     44#. translators: If your word count is based on single characters (e.g. East
     45#. Asian characters), enter 'characters_excluding_spaces' or
     46#. 'characters_including_spaces'. Otherwise, enter 'words'. Do not translate
     47#. into your own language.
     48#: wp-includes/formatting.php:3372 wp-includes/script-loader.php:1100
     49msgctxt "Word count type. Do not translate!"
     50msgid "words"
     51msgstr "characters_including_spaces"
     52
     53#. translators: Maximum number of words used in a post excerpt.
     54#: wp-includes/formatting.php:3640
     55msgctxt "excerpt_length"
     56msgid "55"
     57msgstr "110"
     58
     59#. translators: Maximum number of words used in a comment excerpt.
     60#: wp-includes/comment-template.ph:599
     61msgctxt "comment_excerpt_length"
     62msgid "20"
     63msgstr "40"
     64
     65#. translators: Maximum number of words used in a preview of a draft on the dashboard.
     66#: wp-admin/includes/dashboard.php:591
     67msgctxt "draft_length"
     68msgid "10"
     69msgstr "40"
  • tests/phpunit/tests/formatting/WPTrimWords.php

    diff --git a/tests/phpunit/tests/formatting/WPTrimWords.php b/tests/phpunit/tests/formatting/WPTrimWords.php
    index 7f2a27fc05..fab62372f7 100644
    a b  
    44 * @group formatting
    55 */
    66class Tests_Formatting_WPTrimWords extends WP_UnitTestCase {
     7
     8        /**
     9         * Long Dummy Text.
     10         *
     11         * @since 5.0.0
     12         *
     13         * @var string $long_text
     14         */
    715        private $long_text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit. Fusce varius lacinia vehicula. Etiam sapien risus, ultricies ac posuere eu, convallis sit amet augue. Pellentesque urna massa, lacinia vel iaculis eget, bibendum in mauris. Aenean eleifend pulvinar ligula, a convallis eros gravida non. Suspendisse potenti. Pellentesque et odio tortor. In vulputate pellentesque libero, sed dapibus velit mollis viverra. Pellentesque id urna euismod dolor cursus sagittis.';
    816
    917        function test_trims_to_55_by_default() {
    class Tests_Formatting_WPTrimWords extends WP_UnitTestCase { 
    4250                $text = 'This is some short text.';
    4351                $this->assertEquals( $text, wp_trim_words( $text ) );
    4452        }
     53
     54        /**
     55         * @ticket 44541
     56         */
     57        function test_trims_to_20_counted_by_chars() {
     58                switch_to_locale( 'ja_JP' );
     59                $expected = substr( $this->long_text, 0, 20 ) . '&hellip;';
     60                $actual   = wp_trim_words( $this->long_text, 20 );
     61                restore_previous_locale();
     62                $this->assertEquals( $expected, $actual );
     63        }
     64
     65        /**
     66         * @ticket 44541
     67         */
     68        function test_trims_to_20_counted_by_chars_with_double_width_chars() {
     69                switch_to_locale( 'ja_JP' );
     70                $text     = str_repeat( 'あ', 100 );
     71                $expected = str_repeat( 'あ', 19 ) . '&hellip;';
     72                $actual   = wp_trim_words( $text, 19 );
     73                restore_previous_locale();
     74                $this->assertEquals( $expected, $actual );
     75        }
    4576}
  • tests/phpunit/tests/l10n.php

    diff --git a/tests/phpunit/tests/l10n.php b/tests/phpunit/tests/l10n.php
    index 45a6008363..7b34d32b4e 100644
    a b  
    66 */
    77class Tests_L10n extends WP_UnitTestCase {
    88
     9        /**
     10         * Long Dummy Text.
     11         *
     12         * @since 5.0.0
     13         *
     14         * @var string $long_text
     15         */
     16        private $long_text = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat cupidatat non proident, sunt in culpa qui officia deserunt mollit anim id est laborum.';
     17
    918        /**
    1019         * @ticket 35961
    1120         */
    class Tests_L10n extends WP_UnitTestCase { 
    260269                $this->assertNotEmpty( $array['Project-Id-Version'] );
    261270                $this->assertNotEmpty( $array['X-Generator'] );
    262271        }
     272
     273        /**
     274         * @ticket 44541
     275         */
     276        function test_length_of_excerpt_should_be_counted_by_words() {
     277                global $post;
     278
     279                switch_to_locale( 'en_US' );
     280
     281                $args = array(
     282                        'post_content' => $this->long_text,
     283                        'post_excerpt' => '',
     284                );
     285
     286                $post = $this->factory()->post->create_and_get( $args );
     287                setup_postdata( $post );
     288
     289                $expect = "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat [&hellip;]</p>\n";
     290                the_excerpt();
     291
     292                restore_previous_locale();
     293
     294                $this->expectOutputString( $expect );
     295        }
     296
     297        /**
     298         * @ticket 44541
     299         */
     300        function test_length_of_excerpt_should_be_counted_by_chars() {
     301                global $post;
     302
     303                switch_to_locale( 'ja_JP' );
     304
     305                $args = array(
     306                        'post_content' => $this->long_text,
     307                        'post_excerpt' => '',
     308                );
     309
     310                $post = $this->factory()->post->create_and_get( $args );
     311                setup_postdata( $post );
     312
     313                $expect = "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore  [&hellip;]</p>\n";
     314                the_excerpt();
     315
     316                restore_previous_locale();
     317
     318                $this->expectOutputString( $expect );
     319        }
     320
     321        /**
     322         * @ticket 44541
     323         */
     324        function test_length_of_excerpt_should_be_counted_by_chars_in_japanese() {
     325                global $post;
     326
     327                switch_to_locale( 'ja_JP' );
     328
     329                $args = array(
     330                        'post_content' => str_repeat( 'あ', 200 ),
     331                        'post_excerpt' => '',
     332                );
     333
     334                $post = $this->factory()->post->create_and_get( $args );
     335                setup_postdata( $post );
     336
     337                $expect = '<p>' . str_repeat( 'あ', 110 ) . " [&hellip;]</p>\n";
     338                the_excerpt();
     339
     340                restore_previous_locale();
     341
     342                $this->expectOutputString( $expect );
     343        }
     344
     345        /**
     346         * @ticket 44541
     347         */
     348        function test_length_of_excerpt_rss_should_be_counted_by_words() {
     349                global $post;
     350
     351                switch_to_locale( 'en_US' );
     352
     353                $args = array(
     354                        'post_content' => $this->long_text,
     355                        'post_excerpt' => '',
     356                );
     357
     358                $post = $this->factory()->post->create_and_get( $args );
     359                setup_postdata( $post );
     360
     361                $expect = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut enim ad minim veniam, quis nostrud exercitation ullamco laboris nisi ut aliquip ex ea commodo consequat. Duis aute irure dolor in reprehenderit in voluptate velit esse cillum dolore eu fugiat nulla pariatur. Excepteur sint occaecat [&#8230;]';
     362                the_excerpt_rss();
     363
     364                restore_previous_locale();
     365
     366                $this->expectOutputString( $expect );
     367        }
     368
     369        /**
     370         * @ticket 44541
     371         */
     372        function test_length_of_excerpt_rss_should_be_counted_by_chars() {
     373                global $post;
     374
     375                switch_to_locale( 'ja_JP' );
     376
     377                $args = array(
     378                        'post_content' => $this->long_text,
     379                        'post_excerpt' => '',
     380                );
     381
     382                $post = $this->factory()->post->create_and_get( $args );
     383                setup_postdata( $post );
     384
     385                $expect = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore  [&#8230;]';
     386
     387                the_excerpt_rss();
     388
     389                restore_previous_locale();
     390
     391                $this->expectOutputString( $expect );
     392        }
     393
     394        /**
     395         * @ticket 44541
     396         */
     397        function test_length_of_draft_should_be_counted_by_words() {
     398                require_once ABSPATH . 'wp-admin/includes/dashboard.php';
     399
     400                switch_to_locale( 'en_US' );
     401
     402                $args = array(
     403                        'post_content' => $this->long_text,
     404                        'post_excerpt' => '',
     405                        'post_status'  => 'draft',
     406                );
     407
     408                $this->factory()->post->create( $args );
     409
     410                $expect = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do&hellip;';
     411                wp_dashboard_recent_drafts();
     412
     413                restore_previous_locale();
     414
     415                $this->expectOutputRegex( '/' . $expect . '/' );
     416        }
     417
     418        /**
     419         * @ticket 44541
     420         */
     421        function test_length_of_draft_should_be_counted_by_chars() {
     422                require_once ABSPATH . 'wp-admin/includes/dashboard.php';
     423
     424                switch_to_locale( 'ja_JP' );
     425
     426                $args = array(
     427                        'post_content' => $this->long_text,
     428                        'post_excerpt' => '',
     429                        'post_status'  => 'draft',
     430                );
     431
     432                $post = $this->factory()->post->create( $args );
     433
     434                $expect = 'Lorem ipsum dolor sit amet, consectetur &hellip;';
     435                wp_dashboard_recent_drafts();
     436
     437                restore_previous_locale();
     438
     439                $this->expectOutputRegex( '/' . $expect . '/' );
     440        }
     441
     442        /**
     443         * @ticket 44541
     444         */
     445        function test_length_of_draft_should_be_counted_by_chars_in_japanese() {
     446                require_once ABSPATH . 'wp-admin/includes/dashboard.php';
     447
     448                switch_to_locale( 'ja_JP' );
     449
     450                $args = array(
     451                        'post_content' => str_repeat( 'あ', 200 ),
     452                        'post_excerpt' => '',
     453                        'post_status'  => 'draft',
     454                );
     455
     456                $this->factory()->post->create( $args );
     457
     458                $expect = str_repeat( 'あ', 40 ) . '&hellip;';
     459                wp_dashboard_recent_drafts();
     460
     461                restore_previous_locale();
     462
     463                $this->expectOutputRegex( '/' . $expect . '/' );
     464        }
     465
     466        /**
     467         * @ticket 44541
     468         */
     469        function test_length_of_comment_excerpt_should_be_counted_by_words() {
     470                switch_to_locale( 'en_US' );
     471
     472                $args            = array(
     473                        'comment_content' => $this->long_text,
     474                );
     475                $comment_id      = $this->factory()->comment->create( $args );
     476                $expect          = 'Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut&hellip;';
     477                $comment_excerpt = get_comment_excerpt( $comment_id );
     478
     479                restore_previous_locale();
     480
     481                $this->assertSame( $expect, $comment_excerpt );
     482        }
     483
     484        /**
     485         * @ticket 44541
     486         */
     487        function test_length_of_comment_excerpt_should_be_counted_by_chars() {
     488                switch_to_locale( 'ja_JP' );
     489
     490                $args            = array(
     491                        'comment_content' => $this->long_text,
     492                );
     493                $comment_id      = $this->factory()->comment->create( $args );
     494                $expect          = 'Lorem ipsum dolor sit amet, consectetur &hellip;';
     495                $comment_excerpt = get_comment_excerpt( $comment_id );
     496
     497                restore_previous_locale();
     498
     499                $this->assertSame( $expect, $comment_excerpt );
     500        }
     501
     502        /**
     503         * @ticket 44541
     504         */
     505        function test_length_of_comment_excerpt_should_be_counted_by_chars_in_Japanese() {
     506                switch_to_locale( 'ja_JP' );
     507
     508                $args            = array(
     509                        'comment_content' => str_repeat( 'あ', 200 ),
     510                );
     511                $comment_id      = $this->factory()->comment->create( $args );
     512                $expect          = str_repeat( 'あ', 40 ) . '&hellip;';
     513                $comment_excerpt = get_comment_excerpt( $comment_id );
     514
     515                restore_previous_locale();
     516
     517                $this->assertSame( $expect, $comment_excerpt );
     518        }
    263519}