Making WordPress.org

Changeset 13162


Ignore:
Timestamp:
02/01/2024 04:11:12 AM (2 years ago)
Author:
dd32
Message:

Plugin Directory: Readme: Parse readme's that lack a plugin header.

Fixes #7428.

File:
1 edited

Legend:

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

    r13148 r13162  
    228228            $this->warnings['invalid_plugin_name_header'] = true;
    229229
    230             $this->name = $line = $this->get_first_nonwhitespace( $contents );
    231 
    232             // Ensure that the line read wasn't an actual header or description.
    233             if ( strlen( $line ) > 50 || preg_match( '~^(' . implode( '|', array_keys( $this->valid_headers ) ) . ')\s*:~i', $line ) ) {
    234                 $this->name = false;
    235                 array_unshift( $contents, $line );
    236             }
     230            $this->name = false;
     231            $line       = $this->get_first_nonwhitespace( $contents );
     232
     233            // Ensure that the line read doesn't look like a description.
     234            if ( strlen( $line ) < 50 && ! $this->parse_possible_header( $line, true /* only valid headers */ ) ) {
     235                $this->name = $this->sanitize_text( trim( $line, "#= \t\0\x0B" ) );
     236            }
     237        }
     238
     239        // It's possible to leave the plugin name header off entirely.
     240        if ( $this->parse_possible_header( $this->name, true /* only valid headers */ ) ) {
     241            array_unshift( $contents, $line );
     242
     243            $this->warnings['invalid_plugin_name_header'] = true;
     244            $this->name                                   = false;
    237245        }
    238246
     
    243251        $last_line_was_blank = false;
    244252        do {
    245             $value = null;
     253            $value  = null;
     254            $header = $this->parse_possible_header( $line );
     255
    246256            // If it doesn't look like a header value, maybe break to the next section.
    247             if ( ! str_contains( $line, ':' ) || str_starts_with( $line, '#' ) || str_starts_with( $line, '=' ) ) {
     257            if ( ! $header ) {
    248258                if ( empty( $line ) ) {
    249259                    // Some plugins have line-breaks within the headers...
     
    256266            }
    257267
    258             $bits                = explode( ':', trim( $line ), 2 );
    259             list( $key, $value ) = $bits;
    260             $key                 = strtolower( trim( $key, " \t*-\r\n" ) );
     268            list( $key, $value ) = $header;
    261269
    262270            if ( isset( $this->valid_headers[ $key ] ) ) {
    263                 $headers[ $this->valid_headers[ $key ] ] = trim( $value );
     271                $headers[ $this->valid_headers[ $key ] ] = $value;
    264272            } elseif ( $last_line_was_blank ) {
    265273                // If we skipped over a blank line, and then ended up with an unexpected header, assume we parsed too far and ended up in the Short Description.
     
    520528
    521529        return trim( $desc );
     530    }
     531
     532    /**
     533     * Parse a line to see if it's a header.
     534     *
     535     * @access protected
     536     *
     537     * @param string $line       The line from the readme to parse.
     538     * @param bool   $only_valid Whether to only return a valid known header.
     539     * @return false|array
     540     */
     541    protected function parse_possible_header( $line, $only_valid = false ) {
     542        if ( ! str_contains( $line, ':' ) || str_starts_with( $line, '#' ) || str_starts_with( $line, '=' ) ) {
     543            return false;
     544        }
     545
     546        list( $key, $value ) = explode( ':', $line, 2 );
     547        $key                 = strtolower( trim( $key, " \t*-\r\n" ) );
     548        $value               = trim( $value, " \t*-\r\n" );
     549
     550        if ( $only_valid && ! isset( $this->valid_headers[ $key ] ) ) {
     551            return false;
     552        }
     553
     554        return [ $key, $value ];
    522555    }
    523556
Note: See TracChangeset for help on using the changeset viewer.