WordPress 3.4
[autoinstalls/wordpress.git] / wp-admin / includes / class-wp-users-list-table.php
1 <?php
2 /**
3  * Users List Table class.
4  *
5  * @package WordPress
6  * @subpackage List_Table
7  * @since 3.1.0
8  * @access private
9  */
10 class WP_Users_List_Table extends WP_List_Table {
11
12         var $site_id;
13         var $is_site_users;
14
15         function __construct() {
16                 $screen = get_current_screen();
17                 $this->is_site_users = 'site-users-network' == $screen->id;
18
19                 if ( $this->is_site_users )
20                         $this->site_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
21
22                 parent::__construct( array(
23                         'singular' => 'user',
24                         'plural'   => 'users'
25                 ) );
26         }
27
28         function ajax_user_can() {
29                 if ( $this->is_site_users )
30                         return current_user_can( 'manage_sites' );
31                 else
32                         return current_user_can( 'list_users' );
33         }
34
35         function prepare_items() {
36                 global $role, $usersearch;
37
38                 $usersearch = isset( $_REQUEST['s'] ) ? $_REQUEST['s'] : '';
39
40                 $role = isset( $_REQUEST['role'] ) ? $_REQUEST['role'] : '';
41
42                 $per_page = ( $this->is_site_users ) ? 'site_users_network_per_page' : 'users_per_page';
43                 $users_per_page = $this->get_items_per_page( $per_page );
44
45                 $paged = $this->get_pagenum();
46
47                 $args = array(
48                         'number' => $users_per_page,
49                         'offset' => ( $paged-1 ) * $users_per_page,
50                         'role' => $role,
51                         'search' => $usersearch,
52                         'fields' => 'all_with_meta'
53                 );
54
55                 if ( '' !== $args['search'] )
56                         $args['search'] = '*' . $args['search'] . '*';
57
58                 if ( $this->is_site_users )
59                         $args['blog_id'] = $this->site_id;
60
61                 if ( isset( $_REQUEST['orderby'] ) )
62                         $args['orderby'] = $_REQUEST['orderby'];
63
64                 if ( isset( $_REQUEST['order'] ) )
65                         $args['order'] = $_REQUEST['order'];
66
67                 // Query the user IDs for this page
68                 $wp_user_search = new WP_User_Query( $args );
69
70                 $this->items = $wp_user_search->get_results();
71
72                 $this->set_pagination_args( array(
73                         'total_items' => $wp_user_search->get_total(),
74                         'per_page' => $users_per_page,
75                 ) );
76         }
77
78         function no_items() {
79                 _e( 'No matching users were found.' );
80         }
81
82         function get_views() {
83                 global $wp_roles, $role;
84
85                 if ( $this->is_site_users ) {
86                         $url = 'site-users.php?id=' . $this->site_id;
87                         switch_to_blog( $this->site_id );
88                         $users_of_blog = count_users();
89                         restore_current_blog();
90                 } else {
91                         $url = 'users.php';
92                         $users_of_blog = count_users();
93                 }
94                 $total_users = $users_of_blog['total_users'];
95                 $avail_roles =& $users_of_blog['avail_roles'];
96                 unset($users_of_blog);
97
98                 $current_role = false;
99                 $class = empty($role) ? ' class="current"' : '';
100                 $role_links = array();
101                 $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>';
102                 foreach ( $wp_roles->get_names() as $this_role => $name ) {
103                         if ( !isset($avail_roles[$this_role]) )
104                                 continue;
105
106                         $class = '';
107
108                         if ( $this_role == $role ) {
109                                 $current_role = $role;
110                                 $class = ' class="current"';
111                         }
112
113                         $name = translate_user_role( $name );
114                         /* translators: User role name with count */
115                         $name = sprintf( __('%1$s <span class="count">(%2$s)</span>'), $name, number_format_i18n( $avail_roles[$this_role] ) );
116                         $role_links[$this_role] = "<a href='" . esc_url( add_query_arg( 'role', $this_role, $url ) ) . "'$class>$name</a>";
117                 }
118
119                 return $role_links;
120         }
121
122         function get_bulk_actions() {
123                 $actions = array();
124
125                 if ( is_multisite() ) {
126                         if ( current_user_can( 'remove_users' ) )
127                                 $actions['remove'] = __( 'Remove' );
128                 } else {
129                         if ( current_user_can( 'delete_users' ) )
130                                 $actions['delete'] = __( 'Delete' );
131                 }
132
133                 return $actions;
134         }
135
136         function extra_tablenav( $which ) {
137                 if ( 'top' != $which )
138                         return;
139                 if ( ! current_user_can( 'promote_users' ) )
140                         return;
141 ?>
142         <div class="alignleft actions">
143                 <label class="screen-reader-text" for="new_role"><?php _e( 'Change role to&hellip;' ) ?></label>
144                 <select name="new_role" id="new_role">
145                         <option value=''><?php _e( 'Change role to&hellip;' ) ?></option>
146                         <?php wp_dropdown_roles(); ?>
147                 </select>
148                 <?php submit_button( __( 'Change' ), 'secondary', 'changeit', false ); ?>
149         </div>
150 <?php
151         }
152
153         function current_action() {
154                 if ( isset($_REQUEST['changeit']) && !empty($_REQUEST['new_role']) )
155                         return 'promote';
156
157                 return parent::current_action();
158         }
159
160         function get_columns() {
161                 $c = array(
162                         'cb'       => '<input type="checkbox" />',
163                         'username' => __( 'Username' ),
164                         'name'     => __( 'Name' ),
165                         'email'    => __( 'E-mail' ),
166                         'role'     => __( 'Role' ),
167                         'posts'    => __( 'Posts' )
168                 );
169
170                 if ( $this->is_site_users )
171                         unset( $c['posts'] );
172
173                 return $c;
174         }
175
176         function get_sortable_columns() {
177                 $c = array(
178                         'username' => 'login',
179                         'name'     => 'name',
180                         'email'    => 'email',
181                 );
182
183                 if ( $this->is_site_users )
184                         unset( $c['posts'] );
185
186                 return $c;
187         }
188
189         function display_rows() {
190                 // Query the post counts for this page
191                 if ( ! $this->is_site_users )
192                         $post_counts = count_many_users_posts( array_keys( $this->items ) );
193
194                 $style = '';
195                 foreach ( $this->items as $userid => $user_object ) {
196                         $role = reset( $user_object->roles );
197
198                         if ( is_multisite() && empty( $role ) )
199                                 continue;
200
201                         $style = ( ' class="alternate"' == $style ) ? '' : ' class="alternate"';
202                         echo "\n\t", $this->single_row( $user_object, $style, $role, isset( $post_counts ) ? $post_counts[ $userid ] : 0 );
203                 }
204         }
205
206         /**
207          * Generate HTML for a single row on the users.php admin panel.
208          *
209          * @since 2.1.0
210          *
211          * @param object $user_object
212          * @param string $style Optional. Attributes added to the TR element. Must be sanitized.
213          * @param string $role Key for the $wp_roles array.
214          * @param int $numposts Optional. Post count to display for this user. Defaults to zero, as in, a new user has made zero posts.
215          * @return string
216          */
217         function single_row( $user_object, $style = '', $role = '', $numposts = 0 ) {
218                 global $wp_roles;
219
220                 if ( !( is_object( $user_object ) && is_a( $user_object, 'WP_User' ) ) )
221                         $user_object = new WP_User( (int) $user_object );
222                 $user_object->filter = 'display';
223                 $email = $user_object->user_email;
224
225                 if ( $this->is_site_users )
226                         $url = "site-users.php?id={$this->site_id}&amp;";
227                 else
228                         $url = 'users.php?';
229
230                 $checkbox = '';
231                 // Check if the user for this row is editable
232                 if ( current_user_can( 'list_users' ) ) {
233                         // Set up the user editing link
234                         // TODO: make profile/user-edit determination a separate function
235                         if ( get_current_user_id() == $user_object->ID ) {
236                                 $edit_link = 'profile.php';
237                         } else {
238                                 $edit_link = esc_url( add_query_arg( 'wp_http_referer', urlencode( stripslashes( $_SERVER['REQUEST_URI'] ) ), "user-edit.php?user_id=$user_object->ID" ) );
239                         }
240
241                         // Set up the hover actions for this user
242                         $actions = array();
243
244                         if ( current_user_can( 'edit_user',  $user_object->ID ) ) {
245                                 $edit = "<strong><a href=\"$edit_link\">$user_object->user_login</a></strong><br />";
246                                 $actions['edit'] = '<a href="' . $edit_link . '">' . __( 'Edit' ) . '</a>';
247                         } else {
248                                 $edit = "<strong>$user_object->user_login</strong><br />";
249                         }
250
251                         if ( !is_multisite() && get_current_user_id() != $user_object->ID && current_user_can( 'delete_user', $user_object->ID ) )
252                                 $actions['delete'] = "<a class='submitdelete' href='" . wp_nonce_url( "users.php?action=delete&amp;user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Delete' ) . "</a>";
253                         if ( is_multisite() && get_current_user_id() != $user_object->ID && current_user_can( 'remove_user', $user_object->ID ) )
254                                 $actions['remove'] = "<a class='submitdelete' href='" . wp_nonce_url( $url."action=remove&amp;user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Remove' ) . "</a>";
255                         $actions = apply_filters( 'user_row_actions', $actions, $user_object );
256                         $edit .= $this->row_actions( $actions );
257
258                         // Set up the checkbox ( because the user is editable, otherwise its empty )
259                         $checkbox = "<input type='checkbox' name='users[]' id='user_{$user_object->ID}' class='$role' value='{$user_object->ID}' />";
260
261                 } else {
262                         $edit = '<strong>' . $user_object->user_login . '</strong>';
263                 }
264                 $role_name = isset( $wp_roles->role_names[$role] ) ? translate_user_role( $wp_roles->role_names[$role] ) : __( 'None' );
265                 $avatar = get_avatar( $user_object->ID, 32 );
266
267                 $r = "<tr id='user-$user_object->ID'$style>";
268
269                 list( $columns, $hidden ) = $this->get_column_info();
270
271                 foreach ( $columns as $column_name => $column_display_name ) {
272                         $class = "class=\"$column_name column-$column_name\"";
273
274                         $style = '';
275                         if ( in_array( $column_name, $hidden ) )
276                                 $style = ' style="display:none;"';
277
278                         $attributes = "$class$style";
279
280                         switch ( $column_name ) {
281                                 case 'cb':
282                                         $r .= "<th scope='row' class='check-column'>$checkbox</th>";
283                                         break;
284                                 case 'username':
285                                         $r .= "<td $attributes>$avatar $edit</td>";
286                                         break;
287                                 case 'name':
288                                         $r .= "<td $attributes>$user_object->first_name $user_object->last_name</td>";
289                                         break;
290                                 case 'email':
291                                         $r .= "<td $attributes><a href='mailto:$email' title='" . esc_attr( sprintf( __( 'E-mail: %s' ), $email ) ) . "'>$email</a></td>";
292                                         break;
293                                 case 'role':
294                                         $r .= "<td $attributes>$role_name</td>";
295                                         break;
296                                 case 'posts':
297                                         $attributes = 'class="posts column-posts num"' . $style;
298                                         $r .= "<td $attributes>";
299                                         if ( $numposts > 0 ) {
300                                                 $r .= "<a href='edit.php?author=$user_object->ID' title='" . esc_attr__( 'View posts by this author' ) . "' class='edit'>";
301                                                 $r .= $numposts;
302                                                 $r .= '</a>';
303                                         } else {
304                                                 $r .= 0;
305                                         }
306                                         $r .= "</td>";
307                                         break;
308                                 default:
309                                         $r .= "<td $attributes>";
310                                         $r .= apply_filters( 'manage_users_custom_column', '', $column_name, $user_object->ID );
311                                         $r .= "</td>";
312                         }
313                 }
314                 $r .= '</tr>';
315
316                 return $r;
317         }
318 }