Wordpress 2.5.1
[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($action) ) {
15         if ( isset($_GET['deleteit']) )
16                 $action = 'delete';
17         elseif ( isset($_GET['changeit']) && !empty($_GET['new_role']) )
18                 $action = 'promote';
19 }
20
21 if ( empty($_REQUEST) ) {
22         $referer = '<input type="hidden" name="wp_http_referer" value="'. attribute_escape(stripslashes($_SERVER['REQUEST_URI'])) . '" />';
23 } elseif ( isset($_REQUEST['wp_http_referer']) ) {
24         $redirect = remove_query_arg(array('wp_http_referer', 'updated', 'delete_count'), stripslashes($_REQUEST['wp_http_referer']));
25         $referer = '<input type="hidden" name="wp_http_referer" value="' . attribute_escape($redirect) . '" />';
26 } else {
27         $redirect = 'users.php';
28 }
29
30 switch ($action) {
31
32 case 'promote':
33         check_admin_referer('bulk-users');
34
35         if (empty($_REQUEST['users'])) {
36                 wp_redirect($redirect);
37                 exit();
38         }
39
40         if ( !current_user_can('edit_users') )
41                 wp_die(__('You can&#8217;t edit users.'));
42
43         $userids = $_REQUEST['users'];
44         $update = 'promote';
45         foreach($userids as $id) {
46                 if ( ! current_user_can('edit_user', $id) )
47                         wp_die(__('You can&#8217;t edit that user.'));
48                 // The new role of the current user must also have edit_users caps
49                 if($id == $current_user->ID && !$wp_roles->role_objects[$_REQUEST['new_role']]->has_cap('edit_users')) {
50                         $update = 'err_admin_role';
51                         continue;
52                 }
53
54                 $user = new WP_User($id);
55                 $user->set_role($_REQUEST['new_role']);
56         }
57
58         wp_redirect(add_query_arg('update', $update, $redirect));
59         exit();
60
61 break;
62
63 case 'dodelete':
64
65         check_admin_referer('delete-users');
66
67         if ( empty($_REQUEST['users']) ) {
68                 wp_redirect($redirect);
69                 exit();
70         }
71
72         if ( !current_user_can('delete_users') )
73                 wp_die(__('You can&#8217;t delete users.'));
74
75         $userids = $_REQUEST['users'];
76         $update = 'del';
77         $delete_count = 0;
78
79         foreach ( (array) $userids as $id) {
80                 if ( ! current_user_can('delete_user', $id) )
81                         wp_die(__('You can&#8217;t delete that user.'));
82
83                 if($id == $current_user->ID) {
84                         $update = 'err_admin_del';
85                         continue;
86                 }
87                 switch($_REQUEST['delete_option']) {
88                 case 'delete':
89                         wp_delete_user($id);
90                         break;
91                 case 'reassign':
92                         wp_delete_user($id, $_REQUEST['reassign_user']);
93                         break;
94                 }
95                 ++$delete_count;
96         }
97
98         $redirect = add_query_arg( array('delete_count' => $delete_count, 'update' => $update), $redirect);
99         wp_redirect($redirect);
100         exit();
101
102 break;
103
104 case 'delete':
105
106         check_admin_referer('bulk-users');
107
108         if ( empty($_REQUEST['users']) ) {
109                 wp_redirect($redirect);
110                 exit();
111         }
112
113         if ( !current_user_can('delete_users') )
114                 $errors = new WP_Error('edit_users', __('You can&#8217;t delete users.'));
115
116         $userids = $_REQUEST['users'];
117
118         include ('admin-header.php');
119 ?>
120 <form action="" method="post" name="updateusers" id="updateusers">
121 <?php wp_nonce_field('delete-users') ?>
122 <?php echo $referer; ?>
123
124 <div class="wrap">
125 <h2><?php _e('Delete Users'); ?></h2>
126 <p><?php _e('You have specified these users for deletion:'); ?></p>
127 <ul>
128 <?php
129         $go_delete = false;
130         foreach ( (array) $userids as $id ) {
131                 $user = new WP_User($id);
132                 if ( $id == $current_user->ID ) {
133                         echo "<li>" . sprintf(__('ID #%1s: %2s <strong>The current user will not be deleted.</strong>'), $id, $user->user_login) . "</li>\n";
134                 } else {
135                         echo "<li><input type=\"hidden\" name=\"users[]\" value=\"{$id}\" />" . sprintf(__('ID #%1s: %2s'), $id, $user->user_login) . "</li>\n";
136                         $go_delete = true;
137                 }
138         }
139         $all_logins = $wpdb->get_results("SELECT ID, user_login FROM $wpdb->users ORDER BY user_login");
140         $user_dropdown = '<select name="reassign_user">';
141         foreach ( (array) $all_logins as $login )
142                 if ( $login->ID == $current_user->ID || !in_array($login->ID, $userids) )
143                         $user_dropdown .= "<option value=\"{$login->ID}\">{$login->user_login}</option>";
144         $user_dropdown .= '</select>';
145         ?>
146         </ul>
147 <?php if ( $go_delete ) : ?>
148         <p><?php _e('What should be done with posts and links owned by this user?'); ?></p>
149         <ul style="list-style:none;">
150                 <li><label><input type="radio" id="delete_option0" name="delete_option" value="delete" checked="checked" />
151                 <?php _e('Delete all posts and links.'); ?></label></li>
152                 <li><input type="radio" id="delete_option1" name="delete_option" value="reassign" />
153                 <?php echo '<label for="delete_option1">'.__('Attribute all posts and links to:')."</label> $user_dropdown"; ?></li>
154         </ul>
155         <input type="hidden" name="action" value="dodelete" />
156         <p class="submit"><input type="submit" name="submit" value="<?php _e('Confirm Deletion'); ?>" class="button-secondary" /></p>
157 <?php else : ?>
158         <p><?php _e('There are no valid users selected for deletion.'); ?></p>
159 <?php endif; ?>
160 </div>
161 </form>
162 <?php
163
164 break;
165
166 case 'adduser':
167         check_admin_referer('add-user');
168
169         if ( ! current_user_can('create_users') )
170                 wp_die(__('You can&#8217;t create users.'));
171
172         $user_id = add_user();
173         $update = 'add';
174         if ( is_wp_error( $user_id ) )
175                 $add_user_errors = $user_id;
176         else {
177                 $new_user_login = apply_filters('pre_user_login', sanitize_user(stripslashes($_REQUEST['user_login']), true));
178                 $redirect = add_query_arg( array('usersearch' => urlencode($new_user_login), 'update' => $update), $redirect );
179                 wp_redirect( $redirect . '#user-' . $user_id );
180                 die();
181         }
182
183 default:
184
185         if ( !empty($_GET['_wp_http_referer']) ) {
186                 wp_redirect(remove_query_arg(array('_wp_http_referer', '_wpnonce'), stripslashes($_SERVER['REQUEST_URI'])));
187                 exit;
188         }
189
190         wp_enqueue_script('admin-users');
191         wp_enqueue_script('admin-forms');
192
193         include('admin-header.php');
194
195         // Query the users
196         $wp_user_search = new WP_User_Search($_GET['usersearch'], $_GET['userspage'], $_GET['role']);
197
198         if ( isset($_GET['update']) ) :
199                 switch($_GET['update']) {
200                 case 'del':
201                 case 'del_many':
202                 ?>
203                         <?php $delete_count = (int) $_GET['delete_count']; ?>
204                         <div id="message" class="updated fade"><p><?php printf(__ngettext('%s user deleted', '%s users deleted', $delete_count), $delete_count); ?></p></div>
205                 <?php
206                         break;
207                 case 'add':
208                 ?>
209                         <div id="message" class="updated fade"><p><?php _e('New user created.'); ?></p></div>
210                 <?php
211                         break;
212                 case 'promote':
213                 ?>
214                         <div id="message" class="updated fade"><p><?php _e('Changed roles.'); ?></p></div>
215                 <?php
216                         break;
217                 case 'err_admin_role':
218                 ?>
219                         <div id="message" class="error"><p><?php _e("The current user's role must have user editing capabilities."); ?></p></div>
220                         <div id="message" class="updated fade"><p><?php _e('Other user roles have been changed.'); ?></p></div>
221                 <?php
222                         break;
223                 case 'err_admin_del':
224                 ?>
225                         <div id="message" class="error"><p><?php _e("You can't delete the current user."); ?></p></div>
226                         <div id="message" class="updated fade"><p><?php _e('Other users have been deleted.'); ?></p></div>
227                 <?php
228                         break;
229                 }
230         endif; ?>
231
232 <?php if ( is_wp_error( $errors ) ) : ?>
233         <div class="error">
234                 <ul>
235                 <?php
236                         foreach ( $errors->get_error_messages() as $message )
237                                 echo "<li>$message</li>";
238                 ?>
239                 </ul>
240         </div>
241 <?php endif; ?>
242
243 <div class="wrap">
244 <form id="posts-filter" action="" method="get">
245         <?php if ( $wp_user_search->is_search() ) : ?>
246                 <h2><?php printf(__('Users Matching "%s"'), wp_specialchars($wp_user_search->search_term)); ?></h2>
247         <?php else : ?>
248                 <h2><?php _e('Manage Users'); ?></h2>
249         <?php endif; ?>
250
251 <ul class="subsubsub">
252 <?php
253 $role_links = array();
254 $avail_roles = array();
255 $users_of_blog = get_users_of_blog();
256 //var_dump($users_of_blog);
257 foreach ( (array) $users_of_blog as $b_user ) {
258         $b_roles = unserialize($b_user->meta_value);
259         foreach ( (array) $b_roles as $b_role => $val ) {
260                 if ( !isset($avail_roles[$b_role]) )
261                         $avail_roles[$b_role] = 0;
262                 $avail_roles[$b_role]++;
263         }
264 }
265 unset($users_of_blog);
266
267 $current_role = false;
268 $class = empty($_GET['role']) ? ' class="current"' : '';
269 $role_links[] = "<li><a href=\"users.php\"$class>" . __('All Users') . "</a>";
270 foreach ( $wp_roles->get_names() as $role => $name ) {
271         if ( !isset($avail_roles[$role]) )
272                 continue;
273
274         $class = '';
275
276         if ( $role == $_GET['role'] ) {
277                 $current_role = $_GET['role'];
278                 $class = ' class="current"';
279         }
280
281         $name = translate_with_context($name);
282         $name = sprintf(_c('%1$s (%2$s)|user role with count'), $name, $avail_roles[$role]);
283         $role_links[] = "<li><a href=\"users.php?role=$role\"$class>" . $name . '</a>';
284 }
285 echo implode(' |</li>', $role_links) . '</li>';
286 unset($role_links);
287 ?>
288 </ul>
289         <p id="post-search">
290         <input type="text" id="post-search-input" name="usersearch" value="<?php echo attribute_escape($wp_user_search->search_term); ?>" />
291         <input type="submit" value="<?php _e( 'Search Users' ); ?>" class="button" />
292         </p>
293
294 <div class="tablenav">
295
296 <?php if ( $wp_user_search->results_are_paged() ) : ?>
297         <div class="tablenav-pages"><?php $wp_user_search->page_links(); ?></div>
298 <?php endif; ?>
299
300 <div class="alignleft">
301 <input type="submit" value="<?php _e('Delete'); ?>" name="deleteit" class="button-secondary delete" />
302 <select name="new_role"><option value=''><?php _e('Change role to&hellip;') ?></option>"<?php wp_dropdown_roles(); ?></select>
303 <input type="submit" value="<?php _e('Change'); ?>" name="changeit" class="button-secondary" />
304 <?php wp_nonce_field('bulk-users'); ?>
305 </div>
306
307 <br class="clear" />
308 </div>
309
310 <br class="clear" />
311
312         <?php if ( is_wp_error( $wp_user_search->search_errors ) ) : ?>
313                 <div class="error">
314                         <ul>
315                         <?php
316                                 foreach ( $wp_user_search->search_errors->get_error_messages() as $message )
317                                         echo "<li>$message</li>";
318                         ?>
319                         </ul>
320                 </div>
321         <?php endif; ?>
322
323
324 <?php if ( $wp_user_search->get_results() ) : ?>
325
326         <?php if ( $wp_user_search->is_search() ) : ?>
327                 <p><a href="users.php"><?php _e('&laquo; Back to All Users'); ?></a></p>
328         <?php endif; ?>
329
330 <table class="widefat">
331 <thead>
332 <tr class="thead">
333         <th scope="col" class="check-column"><input type="checkbox" onclick="checkAll(document.getElementById('posts-filter'));" /> </th>
334         <th><?php _e('Username') ?></th>
335         <th><?php _e('Name') ?></th>
336         <th><?php _e('E-mail') ?></th>
337         <th><?php _e('Role') ?></th>
338         <th class="num"><?php _e('Posts') ?></th>
339 </tr>
340 </thead>
341 <tbody id="users" class="list:user user-list">
342 <?php
343 $style = '';
344 foreach ( $wp_user_search->get_results() as $userid ) {
345         $user_object = new WP_User($userid);
346         $roles = $user_object->roles;
347         $role = array_shift($roles);
348
349         $style = ( ' class="alternate"' == $style ) ? '' : ' class="alternate"';
350         echo "\n\t" . user_row($user_object, $style, $role);
351 }
352 ?>
353 </tbody>
354 </table>
355
356 <div class="tablenav">
357
358 <?php if ( $wp_user_search->results_are_paged() ) : ?>
359         <div class="tablenav-pages"><?php $wp_user_search->page_links(); ?></div>
360 <?php endif; ?>
361
362 <br class="clear" />
363 </div>
364
365 <?php endif; ?>
366
367 </form>
368 </div>
369
370 <?php
371         if ( is_wp_error($add_user_errors) ) {
372                 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 ) {
373                         $var = 'new_' . $var;
374                         $$var = attribute_escape(stripslashes($_REQUEST[$formpost]));
375                 }
376                 unset($name);
377         }
378 ?>
379
380 <br class="clear" />
381 <?php if ( current_user_can('create_users') ) { ?>
382
383 <div class="wrap">
384 <h2 id="add-new-user"><?php _e('Add New User') ?></h2>
385
386 <?php if ( is_wp_error( $add_user_errors ) ) : ?>
387         <div class="error">
388                 <?php
389                         foreach ( $add_user_errors->get_error_messages() as $message )
390                                 echo "<p>$message</p>";
391                 ?>
392         </div>
393 <?php endif; ?>
394 <div id="ajax-response"></div>
395
396 <?php
397         if ( get_option('users_can_register') )
398                 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>';
399         else
400                 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>';
401 ?>
402 <form action="#add-new-user" method="post" name="adduser" id="adduser" class="add:users: validate">
403 <?php wp_nonce_field('add-user') ?>
404 <table class="form-table">
405         <tr class="form-field form-required">
406                 <th scope="row"><?php _e('Username (required)') ?><input name="action" type="hidden" id="action" value="adduser" /></th>
407                 <td ><input name="user_login" type="text" id="user_login" value="<?php echo $new_user_login; ?>" /></td>
408         </tr>
409         <tr class="form-field">
410                 <th scope="row"><?php _e('First Name') ?> </th>
411                 <td><input name="first_name" type="text" id="first_name" value="<?php echo $new_user_firstname; ?>" /></td>
412         </tr>
413         <tr class="form-field">
414                 <th scope="row"><?php _e('Last Name') ?> </th>
415                 <td><input name="last_name" type="text" id="last_name" value="<?php echo $new_user_lastname; ?>" /></td>
416         </tr>
417         <tr class="form-field form-required">
418                 <th scope="row"><?php _e('E-mail (required)') ?></th>
419                 <td><input name="email" type="text" id="email" value="<?php echo $new_user_email; ?>" /></td>
420         </tr>
421         <tr class="form-field">
422                 <th scope="row"><?php _e('Website') ?></th>
423                 <td><input name="url" type="text" id="url" value="<?php echo $new_user_uri; ?>" /></td>
424         </tr>
425
426 <?php if ( apply_filters('show_password_fields', true) ) : ?>
427         <tr class="form-field form-required">
428                 <th scope="row"><?php _e('Password (twice)') ?> </th>
429                 <td><input name="pass1" type="password" id="pass1" />
430                 <br />
431                 <input name="pass2" type="password" id="pass2" /></td>
432         </tr>
433 <?php endif; ?>
434
435         <tr class="form-field">
436                 <th scope="row"><?php _e('Role'); ?></th>
437                 <td><select name="role" id="role">
438                         <?php
439                         if ( !$new_user_role )
440                                 $new_user_role = $current_role ? $current_role : get_option('default_role');
441                         wp_dropdown_roles($new_user_role);
442                         ?>
443                         </select>
444                 </td>
445         </tr>
446 </table>
447 <p class="submit">
448         <?php echo $referer; ?>
449         <input name="adduser" type="submit" id="addusersub" value="<?php _e('Add User') ?>" />
450 </p>
451 </form>
452
453 </div>
454
455 <?php
456 }
457 break;
458
459 } // end of the $action switch
460
461 include('admin-footer.php');
462 ?>