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 $mode = empty( $_REQUEST['mode'] ) ? 'list' : $_REQUEST['mode'];
86 /** This filter is documented in wp-admin/includes/class-wp-users-list-table.php */
87 $args = apply_filters( 'users_list_table_query_args', $args );
89 // Query the user IDs for this page
90 $wp_user_search = new WP_User_Query( $args );
92 $this->items = $wp_user_search->get_results();
94 $this->set_pagination_args( array(
95 'total_items' => $wp_user_search->get_total(),
96 'per_page' => $users_per_page,
104 protected function get_bulk_actions() {
106 if ( current_user_can( 'delete_users' ) )
107 $actions['delete'] = __( 'Delete' );
108 $actions['spam'] = _x( 'Mark as Spam', 'user' );
109 $actions['notspam'] = _x( 'Not Spam', 'user' );
117 public function no_items() {
118 _e( 'No users found.' );
123 * @global string $role
126 protected function get_views() {
129 $total_users = get_user_count();
130 $super_admins = get_super_admins();
131 $total_admins = count( $super_admins );
133 $class = $role != 'super' ? ' class="current"' : '';
134 $role_links = array();
135 $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>';
136 $class = $role === 'super' ? ' class="current"' : '';
137 $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>';
143 * @global string $mode
144 * @param string $which
146 protected function pagination( $which ) {
149 parent::pagination ( $which );
151 if ( 'top' === $which ) {
152 $this->view_switcher( $mode );
160 public function get_columns() {
161 $users_columns = array(
162 'cb' => '<input type="checkbox" />',
163 'username' => __( 'Username' ),
164 'name' => __( 'Name' ),
165 'email' => __( 'Email' ),
166 'registered' => _x( 'Registered', 'user' ),
167 'blogs' => __( 'Sites' )
170 * Filter the columns displayed in the Network Admin Users list table.
174 * @param array $users_columns An array of user columns. Default 'cb', 'username',
175 * 'name', 'email', 'registered', 'blogs'.
177 return apply_filters( 'wpmu_users_columns', $users_columns );
184 protected function get_sortable_columns() {
186 'username' => 'login',
189 'registered' => 'id',
194 * Handles the checkbox column output.
199 * @param WP_User $user The current WP_User object.
201 public function column_cb( $user ) {
202 if ( is_super_admin( $user->ID ) ) {
206 <label class="screen-reader-text" for="blog_<?php echo $user->ID; ?>"><?php echo sprintf( __( 'Select %s' ), $user->user_login ); ?></label>
207 <input type="checkbox" id="blog_<?php echo $user->ID ?>" name="allusers[]" value="<?php echo esc_attr( $user->ID ) ?>" />
212 * Handles the ID column output.
217 * @param WP_User $user The current WP_User object.
219 public function column_id( $user ) {
224 * Handles the username column output.
229 * @param WP_User $user The current WP_User object.
231 public function column_username( $user ) {
232 $super_admins = get_super_admins();
233 $avatar = get_avatar( $user->user_email, 32 );
234 $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user->ID ) ) );
238 ?><strong><a href="<?php echo $edit_link; ?>" class="edit"><?php echo $user->user_login; ?></a><?php
239 if ( in_array( $user->user_login, $super_admins ) ) {
240 echo ' - ' . __( 'Super Admin' );
247 * Handles the name column output.
252 * @param WP_User $user The current WP_User object.
254 public function column_name( $user ) {
255 echo "$user->first_name $user->last_name";
259 * Handles the email column output.
264 * @param WP_User $user The current WP_User object.
266 public function column_email( $user ) {
267 echo "<a href='" . esc_url( "mailto:$user->user_email" ) . "'>$user->user_email</a>";
271 * Handles the registered date column output.
276 * @global string $mode
278 * @param WP_User $user The current WP_User object.
280 public function column_registered( $user ) {
282 if ( 'list' === $mode ) {
283 $date = __( 'Y/m/d' );
285 $date = __( 'Y/m/d g:i:s a' );
287 echo mysql2date( $date, $user->user_registered );
294 * @param WP_User $user
295 * @param string $classes
296 * @param string $data
297 * @param string $primary
299 protected function _column_blogs( $user, $classes, $data, $primary ) {
300 echo '<td class="', $classes, ' has-row-actions" ', $data, '>';
301 echo $this->column_blogs( $user );
302 echo $this->handle_row_actions( $user, 'blogs', $primary );
307 * Handles the blogs/sites column output.
312 * @param WP_User $user The current WP_User object.
314 public function column_blogs( $user ) {
315 $blogs = get_blogs_of_user( $user->ID, true );
316 if ( ! is_array( $blogs ) ) {
320 foreach ( $blogs as $val ) {
321 if ( ! can_edit_network( $val->site_id ) ) {
325 $path = ( $val->path === '/' ) ? '' : $val->path;
326 echo '<span class="site-' . $val->site_id . '" >';
327 echo '<a href="'. esc_url( network_admin_url( 'site-info.php?id=' . $val->userblog_id ) ) .'">' . str_replace( '.' . get_current_site()->domain, '', $val->domain . $path ) . '</a>';
328 echo ' <small class="row-actions">';
330 $actions['edit'] = '<a href="'. esc_url( network_admin_url( 'site-info.php?id=' . $val->userblog_id ) ) .'">' . __( 'Edit' ) . '</a>';
333 if ( $val->spam == 1 ) {
334 $class .= 'site-spammed ';
336 if ( $val->mature == 1 ) {
337 $class .= 'site-mature ';
339 if ( $val->deleted == 1 ) {
340 $class .= 'site-deleted ';
342 if ( $val->archived == 1 ) {
343 $class .= 'site-archived ';
346 $actions['view'] = '<a class="' . $class . '" href="' . esc_url( get_home_url( $val->userblog_id ) ) . '">' . __( 'View' ) . '</a>';
349 * Filter the action links displayed next the sites a user belongs to
350 * in the Network Admin Users list table.
354 * @param array $actions An array of action links to be displayed.
355 * Default 'Edit', 'View'.
356 * @param int $userblog_id The site ID.
358 $actions = apply_filters( 'ms_user_list_site_actions', $actions, $val->userblog_id );
361 $action_count = count( $actions );
362 foreach ( $actions as $action => $link ) {
364 $sep = ( $i == $action_count ) ? '' : ' | ';
365 echo "<span class='$action'>$link$sep</span>";
367 echo '</small></span><br/>';
372 * Handles the default column output.
377 * @param WP_User $user The current WP_User object.
378 * @param string $column_name The current column name.
380 public function column_default( $user, $column_name ) {
381 /** This filter is documented in wp-admin/includes/class-wp-users-list-table.php */
382 echo apply_filters( 'manage_users_custom_column', '', $column_name, $user->ID );
385 public function display_rows() {
386 foreach ( $this->items as $user ) {
389 $status_list = array( 'spam' => 'site-spammed', 'deleted' => 'site-deleted' );
391 foreach ( $status_list as $status => $col ) {
392 if ( $user->$status ) {
398 <tr class="<?php echo trim( $class ); ?>">
399 <?php $this->single_row_columns( $user ); ?>
406 * Gets the name of the default primary column.
411 * @return string Name of the default primary column, in this case, 'username'.
413 protected function get_default_primary_column_name() {
418 * Generates and displays row action links.
423 * @param object $user User being acted upon.
424 * @param string $column_name Current column name.
425 * @param string $primary Primary column name.
426 * @return string Row actions output for users in Multisite.
428 protected function handle_row_actions( $user, $column_name, $primary ) {
429 if ( $primary !== $column_name ) {
433 $super_admins = get_super_admins();
434 $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user->ID ) ) );
437 $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
439 if ( current_user_can( 'delete_user', $user->ID ) && ! in_array( $user->user_login, $super_admins ) ) {
440 $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>';
444 * Filter the action links displayed under each user in the Network Admin Users list table.
448 * @param array $actions An array of action links to be displayed.
449 * Default 'Edit', 'Delete'.
450 * @param WP_User $user WP_User object.
452 $actions = apply_filters( 'ms_user_row_actions', $actions, $user );
453 return $this->row_actions( $actions );