[ Index ]

PHP Cross Reference of WordPress

title

Body

[close]

/wp-admin/includes/ -> class-wp-users-list-table.php (source)

   1  <?php
   2  /**
   3   * List Table API: WP_Users_List_Table class
   4   *
   5   * @package WordPress
   6   * @subpackage Administration
   7   * @since 3.1.0
   8   */
   9  
  10  /**
  11   * Core class used to implement displaying users in a list table.
  12   *
  13   * @since 3.1.0
  14   * @access private
  15   *
  16   * @see WP_List_Table
  17   */
  18  class WP_Users_List_Table extends WP_List_Table {
  19  
  20      /**
  21       * Site ID to generate the Users list table for.
  22       *
  23       * @since 3.1.0
  24       * @var int
  25       */
  26      public $site_id;
  27  
  28      /**
  29       * Whether or not the current Users list table is for Multisite.
  30       *
  31       * @since 3.1.0
  32       * @var bool
  33       */
  34      public $is_site_users;
  35  
  36      /**
  37       * Constructor.
  38       *
  39       * @since 3.1.0
  40       *
  41       * @see WP_List_Table::__construct() for more information on default arguments.
  42       *
  43       * @param array $args An associative array of arguments.
  44       */
  45  	public function __construct( $args = array() ) {
  46          parent::__construct(
  47              array(
  48                  'singular' => 'user',
  49                  'plural'   => 'users',
  50                  'screen'   => isset( $args['screen'] ) ? $args['screen'] : null,
  51              )
  52          );
  53  
  54          $this->is_site_users = 'site-users-network' === $this->screen->id;
  55  
  56          if ( $this->is_site_users ) {
  57              $this->site_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
  58          }
  59      }
  60  
  61      /**
  62       * Check the current user's permissions.
  63       *
  64       * @since 3.1.0
  65       *
  66       * @return bool
  67       */
  68  	public function ajax_user_can() {
  69          if ( $this->is_site_users ) {
  70              return current_user_can( 'manage_sites' );
  71          } else {
  72              return current_user_can( 'list_users' );
  73          }
  74      }
  75  
  76      /**
  77       * Prepare the users list for display.
  78       *
  79       * @since 3.1.0
  80       *
  81       * @global string $role
  82       * @global string $usersearch
  83       */
  84  	public function prepare_items() {
  85          global $role, $usersearch;
  86  
  87          $usersearch = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST['s'] ) ) : '';
  88  
  89          $role = isset( $_REQUEST['role'] ) ? $_REQUEST['role'] : '';
  90  
  91          $per_page       = ( $this->is_site_users ) ? 'site_users_network_per_page' : 'users_per_page';
  92          $users_per_page = $this->get_items_per_page( $per_page );
  93  
  94          $paged = $this->get_pagenum();
  95  
  96          if ( 'none' === $role ) {
  97              $args = array(
  98                  'number'  => $users_per_page,
  99                  'offset'  => ( $paged - 1 ) * $users_per_page,
 100                  'include' => wp_get_users_with_no_role( $this->site_id ),
 101                  'search'  => $usersearch,
 102                  'fields'  => 'all_with_meta',
 103              );
 104          } else {
 105              $args = array(
 106                  'number' => $users_per_page,
 107                  'offset' => ( $paged - 1 ) * $users_per_page,
 108                  'role'   => $role,
 109                  'search' => $usersearch,
 110                  'fields' => 'all_with_meta',
 111              );
 112          }
 113  
 114          if ( '' !== $args['search'] ) {
 115              $args['search'] = '*' . $args['search'] . '*';
 116          }
 117  
 118          if ( $this->is_site_users ) {
 119              $args['blog_id'] = $this->site_id;
 120          }
 121  
 122          if ( isset( $_REQUEST['orderby'] ) ) {
 123              $args['orderby'] = $_REQUEST['orderby'];
 124          }
 125  
 126          if ( isset( $_REQUEST['order'] ) ) {
 127              $args['order'] = $_REQUEST['order'];
 128          }
 129  
 130          /**
 131           * Filters the query arguments used to retrieve users for the current users list table.
 132           *
 133           * @since 4.4.0
 134           *
 135           * @param array $args Arguments passed to WP_User_Query to retrieve items for the current
 136           *                    users list table.
 137           */
 138          $args = apply_filters( 'users_list_table_query_args', $args );
 139  
 140          // Query the user IDs for this page
 141          $wp_user_search = new WP_User_Query( $args );
 142  
 143          $this->items = $wp_user_search->get_results();
 144  
 145          $this->set_pagination_args(
 146              array(
 147                  'total_items' => $wp_user_search->get_total(),
 148                  'per_page'    => $users_per_page,
 149              )
 150          );
 151      }
 152  
 153      /**
 154       * Output 'no users' message.
 155       *
 156       * @since 3.1.0
 157       */
 158  	public function no_items() {
 159          _e( 'No users found.' );
 160      }
 161  
 162      /**
 163       * Return an associative array listing all the views that can be used
 164       * with this table.
 165       *
 166       * Provides a list of roles and user count for that role for easy
 167       * Filtersing of the user table.
 168       *
 169       * @since  3.1.0
 170       *
 171       * @global string $role
 172       *
 173       * @return array An array of HTML links, one for each view.
 174       */
 175  	protected function get_views() {
 176          global $role;
 177  
 178          $wp_roles = wp_roles();
 179  
 180          if ( $this->is_site_users ) {
 181              $url = 'site-users.php?id=' . $this->site_id;
 182              switch_to_blog( $this->site_id );
 183              $users_of_blog = count_users( 'time', $this->site_id );
 184              restore_current_blog();
 185          } else {
 186              $url           = 'users.php';
 187              $users_of_blog = count_users();
 188          }
 189  
 190          $total_users = $users_of_blog['total_users'];
 191          $avail_roles =& $users_of_blog['avail_roles'];
 192          unset( $users_of_blog );
 193  
 194          $current_link_attributes = empty( $role ) ? ' class="current" aria-current="page"' : '';
 195  
 196          $role_links        = array();
 197          $role_links['all'] = sprintf(
 198              '<a href="%s"%s>%s</a>',
 199              $url,
 200              $current_link_attributes,
 201              sprintf(
 202                  /* translators: %s: Number of users. */
 203                  _nx(
 204                      'All <span class="count">(%s)</span>',
 205                      'All <span class="count">(%s)</span>',
 206                      $total_users,
 207                      'users'
 208                  ),
 209                  number_format_i18n( $total_users )
 210              )
 211          );
 212  
 213          foreach ( $wp_roles->get_names() as $this_role => $name ) {
 214              if ( ! isset( $avail_roles[ $this_role ] ) ) {
 215                  continue;
 216              }
 217  
 218              $current_link_attributes = '';
 219  
 220              if ( $this_role === $role ) {
 221                  $current_link_attributes = ' class="current" aria-current="page"';
 222              }
 223  
 224              $name = translate_user_role( $name );
 225              $name = sprintf(
 226                  /* translators: User role name with count. */
 227                  __( '%1$s <span class="count">(%2$s)</span>' ),
 228                  $name,
 229                  number_format_i18n( $avail_roles[ $this_role ] )
 230              );
 231  
 232              $role_links[ $this_role ] = "<a href='" . esc_url( add_query_arg( 'role', $this_role, $url ) ) . "'$current_link_attributes>$name</a>";
 233          }
 234  
 235          if ( ! empty( $avail_roles['none'] ) ) {
 236  
 237              $current_link_attributes = '';
 238  
 239              if ( 'none' === $role ) {
 240                  $current_link_attributes = ' class="current" aria-current="page"';
 241              }
 242  
 243              $name = __( 'No role' );
 244              $name = sprintf(
 245                  /* translators: User role name with count. */
 246                  __( '%1$s <span class="count">(%2$s)</span>' ),
 247                  $name,
 248                  number_format_i18n( $avail_roles['none'] )
 249              );
 250  
 251              $role_links['none'] = "<a href='" . esc_url( add_query_arg( 'role', 'none', $url ) ) . "'$current_link_attributes>$name</a>";
 252          }
 253  
 254          return $role_links;
 255      }
 256  
 257      /**
 258       * Retrieve an associative array of bulk actions available on this table.
 259       *
 260       * @since  3.1.0
 261       *
 262       * @return array Array of bulk actions.
 263       */
 264  	protected function get_bulk_actions() {
 265          $actions = array();
 266  
 267          if ( is_multisite() ) {
 268              if ( current_user_can( 'remove_users' ) ) {
 269                  $actions['remove'] = __( 'Remove' );
 270              }
 271          } else {
 272              if ( current_user_can( 'delete_users' ) ) {
 273                  $actions['delete'] = __( 'Delete' );
 274              }
 275          }
 276  
 277          return $actions;
 278      }
 279  
 280      /**
 281       * Output the controls to allow user roles to be changed in bulk.
 282       *
 283       * @since 3.1.0
 284       *
 285       * @param string $which Whether this is being invoked above ("top")
 286       *                      or below the table ("bottom").
 287       */
 288  	protected function extra_tablenav( $which ) {
 289          $id        = 'bottom' === $which ? 'new_role2' : 'new_role';
 290          $button_id = 'bottom' === $which ? 'changeit2' : 'changeit';
 291          ?>
 292      <div class="alignleft actions">
 293          <?php if ( current_user_can( 'promote_users' ) && $this->has_items() ) : ?>
 294          <label class="screen-reader-text" for="<?php echo $id; ?>"><?php _e( 'Change role to&hellip;' ); ?></label>
 295          <select name="<?php echo $id; ?>" id="<?php echo $id; ?>">
 296              <option value=""><?php _e( 'Change role to&hellip;' ); ?></option>
 297              <?php wp_dropdown_roles(); ?>
 298          </select>
 299              <?php
 300              submit_button( __( 'Change' ), '', $button_id, false );
 301          endif;
 302  
 303          /**
 304           * Fires just before the closing div containing the bulk role-change controls
 305           * in the Users list table.
 306           *
 307           * @since 3.5.0
 308           * @since 4.6.0 The `$which` parameter was added.
 309           *
 310           * @param string $which The location of the extra table nav markup: 'top' or 'bottom'.
 311           */
 312          do_action( 'restrict_manage_users', $which );
 313          ?>
 314          </div>
 315          <?php
 316          /**
 317           * Fires immediately following the closing "actions" div in the tablenav for the users
 318           * list table.
 319           *
 320           * @since 4.9.0
 321           *
 322           * @param string $which The location of the extra table nav markup: 'top' or 'bottom'.
 323           */
 324          do_action( 'manage_users_extra_tablenav', $which );
 325      }
 326  
 327      /**
 328       * Capture the bulk action required, and return it.
 329       *
 330       * Overridden from the base class implementation to capture
 331       * the role change drop-down.
 332       *
 333       * @since  3.1.0
 334       *
 335       * @return string The bulk action required.
 336       */
 337  	public function current_action() {
 338          if ( ( isset( $_REQUEST['changeit'] ) || isset( $_REQUEST['changeit2'] ) ) &&
 339              ( ! empty( $_REQUEST['new_role'] ) || ! empty( $_REQUEST['new_role2'] ) ) ) {
 340              return 'promote';
 341          }
 342  
 343          return parent::current_action();
 344      }
 345  
 346      /**
 347       * Get a list of columns for the list table.
 348       *
 349       * @since  3.1.0
 350       *
 351       * @return array Array in which the key is the ID of the column,
 352       *               and the value is the description.
 353       */
 354  	public function get_columns() {
 355          $c = array(
 356              'cb'       => '<input type="checkbox" />',
 357              'username' => __( 'Username' ),
 358              'name'     => __( 'Name' ),
 359              'email'    => __( 'Email' ),
 360              'role'     => __( 'Role' ),
 361              'posts'    => __( 'Posts' ),
 362          );
 363  
 364          if ( $this->is_site_users ) {
 365              unset( $c['posts'] );
 366          }
 367  
 368          return $c;
 369      }
 370  
 371      /**
 372       * Get a list of sortable columns for the list table.
 373       *
 374       * @since 3.1.0
 375       *
 376       * @return array Array of sortable columns.
 377       */
 378  	protected function get_sortable_columns() {
 379          $c = array(
 380              'username' => 'login',
 381              'email'    => 'email',
 382          );
 383  
 384          return $c;
 385      }
 386  
 387      /**
 388       * Generate the list table rows.
 389       *
 390       * @since 3.1.0
 391       */
 392  	public function display_rows() {
 393          // Query the post counts for this page
 394          if ( ! $this->is_site_users ) {
 395              $post_counts = count_many_users_posts( array_keys( $this->items ) );
 396          }
 397  
 398          foreach ( $this->items as $userid => $user_object ) {
 399              echo "\n\t" . $this->single_row( $user_object, '', '', isset( $post_counts ) ? $post_counts[ $userid ] : 0 );
 400          }
 401      }
 402  
 403      /**
 404       * Generate HTML for a single row on the users.php admin panel.
 405       *
 406       * @since 3.1.0
 407       * @since 4.2.0 The `$style` parameter was deprecated.
 408       * @since 4.4.0 The `$role` parameter was deprecated.
 409       *
 410       * @param WP_User $user_object The current user object.
 411       * @param string  $style       Deprecated. Not used.
 412       * @param string  $role        Deprecated. Not used.
 413       * @param int     $numposts    Optional. Post count to display for this user. Defaults
 414       *                             to zero, as in, a new user has made zero posts.
 415       * @return string Output for a single row.
 416       */
 417  	public function single_row( $user_object, $style = '', $role = '', $numposts = 0 ) {
 418          if ( ! ( $user_object instanceof WP_User ) ) {
 419              $user_object = get_userdata( (int) $user_object );
 420          }
 421          $user_object->filter = 'display';
 422          $email               = $user_object->user_email;
 423  
 424          if ( $this->is_site_users ) {
 425              $url = "site-users.php?id={$this->site_id}&amp;";
 426          } else {
 427              $url = 'users.php?';
 428          }
 429  
 430          $user_roles = $this->get_role_list( $user_object );
 431  
 432          // Set up the hover actions for this user
 433          $actions     = array();
 434          $checkbox    = '';
 435          $super_admin = '';
 436  
 437          if ( is_multisite() && current_user_can( 'manage_network_users' ) ) {
 438              if ( in_array( $user_object->user_login, get_super_admins(), true ) ) {
 439                  $super_admin = ' &mdash; ' . __( 'Super Admin' );
 440              }
 441          }
 442  
 443          // Check if the user for this row is editable
 444          if ( current_user_can( 'list_users' ) ) {
 445              // Set up the user editing link
 446              $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user_object->ID ) ) );
 447  
 448              if ( current_user_can( 'edit_user', $user_object->ID ) ) {
 449                  $edit            = "<strong><a href=\"{$edit_link}\">{$user_object->user_login}</a>{$super_admin}</strong><br />";
 450                  $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
 451              } else {
 452                  $edit = "<strong>{$user_object->user_login}{$super_admin}</strong><br />";
 453              }
 454  
 455              if ( ! is_multisite() && get_current_user_id() != $user_object->ID && current_user_can( 'delete_user', $user_object->ID ) ) {
 456                  $actions['delete'] = "<a class='submitdelete' href='" . wp_nonce_url( "users.php?action=delete&amp;user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Delete' ) . '</a>';
 457              }
 458              if ( is_multisite() && current_user_can( 'remove_user', $user_object->ID ) ) {
 459                  $actions['remove'] = "<a class='submitdelete' href='" . wp_nonce_url( $url . "action=remove&amp;user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Remove' ) . '</a>';
 460              }
 461  
 462              // Add a link to the user's author archive, if not empty.
 463              $author_posts_url = get_author_posts_url( $user_object->ID );
 464              if ( $author_posts_url ) {
 465                  $actions['view'] = sprintf(
 466                      '<a href="%s" aria-label="%s">%s</a>',
 467                      esc_url( $author_posts_url ),
 468                      /* translators: %s: Author's display name. */
 469                      esc_attr( sprintf( __( 'View posts by %s' ), $user_object->display_name ) ),
 470                      __( 'View' )
 471                  );
 472              }
 473  
 474              /**
 475               * Filters the action links displayed under each user in the Users list table.
 476               *
 477               * @since 2.8.0
 478               *
 479               * @param string[] $actions     An array of action links to be displayed.
 480               *                              Default 'Edit', 'Delete' for single site, and
 481               *                              'Edit', 'Remove' for Multisite.
 482               * @param WP_User  $user_object WP_User object for the currently listed user.
 483               */
 484              $actions = apply_filters( 'user_row_actions', $actions, $user_object );
 485  
 486              // Role classes.
 487              $role_classes = esc_attr( implode( ' ', array_keys( $user_roles ) ) );
 488  
 489              // Set up the checkbox ( because the user is editable, otherwise it's empty )
 490              $checkbox = sprintf(
 491                  '<label class="screen-reader-text" for="user_%1$s">%2$s</label>' .
 492                  '<input type="checkbox" name="users[]" id="user_%1$s" class="%3$s" value="%1$s" />',
 493                  $user_object->ID,
 494                  /* translators: %s: User login. */
 495                  sprintf( __( 'Select %s' ), $user_object->user_login ),
 496                  $role_classes
 497              );
 498  
 499          } else {
 500              $edit = "<strong>{$user_object->user_login}{$super_admin}</strong>";
 501          }
 502  
 503          $avatar = get_avatar( $user_object->ID, 32 );
 504  
 505          // Comma-separated list of user roles.
 506          $roles_list = implode( ', ', $user_roles );
 507  
 508          $r = "<tr id='user-$user_object->ID'>";
 509  
 510          list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
 511  
 512          foreach ( $columns as $column_name => $column_display_name ) {
 513              $classes = "$column_name column-$column_name";
 514              if ( $primary === $column_name ) {
 515                  $classes .= ' has-row-actions column-primary';
 516              }
 517              if ( 'posts' === $column_name ) {
 518                  $classes .= ' num'; // Special case for that column
 519              }
 520  
 521              if ( in_array( $column_name, $hidden ) ) {
 522                  $classes .= ' hidden';
 523              }
 524  
 525              $data = 'data-colname="' . wp_strip_all_tags( $column_display_name ) . '"';
 526  
 527              $attributes = "class='$classes' $data";
 528  
 529              if ( 'cb' === $column_name ) {
 530                  $r .= "<th scope='row' class='check-column'>$checkbox</th>";
 531              } else {
 532                  $r .= "<td $attributes>";
 533                  switch ( $column_name ) {
 534                      case 'username':
 535                          $r .= "$avatar $edit";
 536                          break;
 537                      case 'name':
 538                          if ( $user_object->first_name && $user_object->last_name ) {
 539                              $r .= "$user_object->first_name $user_object->last_name";
 540                          } elseif ( $user_object->first_name ) {
 541                              $r .= $user_object->first_name;
 542                          } elseif ( $user_object->last_name ) {
 543                              $r .= $user_object->last_name;
 544                          } else {
 545                              $r .= sprintf(
 546                                  '<span aria-hidden="true">&#8212;</span><span class="screen-reader-text">%s</span>',
 547                                  _x( 'Unknown', 'name' )
 548                              );
 549                          }
 550                          break;
 551                      case 'email':
 552                          $r .= "<a href='" . esc_url( "mailto:$email" ) . "'>$email</a>";
 553                          break;
 554                      case 'role':
 555                          $r .= esc_html( $roles_list );
 556                          break;
 557                      case 'posts':
 558                          if ( $numposts > 0 ) {
 559                              $r .= sprintf(
 560                                  '<a href="%s" class="edit"><span aria-hidden="true">%s</span><span class="screen-reader-text">%s</span></a>',
 561                                  "edit.php?author={$user_object->ID}",
 562                                  $numposts,
 563                                  sprintf(
 564                                      /* translators: %s: Number of posts. */
 565                                      _n( '%s post by this author', '%s posts by this author', $numposts ),
 566                                      number_format_i18n( $numposts )
 567                                  )
 568                              );
 569                          } else {
 570                              $r .= 0;
 571                          }
 572                          break;
 573                      default:
 574                          /**
 575                           * Filters the display output of custom columns in the Users list table.
 576                           *
 577                           * @since 2.8.0
 578                           *
 579                           * @param string $output      Custom column output. Default empty.
 580                           * @param string $column_name Column name.
 581                           * @param int    $user_id     ID of the currently-listed user.
 582                           */
 583                          $r .= apply_filters( 'manage_users_custom_column', '', $column_name, $user_object->ID );
 584                  }
 585  
 586                  if ( $primary === $column_name ) {
 587                      $r .= $this->row_actions( $actions );
 588                  }
 589                  $r .= '</td>';
 590              }
 591          }
 592          $r .= '</tr>';
 593  
 594          return $r;
 595      }
 596  
 597      /**
 598       * Gets the name of the default primary column.
 599       *
 600       * @since 4.3.0
 601       *
 602       * @return string Name of the default primary column, in this case, 'username'.
 603       */
 604  	protected function get_default_primary_column_name() {
 605          return 'username';
 606      }
 607  
 608      /**
 609       * Returns an array of user roles for a given user object.
 610       *
 611       * @since 4.4.0
 612       *
 613       * @param WP_User $user_object The WP_User object.
 614       * @return string[] An array of user roles.
 615       */
 616  	protected function get_role_list( $user_object ) {
 617          $wp_roles = wp_roles();
 618  
 619          $role_list = array();
 620  
 621          foreach ( $user_object->roles as $role ) {
 622              if ( isset( $wp_roles->role_names[ $role ] ) ) {
 623                  $role_list[ $role ] = translate_user_role( $wp_roles->role_names[ $role ] );
 624              }
 625          }
 626  
 627          if ( empty( $role_list ) ) {
 628              $role_list['none'] = _x( 'None', 'no user roles' );
 629          }
 630  
 631          /**
 632           * Filters the returned array of roles for a user.
 633           *
 634           * @since 4.4.0
 635           *
 636           * @param string[] $role_list   An array of user roles.
 637           * @param WP_User  $user_object A WP_User object.
 638           */
 639          return apply_filters( 'get_role_list', $role_list, $user_object );
 640      }
 641  
 642  }


Generated: Thu Sep 19 01:00:03 2019 Cross-referenced by PHPXref 0.7.1