[ Index ]

PHP Cross Reference of BBPress

title

Body

[close]

/src/includes/admin/ -> users.php (source)

   1  <?php
   2  
   3  /**
   4   * bbPress Users Admin Class
   5   *
   6   * @package bbPress
   7   * @subpackage Administration
   8   */
   9  
  10  // Exit if accessed directly
  11  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 2.0.0 bbPress (r2464)
  20   */
  21  class BBP_Users_Admin {
  22  
  23      /**
  24       * The bbPress users admin loader
  25       *
  26       * @since 2.0.0 bbPress (r2515)
  27       */
  28  	public function __construct() {
  29          $this->setup_actions();
  30      }
  31  
  32      /**
  33       * Setup the admin hooks, actions and filters
  34       *
  35       * @since 2.0.0 bbPress (r2646)
  36       *
  37       * @access private
  38       */
  39  	function setup_actions() {
  40  
  41          // Bail if in network admin
  42          if ( is_network_admin() ) {
  43              return;
  44          }
  45  
  46          // User profile edit/display actions
  47          add_action( 'edit_user_profile', array( $this, 'secondary_role_display' ) );
  48  
  49          // WordPress user screen
  50          // Remove the bottom list table "change forum role" dropdown from WordPress < 4.6.
  51          // See https://bbpress.trac.wordpress.org/ticket/2906.
  52          if ( bbp_get_major_wp_version() < 4.6 ) {
  53              add_action( 'restrict_manage_users',  array( __CLASS__, 'user_role_bulk_dropdown' )    );
  54          } else {
  55              add_action( 'restrict_manage_users',  array( $this, 'user_role_bulk_dropdown' ), 10, 1 );
  56          }
  57          add_filter( 'manage_users_columns',       array( $this, 'user_role_column'        ), 10, 1 );
  58          add_filter( 'manage_users_custom_column', array( $this, 'user_role_row'           ), 10, 3 );
  59  
  60          // Only list bbPress roles under Forum Role, remove from WordPress' > 4.4 Site Role list.
  61          if ( bbp_get_major_wp_version() >= 4.4 ) {
  62              add_filter( 'get_role_list',          array( $this, 'user_role_list_filter'   ), 10, 2 );
  63          }
  64  
  65          // User List Table
  66          add_action( 'load-users.php',   array( $this, 'user_role_bulk_change' ), 10, 1 );
  67          add_action( 'user_row_actions', array( $this, 'user_row_actions'      ), 10, 2 );
  68      }
  69  
  70      /**
  71       * Default interface for setting a forum role
  72       *
  73       * @since 2.2.0 bbPress (r4285)
  74       *
  75       * @param WP_User $profileuser User data
  76       * @return bool Always false
  77       */
  78  	public static function secondary_role_display( $profileuser ) {
  79  
  80          // Bail if current user cannot edit users
  81          if ( ! current_user_can( 'edit_user', $profileuser->ID ) ) {
  82              return;
  83          }
  84  
  85          // Get the roles
  86          $dynamic_roles = bbp_get_dynamic_roles();
  87  
  88          // Only keymasters can set other keymasters
  89          if ( ! bbp_is_user_keymaster() ) {
  90              unset( $dynamic_roles[ bbp_get_keymaster_role() ] );
  91          } ?>
  92  
  93          <h2><?php esc_html_e( 'Forums', 'bbpress' ); ?></h2>
  94  
  95          <table class="form-table">
  96              <tbody>
  97                  <tr>
  98                      <th><label for="bbp-forums-role"><?php esc_html_e( 'Forum Role', 'bbpress' ); ?></label></th>
  99                      <td>
 100  
 101                          <?php $user_role = bbp_get_user_role( $profileuser->ID ); ?>
 102  
 103                          <select name="bbp-forums-role" id="bbp-forums-role">
 104  
 105                              <?php if ( ! empty( $user_role ) ) : ?>
 106  
 107                                  <option value=""><?php esc_html_e( '&mdash; No role for these forums &mdash;', 'bbpress' ); ?></option>
 108  
 109                              <?php else : ?>
 110  
 111                                  <option value="" selected="selected"><?php esc_html_e( '&mdash; No role for these forums &mdash;', 'bbpress' ); ?></option>
 112  
 113                              <?php endif; ?>
 114  
 115                              <?php foreach ( $dynamic_roles as $role => $details ) : ?>
 116  
 117                                  <option <?php selected( $user_role, $role ); ?> value="<?php echo esc_attr( $role ); ?>"><?php echo bbp_translate_user_role( $details['name'] ); ?></option>
 118  
 119                              <?php endforeach; ?>
 120  
 121                          </select>
 122                      </td>
 123                  </tr>
 124  
 125              </tbody>
 126          </table>
 127  
 128          <?php
 129      }
 130  
 131      /**
 132       * Add bulk forums role dropdown to the WordPress users table
 133       *
 134       * @since 2.2.0 bbPress (r4360)
 135       * @since 2.6.0 bbPress (r6055) Introduced the `$which` parameter.
 136       *
 137       * @param string $which The location of the extra table nav markup: 'top' or 'bottom'.
 138       */
 139  	public static function user_role_bulk_dropdown( $which ) {
 140  
 141          // Remove the bottom list table "change forum role" dropdown from WordPress < 4.6.
 142          // See https://bbpress.trac.wordpress.org/ticket/2906.
 143          if ( bbp_get_major_wp_version() < 4.6 ) {
 144              remove_action( 'restrict_manage_users', array( __CLASS__, 'user_role_bulk_dropdown' ) );
 145          }
 146  
 147          // Bail if current user cannot promote users
 148          if ( ! current_user_can( 'promote_users' ) ) {
 149              return;
 150          }
 151  
 152          // Get the roles
 153          $dynamic_roles = bbp_get_dynamic_roles();
 154  
 155          // Only keymasters can set other keymasters
 156          if ( ! bbp_is_user_keymaster() ) {
 157              unset( $dynamic_roles[ bbp_get_keymaster_role() ] );
 158          }
 159  
 160          $select_id = 'bottom' === $which ? 'bbp-new-role2' : 'bbp-new-role';
 161          $button_id = 'bottom' === $which ? 'bbp-change-role2' : 'bbp-change-role';
 162          ?>
 163  
 164          <label class="screen-reader-text" for="<?php echo $select_id; ?>"><?php esc_html_e( 'Change forum role to&hellip;', 'bbpress' ); ?></label>
 165          <select name="<?php echo $select_id; ?>" id="<?php echo $select_id; ?>" style="display:inline-block; float:none;">
 166              <option value=''><?php esc_html_e( 'Change forum role to&hellip;', 'bbpress' ); ?></option>
 167              <?php foreach ( $dynamic_roles as $role => $details ) : ?>
 168                  <option value="<?php echo esc_attr( $role ); ?>"><?php echo bbp_translate_user_role( $details['name'] ); ?></option>
 169              <?php endforeach; ?>
 170          </select><?php submit_button( esc_html__( 'Change', 'bbpress' ), 'secondary', $button_id, false );
 171  
 172          wp_nonce_field( 'bbp-bulk-users', 'bbp-bulk-users-nonce' );
 173      }
 174  
 175      /**
 176       * Process bulk dropdown form submission from the WordPress Users
 177       * Table
 178       *
 179       * @since 2.2.0 bbPress (r4365)
 180       *
 181       * @return bool Always false
 182       */
 183  	public function user_role_bulk_change() {
 184  
 185          // Bail if no users specified
 186          if ( empty( $_REQUEST['users'] ) ) {
 187              return;
 188          }
 189  
 190          // Bail if this isn't a bbPress action
 191          if ( ( empty( $_REQUEST['bbp-new-role'] ) && empty( $_REQUEST['bbp-new-role2'] ) ) || ( empty( $_REQUEST['bbp-change-role'] ) && empty( $_REQUEST['bbp-change-role2'] ) ) ) {
 192              return;
 193          }
 194  
 195          $new_role = false;
 196          if ( ! empty( $_REQUEST['bbp-change-role2'] ) && ! empty( $_REQUEST['bbp-new-role2'] ) ) {
 197              $new_role = $_REQUEST['bbp-new-role2'];
 198          } elseif ( ! empty( $_REQUEST['bbp-change-role'] ) && ! empty( $_REQUEST['bbp-new-role'] ) ) {
 199              $new_role = $_REQUEST['bbp-new-role'];
 200          }
 201  
 202          // Check that the new role exists
 203          $dynamic_roles = bbp_get_dynamic_roles();
 204          if ( ! $new_role || empty( $dynamic_roles[ $new_role ] ) ) {
 205              return;
 206          }
 207  
 208          // Bail if nonce check fails
 209          check_admin_referer( 'bbp-bulk-users', 'bbp-bulk-users-nonce' );
 210  
 211          // Bail if current user cannot promote users
 212          if ( ! current_user_can( 'promote_users' ) ) {
 213              return;
 214          }
 215  
 216          // Get the current user ID
 217          $current_user_id = (int) bbp_get_current_user_id();
 218  
 219          // Run through user ids
 220          foreach ( (array) $_REQUEST['users'] as $user_id ) {
 221              $user_id = (int) $user_id;
 222  
 223              // Don't let a user change their own role
 224              if ( $user_id === $current_user_id ) {
 225                  continue;
 226              }
 227  
 228              // Set up user and role data
 229              $user_role = bbp_get_user_role( $user_id );
 230              $new_role  = sanitize_text_field( $new_role );
 231  
 232              // Only keymasters can set other keymasters
 233              if ( in_array( bbp_get_keymaster_role(), array( $user_role, $new_role ), true ) && ! bbp_is_user_keymaster() ) {
 234                  continue;
 235              }
 236  
 237              // Set the new forums role
 238              if ( $new_role !== $user_role ) {
 239                  bbp_set_user_role( $user_id, $new_role );
 240              }
 241          }
 242      }
 243  
 244      /**
 245       * Add a "View" link for each user
 246       *
 247       * @since 2.6.0 bbPress (r6502)
 248       *
 249       * @param array   $actions
 250       * @param WP_User $user
 251       *
 252       * @return array Actions with 'view' link added to them
 253       */
 254  	public function user_row_actions( $actions = array(), $user = false ) {
 255  
 256          // Reverse
 257          $actions = array_reverse( $actions );
 258  
 259          // Add the view action link
 260          $actions['view'] = '<a href="' . esc_url( bbp_get_user_profile_url( $user->ID ) ) . '" class="bbp-user-profile-link">' . esc_html__( 'View', 'bbpress' ) . '</a>';
 261  
 262          // Re-reverse
 263          return array_reverse( $actions );
 264      }
 265  
 266      /**
 267       * Add Forum Role column to the WordPress Users table, and change the
 268       * core role title to "Site Role"
 269       *
 270       * @since 2.2.0 bbPress (r4337)
 271       *
 272       * @param array $columns Users table columns
 273       * @return array $columns
 274       */
 275  	public static function user_role_column( $columns = array() ) {
 276  
 277          // New title for old Role column
 278          $columns['role'] = esc_html__( 'Site Role',  'bbpress' );
 279  
 280          // New column
 281          $bbp_user_role = array(
 282              'bbp_user_role' => esc_html__( 'Forum Role', 'bbpress' )
 283          );
 284  
 285          // Make sure role columns are next to each other
 286          $role_pos = array_search( 'role', array_keys( $columns ), true );
 287          $result   = array_slice( $columns, 0, $role_pos + 1 );
 288          $result   = array_merge( $result, $bbp_user_role );
 289  
 290          // Merge and return
 291          return array_merge( $result, array_slice( $columns, $role_pos ) );
 292      }
 293  
 294      /**
 295       * Return user's forums role for display in the WordPress Users list table
 296       *
 297       * @since 2.2.0 bbPress (r4337)
 298       *
 299       * @param string $retval
 300       * @param string $column_name
 301       * @param int $user_id
 302       *
 303       * @return string Displayable bbPress user role
 304       */
 305  	public static function user_role_row( $retval = '', $column_name = '', $user_id = 0 ) {
 306  
 307          // User role column
 308          if ( 'bbp_user_role' === $column_name ) {
 309  
 310              // Get the users role
 311              $user_role = bbp_get_user_role( $user_id );
 312              $retval    = false;
 313  
 314              // Translate user role for display
 315              if ( ! empty( $user_role ) ) {
 316                  $roles  = bbp_get_dynamic_roles();
 317                  $retval = bbp_translate_user_role( $roles[ $user_role ]['name'] );
 318              }
 319          }
 320  
 321          // Pass retval through
 322          return $retval;
 323      }
 324  
 325      /**
 326       * Filter the list of roles included in the WordPress site role list
 327       *
 328       * Ensures forum roles are only displayed under the Forum Role list in the
 329       * WordPress Users list table
 330       *
 331       * @since 2.6.0 bbPress (r6051)
 332       *
 333       * @return array $roles
 334       */
 335  	public static function user_role_list_filter( $roles, $user ) {
 336  
 337          // Get the users role
 338          $user_role = bbp_get_user_role( $user->ID );
 339  
 340          if ( ! empty( $user_role ) ) {
 341              unset( $roles[ $user_role ] );
 342          }
 343  
 344          return $roles;
 345      }
 346  }
 347  new BBP_Users_Admin();
 348  endif; // class exists


Generated: Thu Mar 28 01:00:51 2024 Cross-referenced by PHPXref 0.7.1