Making WordPress.org

Changeset 12517


Ignore:
Timestamp:
03/30/2023 03:37:47 AM (2 years ago)
Author:
dd32
Message:

Plugin Directory: Administration: Add bulk actions for close/disable/open.

This streamlines administration of cases where a large group of plugins need to be disabled or re-opened.

Fixes #6903.

Location:
sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/admin
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/admin/class-customizations.php

    r12507 r12517  
    3131        add_action( 'pre_get_posts', array( $this, 'pre_get_posts' ) );
    3232
    33         add_action( 'load-edit.php', array( $this, 'bulk_reject_plugins' ) );
     33        add_action( 'load-edit.php', array( $this, 'bulk_action_plugins' ) );
    3434        add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_assets' ) );
    3535        add_filter( 'admin_head-edit.php', array( $this, 'plugin_posts_list_table' ) );
     
    229229
    230230    /**
    231      * Rejects plugins in bulk.
    232      */
    233     public function bulk_reject_plugins() {
    234         if ( empty( $_REQUEST['action'] ) || empty( $_REQUEST['action2'] ) || ! in_array( 'plugin_reject', array( $_REQUEST['action'], $_REQUEST['action2'] ) ) || 'plugin' !== $_REQUEST['post_type'] ) {
     231     * Performs plugin status changes in bulk.
     232     */
     233    public function bulk_action_plugins() {
     234        if (
     235            empty( $_REQUEST['action'] ) ||
     236            empty( $_REQUEST['action2'] ) ||
     237            'plugin' !== $_REQUEST['post_type']
     238        ) {
    235239            return;
    236240        }
     
    238242        check_admin_referer( 'bulk-posts' );
    239243
    240         $rejected = 0;
     244        $action = array_intersect(
     245            [ 'plugin_open', 'plugin_close', 'plugin_disable', 'plugin_reject' ],
     246            [ $_REQUEST['action'], $_REQUEST['action2'] ]
     247        );
     248        $action = array_shift( $action );
     249        if ( ! $action ) {
     250            return;
     251        }
     252
     253        switch( $action ) {
     254            case 'plugin_open':
     255                $capability = 'plugin_approve';
     256                $new_status = 'publish';
     257                $message    = _n_noop( '%s plugin opened.', '%s plugins opened.', 'wporg-plugins' );
     258                $from_state = [ 'closed', 'disabled' ];
     259                break;
     260            case 'plugin_close':
     261                $capability = 'plugin_close';
     262                $new_status = 'closed';
     263                $message    = _n_noop( '%s plugin closed.', '%s plugins closed.', 'wporg-plugins' );
     264                $from_state = [ 'closed', 'disabled', 'publish', 'approved' ];
     265                break;
     266            case 'plugin_disable':
     267                $capability = 'plugin_close';
     268                $new_status = 'disabled';
     269                $message    = _n_noop( '%s plugin disabled.', '%s plugins disabled.', 'wporg-plugins' );
     270                $from_state = [ 'closed', 'disabled', 'publish', 'approved' ];
     271                break;
     272            case 'plugin_reject':
     273                $capability = 'plugin_reject';
     274                $new_status = 'rejected';
     275                $message    = _n_noop( '%s plugin rejected.', '%s plugins rejected.', 'wporg-plugins' );
     276                $from_state = [ 'new', 'pending' ];
     277                break;
     278            default:
     279                return;
     280        }
     281
     282        $closed = 0;
    241283        $plugins  = get_posts( array(
    242284            'post_type'      => 'plugin',
    243285            'post__in'       => array_map( 'absint', $_REQUEST['post'] ),
    244             'post_status'    => array( 'new', 'pending' ),
     286            'post_status'    => $from_state,
    245287            'posts_per_page' => count( $_REQUEST['post'] ),
    246288        ) );
    247289
    248290        foreach ( $plugins as $plugin ) {
    249             if ( ! current_user_can( 'plugin_reject', $plugin ) ) {
    250                 wp_die( __( 'You are not allowed to reject this plugin.', 'wporg-plugins' ), '', array( 'back_link' => true ) );
     291            if ( ! current_user_can( $capability, $plugin ) ) {
     292                continue;
    251293            }
    252294
    253295            $updated = wp_update_post( array(
    254296                'ID'          => $plugin->ID,
    255                 'post_status' => 'rejected',
     297                'post_status' => $new_status,
    256298            ) );
    257299
    258300            if ( $updated && ! is_wp_error( $updated ) ) {
    259                 $rejected++;
    260             }
    261         }
    262 
    263         if ( $rejected ) {
    264             set_transient( 'settings_errors', array(
    265                 array(
    266                     'setting' => 'wporg-plugins',
    267                     'code'    => 'plugins-bulk-rejected',
    268                     'message' => sprintf( _n( '%d plugin rejected.', '%d plugins rejected.', $rejected, 'wporg-plugins' ), $rejected ),
    269                     'type'    => 'updated',
    270                 ),
    271             ) );
    272         }
     301                $closed++;
     302            }
     303
     304        }
     305
     306        set_transient( 'settings_errors', array(
     307            array(
     308                'setting' => 'wporg-plugins',
     309                'code'    => 'plugins-bulk-actioned',
     310                'message' => sprintf( translate_nooped_plural( $message, $closed, 'wporg-plugins' ), number_format_i18n( $closed ) ),
     311                'type'    => 'updated',
     312            ),
     313        ) );
    273314
    274315        $send_back = remove_query_arg( array( 'trashed', 'untrashed', 'deleted', 'locked', 'ids', 'action', 'action2', 'tags_input', 'post_author', 'comment_status', 'ping_status', '_status', 'post', 'bulk_edit', 'post_view' ), wp_get_referer() );
    275         wp_redirect( add_query_arg( array( 'settings-updated' => true ), $send_back ) );
     316        wp_safe_redirect( add_query_arg( array( 'settings-updated' => true ), $send_back ) );
    276317        exit;
    277318    }
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/admin/class-status-transitions.php

    r12175 r12517  
    323323     */
    324324    public function save_close_reason( $post_id ) {
    325         if ( ! isset( $_POST['close_reason'] ) ) {
     325        if ( ! isset( $_REQUEST['close_reason'] ) ) {
    326326            return;
    327327        }
     
    331331        }
    332332
    333         $close_reason = sanitize_key( $_POST['close_reason'] );
     333        $close_reason = sanitize_key( $_REQUEST['close_reason'] );
    334334
    335335        update_post_meta( $post_id, '_close_reason', $close_reason );
  • sites/trunk/wordpress.org/public_html/wp-content/plugins/plugin-directory/admin/list-table/class-plugin-posts.php

    r12325 r12517  
    8181        if ( current_user_can( 'plugin_reject' ) && ( empty( $_REQUEST['post_status'] ) || in_array( $_REQUEST['post_status'], array( 'new', 'pending' ) ) ) ) {
    8282            $actions['plugin_reject'] = __( 'Reject', 'wporg-plugins' );
     83        }
     84
     85        if ( current_user_can( 'plugin_approve' ) && ( empty( $_REQUEST['post_status'] ) || in_array( $_REQUEST['post_status'], array( 'closed', 'disabled' ) ) ) ) {
     86            $actions['plugin_open'] = __( 'Open', 'wporg-plugins' );
     87        }
     88
     89        if ( current_user_can( 'plugin_close' ) && ( empty( $_REQUEST['post_status'] ) || in_array( $_REQUEST['post_status'], array( 'publish', 'approved', 'disabled' ) ) ) ) {
     90            $actions['plugin_close'] = __( 'Close', 'wporg-plugins' );
     91        }
     92
     93        if ( current_user_can( 'plugin_close' ) && ( empty( $_REQUEST['post_status'] ) || in_array( $_REQUEST['post_status'], array( 'publish', 'approved', 'closed' ) ) ) ) {
     94            $actions['plugin_disable'] = __( 'Disable', 'wporg-plugins' );
    8395        }
    8496
     
    500512        return $status_links;
    501513    }
     514
     515    /**
     516     * Display the "actions" fields below the filters.
     517     */
     518    public function extra_tablenav( $which ) {
     519        parent::extra_tablenav( $which );
     520
     521        // TODO: This shouldn't have inline CSS/JS.
     522
     523        if ( 'top' === $which ) {
     524            ?>
     525            <fieldset class="alignleft actions hide-if-js bulk-plugin_close bulk-plugin_disable" disabled="disabled">
     526                <select name="close_reason" id="close_reason">
     527                    <option disabled="disabled" value='' selected="selected"><?php esc_html_e( 'Close/Disable Reason:', 'wporg-plugins' ); ?></option>
     528                    <?php foreach ( Template::get_close_reasons() as $key => $label ) : ?>
     529                        <option value="<?php echo esc_attr( $key ); ?>"><?php echo esc_html( $label ); ?></option>
     530                    <?php endforeach; ?>
     531                </select>
     532            </fieldset>
     533            <style>
     534                #posts-filter fieldset.bulk-plugin_close {
     535                    margin: 0;
     536                }
     537            </style>
     538            <script>
     539            jQuery( function( $ ) {
     540                $( '.bulkactions' ).on( 'change', function() {
     541                    var $this = $( this ),
     542                        val = $this.find(':selected').val(),
     543                        $tablenav = $this.parents('form').find( '.tablenav.top' );
     544
     545                    $tablenav.find( '.actions.bulk-plugin_close, .actions.bulk-plugin_disable' ).prop( 'disabled', true ).hide();
     546                    $tablenav.find( '.actions.bulk-' + val ).prop( 'disabled', false ).show();
     547                } );
     548            } );
     549            </script>
     550            <?php
     551        }
     552    }
    502553}
Note: See TracChangeset for help on using the changeset viewer.