From b27bd5b2539eaf9b7495ed396e40f1a0c9619b42 Mon Sep 17 00:00:00 2001 From: PeteMall Date: Wed, 24 Nov 2010 05:31:25 +0000 Subject: [PATCH] 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 --- .../includes/class-wp-users-list-table.php | 43 ++++- wp-admin/network/site-users.php | 180 +++++++----------- 2 files changed, 113 insertions(+), 110 deletions(-) diff --git a/wp-admin/includes/class-wp-users-list-table.php b/wp-admin/includes/class-wp-users-list-table.php index f46a572002..758bad8ff9 100644 --- a/wp-admin/includes/class-wp-users-list-table.php +++ b/wp-admin/includes/class-wp-users-list-table.php @@ -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'] = "" . sprintf( _nx( 'All (%s)', 'All (%s)', $total_users, 'users' ), number_format_i18n( $total_users ) ) . ''; + $role_links['all'] = "" . sprintf( _nx( 'All (%s)', 'All (%s)', $total_users, 'users' ), number_format_i18n( $total_users ) ) . ''; 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 (%2$s)'), $name, $avail_roles[$this_role] ); - $role_links[$this_role] = "$name"; + $role_links[$this_role] = "$name"; } 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}&"; + 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'] = "" . __( 'Delete' ) . ""; if ( is_multisite() && get_current_user_id() != $user_object->ID && current_user_can( 'remove_user', $user_object->ID ) ) - $actions['remove'] = "" . __( 'Remove' ) . ""; + $actions['remove'] = "" . __( 'Remove' ) . ""; $actions = apply_filters( 'user_row_actions', $actions, $user_object ); $edit .= $this->row_actions( $actions ); diff --git a/wp-admin/network/site-users.php b/wp-admin/network/site-users.php index 6895d18313..091c957a24 100644 --- a/wp-admin/network/site-users.php +++ b/wp-admin/network/site-users.php @@ -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’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’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’ 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 ) { } ?> -

' . $msg . '

'; -} ?> +
+ +
+ +views(); ?> +
- $id, 'number' => 20 ) ); -if ( is_array( $blogusers ) ) { - echo ''; - echo ""; - $user_count = 0; +display(); ?> - foreach ( $blogusers as $user_id => $user_object ) { - $user_count++; - $existing_role = reset( $user_object->roles ); - - echo ''; - if ( $user_id != $current_user->data->ID ) { - ?> - - - '; - } else { - echo ""; - } - echo ''; - } - echo "
" . __( 'User' ) . "" . __( 'Role' ) . "" . __( 'Password' ) . "" . __( 'Remove' ) . "
' . $user_object->user_login . ' - - - - " . __ ( 'N/A' ) . "" . __ ( 'N/A' ) . "" . __( 'N/A' ) . "
"; - submit_button(); - if ( 20 == $user_count ) - echo '

' . sprintf( __('First 20 users shown. Manage all users.'), get_admin_url($id, 'users.php') ) . '

'; -} else { - _e('This site has no users.'); -} -?>

-
+