3 * Users List Table class.
9 * @subpackage List_Table
11 class WP_Users_List_Table extends WP_List_Table {
14 * Site ID to generate the Users list table for.
23 * Whether or not the current Users list table is for Multisite.
37 function __construct( $args = array() ) {
38 parent::__construct( array(
41 'screen' => isset( $args['screen'] ) ? $args['screen'] : null,
44 $this->is_site_users = 'site-users-network' == $this->screen->id;
46 if ( $this->is_site_users )
47 $this->site_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
51 * Check the current user's permissions.
56 function ajax_user_can() {
57 if ( $this->is_site_users )
58 return current_user_can( 'manage_sites' );
60 return current_user_can( 'list_users' );
64 * Prepare the users list for display.
69 function prepare_items() {
70 global $role, $usersearch;
72 $usersearch = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST['s'] ) ) : '';
74 $role = isset( $_REQUEST['role'] ) ? $_REQUEST['role'] : '';
76 $per_page = ( $this->is_site_users ) ? 'site_users_network_per_page' : 'users_per_page';
77 $users_per_page = $this->get_items_per_page( $per_page );
79 $paged = $this->get_pagenum();
82 'number' => $users_per_page,
83 'offset' => ( $paged-1 ) * $users_per_page,
85 'search' => $usersearch,
86 'fields' => 'all_with_meta'
89 if ( '' !== $args['search'] )
90 $args['search'] = '*' . $args['search'] . '*';
92 if ( $this->is_site_users )
93 $args['blog_id'] = $this->site_id;
95 if ( isset( $_REQUEST['orderby'] ) )
96 $args['orderby'] = $_REQUEST['orderby'];
98 if ( isset( $_REQUEST['order'] ) )
99 $args['order'] = $_REQUEST['order'];
101 // Query the user IDs for this page
102 $wp_user_search = new WP_User_Query( $args );
104 $this->items = $wp_user_search->get_results();
106 $this->set_pagination_args( array(
107 'total_items' => $wp_user_search->get_total(),
108 'per_page' => $users_per_page,
113 * Output 'no users' message.
118 function no_items() {
119 _e( 'No matching users were found.' );
123 * Return an associative array listing all the views that can be used
126 * Provides a list of roles and user count for that role for easy
127 * filtering of the user table.
132 * @return array An array of HTML links, one for each view.
134 function get_views() {
135 global $wp_roles, $role;
137 if ( $this->is_site_users ) {
138 $url = 'site-users.php?id=' . $this->site_id;
139 switch_to_blog( $this->site_id );
140 $users_of_blog = count_users();
141 restore_current_blog();
144 $users_of_blog = count_users();
146 $total_users = $users_of_blog['total_users'];
147 $avail_roles =& $users_of_blog['avail_roles'];
148 unset($users_of_blog);
150 $current_role = false;
151 $class = empty($role) ? ' class="current"' : '';
152 $role_links = array();
153 $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>';
154 foreach ( $wp_roles->get_names() as $this_role => $name ) {
155 if ( !isset($avail_roles[$this_role]) )
160 if ( $this_role == $role ) {
161 $current_role = $role;
162 $class = ' class="current"';
165 $name = translate_user_role( $name );
166 /* translators: User role name with count */
167 $name = sprintf( __('%1$s <span class="count">(%2$s)</span>'), $name, number_format_i18n( $avail_roles[$this_role] ) );
168 $role_links[$this_role] = "<a href='" . esc_url( add_query_arg( 'role', $this_role, $url ) ) . "'$class>$name</a>";
175 * Retrieve an associative array of bulk actions available on this table.
180 * @return array Array of bulk actions.
182 function get_bulk_actions() {
185 if ( is_multisite() ) {
186 if ( current_user_can( 'remove_users' ) )
187 $actions['remove'] = __( 'Remove' );
189 if ( current_user_can( 'delete_users' ) )
190 $actions['delete'] = __( 'Delete' );
197 * Output the controls to allow user roles to be changed in bulk.
202 * @param string $which Whether this is being invoked above ("top")
203 * or below the table ("bottom").
205 function extra_tablenav( $which ) {
206 if ( 'top' != $which )
209 <div class="alignleft actions">
210 <?php if ( current_user_can( 'promote_users' ) ) : ?>
211 <label class="screen-reader-text" for="new_role"><?php _e( 'Change role to…' ) ?></label>
212 <select name="new_role" id="new_role">
213 <option value=''><?php _e( 'Change role to…' ) ?></option>
214 <?php wp_dropdown_roles(); ?>
217 submit_button( __( 'Change' ), 'button', 'changeit', false );
221 * Fires just before the closing div containing the bulk role-change controls
222 * in the Users list table.
226 do_action( 'restrict_manage_users' );
231 * Capture the bulk action required, and return it.
233 * Overridden from the base class implementation to capture
234 * the role change drop-down.
239 * @return string The bulk action required.
241 function current_action() {
242 if ( isset($_REQUEST['changeit']) && !empty($_REQUEST['new_role']) )
245 return parent::current_action();
249 * Get a list of columns for the list table.
254 * @return array Array in which the key is the ID of the column,
255 * and the value is the description.
257 function get_columns() {
259 'cb' => '<input type="checkbox" />',
260 'username' => __( 'Username' ),
261 'name' => __( 'Name' ),
262 'email' => __( 'E-mail' ),
263 'role' => __( 'Role' ),
264 'posts' => __( 'Posts' )
267 if ( $this->is_site_users )
268 unset( $c['posts'] );
274 * Get a list of sortable columns for the list table.
279 * @return array Array of sortable columns.
281 function get_sortable_columns() {
283 'username' => 'login',
288 if ( $this->is_site_users )
289 unset( $c['posts'] );
295 * Generate the list table rows.
300 function display_rows() {
301 // Query the post counts for this page
302 if ( ! $this->is_site_users )
303 $post_counts = count_many_users_posts( array_keys( $this->items ) );
305 $editable_roles = array_keys( get_editable_roles() );
308 foreach ( $this->items as $userid => $user_object ) {
309 if ( count( $user_object->roles ) <= 1 ) {
310 $role = reset( $user_object->roles );
311 } elseif ( $roles = array_intersect( array_values( $user_object->roles ), $editable_roles ) ) {
312 $role = reset( $roles );
314 $role = reset( $user_object->roles );
317 if ( is_multisite() && empty( $user_object->allcaps ) )
320 $style = ( ' class="alternate"' == $style ) ? '' : ' class="alternate"';
321 echo "\n\t" . $this->single_row( $user_object, $style, $role, isset( $post_counts ) ? $post_counts[ $userid ] : 0 );
326 * Generate HTML for a single row on the users.php admin panel.
331 * @param object $user_object The current user object.
332 * @param string $style Optional. Style attributes added to the <tr> element.
333 * Must be sanitized. Default empty.
334 * @param string $role Optional. Key for the $wp_roles array. Default empty.
335 * @param int $numposts Optional. Post count to display for this user. Defaults
336 * to zero, as in, a new user has made zero posts.
337 * @return string Output for a single row.
339 function single_row( $user_object, $style = '', $role = '', $numposts = 0 ) {
342 if ( !( is_object( $user_object ) && is_a( $user_object, 'WP_User' ) ) )
343 $user_object = get_userdata( (int) $user_object );
344 $user_object->filter = 'display';
345 $email = $user_object->user_email;
347 if ( $this->is_site_users )
348 $url = "site-users.php?id={$this->site_id}&";
353 // Check if the user for this row is editable
354 if ( current_user_can( 'list_users' ) ) {
355 // Set up the user editing link
356 $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user_object->ID ) ) );
358 // Set up the hover actions for this user
361 if ( current_user_can( 'edit_user', $user_object->ID ) ) {
362 $edit = "<strong><a href=\"$edit_link\">$user_object->user_login</a></strong><br />";
363 $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
365 $edit = "<strong>$user_object->user_login</strong><br />";
368 if ( !is_multisite() && get_current_user_id() != $user_object->ID && current_user_can( 'delete_user', $user_object->ID ) )
369 $actions['delete'] = "<a class='submitdelete' href='" . wp_nonce_url( "users.php?action=delete&user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Delete' ) . "</a>";
370 if ( is_multisite() && get_current_user_id() != $user_object->ID && current_user_can( 'remove_user', $user_object->ID ) )
371 $actions['remove'] = "<a class='submitdelete' href='" . wp_nonce_url( $url."action=remove&user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Remove' ) . "</a>";
374 * Filter the action links displayed under each user in the Users list table.
378 * @param array $actions An array of action links to be displayed.
379 * Default 'Edit', 'Delete' for single site, and
380 * 'Edit', 'Remove' for Multisite.
381 * @param WP_User $user_object WP_User object for the currently-listed user.
383 $actions = apply_filters( 'user_row_actions', $actions, $user_object );
384 $edit .= $this->row_actions( $actions );
386 // Set up the checkbox ( because the user is editable, otherwise it's empty )
387 $checkbox = '<label class="screen-reader-text" for="cb-select-' . $user_object->ID . '">' . sprintf( __( 'Select %s' ), $user_object->user_login ) . '</label>'
388 . "<input type='checkbox' name='users[]' id='user_{$user_object->ID}' class='$role' value='{$user_object->ID}' />";
391 $edit = '<strong>' . $user_object->user_login . '</strong>';
393 $role_name = isset( $wp_roles->role_names[$role] ) ? translate_user_role( $wp_roles->role_names[$role] ) : __( 'None' );
394 $avatar = get_avatar( $user_object->ID, 32 );
396 $r = "<tr id='user-$user_object->ID'$style>";
398 list( $columns, $hidden ) = $this->get_column_info();
400 foreach ( $columns as $column_name => $column_display_name ) {
401 $class = "class=\"$column_name column-$column_name\"";
404 if ( in_array( $column_name, $hidden ) )
405 $style = ' style="display:none;"';
407 $attributes = "$class$style";
409 switch ( $column_name ) {
411 $r .= "<th scope='row' class='check-column'>$checkbox</th>";
414 $r .= "<td $attributes>$avatar $edit</td>";
417 $r .= "<td $attributes>$user_object->first_name $user_object->last_name</td>";
420 $r .= "<td $attributes><a href='mailto:$email' title='" . esc_attr( sprintf( __( 'E-mail: %s' ), $email ) ) . "'>$email</a></td>";
423 $r .= "<td $attributes>$role_name</td>";
426 $attributes = 'class="posts column-posts num"' . $style;
427 $r .= "<td $attributes>";
428 if ( $numposts > 0 ) {
429 $r .= "<a href='edit.php?author=$user_object->ID' title='" . esc_attr__( 'View posts by this author' ) . "' class='edit'>";
438 $r .= "<td $attributes>";
441 * Filter the display output of custom columns in the Users list table.
445 * @param string $output Custom column output. Default empty.
446 * @param string $column_name Column name.
447 * @param int $user_id ID of the currently-listed user.
449 $r .= apply_filters( 'manage_users_custom_column', '', $column_name, $user_object->ID );