Changeset 4261 for sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/readme/class-validator.php
- Timestamp:
- 10/19/2016 03:27:01 AM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/readme/class-validator.php
r4259 r4261 22 22 23 23 /** 24 * Validator constructor. 24 * Validates a readme by URL. 25 * 26 * @param string $url The URL of the readme to validate. 27 * @return array Array of the readme validation results. 25 28 */ 26 private function __construct() { 27 add_action( 'plugin_page_readme_validator', array( $this, 'add_form_fields' ) ); 28 add_action( 'plugin_page_readme_validator', array( $this, 'validate' ) ); 29 public function validate_url( $url ) { 30 $url = esc_url_raw( $url ); 31 32 if ( strtolower( substr( $url, -10 ) ) != 'readme.txt' ) { 33 /* Translators: File name; */ 34 $error = sprintf( __( 'URL must end in %s!', 'wporg-plugins' ), '<code>readme.txt</code>' ); 35 return array( 36 'errors' => array( $error ) 37 ); 38 } 39 40 $readme = wp_safe_remote_get( $url ); 41 if ( ! $readme_text = wp_remote_retrieve_body( $readme ) ) { 42 $error = __( 'Invalid readme.txt URL.', 'wporg-plugins' ); 43 return array( 44 'errors' => array( $error ) 45 ); 46 } 47 48 return $this->validate_content( $readme_text ); 29 49 } 30 50 31 51 /** 32 * Displays a for to validate readme.txt files and blobs of text. 33 */ 34 public function display() { 35 ?> 36 <div class="wrap"> 37 <h2><?php _e( 'WordPress Plugin readme.txt Validator', 'wporg-plugins' ); ?></h2> 38 <?php settings_errors( 'wporg-plugins-readme' ); ?> 39 <form method="post" action="<?php echo esc_url( add_query_arg( array( 'post_type' => 'plugin', 'page' => 'readme_validator' ), admin_url( 'edit.php' ) ) ); ?>"> 40 <?php 41 wp_nonce_field( 'validate-readme' ); 42 do_settings_sections( 'readme_validator' ); 43 submit_button( __( 'Validate', 'wporg-plugins' ) ); 44 ?> 45 </form> 46 </div> 47 <?php 48 } 49 50 /** 51 * Registers form fields for this admin page. 52 */ 53 public function add_form_fields() { 54 add_settings_section( 'default', '', array( $this, 'section_description' ), 'readme_validator' ); 55 56 add_settings_field( 'readme_url', __( 'URL to readme.txt file', 'wporg-plugins' ), array( $this, 'url_input' ), 'readme_validator', 'default', array( 57 'label_for' => 'readme_url', 58 ) ); 59 add_settings_field( 'readme_text', __( 'Text of readme.txt', 'wporg-plugins' ), array( $this, 'textarea' ), 'readme_validator', 'default', array( 60 'label_for' => 'readme_contents', 61 ) ); 62 } 63 64 /** 65 * Validates readme.txt contents and adds feedback. 66 */ 67 public function validate() { 68 if ( ! isset( $_REQUEST['_wpnonce'] ) ) { 69 return; 70 } 71 check_admin_referer( 'validate-readme' ); 72 73 $readme = ''; 74 75 if ( ! empty( $_REQUEST['readme_url'] ) ) { 76 $url = esc_url_raw( $_REQUEST['readme_url'] ); 77 78 if ( strtolower( substr( $url, -10 ) ) != 'readme.txt' ) { 79 /* Translators: File name; */ 80 add_settings_error( 'wporg-plugins-readme', 'readme-validator', sprintf( __( 'URL must end in %s!', 'wporg-plugins' ), '<code>readme.txt</code>' ) ); 81 return; 82 } 83 84 if ( ! $readme = @file_get_contents( $url ) ) { 85 add_settings_error( 'wporg-plugins-readme', 'readme-validator', __( 'Invalid readme.txt URL.', 'wporg-plugins' ) ); 86 return; 87 } 88 89 } elseif ( ! empty( $_REQUEST['readme_contents'] ) && is_string( $_REQUEST['readme_contents'] ) ) { 90 $readme = wp_unslash( $_REQUEST['readme_contents'] ); 91 } 92 93 if ( empty( $readme ) ) { 94 return; 95 } 96 97 return $this->validate_content( $readme ); 98 } 99 100 /** 101 * Validates content via a string paramater and adds feedback. 52 * Validates readme contents by string. 53 * 54 * @param string $readme The text of the readme. 55 * @return array Array of the readme validation results. 102 56 */ 103 57 public function validate_content( $readme ) { 104 58 105 $temp_file = Filesystem::temp_directory() . '/readme.txt'; 106 $errors = array(); 107 $warnings = array(); 108 $notes = array(); 59 $readme = new Parser( 'data:text/plain,' . urlencode( $readme ) ); 109 60 110 file_put_contents( $temp_file, $readme ); 111 $readme = new Parser( $temp_file ); 61 $errors = $warnings = $notes = array(); 112 62 113 63 // Fatal errors. 114 64 if ( empty( $readme->name ) ) { 115 65 /* Translators: Plugin header tag; */ 116 add_settings_error( 'wporg-plugins-readme', 'readme-validator', sprintf( __( "Fatal Error:\nNo plugin name detected. Plugin names look like: %s", 'wporg-plugins' ), '<code>=== Plugin Name ===</code>' ) ); 117 $errors[] = array( 'error', sprintf( __( "Fatal Error:\nNo plugin name detected. Plugin names look like: %s", 'wporg-plugins' ), '<code>=== Plugin Name ===</code>' ) ); 118 return $errors; 66 $errors[] = sprintf( __( "No plugin name detected. Plugin names look like: %s", 'wporg-plugins' ), '<code>=== Plugin Name ===</code>' ); 119 67 } 120 68 … … 135 83 /* Translators: Plugin header tag; */ 136 84 $warnings[] = sprintf( __( 'No %s listed.', 'wporg-plugins' ), '<code>Contributors</code>' ); 137 }138 139 if ( $warnings ) {140 $message = __( 'Warnings:', 'wporg-plugins' ) . "\n<ul class='warning error'>\n";141 foreach ( $warnings as $warning ) {142 $message .= "<li>$warning</li>\n";143 }144 $message .= "</ul>\n</div>";145 146 if ( function_exists( 'add_settings_error' ) ) {147 add_settings_error( 'wporg-plugins-readme', 'readme-validator', $message, 'notice-warning' );148 }149 return $message;150 85 } 151 86 … … 171 106 } 172 107 173 if ( $notes ) { 174 $message = __( 'Notes:' ) . "\n<ul class='note error'>\n"; 175 foreach ( $notes as $note ) { 176 $message .= "<li>$note</li>\n"; 177 } 178 $message .= "</ul>\n</div>"; 108 return compact( 'errors', 'warnings', 'notes' ); 179 109 180 if ( function_exists( 'add_settings_error' ) ) {181 add_settings_error( 'wporg-plugins-readme', 'readme-validator', $message, 'notice-info' );182 }183 return $message;184 }185 186 /* Translators: File name; */187 if ( function_exists( 'add_settings_error' ) ) {188 add_settings_error( 'wporg-plugins-readme', 'readme-validator', sprintf( __( 'Your %s rocks. Seriously. Flying colors.', 'wporg-plugins' ), '<code>readme.txt</code>' ), 'updated' );189 }190 110 } 191 111 192 193 /**194 * Help text for the form following after it.195 */196 public function section_description() {197 /* Translators: File name; */198 echo '<p>' . sprintf( __( 'Enter the URL to your %s file or paste its content below.' ), '<code>readme.txt</code>' ) . '</p>';199 }200 201 /**202 * Displays an input field for the readme.txt URL.203 */204 public function url_input() {205 $url = empty( $_REQUEST['readme_url'] ) ? '' : $_REQUEST['readme_url'];206 ?>207 <label>208 <input type="url" id="readme_url" name="readme_url" size="70" value="<?php echo esc_url( $url ); ?>" />209 </label>210 <?php211 }212 213 /**214 * Displays a textarea for readme.txt blobs.215 */216 public function textarea() {217 $text = empty( $_REQUEST['readme_contents'] ) ? '' : wp_unslash( $_REQUEST['readme_contents'] );218 ?>219 <label>220 <textarea type="text" id="readme_contents" class="large-text" name="readme_contents" cols="50" rows="10"><?php echo esc_textarea( $text ); ?></textarea>221 </label>222 <?php223 }224 112 }
Note: See TracChangeset
for help on using the changeset viewer.