WordPress.org

Making WordPress.org

Changeset 3477


Ignore:
Timestamp:
06/19/2016 08:40:01 PM (4 years ago)
Author:
obenland
Message:

Plugin Directory: Move readme classes into their own namespace.

See #1584.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory
Files:
1 added
3 edited
2 moved

Legend:

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

    r3458 r3477  
    33use \WordPressdotorg\Plugin_Directory;
    44use \WordPressdotorg\Plugin_Directory\Tools;
     5use \WordPressdotorg\Plugin_Directory\Readme\Validator;
    56use \WordPressdotorg\Plugin_Directory\Admin\List_Table\Plugin_Posts;
    67
     
    136137        add_submenu_page( 'edit.php?post_type=plugin', 'Plugin Handbook', 'Plugin Handbook', 'read', 'handbook', function() {} );
    137138
    138         $readme_validator = Readme_Validator::instance();
     139        $readme_validator = Validator::instance();
    139140        add_submenu_page( 'edit.php?post_type=plugin', 'Readme Validator', 'Readme Validator', 'read', 'readme_validator', array( $readme_validator, 'display' ) );
    140141
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/cli/class-import.php

    r3469 r3477  
    22namespace WordPressdotorg\Plugin_Directory\CLI;
    33use WordPressdotorg\Plugin_Directory\Plugin_Directory;
    4 use WordPressdotorg\Plugin_Directory\Readme_Parser;
     4use WordPressdotorg\Plugin_Directory\Readme\Parser;
    55use WordPressdotorg\Plugin_Directory\Template;
    66use WordPressdotorg\Plugin_Directory\Tools;
     
    301301
    302302            $trunk_readme_file = self::PLUGIN_SVN_BASE . "/{$plugin_slug}/trunk/{$trunk_readme_file}";
    303             $trunk_readme = new Readme_Parser( $trunk_readme_file );
     303            $trunk_readme = new Parser( $trunk_readme_file );
    304304
    305305            $stable_tag = $trunk_readme->stable_tag;
     
    355355        // The readme may not actually exist, but that's okay.
    356356        $readme = $this->find_readme_file( $tmp_dir . '/export' );
    357         $readme = new Readme_Parser( $readme );
     357        $readme = new Parser( $readme );
    358358
    359359        // There must be valid plugin headers though.
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/readme/class-parser.php

    r3476 r3477  
    11<?php
    2 namespace WordPressdotorg\Plugin_Directory;
     2namespace WordPressdotorg\Plugin_Directory\Readme;
     3use WordPressdotorg\Plugin_Directory\Markdown;
    34
    45/**
     
    78 * Based on Baikonur_ReadmeParser from https://github.com/rmccue/WordPress-Readme-Parser
    89 *
    9  * @package WordPressdotorg\Plugin_Directory
     10 * @package WordPressdotorg\Plugin_Directory\Readme
    1011 */
    11 class Readme_Parser {
    12     public $name              = '';
    13     public $tags              = array();
    14     public $requires          = '';
    15     public $tested            = '';
    16     public $contributors      = array();
    17     public $stable_tag        = '';
    18     public $donate_link       = '';
     12class Parser {
     13
     14    /**
     15     * @var string
     16     */
     17    public $name = '';
     18
     19    /**
     20     * @var array
     21     */
     22    public $tags = array();
     23
     24    /**
     25     * @var string
     26     */
     27    public $requires = '';
     28
     29    /**
     30     * @var string
     31     */
     32    public $tested = '';
     33
     34    /**
     35     * @var array
     36     */
     37    public $contributors = array();
     38
     39    /**
     40     * @var string
     41     */
     42    public $stable_tag = '';
     43
     44    /**
     45     * @var string
     46     */
     47    public $donate_link = '';
     48
     49    /**
     50     * @var string
     51     */
    1952    public $short_description = '';
    20     public $sections          = array();
    21     public $upgrade_notice    = array();
    22     public $screenshots       = array();
    23 
    24     // These are the readme sections which we expect
     53
     54    /**
     55     * @var array
     56     */
     57    public $sections = array();
     58
     59    /**
     60     * @var array
     61     */
     62    public $upgrade_notice = array();
     63
     64    /**
     65     * @var array
     66     */
     67    public $screenshots = array();
     68
     69    /**
     70     * These are the readme sections that we expect.
     71     *
     72     * @var array
     73     */
    2574    private $expected_sections = array(
    2675        'description',
     
    3382    );
    3483
    35     // We alias these sections, from => to
     84    /**
     85     * We alias these sections, from => to
     86     *
     87     * @var array
     88     */
    3689    private $alias_sections = array(
    3790        'frequently_asked_questions' => 'faq',
    38         'change_log' => 'changelog',
    39         'screenshot' => 'screenshots',
     91        'change_log'                 => 'changelog',
     92        'screenshot'                 => 'screenshots',
    4093    );
    4194
    42     // These are the valid header mappings for the header
     95    /**
     96     * These are the valid header mappings for the header.
     97     *
     98     * @var array
     99     */
    43100    private $valid_headers = array(
    44101        'tested'            => 'tested',
     
    52109    );
    53110
     111    /**
     112     * Parser constructor.
     113     *
     114     * @param string $file
     115     */
    54116    public function __construct( $file ) {
    55117        if ( $file ) {
     
    58120    }
    59121
     122    /**
     123     * @param string $file
     124     * @return bool
     125     */
    60126    protected function parse_readme( $file ) {
    61127        $contents = file_get_contents( $file );
    62128        $contents = preg_split( '!\R!', $contents );
    63 
    64129        $contents = array_map( array( $this, 'strip_newlines' ), $contents );
    65130
    66         // Strip UTF8 BOM if present
    67         if ( strpos( $contents[0], "\xEF\xBB\xBF" ) === 0 ) {
     131        // Strip UTF8 BOM if present.
     132        if ( 0 === strpos( $contents[0], "\xEF\xBB\xBF" ) ) {
    68133            $contents[0] = substr( $contents[0], 3 );
    69134        }
    70135
    71         // Convert UTF-16 files
    72         if ( strpos( $contents[0], "\xFF\xFE" ) === 0 ) {
     136        // Convert UTF-16 files.
     137        if ( 0 === strpos( $contents[0], "\xFF\xFE" ) ) {
    73138            foreach ( $contents as $i => $line ) {
    74                 $contents[$i] = mb_convert_encoding( $line, 'UTF-8', 'UTF-16' );
     139                $contents[ $i ] = mb_convert_encoding( $line, 'UTF-8', 'UTF-16' );
    75140            }
    76141        }
     
    79144        $this->name = $this->sanitize_text( trim( $line, "#= \t\0\x0B" ) );
    80145
    81         // Strip Github style header\n==== underlines
    82         if ( '' === trim( $contents[0], '=-' ) ) {
     146        // Strip Github style header\n==== underlines.
     147        if ( ! empty( $contents ) && '' === trim( $contents[0], '=-' ) ) {
    83148            array_shift( $contents );
    84149        }
     
    87152        if ( 'plugin name' == strtolower( $this->name ) ) {
    88153            $this->name = $line = $this->get_first_nonwhitespace( $contents );
    89             // Ensure that the line read wasn't an actual header or description
     154
     155            // Ensure that the line read wasn't an actual header or description.
    90156            if ( strlen( $line ) > 50 || preg_match( '~^(' . implode( '|', array_keys( $this->valid_headers ) ) . ')\s*:~i', $line ) ) {
    91157                $this->name = false;
     
    94160        }
    95161
    96         // Parse headers
     162        // Parse headers.
    97163        $headers = array();
    98164
     
    100166        do {
    101167            $value = null;
    102             if ( strpos( $line, ':' ) === false ) {
     168            if ( false === strpos( $line, ':' ) ) {
     169
    103170                // Some plugins have line-breaks within the headers.
    104171                if ( ! empty( $line ) ) {
     
    140207        }
    141208
    142         // Parse the short description
     209        // Parse the short description.
    143210        while ( ( $line = array_shift( $contents ) ) !== null ) {
    144211            $trimmed = trim( $line );
     
    148215            }
    149216            if ( ( '=' === $trimmed[0] && isset( $trimmed[1] ) && '=' === $trimmed[1] ) ||
    150                  ( '#' === $trimmed[0] && isset( $trimmed[1] ) && '#' === $trimmed[1] ) ) { // Stop after any Markdown heading
     217                 ( '#' === $trimmed[0] && isset( $trimmed[1] ) && '#' === $trimmed[1] )
     218            ) {
     219
     220                // Stop after any Markdown heading.
    151221                array_unshift( $contents, $line );
    152222                break;
     
    157227        $this->short_description = trim( $this->short_description );
    158228
    159         // Parse the rest of the body
    160         // Prefill the sections, we'll filter out empty sections later.
     229        /*
     230         * Parse the rest of the body.
     231         * Pre-fill the sections, we'll filter out empty sections later.
     232         */
    161233        $this->sections = array_fill_keys( $this->expected_sections, '' );
    162         $current = $section_name = $section_title = '';
     234        $current        = $section_name = $section_title = '';
    163235        while ( ( $line = array_shift( $contents ) ) !== null ) {
    164236            $trimmed = trim( $line );
     
    168240            }
    169241
     242            // Stop only after a ## Markdown header, not a ###.
    170243            if ( ( '=' === $trimmed[0] && isset( $trimmed[1] ) && '=' === $trimmed[1] ) ||
    171                  ( '#' === $trimmed[0] && isset( $trimmed[1] ) && '#' === $trimmed[1] && isset( $trimmed[2] ) && '#' !== $trimmed[2] ) ) { // Stop only after a ## Markdown header, not a ###.
     244                 ( '#' === $trimmed[0] && isset( $trimmed[1] ) && '#' === $trimmed[1] && isset( $trimmed[2] ) && '#' !== $trimmed[2] )
     245            ) {
     246
    172247                if ( ! empty( $section_name ) ) {
    173248                    $this->sections[ $section_name ] .= trim( $current );
     
    210285        }
    211286
    212         // Parse out the Upgrade Notice section into it's own data
     287        // Parse out the Upgrade Notice section into it's own data.
    213288        if ( isset( $this->sections['upgrade_notice'] ) ) {
    214             $lines = explode( "\n", $this->sections['upgrade_notice'] );
     289            $lines   = explode( "\n", $this->sections['upgrade_notice'] );
    215290            $version = null;
    216291            $current = '';
     
    240315
    241316        // Markdownify!
    242         $this->sections          = array_map( array( $this, 'parse_markdown' ), $this->sections );
    243         $this->upgrade_notice    = array_map( array( $this, 'parse_markdown' ), $this->upgrade_notice );
    244 
    245         // Sanitize and trim the short_description to match requirements
     317        $this->sections       = array_map( array( $this, 'parse_markdown' ), $this->sections );
     318        $this->upgrade_notice = array_map( array( $this, 'parse_markdown' ), $this->upgrade_notice );
     319
     320        // Sanitize and trim the short_description to match requirements.
    246321        $this->short_description = $this->sanitize_text( $this->short_description );
    247322        $this->short_description = $this->trim_length( $this->short_description, 150 );
     
    252327            preg_match_all( '#<li>(.*?)</li>#is', $this->sections['screenshots'], $screenshots, PREG_SET_ORDER );
    253328            if ( $screenshots ) {
    254                 $i = 1; // Screenshots start from 1
     329                $i = 1; // Screenshots start from 1.
    255330                foreach ( $screenshots as $ss ) {
    256331                    $this->screenshots[ $i++ ] = $this->filter_text( $ss[1] );
     
    260335        }
    261336
    262         // Filter the HTML
     337        // Filter the HTML.
    263338        $this->sections = array_map( array( $this, 'filter_text' ), $this->sections );
    264339
     
    266341    }
    267342
     343    /**
     344     * @access protected
     345     *
     346     * @param string $contents
     347     * @return string
     348     */
    268349    protected function get_first_nonwhitespace( &$contents ) {
    269350        while ( ( $line = array_shift( $contents ) ) !== null ) {
     
    277358    }
    278359
     360    /**
     361     * @access protected
     362     *
     363     * @param string $line
     364     * @return string
     365     */
    279366    protected function strip_newlines( $line ) {
    280367        return rtrim( $line, "\r\n" );
    281368    }
    282369
     370    /**
     371     * @access protected
     372     *
     373     * @param string $desc
     374     * @param int    $length
     375     * @return string
     376     */
    283377    protected function trim_length( $desc, $length = 150 ) {
    284378        if ( mb_strlen( $desc ) > $length ) {
     
    362456        foreach ( $users as $i => $name ) {
    363457            if ( $user = get_user_by( 'login', $name ) ) {
     458
    364459                // Check the case of the user login matches.
    365460                if ( $name !== $user->user_login ) {
     
    367462                }
    368463            } elseif ( false !== ( $user = get_user_by( 'slug', $name ) ) ) {
    369                 // Overwrite the nicename with the user_login
     464
     465                // Overwrite the nicename with the user_login.
    370466                $users[ $i ] = $user->user_login;
    371467            } else {
    372                 // Unknown user, we'll skip these entirely to encourage correct readmes
     468
     469                // Unknown user, we'll skip these entirely to encourage correct readme files.
    373470                unset( $users[ $i ] );
    374471            }
    375472        }
     473
    376474        return $users;
    377475    }
     
    385483    protected function sanitize_stable_tag( $stable_tag ) {
    386484        $stable_tag = trim( $stable_tag );
    387         $stable_tag = trim( $stable_tag, '"\'' ); // "trunk"
     485        $stable_tag = trim( $stable_tag, '"\'' ); // "trunk"
    388486        $stable_tag = preg_replace( '!^/?tags/!i', '', $stable_tag ); // "tags/1.2.3"
    389487        $stable_tag = preg_replace( '![^a-z0-9_.-]!i', '', $stable_tag );
    390488
    391         // If the stable_tag begins with a ., we treat it as 0.blah
     489        // If the stable_tag begins with a ., we treat it as 0.blah.
    392490        if ( '.' == substr( $stable_tag, 0, 1 ) ) {
    393491            $stable_tag = "0{$stable_tag}";
     
    397495    }
    398496
     497    /**
     498     * @param string $text
     499     * @return string
     500     */
    399501    protected function parse_markdown( $text ) {
    400502        static $markdown = null;
     503
    401504        if ( is_null( $markdown ) ) {
    402505            $markdown = new Markdown();
     
    405508        return $markdown->transform( $text );
    406509    }
    407 
    408510}
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/readme/class-validator.php

    r3476 r3477  
    11<?php
    2 namespace WordPressdotorg\Plugin_Directory\Admin;
    3 use \WordPressdotorg\Plugin_Directory\Readme_Parser;
     2namespace WordPressdotorg\Plugin_Directory\Readme;
    43use WordPressdotorg\Plugin_Directory\Tools\Filesystem;
    54
     
    76 * A wp-admin interface to validate readme files.
    87 *
    9  * @package WordPressdotorg\Plugin_Directory\Admin
     8 * @package WordPressdotorg\Plugin_Directory\Readme
    109 */
    11 class Readme_Validator {
    12 
    13     /**
    14      * Fetch the instance of the Readme_Validator class.
     10class Validator {
     11
     12    /**
     13     * Fetch the instance of the Validator class.
    1514     *
    1615     * @static
     
    1918        static $instance = null;
    2019
    21         return ! is_null( $instance ) ? $instance : $instance = new Readme_Validator();
    22     }
    23 
    24     /**
    25      * Constructor.
     20        return ! is_null( $instance ) ? $instance : $instance = new Validator();
     21    }
     22
     23    /**
     24     * Validator constructor.
    2625     */
    2726    private function __construct() {
     
    10099
    101100        file_put_contents( $temp_file, $readme );
    102         $readme = new Readme_Parser( $temp_file );
     101        $readme = new Parser( $temp_file );
    103102
    104103        // Fatal errors.
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/shortcodes/class-upload-handler.php

    r3467 r3477  
    11<?php
    22namespace WordPressdotorg\Plugin_Directory\Shortcodes;
    3 use WordPressdotorg\Plugin_Directory\Readme_Parser;
     3use WordPressdotorg\Plugin_Directory\Readme\Parser;
    44use WordPressdotorg\Plugin_Directory\Plugin_Directory;
    55use WordPressdotorg\Plugin_Directory\Tools\Filesystem;
     
    144144            );
    145145        }
    146         $readme = new Readme_Parser( $readme );
     146        $readme = new Parser( $readme );
    147147
    148148        // Pass it through Plugin Check and see how great this plugin really is.
Note: See TracChangeset for help on using the changeset viewer.