Making WordPress.org

Changeset 13235


Ignore:
Timestamp:
02/22/2024 05:46:59 AM (3 years ago)
Author:
dd32
Message:

Plugin Directory: Introduce a maximum length to the readme sections.

See #7477.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/readme
Files:
2 edited

Legend:

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

    r13162 r13235  
    100100         * @var array
    101101         */
    102         private $expected_sections = array(
     102        public $expected_sections = array(
    103103                'description',
    104104                'installation',
     
    115115         * @var array
    116116         */
    117         private $alias_sections = array(
     117        public $alias_sections = array(
    118118                'frequently_asked_questions' => 'faq',
    119119                'change_log'                 => 'changelog',
     
    126126         * @var array
    127127         */
    128         private $valid_headers = array(
     128        public $valid_headers = array(
    129129                'tested'            => 'tested',
    130130                'tested up to'      => 'tested',
     
    149149                'wordpress',
    150150        );
     151
     152        /**
     153         * The maximum field lengths for the readme.
     154         *
     155         * @var array
     156         */
     157        public $maximum_field_lengths = array(
     158                'short_description' => 150,
     159                'section'           => 1500,
     160        );
     161
     162        /**
     163         * The raw contents of the readme file.
     164         *
     165         * @var string
     166         */
     167        public $raw_contents = '';
    151168
    152169        /**
     
    197214         */
    198215        protected function parse_readme_contents( $contents ) {
     216                $this->raw_contents = $contents;
     217
    199218                if ( preg_match( '!!u', $contents ) ) {
    200219                        $contents = preg_split( '!\R!u', $contents );
     
    411430                }
    412431
     432                foreach ( $this->sections as $section => $content ) {
     433                        $this->sections[ $section ] = $this->trim_length( $content, 'section', 'words' );
     434
     435                        if ( $content !== $this->sections[ $section ] ) {
     436                                $this->warnings["trimmed_section_{$section}"] = true;
     437                        }
     438                }
     439
    413440                // Display FAQs as a definition list.
    414441                if ( isset( $this->sections['faq'] ) ) {
     
    432459                $this->short_description = $this->parse_markdown( $this->short_description );
    433460                $this->short_description = wp_strip_all_tags( $this->short_description );
    434                 $short_description       = $this->trim_length( $this->short_description, 150 );
     461                $short_description       = $this->trim_length( $this->short_description, 'short_description' );
    435462                if ( $short_description !== $this->short_description ) {
    436463                        if ( empty( $this->warnings['no_short_description_present'] ) ) {
     
    506533         * @param string $desc
    507534         * @param int    $length
     535         * @param string $type   The type of the length, 'char' or 'words'.
    508536         * @return string
    509537         */
    510         protected function trim_length( $desc, $length = 150 ) {
     538        protected function trim_length( $desc, $length = 150, $type = 'char' ) {
     539                if ( is_string( $length ) ) {
     540                        $length = $this->maximum_field_lengths[ $length ] ?? $length;
     541                }
     542
     543                if ( 'words' === $type ) {
     544                        return wp_trim_words( $desc, $length, ' …' );
     545                }
     546
    511547                // Apply the length restriction without counting html entities.
    512548                $str_length = mb_strlen( html_entity_decode( $desc ) ?: $desc );
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/readme/class-validator.php

    r13133 r13235  
    181181                        );
    182182                } elseif( isset( $readme->warnings['trimmed_short_description'] ) ) {
    183                         $notes[] = sprintf(
     183                        $warnings[] = sprintf(
    184184                                /* translators: %s: section title */
    185185                                __( 'The %s section is too long and was truncated. A maximum of 150 characters is supported.', 'wporg-plugins' ),
    186                                 '<code>Short Description</code>'
     186                                '<code>Short Description</code>',
     187                                number_format_i18n( $readme->maximum_field_lengths['short_description'] )
     188                        );
     189                }
     190
     191                $trimmed_sections = array_filter( $readme->warnings, function( $warning ) {
     192                        return str_contains( $warning, 'trimmed_section_' );
     193                }, ARRAY_FILTER_USE_KEY );
     194                foreach ( $trimmed_sections as $section_name => $dummy ) {
     195                        $section_name = str_replace( 'trimmed_section_', '', $section_name );
     196                        $warnings[]   = sprintf(
     197                                /* translators: %s: section title */
     198                                __( 'The %s section is too long and was truncated. A maximum of %s words is supported.', 'wporg-plugins' ),
     199                                '<code>' . esc_html( ucwords( str_replace( '_', ' ', $section_name ) ) ) . '</code>',
     200                                number_format_i18n( $readme->maximum_field_lengths[ 'section' ] )
    187201                        );
    188202                }
Note: See TracChangeset for help on using the changeset viewer.