Making WordPress.org

Ticket #5306: 5306-review-tools.diff

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

Add a Review Tools block to the validator page, with an Add/Remove button

  • shortcodes/class-block-validator.php

     
    2727                        </form>
    2828
    2929                        <?php
    30                         if ( $_POST && wp_verify_nonce( $_POST['block-nonce'], 'validate-block-plugin' ) ) {
     30                        if ( $_POST && !empty( $_POST['plugin_url'] ) && wp_verify_nonce( $_POST['block-nonce'], 'validate-block-plugin' ) ) {
    3131                                self::validate_block( $_POST['plugin_url'] );
     32                        } elseif ( $_POST && !empty( $_POST['block-directory-edit'] ) ) {
     33                                $post = get_post( intval( $_POST['plugin-id'] ) );
     34                                if ( $post && wp_verify_nonce( $_POST['block-directory-nonce'], 'block-directory-edit-' . $post->ID ) ) {
     35                                        if ( current_user_can( 'edit_post', $post->ID ) ) {
     36                                                $terms = wp_list_pluck( get_the_terms( $post->ID, 'plugin_section' ), 'slug' );
     37                                                if ( 'add' === $_POST['block-directory-edit'] ) {
     38                                                        $terms[] = 'block';
     39                                                } elseif ( 'remove' === $_POST['block-directory-edit'] ) {
     40                                                        $terms = array_diff( $terms, array( 'block' ) );
     41                                                }
     42                                                wp_set_object_terms( $post->ID, $terms, 'plugin_section' );
     43                                        }
     44
     45                                        self::validate_block( $post->post_name );
     46                                }
    3247                        }
    3348                        ?>
    3449                </div>
     
    4055                return ob_get_clean();
    4156        }
    4257
     58        protected static function get_plugin_by_slug( $slug ) {
     59                if ( !trim( $slug ) )
     60                        return false;
     61
     62                $args = array(
     63                        'post_type' => 'plugin',
     64                        'post_status' => 'publish',
     65                        'posts_per_page' => 1,
     66                        'name' => $slug,
     67                );
     68                $query = new \WP_Query( $args );
     69
     70                if ( 1 === count( $query->posts ) )
     71                        return $query->posts[0];
     72
     73                return false;
     74        }
     75
     76        protected static function plugin_is_in_block_directory( $slug ) {
     77                $plugin = self::get_plugin_by_slug( $slug );
     78
     79                return (
     80                        $plugin &&
     81                        $plugin->post_name === $slug &&
     82                        has_term( 'block', 'plugin_section', $plugin )
     83                );
     84        }
     85
    4386        /**
    4487         * Validates readme.txt contents and adds feedback.
    4588         *
     
    62105                        echo '</p>';
    63106                }
    64107
     108                if ( $checker->slug ) {
     109                        $plugin = self::get_plugin_by_slug( $checker->slug );
     110                        if ( current_user_can( 'edit_post', $plugin->ID ) ) {
     111                                echo '<form method="post" action="">';
     112                                echo '<fieldset>';
     113                                echo '<legend>' . __( 'Plugin Review Tools', 'wporg-plugins' ) . '</legend>';
     114                                echo wp_nonce_field( 'block-directory-edit-' . $plugin->ID, 'block-directory-nonce' );
     115                                echo '<input type="hidden" name="plugin-id" value="' . esc_attr( $plugin->ID ) . '" />';
     116                                if ( self::plugin_is_in_block_directory( $checker->slug ) ) {
     117                                        echo '<button type="submit" name="block-directory-edit" value="remove">' . __( 'Remove from Block Directory', 'wporg-plugins' ) . '</button>';
     118                                } else {
     119                                        echo '<button type="submit" name="block-directory-edit" value="add">' . __( 'Add to Block Directory', 'wporg-plugins' ) . '</button>';
     120                                }
     121       
     122                                echo '<ul><li><a href="' . get_edit_post_link( $plugin->ID ) . '">' . __( 'Edit plugin', 'wporg-plugins' ) . '</a></li>';
     123                                echo '<li><a href="' . esc_url( 'https://plugins.trac.wordpress.org/browser/' . $checker->slug . '/trunk' ) .'">' . __( 'Trac browser', 'wporg-plugins' ) . '</a></li></ul>';
     124                                echo '</fieldset>';
     125                        }
     126                }
     127
    65128                $results_by_type = array();
    66129                foreach ( $results as $item ) {
    67130                        $results_by_type[ $item->type ][] = $item;
     
    72135                if ( empty( $results_by_type['error'] ) ) {
    73136                        $output .= '<h3>' . __( 'Success', 'wporg-plugins' ) . '</h3>';
    74137                        $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' );
     138                        if ( $checker->slug && self::plugin_is_in_block_directory( $checker->slug ) ) {
     139                                $output .= __( 'No problems were found. This plugin is already in the Block Directory.', 'wporg-plugins' );
     140                        } else {
     141                                $output .= __( 'No problems were found. Your plugin has passed the first step towards being included in the Block Directory.', 'wporg-plugins' );
     142                        }
    76143                        $output .= "</div>\n";
    77144                } else {
    78145                        $output .= '<h3>' . __( 'Problems were encountered', 'wporg-plugins' ) . '</h3>';