/wp-content/plugins/bbpress/includes/admin/users.php

https://bitbucket.org/adatux_/uakami · PHP · 254 lines · 103 code · 54 blank · 97 comment · 23 complexity · d43bacfe5fa7dc37f7f0040e74c0644d MD5 · raw file

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