3 * List Table API: WP_Users_List_Table class
6 * @subpackage Administration
11 * Core class used to implement displaying users in a list table.
18 class WP_Users_List_Table extends WP_List_Table {
21 * Site ID to generate the Users list table for.
30 * Whether or not the current Users list table is for Multisite.
36 public $is_site_users;
44 * @see WP_List_Table::__construct() for more information on default arguments.
46 * @param array $args An associative array of arguments.
48 public function __construct( $args = array() ) {
49 parent::__construct( array(
52 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
55 $this->is_site_users = 'site-users-network' === $this->screen->id;
57 if ( $this->is_site_users )
58 $this->site_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
62 * Check the current user's permissions.
69 public function ajax_user_can() {
70 if ( $this->is_site_users )
71 return current_user_can( 'manage_sites' );
73 return current_user_can( 'list_users' );
77 * Prepare the users list for display.
82 * @global string $role
83 * @global string $usersearch
85 public function prepare_items() {
86 global $role, $usersearch;
88 $usersearch = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST['s'] ) ) : '';
90 $role = isset( $_REQUEST['role'] ) ? $_REQUEST['role'] : '';
92 $per_page = ( $this->is_site_users ) ? 'site_users_network_per_page' : 'users_per_page';
93 $users_per_page = $this->get_items_per_page( $per_page );
95 $paged = $this->get_pagenum();
97 if ( 'none' === $role ) {
99 'number' => $users_per_page,
100 'offset' => ( $paged-1 ) * $users_per_page,
101 'include' => wp_get_users_with_no_role(),
102 'search' => $usersearch,
103 'fields' => 'all_with_meta'
107 'number' => $users_per_page,
108 'offset' => ( $paged-1 ) * $users_per_page,
110 'search' => $usersearch,
111 'fields' => 'all_with_meta'
115 if ( '' !== $args['search'] )
116 $args['search'] = '*' . $args['search'] . '*';
118 if ( $this->is_site_users )
119 $args['blog_id'] = $this->site_id;
121 if ( isset( $_REQUEST['orderby'] ) )
122 $args['orderby'] = $_REQUEST['orderby'];
124 if ( isset( $_REQUEST['order'] ) )
125 $args['order'] = $_REQUEST['order'];
128 * Filters the query arguments used to retrieve users for the current users list table.
132 * @param array $args Arguments passed to WP_User_Query to retrieve items for the current
135 $args = apply_filters( 'users_list_table_query_args', $args );
137 // Query the user IDs for this page
138 $wp_user_search = new WP_User_Query( $args );
140 $this->items = $wp_user_search->get_results();
142 $this->set_pagination_args( array(
143 'total_items' => $wp_user_search->get_total(),
144 'per_page' => $users_per_page,
149 * Output 'no users' message.
154 public function no_items() {
155 _e( 'No users found.' );
159 * Return an associative array listing all the views that can be used
162 * Provides a list of roles and user count for that role for easy
163 * Filtersing of the user table.
168 * @global string $role
170 * @return array An array of HTML links, one for each view.
172 protected function get_views() {
175 $wp_roles = wp_roles();
177 if ( $this->is_site_users ) {
178 $url = 'site-users.php?id=' . $this->site_id;
179 switch_to_blog( $this->site_id );
180 $users_of_blog = count_users();
181 restore_current_blog();
184 $users_of_blog = count_users();
187 $total_users = $users_of_blog['total_users'];
188 $avail_roles =& $users_of_blog['avail_roles'];
189 unset($users_of_blog);
191 $class = empty($role) ? ' class="current"' : '';
192 $role_links = array();
193 $role_links['all'] = "<a href='$url'$class>" . sprintf( _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $total_users, 'users' ), number_format_i18n( $total_users ) ) . '</a>';
194 foreach ( $wp_roles->get_names() as $this_role => $name ) {
195 if ( !isset($avail_roles[$this_role]) )
200 if ( $this_role === $role ) {
201 $class = ' class="current"';
204 $name = translate_user_role( $name );
205 /* translators: User role name with count */
206 $name = sprintf( __('%1$s <span class="count">(%2$s)</span>'), $name, number_format_i18n( $avail_roles[$this_role] ) );
207 $role_links[$this_role] = "<a href='" . esc_url( add_query_arg( 'role', $this_role, $url ) ) . "'$class>$name</a>";
210 if ( ! empty( $avail_roles['none' ] ) ) {
214 if ( 'none' === $role ) {
215 $class = ' class="current"';
218 $name = __( 'No role' );
219 /* translators: User role name with count */
220 $name = sprintf( __('%1$s <span class="count">(%2$s)</span>'), $name, number_format_i18n( $avail_roles['none' ] ) );
221 $role_links['none'] = "<a href='" . esc_url( add_query_arg( 'role', 'none', $url ) ) . "'$class>$name</a>";
229 * Retrieve an associative array of bulk actions available on this table.
234 * @return array Array of bulk actions.
236 protected function get_bulk_actions() {
239 if ( is_multisite() ) {
240 if ( current_user_can( 'remove_users' ) )
241 $actions['remove'] = __( 'Remove' );
243 if ( current_user_can( 'delete_users' ) )
244 $actions['delete'] = __( 'Delete' );
251 * Output the controls to allow user roles to be changed in bulk.
256 * @param string $which Whether this is being invoked above ("top")
257 * or below the table ("bottom").
259 protected function extra_tablenav( $which ) {
260 $id = 'bottom' === $which ? 'new_role2' : 'new_role';
262 <div class="alignleft actions">
263 <?php if ( current_user_can( 'promote_users' ) && $this->has_items() ) : ?>
264 <label class="screen-reader-text" for="<?php echo $id ?>"><?php _e( 'Change role to…' ) ?></label>
265 <select name="<?php echo $id ?>" id="<?php echo $id ?>">
266 <option value=""><?php _e( 'Change role to…' ) ?></option>
267 <?php wp_dropdown_roles(); ?>
270 submit_button( __( 'Change' ), '', 'changeit', false );
274 * Fires just before the closing div containing the bulk role-change controls
275 * in the Users list table.
278 * @since 4.6.0 The `$which` parameter was added.
280 * @param string $which The location of the extra table nav markup: 'top' or 'bottom'.
282 do_action( 'restrict_manage_users', $which );
287 * Capture the bulk action required, and return it.
289 * Overridden from the base class implementation to capture
290 * the role change drop-down.
295 * @return string The bulk action required.
297 public function current_action() {
298 if ( isset( $_REQUEST['changeit'] ) &&
299 ( ! empty( $_REQUEST['new_role'] ) || ! empty( $_REQUEST['new_role2'] ) ) ) {
303 return parent::current_action();
307 * Get a list of columns for the list table.
312 * @return array Array in which the key is the ID of the column,
313 * and the value is the description.
315 public function get_columns() {
317 'cb' => '<input type="checkbox" />',
318 'username' => __( 'Username' ),
319 'name' => __( 'Name' ),
320 'email' => __( 'Email' ),
321 'role' => __( 'Role' ),
322 'posts' => __( 'Posts' )
325 if ( $this->is_site_users )
326 unset( $c['posts'] );
332 * Get a list of sortable columns for the list table.
337 * @return array Array of sortable columns.
339 protected function get_sortable_columns() {
341 'username' => 'login',
349 * Generate the list table rows.
354 public function display_rows() {
355 // Query the post counts for this page
356 if ( ! $this->is_site_users )
357 $post_counts = count_many_users_posts( array_keys( $this->items ) );
359 foreach ( $this->items as $userid => $user_object ) {
360 if ( is_multisite() && empty( $user_object->allcaps ) )
363 echo "\n\t" . $this->single_row( $user_object, '', '', isset( $post_counts ) ? $post_counts[ $userid ] : 0 );
368 * Generate HTML for a single row on the users.php admin panel.
371 * @since 4.2.0 The `$style` parameter was deprecated.
372 * @since 4.4.0 The `$role` parameter was deprecated.
375 * @param object $user_object The current user object.
376 * @param string $style Deprecated. Not used.
377 * @param string $role Deprecated. Not used.
378 * @param int $numposts Optional. Post count to display for this user. Defaults
379 * to zero, as in, a new user has made zero posts.
380 * @return string Output for a single row.
382 public function single_row( $user_object, $style = '', $role = '', $numposts = 0 ) {
383 if ( ! ( $user_object instanceof WP_User ) ) {
384 $user_object = get_userdata( (int) $user_object );
386 $user_object->filter = 'display';
387 $email = $user_object->user_email;
389 if ( $this->is_site_users )
390 $url = "site-users.php?id={$this->site_id}&";
394 $user_roles = $this->get_role_list( $user_object );
396 // Set up the hover actions for this user
399 // Check if the user for this row is editable
400 if ( current_user_can( 'list_users' ) ) {
401 // Set up the user editing link
402 $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user_object->ID ) ) );
404 if ( current_user_can( 'edit_user', $user_object->ID ) ) {
405 $edit = "<strong><a href=\"$edit_link\">$user_object->user_login</a></strong><br />";
406 $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
408 $edit = "<strong>$user_object->user_login</strong><br />";
411 if ( !is_multisite() && get_current_user_id() != $user_object->ID && current_user_can( 'delete_user', $user_object->ID ) )
412 $actions['delete'] = "<a class='submitdelete' href='" . wp_nonce_url( "users.php?action=delete&user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Delete' ) . "</a>";
413 if ( is_multisite() && get_current_user_id() != $user_object->ID && current_user_can( 'remove_user', $user_object->ID ) )
414 $actions['remove'] = "<a class='submitdelete' href='" . wp_nonce_url( $url."action=remove&user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Remove' ) . "</a>";
417 * Filters the action links displayed under each user in the Users list table.
421 * @param array $actions An array of action links to be displayed.
422 * Default 'Edit', 'Delete' for single site, and
423 * 'Edit', 'Remove' for Multisite.
424 * @param WP_User $user_object WP_User object for the currently-listed user.
426 $actions = apply_filters( 'user_row_actions', $actions, $user_object );
429 $role_classes = esc_attr( implode( ' ', array_keys( $user_roles ) ) );
431 // Set up the checkbox ( because the user is editable, otherwise it's empty )
432 $checkbox = '<label class="screen-reader-text" for="user_' . $user_object->ID . '">' . sprintf( __( 'Select %s' ), $user_object->user_login ) . '</label>'
433 . "<input type='checkbox' name='users[]' id='user_{$user_object->ID}' class='{$role_classes}' value='{$user_object->ID}' />";
436 $edit = '<strong>' . $user_object->user_login . '</strong>';
438 $avatar = get_avatar( $user_object->ID, 32 );
440 // Comma-separated list of user roles.
441 $roles_list = implode( ', ', $user_roles );
443 $r = "<tr id='user-$user_object->ID'>";
445 list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
447 foreach ( $columns as $column_name => $column_display_name ) {
448 $classes = "$column_name column-$column_name";
449 if ( $primary === $column_name ) {
450 $classes .= ' has-row-actions column-primary';
452 if ( 'posts' === $column_name ) {
453 $classes .= ' num'; // Special case for that column
456 if ( in_array( $column_name, $hidden ) ) {
457 $classes .= ' hidden';
460 $data = 'data-colname="' . wp_strip_all_tags( $column_display_name ) . '"';
462 $attributes = "class='$classes' $data";
464 if ( 'cb' === $column_name ) {
465 $r .= "<th scope='row' class='check-column'>$checkbox</th>";
467 $r .= "<td $attributes>";
468 switch ( $column_name ) {
470 $r .= "$avatar $edit";
473 $r .= "$user_object->first_name $user_object->last_name";
476 $r .= "<a href='" . esc_url( "mailto:$email" ) . "'>$email</a>";
479 $r .= esc_html( $roles_list );
482 if ( $numposts > 0 ) {
483 $r .= "<a href='edit.php?author=$user_object->ID' class='edit'>";
484 $r .= '<span aria-hidden="true">' . $numposts . '</span>';
485 $r .= '<span class="screen-reader-text">' . sprintf( _n( '%s post by this author', '%s posts by this author', $numposts ), number_format_i18n( $numposts ) ) . '</span>';
493 * Filters the display output of custom columns in the Users list table.
497 * @param string $output Custom column output. Default empty.
498 * @param string $column_name Column name.
499 * @param int $user_id ID of the currently-listed user.
501 $r .= apply_filters( 'manage_users_custom_column', '', $column_name, $user_object->ID );
504 if ( $primary === $column_name ) {
505 $r .= $this->row_actions( $actions );
516 * Gets the name of the default primary column.
521 * @return string Name of the default primary column, in this case, 'username'.
523 protected function get_default_primary_column_name() {
528 * Returns an array of user roles for a given user object.
533 * @param WP_User $user_object The WP_User object.
534 * @return array An array of user roles.
536 protected function get_role_list( $user_object ) {
537 $wp_roles = wp_roles();
539 $role_list = array();
541 foreach ( $user_object->roles as $role ) {
542 if ( isset( $wp_roles->role_names[ $role ] ) ) {
543 $role_list[ $role ] = translate_user_role( $wp_roles->role_names[ $role ] );
547 if ( empty( $role_list ) ) {
548 $role_list['none'] = _x( 'None', 'no user roles' );
552 * Filters the returned array of roles for a user.
556 * @param array $role_list An array of user roles.
557 * @param WP_User $user_object A WP_User object.
559 return apply_filters( 'get_role_list', $role_list, $user_object );