Making WordPress.org


Ignore:
Timestamp:
10/19/2016 03:27:01 AM (8 years ago)
Author:
dd32
Message:

Plugin Directory: Readme Validator: Strip out the wp-admin handling in the readme handler and streamline the error checks a little.

See #2128

File:
1 edited

Legend:

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

    r4259 r4261  
    55class Readme_Validator {
    66
    7     private $errors = array();
    8 
    9     /**
    10      * Fetch the instance of the Readme_Validator class.
    11      *
    12      * @static
    13      */
    14     public static function instance() {
    15         static $instance = null;
    16 
    17         return ! is_null( $instance ) ? $instance : $instance = new Readme_Validator();
    18     }
    19 
    20     private function __construct() {
    21     }
    22 
    23     private function display_errors( $errors ) {
    24         if ( is_array( $errors ) && !empty( $errors ) ) {
    25             $message = __( 'Warnings:', 'wporg-plugins' ) . "\n<ul class='warning error'>\n";
    26             foreach ( $errors as $error ) {
    27                 $message .= "<li>" . esc_html($error) . "</li>\n";
    28             }
    29             $message .= "</ul>\n</div>";
    30 
    31             echo $message;
    32         }
    33 
    34     }
    35 
    367    /**
    378     * Displays a form to validate readme.txt files and blobs of text.
    389     */
    39     public function display() {
    40 
    41         $message = null;
    42         if ( !empty( $_POST ) ) {
    43             $message = self::validate();
    44         }
    45 
     10    public static function display() {
    4611        ?>
    4712        <div class="wrap">
    4813            <h2><?php _e( 'WordPress Plugin readme.txt Validator', 'wporg-plugins' ); ?></h2>
    49             <?php if ( $message ) echo $message; ?>
    50             <form method="post" action="<?php echo esc_url( add_query_arg( array() ) ); ?>">
    51                 <input type="hidden" name="url" value="1" />
    52                 <p>http://<input type="text" name="readme_url" size="70" /> <input type="submit" value="Validate!" /></p>
    53                 <?php
    54                 wp_nonce_field( 'validate-readme-url' );
    55                 ?>
     14
     15            <?php
     16            if ( $_POST ) {
     17                self::validate_readme();
     18            }
     19            ?>
     20
     21            <form method="post" action="">
     22                <p>
     23                    <input type="text" name="readme_url" size="70" placeholder="https://" value="<?php if ( isset( $_GET['readme_url'] ) ) { echo esc_attr( $_POST['readme_url'] ); } ?>" />
     24                    <input type="submit" value="<?php esc_attr_e( 'Validate!', 'wporg-plugins' ); ?>" />
     25                </p>
    5626            </form>
     27
    5728            <p><?php _e( '... or paste your <code>readme.txt</code> here:', 'wporg-plugins' ); ?></p>
    58             <form method="post" action="<?php echo esc_url( add_query_arg( array() ) ); ?>">
    59                 <input type="hidden" name="text" value="1" />
    60                 <textarea rows="20" cols="100" name="readme_contents"></textarea>
    61                 <p><input type="submit" value="Validate!" /></p>
    62                 <?php
    63                 wp_nonce_field( 'validate-readme-text' );
    64                 ?>
     29            <form method="post" action="">
     30                <textarea rows="20" cols="100" name="readme_contents" placeholder="=== Plugin Name ==="><?php
     31                    if ( isset( $_POST['readme_contents'] ) ) {
     32                        echo esc_textarea( wp_unslash( $_POST['readme_contents'] ) );
     33                    }
     34                ?></textarea>
     35                <p><input type="submit" value="<?php esc_attr_e( 'Validate!', 'wporg-plugins' ); ?>" /></p>
    6536            </form>
    6637        </div>
     
    7142     * Validates readme.txt contents and adds feedback.
    7243     */
    73     public function validate() {
     44    protected static function validate_readme() {
     45        if (  !empty( $_POST['readme_url'] ) ) {
     46            $errors = Validator::instance()->validate_url( wp_unslash( $_POST['readme_url'] ) );
    7447
    75         $readme    = '';
     48        } elseif ( !empty( $_POST['readme_contents'] ) ) {
     49            $errors = Validator::instance()->validate_content( wp_unslash( $_REQUEST['readme_contents'] ) );
    7650
    77         if ( !empty( $_POST['url'] )
    78             && !empty( $_POST['readme_url'] )
    79             && !empty( $_POST['_wpnonce'] )
    80             && wp_verify_nonce( $_POST['_wpnonce'], 'validate-readme-url' ) ) {
    81                 $url = esc_url_raw( $_POST['readme_url'] );
    82 
    83                 if ( strtolower( substr( $url, -10 ) ) != 'readme.txt' ) {
    84                     /* Translators: File name; */
    85                     self::instance()->errors[] = sprintf( __( 'URL must end in %s!', 'wporg-plugins' ), '<code>readme.txt</code>' );
    86                     return;
    87                 }
    88 
    89                 if ( ! $readme = @file_get_contents( $url ) ) {
    90                     self::instance()->errors[] = __( 'Invalid readme.txt URL.', 'wporg-plugins' );
    91                     return;
    92                 }
    93             }
    94         elseif ( !empty( $_POST['text'] )
    95             && !empty( $_POST['readme_contents'] )
    96             && !empty( $_POST['_wpnonce'] )
    97             && wp_verify_nonce( $_POST['_wpnonce'], 'validate-readme-text' ) ) {
    98                 $readme = wp_unslash( $_REQUEST['readme_contents'] );
    99             }
    100 
    101         if ( empty( $readme ) ) {
     51        } else {
    10252            return;
    10353        }
    10454
    105         return Validator::instance()->validate_content( $readme );
    106 
     55        $error_types = array(
     56            'errors'   => __( 'Fatal Errors:', 'wporg-plugins' ),
     57            'warnings' => __( 'Warnings:', 'wporg-plugins' ),
     58            'notes'    => __( 'Notes:', 'wporg-plugins' )
     59        );
     60        foreach ( $error_types as $field => $warning_label ) {
     61            if ( !empty( $errors[ $field ] ) ) {
     62                echo "{$warning_label}\n<ul class='{$field} error'>\n";
     63                foreach ( $errors[ $field ] as $notice ) {
     64                    echo "<li>{$notice}</li>\n";
     65                }
     66                echo "</ul>\n";
     67            }
     68        }
    10769    }
    10870}
Note: See TracChangeset for help on using the changeset viewer.