[ Index ]

PHP Cross Reference of WordPress

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


Generated: Mon Nov 18 01:00:04 2019 Cross-referenced by PHPXref 0.7.1