3 * Users List Table class.
6 * @subpackage List_Table
10 class WP_Users_List_Table extends WP_List_Table {
15 function WP_Users_List_Table() {
16 $screen = get_current_screen();
17 $this->is_site_users = 'site-users-network' == $screen->id;
19 if ( $this->is_site_users )
20 $this->site_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
22 parent::WP_List_Table( array(
28 function ajax_user_can() {
29 if ( $this->is_site_users )
30 return current_user_can( 'manage_sites' );
32 return current_user_can( 'list_users' );
35 function prepare_items() {
36 global $role, $usersearch;
38 $usersearch = isset( $_REQUEST['s'] ) ? $_REQUEST['s'] : '';
40 $role = isset( $_REQUEST['role'] ) ? $_REQUEST['role'] : '';
42 $per_page = ( $this->is_site_users ) ? 'site_users_network_per_page' : 'users_per_page';
43 $users_per_page = $this->get_items_per_page( $per_page );
45 $paged = $this->get_pagenum();
48 'number' => $users_per_page,
49 'offset' => ( $paged-1 ) * $users_per_page,
51 'search' => $usersearch,
52 'fields' => 'all_with_meta'
55 $args['search'] = '*' . $args['search'] . '*';
57 if ( $this->is_site_users )
58 $args['blog_id'] = $this->site_id;
60 if ( isset( $_REQUEST['orderby'] ) )
61 $args['orderby'] = $_REQUEST['orderby'];
63 if ( isset( $_REQUEST['order'] ) )
64 $args['order'] = $_REQUEST['order'];
66 // Query the user IDs for this page
67 $wp_user_search = new WP_User_Query( $args );
69 $this->items = $wp_user_search->get_results();
71 $this->set_pagination_args( array(
72 'total_items' => $wp_user_search->get_total(),
73 'per_page' => $users_per_page,
78 _e( 'No matching users were found.' );
81 function get_views() {
82 global $wp_roles, $role;
84 if ( $this->is_site_users ) {
85 $url = 'site-users.php?id=' . $this->site_id;
86 switch_to_blog( $this->site_id );
87 $users_of_blog = count_users();
88 restore_current_blog();
91 $users_of_blog = count_users();
93 $total_users = $users_of_blog['total_users'];
94 $avail_roles =& $users_of_blog['avail_roles'];
95 unset($users_of_blog);
97 $current_role = false;
98 $class = empty($role) ? ' class="current"' : '';
99 $role_links = array();
100 $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>';
101 foreach ( $wp_roles->get_names() as $this_role => $name ) {
102 if ( !isset($avail_roles[$this_role]) )
107 if ( $this_role == $role ) {
108 $current_role = $role;
109 $class = ' class="current"';
112 $name = translate_user_role( $name );
113 /* translators: User role name with count */
114 $name = sprintf( __('%1$s <span class="count">(%2$s)</span>'), $name, $avail_roles[$this_role] );
115 $role_links[$this_role] = "<a href='" . add_query_arg( 'role', $this_role, $url ) . "'$class>$name</a>";
121 function get_bulk_actions() {
124 if ( is_multisite() ) {
125 if ( current_user_can( 'remove_users' ) )
126 $actions['remove'] = __( 'Remove' );
128 if ( current_user_can( 'delete_users' ) )
129 $actions['delete'] = __( 'Delete' );
135 function extra_tablenav( $which ) {
136 if ( 'top' != $which )
138 if ( ! current_user_can( 'promote_users' ) )
141 <div class="alignleft actions">
142 <label class="screen-reader-text" for="new_role"><?php _e( 'Change role to…' ) ?></label>
143 <select name="new_role" id="new_role">
144 <option value=''><?php _e( 'Change role to…' ) ?></option>
145 <?php wp_dropdown_roles(); ?>
147 <?php submit_button( __( 'Change' ), 'secondary', 'changeit', false ); ?>
152 function current_action() {
153 if ( isset($_REQUEST['changeit']) && !empty($_REQUEST['new_role']) )
156 return parent::current_action();
159 function get_columns() {
161 'cb' => '<input type="checkbox" />',
162 'username' => __( 'Username' ),
163 'name' => __( 'Name' ),
164 'email' => __( 'E-mail' ),
165 'role' => __( 'Role' ),
166 'posts' => __( 'Posts' )
169 if ( $this->is_site_users )
170 unset( $c['posts'] );
175 function get_sortable_columns() {
177 'username' => 'login',
182 if ( $this->is_site_users )
183 unset( $c['posts'] );
188 function display_rows() {
189 // Query the post counts for this page
190 if ( ! $this->is_site_users )
191 $post_counts = count_many_users_posts( array_keys( $this->items ) );
194 foreach ( $this->items as $userid => $user_object ) {
195 $role = reset( $user_object->roles );
197 if ( is_multisite() && empty( $role ) )
200 $style = ( ' class="alternate"' == $style ) ? '' : ' class="alternate"';
201 echo "\n\t", $this->single_row( $user_object, $style, $role, isset( $post_counts ) ? $post_counts[ $userid ] : 0 );
206 * Generate HTML for a single row on the users.php admin panel.
210 * @param object $user_object
211 * @param string $style Optional. Attributes added to the TR element. Must be sanitized.
212 * @param string $role Key for the $wp_roles array.
213 * @param int $numposts Optional. Post count to display for this user. Defaults to zero, as in, a new user has made zero posts.
216 function single_row( $user_object, $style = '', $role = '', $numposts = 0 ) {
219 if ( !( is_object( $user_object ) && is_a( $user_object, 'WP_User' ) ) )
220 $user_object = new WP_User( (int) $user_object );
221 $user_object = sanitize_user_object( $user_object, 'display' );
222 $email = $user_object->user_email;
224 if ( $this->is_site_users )
225 $url = "site-users.php?id={$this->site_id}&";
230 // Check if the user for this row is editable
231 if ( current_user_can( 'list_users' ) ) {
232 // Set up the user editing link
233 // TODO: make profile/user-edit determination a separate function
234 if ( get_current_user_id() == $user_object->ID ) {
235 $edit_link = 'profile.php';
237 $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( stripslashes( $_SERVER['REQUEST_URI'] ) ), "user-edit.php?user_id=$user_object->ID" ) );
240 // Set up the hover actions for this user
243 if ( current_user_can( 'edit_user', $user_object->ID ) ) {
244 $edit = "<strong><a href=\"$edit_link\">$user_object->user_login</a></strong><br />";
245 $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
247 $edit = "<strong>$user_object->user_login</strong><br />";
250 if ( !is_multisite() && get_current_user_id() != $user_object->ID && current_user_can( 'delete_user', $user_object->ID ) )
251 $actions['delete'] = "<a class='submitdelete' href='" . wp_nonce_url( "users.php?action=delete&user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Delete' ) . "</a>";
252 if ( is_multisite() && get_current_user_id() != $user_object->ID && current_user_can( 'remove_user', $user_object->ID ) )
253 $actions['remove'] = "<a class='submitdelete' href='" . wp_nonce_url( $url."action=remove&user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Remove' ) . "</a>";
254 $actions = apply_filters( 'user_row_actions', $actions, $user_object );
255 $edit .= $this->row_actions( $actions );
257 // Set up the checkbox ( because the user is editable, otherwise its empty )
258 $checkbox = "<input type='checkbox' name='users[]' id='user_{$user_object->ID}' class='$role' value='{$user_object->ID}' />";
261 $edit = '<strong>' . $user_object->user_login . '</strong>';
263 $role_name = isset( $wp_roles->role_names[$role] ) ? translate_user_role( $wp_roles->role_names[$role] ) : __( 'None' );
264 $avatar = get_avatar( $user_object->ID, 32 );
266 $r = "<tr id='user-$user_object->ID'$style>";
268 list( $columns, $hidden ) = $this->get_column_info();
270 foreach ( $columns as $column_name => $column_display_name ) {
271 $class = "class=\"$column_name column-$column_name\"";
274 if ( in_array( $column_name, $hidden ) )
275 $style = ' style="display:none;"';
277 $attributes = "$class$style";
279 switch ( $column_name ) {
281 $r .= "<th scope='row' class='check-column'>$checkbox</th>";
284 $r .= "<td $attributes>$avatar $edit</td>";
287 $r .= "<td $attributes>$user_object->first_name $user_object->last_name</td>";
290 $r .= "<td $attributes><a href='mailto:$email' title='" . esc_attr( sprintf( __( 'E-mail: %s' ), $email ) ) . "'>$email</a></td>";
293 $r .= "<td $attributes>$role_name</td>";
296 $attributes = 'class="posts column-posts num"' . $style;
297 $r .= "<td $attributes>";
298 if ( $numposts > 0 ) {
299 $r .= "<a href='edit.php?author=$user_object->ID' title='" . esc_attr__( 'View posts by this author' ) . "' class='edit'>";
308 $r .= "<td $attributes>";
309 $r .= apply_filters( 'manage_users_custom_column', '', $column_name, $user_object->ID );