]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-wp-ms-users-list-table.php
WordPress 4.4.1
[autoinstalls/wordpress.git] / wp-admin / includes / class-wp-ms-users-list-table.php
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  */
18 class 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                 $mode = empty( $_REQUEST['mode'] ) ? 'list' : $_REQUEST['mode'];
85
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 );
88
89                 // Query the user IDs for this page
90                 $wp_user_search = new WP_User_Query( $args );
91
92                 $this->items = $wp_user_search->get_results();
93
94                 $this->set_pagination_args( array(
95                         'total_items' => $wp_user_search->get_total(),
96                         'per_page' => $users_per_page,
97                 ) );
98         }
99
100         /**
101          *
102          * @return array
103          */
104         protected function get_bulk_actions() {
105                 $actions = array();
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' );
110
111                 return $actions;
112         }
113
114         /**
115          * @access public
116          */
117         public function no_items() {
118                 _e( 'No users found.' );
119         }
120
121         /**
122          *
123          * @global string $role
124          * @return array
125          */
126         protected function get_views() {
127                 global $role;
128
129                 $total_users = get_user_count();
130                 $super_admins = get_super_admins();
131                 $total_admins = count( $super_admins );
132
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>';
138
139                 return $role_links;
140         }
141
142         /**
143          * @global string $mode
144          * @param string $which
145          */
146         protected function pagination( $which ) {
147                 global $mode;
148
149                 parent::pagination ( $which );
150
151                 if ( 'top' === $which ) {
152                         $this->view_switcher( $mode );
153                 }
154         }
155
156         /**
157          *
158          * @return array
159          */
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' )
168                 );
169                 /**
170                  * Filter the columns displayed in the Network Admin Users list table.
171                  *
172                  * @since MU
173                  *
174                  * @param array $users_columns An array of user columns. Default 'cb', 'username',
175                  *                             'name', 'email', 'registered', 'blogs'.
176                  */
177                 return apply_filters( 'wpmu_users_columns', $users_columns );
178         }
179
180         /**
181          *
182          * @return array
183          */
184         protected function get_sortable_columns() {
185                 return array(
186                         'username'   => 'login',
187                         'name'       => 'name',
188                         'email'      => 'email',
189                         'registered' => 'id',
190                 );
191         }
192
193         /**
194          * Handles the checkbox column output.
195          *
196          * @since 4.3.0
197          * @access public
198          *
199          * @param WP_User $user The current WP_User object.
200          */
201         public function column_cb( $user ) {
202                 if ( is_super_admin( $user->ID ) ) {
203                         return;
204                 }
205                 ?>
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 ) ?>" />
208                 <?php
209         }
210
211         /**
212          * Handles the ID column output.
213          *
214          * @since 4.4.0
215          * @access public
216          *
217          * @param WP_User $user The current WP_User object.
218          */
219         public function column_id( $user ) {
220                 echo $user->ID;
221         }
222
223         /**
224          * Handles the username column output.
225          *
226          * @since 4.3.0
227          * @access public
228          *
229          * @param WP_User $user The current WP_User object.
230          */
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 ) ) );
235
236                 echo $avatar;
237
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' );
241                 }
242                 ?></strong>
243         <?php
244         }
245
246         /**
247          * Handles the name column output.
248          *
249          * @since 4.3.0
250          * @access public
251          *
252          * @param WP_User $user The current WP_User object.
253          */
254         public function column_name( $user ) {
255                 echo "$user->first_name $user->last_name";
256         }
257
258         /**
259          * Handles the email column output.
260          *
261          * @since 4.3.0
262          * @access public
263          *
264          * @param WP_User $user The current WP_User object.
265          */
266         public function column_email( $user ) {
267                 echo "<a href='" . esc_url( "mailto:$user->user_email" ) . "'>$user->user_email</a>";
268         }
269
270         /**
271          * Handles the registered date column output.
272          *
273          * @since 4.3.0
274          * @access public
275          *
276          * @global string $mode
277          *
278          * @param WP_User $user The current WP_User object.
279          */
280         public function column_registered( $user ) {
281                 global $mode;
282                 if ( 'list' === $mode ) {
283                         $date = __( 'Y/m/d' );
284                 } else {
285                         $date = __( 'Y/m/d g:i:s a' );
286                 }
287                 echo mysql2date( $date, $user->user_registered );
288         }
289
290         /**
291          * @since 4.3.0
292          * @access protected
293          *
294          * @param WP_User $user
295          * @param string  $classes
296          * @param string  $data
297          * @param string  $primary
298          */
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 );
303                 echo '</td>';
304         }
305
306         /**
307          * Handles the blogs/sites column output.
308          *
309          * @since 4.3.0
310          * @access public
311          *
312          * @param WP_User $user The current WP_User object.
313          */
314         public function column_blogs( $user ) {
315                 $blogs = get_blogs_of_user( $user->ID, true );
316                 if ( ! is_array( $blogs ) ) {
317                         return;
318                 }
319
320                 foreach ( $blogs as $val ) {
321                         if ( ! can_edit_network( $val->site_id ) ) {
322                                 continue;
323                         }
324
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">';
329                         $actions = array();
330                         $actions['edit'] = '<a href="'. esc_url( network_admin_url( 'site-info.php?id=' . $val->userblog_id ) ) .'">' . __( 'Edit' ) . '</a>';
331
332                         $class = '';
333                         if ( $val->spam == 1 ) {
334                                 $class .= 'site-spammed ';
335                         }
336                         if ( $val->mature == 1 ) {
337                                 $class .= 'site-mature ';
338                         }
339                         if ( $val->deleted == 1 ) {
340                                 $class .= 'site-deleted ';
341                         }
342                         if ( $val->archived == 1 ) {
343                                 $class .= 'site-archived ';
344                         }
345
346                         $actions['view'] = '<a class="' . $class . '" href="' . esc_url( get_home_url( $val->userblog_id ) ) . '">' . __( 'View' ) . '</a>';
347
348                         /**
349                          * Filter the action links displayed next the sites a user belongs to
350                          * in the Network Admin Users list table.
351                          *
352                          * @since 3.1.0
353                          *
354                          * @param array $actions     An array of action links to be displayed.
355                          *                           Default 'Edit', 'View'.
356                          * @param int   $userblog_id The site ID.
357                          */
358                         $actions = apply_filters( 'ms_user_list_site_actions', $actions, $val->userblog_id );
359
360                         $i=0;
361                         $action_count = count( $actions );
362                         foreach ( $actions as $action => $link ) {
363                                 ++$i;
364                                 $sep = ( $i == $action_count ) ? '' : ' | ';
365                                 echo "<span class='$action'>$link$sep</span>";
366                         }
367                         echo '</small></span><br/>';
368                 }
369         }
370
371         /**
372          * Handles the default column output.
373          *
374          * @since 4.3.0
375          * @access public
376          *
377          * @param WP_User $user       The current WP_User object.
378          * @param string $column_name The current column name.
379          */
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 );
383         }
384
385         public function display_rows() {
386                 foreach ( $this->items as $user ) {
387                         $class = '';
388
389                         $status_list = array( 'spam' => 'site-spammed', 'deleted' => 'site-deleted' );
390
391                         foreach ( $status_list as $status => $col ) {
392                                 if ( $user->$status ) {
393                                         $class .= " $col";
394                                 }
395                         }
396
397                         ?>
398                         <tr class="<?php echo trim( $class ); ?>">
399                                 <?php $this->single_row_columns( $user ); ?>
400                         </tr>
401                         <?php
402                 }
403         }
404
405         /**
406          * Gets the name of the default primary column.
407          *
408          * @since 4.3.0
409          * @access protected
410          *
411          * @return string Name of the default primary column, in this case, 'username'.
412          */
413         protected function get_default_primary_column_name() {
414                 return 'username';
415         }
416
417         /**
418          * Generates and displays row action links.
419          *
420          * @since 4.3.0
421          * @access protected
422          *
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.
427          */
428         protected function handle_row_actions( $user, $column_name, $primary ) {
429                 if ( $primary !== $column_name ) {
430                         return '';
431                 }
432
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 ) ) );
435
436                 $actions = array();
437                 $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
438
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' ) . '&amp;action=deleteuser&amp;id=' . $user->ID ) ) ) . '" class="delete">' . __( 'Delete' ) . '</a>';
441                 }
442
443                 /**
444                  * Filter the action links displayed under each user in the Network Admin Users list table.
445                  *
446                  * @since 3.2.0
447                  *
448                  * @param array   $actions An array of action links to be displayed.
449                  *                         Default 'Edit', 'Delete'.
450                  * @param WP_User $user    WP_User object.
451                  */
452                 $actions = apply_filters( 'ms_user_row_actions', $actions, $user );
453                 return $this->row_actions( $actions );
454         }
455 }