]> scripts.mit.edu Git - autoinstalls/wordpress.git/blob - wp-admin/includes/class-wp-users-list-table.php
Wordpress 4.5.3
[autoinstalls/wordpress.git] / wp-admin / includes / class-wp-users-list-table.php
1 <?php
2 /**
3  * List Table API: WP_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.
12  *
13  * @since 3.1.0
14  * @access private
15  *
16  * @see WP_List_Table
17  */
18 class WP_Users_List_Table extends WP_List_Table {
19
20         /**
21          * Site ID to generate the Users list table for.
22          *
23          * @since 3.1.0
24          * @access public
25          * @var int
26          */
27         public $site_id;
28
29         /**
30          * Whether or not the current Users list table is for Multisite.
31          *
32          * @since 3.1.0
33          * @access public
34          * @var bool
35          */
36         public $is_site_users;
37
38         /**
39          * Constructor.
40          *
41          * @since 3.1.0
42          * @access public
43          *
44          * @see WP_List_Table::__construct() for more information on default arguments.
45          *
46          * @param array $args An associative array of arguments.
47          */
48         public function __construct( $args = array() ) {
49                 parent::__construct( array(
50                         'singular' => 'user',
51                         'plural'   => 'users',
52                         'screen'   => isset( $args['screen'] ) ? $args['screen'] : null,
53                 ) );
54
55                 $this->is_site_users = 'site-users-network' === $this->screen->id;
56
57                 if ( $this->is_site_users )
58                         $this->site_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
59         }
60
61         /**
62          * Check the current user's permissions.
63          *
64          * @since 3.1.0
65          * @access public
66          *
67          * @return bool
68          */
69         public function ajax_user_can() {
70                 if ( $this->is_site_users )
71                         return current_user_can( 'manage_sites' );
72                 else
73                         return current_user_can( 'list_users' );
74         }
75
76         /**
77          * Prepare the users list for display.
78          *
79          * @since 3.1.0
80          * @access public
81          *
82          * @global string $role
83          * @global string $usersearch
84          */
85         public function prepare_items() {
86                 global $role, $usersearch;
87
88                 $usersearch = isset( $_REQUEST['s'] ) ? wp_unslash( trim( $_REQUEST['s'] ) ) : '';
89
90                 $role = isset( $_REQUEST['role'] ) ? $_REQUEST['role'] : '';
91
92                 $per_page = ( $this->is_site_users ) ? 'site_users_network_per_page' : 'users_per_page';
93                 $users_per_page = $this->get_items_per_page( $per_page );
94
95                 $paged = $this->get_pagenum();
96
97                 if ( 'none' === $role ) {
98                         $args = array(
99                                 'number' => $users_per_page,
100                                 'offset' => ( $paged-1 ) * $users_per_page,
101                                 'include' => wp_get_users_with_no_role(),
102                                 'search' => $usersearch,
103                                 'fields' => 'all_with_meta'
104                         );
105                 } else {
106                         $args = array(
107                                 'number' => $users_per_page,
108                                 'offset' => ( $paged-1 ) * $users_per_page,
109                                 'role' => $role,
110                                 'search' => $usersearch,
111                                 'fields' => 'all_with_meta'
112                         );
113                 }
114
115                 if ( '' !== $args['search'] )
116                         $args['search'] = '*' . $args['search'] . '*';
117
118                 if ( $this->is_site_users )
119                         $args['blog_id'] = $this->site_id;
120
121                 if ( isset( $_REQUEST['orderby'] ) )
122                         $args['orderby'] = $_REQUEST['orderby'];
123
124                 if ( isset( $_REQUEST['order'] ) )
125                         $args['order'] = $_REQUEST['order'];
126
127                 /**
128                  * Filter the query arguments used to retrieve users for the current users list table.
129                  *
130                  * @since 4.4.0
131                  *
132                  * @param array $args Arguments passed to WP_User_Query to retrieve items for the current
133                  *                    users list table.
134                  */
135                 $args = apply_filters( 'users_list_table_query_args', $args );
136
137                 // Query the user IDs for this page
138                 $wp_user_search = new WP_User_Query( $args );
139
140                 $this->items = $wp_user_search->get_results();
141
142                 $this->set_pagination_args( array(
143                         'total_items' => $wp_user_search->get_total(),
144                         'per_page' => $users_per_page,
145                 ) );
146         }
147
148         /**
149          * Output 'no users' message.
150          *
151          * @since 3.1.0
152          * @access public
153          */
154         public function no_items() {
155                 _e( 'No users found.' );
156         }
157
158         /**
159          * Return an associative array listing all the views that can be used
160          * with this table.
161          *
162          * Provides a list of roles and user count for that role for easy
163          * filtering of the user table.
164          *
165          * @since  3.1.0
166          * @access protected
167          *
168          * @global string $role
169          *
170          * @return array An array of HTML links, one for each view.
171          */
172         protected function get_views() {
173                 global $role;
174
175                 $wp_roles = wp_roles();
176
177                 if ( $this->is_site_users ) {
178                         $url = 'site-users.php?id=' . $this->site_id;
179                         switch_to_blog( $this->site_id );
180                         $users_of_blog = count_users();
181                         restore_current_blog();
182                 } else {
183                         $url = 'users.php';
184                         $users_of_blog = count_users();
185                 }
186
187                 $total_users = $users_of_blog['total_users'];
188                 $avail_roles =& $users_of_blog['avail_roles'];
189                 unset($users_of_blog);
190
191                 $class = empty($role) ? ' class="current"' : '';
192                 $role_links = array();
193                 $role_links['all'] = "<a href='$url'$class>" . sprintf( _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $total_users, 'users' ), number_format_i18n( $total_users ) ) . '</a>';
194                 foreach ( $wp_roles->get_names() as $this_role => $name ) {
195                         if ( !isset($avail_roles[$this_role]) )
196                                 continue;
197
198                         $class = '';
199
200                         if ( $this_role === $role ) {
201                                 $class = ' class="current"';
202                         }
203
204                         $name = translate_user_role( $name );
205                         /* translators: User role name with count */
206                         $name = sprintf( __('%1$s <span class="count">(%2$s)</span>'), $name, number_format_i18n( $avail_roles[$this_role] ) );
207                         $role_links[$this_role] = "<a href='" . esc_url( add_query_arg( 'role', $this_role, $url ) ) . "'$class>$name</a>";
208                 }
209
210                 if ( ! empty( $avail_roles['none' ] ) ) {
211
212                         $class = '';
213
214                         if ( 'none' === $role ) {
215                                 $class = ' class="current"';
216                         }
217
218                         $name = __( 'No role' );
219                         /* translators: User role name with count */
220                         $name = sprintf( __('%1$s <span class="count">(%2$s)</span>'), $name, number_format_i18n( $avail_roles['none' ] ) );
221                         $role_links['none'] = "<a href='" . esc_url( add_query_arg( 'role', 'none', $url ) ) . "'$class>$name</a>";
222
223                 }
224
225                 return $role_links;
226         }
227
228         /**
229          * Retrieve an associative array of bulk actions available on this table.
230          *
231          * @since  3.1.0
232          * @access protected
233          *
234          * @return array Array of bulk actions.
235          */
236         protected function get_bulk_actions() {
237                 $actions = array();
238
239                 if ( is_multisite() ) {
240                         if ( current_user_can( 'remove_users' ) )
241                                 $actions['remove'] = __( 'Remove' );
242                 } else {
243                         if ( current_user_can( 'delete_users' ) )
244                                 $actions['delete'] = __( 'Delete' );
245                 }
246
247                 return $actions;
248         }
249
250         /**
251          * Output the controls to allow user roles to be changed in bulk.
252          *
253          * @since 3.1.0
254          * @access protected
255          *
256          * @param string $which Whether this is being invoked above ("top")
257          *                      or below the table ("bottom").
258          */
259         protected function extra_tablenav( $which ) {
260                 $id = 'bottom' === $which ? 'new_role2' : 'new_role';
261         ?>
262         <div class="alignleft actions">
263                 <?php if ( current_user_can( 'promote_users' ) && $this->has_items() ) : ?>
264                 <label class="screen-reader-text" for="<?php echo $id ?>"><?php _e( 'Change role to&hellip;' ) ?></label>
265                 <select name="<?php echo $id ?>" id="<?php echo $id ?>">
266                         <option value=""><?php _e( 'Change role to&hellip;' ) ?></option>
267                         <?php wp_dropdown_roles(); ?>
268                 </select>
269         <?php
270                         submit_button( __( 'Change' ), 'button', 'changeit', false );
271                 endif;
272
273                 /**
274                  * Fires just before the closing div containing the bulk role-change controls
275                  * in the Users list table.
276                  *
277                  * @since 3.5.0
278                  */
279                 do_action( 'restrict_manage_users' );
280                 echo '</div>';
281         }
282
283         /**
284          * Capture the bulk action required, and return it.
285          *
286          * Overridden from the base class implementation to capture
287          * the role change drop-down.
288          *
289          * @since  3.1.0
290          * @access public
291          *
292          * @return string The bulk action required.
293          */
294         public function current_action() {
295                 if ( isset( $_REQUEST['changeit'] ) &&
296                         ( ! empty( $_REQUEST['new_role'] ) || ! empty( $_REQUEST['new_role2'] ) ) ) {
297                         return 'promote';
298                 }
299
300                 return parent::current_action();
301         }
302
303         /**
304          * Get a list of columns for the list table.
305          *
306          * @since  3.1.0
307          * @access public
308          *
309          * @return array Array in which the key is the ID of the column,
310          *               and the value is the description.
311          */
312         public function get_columns() {
313                 $c = array(
314                         'cb'       => '<input type="checkbox" />',
315                         'username' => __( 'Username' ),
316                         'name'     => __( 'Name' ),
317                         'email'    => __( 'Email' ),
318                         'role'     => __( 'Role' ),
319                         'posts'    => __( 'Posts' )
320                 );
321
322                 if ( $this->is_site_users )
323                         unset( $c['posts'] );
324
325                 return $c;
326         }
327
328         /**
329          * Get a list of sortable columns for the list table.
330          *
331          * @since 3.1.0
332          * @access protected
333          *
334          * @return array Array of sortable columns.
335          */
336         protected function get_sortable_columns() {
337                 $c = array(
338                         'username' => 'login',
339                         'name'     => 'name',
340                         'email'    => 'email',
341                 );
342
343                 return $c;
344         }
345
346         /**
347          * Generate the list table rows.
348          *
349          * @since 3.1.0
350          * @access public
351          */
352         public function display_rows() {
353                 // Query the post counts for this page
354                 if ( ! $this->is_site_users )
355                         $post_counts = count_many_users_posts( array_keys( $this->items ) );
356
357                 foreach ( $this->items as $userid => $user_object ) {
358                         if ( is_multisite() && empty( $user_object->allcaps ) )
359                                 continue;
360
361                         echo "\n\t" . $this->single_row( $user_object, '', '', isset( $post_counts ) ? $post_counts[ $userid ] : 0 );
362                 }
363         }
364
365         /**
366          * Generate HTML for a single row on the users.php admin panel.
367          *
368          * @since 3.1.0
369          * @since 4.2.0 The `$style` parameter was deprecated.
370          * @since 4.4.0 The `$role` parameter was deprecated.
371          * @access public
372          *
373          * @param object $user_object The current user object.
374          * @param string $style       Deprecated. Not used.
375          * @param string $role        Deprecated. Not used.
376          * @param int    $numposts    Optional. Post count to display for this user. Defaults
377          *                            to zero, as in, a new user has made zero posts.
378          * @return string Output for a single row.
379          */
380         public function single_row( $user_object, $style = '', $role = '', $numposts = 0 ) {
381                 if ( ! ( $user_object instanceof WP_User ) ) {
382                         $user_object = get_userdata( (int) $user_object );
383                 }
384                 $user_object->filter = 'display';
385                 $email = $user_object->user_email;
386
387                 if ( $this->is_site_users )
388                         $url = "site-users.php?id={$this->site_id}&amp;";
389                 else
390                         $url = 'users.php?';
391
392                 $user_roles = $this->get_role_list( $user_object );
393
394                 // Set up the hover actions for this user
395                 $actions = array();
396                 $checkbox = '';
397                 // Check if the user for this row is editable
398                 if ( current_user_can( 'list_users' ) ) {
399                         // Set up the user editing link
400                         $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( wp_unslash( $_SERVER['REQUEST_URI'] ) ), get_edit_user_link( $user_object->ID ) ) );
401
402                         if ( current_user_can( 'edit_user',  $user_object->ID ) ) {
403                                 $edit = "<strong><a href=\"$edit_link\">$user_object->user_login</a></strong><br />";
404                                 $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
405                         } else {
406                                 $edit = "<strong>$user_object->user_login</strong><br />";
407                         }
408
409                         if ( !is_multisite() && get_current_user_id() != $user_object->ID && current_user_can( 'delete_user', $user_object->ID ) )
410                                 $actions['delete'] = "<a class='submitdelete' href='" . wp_nonce_url( "users.php?action=delete&amp;user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Delete' ) . "</a>";
411                         if ( is_multisite() && get_current_user_id() != $user_object->ID && current_user_can( 'remove_user', $user_object->ID ) )
412                                 $actions['remove'] = "<a class='submitdelete' href='" . wp_nonce_url( $url."action=remove&amp;user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Remove' ) . "</a>";
413
414                         /**
415                          * Filter the action links displayed under each user in the Users list table.
416                          *
417                          * @since 2.8.0
418                          *
419                          * @param array   $actions     An array of action links to be displayed.
420                          *                             Default 'Edit', 'Delete' for single site, and
421                          *                             'Edit', 'Remove' for Multisite.
422                          * @param WP_User $user_object WP_User object for the currently-listed user.
423                          */
424                         $actions = apply_filters( 'user_row_actions', $actions, $user_object );
425
426                         // Role classes.
427                         $role_classes = esc_attr( implode( ' ', array_keys( $user_roles ) ) );
428
429                         // Set up the checkbox ( because the user is editable, otherwise it's empty )
430                         $checkbox = '<label class="screen-reader-text" for="user_' . $user_object->ID . '">' . sprintf( __( 'Select %s' ), $user_object->user_login ) . '</label>'
431                                                 . "<input type='checkbox' name='users[]' id='user_{$user_object->ID}' class='{$role_classes}' value='{$user_object->ID}' />";
432
433                 } else {
434                         $edit = '<strong>' . $user_object->user_login . '</strong>';
435                 }
436                 $avatar = get_avatar( $user_object->ID, 32 );
437
438                 // Comma-separated list of user roles.
439                 $roles_list = implode( ', ', $user_roles );
440
441                 $r = "<tr id='user-$user_object->ID'>";
442
443                 list( $columns, $hidden, $sortable, $primary ) = $this->get_column_info();
444
445                 foreach ( $columns as $column_name => $column_display_name ) {
446                         $classes = "$column_name column-$column_name";
447                         if ( $primary === $column_name ) {
448                                 $classes .= ' has-row-actions column-primary';
449                         }
450                         if ( 'posts' === $column_name ) {
451                                 $classes .= ' num'; // Special case for that column
452                         }
453
454                         if ( in_array( $column_name, $hidden ) ) {
455                                 $classes .= ' hidden';
456                         }
457
458                         $data = 'data-colname="' . wp_strip_all_tags( $column_display_name ) . '"';
459
460                         $attributes = "class='$classes' $data";
461
462                         if ( 'cb' === $column_name ) {
463                                 $r .= "<th scope='row' class='check-column'>$checkbox</th>";
464                         } else {
465                                 $r .= "<td $attributes>";
466                                 switch ( $column_name ) {
467                                         case 'username':
468                                                 $r .= "$avatar $edit";
469                                                 break;
470                                         case 'name':
471                                                 $r .= "$user_object->first_name $user_object->last_name";
472                                                 break;
473                                         case 'email':
474                                                 $r .= "<a href='" . esc_url( "mailto:$email" ) . "'>$email</a>";
475                                                 break;
476                                         case 'role':
477                                                 $r .= esc_html( $roles_list );
478                                                 break;
479                                         case 'posts':
480                                                 if ( $numposts > 0 ) {
481                                                         $r .= "<a href='edit.php?author=$user_object->ID' class='edit'>";
482                                                         $r .= '<span aria-hidden="true">' . $numposts . '</span>';
483                                                         $r .= '<span class="screen-reader-text">' . sprintf( _n( '%s post by this author', '%s posts by this author', $numposts ), number_format_i18n( $numposts ) ) . '</span>';
484                                                         $r .= '</a>';
485                                                 } else {
486                                                         $r .= 0;
487                                                 }
488                                                 break;
489                                         default:
490                                                 /**
491                                                  * Filter the display output of custom columns in the Users list table.
492                                                  *
493                                                  * @since 2.8.0
494                                                  *
495                                                  * @param string $output      Custom column output. Default empty.
496                                                  * @param string $column_name Column name.
497                                                  * @param int    $user_id     ID of the currently-listed user.
498                                                  */
499                                                 $r .= apply_filters( 'manage_users_custom_column', '', $column_name, $user_object->ID );
500                                 }
501
502                                 if ( $primary === $column_name ) {
503                                         $r .= $this->row_actions( $actions );
504                                 }
505                                 $r .= "</td>";
506                         }
507                 }
508                 $r .= '</tr>';
509
510                 return $r;
511         }
512
513         /**
514          * Gets the name of the default primary column.
515          *
516          * @since 4.3.0
517          * @access protected
518          *
519          * @return string Name of the default primary column, in this case, 'username'.
520          */
521         protected function get_default_primary_column_name() {
522                 return 'username';
523         }
524
525         /**
526          * Returns an array of user roles for a given user object.
527          *
528          * @since 4.4.0
529          * @access protected
530          *
531          * @param WP_User $user_object The WP_User object.
532          * @return array An array of user roles.
533          */
534         protected function get_role_list( $user_object ) {
535                 $wp_roles = wp_roles();
536
537                 $role_list = array();
538
539                 foreach ( $user_object->roles as $role ) {
540                         if ( isset( $wp_roles->role_names[ $role ] ) ) {
541                                 $role_list[ $role ] = translate_user_role( $wp_roles->role_names[ $role ] );
542                         }
543                 }
544
545                 if ( empty( $role_list ) ) {
546                         $role_list['none'] = _x( 'None', 'no user roles' );
547                 }
548
549                 /**
550                  * Filter the returned array of roles for a user.
551                  *
552                  * @since 4.4.0
553                  *
554                  * @param array   $role_list   An array of user roles.
555                  * @param WP_User $user_object A WP_User object.
556                  */
557                 return apply_filters( 'get_role_list', $role_list, $user_object );
558         }
559
560 }