web/wp-content/plugins/bbpress/includes/admin/users.php
changeset 196 5e8dcbe22c24
child 204 09a1c134465b
equal deleted inserted replaced
195:c7c0fbc09788 196:5e8dcbe22c24
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * bbPress Users Admin Class
       
     5  *
       
     6  * @package bbPress
       
     7  * @subpackage Administration
       
     8  */
       
     9 
       
    10 // Exit if accessed directly
       
    11 if ( !defined( 'ABSPATH' ) ) exit;
       
    12 
       
    13 if ( !class_exists( 'BBP_Users_Admin' ) ) :
       
    14 /**
       
    15  * Loads bbPress users admin area
       
    16  *
       
    17  * @package bbPress
       
    18  * @subpackage Administration
       
    19  * @since bbPress (r2464)
       
    20  */
       
    21 class BBP_Users_Admin {
       
    22 
       
    23 	/**
       
    24 	 * The bbPress users admin loader
       
    25 	 *
       
    26 	 * @since bbPress (r2515)
       
    27 	 *
       
    28 	 * @uses BBP_Users_Admin::setup_globals() Setup the globals needed
       
    29 	 * @uses BBP_Users_Admin::setup_actions() Setup the hooks and actions
       
    30 	 */
       
    31 	public function __construct() {
       
    32 		$this->setup_actions();
       
    33 	}
       
    34 
       
    35 	/**
       
    36 	 * Setup the admin hooks, actions and filters
       
    37 	 *
       
    38 	 * @since bbPress (r2646)
       
    39 	 * @access private
       
    40 	 *
       
    41 	 * @uses add_action() To add various actions
       
    42 	 */
       
    43 	function setup_actions() {
       
    44 
       
    45 		// Bail if in network admin
       
    46 		if ( is_network_admin() )
       
    47 			return;
       
    48 
       
    49 		// User profile edit/display actions
       
    50 		add_action( 'edit_user_profile', array( $this, 'secondary_role_display' ) );
       
    51 
       
    52 		// WordPress user screen
       
    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'   )        );
       
    59 	}
       
    60 
       
    61 	/**
       
    62 	 * Default interface for setting a forum role
       
    63 	 *
       
    64 	 * @since bbPress (r4285)
       
    65 	 *
       
    66 	 * @param WP_User $profileuser User data
       
    67 	 * @return bool Always false
       
    68 	 */
       
    69 	public static function secondary_role_display( $profileuser ) {
       
    70 
       
    71 		// Bail if current user cannot edit users
       
    72 		if ( ! current_user_can( 'edit_user', $profileuser->ID ) )
       
    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() ] ); ?>
       
    81 
       
    82 		<h3><?php _e( 'Forums', 'bbpress' ); ?></h3>
       
    83 
       
    84 		<table class="form-table">
       
    85 			<tbody>
       
    86 				<tr>
       
    87 					<th><label for="bbp-forums-role"><?php _e( 'Forum Role', 'bbpress' ); ?></label></th>
       
    88 					<td>
       
    89 
       
    90 						<?php $user_role = bbp_get_user_role( $profileuser->ID ); ?>
       
    91 
       
    92 						<select name="bbp-forums-role" id="bbp-forums-role">
       
    93 
       
    94 							<?php if ( ! empty( $user_role ) ) : ?>
       
    95 
       
    96 								<option value=""><?php _e( '&mdash; No role for this forum &mdash;', 'bbpress' ); ?></option>
       
    97 
       
    98 							<?php else : ?>
       
    99 
       
   100 								<option value="" selected="selected"><?php _e( '&mdash; No role for this forum &mdash;', 'bbpress' ); ?></option>
       
   101 
       
   102 							<?php endif; ?>
       
   103 
       
   104 							<?php foreach ( $dynamic_roles as $role => $details ) : ?>
       
   105 
       
   106 								<option <?php selected( $user_role, $role ); ?> value="<?php echo esc_attr( $role ); ?>"><?php echo translate_user_role( $details['name'] ); ?></option>
       
   107 
       
   108 							<?php endforeach; ?>
       
   109 
       
   110 						</select>
       
   111 					</td>
       
   112 				</tr>
       
   113 
       
   114 			</tbody>
       
   115 		</table>
       
   116 
       
   117 		<?php
       
   118 	}
       
   119 
       
   120 	/**
       
   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 	/**
       
   207 	 * Add Forum Role column to the WordPress Users table, and change the
       
   208 	 * core role title to "Site Role"
       
   209 	 *
       
   210 	 * @since bbPress (r4337)
       
   211 	 *
       
   212 	 * @param array $columns Users table columns
       
   213 	 * @return array $columns
       
   214 	 */
       
   215 	public static function user_role_column( $columns = array() ) {
       
   216 		$columns['role']          = __( 'Site Role',  'bbpress' );
       
   217 		$columns['bbp_user_role'] = __( 'Forum Role', 'bbpress' );
       
   218 
       
   219 		return $columns;
       
   220 	}
       
   221 
       
   222 	/**
       
   223 	 * Return user's forums role for display in the WordPress Users list table
       
   224 	 *
       
   225 	 * @since bbPress (r4337)
       
   226 	 *
       
   227 	 * @param string $retval
       
   228 	 * @param string $column_name
       
   229 	 * @param int $user_id
       
   230 	 *
       
   231 	 * @return string Displayable bbPress user role
       
   232 	 */
       
   233 	public static function user_role_row( $retval = '', $column_name = '', $user_id = 0 ) {
       
   234 
       
   235 		// Only looking for bbPress's user role column
       
   236 		if ( 'bbp_user_role' == $column_name ) {
       
   237 
       
   238 			// Get the users role
       
   239 			$user_role = bbp_get_user_role( $user_id );
       
   240 			$retval    = false;
       
   241 
       
   242 			// Translate user role for display
       
   243 			if ( ! empty( $user_role ) ) {
       
   244 				$roles  = bbp_get_dynamic_roles();
       
   245 				$retval = translate_user_role( $roles[$user_role]['name'] );
       
   246 			}
       
   247 		}
       
   248 
       
   249 		// Pass retval through
       
   250 		return $retval;
       
   251 	}
       
   252 }
       
   253 new BBP_Users_Admin();
       
   254 endif; // class exists