Make WordPress Core

Changeset 45505

Timestamp:
06/08/2019 06:41:08 PM (5 years ago)
Author:
johnbillion
Message:

I18N: Allow the length of automatically generated excerpts to be localized.

This introduces three new strings that can be used to control the maximum length of automatically generated excerpts for posts, comments, and draft post previews in the dashboard. Optionally combined with the existing word count type control this allows languages which include many multibyte characters to specify more appropriate maximum excerpt lengths.

Props miyauchi, birgire, johnbillion

Fixes #44541

Location:
trunk
Files:
7 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-admin/includes/dashboard.php

    r45450 r45505  
    585585    echo '<h2 class="hide-if-no-js">' . __( 'Your Recent Drafts' ) . "</h2>\n<ul>";
    586586
     587
     588
     589
    587590    $drafts = array_slice( $drafts, 0, 3 );
    588591    foreach ( $drafts as $draft ) {
     
    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, ) ) {
    596599            echo '<p>' . $the_content . '</p>';
    597600        }
  • trunk/src/wp-includes/comment-template.php

    r45496 r45505  
    578578
    579579/**
    580  * Retrieve the excerpt of the current comment.
    581  *
    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.
     580 * Retrieves the excerpt of the given comment.
     581 *
     582 * Returns a maximum of 20 words with an ellipsis appended if necessary.
    585583 *
    586584 * @since 1.5.0
     
    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 .
    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 );
    597 
    598     /**
    599      * Filters the amount of words used in the comment excerpt.
     594
     595    /* translators: Maximum number of words used in a comment excerpt. */
     596    $comment_excerpt_length = intval( _x( '20', 'comment_excerpt_length' ) );
     597
     598    /**
     599     * Filters the maximum number of words used in the comment excerpt.
    600600     *
    601601     * @since 4.4.0
     
    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 );
    606 
    607     $use_ellipsis = count( $words ) > $comment_excerpt_length;
    608     if ( $use_ellipsis ) {
    609         $words = array_slice( $words, 0, $comment_excerpt_length );
    610     }
    611 
    612     $excerpt = trim( join( ' ', $words ) );
    613     if ( $use_ellipsis ) {
    614         $excerpt .= '&hellip;';
    615     }
     605    $comment_excerpt_length = apply_filters( 'comment_excerpt_length', $comment_excerpt_length );
     606
     607    $excerpt = wp_trim_words( $comment_text, $comment_excerpt_length, '&hellip;' );
     608
    616609    /**
    617610     * Filters the retrieved comment excerpt.
  • trunk/src/wp-includes/formatting.php

    r45445 r45505  
    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
     
    37083706        $text = str_replace( ']]>', ']]&gt;', $text );
    37093707
     3708
     3709
     3710
    37103711        /**
    3711          * Filters the number of words in an excerpt.
     3712         * Filters the excerpt.
    37123713         *
    37133714         * @since 2.7.0
    37143715         *
    3715          * @param int $number The number of words. Default 55.
     3716         * @param int $number The 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.
     
    37263728        $text         = wp_trim_words( $text, $excerpt_length, $excerpt_more );
    37273729    }
     3730
    37283731    /**
    37293732     * Filters the trimmed excerpt string.
  • trunk/tests/phpunit/data/languages/ja_JP.po

    r43359 r45505  
    4141msgid "Update %s now"
    4242msgstr "今すぐ %s を更新"
     43
     44
     45
     46
     47
     48
     49
     50
     51
     52
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
  • trunk/tests/phpunit/tests/formatting/WPTrimWords.php

    r42343 r45505  
    55 */
    66class Tests_Formatting_WPTrimWords extends WP_UnitTestCase {
     7
     8
     9
     10
     11
     12
     13
     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
     
    4351        $this->assertEquals( $text, wp_trim_words( $text ) );
    4452    }
     53
     54
     55
     56
     57
     58
     59
     60
     61
     62
     63
     64
     65
     66
     67
     68
     69
     70
     71
     72
     73
     74
     75
    4576}
  • trunk/tests/phpunit/tests/l10n.php

    r44516 r45505  
    66 */
    77class Tests_L10n extends WP_UnitTestCase {
     8
     9
     10
     11
     12
     13
     14
     15
     16
    817
    918    /**
     
    261270        $this->assertNotEmpty( $array['X-Generator'] );
    262271    }
     272
     273
     274
     275
     276
     277
     278
     279
     280
     281
     282
     283
     284
     285
     286
     287
     288
     289
     290
     291
     292
     293
     294
     295
     296
     297
     298
     299
     300
     301
     302
     303
     304
     305
     306
     307
     308
     309
     310
     311
     312
     313
     314
     315
     316
     317
     318
     319
     320
     321
     322
     323
     324
     325
     326
     327
     328
     329
     330
     331
     332
     333
     334
     335
     336
     337
     338
     339
     340
     341
     342
     343
     344
     345
     346
     347
     348
     349
     350
     351
     352
     353
     354
     355
     356
     357
     358
     359
     360
     361
     362
     363
     364
     365
     366
     367
     368
     369
     370
     371
     372
     373
     374
     375
     376
     377
     378
     379
     380
     381
     382
     383
     384
     385
     386
     387
     388
     389
     390
     391
     392
     393
     394
     395
     396
     397
     398
     399
     400
     401
     402
     403
     404
     405
     406
     407
     408
     409
     410
     411
     412
     413
     414
     415
     416
     417
     418
     419
     420
     421
     422
     423
     424
     425
     426
     427
     428
     429
     430
     431
     432
     433
     434
     435
     436
     437
     438
     439
     440
     441
     442
     443
     444
     445
     446
     447
     448
     449
     450
     451
     452
     453
     454
     455
     456
     457
     458
     459
     460
     461
     462
     463
     464
     465
     466
     467
     468
     469
     470
     471
     472
     473
     474
     475
     476
     477
     478
     479
     480
     481
     482
     483
     484
     485
     486
     487
     488
     489
     490
     491
     492
     493
     494
     495
     496
     497
     498
     499
     500
     501
     502
     503
     504
     505
     506
     507
     508
     509
     510
     511
     512
     513
     514
     515
     516
     517
     518
    263519}
Note: See TracChangeset for help on using the changeset viewer.