BBP_Users_Admin

Loads bbPress users admin area.

Defined (1)

The class is defined in the following location(s).

/includes/admin/users.php  
  1. class BBP_Users_Admin { 
  2.  
  3. /** 
  4. * The bbPress users admin loader 
  5. * @since bbPress (r2515) 
  6. * @uses BBP_Users_Admin::setup_globals() Setup the globals needed 
  7. * @uses BBP_Users_Admin::setup_actions() Setup the hooks and actions 
  8. */ 
  9. public function __construct() { 
  10. $this->setup_actions(); 
  11.  
  12. /** 
  13. * Setup the admin hooks, actions and filters 
  14. * @since bbPress (r2646) 
  15. * @access private 
  16. * @uses add_action() To add various actions 
  17. */ 
  18. function setup_actions() { 
  19.  
  20. // Bail if in network admin 
  21. if ( is_network_admin() ) 
  22. return; 
  23.  
  24. // User profile edit/display actions 
  25. add_action( 'edit_user_profile', array( $this, 'secondary_role_display' ) ); 
  26.  
  27. // WordPress user screen 
  28. add_action( 'restrict_manage_users', array( $this, 'user_role_bulk_dropdown' ) ); 
  29. add_filter( 'manage_users_columns', array( $this, 'user_role_column' ) ); 
  30. add_filter( 'manage_users_custom_column', array( $this, 'user_role_row' ), 10, 3 ); 
  31.  
  32. // Process bulk role change 
  33. add_action( 'load-users.php', array( $this, 'user_role_bulk_change' ) ); 
  34.  
  35. /** 
  36. * Default interface for setting a forum role 
  37. * @since bbPress (r4285) 
  38. * @param WP_User $profileuser User data 
  39. * @return bool Always false 
  40. */ 
  41. public static function secondary_role_display( $profileuser ) { 
  42.  
  43. // Bail if current user cannot edit users 
  44. if ( ! current_user_can( 'edit_user', $profileuser->ID ) ) 
  45. return; 
  46.  
  47. // Get the roles 
  48. $dynamic_roles = bbp_get_dynamic_roles(); 
  49.  
  50. // Only keymasters can set other keymasters 
  51. if ( ! bbp_is_user_keymaster() ) 
  52. unset( $dynamic_roles[ bbp_get_keymaster_role() ] ); ?> 
  53.  
  54. <h3><?php esc_html_e( 'Forums', 'bbpress' ); ?></h3> 
  55.  
  56. <table class="form-table"> 
  57. <tbody> 
  58. <tr> 
  59. <th><label for="bbp-forums-role"><?php esc_html_e( 'Forum Role', 'bbpress' ); ?></label></th> 
  60. <td> 
  61.  
  62. <?php $user_role = bbp_get_user_role( $profileuser->ID ); ?> 
  63.  
  64. <select name="bbp-forums-role" id="bbp-forums-role"> 
  65.  
  66. <?php if ( ! empty( $user_role ) ) : ?> 
  67.  
  68. <option value=""><?php esc_html_e( '— No role for these forums —', 'bbpress' ); ?></option> 
  69.  
  70. <?php else : ?> 
  71.  
  72. <option value="" selected="selected"><?php esc_html_e( '— No role for these forums —', 'bbpress' ); ?></option> 
  73.  
  74. <?php endif; ?> 
  75.  
  76. <?php foreach ( $dynamic_roles as $role => $details ) : ?> 
  77.  
  78. <option <?php selected( $user_role, $role ); ?> value="<?php echo esc_attr( $role ); ?>"><?php echo translate_user_role( $details['name'] ); ?></option> 
  79.  
  80. <?php endforeach; ?> 
  81.  
  82. </select> 
  83. </td> 
  84. </tr> 
  85.  
  86. </tbody> 
  87. </table> 
  88.  
  89. <?php 
  90.  
  91. /** 
  92. * Add bulk forums role dropdown to the WordPress users table 
  93. * @since bbPress (r4360) 
  94. */ 
  95. public static function user_role_bulk_dropdown() { 
  96.  
  97. // Bail if current user cannot promote users  
  98. if ( !current_user_can( 'promote_users' ) ) 
  99. return; 
  100.  
  101. // Get the roles 
  102. $dynamic_roles = bbp_get_dynamic_roles(); 
  103.  
  104. // Only keymasters can set other keymasters 
  105. if ( ! bbp_is_user_keymaster() ) 
  106. unset( $dynamic_roles[ bbp_get_keymaster_role() ] ); ?> 
  107.  
  108. <label class="screen-reader-text" for="bbp-new-role"><?php esc_html_e( 'Change forum role to…', 'bbpress' ) ?></label> 
  109. <select name="bbp-new-role" id="bbp-new-role" style="display:inline-block; float:none;"> 
  110. <option value=''><?php esc_html_e( 'Change forum role to…', 'bbpress' ) ?></option> 
  111. <?php foreach ( $dynamic_roles as $role => $details ) : ?> 
  112. <option value="<?php echo esc_attr( $role ); ?>"><?php echo translate_user_role( $details['name'] ); ?></option> 
  113. <?php endforeach; ?> 
  114. </select><?php submit_button( __( 'Change', 'bbpress' ), 'secondary', 'bbp-change-role', false ); 
  115.  
  116. wp_nonce_field( 'bbp-bulk-users', 'bbp-bulk-users-nonce' ); 
  117.  
  118. /** 
  119. * Process bulk dropdown form submission from the WordPress Users 
  120. * Table 
  121. * @uses current_user_can() to check for 'promote users' capability 
  122. * @uses bbp_get_dynamic_roles() to get forum roles 
  123. * @uses bbp_get_user_role() to get a user's current forums role 
  124. * @uses bbp_set_user_role() to set the user's new forums role 
  125. * @return bool Always false 
  126. */ 
  127. public function user_role_bulk_change() { 
  128.  
  129. // Bail if no users specified 
  130. if ( empty( $_REQUEST['users'] ) ) 
  131. return; 
  132.  
  133. // Bail if this isn't a bbPress action 
  134. if ( empty( $_REQUEST['bbp-new-role'] ) || empty( $_REQUEST['bbp-change-role'] ) ) 
  135. return; 
  136.  
  137. // Check that the new role exists 
  138. $dynamic_roles = bbp_get_dynamic_roles(); 
  139. if ( empty( $dynamic_roles[ $_REQUEST['bbp-new-role'] ] ) ) 
  140. return; 
  141.  
  142. // Bail if nonce check fails 
  143. check_admin_referer( 'bbp-bulk-users', 'bbp-bulk-users-nonce' ); 
  144.  
  145. // Bail if current user cannot promote users  
  146. if ( !current_user_can( 'promote_users' ) ) 
  147. return; 
  148.  
  149. // Get the current user ID 
  150. $current_user_id = (int) bbp_get_current_user_id(); 
  151.  
  152. // Run through user ids 
  153. foreach ( (array) $_REQUEST['users'] as $user_id ) { 
  154. $user_id = (int) $user_id; 
  155.  
  156. // Don't let a user change their own role 
  157. if ( $user_id === $current_user_id ) 
  158. continue; 
  159.  
  160. // Set up user and role data 
  161. $user_role = bbp_get_user_role( $user_id );  
  162. $new_role = sanitize_text_field( $_REQUEST['bbp-new-role'] ); 
  163.  
  164. // Only keymasters can set other keymasters 
  165. if ( in_array( bbp_get_keymaster_role(), array( $user_role, $new_role ) ) && ! bbp_is_user_keymaster() ) 
  166. continue; 
  167.  
  168. // Set the new forums role 
  169. if ( $new_role !== $user_role ) { 
  170. bbp_set_user_role( $user_id, $new_role ); 
  171.  
  172. /** 
  173. * Add Forum Role column to the WordPress Users table, and change the 
  174. * core role title to "Site Role" 
  175. * @since bbPress (r4337) 
  176. * @param array $columns Users table columns 
  177. * @return array $columns 
  178. */ 
  179. public static function user_role_column( $columns = array() ) { 
  180. $columns['role'] = __( 'Site Role', 'bbpress' ); 
  181. $columns['bbp_user_role'] = __( 'Forum Role', 'bbpress' ); 
  182.  
  183. return $columns; 
  184.  
  185. /** 
  186. * Return user's forums role for display in the WordPress Users list table 
  187. * @since bbPress (r4337) 
  188. * @param string $retval 
  189. * @param string $column_name 
  190. * @param int $user_id 
  191. * @return string Displayable bbPress user role 
  192. */ 
  193. public static function user_role_row( $retval = '', $column_name = '', $user_id = 0 ) { 
  194.  
  195. // Only looking for bbPress's user role column 
  196. if ( 'bbp_user_role' === $column_name ) { 
  197.  
  198. // Get the users role 
  199. $user_role = bbp_get_user_role( $user_id ); 
  200. $retval = false; 
  201.  
  202. // Translate user role for display 
  203. if ( ! empty( $user_role ) ) { 
  204. $roles = bbp_get_dynamic_roles(); 
  205. $retval = translate_user_role( $roles[$user_role]['name'] ); 
  206.  
  207. // Pass retval through 
  208. return $retval;