<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" />
<title>[3383] sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/admin: Plugin Directory: First pass at a readme validator.</title>
</head>
<body>

<style type="text/css"><!--
#msg dl.meta { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dl.meta dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer, #logmsg { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt;  }
#msg dl a { font-weight: bold}
#msg dl a:link    { color:#fc3; }
#msg dl a:active  { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fa0 solid; padding: 6px; }
#logmsg { background: #ffc; border: 1px #fa0 solid; padding: 1em 1em 0 1em; }
#logmsg p, #logmsg pre, #logmsg blockquote { margin: 0 0 1em 0; }
#logmsg p, #logmsg li, #logmsg dt, #logmsg dd { line-height: 14pt; }
#logmsg h1, #logmsg h2, #logmsg h3, #logmsg h4, #logmsg h5, #logmsg h6 { margin: .5em 0; }
#logmsg h1:first-child, #logmsg h2:first-child, #logmsg h3:first-child, #logmsg h4:first-child, #logmsg h5:first-child, #logmsg h6:first-child { margin-top: 0; }
#logmsg ul, #logmsg ol { padding: 0; list-style-position: inside; margin: 0 0 0 1em; }
#logmsg ul { text-indent: -1em; padding-left: 1em; }#logmsg ol { text-indent: -1.5em; padding-left: 1.5em; }
#logmsg > ul, #logmsg > ol { margin: 0 0 1em 0; }
#logmsg pre { background: #eee; padding: 1em; }
#logmsg blockquote { border: 1px solid #fa0; border-left-width: 10px; padding: 1em 1em 0 1em; background: white;}
#logmsg dl { margin: 0; }
#logmsg dt { font-weight: bold; }
#logmsg dd { margin: 0; padding: 0 0 0.5em 0; }
#logmsg dd:before { content:'\00bb';}
#logmsg table { border-spacing: 0px; border-collapse: collapse; border-top: 4px solid #fa0; border-bottom: 1px solid #fa0; background: #fff; }
#logmsg table th { text-align: left; font-weight: normal; padding: 0.2em 0.5em; border-top: 1px dotted #fa0; }
#logmsg table td { text-align: right; border-top: 1px dotted #fa0; padding: 0.2em 0.5em; }
#logmsg table thead th { text-align: center; border-bottom: 1px solid #fa0; }
#logmsg table th.Corner { text-align: left; }
#logmsg hr { border: none 0; border-top: 2px dashed #fa0; height: 1px; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff  {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<div id="msg">
<dl class="meta" style="font-size: 105%">
<dt style="float: left; width: 6em; font-weight: bold">Revision</dt> <dd><a style="font-weight: bold" href="http://meta.trac.wordpress.org/changeset/3383">3383</a><script type="application/ld+json">{"@context":"http://schema.org","@type":"EmailMessage","description":"Review this Commit","action":{"@type":"ViewAction","url":"http://meta.trac.wordpress.org/changeset/3383","name":"Review Commit"}}</script></dd>
<dt style="float: left; width: 6em; font-weight: bold">Author</dt> <dd>obenland</dd>
<dt style="float: left; width: 6em; font-weight: bold">Date</dt> <dd>2016-06-16 08:15:28 +0000 (Thu, 16 Jun 2016)</dd>
</dl>

<pre style='padding-left: 1em; margin: 2em 0; border-left: 2px solid #ccc; line-height: 1.25; font-size: 105%; font-family: sans-serif'>Plugin Directory: First pass at a readme validator.

Adds an admin UI and the basic infrastructure to validate readme files.
Needs some more work in the parsing part.

See https://wordpress.slack.com/archives/meta/p1466062950000169.
See <a href="http://meta.trac.wordpress.org/ticket/1571">#1571</a>.</pre>

<h3>Modified Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryadminclasscustomizationsphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/admin/class-customizations.php</a></li>
</ul>

<h3>Added Paths</h3>
<ul>
<li><a href="#sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryadminclassreadmevalidatorphp">sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/admin/class-readme-validator.php</a></li>
</ul>

</div>
<div id="patch">
<h3>Diff</h3>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryadminclasscustomizationsphp"></a>
<div class="modfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Modified: sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/admin/class-customizations.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/admin/class-customizations.php  2016-06-16 07:55:34 UTC (rev 3382)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/admin/class-customizations.php    2016-06-16 08:15:28 UTC (rev 3383)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -134,8 +134,10 @@
</span><span class="cx" style="display: block; padding: 0 10px">                 * If it doesn't have at least one submenu then users who cannot also publish posts will not be able to access the post type.
</span><span class="cx" style="display: block; padding: 0 10px">                 */
</span><span class="cx" style="display: block; padding: 0 10px">                add_submenu_page( 'edit.php?post_type=plugin', 'Plugin Handbook', 'Plugin Handbook', 'read', 'handbook', function() {} );
</span><del style="background-color: #fdd; text-decoration:none; display:block; padding: 0 10px">-                add_submenu_page( 'edit.php?post_type=plugin', 'Readme Validator', 'Readme Validator', 'read', 'readme_validator', function() {} );
</del><span class="cx" style="display: block; padding: 0 10px"> 
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+                $readme_validator = Readme_Validator::instance();
+               add_submenu_page( 'edit.php?post_type=plugin', 'Readme Validator', 'Readme Validator', 'read', 'readme_validator', array( $readme_validator, 'display' ) );
+
</ins><span class="cx" style="display: block; padding: 0 10px">                 if ( ! current_user_can( 'manage_options' ) ) {
</span><span class="cx" style="display: block; padding: 0 10px">                        remove_menu_page( 'index.php' );
</span><span class="cx" style="display: block; padding: 0 10px">                        remove_menu_page( 'profile.php' );
</span></span></pre></div>
<a id="sitestrunkwordpressorgpublic_htmlwpcontentpluginsplugindirectoryadminclassreadmevalidatorphp"></a>
<div class="addfile"><h4 style="background-color: #eee; color: inherit; margin: 1em 0; padding: 1.3em; font-size: 115%">Added: sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/admin/class-readme-validator.php</h4>
<pre class="diff"><span>
<span class="info" style="display: block; padding: 0 10px; color: #888">--- sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/admin/class-readme-validator.php                                (rev 0)
+++ sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/admin/class-readme-validator.php  2016-06-16 08:15:28 UTC (rev 3383)
</span><span class="lines" style="display: block; padding: 0 10px; color: #888">@@ -0,0 +1,206 @@
</span><ins style="background-color: #dfd; text-decoration:none; display:block; padding: 0 10px">+<?php
+namespace WordPressdotorg\Plugin_Directory\Admin;
+use \WordPressdotorg\Plugin_Directory\Readme_Parser;
+use WordPressdotorg\Plugin_Directory\Tools\Filesystem;
+
+/**
+ * A wp-admin interface to validate readme files.
+ *
+ * @package WordPressdotorg\Plugin_Directory\Admin
+ */
+class Readme_Validator {
+
+       /**
+        * Fetch the instance of the Readme_Validator class.
+        *
+        * @static
+        */
+       public static function instance() {
+               static $instance = null;
+
+               return ! is_null( $instance ) ? $instance : $instance = new Readme_Validator();
+       }
+
+       /**
+        * Constructor.
+        */
+       private function __construct() {
+               add_action( 'plugin_page_readme_validator', array( $this, 'add_form_fields' ) );
+               add_action( 'plugin_page_readme_validator', array( $this, 'validate' ) );
+       }
+
+       /**
+        * Displays a for to validate readme.txt files and blobs of text.
+        */
+       public function display() {
+               ?>
+               <div class="wrap">
+                       <h2><?php _e( 'WordPress Plugin readme.txt Validator', 'wporg-plugins' ); ?></h2>
+                       <form method="post" action="<?php echo esc_url( add_query_arg( array( 'post_type' => 'plugin', 'page' => 'readme_validator' ), admin_url( 'edit.php' ) ) ); ?>">
+                               <?php
+                               wp_nonce_field( 'validate-readme' );
+                               do_settings_sections( 'readme_validator' );
+                               submit_button( __( 'Validate', 'wporg-plugins' ) );
+                               ?>
+                       </form>
+               </div>
+               <?php
+       }
+
+       /**
+        * Registers form fields for this admin page.
+        */
+       public function add_form_fields() {
+               add_settings_section( 'default', '', array( $this, 'section_description' ), 'readme_validator' );
+
+               add_settings_field( 'readme_url', __( 'URL to readme.txt file', 'wporg-plugins' ), array( $this, 'url_input' ), 'readme_validator', 'default', array(
+                       'label_for' => 'readme_url',
+               ) );
+               add_settings_field( 'readme_text', __( 'Text of readme.txt', 'wporg-plugins' ),    array( $this, 'textarea' ),  'readme_validator', 'default', array(
+                       'label_for' => 'readme_contents',
+               ) );
+       }
+
+       /**
+        * Validates readme.txt contents and adds feedback.
+        */
+       public function validate() {
+               check_admin_referer( 'validate-readme' );
+
+               $readme    = '';
+               $temp_file = Filesystem::temp_directory() . '/readme.txt';
+               $warnings  = array();
+               $notes     = array();
+
+               if ( ! empty( $_REQUEST['readme_url'] ) ) {
+                       $url = esc_url_raw( $_REQUEST['readme_url'] );
+
+                       if ( strtolower( substr( $url, - 10, 10 ) ) != 'readme.txt' ) {
+                               add_settings_error( 'wporg-plugins', 'readme-validator', __( 'URL must end in <code>readme.txt</code>!', 'wporg-plugins' ) );
+                               return;
+                       }
+
+                       if ( ! $readme = @file_get_contents( $url ) ) {
+                               add_settings_error( 'wporg-plugins', 'readme-validator', __( 'Invalid readme.txt URL.', 'wporg-plugins' ) );
+                               return;
+                       }
+
+               } elseif ( ! empty( $_REQUEST['readme_contents'] ) && is_string( $_REQUEST['readme_contents'] ) ) {
+                       $readme = wp_unslash( $_REQUEST['readme_contents'] );
+               }
+
+               if ( empty( $readme ) ) {
+                       return;
+               }
+
+               file_put_contents( $temp_file, $readme );
+               $readme = new Readme_Parser( $temp_file );
+
+               // Fatal errors.
+               if ( empty( $readme->name ) ) {
+                       add_settings_error( 'wporg-plugins', 'readme-validator', __( "Fatal Error:\nNo plugin name detected. Plugin names look like: <code>=== Plugin Name ===</code>", 'wporg-plugins' ) );
+                       return;
+               }
+
+               // Warnings.
+               if ( empty( $readme->requires_at_least ) ) {
+                       $warnings[] = __( '<code>Requires at least</code> is missing.', 'wporg-plugins' );
+               }
+               if ( empty( $readme->tested_up_to ) ) {
+                       $warnings[] = __( '<code>Tested up to</code> is missing.', 'wporg-plugins' );
+               }
+               if ( empty( $readme->stable_tag ) ) {
+                       $warnings[] = __( '<code>Stable tag</code> is missing.  Hint: If you treat <code>/trunk/</code> as stable, put <code>Stable tag: trunk</code>.', 'wporg-plugins' );
+               }
+               if ( ! count( $readme->contributors ) ) {
+                       $warnings[] = __( 'No <code>Contributors</code> listed.', 'wporg-plugins' );
+               }
+               if ( ! count( $readme->tags ) ) {
+                       $warnings[] = __( 'No <code>Tags</code> specified', 'wporg-plugins' );
+               }
+               if ( ! empty( $readme->is_excerpt ) ) {
+                       $warnings[] = __( 'No <code>== Description ==</code> section was found... your short description section will be used instead.', 'wporg-plugins' );
+               }
+               if ( ! empty( $readme->is_truncated ) ) {
+                       $warnings[] = __( 'Your short description exceeds the 150 character limit.', 'wporg-plugins' );
+               }
+
+               if ( $warnings ) {
+                       $message = __( 'Warnings:', 'wporg-plugins' ) . "\n<ul class='warning error'>\n";
+                       foreach ( $warnings as $warning ) {
+                               $message .= "<li>$warning</li>\n";
+                       }
+                       $message .= "</ul>\n</div>";
+
+                       add_settings_error( 'wporg-plugins', 'readme-validator', $message, 'notice-warning' );
+                       return;
+               }
+
+               // Notes.
+               if ( empty( $readme->license ) ) {
+                       $notes[] = __( 'No <code>License</code> is specified. WordPress is licensed under &#8220;GPLv2 or later&#8221;', 'wporg-plugins' );
+               }
+               if ( empty( $readme->sections['installation'] ) ) {
+                       $notes[] = __( 'No <code>== Installation ==</code> section was found', 'wporg-plugins' );
+               }
+               if ( empty( $readme->sections['frequently_asked_questions'] ) ) {
+                       $notes[] = __( 'No <code>== Frequently Asked Questions ==</code> section was found', 'wporg-plugins' );
+               }
+               if ( empty( $readme->sections['changelog'] ) ) {
+                       $notes[] = __( 'No <code>== Changelog ==</code> section was found', 'wporg-plugins' );
+               }
+               if ( empty( $readme->upgrade_notice ) ) {
+                       $notes[] = __( 'No <code>== Upgrade Notice ==</code> section was found', 'wporg-plugins' );
+               }
+               if ( empty( $readme->sections['screenshots'] ) ) {
+                       $notes[] = __( 'No <code>== Screenshots ==</code> section was found', 'wporg-plugins' );
+               }
+               if ( empty( $readme->donate_link ) ) {
+                       $notes[] = __( 'No donate link was found', 'wporg-plugins' );
+               }
+
+               if ( $notes ) {
+                       $message = __( 'Notes:' ) . "\n<ul class='note error'>\n";
+                       foreach ( $notes as $note ) {
+                               $message .= "<li>$note</li>\n";
+                       }
+                       $message .= "</ul>\n</div>";
+
+                       add_settings_error( 'wporg-plugins', 'readme-validator', $message, 'notice-info' );
+                       return;
+               }
+
+               add_settings_error( 'wporg-plugins', 'readme-validator', __( 'Your <code>readme.txt</code> rocks.  Seriously.  Flying colors.', 'wporg-plugins' ), 'updated' );
+       }
+
+       /**
+        * Help text for the form following after it.
+        */
+       public function section_description() {
+               echo '<p>' . __( 'Enter the URL to your <code>readme.txt</code> file or paste its content below.' ) . '</p>';
+       }
+
+       /**
+        * Displays an input field for the readme.txt URL.
+        */
+       public function url_input() {
+               $url = empty( $_REQUEST['readme_url'] ) ? '' : $_REQUEST['readme_url'];
+               ?>
+               <label>
+                       <input type="url" id="readme_url" name="readme_url" size="70" value="<?php echo esc_url( $url ); ?>" />
+               </label>
+               <?php
+       }
+
+       /**
+        * Displays a textarea for readme.txt blobs.
+        */
+       public function textarea() {
+               $text = empty( $_REQUEST['readme_contents'] ) ? '' : $_REQUEST['readme_contents'];
+               ?>
+               <label>
+                       <textarea type="text" id="readme_contents" class="large-text" name="readme_contents" cols="50" rows="10"><?php echo esc_textarea( $text ); ?></textarea>
+               </label>
+               <?php
+       }
+}
</ins></span></pre>
</div>
</div>

</body>
</html>