Make WordPress Core

Ticket #44541: 44541.4.patch

File 44541.4.patch, 13.3 KB (added by miyauchi, 7 years ago)

fixed: https://core.trac.wordpress.org/ticket/44541#comment:9

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