WordPress.org

Making WordPress.org

Changeset 10565


Ignore:
Timestamp:
01/11/2021 02:34:51 AM (3 months ago)
Author:
dd32
Message:

Plugin Driectory: Readme: When trimming the short_description to length, decode HTML entities first to limit it to displayed length rather than computed length.

This also ensures that the … is only applied after a shortened string, and restores the functionality to limit it to a full setence if one ends between characters 120-150.

See #5570.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/readme/class-parser.php

    r10430 r10565  
    470470     */
    471471    protected function trim_length( $desc, $length = 150 ) {
    472         if ( mb_strlen( $desc ) > $length ) {
    473             $desc = mb_substr( $desc, 0, $length ) . ' …';
    474 
    475             // If not a full sentence, and one ends within 20% of the end, trim it to that.
    476             if ( '.' !== mb_substr( $desc, -1 ) && ( $pos = mb_strrpos( $desc, '.' ) ) > ( 0.8 * $length ) ) {
    477                 $desc = mb_substr( $desc, 0, $pos + 1 );
     472        // Apply the length restriction without counting html entities.
     473        $str_length = mb_strlen( html_entity_decode( $desc ) ?: $desc );
     474
     475        if ( $str_length > $length ) {
     476            $desc = mb_substr( $desc, 0, $length );
     477
     478            // If not a full sentence...
     479            if ( '.' !== mb_substr( $desc, -1 ) ) {
     480                // ..and one ends within 20% of the end, trim it to that.
     481                if ( ( $pos = mb_strrpos( $desc, '.' ) ) > ( 0.8 * $length ) ) {
     482                    $desc = mb_substr( $desc, 0, $pos + 1 );
     483                } else {
     484                    // ..else mark it as being trimmed.
     485                    $desc .= ' …';
     486                }
    478487            }
    479488        }
Note: See TracChangeset for help on using the changeset viewer.