WordPress.org

Make WordPress Core

Ticket #44541: 44541.patch

File 44541.patch, 17.0 KB (added by miyauchi, 2 years ago)
  • src/wp-admin/includes/dashboard.php

    diff --git src/wp-admin/includes/dashboard.php src/wp-admin/includes/dashboard.php
    index 0c5bb5f5b1..e1762f34be 100644
    function wp_dashboard_recent_drafts( $drafts = false ) { 
    587587        }
    588588        echo '<h2 class="hide-if-no-js">' . __( 'Your Recent Drafts' ) . "</h2>\n<ul>";
    589589
     590        /* translators: This sets the text length for the draft. */
     591        $draft_length = intval( _x( '10', 'draft_length' ) );
     592
    590593        $drafts = array_slice( $drafts, 0, 3 );
    591594        foreach ( $drafts as $draft ) {
    592595                $url   = get_edit_post_link( $draft->ID );
    function wp_dashboard_recent_drafts( $drafts = false ) { 
    595598                /* translators: %s: post title */
    596599                echo '<div class="draft-title"><a href="' . esc_url( $url ) . '" aria-label="' . esc_attr( sprintf( __( 'Edit &#8220;%s&#8221;' ), $title ) ) . '">' . esc_html( $title ) . '</a>';
    597600                echo '<time datetime="' . get_the_time( 'c', $draft ) . '">' . get_the_time( __( 'F j, Y' ), $draft ) . '</time></div>';
    598                 if ( $the_content = wp_trim_words( $draft->post_content, 10 ) ) {
     601                if ( $the_content = wp_trim_words( $draft->post_content, $draft_length ) ) {
    599602                        echo '<p>' . $the_content . '</p>';
    600603                }
    601604                echo "</li>\n";
  • src/wp-includes/comment-template.php

    diff --git src/wp-includes/comment-template.php src/wp-includes/comment-template.php
    index 649f29a10b..634cbb54e8 100644
    function comment_date( $d = '', $comment_ID = 0 ) { 
    593593function get_comment_excerpt( $comment_ID = 0 ) {
    594594        $comment      = get_comment( $comment_ID );
    595595        $comment_text = strip_tags( str_replace( array( "\n", "\r" ), ' ', $comment->comment_content ) );
    596         $words        = explode( ' ', $comment_text );
     596
     597        /* translators: This sets the text length for the comment excerpt. */
     598        $comment_excerpt_length = intval( _x( '20', 'comment_excerpt_length' ) );
    597599
    598600        /**
    599601         * Filters the amount of words used in the comment excerpt.
    function get_comment_excerpt( $comment_ID = 0 ) { 
    602604         *
    603605         * @param int $comment_excerpt_length The amount of words you want to display in the comment excerpt.
    604606         */
    605         $comment_excerpt_length = apply_filters( 'comment_excerpt_length', 20 );
     607        $comment_excerpt_length = apply_filters( 'comment_excerpt_length', $comment_excerpt_length );
    606608
    607         $use_ellipsis = count( $words ) > $comment_excerpt_length;
    608         if ( $use_ellipsis ) {
    609                 $words = array_slice( $words, 0, $comment_excerpt_length );
    610         }
     609        $excerpt = wp_trim_words( $comment_text, $comment_excerpt_length, '&hellip;' );
    611610
    612         $excerpt = trim( join( ' ', $words ) );
    613         if ( $use_ellipsis ) {
    614                 $excerpt .= '&hellip;';
    615         }
    616611        /**
    617612         * Filters the retrieved comment excerpt.
    618613         *
  • src/wp-includes/formatting.php

    diff --git src/wp-includes/formatting.php src/wp-includes/formatting.php
    index bf03e78e1e..c351dd35f1 100644
    function wp_trim_excerpt( $text = '' ) { 
    36363636                $text = apply_filters( 'the_content', $text );
    36373637                $text = str_replace( ']]>', ']]&gt;', $text );
    36383638
     3639                /* translators: This sets the text length for the excerpt. */
     3640                $excerpt_length = intval( _x( '55', 'excerpt_length' ) );
     3641
    36393642                /**
    36403643                 * Filters the number of words in an excerpt.
    36413644                 *
    function wp_trim_excerpt( $text = '' ) { 
    36433646                 *
    36443647                 * @param int $number The number of words. Default 55.
    36453648                 */
    3646                 $excerpt_length = apply_filters( 'excerpt_length', 55 );
     3649                $excerpt_length = apply_filters( 'excerpt_length', $excerpt_length );
    36473650                /**
    36483651                 * Filters the string in the "more" link displayed after a trimmed excerpt.
    36493652                 *
  • tests/phpunit/data/languages/ja_JP.po

    diff --git tests/phpunit/data/languages/ja_JP.mo tests/phpunit/data/languages/ja_JP.mo
    index 1399898aa7..da1681326c 100644
    Binary files tests/phpunit/data/languages/ja_JP.mo and tests/phpunit/data/languages/ja_JP.mo differ
    diff --git tests/phpunit/data/languages/ja_JP.po tests/phpunit/data/languages/ja_JP.po
    index f71a737714..82a6d7e277 100644
    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: This sets the text length for the excerpt.
     54#: wp-includes/formatting.php:3640
     55msgctxt "excerpt_length"
     56msgid "55"
     57msgstr "110"
     58
     59#. translators: This sets the text length for the comment excerpt.
     60#: src/wp-includes/comment-template.ph:599
     61msgctxt "comment_excerpt_length"
     62msgid "20"
     63msgstr "40"
     64
     65#. translators: This sets the text length for the comment excerpt.
     66#: src/wp-admin/includes/dashboard.php:591
     67msgctxt "draft_length"
     68msgid "10"
     69msgstr "40"
  • tests/phpunit/tests/formatting/WPTrimWords.php

    diff --git tests/phpunit/tests/formatting/WPTrimWords.php tests/phpunit/tests/formatting/WPTrimWords.php
    index 7f2a27fc05..50e4b4c6c1 100644
    class Tests_Formatting_WPTrimWords extends WP_UnitTestCase { 
    4242                $text = 'This is some short text.';
    4343                $this->assertEquals( $text, wp_trim_words( $text ) );
    4444        }
     45
     46        function test_trims_to_20_counted_by_chars() {
     47                switch_to_locale( 'ja_JP' );
     48                $expected = substr( $this->long_text, 0, 20 ) . '&hellip;';
     49                $this->assertEquals( $expected, wp_trim_words( $this->long_text, 20 ) );
     50                restore_previous_locale();
     51        }
     52
     53        function test_trims_to_20_counted_by_chars_with_double_width_chars() {
     54                switch_to_locale( 'ja_JP' );
     55                $text = str_repeat( 'あ', 100 );
     56                $expected = str_repeat( 'あ', 19 ) . '&hellip;';
     57                $this->assertEquals( $expected, wp_trim_words( $text, 19 ) );
     58                restore_previous_locale();
     59        }
    4560}
  • tests/phpunit/tests/l10n.php

    diff --git tests/phpunit/tests/l10n.php tests/phpunit/tests/l10n.php
    index 5173621d2c..9ecd49d768 100644
     
    55 * @group i18n
    66 */
    77class Tests_L10n extends WP_UnitTestCase {
    8 
    98        /**
    109         * @ticket 35961
    1110         */
    class Tests_L10n extends WP_UnitTestCase { 
    242241                $this->assertNotEmpty( $array['Project-Id-Version'] );
    243242                $this->assertNotEmpty( $array['X-Generator'] );
    244243        }
     244
     245        /**
     246         * The text length of excerpt should be counted by words.
     247         */
     248        function test_length_of_excerpt_should_be_counted_by_words() {
     249                global $post;
     250
     251                switch_to_locale( 'en_US' );
     252
     253                $args = array(
     254                        'post_content' => '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.',
     255                        'post_excerpt' => '',
     256                );
     257
     258                $post = $this->factory()->post->create_and_get( $args );
     259                setup_postdata( $post );
     260
     261                $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";
     262                $this->expectOutputString( $expect );
     263                the_excerpt();
     264
     265                restore_previous_locale();
     266        }
     267
     268        /**
     269         * The text length of excerpt should be counted by chars.
     270         */
     271        function test_length_of_excerpt_should_be_counted_by_chars() {
     272                global $post;
     273
     274                switch_to_locale( 'ja_JP' );
     275
     276                $args = array(
     277                        'post_content' => '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.',
     278                        'post_excerpt' => '',
     279                );
     280
     281                $post = $this->factory()->post->create_and_get( $args );
     282                setup_postdata( $post );
     283
     284                $expect = "<p>Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore  [&hellip;]</p>\n";
     285                $this->expectOutputString( $expect );
     286                the_excerpt();
     287
     288                restore_previous_locale();
     289        }
     290
     291        /**
     292         * The text length of excerpt should be counted by chars with Japanese.
     293         */
     294        function test_length_of_excerpt_should_be_counted_by_chars_in_japanese() {
     295                global $post;
     296
     297                switch_to_locale( 'ja_JP' );
     298
     299                $args = array(
     300                        'post_content' => str_repeat( 'あ', 200 ),
     301                        'post_excerpt' => '',
     302                );
     303
     304                $post = $this->factory()->post->create_and_get( $args );
     305                setup_postdata( $post );
     306
     307                $expect = "<p>" . str_repeat( 'あ', 110 ) . " [&hellip;]</p>\n";
     308                $this->expectOutputString( $expect );
     309                the_excerpt();
     310
     311                restore_previous_locale();
     312        }
     313
     314        /**
     315         * The text length of excerpt should be counted by words.
     316         */
     317        function test_length_of_excerpt_rss_should_be_counted_by_words() {
     318                global $post;
     319
     320                switch_to_locale( 'en_US' );
     321
     322                $args = array(
     323                        'post_content' => '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.',
     324                        'post_excerpt' => '',
     325                );
     326
     327                $post = $this->factory()->post->create_and_get( $args );
     328                setup_postdata( $post );
     329
     330                $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;]";
     331                $this->expectOutputString( $expect );
     332                the_excerpt_rss();
     333
     334                restore_previous_locale();
     335        }
     336
     337        /**
     338         * The text length of excerpt should be counted by chars.
     339         */
     340        function test_length_of_excerpt_rss_should_be_counted_by_chars() {
     341                global $post;
     342
     343                switch_to_locale( 'ja_JP' );
     344
     345                $args = array(
     346                        'post_content' => '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.',
     347                        'post_excerpt' => '',
     348                );
     349
     350                $post = $this->factory()->post->create_and_get( $args );
     351                setup_postdata( $post );
     352
     353                $expect = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore  [&#8230;]";
     354                $this->expectOutputString( $expect );
     355                the_excerpt_rss();
     356
     357                restore_previous_locale();
     358        }
     359
     360        /**
     361         * The text length of draft in dashboard should be counted by chars.
     362         */
     363        function test_length_of_draft_should_be_counted_by_words() {
     364                require_once dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) . '/src/wp-admin/includes/dashboard.php';
     365
     366                switch_to_locale( 'en_US' );
     367
     368                $args = array(
     369                        'post_content' => '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.',
     370                        'post_excerpt' => '',
     371                        'post_status'  => 'draft',
     372                );
     373
     374                $post = $this->factory()->post->create( $args );
     375
     376                $expect = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do&hellip;";
     377
     378                ob_start();
     379                wp_dashboard_recent_drafts();
     380                $result = ob_get_clean();
     381
     382                $this->assertTrue( !! strpos( $result, $expect ) );
     383
     384                restore_previous_locale();
     385        }
     386
     387        /**
     388         * The text length of draft in dashboard should be counted by chars.
     389         */
     390        function test_length_of_draft_should_be_counted_by_chars() {
     391                require_once dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) . '/src/wp-admin/includes/dashboard.php';
     392
     393                switch_to_locale( 'ja_JP' );
     394
     395                $args = array(
     396                        'post_content' => '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.',
     397                        'post_excerpt' => '',
     398                        'post_status'  => 'draft',
     399                );
     400
     401                $post = $this->factory()->post->create( $args );
     402
     403                $expect = "Lorem ipsum dolor sit amet, consectetur &hellip;";
     404
     405                ob_start();
     406                wp_dashboard_recent_drafts();
     407                $result = ob_get_clean();
     408
     409                $this->assertTrue( !! strpos( $result, $expect ) );
     410
     411                restore_previous_locale();
     412        }
     413
     414        /**
     415         * The text length of draft in dashboard should be counted by chars in Japanese.
     416         */
     417        function test_length_of_draft_should_be_counted_by_chars_in_japanese() {
     418                require_once dirname( dirname( dirname( dirname( __FILE__ ) ) ) ) . '/src/wp-admin/includes/dashboard.php';
     419
     420                switch_to_locale( 'ja_JP' );
     421
     422                $args = array(
     423                        'post_content' => str_repeat( 'あ', 200 ),
     424                        'post_excerpt' => '',
     425                        'post_status'  => 'draft',
     426                );
     427
     428                $post = $this->factory()->post->create( $args );
     429
     430                $expect = str_repeat( 'あ', 40 ) . "&hellip;";
     431
     432                ob_start();
     433                wp_dashboard_recent_drafts();
     434                $result = ob_get_clean();
     435
     436                $this->assertTrue( !! strpos( $result, $expect ) );
     437
     438                restore_previous_locale();
     439        }
     440
     441        /**
     442         * The text length of draft in dashboard should be counted by chars.
     443         */
     444        function test_length_of_comment_excerpt_should_be_counted_by_words() {
     445                switch_to_locale( 'en_US' );
     446
     447                $args = array(
     448                        'comment_content' => '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.',
     449                );
     450
     451                $comment_id = $this->factory()->comment->create( $args );
     452
     453                $expect = "Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua. Ut&hellip;";
     454
     455                $comment_excerpt = get_comment_excerpt( $comment_id );
     456
     457                $this->assertSame( $expect, $comment_excerpt );
     458
     459                restore_previous_locale();
     460        }
     461
     462        /**
     463         * The text length of draft in dashboard should be counted by chars.
     464         */
     465        function test_length_of_comment_excerpt_should_be_counted_by_chars() {
     466                switch_to_locale( 'ja_JP' );
     467
     468                $args = array(
     469                        'comment_content' => '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.',
     470                );
     471
     472                $comment_id = $this->factory()->comment->create( $args );
     473
     474                $expect = "Lorem ipsum dolor sit amet, consectetur &hellip;";
     475
     476                $comment_excerpt = get_comment_excerpt( $comment_id );
     477
     478                $this->assertSame( $expect, $comment_excerpt );
     479
     480                restore_previous_locale();
     481        }
     482
     483        /**
     484         * The text length of draft in dashboard should be counted by chars.
     485         */
     486        function test_length_of_comment_excerpt_should_be_counted_by_chars_in_Japanese() {
     487                switch_to_locale( 'ja_JP' );
     488
     489                $args = array(
     490                        'comment_content' => str_repeat( 'あ', 200 ),
     491                );
     492
     493                $comment_id = $this->factory()->comment->create( $args );
     494
     495                $expect = str_repeat( 'あ', 40 ) . "&hellip;";
     496
     497                $comment_excerpt = get_comment_excerpt( $comment_id );
     498
     499                $this->assertSame( $expect, $comment_excerpt );
     500
     501                restore_previous_locale();
     502        }
    245503}