From 5e7c850484b527f7efaaf943a368512083df0d8f Mon Sep 17 00:00:00 2001 From: ryan Date: Thu, 8 Jun 2006 18:36:05 +0000 Subject: [PATCH] User management improvements from Mark Jaquith and David House. #2793 git-svn-id: http://svn.automattic.com/wordpress/trunk@3857 1a063a9b-81f0-0310-95a4-ce76da25c4cd --- wp-admin/admin-functions.php | 12 +- wp-admin/menu.php | 10 +- wp-admin/profile.php | 5 +- wp-admin/user-edit.php | 19 +- wp-admin/users.php | 368 +++++++++++++++++++++++------------ wp-admin/wp-admin.css | 2 +- wp-includes/functions.php | 35 +++- 7 files changed, 310 insertions(+), 141 deletions(-) diff --git a/wp-admin/admin-functions.php b/wp-admin/admin-functions.php index 9f596fc7d..996765308 100644 --- a/wp-admin/admin-functions.php +++ b/wp-admin/admin-functions.php @@ -729,17 +729,21 @@ function user_row( $user_object, $style = '' ) { if (strlen($short_url) > 35) $short_url = substr($short_url, 0, 32).'...'; $numposts = get_usernumposts($user_object->ID); - if (0 < $numposts) $numposts = "$numposts"; $r = " $email $short_url"; - $r .= "\n\t\t$numposts"; - $r .= "\n\t\t"; + $r .= "\n\t\t"; + if ($numposts > 0) { + $r .= ""; + $r .= sprintf(__('View %1$s %2$s'), $numposts, __ngettext('post', 'posts', $numposts)); + } + $r .= "\n\t\t"; + $edit_link = add_query_arg('wp_http_referer', wp_specialchars(urlencode(stripslashes($_SERVER['REQUEST_URI']))), "user-edit.php?user_id=$user_object->ID"); if ( current_user_can('edit_user', $user_object->ID) ) - $r .= "".__('Edit').""; + $r .= "".__('Edit').""; $r .= "\n\t"; return $r; } diff --git a/wp-admin/menu.php b/wp-admin/menu.php index fa5ee19a7..cbeb16ba4 100644 --- a/wp-admin/menu.php +++ b/wp-admin/menu.php @@ -11,7 +11,7 @@ $menu[20] = array(__('Bookmarks'), 'manage_links', 'link-manager.php'); $menu[25] = array(__('Presentation'), 'switch_themes', 'themes.php'); $menu[30] = array(__('Plugins'), 'activate_plugins', 'plugins.php'); if ( current_user_can('edit_users') ) - $menu[35] = array(__('Users'), 'read', 'profile.php'); + $menu[35] = array(__('Users'), 'edit_users', 'users.php'); else $menu[35] = array(__('Profile'), 'read', 'profile.php'); $menu[40] = array(__('Options'), 'manage_options', 'options-general.php'); @@ -34,8 +34,12 @@ $submenu['link-manager.php'][5] = array(__('Manage Bookmarks'), 'manage_links', $submenu['link-manager.php'][10] = array(__('Add Bookmark'), 'manage_links', 'link-add.php'); $submenu['link-manager.php'][20] = array(__('Import Bookmarks'), 'manage_links', 'link-import.php'); -$submenu['profile.php'][5] = array(__('Your Profile'), 'read', 'profile.php'); -$submenu['profile.php'][10] = array(__('Authors & Users'), 'edit_users', 'users.php'); +if ( current_user_can('edit_users') ) { + $submenu['users.php'][5] = array(__('Authors & Users'), 'edit_users', 'users.php'); + $submenu['users.php'][10] = array(__('Your Profile'), 'read', 'profile.php'); +} else { + $submenu['profile.php'][5] = array(__('Your Profile'), 'read', 'profile.php'); +} $submenu['options-general.php'][10] = array(__('General'), 'manage_options', 'options-general.php'); $submenu['options-general.php'][15] = array(__('Writing'), 'manage_options', 'options-writing.php'); diff --git a/wp-admin/profile.php b/wp-admin/profile.php index 04f2fc492..eb66e078a 100644 --- a/wp-admin/profile.php +++ b/wp-admin/profile.php @@ -3,7 +3,10 @@ require_once('admin.php'); $title = __('Profile'); -$parent_file = 'profile.php'; +if ( current_user_can('edit_users') ) + $parent_file = 'users.php'; +else + $parent_file = 'profile.php'; include_once('admin-header.php'); $profileuser = new WP_User($user_ID); diff --git a/wp-admin/user-edit.php b/wp-admin/user-edit.php index 362dbde09..851451ac0 100644 --- a/wp-admin/user-edit.php +++ b/wp-admin/user-edit.php @@ -2,10 +2,13 @@ require_once('admin.php'); $title = __('Edit User'); -$parent_file = 'profile.php'; +if ( current_user_can('edit_users') ) + $parent_file = 'users.php'; +else + $parent_file = 'profile.php'; $submenu_file = 'users.php'; -$wpvarstoreset = array('action', 'redirect', 'profile', 'user_id'); +$wpvarstoreset = array('action', 'redirect', 'profile', 'user_id', 'wp_http_referer'); for ($i=0; $i

+ +

+
@@ -75,6 +85,9 @@ if ( !current_user_can('edit_user', $user_id) )
+ + +

diff --git a/wp-admin/users.php b/wp-admin/users.php index 86b2cf442..87a33e84c 100644 --- a/wp-admin/users.php +++ b/wp-admin/users.php @@ -3,39 +3,51 @@ require_once('admin.php'); require_once( ABSPATH . WPINC . '/registration-functions.php'); $title = __('Users'); -$parent_file = 'profile.php'; +if ( current_user_can('edit_users') ) + $parent_file = 'users.php'; +else + $parent_file = 'profile.php'; $action = $_REQUEST['action']; $update = ''; +if ( empty($_POST) ) { + $referer = ''; +} elseif ( isset($_POST['wp_http_referer']) ) { + $redirect = remove_query_arg(array('wp_http_referer', 'updated', 'delete_count'), urlencode(stripslashes($_POST['wp_http_referer']))); + $referer = ''; +} else { + $redirect = 'users.php'; +} + switch ($action) { case 'promote': check_admin_referer('bulk-users'); if (empty($_POST['users'])) { - header('Location: users.php'); + header('Location: ' . $redirect); } if ( !current_user_can('edit_users') ) die(__('You can’t edit users.')); - $userids = $_POST['users']; + $userids = $_POST['users']; $update = 'promote'; - foreach($userids as $id) { - if ( ! current_user_can('edit_user', $id) ) - die(__('You can’t edit that user.')); + foreach($userids as $id) { + if ( ! current_user_can('edit_user', $id) ) + die(__('You can’t edit that user.')); // The new role of the current user must also have edit_users caps if($id == $current_user->id && !$wp_roles->role_objects[$_POST['new_role']]->has_cap('edit_users')) { $update = 'err_admin_role'; continue; } - $user = new WP_User($id); - $user->set_role($_POST['new_role']); - } + $user = new WP_User($id); + $user->set_role($_POST['new_role']); + } - header('Location: users.php?update=' . $update); + header('Location: ' . add_query_arg('update', $update, $redirect)); break; @@ -44,24 +56,25 @@ case 'dodelete': check_admin_referer('delete-users'); if ( empty($_POST['users']) ) { - header('Location: users.php'); + header('Location: ' . $redirect); } if ( !current_user_can('delete_users') ) die(__('You can’t delete users.')); $userids = $_POST['users']; - $update = 'del'; - foreach ($userids as $id) { - if ( ! current_user_can('delete_user', $id) ) - die(__('You can’t delete that user.')); - + $delete_count = 0; + + foreach ( (array) $userids as $id) { + if ( ! current_user_can('delete_user', $id) ) + die(__('You can’t delete that user.')); + if($id == $current_user->id) { $update = 'err_admin_del'; continue; } - switch($_POST['delete_option']) { + switch($_POST['delete_option']) { case 'delete': wp_delete_user($id); break; @@ -69,9 +82,12 @@ case 'dodelete': wp_delete_user($id, $_POST['reassign_user']); break; } + ++$delete_count; } - header('Location: users.php?update=' . $update); + $redirect = add_query_arg('delete_count', $delete_count, $redirect); + + header('Location: ' . add_query_arg('update', $update, $redirect)); break; @@ -79,12 +95,11 @@ case 'delete': check_admin_referer('bulk-users'); - if (empty($_POST['users'])) { - header('Location: users.php'); - } + if ( empty($_POST['users']) ) + header('Location: ' . $redirect); if ( !current_user_can('delete_users') ) - $error = new WP_Error('edit_users', __('You can’t delete users.')); + $errors = new WP_Error('edit_users', __('You can’t delete users.')); $userids = $_POST['users']; @@ -92,33 +107,32 @@ case 'delete': ?> +

    id) { + foreach ( (array) $userids as $id ) { + $user = new WP_User($id); + if ( $id == $current_user->id ) { echo "
  • " . sprintf(__('ID #%1s: %2s The current user will not be deleted.'), $id, $user->user_login) . "
  • \n"; } else { echo "
  • " . sprintf(__('ID #%1s: %2s'), $id, $user->user_login) . "
  • \n"; $go_delete = true; } - } - $all_logins = $wpdb->get_results("SELECT ID, user_login FROM $wpdb->users ORDER BY user_login"); - $user_dropdown = ''; - ?> -
- -

+ } + $all_logins = $wpdb->get_results("SELECT ID, user_login FROM $wpdb->users ORDER BY user_login"); + $user_dropdown = ''; + ?> + + +

  • @@ -143,35 +157,86 @@ case 'adduser': die(__('You can’t create users.')); $user_id = add_user(); + $update = 'add'; if ( is_wp_error( $user_id ) ) - $errors = $user_id; + $add_user_errors = $user_id; else { - header('Location: users.php?update=add'); + $new_user_login = apply_filters('pre_user_login', sanitize_user(stripslashes($_POST['user_login']), true)); + $redirect = add_query_arg('usersearch', $new_user_login, $redirect); + header('Location: ' . add_query_arg('update', $update, $redirect) . '#user-' . $user_id); die(); } default: - wp_enqueue_script( 'admin-users' ); + wp_enqueue_script('admin-users'); - include ('admin-header.php'); + include('admin-header.php'); - $userids = $wpdb->get_col("SELECT ID FROM $wpdb->users;"); + /* Paging and Search by Mark Jaquith, June 6th, 2006 */ - foreach($userids as $userid) { + $users_per_page = 50; + + $page = (int) $_GET['userspage']; + if ( !$page ) + $page = 1; + + $starton = ($page - 1) * $users_per_page; + + $limit = 'LIMIT ' . $starton . ',' . $users_per_page; + + $search_term = $_GET['usersearch']; + if ( $search_term ) { + $searches = array(); + $search_sql = 'AND ('; + foreach ( array('user_login', 'user_nicename', 'user_email', 'user_url', 'display_name') as $col ) + $searches[] = $col . " LIKE '%$search_term%'"; + $search_sql .= implode(' OR ', $searches); + $search_sql .= ')'; + $search_term = stripslashes($search_term); // done with DB, from now on we want slashes gone + } + + if ( !$_GET['update'] && !$search_term && !$_GET['userspage'] && $wpdb->get_var("SELECT COUNT(ID) FROM $wpdb->users") > $users_per_page ) + $too_many_users = sprintf(__('Because this blog has more than %s users, they cannot all be shown on one page. Use the paging or search functionality in order to find the user you want to edit.'), $users_per_page); + + $from_where = "FROM $wpdb->users WHERE 1=1 $search_sql"; + $userids = $wpdb->get_col('SELECT ID ' . $from_where . $limit); + + if ( $userids ) + $total_users_for_this_query = $wpdb->get_var('SELECT COUNT(ID) ' . $from_where); // no limit + else + $errors = new WP_Error('no_matching_users_found', __('No matching users were found!')); + + // Now for the paging + if ( $total_users_for_this_query > $users_per_page ) { // have to page the results + $prev_page = ( $page > 1) ? true : false; + $next_page = ( ($page * $users_per_page) < $total_users_for_this_query ) ? true : false; + $paging_text = ''; + if ( $prev_page ) + $paging_text .= '

    « Previous Page

    '; + if ( $next_page ) + $paging_text .= '

    Next Page »

    '; + if ( $prev_page || $next_page ) + $paging_text .= '
    '; + } + + // Clean up, we're done with these variables + unset($prev_page, $next_page, $limit, $searches, $search_sql, $col); + + // Make the user objects + foreach ( (array) $userids as $userid ) { $tmp_user = new WP_User($userid); $roles = $tmp_user->roles; $role = array_shift($roles); $roleclasses[$role][$tmp_user->user_login] = $tmp_user; } - ?> - - -

    + +

    + endif; ?> + +
      get_error_messages() as $message ) - echo "
    • $message
    • "; + echo "
    • $message
    • "; ?>
    - + + + +
    +

    +
    + + +
    +

    + +

    + + +

    + +
    + +
    -

    + +

    +
    + +

    + +

    + + +

    $roleclass) { @@ -222,100 +313,131 @@ foreach($roleclasses as $role => $roleclass) { ?> - + - - + - - + - +

    role_names[$role]; ?>

    role_names[$role]; ?>

     
    + +
    +

    -
      -
    • -
    • - - -
    • -
    -

    +
      +
    • +
    • + + +
    • +
    +

    + + +

    -
    -

    -'.sprintf(__('Users can register themselves or you can manually create users here.'), get_settings('siteurl').'/wp-register.php').'

    '; ?> -
    - - - - - - - - - - - - - - - - - - - - - - + + 'user_login', 'first_name' => 'user_firstname', 'last_name' => 'user_lastname', 'email' => 'user_email', 'url' => 'user_uri', 'role' => 'user_role') as $formpost => $var ) { + $var = 'new_' . $var; + $$var = wp_specialchars(stripslashes($_POST[$formpost])); + } + unset($name); + } ?> - - - - + +
    +

    +'.sprintf(__('Users can register themselves or you can manually create users here.'), get_settings('siteurl').'/wp-register.php').'

    '; ?> + + +
    -
    -
    -
    + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + +
    +
    +
    +
    +

    + + +

    +
    + + +
    +
      + get_error_messages() as $message ) + echo "$message
      "; + ?> +
    +
    - - - - - -

    - -

    -
    - +?> \ No newline at end of file diff --git a/wp-admin/wp-admin.css b/wp-admin/wp-admin.css index b73b73ec0..eb7052dd6 100644 --- a/wp-admin/wp-admin.css +++ b/wp-admin/wp-admin.css @@ -52,7 +52,7 @@ a.delete:hover { font-size: 16px; } -thead { +thead, .thead { background: #dfdfdf } diff --git a/wp-includes/functions.php b/wp-includes/functions.php index 17fcfbd10..e3dae435f 100644 --- a/wp-includes/functions.php +++ b/wp-includes/functions.php @@ -809,17 +809,24 @@ add_query_arg(associative_array, oldquery_or_uri) function add_query_arg() { $ret = ''; if ( is_array(func_get_arg(0)) ) { - if ( @func_num_args() < 2 ) + if ( @func_num_args() < 2 || '' == @func_get_arg(1) ) $uri = $_SERVER['REQUEST_URI']; else $uri = @func_get_arg(1); } else { - if ( @func_num_args() < 3 ) + if ( @func_num_args() < 3 || '' == @func_get_arg(2) ) $uri = $_SERVER['REQUEST_URI']; else $uri = @func_get_arg(2); } + if ( preg_match('|^https?://|i', $uri, $matches) ) { + $protocol = $matches[0]; + $uri = substr($uri, strlen($protocol)); + } else { + $protocol = ''; + } + if ( strstr($uri, '?') ) { $parts = explode('?', $uri, 2); if ( 1 == count($parts) ) { @@ -829,8 +836,7 @@ function add_query_arg() { $base = $parts[0] . '?'; $query = $parts[1]; } - } - else if ( strstr($uri, '/') ) { + } else if ( strstr($uri, '/') ) { $base = $uri . '?'; $query = ''; } else { @@ -853,11 +859,28 @@ function add_query_arg() { $ret .= "$k=$v"; } } - $ret = $base . $ret; + $ret = $protocol . $base . $ret; + if ( get_magic_quotes_gpc() ) + $ret = stripslashes($ret); // parse_str() adds slashes if magicquotes is on. See: http://php.net/parse_str return trim($ret, '?'); } -function remove_query_arg($key, $query) { +/* +remove_query_arg: Returns a modified querystring by removing +a single key or an array of keys. +Omitting oldquery_or_uri uses the $_SERVER value. + +Parameters: +remove_query_arg(removekey, [oldquery_or_uri]) or +remove_query_arg(removekeyarray, [oldquery_or_uri]) +*/ + +function remove_query_arg($key, $query='') { + if ( is_array($key) ) { // removing multiple keys + foreach ( (array) $key as $k ) + $query = add_query_arg($k, '', $query); + return $query; + } return add_query_arg($key, '', $query); }