3 * Users List Table class.
6 * @subpackage List_Table
10 class WP_Users_List_Table extends WP_List_Table {
15 function __construct() {
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::__construct( 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 if ( '' !== $args['search'] )
56 $args['search'] = '*' . $args['search'] . '*';
58 if ( $this->is_site_users )
59 $args['blog_id'] = $this->site_id;
61 if ( isset( $_REQUEST['orderby'] ) )
62 $args['orderby'] = $_REQUEST['orderby'];
64 if ( isset( $_REQUEST['order'] ) )
65 $args['order'] = $_REQUEST['order'];
67 // Query the user IDs for this page
68 $wp_user_search = new WP_User_Query( $args );
70 $this->items = $wp_user_search->get_results();
72 $this->set_pagination_args( array(
73 'total_items' => $wp_user_search->get_total(),
74 'per_page' => $users_per_page,
79 _e( 'No matching users were found.' );
82 function get_views() {
83 global $wp_roles, $role;
85 if ( $this->is_site_users ) {
86 $url = 'site-users.php?id=' . $this->site_id;
87 switch_to_blog( $this->site_id );
88 $users_of_blog = count_users();
89 restore_current_blog();
92 $users_of_blog = count_users();
94 $total_users = $users_of_blog['total_users'];
95 $avail_roles =& $users_of_blog['avail_roles'];
96 unset($users_of_blog);
98 $current_role = false;
99 $class = empty($role) ? ' class="current"' : '';
100 $role_links = array();
101 $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>';
102 foreach ( $wp_roles->get_names() as $this_role => $name ) {
103 if ( !isset($avail_roles[$this_role]) )
108 if ( $this_role == $role ) {
109 $current_role = $role;
110 $class = ' class="current"';
113 $name = translate_user_role( $name );
114 /* translators: User role name with count */
115 $name = sprintf( __('%1$s <span class="count">(%2$s)</span>'), $name, number_format_i18n( $avail_roles[$this_role] ) );
116 $role_links[$this_role] = "<a href='" . esc_url( add_query_arg( 'role', $this_role, $url ) ) . "'$class>$name</a>";
122 function get_bulk_actions() {
125 if ( is_multisite() ) {
126 if ( current_user_can( 'remove_users' ) )
127 $actions['remove'] = __( 'Remove' );
129 if ( current_user_can( 'delete_users' ) )
130 $actions['delete'] = __( 'Delete' );
136 function extra_tablenav( $which ) {
137 if ( 'top' != $which )
139 if ( ! current_user_can( 'promote_users' ) )
142 <div class="alignleft actions">
143 <label class="screen-reader-text" for="new_role"><?php _e( 'Change role to…' ) ?></label>
144 <select name="new_role" id="new_role">
145 <option value=''><?php _e( 'Change role to…' ) ?></option>
146 <?php wp_dropdown_roles(); ?>
148 <?php submit_button( __( 'Change' ), 'secondary', 'changeit', false ); ?>
153 function current_action() {
154 if ( isset($_REQUEST['changeit']) && !empty($_REQUEST['new_role']) )
157 return parent::current_action();
160 function get_columns() {
162 'cb' => '<input type="checkbox" />',
163 'username' => __( 'Username' ),
164 'name' => __( 'Name' ),
165 'email' => __( 'E-mail' ),
166 'role' => __( 'Role' ),
167 'posts' => __( 'Posts' )
170 if ( $this->is_site_users )
171 unset( $c['posts'] );
176 function get_sortable_columns() {
178 'username' => 'login',
183 if ( $this->is_site_users )
184 unset( $c['posts'] );
189 function display_rows() {
190 // Query the post counts for this page
191 if ( ! $this->is_site_users )
192 $post_counts = count_many_users_posts( array_keys( $this->items ) );
195 foreach ( $this->items as $userid => $user_object ) {
196 $role = reset( $user_object->roles );
198 if ( is_multisite() && empty( $role ) )
201 $style = ( ' class="alternate"' == $style ) ? '' : ' class="alternate"';
202 echo "\n\t", $this->single_row( $user_object, $style, $role, isset( $post_counts ) ? $post_counts[ $userid ] : 0 );
207 * Generate HTML for a single row on the users.php admin panel.
211 * @param object $user_object
212 * @param string $style Optional. Attributes added to the TR element. Must be sanitized.
213 * @param string $role Key for the $wp_roles array.
214 * @param int $numposts Optional. Post count to display for this user. Defaults to zero, as in, a new user has made zero posts.
217 function single_row( $user_object, $style = '', $role = '', $numposts = 0 ) {
220 if ( !( is_object( $user_object ) && is_a( $user_object, 'WP_User' ) ) )
221 $user_object = new WP_User( (int) $user_object );
222 $user_object->filter = 'display';
223 $email = $user_object->user_email;
225 if ( $this->is_site_users )
226 $url = "site-users.php?id={$this->site_id}&";
231 // Check if the user for this row is editable
232 if ( current_user_can( 'list_users' ) ) {
233 // Set up the user editing link
234 // TODO: make profile/user-edit determination a separate function
235 if ( get_current_user_id() == $user_object->ID ) {
236 $edit_link = 'profile.php';
238 $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( stripslashes( $_SERVER['REQUEST_URI'] ) ), "user-edit.php?user_id=$user_object->ID" ) );
241 // Set up the hover actions for this user
244 if ( current_user_can( 'edit_user', $user_object->ID ) ) {
245 $edit = "<strong><a href=\"$edit_link\">$user_object->user_login</a></strong><br />";
246 $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
248 $edit = "<strong>$user_object->user_login</strong><br />";
251 if ( !is_multisite() && get_current_user_id() != $user_object->ID && current_user_can( 'delete_user', $user_object->ID ) )
252 $actions['delete'] = "<a class='submitdelete' href='" . wp_nonce_url( "users.php?action=delete&user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Delete' ) . "</a>";
253 if ( is_multisite() && get_current_user_id() != $user_object->ID && current_user_can( 'remove_user', $user_object->ID ) )
254 $actions['remove'] = "<a class='submitdelete' href='" . wp_nonce_url( $url."action=remove&user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Remove' ) . "</a>";
255 $actions = apply_filters( 'user_row_actions', $actions, $user_object );
256 $edit .= $this->row_actions( $actions );
258 // Set up the checkbox ( because the user is editable, otherwise its empty )
259 $checkbox = "<input type='checkbox' name='users[]' id='user_{$user_object->ID}' class='$role' value='{$user_object->ID}' />";
262 $edit = '<strong>' . $user_object->user_login . '</strong>';
264 $role_name = isset( $wp_roles->role_names[$role] ) ? translate_user_role( $wp_roles->role_names[$role] ) : __( 'None' );
265 $avatar = get_avatar( $user_object->ID, 32 );
267 $r = "<tr id='user-$user_object->ID'$style>";
269 list( $columns, $hidden ) = $this->get_column_info();
271 foreach ( $columns as $column_name => $column_display_name ) {
272 $class = "class=\"$column_name column-$column_name\"";
275 if ( in_array( $column_name, $hidden ) )
276 $style = ' style="display:none;"';
278 $attributes = "$class$style";
280 switch ( $column_name ) {
282 $r .= "<th scope='row' class='check-column'>$checkbox</th>";
285 $r .= "<td $attributes>$avatar $edit</td>";
288 $r .= "<td $attributes>$user_object->first_name $user_object->last_name</td>";
291 $r .= "<td $attributes><a href='mailto:$email' title='" . esc_attr( sprintf( __( 'E-mail: %s' ), $email ) ) . "'>$email</a></td>";
294 $r .= "<td $attributes>$role_name</td>";
297 $attributes = 'class="posts column-posts num"' . $style;
298 $r .= "<td $attributes>";
299 if ( $numposts > 0 ) {
300 $r .= "<a href='edit.php?author=$user_object->ID' title='" . esc_attr__( 'View posts by this author' ) . "' class='edit'>";
309 $r .= "<td $attributes>";
310 $r .= apply_filters( 'manage_users_custom_column', '', $column_name, $user_object->ID );