<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN"
"http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head><meta http-equiv="content-type" content="text/html; charset=utf-8" /><style type="text/css"><!--
#msg dl { border: 1px #006 solid; background: #369; padding: 6px; color: #fff; }
#msg dt { float: left; width: 6em; font-weight: bold; }
#msg dt:after { content:':';}
#msg dl, #msg dt, #msg ul, #msg li, #header, #footer { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; }
#msg dl a { font-weight: bold}
#msg dl a:link { color:#fc3; }
#msg dl a:active { color:#ff0; }
#msg dl a:visited { color:#cc6; }
h3 { font-family: verdana,arial,helvetica,sans-serif; font-size: 10pt; font-weight: bold; }
#msg pre { overflow: auto; background: #ffc; border: 1px #fc0 solid; padding: 6px; }
#msg ul, pre { overflow: auto; }
#header, #footer { color: #fff; background: #636; border: 1px #300 solid; padding: 6px; }
#patch { width: 100%; }
#patch h4 {font-family: verdana,arial,helvetica,sans-serif;font-size:10pt;padding:8px;background:#369;color:#fff;margin:0;}
#patch .propset h4, #patch .binary h4 {margin:0;}
#patch pre {padding:0;line-height:1.2em;margin:0;}
#patch .diff {width:100%;background:#eee;padding: 0 0 10px 0;overflow:auto;}
#patch .propset .diff, #patch .binary .diff {padding:10px 0;}
#patch span {display:block;padding:0 10px;}
#patch .modfile, #patch .addfile, #patch .delfile, #patch .propset, #patch .binary, #patch .copfile {border:1px solid #ccc;margin:10px 0;}
#patch ins {background:#dfd;text-decoration:none;display:block;padding:0 10px;}
#patch del {background:#fdd;text-decoration:none;display:block;padding:0 10px;}
#patch .lines, .info {color:#888;background:#fff;}
--></style>
<title>[16560] trunk/wp-admin: Adapt user-list-table for network admin site-users.</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/16560">16560</a></dd>
<dt>Author</dt> <dd>PeteMall</dd>
<dt>Date</dt> <dd>2010-11-24 05:31:25 +0000 (Wed, 24 Nov 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>Adapt user-list-table for network admin site-users. See <a href="http://trac.wordpress.org/ticket/15558">#15558</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadminincludesclasswpuserslisttablephp">trunk/wp-admin/includes/class-wp-users-list-table.php</a></li>
<li><a href="#trunkwpadminnetworksiteusersphp">trunk/wp-admin/network/site-users.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadminincludesclasswpuserslisttablephp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/includes/class-wp-users-list-table.php (16559 => 16560)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/includes/class-wp-users-list-table.php        2010-11-24 00:27:54 UTC (rev 16559)
+++ trunk/wp-admin/includes/class-wp-users-list-table.php        2010-11-24 05:31:25 UTC (rev 16560)
</span><span class="lines">@@ -7,10 +7,29 @@
</span><span class="cx"> * @since 3.1.0
</span><span class="cx"> */
</span><span class="cx"> class WP_Users_List_Table extends WP_List_Table {
</span><ins>+        
+        var $site_id;
+        var $is_site_users;
+        
+        function WP_Users_List_Table() {
+                $screen = get_current_screen();
+                $this->is_site_users = ( 'site-users-network' == $screen->id ) ? true : false;
</ins><span class="cx">
</span><ins>+                if ( $this->is_site_users )
+                        $this->site_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
+
+                parent::WP_List_Table( array(
+                        'singular' => 'user',
+                        'plural' => 'users'
+                ) );
+        }
+
</ins><span class="cx">         function check_permissions() {
</span><span class="cx">                 if ( !current_user_can('list_users') )
</span><span class="cx">                         wp_die(__('Cheatin&#8217; uh?'));
</span><ins>+
+                if ( $this->is_site_users && !current_user_can('manage_sites') )
+                        wp_die(__('You do not have sufficient permissions to edit this site.'));
</ins><span class="cx">         }
</span><span class="cx">
</span><span class="cx">         function prepare_items() {
</span><span class="lines">@@ -30,6 +49,9 @@
</span><span class="cx">                         'role' => $role,
</span><span class="cx">                         'search' => $usersearch
</span><span class="cx">                 );
</span><ins>+                
+                if ( $this->is_site_users )
+                        $args['blog_id'] = $this->site_id;
</ins><span class="cx">
</span><span class="cx">                 if ( isset( $_REQUEST['orderby'] ) )
</span><span class="cx">                         $args['orderby'] = $_REQUEST['orderby'];
</span><span class="lines">@@ -55,7 +77,15 @@
</span><span class="cx">         function get_views() {
</span><span class="cx">                 global $wp_roles, $role;
</span><span class="cx">
</span><del>-                $users_of_blog = count_users();
</del><ins>+                if ( $this->is_site_users ) {
+                        $url = 'site-users.php?id=' . $this->site_id;                        
+                        switch_to_blog( $this->site_id );
+                        $users_of_blog = count_users();
+                        restore_current_blog();
+                } else {
+                        $url = 'users.php';
+                        $users_of_blog = count_users();
+                }
</ins><span class="cx">                 $total_users = $users_of_blog['total_users'];
</span><span class="cx">                 $avail_roles =& $users_of_blog['avail_roles'];
</span><span class="cx">                 unset($users_of_blog);
</span><span class="lines">@@ -63,7 +93,7 @@
</span><span class="cx">                 $current_role = false;
</span><span class="cx">                 $class = empty($role) ? ' class="current"' : '';
</span><span class="cx">                 $role_links = array();
</span><del>-                $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>';
</del><ins>+                $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>';
</ins><span class="cx">                 foreach ( $wp_roles->get_names() as $this_role => $name ) {
</span><span class="cx">                         if ( !isset($avail_roles[$this_role]) )
</span><span class="cx">                                 continue;
</span><span class="lines">@@ -78,7 +108,7 @@
</span><span class="cx">                         $name = translate_user_role( $name );
</span><span class="cx">                         /* translators: User role name with count */
</span><span class="cx">                         $name = sprintf( __('%1$s <span class="count">(%2$s)</span>'), $name, $avail_roles[$this_role] );
</span><del>-                        $role_links[$this_role] = "<a href='users.php?role=$this_role'$class>$name</a>";
</del><ins>+                        $role_links[$this_role] = "<a href='" . add_query_arg( 'role', $this_role, $url ) . "'$class>$name</a>";
</ins><span class="cx">                 }
</span><span class="cx">
</span><span class="cx">                 return $role_links;
</span><span class="lines">@@ -171,6 +201,11 @@
</span><span class="cx">                         $user_object = new WP_User( (int) $user_object );
</span><span class="cx">                 $user_object = sanitize_user_object( $user_object, 'display' );
</span><span class="cx">                 $email = $user_object->user_email;
</span><ins>+                
+                if ( $this->is_site_users )
+                        $url = "site-users.php?id={$this->site_id}&amp;";
+                else
+                        $url = 'users.php?';
</ins><span class="cx">
</span><span class="cx">                 $checkbox = '';
</span><span class="cx">                 // Check if the user for this row is editable
</span><span class="lines">@@ -197,7 +232,7 @@
</span><span class="cx">                         if ( !is_multisite() && get_current_user_id() != $user_object->ID && current_user_can( 'delete_user', $user_object->ID ) )
</span><span class="cx">                                 $actions['delete'] = "<a class='submitdelete' href='" . wp_nonce_url( "users.php?action=delete&amp;user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Delete' ) . "</a>";
</span><span class="cx">                         if ( is_multisite() && get_current_user_id() != $user_object->ID && current_user_can( 'remove_user', $user_object->ID ) )
</span><del>-                                $actions['remove'] = "<a class='submitdelete' href='" . wp_nonce_url( "users.php?action=remove&amp;user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Remove' ) . "</a>";
</del><ins>+                                $actions['remove'] = "<a class='submitdelete' href='" . wp_nonce_url( $url."action=remove&amp;user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Remove' ) . "</a>";
</ins><span class="cx">                         $actions = apply_filters( 'user_row_actions', $actions, $user_object );
</span><span class="cx">                         $edit .= $this->row_actions( $actions );
</span><span class="cx">
</span></span></pre></div>
<a id="trunkwpadminnetworksiteusersphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/network/site-users.php (16559 => 16560)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/network/site-users.php        2010-11-24 00:27:54 UTC (rev 16559)
+++ trunk/wp-admin/network/site-users.php        2010-11-24 05:31:25 UTC (rev 16560)
</span><span class="lines">@@ -16,6 +16,17 @@
</span><span class="cx"> if ( ! current_user_can('manage_sites') )
</span><span class="cx">         wp_die(__('You do not have sufficient permissions to edit this site.'));
</span><span class="cx">
</span><ins>+$wp_list_table = get_list_table('WP_Users_List_Table');
+$wp_list_table->check_permissions();
+$wp_list_table->prepare_items();
+
+$action = $wp_list_table->current_action();
+
+$s = isset($_REQUEST['s']) ? $_REQUEST['s'] : '';
+
+// Clean up request URI from temporary args for screen options/paging uri's to work as expected.
+$_SERVER['REQUEST_URI'] = remove_query_arg(array('enable', 'disable', 'enable-selected', 'disable-selected'), $_SERVER['REQUEST_URI']);
+
</ins><span class="cx"> $id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
</span><span class="cx">
</span><span class="cx"> if ( ! $id )
</span><span class="lines">@@ -38,74 +49,65 @@
</span><span class="cx">         $editblog_roles = $wp_roles->roles;
</span><span class="cx"> }
</span><span class="cx">
</span><del>-if ( isset($_REQUEST['action']) && 'update-site' == $_REQUEST['action'] ) {
-        check_admin_referer( 'edit-site' );
</del><ins>+$action = $wp_list_table->current_action();
</ins><span class="cx">
</span><ins>+if ( $action ) {
</ins><span class="cx">         switch_to_blog( $id );
</span><ins>+        
+        switch ( $action ) {
+                case 'adduser':
+                        if ( !empty( $_POST['newuser'] ) ) {
+                                $newuser = $_POST['newuser'];
+                                $userid = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM " . $wpdb->users . " WHERE user_login = %s", $newuser ) );
+                                if ( $userid ) {
+                                        $user = $wpdb->get_var( "SELECT user_id FROM " . $wpdb->usermeta . " WHERE user_id='$userid' AND meta_key='{$blog_prefix}capabilities'" );
+                                        if ( $user == false )
+                                                add_user_to_blog( $id, $userid, $_POST['new_role'] );
+                                }
+                        }
+                        break;
+                
+                case 'remove':
+                        if ( !current_user_can('remove_users') )
+                                die(__('You can&#8217;t remove users.'));
</ins><span class="cx">
</span><del>-        // user roles
-        if ( isset( $_POST['role'] ) && is_array( $_POST['role'] ) == true ) {
-                $newroles = $_POST['role'];
</del><ins>+                        if ( isset( $_REQUEST['users'] ) ) {
+                                $userids = $_REQUEST['users'];
</ins><span class="cx">
</span><del>-                reset( $newroles );
-                foreach ( (array) $newroles as $userid => $role ) {
-                        $user = new WP_User( $userid );
-                        if ( empty( $user->ID ) )
-                                continue;
-                        $user->for_blog( $id );
-                        $user->set_role( $role );
-                }
-        }
</del><ins>+                                foreach ( $userids as $user_id ) {
+                                        $user_id = (int) $user_id;
+                                        remove_user_from_blog( $user_id, $id );
+                                }
+                        } else {
+                                remove_user_from_blog( $_GET['user'] );
+                        }
+                        break;
</ins><span class="cx">
</span><del>-        // remove user
-        if ( isset( $_POST['blogusers'] ) && is_array( $_POST['blogusers'] ) ) {
-                reset( $_POST['blogusers'] );
-                foreach ( (array) $_POST['blogusers'] as $key => $val )
-                        remove_user_from_blog( $key, $id );
-        }
</del><ins>+                case 'promote':
+                        $editable_roles = get_editable_roles();
+                        if ( empty( $editable_roles[$_REQUEST['new_role']] ) )
+                                wp_die(__('You can&#8217;t give users that role.'));
</ins><span class="cx">
</span><del>-        // change password
-        if ( isset( $_POST['user_password'] ) && is_array( $_POST['user_password'] ) ) {
-                reset( $_POST['user_password'] );
-                $newroles = $_POST['role'];
-                foreach ( (array) $_POST['user_password'] as $userid => $pass ) {
-                        unset( $_POST['role'] );
-                        $_POST['role'] = $newroles[ $userid ];
-                        if ( $pass != '' ) {
-                                $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 ) );
-                                $userdata = get_userdata($userid);
-                                $_POST['pass1'] = $_POST['pass2'] = $pass;
-                                $_POST['email'] = $userdata->user_email;
-                                $_POST['rich_editing'] = $userdata->rich_editing;
-                                edit_user( $userid );
-                                if ( $cap == null )
-                                        $wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->usermeta} WHERE user_id = %d AND meta_key = '{$blog_prefix}capabilities' AND meta_value = 'a:0:{}'", $userid ) );
-                        }
-                }
-                unset( $_POST['role'] );
-                $_POST['role'] = $newroles;
-        }
</del><ins>+                        $userids = $_REQUEST['users'];
+                        $update = 'promote';
+                        foreach ( $userids as $user_id ) {
+                                $user_id = (int) $user_id;
</ins><span class="cx">
</span><del>-        // add user
-        if ( !empty( $_POST['newuser'] ) ) {
-                $newuser = $_POST['newuser'];
-                $userid = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM " . $wpdb->users . " WHERE user_login = %s", $newuser ) );
-                if ( $userid ) {
-                        $user = $wpdb->get_var( "SELECT user_id FROM " . $wpdb->usermeta . " WHERE user_id='$userid' AND meta_key='{$blog_prefix}capabilities'" );
-                        if ( $user == false )
-                                add_user_to_blog( $id, $userid, $_POST['new_role'] );
-                }
-        }
</del><ins>+                                // If the user doesn't already belong to the blog, bail.
+                                if ( !is_user_member_of_blog( $user_id ) )
+                                        wp_die(__('Cheatin&#8217; uh?'));
</ins><span class="cx">
</span><ins>+                                $user = new WP_User( $user_id );
+                                $user->set_role( $_REQUEST['new_role'] );
+                        }
+                        break;
+        }
+        
</ins><span class="cx">         restore_current_blog();
</span><del>-        wp_redirect( add_query_arg( array( 'update' => 'updated', 'id' => $id ), 'site-users.php') );
</del><ins>+        wp_redirect( wp_get_referer() ); // @todo add_query_arg for update message
</ins><span class="cx"> }
</span><span class="cx">
</span><del>-if ( isset($_GET['update']) ) {
-        $messages = array();
-        if ( 'updated' == $_GET['update'] )
-                $messages[] = __('Site users updated.');
-}
</del><ins>+add_screen_option( 'per_page', array( 'label' => _x( 'Users', 'users per page (screen options)' ) ) );
</ins><span class="cx">
</span><span class="cx"> $title = sprintf( __('Edit Site: %s'), get_blogaddress_by_id($id));
</span><span class="cx"> $parent_file = 'sites.php';
</span><span class="lines">@@ -128,61 +130,27 @@
</span><span class="cx"> }
</span><span class="cx"> ?>
</span><span class="cx"> </h3>
</span><del>-<?php
-if ( ! empty( $messages ) ) {
-        foreach ( $messages as $msg )
-                echo '<div id="message" class="updated"><p>' . $msg . '</p></div>';
-} ?>
</del><ins>+<form class="search-form" action="" method="get">
+<p class="search-box">
+        <label class="screen-reader-text" for="user-search-input"><?php _e( 'Search Users' ); ?>:</label>
+        <input type="text" id="user-search-input" name="s" value="<?php echo esc_attr($usersearch); ?>" />
+        <?php submit_button( __( 'Search Users' ), 'button', 'submit', false ); ?>
+</p>
+</form>
+
+<?php $wp_list_table->views(); ?>
+
</ins><span class="cx"> <form method="post" action="site-users.php?action=update-site">
</span><span class="cx">         <?php wp_nonce_field( 'edit-site' ); ?>
</span><span class="cx">         <input type="hidden" name="id" value="<?php echo esc_attr( $id ) ?>" />
</span><del>-<?php
-$blogusers = get_users( array( 'blog_id' => $id, 'number' => 20 ) );
</del><span class="cx">
</span><del>-if ( is_array( $blogusers ) ) {
-        echo '<table class="form-table">';
-        echo "<tr><th>" . __( 'User' ) . "</th><th>" . __( 'Role' ) . "</th><th>" . __( 'Password' ) . "</th><th>" . __( 'Remove' ) . "</th></tr>";
-        $user_count = 0;
</del><ins>+<?php $wp_list_table->display(); ?>
</ins><span class="cx">
</span><del>-        foreach ( $blogusers as $user_id => $user_object ) {
-                $user_count++;
-                $existing_role = reset( $user_object->roles );
-
-                echo '<tr><td><a href="user-edit.php?user_id=' . $user_id . '">' . $user_object->user_login . '</a></td>';
-                if ( $user_id != $current_user->data->ID ) {
-                        ?>
-                        <td>
-                                <select name="role[<?php echo $user_id ?>]" id="new_role_1"><?php
-                                        foreach ( $editblog_roles as $role => $role_assoc ){
-                                                $name = translate_user_role( $role_assoc['name'] );
-                                                echo '<option ' . selected( $role, $existing_role, false ) . ' value="' . esc_attr( $role ) . '">' . esc_html( $name ) . '</option>';
-                                        }
-                                        ?>
-                                </select>
-                        </td>
-                        <td>
-                                <input type="text" name="user_password[<?php echo esc_attr( $user_id ) ?>]" />
-                        </td>
-                        <?php
-                        echo '<td><input title="' . __( 'Click to remove user' ) . '" type="checkbox" name="blogusers[' . esc_attr( $user_id ) . ']" /></td>';
-                } else {
-                        echo "<td><strong>" . __ ( 'N/A' ) . "</strong></td><td><strong>" . __ ( 'N/A' ) . "</strong></td><td><strong>" . __( 'N/A' ) . "</strong></td>";
-                }
-                echo '</tr>';
-        }
-        echo "</table>";
-        submit_button();
-        if ( 20 == $user_count )
-                echo '<p>' . sprintf( __('First 20 users shown. <a href="%s">Manage all users</a>.'), get_admin_url($id, 'users.php') ) . '</p>';
-} else {
-        _e('This site has no users.');
-}
-?>
</del><span class="cx"> </form>
</span><span class="cx">
</span><span class="cx"> <h3 id="add-new-user"><?php _e('Add Existing User') ?></h3>
</span><span class="cx"> <p class="description"><?php _e( 'Enter the username of an existing user.' ) ?></p>
</span><del>-        <form action="site-users.php?action=update-site" id="adduser" method="post">
</del><ins>+        <form action="site-users.php?action=adduser" id="adduser" method="post">
</ins><span class="cx">         <?php wp_nonce_field( 'edit-site' ); ?>
</span><span class="cx">         <input type="hidden" name="id" value="<?php echo esc_attr( $id ) ?>" />
</span><span class="cx">         <table class="form-table">
</span></span></pre>
</div>
</div>
</body>
</html>