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