[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

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

   1  <?php
   2  /**
   3   * List Table API: WP_MS_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 for the network admin.
  12   *
  13   * @since 3.1.0
  14   *
  15   * @see WP_List_Table
  16   */
  17  class WP_MS_Users_List_Table extends WP_List_Table {
  18      /**
  19       * Checks if the current user has permissions to perform an Ajax action.
  20       *
  21       * @since 3.1.0
  22       *
  23       * @return bool Whether the current user can perform an Ajax action.
  24       */
  25  	public function ajax_user_can() {
  26          return current_user_can( 'manage_network_users' );
  27      }
  28  
  29      /**
  30       * Prepares the users list for display.
  31       *
  32       * @since 3.1.0
  33       *
  34       * @global string $mode       List table view mode.
  35       * @global string $usersearch User search query.
  36       * @global string $role       The user role to filter by. Only 'super' (super admins) is supported.
  37       */
  38  	public function prepare_items() {
  39          global $mode, $usersearch, $role;
  40  
  41          if ( ! empty( $_REQUEST['mode'] ) ) {
  42              $mode = 'excerpt' === $_REQUEST['mode'] ? 'excerpt' : 'list';
  43              set_user_setting( 'network_users_list_mode', $mode );
  44          } else {
  45              $mode = get_user_setting( 'network_users_list_mode', 'list' );
  46          }
  47  
  48          $usersearch = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST['s'] ) ) : '';
  49  
  50          $users_per_page = $this->get_items_per_page( 'users_network_per_page' );
  51  
  52          $role = $_REQUEST['role'] ?? '';
  53  
  54          $paged = $this->get_pagenum();
  55  
  56          $args = array(
  57              'number'  => $users_per_page,
  58              'offset'  => ( $paged - 1 ) * $users_per_page,
  59              'search'  => $usersearch,
  60              'blog_id' => 0,
  61              'fields'  => 'all_with_meta',
  62          );
  63  
  64          if ( wp_is_large_network( 'users' ) ) {
  65              $args['search'] = ltrim( $args['search'], '*' );
  66          } elseif ( '' !== $args['search'] ) {
  67              $args['search'] = trim( $args['search'], '*' );
  68              $args['search'] = '*' . $args['search'] . '*';
  69          }
  70  
  71          if ( 'super' === $role ) {
  72              $args['login__in'] = get_super_admins();
  73          }
  74  
  75          /*
  76           * If the network is large and a search is not being performed,
  77           * show only the latest users with no paging in order to avoid
  78           * expensive count queries.
  79           */
  80          if ( ! $usersearch && wp_is_large_network( 'users' ) ) {
  81              if ( ! isset( $_REQUEST['orderby'] ) ) {
  82                  $_GET['orderby']     = 'id';
  83                  $_REQUEST['orderby'] = 'id';
  84              }
  85              if ( ! isset( $_REQUEST['order'] ) ) {
  86                  $_GET['order']     = 'DESC';
  87                  $_REQUEST['order'] = 'DESC';
  88              }
  89              $args['count_total'] = false;
  90          }
  91  
  92          if ( isset( $_REQUEST['orderby'] ) ) {
  93              $args['orderby'] = $_REQUEST['orderby'];
  94          }
  95  
  96          if ( isset( $_REQUEST['order'] ) ) {
  97              $args['order'] = $_REQUEST['order'];
  98          }
  99  
 100          /** This filter is documented in wp-admin/includes/class-wp-users-list-table.php */
 101          $args = apply_filters( 'users_list_table_query_args', $args );
 102  
 103          // Query the user IDs for this page.
 104          $wp_user_search = new WP_User_Query( $args );
 105  
 106          $this->items = $wp_user_search->get_results();
 107  
 108          $this->set_pagination_args(
 109              array(
 110                  'total_items' => $wp_user_search->get_total(),
 111                  'per_page'    => $users_per_page,
 112              )
 113          );
 114      }
 115  
 116      /**
 117       * Gets the available bulk actions for the users list table.
 118       *
 119       * @since 3.1.0
 120       *
 121       * @return array<string, string> Bulk action labels keyed by action name.
 122       */
 123  	protected function get_bulk_actions() {
 124          $actions = array();
 125          if ( current_user_can( 'delete_users' ) ) {
 126              $actions['delete'] = __( 'Delete' );
 127          }
 128          $actions['spam']    = _x( 'Mark as spam', 'user' );
 129          $actions['notspam'] = _x( 'Not spam', 'user' );
 130  
 131          return $actions;
 132      }
 133  
 134      /**
 135       * Displays a message when there are no items.
 136       *
 137       * @since 3.1.0
 138       */
 139  	public function no_items() {
 140          _e( 'No users found.' );
 141      }
 142  
 143      /**
 144       * Gets the list of views (all, super admin) available for the users list table.
 145       *
 146       * @since 3.1.0
 147       *
 148       * @global string $role The user role to filter by. Only 'super' (super admins) is supported.
 149       *
 150       * @return array<string, string> View link markup keyed by view name ('all' or 'super').
 151       */
 152  	protected function get_views() {
 153          global $role;
 154  
 155          $total_users  = get_user_count();
 156          $super_admins = get_super_admins();
 157          $total_admins = count( $super_admins );
 158  
 159          $role_links        = array();
 160          $role_links['all'] = array(
 161              'url'     => network_admin_url( 'users.php' ),
 162              'label'   => sprintf(
 163                  /* translators: Number of users. */
 164                  _nx(
 165                      'All <span class="count">(%s)</span>',
 166                      'All <span class="count">(%s)</span>',
 167                      $total_users,
 168                      'users'
 169                  ),
 170                  number_format_i18n( $total_users )
 171              ),
 172              'current' => 'super' !== $role,
 173          );
 174  
 175          $role_links['super'] = array(
 176              'url'     => network_admin_url( 'users.php?role=super' ),
 177              'label'   => sprintf(
 178                  /* translators: Number of users. */
 179                  _n(
 180                      'Super Admin <span class="count">(%s)</span>',
 181                      'Super Admins <span class="count">(%s)</span>',
 182                      $total_admins
 183                  ),
 184                  number_format_i18n( $total_admins )
 185              ),
 186              'current' => 'super' === $role,
 187          );
 188  
 189          return $this->get_views_links( $role_links );
 190      }
 191  
 192      /**
 193       * Generates the list table pagination.
 194       *
 195       * @since 3.1.0
 196       *
 197       * @global string $mode List table view mode.
 198       *
 199       * @param string $which The location of the pagination: Either 'top' or 'bottom'.
 200       *
 201       * @phpstan-param 'top'|'bottom' $which
 202       */
 203  	protected function pagination( $which ) {
 204          global $mode;
 205  
 206          parent::pagination( $which );
 207  
 208          if ( 'top' === $which ) {
 209              $this->view_switcher( $mode );
 210          }
 211      }
 212  
 213      /**
 214       * Gets the list of columns for the users list table.
 215       *
 216       * @since 3.1.0
 217       *
 218       * @return array<string, string> Array of column titles keyed by their column name.
 219       */
 220  	public function get_columns() {
 221          $users_columns = array(
 222              'cb'         => '<input type="checkbox" />',
 223              'username'   => __( 'Username' ),
 224              'name'       => __( 'Name' ),
 225              'email'      => __( 'Email' ),
 226              'registered' => _x( 'Registered', 'user' ),
 227              'blogs'      => __( 'Sites' ),
 228          );
 229          /**
 230           * Filters the columns displayed in the Network Admin Users list table.
 231           *
 232           * @since MU (3.0.0)
 233           *
 234           * @param array<string, string> $users_columns Column titles keyed by column name. Default keys are 'cb',
 235           *                                             'username', 'name', 'email', 'registered', and 'blogs'.
 236           */
 237          return apply_filters( 'wpmu_users_columns', $users_columns );
 238      }
 239  
 240      /**
 241       * Gets the list of sortable columns for the users list table.
 242       *
 243       * @since 3.1.0
 244       *
 245       * @return array<string, array<int, string|bool>> Sortable columns.
 246       *
 247       * @phpstan-return array<string, array{0: string, 1: bool, 2: string, 3: string, 4?: 'asc'|'desc'}>
 248       */
 249  	protected function get_sortable_columns() {
 250          return array(
 251              'username'   => array( 'login', false, __( 'Username' ), __( 'Table ordered by Username.' ), 'asc' ),
 252              'name'       => array( 'name', false, __( 'Name' ), __( 'Table ordered by Name.' ) ),
 253              'email'      => array( 'email', false, __( 'E-mail' ), __( 'Table ordered by E-mail.' ) ),
 254              'registered' => array( 'id', false, _x( 'Registered', 'user' ), __( 'Table ordered by User Registered Date.' ) ),
 255          );
 256      }
 257  
 258      /**
 259       * Handles the checkbox column output.
 260       *
 261       * @since 4.3.0
 262       * @since 5.9.0 Renamed `$user` to `$item` to match parent class for PHP 8 named parameter support.
 263       *
 264       * @param WP_User $item The current WP_User object.
 265       */
 266  	public function column_cb( $item ) {
 267          // Restores the more descriptive, specific name for use within this method.
 268          $user = $item;
 269  
 270          if ( is_super_admin( $user->ID ) ) {
 271              return;
 272          }
 273          ?>
 274          <input type="checkbox" id="blog_<?php echo $user->ID; ?>" name="allusers[]" value="<?php echo esc_attr( $user->ID ); ?>" />
 275          <label for="blog_<?php echo $user->ID; ?>">
 276              <span class="screen-reader-text">
 277              <?php
 278              /* translators: Hidden accessibility text. %s: User login. */
 279              printf( __( 'Select %s' ), $user->user_login );
 280              ?>
 281              </span>
 282          </label>
 283          <?php
 284      }
 285  
 286      /**
 287       * Handles the ID column output.
 288       *
 289       * @since 4.4.0
 290       *
 291       * @param WP_User $user The current WP_User object.
 292       */
 293  	public function column_id( $user ) {
 294          echo $user->ID;
 295      }
 296  
 297      /**
 298       * Handles the username column output.
 299       *
 300       * @since 4.3.0
 301       *
 302       * @param WP_User $user The current WP_User object.
 303       */
 304  	public function column_username( $user ) {
 305          $super_admins = get_super_admins();
 306          $avatar       = get_avatar( $user->user_email, 32 );
 307  
 308          echo $avatar;
 309  
 310          if ( current_user_can( 'edit_user', $user->ID ) ) {
 311              $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user->ID ) ) );
 312              $edit      = "<a href=\"{$edit_link}\">{$user->user_login}</a>";
 313          } else {
 314              $edit = $user->user_login;
 315          }
 316  
 317          ?>
 318          <strong>
 319              <?php
 320              echo $edit;
 321  
 322              if ( in_array( $user->user_login, $super_admins, true ) ) {
 323                  echo ' &mdash; ' . __( 'Super Admin' );
 324              }
 325              ?>
 326          </strong>
 327          <?php
 328      }
 329  
 330      /**
 331       * Handles the name column output.
 332       *
 333       * @since 4.3.0
 334       *
 335       * @param WP_User $user The current WP_User object.
 336       */
 337  	public function column_name( $user ) {
 338          if ( $user->first_name && $user->last_name ) {
 339              printf(
 340                  /* translators: 1: User's first name, 2: Last name. */
 341                  _x( '%1$s %2$s', 'Display name based on first name and last name' ),
 342                  $user->first_name,
 343                  $user->last_name
 344              );
 345          } elseif ( $user->first_name ) {
 346              echo $user->first_name;
 347          } elseif ( $user->last_name ) {
 348              echo $user->last_name;
 349          } else {
 350              echo '<span aria-hidden="true">&#8212;</span><span class="screen-reader-text">' .
 351                  /* translators: Hidden accessibility text. */
 352                  _x( 'Unknown', 'name' ) .
 353              '</span>';
 354          }
 355      }
 356  
 357      /**
 358       * Handles the email column output.
 359       *
 360       * @since 4.3.0
 361       *
 362       * @param WP_User $user The current WP_User object.
 363       */
 364  	public function column_email( $user ) {
 365          echo "<a href='" . esc_url( "mailto:$user->user_email" ) . "'>$user->user_email</a>";
 366      }
 367  
 368      /**
 369       * Handles the registered date column output.
 370       *
 371       * @since 4.3.0
 372       *
 373       * @global string $mode List table view mode.
 374       *
 375       * @param WP_User $user The current WP_User object.
 376       */
 377  	public function column_registered( $user ) {
 378          global $mode;
 379          if ( 'list' === $mode ) {
 380              $date = __( 'Y/m/d' );
 381          } else {
 382              $date = __( 'Y/m/d g:i:s a' );
 383          }
 384          echo mysql2date( $date, $user->user_registered );
 385      }
 386  
 387      /**
 388       * Outputs the sites column content.
 389       *
 390       * @since 4.3.0
 391       *
 392       * @param WP_User $user    The current WP_User object.
 393       * @param string  $classes CSS classes for the cell.
 394       * @param string  $data    Custom data attributes for the cell.
 395       * @param string  $primary The primary column name.
 396       */
 397  	protected function _column_blogs( $user, $classes, $data, $primary ) {
 398          echo '<td class="', $classes, ' has-row-actions" ', $data, '>';
 399          echo $this->column_blogs( $user );
 400          echo $this->handle_row_actions( $user, 'blogs', $primary );
 401          echo '</td>';
 402      }
 403  
 404      /**
 405       * Handles the sites column output.
 406       *
 407       * @since 4.3.0
 408       *
 409       * @param WP_User $user The current WP_User object.
 410       */
 411  	public function column_blogs( $user ) {
 412          $blogs = get_blogs_of_user( $user->ID, true );
 413          if ( ! is_array( $blogs ) ) {
 414              return;
 415          }
 416  
 417          foreach ( $blogs as $site ) {
 418              if ( ! can_edit_network( $site->site_id ) ) {
 419                  continue;
 420              }
 421  
 422              $path         = ( '/' === $site->path ) ? '' : $site->path;
 423              $site_classes = array( 'site-' . $site->site_id );
 424  
 425              /**
 426               * Filters the span class for a site listing on the multisite user list table.
 427               *
 428               * @since 5.2.0
 429               *
 430               * @param string[] $site_classes Array of class names used within the span tag.
 431               *                               Default "site-#" with the site's network ID.
 432               * @param int      $site_id      Site ID.
 433               * @param int      $network_id   Network ID.
 434               * @param WP_User  $user         WP_User object.
 435               */
 436              $site_classes = apply_filters( 'ms_user_list_site_class', $site_classes, $site->userblog_id, $site->site_id, $user );
 437  
 438              if ( is_array( $site_classes ) && ! empty( $site_classes ) ) {
 439                  $site_classes = array_map( 'sanitize_html_class', array_unique( $site_classes ) );
 440                  echo '<span class="' . esc_attr( implode( ' ', $site_classes ) ) . '">';
 441              } else {
 442                  echo '<span>';
 443              }
 444  
 445              echo '<a href="' . esc_url( network_admin_url( 'site-info.php?id=' . $site->userblog_id ) ) . '">' . str_replace( '.' . get_network()->domain, '', $site->domain . $path ) . '</a>';
 446              echo ' <small class="row-actions">';
 447  
 448              $actions         = array();
 449              $actions['edit'] = '<a href="' . esc_url( network_admin_url( 'site-info.php?id=' . $site->userblog_id ) ) . '">' . __( 'Edit' ) . '</a>';
 450  
 451              $class = '';
 452              if ( 1 === (int) $site->spam ) {
 453                  $class .= 'site-spammed ';
 454              }
 455              if ( 1 === (int) $site->mature ) {
 456                  $class .= 'site-mature ';
 457              }
 458              if ( 1 === (int) $site->deleted ) {
 459                  $class .= 'site-deleted ';
 460              }
 461              if ( 1 === (int) $site->archived ) {
 462                  $class .= 'site-archived ';
 463              }
 464  
 465              $actions['view'] = '<a class="' . $class . '" href="' . esc_url( get_home_url( $site->userblog_id ) ) . '">' . __( 'View' ) . '</a>';
 466  
 467              /**
 468               * Filters the action links displayed next the sites a user belongs to
 469               * in the Network Admin Users list table.
 470               *
 471               * @since 3.1.0
 472               *
 473               * @param string[] $actions     An array of action links to be displayed. Default 'Edit', 'View'.
 474               * @param int      $userblog_id The site ID.
 475               */
 476              $actions = apply_filters( 'ms_user_list_site_actions', $actions, $site->userblog_id );
 477  
 478              $action_count = count( $actions );
 479  
 480              $i = 0;
 481  
 482              foreach ( $actions as $action => $link ) {
 483                  ++$i;
 484  
 485                  $separator = ( $i < $action_count ) ? ' | ' : '';
 486  
 487                  echo "<span class='$action'>{$link}{$separator}</span>";
 488              }
 489  
 490              echo '</small></span><br />';
 491          }
 492      }
 493  
 494      /**
 495       * Handles the default column output.
 496       *
 497       * @since 4.3.0
 498       * @since 5.9.0 Renamed `$user` to `$item` to match parent class for PHP 8 named parameter support.
 499       *
 500       * @param WP_User $item        The current WP_User object.
 501       * @param string  $column_name The current column name.
 502       */
 503  	public function column_default( $item, $column_name ) {
 504          // Restores the more descriptive, specific name for use within this method.
 505          $user = $item;
 506  
 507          /** This filter is documented in wp-admin/includes/class-wp-users-list-table.php */
 508          $column_output = apply_filters( 'manage_users_custom_column', '', $column_name, $user->ID );
 509  
 510          /**
 511           * Filters the display output of custom columns in the Network Users list table.
 512           *
 513           * @since 6.8.0
 514           *
 515           * @param string $output      Custom column output. Default empty.
 516           * @param string $column_name Name of the custom column.
 517           * @param int    $user_id     ID of the currently-listed user.
 518           */
 519          echo apply_filters( 'manage_users-network_custom_column', $column_output, $column_name, $user->ID );
 520      }
 521  
 522      /**
 523       * Generates the list table rows.
 524       *
 525       * @since 3.1.0
 526       */
 527  	public function display_rows() {
 528          foreach ( $this->items as $user ) {
 529              $class = '';
 530  
 531              $status_list = array(
 532                  'spam'    => 'site-spammed',
 533                  'deleted' => 'site-deleted',
 534              );
 535  
 536              foreach ( $status_list as $status => $col ) {
 537                  if ( $user->$status ) {
 538                      $class .= " $col";
 539                  }
 540              }
 541  
 542              ?>
 543              <tr id="user-<?php echo (int) $user->ID; ?>" class="<?php echo trim( $class ); ?>">
 544                  <?php $this->single_row_columns( $user ); ?>
 545              </tr>
 546              <?php
 547          }
 548      }
 549  
 550      /**
 551       * Gets the name of the default primary column.
 552       *
 553       * @since 4.3.0
 554       *
 555       * @return string Name of the default primary column, in this case, 'username'.
 556       */
 557  	protected function get_default_primary_column_name() {
 558          return 'username';
 559      }
 560  
 561      /**
 562       * Generates and displays row action links.
 563       *
 564       * @since 4.3.0
 565       * @since 5.9.0 Renamed `$user` to `$item` to match parent class for PHP 8 named parameter support.
 566       *
 567       * @param WP_User $item        User being acted upon.
 568       * @param string  $column_name Current column name.
 569       * @param string  $primary     Primary column name.
 570       * @return string Row actions output for users in Multisite, or an empty string
 571       *                if the current column is not the primary column.
 572       */
 573  	protected function handle_row_actions( $item, $column_name, $primary ) {
 574          if ( $primary !== $column_name ) {
 575              return '';
 576          }
 577  
 578          // Restores the more descriptive, specific name for use within this method.
 579          $user = $item;
 580  
 581          $super_admins = get_super_admins();
 582          $actions      = array();
 583  
 584          if ( current_user_can( 'edit_user', $user->ID ) ) {
 585              $edit_link       = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user->ID ) ) );
 586              $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
 587          }
 588  
 589          if ( current_user_can( 'delete_user', $user->ID ) && ! in_array( $user->user_login, $super_admins, true ) ) {
 590              $actions['delete'] = '<a href="' . esc_url( network_admin_url( add_query_arg( '_wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), wp_nonce_url( 'users.php', 'deleteuser' ) . '&amp;action=deleteuser&amp;id=' . $user->ID ) ) ) . '" class="delete">' . __( 'Delete' ) . '</a>';
 591          }
 592  
 593          /**
 594           * Filters the action links displayed under each user in the Network Admin Users list table.
 595           *
 596           * @since 3.2.0
 597           *
 598           * @param string[] $actions An array of action links to be displayed. Default 'Edit', 'Delete'.
 599           * @param WP_User  $user    WP_User object.
 600           */
 601          $actions = apply_filters( 'ms_user_row_actions', $actions, $user );
 602  
 603          return $this->row_actions( $actions );
 604      }
 605  
 606      /**
 607       * Returns a clean label for the primary (Username) column's row header `aria-label`.
 608       *
 609       * Provides screen readers with just the user login as the row header name,
 610       * preventing them from computing the name from the full cell content.
 611       *
 612       * @since 7.1.0
 613       *
 614       * @param WP_User $user The current WP_User object.
 615       * @return string The user login.
 616       */
 617  	protected function get_primary_column_aria_label( $user ) {
 618          return $user->user_login;
 619      }
 620  }


Generated : Mon Oct 19 08:20:34 2026 Cross-referenced by PHPXref