Making WordPress.org

Ticket #2538: 2538.diff

File 2538.diff, 2.4 KB (added by tellyworth, 6 years ago)

Add a simple preview to the Readme Validator

  • readme/class-parser.php

     
    161161        }
    162162
    163163        /**
     164         * Parses a readme by URL.
     165         *
     166         * @param string $url The URL of the readme to validate.
     167         * @return bool
     168         */
     169        public function parse_from_url( $url ) {
     170                $url = esc_url_raw( $url );
     171
     172                if ( strtolower( substr( $url, -10 ) ) != 'readme.txt' ) {
     173                        return false;
     174                }
     175
     176                $readme = wp_safe_remote_get( $url );
     177                if ( ! $readme_text = wp_remote_retrieve_body( $readme ) ) {
     178                        return false;
     179                }
     180
     181                return $this->parse_readme_contents( $readme_text );
     182        }
     183
     184        /**
     185         * Parse readme contents by file.
     186         *
    164187         * @param string $file
    165188         * @return bool
    166189         */
    167190        protected function parse_readme( $file ) {
    168191                $contents = file_get_contents( $file );
     192                return $this->parse_readme_contents( $contents );
     193        }
     194
     195        /**
     196         * Parse readme contents by string.
     197         *
     198         * @param string $file
     199         * @return bool
     200         */
     201        protected function parse_readme_contents( $contents ) {
    169202                if ( preg_match( '!!u', $contents ) ) {
    170203                        $contents = preg_split( '!\R!u', $contents );
    171204                } else {
  • shortcodes/class-readme-validator.php

     
    22namespace WordPressdotorg\Plugin_Directory\Shortcodes;
    33
    44use WordPressdotorg\Plugin_Directory\Readme\Validator;
     5use WordPressdotorg\Plugin_Directory\Readme\Parser;
    56
    67class Readme_Validator {
    78
     
    9192                }
    9293
    9394                echo $output;
     95
     96                $parser = new Parser( false );
     97                if ( ! empty( $_POST['readme_url'] ) ) {
     98                        $parsed = $parser->parse_from_url( wp_unslash( $_POST['readme_url'] ) );
     99                } elseif ( ! empty( $_POST['readme_contents'] ) ) {
     100                        $parsed = $parser->parse_readme( base64_decode( wp_unslash( $_REQUEST['readme_contents'] ) ) );
     101                }
     102
     103                if ( $parsed && !empty( $parser-sections ) ) {
     104                        $output = '<h2>' . __( 'Preview', 'wporg-plugins' ) . '</h2>';
     105                        $output .= '<div class="readme-content">';
     106                        foreach ( $parser->sections as $title => $section ) {
     107                                $output .= '<h3 style="text-transform: capitalize;">' . esc_html( $title ) . '</h3>';
     108                                $output .= wp_kses_post( $section );
     109                        }
     110                        $output .= '</div>';
     111                        echo $output;
     112                }
    94113        }
    95114}