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

git-svn-id: http://svn.automattic.com/wordpress/trunk@16560 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
PeteMall 2010-11-24 05:31:25 +00:00
parent 5c803c9b28
commit b27bd5b253
2 changed files with 113 additions and 110 deletions

View File

@ -7,10 +7,29 @@
* @since 3.1.0
*/
class WP_Users_List_Table extends WP_List_Table {
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;
if ( $this->is_site_users )
$this->site_id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
parent::WP_List_Table( array(
'singular' => 'user',
'plural' => 'users'
) );
}
function check_permissions() {
if ( !current_user_can('list_users') )
wp_die(__('Cheatin’ uh?'));
if ( $this->is_site_users && !current_user_can('manage_sites') )
wp_die(__('You do not have sufficient permissions to edit this site.'));
}
function prepare_items() {
@ -30,6 +49,9 @@ class WP_Users_List_Table extends WP_List_Table {
'role' => $role,
'search' => $usersearch
);
if ( $this->is_site_users )
$args['blog_id'] = $this->site_id;
if ( isset( $_REQUEST['orderby'] ) )
$args['orderby'] = $_REQUEST['orderby'];
@ -55,7 +77,15 @@ class WP_Users_List_Table extends WP_List_Table {
function get_views() {
global $wp_roles, $role;
$users_of_blog = count_users();
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();
}
$total_users = $users_of_blog['total_users'];
$avail_roles =& $users_of_blog['avail_roles'];
unset($users_of_blog);
@ -63,7 +93,7 @@ class WP_Users_List_Table extends WP_List_Table {
$current_role = false;
$class = empty($role) ? ' class="current"' : '';
$role_links = array();
$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>';
$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>';
foreach ( $wp_roles->get_names() as $this_role => $name ) {
if ( !isset($avail_roles[$this_role]) )
continue;
@ -78,7 +108,7 @@ class WP_Users_List_Table extends WP_List_Table {
$name = translate_user_role( $name );
/* translators: User role name with count */
$name = sprintf( __('%1$s <span class="count">(%2$s)</span>'), $name, $avail_roles[$this_role] );
$role_links[$this_role] = "<a href='users.php?role=$this_role'$class>$name</a>";
$role_links[$this_role] = "<a href='" . add_query_arg( 'role', $this_role, $url ) . "'$class>$name</a>";
}
return $role_links;
@ -171,6 +201,11 @@ class WP_Users_List_Table extends WP_List_Table {
$user_object = new WP_User( (int) $user_object );
$user_object = sanitize_user_object( $user_object, 'display' );
$email = $user_object->user_email;
if ( $this->is_site_users )
$url = "site-users.php?id={$this->site_id}&amp;";
else
$url = 'users.php?';
$checkbox = '';
// Check if the user for this row is editable
@ -197,7 +232,7 @@ class WP_Users_List_Table extends WP_List_Table {
if ( !is_multisite() && get_current_user_id() != $user_object->ID && current_user_can( 'delete_user', $user_object->ID ) )
$actions['delete'] = "<a class='submitdelete' href='" . wp_nonce_url( "users.php?action=delete&amp;user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Delete' ) . "</a>";
if ( is_multisite() && get_current_user_id() != $user_object->ID && current_user_can( 'remove_user', $user_object->ID ) )
$actions['remove'] = "<a class='submitdelete' href='" . wp_nonce_url( "users.php?action=remove&amp;user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Remove' ) . "</a>";
$actions['remove'] = "<a class='submitdelete' href='" . wp_nonce_url( $url."action=remove&amp;user=$user_object->ID", 'bulk-users' ) . "'>" . __( 'Remove' ) . "</a>";
$actions = apply_filters( 'user_row_actions', $actions, $user_object );
$edit .= $this->row_actions( $actions );

View File

@ -16,6 +16,17 @@ if ( ! is_multisite() )
if ( ! current_user_can('manage_sites') )
wp_die(__('You do not have sufficient permissions to edit this site.'));
$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']);
$id = isset( $_REQUEST['id'] ) ? intval( $_REQUEST['id'] ) : 0;
if ( ! $id )
@ -38,74 +49,65 @@ if ( ! empty($wp_roles->use_db) ) {
$editblog_roles = $wp_roles->roles;
}
if ( isset($_REQUEST['action']) && 'update-site' == $_REQUEST['action'] ) {
check_admin_referer( 'edit-site' );
$action = $wp_list_table->current_action();
if ( $action ) {
switch_to_blog( $id );
// user roles
if ( isset( $_POST['role'] ) && is_array( $_POST['role'] ) == true ) {
$newroles = $_POST['role'];
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 );
}
}
// 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 );
}
// 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 ) );
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'] );
}
}
}
unset( $_POST['role'] );
$_POST['role'] = $newroles;
}
break;
case 'remove':
if ( !current_user_can('remove_users') )
die(__('You can&#8217;t remove users.'));
// 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'] );
}
}
if ( isset( $_REQUEST['users'] ) ) {
$userids = $_REQUEST['users'];
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;
case 'promote':
$editable_roles = get_editable_roles();
if ( empty( $editable_roles[$_REQUEST['new_role']] ) )
wp_die(__('You can&#8217;t give users that role.'));
$userids = $_REQUEST['users'];
$update = 'promote';
foreach ( $userids as $user_id ) {
$user_id = (int) $user_id;
// If the user doesn't already belong to the blog, bail.
if ( !is_user_member_of_blog( $user_id ) )
wp_die(__('Cheatin&#8217; uh?'));
$user = new WP_User( $user_id );
$user->set_role( $_REQUEST['new_role'] );
}
break;
}
restore_current_blog();
wp_redirect( add_query_arg( array( 'update' => 'updated', 'id' => $id ), 'site-users.php') );
wp_redirect( wp_get_referer() ); // @todo add_query_arg for update message
}
if ( isset($_GET['update']) ) {
$messages = array();
if ( 'updated' == $_GET['update'] )
$messages[] = __('Site users updated.');
}
add_screen_option( 'per_page', array( 'label' => _x( 'Users', 'users per page (screen options)' ) ) );
$title = sprintf( __('Edit Site: %s'), get_blogaddress_by_id($id));
$parent_file = 'sites.php';
@ -128,61 +130,27 @@ foreach ( $tabs as $tab_id => $tab ) {
}
?>
</h3>
<?php
if ( ! empty( $messages ) ) {
foreach ( $messages as $msg )
echo '<div id="message" class="updated"><p>' . $msg . '</p></div>';
} ?>
<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(); ?>
<form method="post" action="site-users.php?action=update-site">
<?php wp_nonce_field( 'edit-site' ); ?>
<input type="hidden" name="id" value="<?php echo esc_attr( $id ) ?>" />
<?php
$blogusers = get_users( array( 'blog_id' => $id, 'number' => 20 ) );
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;
<?php $wp_list_table->display(); ?>
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.');
}
?>
</form>
<h3 id="add-new-user"><?php _e('Add Existing User') ?></h3>
<p class="description"><?php _e( 'Enter the username of an existing user.' ) ?></p>
<form action="site-users.php?action=update-site" id="adduser" method="post">
<form action="site-users.php?action=adduser" id="adduser" method="post">
<?php wp_nonce_field( 'edit-site' ); ?>
<input type="hidden" name="id" value="<?php echo esc_attr( $id ) ?>" />
<table class="form-table">