Wordpress 2.3.2
[autoinstalls/wordpress.git] / wp-admin / users.php
1 <?php
2 require_once('admin.php');
3 require_once( ABSPATH . WPINC . '/registration.php');
4
5 if ( !current_user_can('edit_users') )
6         wp_die(__('Cheatin&#8217; uh?'));
7
8 $title = __('Users');
9 $parent_file = 'users.php';
10
11 $action = $_REQUEST['action'];
12 $update = '';
13
14 if ( empty($_POST) ) {
15         $referer = '<input type="hidden" name="wp_http_referer" value="'. attribute_escape(stripslashes($_SERVER['REQUEST_URI'])) . '" />';
16 } elseif ( isset($_POST['wp_http_referer']) ) {
17         $redirect = remove_query_arg(array('wp_http_referer', 'updated', 'delete_count'), stripslashes($_POST['wp_http_referer']));
18         $referer = '<input type="hidden" name="wp_http_referer" value="' . attribute_escape($redirect) . '" />';
19 } else {
20         $redirect = 'users.php';
21 }
22
23
24 // WP_User_Search class
25 // by Mark Jaquith
26
27
28 class WP_User_Search {
29         var $results;
30         var $search_term;
31         var $page;
32         var $raw_page;
33         var $users_per_page = 50;
34         var $first_user;
35         var $last_user;
36         var $query_limit;
37         var $query_from_where;
38         var $total_users_for_query = 0;
39         var $too_many_total_users = false;
40         var $search_errors;
41
42         function WP_User_Search ($search_term = '', $page = '') { // constructor
43                 $this->search_term = $search_term;
44                 $this->raw_page = ( '' == $page ) ? false : (int) $page;
45                 $this->page = (int) ( '' == $page ) ? 1 : $page;
46
47                 $this->prepare_query();
48                 $this->query();
49                 $this->prepare_vars_for_template_usage();
50                 $this->do_paging();
51         }
52
53         function prepare_query() {
54                 global $wpdb;
55                 $this->first_user = ($this->page - 1) * $this->users_per_page;
56                 $this->query_limit = 'LIMIT ' . $this->first_user . ',' . $this->users_per_page;
57                 if ( $this->search_term ) {
58                         $searches = array();
59                         $search_sql = 'AND (';
60                         foreach ( array('user_login', 'user_nicename', 'user_email', 'user_url', 'display_name') as $col )
61                                 $searches[] = $col . " LIKE '%$this->search_term%'";
62                         $search_sql .= implode(' OR ', $searches);
63                         $search_sql .= ')';
64                 }
65                 $this->query_from_where = "FROM $wpdb->users WHERE 1=1 $search_sql";
66
67         }
68
69         function query() {
70                 global $wpdb;
71                 $this->results = $wpdb->get_col('SELECT ID ' . $this->query_from_where . $this->query_limit);
72
73                 if ( $this->results )
74                         $this->total_users_for_query = $wpdb->get_var('SELECT COUNT(ID) ' . $this->query_from_where); // no limit
75                 else
76                         $this->search_errors = new WP_Error('no_matching_users_found', __('No matching users were found!'));
77         }
78
79         function prepare_vars_for_template_usage() {
80                 $this->search_term = stripslashes($this->search_term); // done with DB, from now on we want slashes gone
81         }
82
83         function do_paging() {
84                 if ( $this->total_users_for_query > $this->users_per_page ) { // have to page the results
85                         $this->paging_text = paginate_links( array(
86                                 'total' => ceil($this->total_users_for_query / $this->users_per_page),
87                                 'current' => $this->page,
88                                 'prev_text' => __('&laquo; Previous Page'),
89                                 'next_text' => __('Next Page &raquo;'),
90                                 'base' => 'users.php?%_%',
91                                 'format' => 'userspage=%#%',
92                                 'add_args' => array( 'usersearch' => urlencode($this->search_term) )
93                         ) );
94                 }
95         }
96
97         function get_results() {
98                 return (array) $this->results;
99         }
100
101         function page_links() {
102                 echo $this->paging_text;
103         }
104
105         function results_are_paged() {
106                 if ( $this->paging_text )
107                         return true;
108                 return false;
109         }
110
111         function is_search() {
112                 if ( $this->search_term )
113                         return true;
114                 return false;
115         }
116 }
117
118
119 switch ($action) {
120
121 case 'promote':
122         check_admin_referer('bulk-users');
123
124         if (empty($_POST['users'])) {
125                 wp_redirect($redirect);
126                 exit();
127         }
128
129         if ( !current_user_can('edit_users') )
130                 wp_die(__('You can&#8217;t edit users.'));
131
132         $userids = $_POST['users'];
133         $update = 'promote';
134         foreach($userids as $id) {
135                 if ( ! current_user_can('edit_user', $id) )
136                         wp_die(__('You can&#8217;t edit that user.'));
137                 // The new role of the current user must also have edit_users caps
138                 if($id == $current_user->ID && !$wp_roles->role_objects[$_POST['new_role']]->has_cap('edit_users')) {
139                         $update = 'err_admin_role';
140                         continue;
141                 }
142
143                 $user = new WP_User($id);
144                 $user->set_role($_POST['new_role']);
145         }
146
147         wp_redirect(add_query_arg('update', $update, $redirect));
148         exit();
149
150 break;
151
152 case 'dodelete':
153
154         check_admin_referer('delete-users');
155
156         if ( empty($_POST['users']) ) {
157                 wp_redirect($redirect);
158                 exit();
159         }
160
161         if ( !current_user_can('delete_users') )
162                 wp_die(__('You can&#8217;t delete users.'));
163
164         $userids = $_POST['users'];
165         $update = 'del';
166         $delete_count = 0;
167
168         foreach ( (array) $userids as $id) {
169                 if ( ! current_user_can('delete_user', $id) )
170                         wp_die(__('You can&#8217;t delete that user.'));
171
172                 if($id == $current_user->ID) {
173                         $update = 'err_admin_del';
174                         continue;
175                 }
176                 switch($_POST['delete_option']) {
177                 case 'delete':
178                         wp_delete_user($id);
179                         break;
180                 case 'reassign':
181                         wp_delete_user($id, $_POST['reassign_user']);
182                         break;
183                 }
184                 ++$delete_count;
185         }
186
187         $redirect = add_query_arg( array('delete_count' => $delete_count, 'update' => $update), $redirect);
188         wp_redirect($redirect);
189         exit();
190
191 break;
192
193 case 'delete':
194
195         check_admin_referer('bulk-users');
196
197         if ( empty($_POST['users']) ) {
198                 wp_redirect($redirect);
199                 exit();
200         }
201
202         if ( !current_user_can('delete_users') )
203                 $errors = new WP_Error('edit_users', __('You can&#8217;t delete users.'));
204
205         $userids = $_POST['users'];
206
207         include ('admin-header.php');
208 ?>
209 <form action="" method="post" name="updateusers" id="updateusers">
210 <?php wp_nonce_field('delete-users') ?>
211 <?php echo $referer; ?>
212 <div class="wrap">
213 <h2><?php _e('Delete Users'); ?></h2>
214 <p><?php _e('You have specified these users for deletion:'); ?></p>
215 <ul>
216 <?php
217         $go_delete = false;
218         foreach ( (array) $userids as $id ) {
219                 $user = new WP_User($id);
220                 if ( $id == $current_user->ID ) {
221                         echo "<li>" . sprintf(__('ID #%1s: %2s <strong>The current user will not be deleted.</strong>'), $id, $user->user_login) . "</li>\n";
222                 } else {
223                         echo "<li><input type=\"hidden\" name=\"users[]\" value=\"{$id}\" />" . sprintf(__('ID #%1s: %2s'), $id, $user->user_login) . "</li>\n";
224                         $go_delete = true;
225                 }
226         }
227         $all_logins = $wpdb->get_results("SELECT ID, user_login FROM $wpdb->users ORDER BY user_login");
228         $user_dropdown = '<select name="reassign_user">';
229         foreach ( (array) $all_logins as $login )
230                 if ( $login->ID == $current_user->ID || !in_array($login->ID, $userids) )
231                         $user_dropdown .= "<option value=\"{$login->ID}\">{$login->user_login}</option>";
232         $user_dropdown .= '</select>';
233         ?>
234         </ul>
235 <?php if ( $go_delete ) : ?>
236         <p><?php _e('What should be done with posts and links owned by this user?'); ?></p>
237         <ul style="list-style:none;">
238                 <li><label><input type="radio" id="delete_option0" name="delete_option" value="delete" checked="checked" />
239                 <?php _e('Delete all posts and links.'); ?></label></li>
240                 <li><input type="radio" id="delete_option1" name="delete_option" value="reassign" />
241                 <?php echo '<label for="delete_option1">'.__('Attribute all posts and links to:')."</label> $user_dropdown"; ?></li>
242         </ul>
243         <input type="hidden" name="action" value="dodelete" />
244         <p class="submit"><input type="submit" name="submit" value="<?php _e('Confirm Deletion'); ?>" /></p>
245 <?php else : ?>
246         <p><?php _e('There are no valid users selected for deletion.'); ?></p>
247 <?php endif; ?>
248 </div>
249 </form>
250 <?php
251
252 break;
253
254 case 'adduser':
255         check_admin_referer('add-user');
256
257         if ( ! current_user_can('create_users') )
258                 wp_die(__('You can&#8217;t create users.'));
259
260         $user_id = add_user();
261         $update = 'add';
262         if ( is_wp_error( $user_id ) )
263                 $add_user_errors = $user_id;
264         else {
265                 $new_user_login = apply_filters('pre_user_login', sanitize_user(stripslashes($_POST['user_login']), true));
266                 $redirect = add_query_arg( array('usersearch' => urlencode($new_user_login), 'update' => $update), $redirect );
267                 wp_redirect( $redirect . '#user-' . $user_id );
268                 die();
269         }
270
271 default:
272         wp_enqueue_script('admin-users');
273
274         include('admin-header.php');
275
276         // Query the users
277         $wp_user_search = new WP_User_Search($_GET['usersearch'], $_GET['userspage']);
278
279         // Make the user objects
280         foreach ( $wp_user_search->get_results() as $userid ) {
281                 $tmp_user = new WP_User($userid);
282                 $roles = $tmp_user->roles;
283                 $role = array_shift($roles);
284                 $roleclasses[$role][$tmp_user->user_login] = $tmp_user;
285         }
286
287         if ( isset($_GET['update']) ) :
288                 switch($_GET['update']) {
289                 case 'del':
290                 case 'del_many':
291                 ?>
292                         <?php $delete_count = (int) $_GET['delete_count']; ?>
293                         <div id="message" class="updated fade"><p><?php printf(__ngettext('%s user deleted', '%s users deleted', $delete_count), $delete_count); ?></p></div>
294                 <?php
295                         break;
296                 case 'add':
297                 ?>
298                         <div id="message" class="updated fade"><p><?php _e('New user created.'); ?></p></div>
299                 <?php
300                         break;
301                 case 'promote':
302                 ?>
303                         <div id="message" class="updated fade"><p><?php _e('Changed roles.'); ?></p></div>
304                 <?php
305                         break;
306                 case 'err_admin_role':
307                 ?>
308                         <div id="message" class="error"><p><?php _e("The current user's role must have user editing capabilities."); ?></p></div>
309                         <div id="message" class="updated fade"><p><?php _e('Other user roles have been changed.'); ?></p></div>
310                 <?php
311                         break;
312                 case 'err_admin_del':
313                 ?>
314                         <div id="message" class="error"><p><?php _e("You can't delete the current user."); ?></p></div>
315                         <div id="message" class="updated fade"><p><?php _e('Other users have been deleted.'); ?></p></div>
316                 <?php
317                         break;
318                 }
319         endif; ?>
320
321 <?php if ( is_wp_error( $errors ) ) : ?>
322         <div class="error">
323                 <ul>
324                 <?php
325                         foreach ( $errors->get_error_messages() as $message )
326                                 echo "<li>$message</li>";
327                 ?>
328                 </ul>
329         </div>
330 <?php endif; ?>
331
332 <div class="wrap">
333
334         <?php if ( $wp_user_search->is_search() ) : ?>
335                 <h2><?php printf(__('Users Matching "%s" by Role'), wp_specialchars($wp_user_search->search_term)); ?></h2>
336         <?php else : ?>
337                 <h2><?php _e('User List by Role'); ?></h2>
338         <?php endif; ?>
339
340         <form action="" method="get" name="search" id="search">
341                 <p><input type="text" name="usersearch" id="usersearch" value="<?php echo attribute_escape($wp_user_search->search_term); ?>" /> <input type="submit" value="<?php _e('Search Users &raquo;'); ?>" class="button" /></p>
342         </form>
343
344         <?php if ( is_wp_error( $wp_user_search->search_errors ) ) : ?>
345                 <div class="error">
346                         <ul>
347                         <?php
348                                 foreach ( $wp_user_search->search_errors->get_error_messages() as $message )
349                                         echo "<li>$message</li>";
350                         ?>
351                         </ul>
352                 </div>
353         <?php endif; ?>
354
355
356 <?php if ( $wp_user_search->get_results() ) : ?>
357
358         <?php if ( $wp_user_search->is_search() ) : ?>
359                 <p><a href="users.php"><?php _e('&laquo; Back to All Users'); ?></a></p>
360         <?php endif; ?>
361
362         <h3><?php
363         if ( 0 == $wp_user_search->first_user && $wp_user_search->total_users_for_query <= 50 )
364                 printf(__('%3$s shown below'), $wp_user_search->first_user + 1, min($wp_user_search->first_user + $wp_user_search->users_per_page, $wp_user_search->total_users_for_query), $wp_user_search->total_users_for_query);
365         else
366                 printf(__('%1$s &#8211; %2$s of %3$s shown below'), $wp_user_search->first_user + 1, min($wp_user_search->first_user + $wp_user_search->users_per_page, $wp_user_search->total_users_for_query), $wp_user_search->total_users_for_query); ?></h3>
367
368         <?php if ( $wp_user_search->results_are_paged() ) : ?>
369                 <div class="user-paging-text"><p><?php $wp_user_search->page_links(); ?></p></div>
370         <?php endif; ?>
371
372 <form action="" method="post" name="updateusers" id="updateusers">
373 <?php wp_nonce_field('bulk-users') ?>
374 <table class="widefat">
375 <?php
376 foreach($roleclasses as $role => $roleclass) {
377         uksort($roleclass, "strnatcasecmp");
378 ?>
379 <tbody>
380 <tr>
381 <?php if ( !empty($role) ) : ?>
382         <th colspan="7"><h3><?php echo $wp_roles->role_names[$role]; ?></h3></th>
383 <?php else : ?>
384         <th colspan="7"><h3><em><?php _e('No role for this blog'); ?></em></h3></th>
385 <?php endif; ?>
386 </tr>
387 <tr class="thead">
388         <th><?php _e('ID') ?></th>
389         <th><?php _e('Username') ?></th>
390         <th><?php _e('Name') ?></th>
391         <th><?php _e('E-mail') ?></th>
392         <th><?php _e('Website') ?></th>
393         <th colspan="2" style="text-align: center"><?php _e('Actions') ?></th>
394 </tr>
395 </tbody>
396 <tbody id="role-<?php echo $role; ?>"><?php
397 $style = '';
398 foreach ( (array) $roleclass as $user_object ) {
399         $style = ( ' class="alternate"' == $style ) ? '' : ' class="alternate"';
400         echo "\n\t" . user_row($user_object, $style);
401 }
402 ?>
403
404 </tbody>
405 <?php } ?>
406 </table>
407
408 <?php if ( $wp_user_search->results_are_paged() ) : ?>
409         <div class="user-paging-text"><p><?php $wp_user_search->page_links(); ?></p></div>
410 <?php endif; ?>
411
412         <h3><?php _e('Update Selected'); ?></h3>
413         <ul style="list-style:none;">
414                 <li><input type="radio" name="action" id="action0" value="delete" /> <label for="action0"><?php _e('Delete checked users.'); ?></label></li>
415                 <li>
416                         <input type="radio" name="action" id="action1" value="promote" /> <label for="action1"><?php _e('Set the Role of checked users to:'); ?></label>
417                         <select name="new_role" onchange="getElementById('action1').checked = 'true'"><?php wp_dropdown_roles(); ?></select>
418                 </li>
419         </ul>
420         <p class="submit" style="width: 420px">
421                 <?php echo $referer; ?>
422                 <input type="submit" value="<?php _e('Bulk Update &raquo;'); ?>" />
423         </p>
424 </form>
425 <?php endif; ?>
426 </div>
427
428 <?php
429         if ( is_wp_error($add_user_errors) ) {
430                 foreach ( array('user_login' => 'user_login', 'first_name' => 'user_firstname', 'last_name' => 'user_lastname', 'email' => 'user_email', 'url' => 'user_uri', 'role' => 'user_role') as $formpost => $var ) {
431                         $var = 'new_' . $var;
432                         $$var = attribute_escape(stripslashes($_POST[$formpost]));
433                 }
434                 unset($name);
435         }
436 ?>
437
438 <div class="wrap">
439 <h2 id="add-new-user"><?php _e('Add New User') ?></h2>
440
441 <?php if ( is_wp_error( $add_user_errors ) ) : ?>
442         <div class="error">
443                 <?php
444                         foreach ( $add_user_errors->get_error_messages() as $message )
445                                 echo "<p>$message</p>";
446                 ?>
447         </div>
448 <?php endif; ?>
449 <div id="ajax-response"></div>
450
451 <div class="narrow">
452
453 <?php
454         if ( get_option('users_can_register') )
455                 echo '<p>' . sprintf(__('Users can <a href="%1$s">register themselves</a> or you can manually create users here.'), get_option('siteurl').'/wp-register.php') . '</p>';
456         else
457         echo '<p>' . sprintf(__('Users cannot currently <a href="%1$s">register themselves</a>, but you can manually create users here.'), get_option('siteurl').'/wp-admin/options-general.php#users_can_register') . '</p>';
458 ?>
459 <form action="#add-new-user" method="post" name="adduser" id="adduser">
460 <?php wp_nonce_field('add-user') ?>
461 <table class="editform" width="100%" cellspacing="2" cellpadding="5">
462         <tr>
463                 <th scope="row" width="33%"><?php _e('Username (required)') ?><input name="action" type="hidden" id="action" value="adduser" /></th>
464                 <td width="66%"><input name="user_login" type="text" id="user_login" value="<?php echo $new_user_login; ?>" /></td>
465         </tr>
466         <tr>
467                 <th scope="row"><?php _e('First Name') ?> </th>
468                 <td><input name="first_name" type="text" id="first_name" value="<?php echo $new_user_firstname; ?>" /></td>
469         </tr>
470         <tr>
471                 <th scope="row"><?php _e('Last Name') ?> </th>
472                 <td><input name="last_name" type="text" id="last_name" value="<?php echo $new_user_lastname; ?>" /></td>
473         </tr>
474         <tr>
475                 <th scope="row"><?php _e('E-mail (required)') ?></th>
476                 <td><input name="email" type="text" id="email" value="<?php echo $new_user_email; ?>" /></td>
477         </tr>
478         <tr>
479                 <th scope="row"><?php _e('Website') ?></th>
480                 <td><input name="url" type="text" id="url" value="<?php echo $new_user_uri; ?>" /></td>
481         </tr>
482
483 <?php if ( apply_filters('show_password_fields', true) ) : ?>
484         <tr>
485                 <th scope="row"><?php _e('Password (twice)') ?> </th>
486                 <td><input name="pass1" type="password" id="pass1" />
487                 <br />
488                 <input name="pass2" type="password" id="pass2" /></td>
489         </tr>
490 <?php endif; ?>
491
492         <tr>
493                 <th scope="row"><?php _e('Role'); ?></th>
494                 <td><select name="role" id="role">
495                         <?php
496                         if ( !$new_user_role )
497                                 $new_user_role = get_option('default_role');
498                         wp_dropdown_roles($new_user_role);
499                         ?>
500                         </select>
501                 </td>
502         </tr>
503 </table>
504 <p class="submit">
505         <?php echo $referer; ?>
506         <input name="adduser" type="submit" id="addusersub" value="<?php _e('Add User &raquo;') ?>" />
507 </p>
508 </form>
509
510 </div>
511 </div>
512
513 <?php
514 break;
515
516 } // end of the $action switch
517
518 include('admin-footer.php');
519 ?>