WordPress.org

Make WordPress Core

Ticket #44541: 44541.2.patch

File 44541.2.patch, 16.4 KB (added by miyauchi, 2 years ago)

Addde annotation

  • 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..8a207a6f9c 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        /**
     47         * @ticket 44541
     48         */
     49        function test_trims_to_20_counted_by_chars() {
     50                switch_to_locale( 'ja_JP' );
     51                $expected = substr( $this->long_text, 0, 20 ) . '&hellip;';
     52                $this->assertEquals( $expected, wp_trim_words( $this->long_text, 20 ) );
     53                restore_previous_locale();
     54        }
     55
     56        /**
     57         * @ticket 44541
     58         */
     59        function test_trims_to_20_counted_by_chars_with_double_width_chars() {
     60                switch_to_locale( 'ja_JP' );
     61                $text = str_repeat( 'あ', 100 );
     62                $expected = str_repeat( 'あ', 19 ) . '&hellip;';
     63                $this->assertEquals( $expected, wp_trim_words( $text, 19 ) );
     64                restore_previous_locale();
     65        }
    4566}
  • tests/phpunit/tests/l10n.php

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