WordPress.org

Make WordPress Core

Changeset 16560


Ignore:
Timestamp:
11/24/2010 05:31:25 AM (7 years ago)
Author:
PeteMall
Message:

Adapt user-list-table for network admin site-users. See #15558

Location:
trunk/wp-admin
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/wp-admin/includes/class-wp-users-list-table.php

    r16536 r16560  
    88 */
    99class WP_Users_List_Table extends WP_List_Table {
     10   
     11    var $site_id;
     12    var $is_site_users;
     13   
     14    function WP_Users_List_Table() {
     15        $screen = get_current_screen();
     16        $this->is_site_users = ( 'site-users-network' == $screen->id ) ? true : false;
     17
     18        if ( $this->is_site_users )
     19            $this->site_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
     20
     21        parent::WP_List_Table( array(
     22            'singular' => 'user',
     23            'plural'   => 'users'
     24        ) );
     25    }
    1026
    1127    function check_permissions() {
    1228        if ( !current_user_can('list_users') )
    1329            wp_die(__('Cheatin’ uh?'));
     30
     31        if ( $this->is_site_users && !current_user_can('manage_sites') )
     32            wp_die(__('You do not have sufficient permissions to edit this site.'));
    1433    }
    1534
     
    3150            'search' => $usersearch
    3251        );
     52       
     53        if ( $this->is_site_users )
     54            $args['blog_id'] = $this->site_id;
    3355
    3456        if ( isset( $_REQUEST['orderby'] ) )
     
    5678        global $wp_roles, $role;
    5779
    58         $users_of_blog = count_users();
     80        if ( $this->is_site_users ) {
     81            $url = 'site-users.php?id=' . $this->site_id;           
     82            switch_to_blog( $this->site_id );
     83            $users_of_blog = count_users();
     84            restore_current_blog();
     85        } else {
     86            $url = 'users.php';
     87            $users_of_blog = count_users();
     88        }
    5989        $total_users = $users_of_blog['total_users'];
    6090        $avail_roles =& $users_of_blog['avail_roles'];
     
    6494        $class = empty($role) ? ' class="current"' : '';
    6595        $role_links = array();
    66         $role_links['all'] = "<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>';
     96        $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>';
    6797        foreach ( $wp_roles->get_names() as $this_role => $name ) {
    6898            if ( !isset($avail_roles[$this_role]) )
     
    79109            /* translators: User role name with count */
    80110            $name = sprintf( __('%1$s <span class="count">(%2$s)</span>'), $name, $avail_roles[$this_role] );
    81             $role_links[$this_role] = "<a href='users.php?role=$this_role'$class>$name</a>";
     111            $role_links[$this_role] = "<a href='" . add_query_arg( 'role', $this_role, $url ) . "'$class>$name</a>";
    82112        }
    83113
     
    172202        $user_object = sanitize_user_object( $user_object, 'display' );
    173203        $email = $user_object->user_email;
     204       
     205        if ( $this->is_site_users )
     206            $url = "site-users.php?id={$this->site_id}&amp;";
     207        else
     208            $url = 'users.php?';
    174209
    175210        $checkbox = '';
     
    198233                $actions['delete'] = "<a class='submitdelete' href='" . wp_nonce_url( "users.php?action=delete&amp;user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Delete' ) . "</a>";
    199234            if ( is_multisite() && get_current_user_id() != $user_object->ID && current_user_can( 'remove_user', $user_object->ID ) )
    200                 $actions['remove'] = "<a class='submitdelete' href='" . wp_nonce_url( "users.php?action=remove&amp;user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Remove' ) . "</a>";
     235                $actions['remove'] = "<a class='submitdelete' href='" . wp_nonce_url( $url."action=remove&amp;user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Remove' ) . "</a>";
    201236            $actions = apply_filters( 'user_row_actions', $actions, $user_object );
    202237            $edit .= $this->row_actions( $actions );
  • trunk/wp-admin/network/site-users.php

    r16441 r16560  
    1616if ( ! current_user_can('manage_sites') )
    1717    wp_die(__('You do not have sufficient permissions to edit this site.'));
     18
     19$wp_list_table = get_list_table('WP_Users_List_Table');
     20$wp_list_table->check_permissions();
     21$wp_list_table->prepare_items();
     22
     23$action = $wp_list_table->current_action();
     24
     25$s = isset($_REQUEST['s']) ? $_REQUEST['s'] : '';
     26
     27// Clean up request URI from temporary args for screen options/paging uri's to work as expected.
     28$_SERVER['REQUEST_URI'] = remove_query_arg(array('enable', 'disable', 'enable-selected', 'disable-selected'), $_SERVER['REQUEST_URI']);
    1829
    1930$id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
     
    3950}
    4051
    41 if ( isset($_REQUEST['action']) && 'update-site' == $_REQUEST['action'] ) {
    42     check_admin_referer( 'edit-site' );
     52$action = $wp_list_table->current_action();
    4353
     54if ( $action ) {
    4455    switch_to_blog( $id );
     56   
     57    switch ( $action ) {
     58        case 'adduser':
     59            if ( !empty( $_POST['newuser'] ) ) {
     60                $newuser = $_POST['newuser'];
     61                $userid = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM " . $wpdb->users . " WHERE user_login = %s", $newuser ) );
     62                if ( $userid ) {
     63                    $user = $wpdb->get_var( "SELECT user_id FROM " . $wpdb->usermeta . " WHERE user_id='$userid' AND meta_key='{$blog_prefix}capabilities'" );
     64                    if ( $user == false )
     65                        add_user_to_blog( $id, $userid, $_POST['new_role'] );
     66                }
     67            }
     68            break;
     69       
     70        case 'remove':
     71            if ( !current_user_can('remove_users')  )
     72                die(__('You can&#8217;t remove users.'));
    4573
    46     // user roles
    47     if ( isset( $_POST['role'] ) && is_array( $_POST['role'] ) == true ) {
    48         $newroles = $_POST['role'];
     74            if ( isset( $_REQUEST['users'] ) ) {
     75                $userids = $_REQUEST['users'];
    4976
    50         reset( $newroles );
    51         foreach ( (array) $newroles as $userid => $role ) {
    52             $user = new WP_User( $userid );
    53             if ( empty( $user->ID ) )
    54                 continue;
    55             $user->for_blog( $id );
    56             $user->set_role( $role );
    57         }
     77                foreach ( $userids as $user_id ) {
     78                    $user_id = (int) $user_id;
     79                    remove_user_from_blog( $user_id, $id );
     80                }
     81            } else {
     82                remove_user_from_blog( $_GET['user'] );
     83            }
     84            break;
     85
     86        case 'promote':
     87            $editable_roles = get_editable_roles();
     88            if ( empty( $editable_roles[$_REQUEST['new_role']] ) )
     89                wp_die(__('You can&#8217;t give users that role.'));
     90
     91            $userids = $_REQUEST['users'];
     92            $update = 'promote';
     93            foreach ( $userids as $user_id ) {
     94                $user_id = (int) $user_id;
     95
     96                // If the user doesn't already belong to the blog, bail.
     97                if ( !is_user_member_of_blog( $user_id ) )
     98                    wp_die(__('Cheatin&#8217; uh?'));
     99
     100                $user = new WP_User( $user_id );
     101                $user->set_role( $_REQUEST['new_role'] );
     102            }
     103            break;
    58104    }
    59 
    60     // remove user
    61     if ( isset( $_POST['blogusers'] ) && is_array( $_POST['blogusers'] ) ) {
    62         reset( $_POST['blogusers'] );
    63         foreach ( (array) $_POST['blogusers'] as $key => $val )
    64             remove_user_from_blog( $key, $id );
    65     }
    66 
    67     // change password
    68     if ( isset( $_POST['user_password'] ) && is_array( $_POST['user_password'] ) ) {
    69         reset( $_POST['user_password'] );
    70         $newroles = $_POST['role'];
    71         foreach ( (array) $_POST['user_password'] as $userid => $pass ) {
    72             unset( $_POST['role'] );
    73             $_POST['role'] = $newroles[ $userid ];
    74             if ( $pass != '' ) {
    75                 $cap = $wpdb->get_var( $wpdb->prepare( "SELECT meta_value FROM {$wpdb->usermeta} WHERE user_id = %d AND meta_key = '{$blog_prefix}capabilities' AND meta_value = 'a:0:{}'", $userid ) );
    76                 $userdata = get_userdata($userid);
    77                 $_POST['pass1'] = $_POST['pass2'] = $pass;
    78                 $_POST['email'] = $userdata->user_email;
    79                 $_POST['rich_editing'] = $userdata->rich_editing;
    80                 edit_user( $userid );
    81                 if ( $cap == null )
    82                     $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->usermeta} WHERE user_id = %d AND meta_key = '{$blog_prefix}capabilities' AND meta_value = 'a:0:{}'", $userid ) );
    83             }
    84         }
    85         unset( $_POST['role'] );
    86         $_POST['role'] = $newroles;
    87     }
    88 
    89     // add user
    90     if ( !empty( $_POST['newuser'] ) ) {
    91         $newuser = $_POST['newuser'];
    92         $userid = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM " . $wpdb->users . " WHERE user_login = %s", $newuser ) );
    93         if ( $userid ) {
    94             $user = $wpdb->get_var( "SELECT user_id FROM " . $wpdb->usermeta . " WHERE user_id='$userid' AND meta_key='{$blog_prefix}capabilities'" );
    95             if ( $user == false )
    96                 add_user_to_blog( $id, $userid, $_POST['new_role'] );
    97         }
    98     }
    99 
     105   
    100106    restore_current_blog();
    101     wp_redirect( add_query_arg( array( 'update' => 'updated', 'id' => $id ), 'site-users.php') );
     107    wp_redirect( wp_get_referer() ); // @todo add_query_arg for update message
    102108}
    103109
    104 if ( isset($_GET['update']) ) {
    105     $messages = array();
    106     if ( 'updated' == $_GET['update'] )
    107         $messages[] = __('Site users updated.');
    108 }
     110add_screen_option( 'per_page', array( 'label' => _x( 'Users', 'users per page (screen options)' ) ) );
    109111
    110112$title = sprintf( __('Edit Site: %s'), get_blogaddress_by_id($id));
     
    129131?>
    130132</h3>
    131 <?php
    132 if ( ! empty( $messages ) ) {
    133     foreach ( $messages as $msg )
    134         echo '<div id="message" class="updated"><p>' . $msg . '</p></div>';
    135 } ?>
     133<form class="search-form" action="" method="get">
     134<p class="search-box">
     135    <label class="screen-reader-text" for="user-search-input"><?php _e( 'Search Users' ); ?>:</label>
     136    <input type="text" id="user-search-input" name="s" value="<?php echo esc_attr($usersearch); ?>" />
     137    <?php submit_button( __( 'Search Users' ), 'button', 'submit', false ); ?>
     138</p>
     139</form>
     140
     141<?php $wp_list_table->views(); ?>
     142
    136143<form method="post" action="site-users.php?action=update-site">
    137144    <?php wp_nonce_field( 'edit-site' ); ?>
    138145    <input type="hidden" name="id" value="<?php echo esc_attr( $id ) ?>" />
    139 <?php
    140 $blogusers = get_users( array( 'blog_id' => $id, 'number' => 20 ) );
    141146
    142 if ( is_array( $blogusers ) ) {
    143     echo '<table class="form-table">';
    144     echo "<tr><th>" . __( 'User' ) . "</th><th>" . __( 'Role' ) . "</th><th>" . __( 'Password' ) . "</th><th>" . __( 'Remove' ) . "</th></tr>";
    145     $user_count = 0;
     147<?php $wp_list_table->display(); ?>
    146148
    147     foreach ( $blogusers as $user_id => $user_object ) {
    148         $user_count++;
    149         $existing_role = reset( $user_object->roles );
    150 
    151         echo '<tr><td><a href="user-edit.php?user_id=' . $user_id . '">' . $user_object->user_login . '</a></td>';
    152         if ( $user_id != $current_user->data->ID ) {
    153             ?>
    154             <td>
    155                 <select name="role[<?php echo $user_id ?>]" id="new_role_1"><?php
    156                     foreach ( $editblog_roles as $role => $role_assoc ){
    157                         $name = translate_user_role( $role_assoc['name'] );
    158                         echo '<option ' . selected( $role, $existing_role, false ) . ' value="' . esc_attr( $role ) . '">' . esc_html( $name ) . '</option>';
    159                     }
    160                     ?>
    161                 </select>
    162             </td>
    163             <td>
    164                 <input type="text" name="user_password[<?php echo esc_attr( $user_id ) ?>]" />
    165             </td>
    166             <?php
    167             echo '<td><input title="' . __( 'Click to remove user' ) . '" type="checkbox" name="blogusers[' . esc_attr( $user_id ) . ']" /></td>';
    168         } else {
    169             echo "<td><strong>" . __ ( 'N/A' ) . "</strong></td><td><strong>" . __ ( 'N/A' ) . "</strong></td><td><strong>" . __( 'N/A' ) . "</strong></td>";
    170         }
    171         echo '</tr>';
    172     }
    173     echo "</table>";
    174     submit_button();
    175     if ( 20 == $user_count )
    176         echo '<p>' . sprintf( __('First 20 users shown. <a href="%s">Manage all users</a>.'), get_admin_url($id, 'users.php') ) . '</p>';
    177 } else {
    178     _e('This site has no users.');
    179 }
    180 ?>
    181149</form>
    182150
    183151<h3 id="add-new-user"><?php _e('Add Existing User') ?></h3>
    184152<p class="description"><?php _e( 'Enter the username of an existing user.' ) ?></p>
    185     <form action="site-users.php?action=update-site" id="adduser" method="post">
     153    <form action="site-users.php?action=adduser" id="adduser" method="post">
    186154    <?php wp_nonce_field( 'edit-site' ); ?>
    187155    <input type="hidden" name="id" value="<?php echo esc_attr( $id ) ?>" />
Note: See TracChangeset for help on using the changeset viewer.