Skip to:
Content

bbPress.org

Changeset 4365


Ignore:
Timestamp:
11/09/2012 09:32:13 AM (12 years ago)
Author:
johnjamesjacoby
Message:

Capabilities:

  • Introduce functionality for bulk-edit of user forums roles.
  • Uses new 'keep_gate' capability to prevent granting keymaster role from a non-keymaster.
  • For WordPress 3.5 and higher.
  • Props jmdodd.
  • See #1939.
  • Fixes #2016.
Location:
trunk/includes
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/includes/admin/users.php

    r4352 r4365  
    5151
    5252        // WordPress user screen
    53         add_filter( 'manage_users_columns',       array( $this, 'user_role_column' )        );
    54         add_filter( 'manage_users_custom_column', array( $this, 'user_role_row'    ), 10, 3 );
     53        add_action( 'restrict_manage_users',      array( $this, 'user_role_bulk_dropdown' )        );
     54        add_filter( 'manage_users_columns',       array( $this, 'user_role_column'        )        );
     55        add_filter( 'manage_users_custom_column', array( $this, 'user_role_row'           ), 10, 3 );
     56
     57        // Process bulk role change
     58        add_action( 'load-users.php',             array( $this, 'user_role_bulk_change'   )        );
    5559    }
    5660
     
    6771        // Bail if current user cannot edit users
    6872        if ( ! current_user_can( 'edit_user', $profileuser->ID ) )
    69             return; ?>
     73            return;
     74
     75        // Get the roles
     76        $dynamic_roles = bbp_get_dynamic_roles();
     77
     78        // Only keymasters can set other keymasters
     79        if ( ! current_user_can( 'keep_gate' ) )
     80            unset( $dynamic_roles[ bbp_get_keymaster_role() ] ); ?>
    7081
    7182        <h3><?php _e( 'Forums', 'bbpress' ); ?></h3>
     
    91102                            <?php endif; ?>
    92103
    93                             <?php foreach ( bbp_get_dynamic_roles() as $role => $details ) : ?>
     104                            <?php foreach ( $dynamic_roles as $role => $details ) : ?>
    94105
    95106                                <option <?php selected( $user_role, $role ); ?> value="<?php echo esc_attr( $role ); ?>"><?php echo translate_user_role( $details['name'] ); ?></option>
     
    108119
    109120    /**
     121     * Add bulk forums role dropdown to the WordPress users table
     122     *
     123     * @since bbPress (r4360)
     124     */
     125    public static function user_role_bulk_dropdown() {
     126
     127        // Bail if current user cannot promote users
     128        if ( !current_user_can( 'promote_users' ) )
     129            return;
     130
     131        // Get the roles
     132        $dynamic_roles = bbp_get_dynamic_roles();
     133
     134        // Only keymasters can set other keymasters
     135        if ( ! current_user_can( 'keep_gate' ) )
     136            unset( $dynamic_roles[ bbp_get_keymaster_role() ] ); ?>
     137
     138        <label class="screen-reader-text" for="bbp-new-role"><?php _e( 'Change forum role to&hellip;', 'bbpress' ) ?></label>
     139        <select name="bbp-new-role" id="bbp-new-role" style="display:inline-block; float:none;">
     140            <option value=''><?php _e( 'Change forum role to&hellip;', 'bbpress' ) ?></option>
     141            <?php foreach ( $dynamic_roles as $role => $details ) : ?>
     142
     143                <option value="<?php echo esc_attr( $role ); ?>"><?php echo translate_user_role( $details['name'] ); ?></option>
     144
     145            <?php endforeach; ?>
     146        </select>
     147
     148        <?php submit_button( __( 'Change', 'bbpress' ), 'secondary', 'bbp-change-role', false );
     149    }
     150
     151    /**
     152     * Process bulk dropdown form submission from the WordPress Users
     153     * Table
     154     *
     155     * @uses current_user_can() to check for 'promote users' capability
     156     * @uses bbp_get_dynamic_roles() to get forum roles
     157     * @uses bbp_get_user_role() to get a user's current forums role
     158     * @uses bbp_set_user_role() to set the user's new forums role
     159     * @return bool Always false
     160     */
     161    public function user_role_bulk_change() {
     162
     163        // Bail if current user cannot promote users
     164        if ( !current_user_can( 'promote_users' ) )
     165            return;
     166
     167        // Bail if no users specified
     168        if ( empty( $_REQUEST['users'] ) )
     169            return;
     170
     171        // Bail if this isn't a bbPress action
     172        if ( empty( $_REQUEST['bbp-new-role'] ) || empty( $_REQUEST['bbp-change-role'] ) )
     173            return;
     174
     175        // Check that the new role exists
     176        $dynamic_roles = bbp_get_dynamic_roles();
     177        if ( empty( $dynamic_roles[ $_REQUEST['bbp-new-role'] ] ) )
     178            return;
     179
     180        // Get the current user ID
     181        $current_user_id = (int) bbp_get_current_user_id();
     182
     183        // Run through user ids
     184        foreach ( (array) $_REQUEST['users'] as $user_id ) {
     185            $user_id = (int) $user_id;
     186
     187            // Don't let a user change their own role
     188            if ( $user_id == $current_user_id )
     189                continue;
     190
     191            // Set up user and role data
     192            $user_role = bbp_get_user_role( $user_id );         
     193            $new_role  = sanitize_text_field( $_REQUEST['bbp-new-role'] );
     194
     195            // Only keymasters can set other keymasters
     196            if ( in_array( bbp_get_keymaster_role(), array( $user_role, $new_role ) ) && ! current_user_can( 'keep_gate' ) )
     197                continue;
     198
     199            // Set the new forums role
     200            if ( $new_role != $user_role ) {
     201                bbp_set_user_role( $user_id, $new_role );
     202            }
     203        }
     204    }
     205
     206    /**
    110207     * Add Forum Role column to the WordPress Users table, and change the
    111208     * core role title to "Site Role"
     
    118215    public static function user_role_column( $columns = array() ) {
    119216        $columns['role']          = __( 'Site Role',  'bbpress' );
    120         $columns['bbp_user_role'] = __( 'Forum Role', 'bbpress' );
     217        $columns['bbp_user_role'] = __( 'Forum Role', 'bbpress' );
    121218
    122219        return $columns;
  • trunk/includes/users/template-tags.php

    r4363 r4365  
    10041004        return;
    10051005
    1006     $user_role = bbp_get_user_role( bbp_get_displayed_user_id() ); ?>
     1006    // Get the user's role
     1007    $user_role     = bbp_get_user_role( bbp_get_displayed_user_id() );
     1008
     1009    // Get the roles
     1010    $dynamic_roles = bbp_get_dynamic_roles();
     1011
     1012    // Only keymasters can set other keymasters
     1013    if ( ! current_user_can( 'keep_gate' ) )
     1014        unset( $dynamic_roles[ bbp_get_keymaster_role() ] ); ?>
    10071015
    10081016    <select name="bbp-forums-role" id="bbp-forums-role">
    10091017        <option value=""><?php _e( '&mdash; No role for this forum &mdash;', 'bbpress' ); ?></option>
    10101018
    1011         <?php foreach ( bbp_get_dynamic_roles() as $role => $details ) : ?>
     1019        <?php foreach ( $dynamic_roles as $role => $details ) : ?>
    10121020
    10131021            <option <?php selected( $user_role, $role ); ?> value="<?php echo esc_attr( $role ); ?>"><?php echo translate_user_role( $details['name'] ); ?></option>
Note: See TracChangeset for help on using the changeset viewer.