WordPress.org

Making WordPress.org

Changeset 8789


Ignore:
Timestamp:
05/14/2019 06:58:26 AM (3 years ago)
Author:
dd32
Message:

Theme Directory: Start extracting headers from the readme.txt file in themes.

This is an MVP and does not use a proper readme parser as used in the Plugin Directory.
This is intended to collect the minimal header data we current have a need for.

In the future it's likely this will be expanded to allow more data from readme.txt to be utilised, but that's not the current priority.

See #3718

File:
1 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/theme-directory/class-wporg-themes-upload.php

    r6940 r8789  
    6464
    6565    /**
     66     * The theme readme.txt data.
     67     *
     68     * @var array
     69     */
     70     protected $readme;
     71
     72    /**
    6673     * Trac ticket information.
    6774     *
     
    7683     */
    7784    protected $trac;
     85
     86    /**
     87     * The list of headers to extract from readme.txt.
     88     *
     89     * @var array
     90     */
     91    protected $readme_header_fields = array(
     92        'tested'       => 'tested up to',
     93        'requires'     => 'requires at least',
     94        'requires_php' => 'requires php',
     95        'readme_tags'  => 'tags',
     96        'contributors' => 'contributors',
     97        'license'      => 'license',
     98        'license_uri'  => 'license uri',
     99    );
    78100
    79101    /**
     
    108130            );
    109131        }
     132
     133        // Do we have a readme.txt? Fetch extra data from there too.
     134        $this->readme = $this->get_readme_data( $theme_files );
    110135
    111136        // We have a stylesheet, let's set up the theme, theme post, and author.
     
    371396
    372397        return (string) array_pop( $stylesheets );
     398    }
     399
     400    /**
     401     * Find the first readme.txt file, and extract it's headers.
     402     *
     403     * @param array $theme_files The theme files.
     404     * @return array Array of headers if present.
     405     */
     406    public function get_readme_data( $theme_files ) {
     407        $readmes = preg_grep( '/readme.txt/i', $theme_files );
     408        usort( $readmes, array( $this, 'sort_by_string_length' ) );
     409
     410        if ( ! $readmes ) {
     411            return array();
     412        }
     413
     414        $readme_txt = (string) array_pop( $readmes );
     415
     416        // WARNING: This function call will be changed in the future to a readme parser.
     417        $data = get_file_data( $readme_txt, $this->readme_header_fields );
     418        $data = array_filter( $data, 'strlen' );
     419
     420        // Sanitize Contributors down to a user_nicename
     421        if ( isset( $data['contributors'] ) ) {
     422            $data['contributors'] = explode( ',', $data['contributors'] );
     423            $data['contributors'] = array_map( 'trim', $data['contributors'] );
     424            foreach ( $data['contributors'] as $i => $name ) {
     425                $user = get_user_by( 'login', $name ) ?: get_user_by( 'slug', $name );
     426
     427                if ( $user ) {
     428                    $data['contributors'][ $i ] = $user->user_nicename;
     429                } else {
     430                    unset( $data['contributors'][ $i ] );
     431                }
     432            }
     433
     434            if ( ! $data['contributors'] ) {
     435                unset( $data['contributors'] );
     436            }
     437        }
     438
     439        return $data;
    373440    }
    374441
     
    724791
    725792        // Finally, add post meta.
    726         $post_meta   = array(
     793        $post_meta = array(
    727794            '_theme_url'   => $this->theme->get( 'ThemeURI' ),
    728795            '_author_url'  => $this->theme->get( 'AuthorURI' ),
     
    731798            '_screenshot'  => $this->theme->screenshot,
    732799        );
     800
     801        // Store readme.txt data if present.
     802        foreach ( $this->readme as $field => $data ) {
     803            $post_meta[ "_{$field}" ] = $data;
     804        }
    733805
    734806        foreach ( $post_meta as $meta_key => $meta_value ) {
Note: See TracChangeset for help on using the changeset viewer.