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


Generated : Sat Nov 23 08:20:01 2024 Cross-referenced by PHPXref