Wordpress 2.6.2-scripts
[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         <fieldset><p><legend><?php _e('What should be done with posts and links owned by this user?'); ?></legend></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></fieldset>
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         <label class="hidden" for="post-search-input"><?php _e( 'Search Users' ); ?>:</label>
291         <input type="text" id="post-search-input" name="usersearch" value="<?php echo attribute_escape($wp_user_search->search_term); ?>" />
292         <input type="submit" value="<?php _e( 'Search Users' ); ?>" class="button" />
293         </p>
294
295 <div class="tablenav">
296
297 <?php if ( $wp_user_search->results_are_paged() ) : ?>
298         <div class="tablenav-pages"><?php $wp_user_search->page_links(); ?></div>
299 <?php endif; ?>
300
301 <div class="alignleft">
302 <input type="submit" value="<?php _e('Delete'); ?>" name="deleteit" class="button-secondary delete" />
303 <label class="hidden" for="new_role"><?php _e('Change role to&hellip;') ?></label><select name="new_role" id="new_role"><option value=''><?php _e('Change role to&hellip;') ?></option>"<?php wp_dropdown_roles(); ?></select>
304 <input type="submit" value="<?php _e('Change'); ?>" name="changeit" class="button-secondary" />
305 <?php wp_nonce_field('bulk-users'); ?>
306 </div>
307
308 <br class="clear" />
309 </div>
310
311 <br class="clear" />
312
313         <?php if ( is_wp_error( $wp_user_search->search_errors ) ) : ?>
314                 <div class="error">
315                         <ul>
316                         <?php
317                                 foreach ( $wp_user_search->search_errors->get_error_messages() as $message )
318                                         echo "<li>$message</li>";
319                         ?>
320                         </ul>
321                 </div>
322         <?php endif; ?>
323
324
325 <?php if ( $wp_user_search->get_results() ) : ?>
326
327         <?php if ( $wp_user_search->is_search() ) : ?>
328                 <p><a href="users.php"><?php _e('&laquo; Back to All Users'); ?></a></p>
329         <?php endif; ?>
330
331 <table class="widefat">
332 <thead>
333 <tr class="thead">
334         <th scope="col" class="check-column"><input type="checkbox" /></th>
335         <th><?php _e('Username') ?></th>
336         <th><?php _e('Name') ?></th>
337         <th><?php _e('E-mail') ?></th>
338         <th><?php _e('Role') ?></th>
339         <th class="num"><?php _e('Posts') ?></th>
340 </tr>
341 </thead>
342 <tbody id="users" class="list:user user-list">
343 <?php
344 $style = '';
345 foreach ( $wp_user_search->get_results() as $userid ) {
346         $user_object = new WP_User($userid);
347         $roles = $user_object->roles;
348         $role = array_shift($roles);
349
350         $style = ( ' class="alternate"' == $style ) ? '' : ' class="alternate"';
351         echo "\n\t" . user_row($user_object, $style, $role);
352 }
353 ?>
354 </tbody>
355 </table>
356
357 <div class="tablenav">
358
359 <?php if ( $wp_user_search->results_are_paged() ) : ?>
360         <div class="tablenav-pages"><?php $wp_user_search->page_links(); ?></div>
361 <?php endif; ?>
362
363 <br class="clear" />
364 </div>
365
366 <?php endif; ?>
367
368 </form>
369 </div>
370
371 <?php
372         if ( is_wp_error($add_user_errors) ) {
373                 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 ) {
374                         $var = 'new_' . $var;
375                         $$var = attribute_escape(stripslashes($_REQUEST[$formpost]));
376                 }
377                 unset($name);
378         }
379 ?>
380
381 <br class="clear" />
382 <?php if ( current_user_can('create_users') ) { ?>
383
384 <div class="wrap">
385 <h2 id="add-new-user"><?php _e('Add New User') ?></h2>
386
387 <?php if ( is_wp_error( $add_user_errors ) ) : ?>
388         <div class="error">
389                 <?php
390                         foreach ( $add_user_errors->get_error_messages() as $message )
391                                 echo "<p>$message</p>";
392                 ?>
393         </div>
394 <?php endif; ?>
395 <div id="ajax-response"></div>
396
397 <?php
398         if ( get_option('users_can_register') )
399                 echo '<p>' . sprintf(__('Users can <a href="%1$s">register themselves</a> or you can manually create users here.'), site_url('wp-register.php')) . '</p>';
400         else
401                 echo '<p>' . sprintf(__('Users cannot currently <a href="%1$s">register themselves</a>, but you can manually create users here.'), admin_url('options-general.php#users_can_register')) . '</p>';
402 ?>
403 <form action="#add-new-user" method="post" name="adduser" id="adduser" class="add:users: validate">
404 <?php wp_nonce_field('add-user') ?>
405 <table class="form-table">
406         <tr class="form-field form-required">
407                 <th scope="row"><label for="user_login"><?php _e('Username (required)') ?></label><input name="action" type="hidden" id="action" value="adduser" /></th>
408                 <td ><input name="user_login" type="text" id="user_login" value="<?php echo $new_user_login; ?>" aria-required="true" /></td>
409         </tr>
410         <tr class="form-field">
411                 <th scope="row"><label for="first_name"><?php _e('First Name') ?> </label></th>
412                 <td><input name="first_name" type="text" id="first_name" value="<?php echo $new_user_firstname; ?>" /></td>
413         </tr>
414         <tr class="form-field">
415                 <th scope="row"><label for="last_name"><?php _e('Last Name') ?> </label></th>
416                 <td><input name="last_name" type="text" id="last_name" value="<?php echo $new_user_lastname; ?>" /></td>
417         </tr>
418         <tr class="form-field form-required">
419                 <th scope="row"><label for="email"><?php _e('E-mail (required)') ?></label></th>
420                 <td><input name="email" type="text" id="email" value="<?php echo $new_user_email; ?>" /></td>
421         </tr>
422         <tr class="form-field">
423                 <th scope="row"><label for="url"><?php _e('Website') ?></label></th>
424                 <td><input name="url" type="text" id="url" value="<?php echo $new_user_uri; ?>" /></td>
425         </tr>
426
427 <?php if ( apply_filters('show_password_fields', true) ) : ?>
428         <tr class="form-field form-required">
429                 <th scope="row"><label for="pass1"><?php _e('Password (twice)') ?> </label></th>
430                 <td><input name="pass1" type="password" id="pass1" />
431                 <br />
432                 <input name="pass2" type="password" id="pass2" /></td>
433         </tr>
434 <?php endif; ?>
435
436         <tr class="form-field">
437                 <th scope="row"><label for="role"><?php _e('Role'); ?></label></th>
438                 <td><select name="role" id="role">
439                         <?php
440                         if ( !$new_user_role )
441                                 $new_user_role = $current_role ? $current_role : get_option('default_role');
442                         wp_dropdown_roles($new_user_role);
443                         ?>
444                         </select>
445                 </td>
446         </tr>
447 </table>
448 <p class="submit">
449         <?php echo $referer; ?>
450         <input name="adduser" type="submit" id="addusersub" value="<?php _e('Add User') ?>" />
451 </p>
452 </form>
453
454 </div>
455
456 <?php
457 }
458 break;
459
460 } // end of the $action switch
461
462 include('admin-footer.php');
463 ?>