Making WordPress.org

Ticket #5306: 5306-review-tools.2.diff

File 5306-review-tools.2.diff, 4.0 KB (added by tellyworth, 3 years ago)

Improved patch

  • shortcodes/class-block-validator.php

     
    22namespace WordPressdotorg\Plugin_Directory\Shortcodes;
    33
    44use WordPressdotorg\Plugin_Directory\CLI\Block_Plugin_Checker;
     5use WordPressdotorg\Plugin_Directory\Plugin_Directory;
    56
    67class Block_Validator {
    78
     
    2728                        </form>
    2829
    2930                        <?php
    30                         if ( $_POST && wp_verify_nonce( $_POST['block-nonce'], 'validate-block-plugin' ) ) {
     31                        if ( $_POST && !empty( $_POST['plugin_url'] ) && wp_verify_nonce( $_POST['block-nonce'], 'validate-block-plugin' ) ) {
    3132                                self::validate_block( $_POST['plugin_url'] );
     33                        } elseif ( $_POST && !empty( $_POST['block-directory-edit'] ) ) {
     34                                $post = get_post( intval( $_POST['plugin-id'] ) );
     35                                if ( $post && wp_verify_nonce( $_POST['block-directory-nonce'], 'block-directory-edit-' . $post->ID ) ) {
     36                                        if ( current_user_can( 'edit_post', $post->ID ) ) {
     37                                                $terms = wp_list_pluck( get_the_terms( $post->ID, 'plugin_section' ), 'slug' );
     38                                                if ( 'add' === $_POST['block-directory-edit'] ) {
     39                                                        $terms[] = 'block';
     40                                                } elseif ( 'remove' === $_POST['block-directory-edit'] ) {
     41                                                        $terms = array_diff( $terms, array( 'block' ) );
     42                                                }
     43                                                wp_set_object_terms( $post->ID, $terms, 'plugin_section' );
     44                                        }
     45
     46                                        self::validate_block( $post->post_name );
     47                                }
    3248                        }
    3349                        ?>
    3450                </div>
     
    4056                return ob_get_clean();
    4157        }
    4258
     59        protected static function plugin_is_in_block_directory( $slug ) {
     60                $plugin = Plugin_Directory::get_plugin_post( $slug );
     61
     62                return (
     63                        $plugin &&
     64                        $plugin->post_name === $slug &&
     65                        has_term( 'block', 'plugin_section', $plugin )
     66                );
     67        }
     68
    4369        /**
    4470         * Validates readme.txt contents and adds feedback.
    4571         *
     
    6288                        echo '</p>';
    6389                }
    6490
     91                if ( $checker->slug ) {
     92                        $plugin = Plugin_Directory::get_plugin_post( $checker->slug );
     93                        if ( current_user_can( 'edit_post', $plugin->ID ) ) {
     94                                echo '<form method="post">';
     95                                echo '<fieldset>';
     96                                echo '<legend>' . __( 'Plugin Review Tools', 'wporg-plugins' ) . '</legend>';
     97                                echo wp_nonce_field( 'block-directory-edit-' . $plugin->ID, 'block-directory-nonce' );
     98                                echo '<input type="hidden" name="plugin-id" value="' . esc_attr( $plugin->ID ) . '" />';
     99                                if ( self::plugin_is_in_block_directory( $checker->slug ) ) {
     100                                        echo '<button type="submit" name="block-directory-edit" value="remove">' . __( 'Remove from Block Directory', 'wporg-plugins' ) . '</button>';
     101                                } else {
     102                                        echo '<button type="submit" name="block-directory-edit" value="add">' . __( 'Add to Block Directory', 'wporg-plugins' ) . '</button>';
     103                                }
     104       
     105                                echo '<ul><li><a href="' . get_edit_post_link( $plugin->ID ) . '">' . __( 'Edit plugin', 'wporg-plugins' ) . '</a></li>';
     106                                echo '<li><a href="' . esc_url( 'https://plugins.trac.wordpress.org/browser/' . $checker->slug . '/trunk' ) .'">' . __( 'Trac browser', 'wporg-plugins' ) . '</a></li></ul>';
     107                                echo '</fieldset>';
     108                                echo '</form>';
     109                        }
     110                }
     111
    65112                $results_by_type = array();
    66113                foreach ( $results as $item ) {
    67114                        $results_by_type[ $item->type ][] = $item;
     
    72119                if ( empty( $results_by_type['error'] ) ) {
    73120                        $output .= '<h3>' . __( 'Success', 'wporg-plugins' ) . '</h3>';
    74121                        $output .= "<div class='notice notice-success notice-alt'>\n";
    75                         $output .= __( 'No problems were found. Your plugin has passed the first step towards being included in the Block Directory.', 'wporg-plugins' );
     122                        if ( $checker->slug && self::plugin_is_in_block_directory( $checker->slug ) ) {
     123                                $output .= __( 'No problems were found. This plugin is already in the Block Directory.', 'wporg-plugins' );
     124                        } else {
     125                                $output .= __( 'No problems were found. Your plugin has passed the first step towards being included in the Block Directory.', 'wporg-plugins' );
     126                        }
    76127                        $output .= "</div>\n";
    77128                } else {
    78129                        $output .= '<h3>' . __( 'Problems were encountered', 'wporg-plugins' ) . '</h3>';