/wp-admin/includes/class-wp-ms-users-list-table.php
PHP | 460 lines | 224 code | 56 blank | 180 comment | 34 complexity | 2d61f76f0b6d9582514a821aa3987662 MD5 | raw file
1<?php
2/**
3 * List Table API: WP_MS_Users_List_Table class
4 *
5 * @package WordPress
6 * @subpackage Administration
7 * @since 3.1.0
8 */
9
10/**
11 * Core class used to implement displaying users in a list table for the network admin.
12 *
13 * @since 3.1.0
14 * @access private
15 *
16 * @see WP_List_Table
17 */
18class WP_MS_Users_List_Table extends WP_List_Table {
19 /**
20 *
21 * @return bool
22 */
23 public function ajax_user_can() {
24 return current_user_can( 'manage_network_users' );
25 }
26
27 /**
28 *
29 * @global string $usersearch
30 * @global string $role
31 * @global wpdb $wpdb
32 * @global string $mode
33 */
34 public function prepare_items() {
35 global $usersearch, $role, $wpdb, $mode;
36
37 $usersearch = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST['s'] ) ) : '';
38
39 $users_per_page = $this->get_items_per_page( 'users_network_per_page' );
40
41 $role = isset( $_REQUEST['role'] ) ? $_REQUEST['role'] : '';
42
43 $paged = $this->get_pagenum();
44
45 $args = array(
46 'number' => $users_per_page,
47 'offset' => ( $paged-1 ) * $users_per_page,
48 'search' => $usersearch,
49 'blog_id' => 0,
50 'fields' => 'all_with_meta'
51 );
52
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'] . '*';
58 }
59
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')" );
63 }
64
65 /*
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.
69 */
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;
76 }
77
78 if ( isset( $_REQUEST['orderby'] ) )
79 $args['orderby'] = $_REQUEST['orderby'];
80
81 if ( isset( $_REQUEST['order'] ) )
82 $args['order'] = $_REQUEST['order'];
83
84 if ( ! empty( $_REQUEST['mode'] ) ) {
85 $mode = $_REQUEST['mode'] === 'excerpt' ? 'excerpt' : 'list';
86 set_user_setting( 'network_users_list_mode', $mode );
87 } else {
88 $mode = get_user_setting( 'network_users_list_mode', 'list' );
89 }
90
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 );
93
94 // Query the user IDs for this page
95 $wp_user_search = new WP_User_Query( $args );
96
97 $this->items = $wp_user_search->get_results();
98
99 $this->set_pagination_args( array(
100 'total_items' => $wp_user_search->get_total(),
101 'per_page' => $users_per_page,
102 ) );
103 }
104
105 /**
106 *
107 * @return array
108 */
109 protected function get_bulk_actions() {
110 $actions = array();
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' );
115
116 return $actions;
117 }
118
119 /**
120 * @access public
121 */
122 public function no_items() {
123 _e( 'No users found.' );
124 }
125
126 /**
127 *
128 * @global string $role
129 * @return array
130 */
131 protected function get_views() {
132 global $role;
133
134 $total_users = get_user_count();
135 $super_admins = get_super_admins();
136 $total_admins = count( $super_admins );
137
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>';
143
144 return $role_links;
145 }
146
147 /**
148 * @global string $mode
149 * @param string $which
150 */
151 protected function pagination( $which ) {
152 global $mode;
153
154 parent::pagination ( $which );
155
156 if ( 'top' === $which ) {
157 $this->view_switcher( $mode );
158 }
159 }
160
161 /**
162 *
163 * @return array
164 */
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' )
173 );
174 /**
175 * Filter the columns displayed in the Network Admin Users list table.
176 *
177 * @since MU
178 *
179 * @param array $users_columns An array of user columns. Default 'cb', 'username',
180 * 'name', 'email', 'registered', 'blogs'.
181 */
182 return apply_filters( 'wpmu_users_columns', $users_columns );
183 }
184
185 /**
186 *
187 * @return array
188 */
189 protected function get_sortable_columns() {
190 return array(
191 'username' => 'login',
192 'name' => 'name',
193 'email' => 'email',
194 'registered' => 'id',
195 );
196 }
197
198 /**
199 * Handles the checkbox column output.
200 *
201 * @since 4.3.0
202 * @access public
203 *
204 * @param WP_User $user The current WP_User object.
205 */
206 public function column_cb( $user ) {
207 if ( is_super_admin( $user->ID ) ) {
208 return;
209 }
210 ?>
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 ) ?>" />
213 <?php
214 }
215
216 /**
217 * Handles the ID column output.
218 *
219 * @since 4.4.0
220 * @access public
221 *
222 * @param WP_User $user The current WP_User object.
223 */
224 public function column_id( $user ) {
225 echo $user->ID;
226 }
227
228 /**
229 * Handles the username column output.
230 *
231 * @since 4.3.0
232 * @access public
233 *
234 * @param WP_User $user The current WP_User object.
235 */
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 ) ) );
240
241 echo $avatar;
242
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' );
246 }
247 ?></strong>
248 <?php
249 }
250
251 /**
252 * Handles the name column output.
253 *
254 * @since 4.3.0
255 * @access public
256 *
257 * @param WP_User $user The current WP_User object.
258 */
259 public function column_name( $user ) {
260 echo "$user->first_name $user->last_name";
261 }
262
263 /**
264 * Handles the email column output.
265 *
266 * @since 4.3.0
267 * @access public
268 *
269 * @param WP_User $user The current WP_User object.
270 */
271 public function column_email( $user ) {
272 echo "<a href='" . esc_url( "mailto:$user->user_email" ) . "'>$user->user_email</a>";
273 }
274
275 /**
276 * Handles the registered date column output.
277 *
278 * @since 4.3.0
279 * @access public
280 *
281 * @global string $mode
282 *
283 * @param WP_User $user The current WP_User object.
284 */
285 public function column_registered( $user ) {
286 global $mode;
287 if ( 'list' === $mode ) {
288 $date = __( 'Y/m/d' );
289 } else {
290 $date = __( 'Y/m/d g:i:s a' );
291 }
292 echo mysql2date( $date, $user->user_registered );
293 }
294
295 /**
296 * @since 4.3.0
297 * @access protected
298 *
299 * @param WP_User $user
300 * @param string $classes
301 * @param string $data
302 * @param string $primary
303 */
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 );
308 echo '</td>';
309 }
310
311 /**
312 * Handles the sites column output.
313 *
314 * @since 4.3.0
315 * @access public
316 *
317 * @param WP_User $user The current WP_User object.
318 */
319 public function column_blogs( $user ) {
320 $blogs = get_blogs_of_user( $user->ID, true );
321 if ( ! is_array( $blogs ) ) {
322 return;
323 }
324
325 foreach ( $blogs as $val ) {
326 if ( ! can_edit_network( $val->site_id ) ) {
327 continue;
328 }
329
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_current_site()->domain, '', $val->domain . $path ) . '</a>';
333 echo ' <small class="row-actions">';
334 $actions = array();
335 $actions['edit'] = '<a href="'. esc_url( network_admin_url( 'site-info.php?id=' . $val->userblog_id ) ) .'">' . __( 'Edit' ) . '</a>';
336
337 $class = '';
338 if ( $val->spam == 1 ) {
339 $class .= 'site-spammed ';
340 }
341 if ( $val->mature == 1 ) {
342 $class .= 'site-mature ';
343 }
344 if ( $val->deleted == 1 ) {
345 $class .= 'site-deleted ';
346 }
347 if ( $val->archived == 1 ) {
348 $class .= 'site-archived ';
349 }
350
351 $actions['view'] = '<a class="' . $class . '" href="' . esc_url( get_home_url( $val->userblog_id ) ) . '">' . __( 'View' ) . '</a>';
352
353 /**
354 * Filter the action links displayed next the sites a user belongs to
355 * in the Network Admin Users list table.
356 *
357 * @since 3.1.0
358 *
359 * @param array $actions An array of action links to be displayed.
360 * Default 'Edit', 'View'.
361 * @param int $userblog_id The site ID.
362 */
363 $actions = apply_filters( 'ms_user_list_site_actions', $actions, $val->userblog_id );
364
365 $i=0;
366 $action_count = count( $actions );
367 foreach ( $actions as $action => $link ) {
368 ++$i;
369 $sep = ( $i == $action_count ) ? '' : ' | ';
370 echo "<span class='$action'>$link$sep</span>";
371 }
372 echo '</small></span><br/>';
373 }
374 }
375
376 /**
377 * Handles the default column output.
378 *
379 * @since 4.3.0
380 * @access public
381 *
382 * @param WP_User $user The current WP_User object.
383 * @param string $column_name The current column name.
384 */
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 );
388 }
389
390 public function display_rows() {
391 foreach ( $this->items as $user ) {
392 $class = '';
393
394 $status_list = array( 'spam' => 'site-spammed', 'deleted' => 'site-deleted' );
395
396 foreach ( $status_list as $status => $col ) {
397 if ( $user->$status ) {
398 $class .= " $col";
399 }
400 }
401
402 ?>
403 <tr class="<?php echo trim( $class ); ?>">
404 <?php $this->single_row_columns( $user ); ?>
405 </tr>
406 <?php
407 }
408 }
409
410 /**
411 * Gets the name of the default primary column.
412 *
413 * @since 4.3.0
414 * @access protected
415 *
416 * @return string Name of the default primary column, in this case, 'username'.
417 */
418 protected function get_default_primary_column_name() {
419 return 'username';
420 }
421
422 /**
423 * Generates and displays row action links.
424 *
425 * @since 4.3.0
426 * @access protected
427 *
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.
432 */
433 protected function handle_row_actions( $user, $column_name, $primary ) {
434 if ( $primary !== $column_name ) {
435 return '';
436 }
437
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 ) ) );
440
441 $actions = array();
442 $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
443
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>';
446 }
447
448 /**
449 * Filter the action links displayed under each user in the Network Admin Users list table.
450 *
451 * @since 3.2.0
452 *
453 * @param array $actions An array of action links to be displayed.
454 * Default 'Edit', 'Delete'.
455 * @param WP_User $user WP_User object.
456 */
457 $actions = apply_filters( 'ms_user_row_actions', $actions, $user );
458 return $this->row_actions( $actions );
459 }
460}