3 * List Table API: WP_MS_Users_List_Table class
6 * @subpackage Administration
11 * Core class used to implement displaying users in a list table for the network admin.
18 class WP_MS_Users_List_Table extends WP_List_Table {
23 public function ajax_user_can() {
24 return current_user_can( 'manage_network_users' );
29 * @global string $usersearch
30 * @global string $role
32 * @global string $mode
34 public function prepare_items() {
35 global $usersearch, $role, $wpdb, $mode;
37 $usersearch = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST['s'] ) ) : '';
39 $users_per_page = $this->get_items_per_page( 'users_network_per_page' );
41 $role = isset( $_REQUEST['role'] ) ? $_REQUEST['role'] : '';
43 $paged = $this->get_pagenum();
46 'number' => $users_per_page,
47 'offset' => ( $paged-1 ) * $users_per_page,
48 'search' => $usersearch,
50 'fields' => 'all_with_meta'
53 if ( wp_is_large_network( 'users' ) ) {
54 $args['search'] = ltrim( $args['search'], '*' );
55 } else if ( '' !== $args['search'] ) {
56 $args['search'] = trim( $args['search'], '*' );
57 $args['search'] = '*' . $args['search'] . '*';
60 if ( $role === 'super' ) {
61 $logins = implode( "', '", get_super_admins() );
62 $args['include'] = $wpdb->get_col( "SELECT ID FROM $wpdb->users WHERE user_login IN ('$logins')" );
66 * If the network is large and a search is not being performed,
67 * show only the latest users with no paging in order to avoid
68 * expensive count queries.
70 if ( !$usersearch && wp_is_large_network( 'users' ) ) {
71 if ( !isset($_REQUEST['orderby']) )
72 $_GET['orderby'] = $_REQUEST['orderby'] = 'id';
73 if ( !isset($_REQUEST['order']) )
74 $_GET['order'] = $_REQUEST['order'] = 'DESC';
75 $args['count_total'] = false;
78 if ( isset( $_REQUEST['orderby'] ) )
79 $args['orderby'] = $_REQUEST['orderby'];
81 if ( isset( $_REQUEST['order'] ) )
82 $args['order'] = $_REQUEST['order'];
84 if ( ! empty( $_REQUEST['mode'] ) ) {
85 $mode = $_REQUEST['mode'] === 'excerpt' ? 'excerpt' : 'list';
86 set_user_setting( 'network_users_list_mode', $mode );
88 $mode = get_user_setting( 'network_users_list_mode', 'list' );
91 /** This filter is documented in wp-admin/includes/class-wp-users-list-table.php */
92 $args = apply_filters( 'users_list_table_query_args', $args );
94 // Query the user IDs for this page
95 $wp_user_search = new WP_User_Query( $args );
97 $this->items = $wp_user_search->get_results();
99 $this->set_pagination_args( array(
100 'total_items' => $wp_user_search->get_total(),
101 'per_page' => $users_per_page,
109 protected function get_bulk_actions() {
111 if ( current_user_can( 'delete_users' ) )
112 $actions['delete'] = __( 'Delete' );
113 $actions['spam'] = _x( 'Mark as Spam', 'user' );
114 $actions['notspam'] = _x( 'Not Spam', 'user' );
122 public function no_items() {
123 _e( 'No users found.' );
128 * @global string $role
131 protected function get_views() {
134 $total_users = get_user_count();
135 $super_admins = get_super_admins();
136 $total_admins = count( $super_admins );
138 $class = $role != 'super' ? ' class="current"' : '';
139 $role_links = array();
140 $role_links['all'] = "<a href='" . network_admin_url('users.php') . "'$class>" . sprintf( _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $total_users, 'users' ), number_format_i18n( $total_users ) ) . '</a>';
141 $class = $role === 'super' ? ' class="current"' : '';
142 $role_links['super'] = "<a href='" . network_admin_url('users.php?role=super') . "'$class>" . sprintf( _n( 'Super Admin <span class="count">(%s)</span>', 'Super Admins <span class="count">(%s)</span>', $total_admins ), number_format_i18n( $total_admins ) ) . '</a>';
148 * @global string $mode
149 * @param string $which
151 protected function pagination( $which ) {
154 parent::pagination ( $which );
156 if ( 'top' === $which ) {
157 $this->view_switcher( $mode );
165 public function get_columns() {
166 $users_columns = array(
167 'cb' => '<input type="checkbox" />',
168 'username' => __( 'Username' ),
169 'name' => __( 'Name' ),
170 'email' => __( 'Email' ),
171 'registered' => _x( 'Registered', 'user' ),
172 'blogs' => __( 'Sites' )
175 * Filters the columns displayed in the Network Admin Users list table.
179 * @param array $users_columns An array of user columns. Default 'cb', 'username',
180 * 'name', 'email', 'registered', 'blogs'.
182 return apply_filters( 'wpmu_users_columns', $users_columns );
189 protected function get_sortable_columns() {
191 'username' => 'login',
194 'registered' => 'id',
199 * Handles the checkbox column output.
204 * @param WP_User $user The current WP_User object.
206 public function column_cb( $user ) {
207 if ( is_super_admin( $user->ID ) ) {
211 <label class="screen-reader-text" for="blog_<?php echo $user->ID; ?>"><?php echo sprintf( __( 'Select %s' ), $user->user_login ); ?></label>
212 <input type="checkbox" id="blog_<?php echo $user->ID ?>" name="allusers[]" value="<?php echo esc_attr( $user->ID ) ?>" />
217 * Handles the ID column output.
222 * @param WP_User $user The current WP_User object.
224 public function column_id( $user ) {
229 * Handles the username column output.
234 * @param WP_User $user The current WP_User object.
236 public function column_username( $user ) {
237 $super_admins = get_super_admins();
238 $avatar = get_avatar( $user->user_email, 32 );
239 $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user->ID ) ) );
243 ?><strong><a href="<?php echo $edit_link; ?>" class="edit"><?php echo $user->user_login; ?></a><?php
244 if ( in_array( $user->user_login, $super_admins ) ) {
245 echo ' - ' . __( 'Super Admin' );
252 * Handles the name column output.
257 * @param WP_User $user The current WP_User object.
259 public function column_name( $user ) {
260 echo "$user->first_name $user->last_name";
264 * Handles the email column output.
269 * @param WP_User $user The current WP_User object.
271 public function column_email( $user ) {
272 echo "<a href='" . esc_url( "mailto:$user->user_email" ) . "'>$user->user_email</a>";
276 * Handles the registered date column output.
281 * @global string $mode
283 * @param WP_User $user The current WP_User object.
285 public function column_registered( $user ) {
287 if ( 'list' === $mode ) {
288 $date = __( 'Y/m/d' );
290 $date = __( 'Y/m/d g:i:s a' );
292 echo mysql2date( $date, $user->user_registered );
299 * @param WP_User $user
300 * @param string $classes
301 * @param string $data
302 * @param string $primary
304 protected function _column_blogs( $user, $classes, $data, $primary ) {
305 echo '<td class="', $classes, ' has-row-actions" ', $data, '>';
306 echo $this->column_blogs( $user );
307 echo $this->handle_row_actions( $user, 'blogs', $primary );
312 * Handles the sites column output.
317 * @param WP_User $user The current WP_User object.
319 public function column_blogs( $user ) {
320 $blogs = get_blogs_of_user( $user->ID, true );
321 if ( ! is_array( $blogs ) ) {
325 foreach ( $blogs as $val ) {
326 if ( ! can_edit_network( $val->site_id ) ) {
330 $path = ( $val->path === '/' ) ? '' : $val->path;
331 echo '<span class="site-' . $val->site_id . '" >';
332 echo '<a href="'. esc_url( network_admin_url( 'site-info.php?id=' . $val->userblog_id ) ) .'">' . str_replace( '.' . get_network()->domain, '', $val->domain . $path ) . '</a>';
333 echo ' <small class="row-actions">';
335 $actions['edit'] = '<a href="'. esc_url( network_admin_url( 'site-info.php?id=' . $val->userblog_id ) ) .'">' . __( 'Edit' ) . '</a>';
338 if ( $val->spam == 1 ) {
339 $class .= 'site-spammed ';
341 if ( $val->mature == 1 ) {
342 $class .= 'site-mature ';
344 if ( $val->deleted == 1 ) {
345 $class .= 'site-deleted ';
347 if ( $val->archived == 1 ) {
348 $class .= 'site-archived ';
351 $actions['view'] = '<a class="' . $class . '" href="' . esc_url( get_home_url( $val->userblog_id ) ) . '">' . __( 'View' ) . '</a>';
354 * Filters the action links displayed next the sites a user belongs to
355 * in the Network Admin Users list table.
359 * @param array $actions An array of action links to be displayed.
360 * Default 'Edit', 'View'.
361 * @param int $userblog_id The site ID.
363 $actions = apply_filters( 'ms_user_list_site_actions', $actions, $val->userblog_id );
366 $action_count = count( $actions );
367 foreach ( $actions as $action => $link ) {
369 $sep = ( $i == $action_count ) ? '' : ' | ';
370 echo "<span class='$action'>$link$sep</span>";
372 echo '</small></span><br/>';
377 * Handles the default column output.
382 * @param WP_User $user The current WP_User object.
383 * @param string $column_name The current column name.
385 public function column_default( $user, $column_name ) {
386 /** This filter is documented in wp-admin/includes/class-wp-users-list-table.php */
387 echo apply_filters( 'manage_users_custom_column', '', $column_name, $user->ID );
390 public function display_rows() {
391 foreach ( $this->items as $user ) {
394 $status_list = array( 'spam' => 'site-spammed', 'deleted' => 'site-deleted' );
396 foreach ( $status_list as $status => $col ) {
397 if ( $user->$status ) {
403 <tr class="<?php echo trim( $class ); ?>">
404 <?php $this->single_row_columns( $user ); ?>
411 * Gets the name of the default primary column.
416 * @return string Name of the default primary column, in this case, 'username'.
418 protected function get_default_primary_column_name() {
423 * Generates and displays row action links.
428 * @param object $user User being acted upon.
429 * @param string $column_name Current column name.
430 * @param string $primary Primary column name.
431 * @return string Row actions output for users in Multisite.
433 protected function handle_row_actions( $user, $column_name, $primary ) {
434 if ( $primary !== $column_name ) {
438 $super_admins = get_super_admins();
439 $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user->ID ) ) );
442 $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
444 if ( current_user_can( 'delete_user', $user->ID ) && ! in_array( $user->user_login, $super_admins ) ) {
445 $actions['delete'] = '<a href="' . $delete = esc_url( network_admin_url( add_query_arg( '_wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), wp_nonce_url( 'users.php', 'deleteuser' ) . '&action=deleteuser&id=' . $user->ID ) ) ) . '" class="delete">' . __( 'Delete' ) . '</a>';
449 * Filters the action links displayed under each user in the Network Admin Users list table.
453 * @param array $actions An array of action links to be displayed.
454 * Default 'Edit', 'Delete'.
455 * @param WP_User $user WP_User object.
457 $actions = apply_filters( 'ms_user_row_actions', $actions, $user );
458 return $this->row_actions( $actions );