WordPress.org

Make WordPress Core

Ticket #6405: users.patch

File users.patch, 12.4 KB (added by rajnikmit, 8 years ago)
  • users.php

     
    1515if ( !current_user_can('edit_users') )
    1616        wp_die(__('Cheatin’ uh?'));
    1717
    18 if ( ! is_multisite() && current_user_can('delete_users') ) {
    19         $del_cap_user = 'delete_user';
    20         $del_cap_users = 'delete_users';
    21 } else {
    22         $del_cap_user = 'remove_user';
    23         $del_cap_users = 'remove_users';
    24 }
    25 
    2618$title = __('Users');
    2719$parent_file = 'users.php';
    2820
     
    5143case 'promote':
    5244        check_admin_referer('bulk-users');
    5345
    54         if ( empty($_REQUEST['users']) ) {
     46        if (empty($_REQUEST['users'])) {
    5547                wp_redirect($redirect);
    5648                exit();
    5749        }
    5850
    5951        $editable_roles = get_editable_roles();
    60         if ( empty( $editable_roles[$_REQUEST['new_role']] ) )
     52        if (!$editable_roles[$_REQUEST['new_role']])
    6153                wp_die(__('You can’t give users that role.'));
    6254
    6355        $userids = $_REQUEST['users'];
    6456        $update = 'promote';
    65         foreach ( $userids as $id ) {
     57        foreach($userids as $id) {
    6658                if ( ! current_user_can('edit_user', $id) )
    6759                        wp_die(__('You can’t edit that user.'));
    6860                // The new role of the current user must also have edit_users caps
    69                 if ( $id == $current_user->ID && !$wp_roles->role_objects[$_REQUEST['new_role']]->has_cap('edit_users') ) {
     61                if($id == $current_user->ID && !$wp_roles->role_objects[$_REQUEST['new_role']]->has_cap('edit_users')) {
    7062                        $update = 'err_admin_role';
    7163                        continue;
    7264                }
     
    8981                exit();
    9082        }
    9183
    92         if ( ! current_user_can($del_cap_users ) )
    93                 wp_die(__('You can’t remove users.'));
     84        if ( !current_user_can('delete_users') )
     85                wp_die(__('You can’t delete users.'));
    9486
    9587        $userids = $_REQUEST['users'];
    9688        $update = 'del';
    9789        $delete_count = 0;
    9890
    9991        foreach ( (array) $userids as $id) {
    100                 if ( ! current_user_can( $del_cap_user, $id ) )
    101                         wp_die(__( 'You can’t remove that user.' ) );
     92                if ( ! current_user_can('delete_user', $id) )
     93                        wp_die(__('You can’t delete that user.'));
    10294
    103                 if ( $id == $current_user->ID ) {
     95                if($id == $current_user->ID) {
    10496                        $update = 'err_admin_del';
    10597                        continue;
    10698                }
    107                 switch ( $_REQUEST['delete_option'] ) {
     99                switch($_REQUEST['delete_option']) {
    108100                case 'delete':
    109                         if ( !is_multisite() && current_user_can('delete_user', $id) )
    110                                 wp_delete_user($id);
    111                         else
    112                                 remove_user_from_blog($id, $blog_id); // WPMU only remove user from blog
     101                        wp_delete_user($id);
    113102                        break;
    114103                case 'reassign':
    115                         if ( !is_multisite() && current_user_can('delete_user', $id) )
    116                                 wp_delete_user($id, $_REQUEST['reassign_user']);
    117                         else
    118                                 remove_user_from_blog($id, $blog_id, $_REQUEST['reassign_user']);
     104                        wp_delete_user($id, $_REQUEST['reassign_user']);
    119105                        break;
    120106                }
    121107                ++$delete_count;
     
    136122                exit();
    137123        }
    138124
    139         if ( ! current_user_can( $del_cap_users ) )
    140                 $errors = new WP_Error( 'edit_users', __( 'You can’t delete users.' ) );
     125        if ( !current_user_can('delete_users') )
     126                $errors = new WP_Error('edit_users', __('You can’t delete users.'));
    141127
    142128        if ( empty($_REQUEST['users']) )
    143129                $userids = array(intval($_REQUEST['user']));
     
    146132
    147133        include ('admin-header.php');
    148134?>
     135
     136
    149137<form action="" method="post" name="updateusers" id="updateusers">
    150138<?php wp_nonce_field('delete-users') ?>
    151139<?php echo $referer; ?>
     
    153141<div class="wrap">
    154142<?php screen_icon(); ?>
    155143<h2><?php _e('Delete Users'); ?></h2>
     144
    156145<p><?php _e('You have specified these users for deletion:'); ?></p>
    157146<ul>
    158147<?php
     
    162151                $user = new WP_User($id);
    163152                if ( $id == $current_user->ID ) {
    164153                        echo "<li>" . sprintf(__('ID #%1s: %2s <strong>The current user will not be deleted.</strong>'), $id, $user->user_login) . "</li>\n";
    165                 } else {
     154                }       else {
    166155                        echo "<li><input type=\"hidden\" name=\"users[]\" value=\"" . esc_attr($id) . "\" />" . sprintf(__('ID #%1s: %2s'), $id, $user->user_login) . "</li>\n";
    167156                        $go_delete = true;
    168157                }
    169158        }
    170         if ( !is_multisite() ) {
    171                 $all_logins = $wpdb->get_results("SELECT ID, user_login FROM $wpdb->users ORDER BY user_login");
    172         } else {
    173                 // WPMU only searches users of current blog
    174                 $all_logins = $wpdb->get_results("SELECT ID, user_login FROM $wpdb->users, $wpdb->usermeta WHERE $wpdb->users.ID = $wpdb->usermeta.user_id AND meta_key = '".$wpdb->prefix."capabilities' ORDER BY user_login");
    175         }
     159        $all_logins = $wpdb->get_results("SELECT ID, user_login FROM $wpdb->users ORDER BY user_login");
    176160        $user_dropdown = '<select name="reassign_user">';
    177161        foreach ( (array) $all_logins as $login )
    178162                if ( $login->ID == $current_user->ID || !in_array($login->ID, $userids) )
     
    180164        $user_dropdown .= '</select>';
    181165        ?>
    182166        </ul>
    183 <?php if ( $go_delete ) : ?>
     167<?php
     168
     169
     170        $link_owners = $wpdb->get_col( $wpdb->prepare(
     171"SELECT $wpdb->links.link_owner FROM $wpdb->links WHERE `link_owner`=%s"
     172, $id
     173 ));
     174foreach ( $link_owners as $link_owner ){
     175 
     176if($link_owner == $id){
     177
     178$num_links=$num_links+1;
     179
     180}
     181
     182}
     183
     184
     185
     186$post_authors = $wpdb->get_col( $wpdb->prepare(
     187"SELECT $wpdb->posts.post_author FROM $wpdb->posts WHERE `post_author`=%s"
     188, $id
     189 ));
     190foreach ( $post_authors as $post_author ){
     191 
     192if($post_author == $id){
     193
     194$num_posts=$num_posts+1;
     195
     196}
     197
     198}
     199       
     200        if ( $go_delete ) :
     201
     202
     203
     204if($num_posts>0 && $num_links>0){
     205           
     206                ?>
     207
     208
    184209        <fieldset><p><legend><?php _e('What should be done with posts and links owned by this user?'); ?></legend></p>
    185210        <ul style="list-style:none;">
    186211                <li><label><input type="radio" id="delete_option0" name="delete_option" value="delete" checked="checked" />
    187                 <?php _e('Delete all posts and links.'); ?></label></li>
     212                <?php _e('Delete all posts.'); ?></label></li>
    188213                <li><input type="radio" id="delete_option1" name="delete_option" value="reassign" />
    189                 <?php echo '<label for="delete_option1">'.__('Attribute all posts and links to:')."</label> $user_dropdown"; ?></li>
     214                <?php echo '<label for="delete_option1">'.__('Attribute all posts to:')."</label> $user_dropdown"; ?></li>
    190215        </ul></fieldset>
     216       
     217       
     218       
     219       
     220       
     221       
     222       
     223       
     224       
     225        <?php }elseif($num_posts>0){
     226           
     227                ?>
     228
     229
     230        <fieldset><p><legend><?php _e('What should be done with posts owned by this user?'); ?></legend></p>
     231        <ul style="list-style:none;">
     232                <li><label><input type="radio" id="delete_option0" name="delete_option" value="delete" checked="checked" />
     233                <?php _e('Delete all posts.'); ?></label></li>
     234                <li><input type="radio" id="delete_option1" name="delete_option" value="reassign" />
     235                <?php echo '<label for="delete_option1">'.__('Attribute all posts to:')."</label> $user_dropdown"; ?></li>
     236        </ul></fieldset><?php }elseif($num_links>0) {  ?>
     237
     238
     239
     240
     241<fieldset><p><legend><?php _e('What should be done with links owned by this user?'); ?></legend></p>
     242        <ul style="list-style:none;">
     243                <li><label><input type="radio" id="delete_option0" name="delete_option" value="delete" checked="checked" />
     244                <?php _e('Delete all links.'); ?></label></li>
     245                <li><input type="radio" id="delete_option1" name="delete_option" value="reassign" />
     246                <?php echo '<label for="delete_option1">'.__('Attribute all links to:')."</label> $user_dropdown"; ?></li>
     247        </ul></fieldset>
     248<?php } else { ?>
     249
     250
     251<label><input type="hidden" id="delete_option0" name="delete_option" value="delete" checked="checked" />
     252                </label>
     253                <input type="hidden" id="delete_option1" name="delete_option" value="reassign" />
     254                <?php echo '<label for="delete_option1">'."</label> "; ?>
     255
     256                <?php } ?>
     257
     258
     259
     260
     261
     262
     263
     264
     265
     266
    191267        <input type="hidden" name="action" value="dodelete" />
    192268        <p class="submit"><input type="submit" name="submit" value="<?php esc_attr_e('Confirm Deletion'); ?>" class="button-secondary" /></p>
     269
     270               
     271               
     272               
     273               
     274               
     275               
     276               
     277               
    193278<?php else : ?>
    194279        <p><?php _e('There are no valid users selected for deletion.'); ?></p>
    195280<?php endif; ?>
    196281</div>
    197282</form>
     283
    198284<?php
    199285
    200286break;
     
    212298        $userspage = isset($_GET['userspage']) ? $_GET['userspage'] : null;
    213299        $role = isset($_GET['role']) ? $_GET['role'] : null;
    214300
    215         // Query the user IDs for this page
     301        // Query the users
    216302        $wp_user_search = new WP_User_Search($usersearch, $userspage, $role);
    217303
    218         // Query the post counts for this page
    219         $post_counts = count_many_users_posts($wp_user_search->get_results());
    220 
    221         // Query the users for this page
    222         cache_users($wp_user_search->get_results());
    223 
    224304        $messages = array();
    225305        if ( isset($_GET['update']) ) :
    226306                switch($_GET['update']) {
    227307                case 'del':
    228308                case 'del_many':
    229309                        $delete_count = isset($_GET['delete_count']) ? (int) $_GET['delete_count'] : 0;
    230                         $messages[] = '<div id="message" class="updated"><p>' . sprintf(_n('%s user deleted', '%s users deleted', $delete_count), $delete_count) . '</p></div>';
     310                        $messages[] = '<div id="message" class="updated fade"><p>' . sprintf(_n('%s user deleted', '%s users deleted', $delete_count), $delete_count) . '</p></div>';
    231311                        break;
    232312                case 'add':
    233                         $messages[] = '<div id="message" class="updated"><p>' . __('New user created.') . '</p></div>';
     313                        $messages[] = '<div id="message" class="updated fade"><p>' . __('New user created.') . '</p></div>';
    234314                        break;
    235315                case 'promote':
    236                         $messages[] = '<div id="message" class="updated"><p>' . __('Changed roles.') . '</p></div>';
     316                        $messages[] = '<div id="message" class="updated fade"><p>' . __('Changed roles.') . '</p></div>';
    237317                        break;
    238318                case 'err_admin_role':
    239319                        $messages[] = '<div id="message" class="error"><p>' . __('The current user&#8217;s role must have user editing capabilities.') . '</p></div>';
    240                         $messages[] = '<div id="message" class="updated"><p>' . __('Other user roles have been changed.') . '</p></div>';
     320                        $messages[] = '<div id="message" class="updated fade"><p>' . __('Other user roles have been changed.') . '</p></div>';
    241321                        break;
    242322                case 'err_admin_del':
    243323                        $messages[] = '<div id="message" class="error"><p>' . __('You can&#8217;t delete the current user.') . '</p></div>';
    244                         $messages[] = '<div id="message" class="updated"><p>' . __('Other users have been deleted.') . '</p></div>';
     324                        $messages[] = '<div id="message" class="updated fade"><p>' . __('Other users have been deleted.') . '</p></div>';
    245325                        break;
    246326                }
    247327        endif; ?>
     
    264344
    265345<div class="wrap">
    266346<?php screen_icon(); ?>
    267 <h2><?php echo esc_html( $title ); if ( current_user_can( 'create_users' ) ) { ?>  <a href="user-new.php" class="button add-new-h2"><?php echo esc_html_x('Add New', 'user'); ?></a><?php }
     347<h2><?php echo esc_html( $title ); ?>  <a href="user-new.php" class="button add-new-h2"><?php echo esc_html_x('Add New', 'user'); ?></a> <?php
    268348if ( isset($_GET['usersearch']) && $_GET['usersearch'] )
    269349        printf( '<span class="subtitle">' . __('Search results for &#8220;%s&#8221;') . '</span>', esc_html( $_GET['usersearch'] ) ); ?>
    270350</h2>
     
    273353<form id="list-filter" action="" method="get">
    274354<ul class="subsubsub">
    275355<?php
    276 $users_of_blog = count_users();
    277 $total_users = $users_of_blog['total_users'];
    278 $avail_roles =& $users_of_blog['avail_roles'];
     356$role_links = array();
     357$avail_roles = array();
     358$users_of_blog = get_users_of_blog();
     359$total_users = count( $users_of_blog );
     360foreach ( (array) $users_of_blog as $b_user ) {
     361        $b_roles = unserialize($b_user->meta_value);
     362        foreach ( (array) $b_roles as $b_role => $val ) {
     363                if ( !isset($avail_roles[$b_role]) )
     364                        $avail_roles[$b_role] = 0;
     365                $avail_roles[$b_role]++;
     366        }
     367}
    279368unset($users_of_blog);
    280369
    281370$current_role = false;
    282371$class = empty($role) ? ' class="current"' : '';
    283 $role_links = array();
    284372$role_links[] = "<li><a href='users.php'$class>" . sprintf( _nx( 'All <span class="count">(%s)</span>', 'All <span class="count">(%s)</span>', $total_users, 'users' ), number_format_i18n( $total_users ) ) . '</a>';
    285373foreach ( $wp_roles->get_names() as $this_role => $name ) {
    286374        if ( !isset($avail_roles[$this_role]) )
     
    373461        $roles = $user_object->roles;
    374462        $role = array_shift($roles);
    375463
    376         if ( is_multisite() && empty( $role ) )
    377                 continue;
    378 
    379464        $style = ( ' class="alternate"' == $style ) ? '' : ' class="alternate"';
    380         echo "\n\t", user_row( $user_object, $style, $role, $post_counts[ $userid ] );
     465        echo "\n\t" . user_row($user_object, $style, $role);
    381466}
    382467?>
    383468</tbody>
     
    405490</form>
    406491</div>
    407492
    408 <?php
    409 if ( is_multisite() ) {
    410         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 ) {
    411                 $var = 'new_' . $var;
    412                 $$var = isset($_REQUEST[$formpost]) ? esc_attr(stripslashes($_REQUEST[$formpost])) : '';
    413         }
    414         unset($name);
    415 }
    416 ?>
    417 
    418493<br class="clear" />
    419494<?php
    420495break;