3 * Multisite Users List Table class.
6 * @subpackage List_Table
10 class WP_MS_Users_List_Table extends WP_List_Table {
15 public function ajax_user_can() {
16 return current_user_can( 'manage_network_users' );
21 * @global string $usersearch
22 * @global string $role
24 * @global string $mode
26 public function prepare_items() {
27 global $usersearch, $role, $wpdb, $mode;
29 $usersearch = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST['s'] ) ) : '';
31 $users_per_page = $this->get_items_per_page( 'users_network_per_page' );
33 $role = isset( $_REQUEST['role'] ) ? $_REQUEST['role'] : '';
35 $paged = $this->get_pagenum();
38 'number' => $users_per_page,
39 'offset' => ( $paged-1 ) * $users_per_page,
40 'search' => $usersearch,
42 'fields' => 'all_with_meta'
45 if ( wp_is_large_network( 'users' ) )
46 $args['search'] = ltrim( $args['search'], '*' );
48 if ( $role == 'super' ) {
49 $logins = implode( "', '", get_super_admins() );
50 $args['include'] = $wpdb->get_col( "SELECT ID FROM $wpdb->users WHERE user_login IN ('$logins')" );
54 * If the network is large and a search is not being performed,
55 * show only the latest users with no paging in order to avoid
56 * expensive count queries.
58 if ( !$usersearch && wp_is_large_network( 'users' ) ) {
59 if ( !isset($_REQUEST['orderby']) )
60 $_GET['orderby'] = $_REQUEST['orderby'] = 'id';
61 if ( !isset($_REQUEST['order']) )
62 $_GET['order'] = $_REQUEST['order'] = 'DESC';
63 $args['count_total'] = false;
66 if ( isset( $_REQUEST['orderby'] ) )
67 $args['orderby'] = $_REQUEST['orderby'];
69 if ( isset( $_REQUEST['order'] ) )
70 $args['order'] = $_REQUEST['order'];
72 $mode = empty( $_REQUEST['mode'] ) ? 'list' : $_REQUEST['mode'];
74 // Query the user IDs for this page
75 $wp_user_search = new WP_User_Query( $args );
77 $this->items = $wp_user_search->get_results();
79 $this->set_pagination_args( array(
80 'total_items' => $wp_user_search->get_total(),
81 'per_page' => $users_per_page,
89 protected function get_bulk_actions() {
91 if ( current_user_can( 'delete_users' ) )
92 $actions['delete'] = __( 'Delete' );
93 $actions['spam'] = _x( 'Mark as Spam', 'user' );
94 $actions['notspam'] = _x( 'Not Spam', 'user' );
102 public function no_items() {
103 _e( 'No users found.' );
108 * @global string $role
111 protected function get_views() {
114 $total_users = get_user_count();
115 $super_admins = get_super_admins();
116 $total_admins = count( $super_admins );
118 $class = $role != 'super' ? ' class="current"' : '';
119 $role_links = array();
120 $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>';
121 $class = $role == 'super' ? ' class="current"' : '';
122 $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>';
128 * @global string $mode
129 * @param string $which
131 protected function pagination( $which ) {
134 parent::pagination ( $which );
136 if ( 'top' == $which )
137 $this->view_switcher( $mode );
144 public function get_columns() {
145 $users_columns = array(
146 'cb' => '<input type="checkbox" />',
147 'username' => __( 'Username' ),
148 'name' => __( 'Name' ),
149 'email' => __( 'E-mail' ),
150 'registered' => _x( 'Registered', 'user' ),
151 'blogs' => __( 'Sites' )
154 * Filter the columns displayed in the Network Admin Users list table.
158 * @param array $users_columns An array of user columns. Default 'cb', 'username',
159 * 'name', 'email', 'registered', 'blogs'.
161 return apply_filters( 'wpmu_users_columns', $users_columns );
168 protected function get_sortable_columns() {
170 'username' => 'login',
173 'registered' => 'id',
178 * Handles the checkbox column output.
183 * @param WP_User $user The current WP_User object.
185 public function column_cb( $user ) {
187 <label class="screen-reader-text" for="blog_<?php echo $user->ID; ?>"><?php echo sprintf( __( 'Select %s' ), $user->user_login ); ?></label>
188 <input type="checkbox" id="blog_<?php echo $user->ID ?>" name="allusers[]" value="<?php echo esc_attr( $user->ID ) ?>" />
193 * Handles the username column output.
198 * @param WP_User $user The current WP_User object.
200 public function column_username( $user ) {
201 $super_admins = get_super_admins();
202 $avatar = get_avatar( $user->user_email, 32 );
203 $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user->ID ) ) );
207 ?><strong><a href="<?php echo $edit_link; ?>" class="edit"><?php echo $user->user_login; ?></a><?php
208 if ( in_array( $user->user_login, $super_admins ) ) {
209 echo ' - ' . __( 'Super Admin' );
216 * Handles the name column output.
221 * @param WP_User $user The current WP_User object.
223 public function column_name( $user ) {
224 echo "$user->first_name $user->last_name";
228 * Handles the email column output.
233 * @param WP_User $user The current WP_User object.
235 public function column_email( $user ) {
236 echo "<a href='" . esc_url( "mailto:$user->user_email" ) . "'>$user->user_email</a>";
240 * Handles the registered date column output.
245 * @global string $mode
247 * @param WP_User $user The current WP_User object.
249 public function column_registered( $user ) {
251 if ( 'list' == $mode ) {
252 $date = __( 'Y/m/d' );
254 $date = __( 'Y/m/d g:i:s a' );
256 echo mysql2date( $date, $user->user_registered );
263 * @param WP_User $user
264 * @param string $classes
265 * @param string $data
266 * @param string $primary
268 protected function _column_blogs( $user, $classes, $data, $primary ) {
269 echo '<td class="', $classes, ' has-row-actions" ', $data, '>';
270 echo $this->column_blogs( $user );
271 echo $this->handle_row_actions( $user, 'blogs', $primary );
276 * Handles the blogs/sites column output.
281 * @param WP_User $user The current WP_User object.
283 public function column_blogs( $user ) {
284 $blogs = get_blogs_of_user( $user->ID, true );
285 if ( ! is_array( $blogs ) ) {
289 foreach ( $blogs as $val ) {
290 if ( ! can_edit_network( $val->site_id ) ) {
294 $path = ( $val->path == '/' ) ? '' : $val->path;
295 echo '<span class="site-' . $val->site_id . '" >';
296 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>';
297 echo ' <small class="row-actions">';
299 $actions['edit'] = '<a href="'. esc_url( network_admin_url( 'site-info.php?id=' . $val->userblog_id ) ) .'">' . __( 'Edit' ) . '</a>';
302 if ( $val->spam == 1 ) {
303 $class .= 'site-spammed ';
305 if ( $val->mature == 1 ) {
306 $class .= 'site-mature ';
308 if ( $val->deleted == 1 ) {
309 $class .= 'site-deleted ';
311 if ( $val->archived == 1 ) {
312 $class .= 'site-archived ';
315 $actions['view'] = '<a class="' . $class . '" href="' . esc_url( get_home_url( $val->userblog_id ) ) . '">' . __( 'View' ) . '</a>';
318 * Filter the action links displayed next the sites a user belongs to
319 * in the Network Admin Users list table.
323 * @param array $actions An array of action links to be displayed.
324 * Default 'Edit', 'View'.
325 * @param int $userblog_id The site ID.
327 $actions = apply_filters( 'ms_user_list_site_actions', $actions, $val->userblog_id );
330 $action_count = count( $actions );
331 foreach ( $actions as $action => $link ) {
333 $sep = ( $i == $action_count ) ? '' : ' | ';
334 echo "<span class='$action'>$link$sep</span>";
336 echo '</small></span><br/>';
341 * Handles the default column output.
346 * @param WP_User $user The current WP_User object.
347 * @param string $column_name The current column name.
349 public function column_default( $user, $column_name ) {
350 /** This filter is documented in wp-admin/includes/class-wp-users-list-table.php */
351 echo apply_filters( 'manage_users_custom_column', '', $column_name, $user->ID );
354 public function display_rows() {
355 foreach ( $this->items as $user ) {
358 $status_list = array( 'spam' => 'site-spammed', 'deleted' => 'site-deleted' );
360 foreach ( $status_list as $status => $col ) {
361 if ( $user->$status ) {
367 <tr class="<?php echo trim( $class ); ?>">
368 <?php $this->single_row_columns( $user ); ?>
375 * Gets the name of the default primary column.
380 * @return string Name of the default primary column, in this case, 'username'.
382 protected function get_default_primary_column_name() {
387 * Generates and displays row action links.
392 * @param object $user User being acted upon.
393 * @param string $column_name Current column name.
394 * @param string $primary Primary column name.
395 * @return string Row actions output for users in Multisite.
397 protected function handle_row_actions( $user, $column_name, $primary ) {
398 if ( $primary !== $column_name ) {
402 $super_admins = get_super_admins();
403 $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user->ID ) ) );
406 $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
408 if ( current_user_can( 'delete_user', $user->ID ) && ! in_array( $user->user_login, $super_admins ) ) {
409 $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>';
413 * Filter the action links displayed under each user in the Network Admin Users list table.
417 * @param array $actions An array of action links to be displayed.
418 * Default 'Edit', 'Delete'.
419 * @param WP_User $user WP_User object.
421 $actions = apply_filters( 'ms_user_row_actions', $actions, $user );
422 return $this->row_actions( $actions );