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