<!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>[12778] trunk: Add email and login dupe checking down into wp_insert_user()
.</title>
</head>
<body>
<div id="msg">
<dl>
<dt>Revision</dt> <dd><a href="http://trac.wordpress.org/changeset/12778">12778</a></dd>
<dt>Author</dt> <dd>ryan</dd>
<dt>Date</dt> <dd>2010-01-20 21:58:13 +0000 (Wed, 20 Jan 2010)</dd>
</dl>
<h3>Log Message</h3>
<pre>Add email and login dupe checking down into wp_insert_user(). Tidy up user-new.php. see <a href="http://trac.wordpress.org/ticket/11644">#11644</a></pre>
<h3>Modified Paths</h3>
<ul>
<li><a href="#trunkwpadminmenuphp">trunk/wp-admin/menu.php</a></li>
<li><a href="#trunkwpadminusernewphp">trunk/wp-admin/user-new.php</a></li>
<li><a href="#trunkwpincludescapabilitiesphp">trunk/wp-includes/capabilities.php</a></li>
<li><a href="#trunkwpincludesmsfunctionsphp">trunk/wp-includes/ms-functions.php</a></li>
<li><a href="#trunkwpincludesregistrationphp">trunk/wp-includes/registration.php</a></li>
</ul>
</div>
<div id="patch">
<h3>Diff</h3>
<a id="trunkwpadminmenuphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/menu.php (12777 => 12778)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/menu.php        2010-01-20 20:42:20 UTC (rev 12777)
+++ trunk/wp-admin/menu.php        2010-01-20 21:58:13 UTC (rev 12778)
</span><span class="lines">@@ -130,10 +130,7 @@
</span><span class="cx"> if ( current_user_can('edit_users') ) {
</span><span class="cx">         $_wp_real_parent_file['profile.php'] = 'users.php'; // Back-compat for plugins adding submenus to profile.php.
</span><span class="cx">         $submenu['users.php'][5] = array(__('Authors &amp; Users'), 'edit_users', 'users.php');
</span><del>-        if ( !is_multisite() )
-                $submenu['users.php'][10] = array(_x('Add New', 'user'), 'create_users', 'user-new.php');
-        elseif ( is_super_admin() || get_site_option( 'add_new_users' ) )
-                $submenu['users.php'][10] = array(__('Add New') . ' <strong>*</strong>', 'create_users', 'ms-options.php#addnewusers');
</del><ins>+        $submenu['users.php'][10] = array(_x('Add New', 'user'), 'create_users', 'user-new.php');
</ins><span class="cx">
</span><span class="cx">         $submenu['users.php'][15] = array(__('Your Profile'), 'read', 'profile.php');
</span><span class="cx"> } else {
</span></span></pre></div>
<a id="trunkwpadminusernewphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-admin/user-new.php (12777 => 12778)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-admin/user-new.php        2010-01-20 20:42:20 UTC (rev 12777)
+++ trunk/wp-admin/user-new.php        2010-01-20 21:58:13 UTC (rev 12778)
</span><span class="lines">@@ -62,10 +62,10 @@
</span><span class="cx">                         $redirect = 'user-new.php';
</span><span class="cx">                         $username = $user_details->user_login;
</span><span class="cx">                         $user_id = $user_details->ID;
</span><del>-                        if ( ($username != null && is_site_admin( $username ) == false ) && ( array_key_exists($blog_id, get_blogs_of_user($user_id)) ) ) {
</del><ins>+                        if ( ( $username != null && !is_super_admin( $user_id ) ) && ( array_key_exists($blog_id, get_blogs_of_user($user_id)) ) ) {
</ins><span class="cx">                                 $redirect = add_query_arg( array('update' => 'addexisting'), 'user-new.php' );
</span><span class="cx">                         } else {
</span><del>-                                if ( isset( $_POST[ 'noconfirmation' ] ) && is_site_admin() ) {
</del><ins>+                                if ( isset( $_POST[ 'noconfirmation' ] ) && is_super_admin() ) {
</ins><span class="cx">                                         add_existing_user_to_blog( array( 'user_id' => $user_id, 'role' => $_REQUEST[ 'role' ] ) );
</span><span class="cx">                                         $redirect = add_query_arg( array('update' => 'addnoconfirmation'), 'user-new.php' );
</span><span class="cx">                                 } else {
</span><span class="lines">@@ -184,12 +184,16 @@
</span><span class="cx"> $new_user_send_password = !$_POST || isset($_POST['send_password']);
</span><span class="cx"> ?>
</span><span class="cx"> <table class="form-table">
</span><del>-<?php if ( !is_multisite() ) { ?>
</del><span class="cx">         <tr class="form-field form-required">
</span><span class="cx">                 <th scope="row"><label for="user_login"><?php _e('Username'); ?> <span class="description"><?php _e('(required)'); ?></span></label>
</span><span class="cx">                 <input name="action" type="hidden" id="action" value="adduser" /></th>
</span><span class="cx">                 <td><input name="user_login" type="text" id="user_login" value="<?php echo esc_attr($new_user_login); ?>" aria-required="true" /></td>
</span><span class="cx">         </tr>
</span><ins>+        <tr class="form-field form-required">
+                <th scope="row"><label for="email"><?php _e('E-mail'); ?> <span class="description"><?php _e('(required)'); ?></span></label></th>
+                <td><input name="email" type="text" id="email" value="<?php echo esc_attr($new_user_email); ?>" /></td>
+        </tr>
+<?php if ( !is_multisite() ) { ?>
</ins><span class="cx">         <tr class="form-field">
</span><span class="cx">                 <th scope="row"><label for="first_name"><?php _e('First Name') ?> </label></th>
</span><span class="cx">                 <td><input name="first_name" type="text" id="first_name" value="<?php echo esc_attr($new_user_firstname); ?>" /></td>
</span><span class="lines">@@ -198,15 +202,10 @@
</span><span class="cx">                 <th scope="row"><label for="last_name"><?php _e('Last Name') ?> </label></th>
</span><span class="cx">                 <td><input name="last_name" type="text" id="last_name" value="<?php echo esc_attr($new_user_lastname); ?>" /></td>
</span><span class="cx">         </tr>
</span><del>-        <tr class="form-field form-required">
-                <th scope="row"><label for="email"><?php _e('E-mail'); ?> <span class="description"><?php _e('(required)'); ?></span></label></th>
-                <td><input name="email" type="text" id="email" value="<?php echo esc_attr($new_user_email); ?>" /></td>
-        </tr>
</del><span class="cx">         <tr class="form-field">
</span><span class="cx">                 <th scope="row"><label for="url"><?php _e('Website') ?></label></th>
</span><span class="cx">                 <td><input name="url" type="text" id="url" class="code" value="<?php echo esc_attr($new_user_uri); ?>" /></td>
</span><span class="cx">         </tr>
</span><del>-
</del><span class="cx"> <?php if ( apply_filters('show_password_fields', true) ) : ?>
</span><span class="cx">         <tr class="form-field form-required">
</span><span class="cx">                 <th scope="row"><label for="pass1"><?php _e('Password'); ?> <span class="description"><?php _e('(twice, required)'); ?></span></label></th>
</span><span class="lines">@@ -223,16 +222,7 @@
</span><span class="cx">                 <td><label for="send_password"><input type="checkbox" name="send_password" id="send_password" <?php checked($new_user_send_password, true); ?> /> <?php _e('Send this password to the new user by email.'); ?></label></td>
</span><span class="cx">         </tr>
</span><span class="cx"> <?php endif; ?>
</span><del>-<?php } else { // multisite ?>
-        <tr class="form-field form-required">
-                <th scope="row"><label for="user_login"><?php _e('Username (required)') ?></label><input name="action" type="hidden" id="action" value="adduser" /></th>
-                <td ><input name="user_login" type="text" id="user_login" value="<?php echo $new_user_login; ?>" aria-required="true" /></td>
-        </tr>
-        <tr class="form-field form-required">
-                <th scope="row"><label for="email"><?php _e('E-mail (required)') ?></label></th>
-                <td><input name="email" type="text" id="email" value="<?php echo $new_user_email; ?>" /></td>
-        </tr>
-<?php } ?>
</del><ins>+<?php } // !is_multisite ?>
</ins><span class="cx">         <tr class="form-field">
</span><span class="cx">                 <th scope="row"><label for="role"><?php _e('Role'); ?></label></th>
</span><span class="cx">                 <td><select name="role" id="role">
</span><span class="lines">@@ -244,11 +234,10 @@
</span><span class="cx">                         </select>
</span><span class="cx">                 </td>
</span><span class="cx">         </tr>
</span><del>-
</del><span class="cx">         <?php if ( is_multisite() && is_super_admin() ) { ?>
</span><del>-        <tr class="form-field">
</del><ins>+        <tr>
</ins><span class="cx">                 <th scope="row"><label for="noconfirmation"><?php _e('Skip Confirmation Email') ?></label></th>
</span><del>-                <td><input name="noconfirmation" type="checkbox" id="noconfirmation" value="1" /> <label for="noconfirmation"><?php _e( 'Site administrators can add a user without sending the confirmation email.' ); ?></label></td>
</del><ins>+                <td><label for="noconfirmation"><input type="checkbox" name="noconfirmation" id="noconfirmation" value="1" /> <?php _e( 'Site administrators can add a user without sending the confirmation email.' ); ?></label></td>
</ins><span class="cx">         </tr>
</span><span class="cx">         <?php } ?>
</span><span class="cx"> </table>
</span></span></pre></div>
<a id="trunkwpincludescapabilitiesphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/capabilities.php (12777 => 12778)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/capabilities.php        2010-01-20 20:42:20 UTC (rev 12777)
+++ trunk/wp-includes/capabilities.php        2010-01-20 21:58:13 UTC (rev 12778)
</span><span class="lines">@@ -980,6 +980,12 @@
</span><span class="cx">                 else
</span><span class="cx">                         $caps[] = $cap;
</span><span class="cx">                 break;
</span><ins>+        case 'create_users':
+                if ( is_multisite() && !get_site_option( 'add_new_users' ) )
+                        $caps[] = 'do_not_allow';
+                else
+                        $caps[] = $cap;
+                break;
</ins><span class="cx">         default:
</span><span class="cx">                 // If no meta caps match, return the original cap.
</span><span class="cx">                 $caps[] = $cap;
</span></span></pre></div>
<a id="trunkwpincludesmsfunctionsphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/ms-functions.php (12777 => 12778)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/ms-functions.php        2010-01-20 20:42:20 UTC (rev 12777)
+++ trunk/wp-includes/ms-functions.php        2010-01-20 21:58:13 UTC (rev 12778)
</span><span class="lines">@@ -1222,14 +1222,11 @@
</span><span class="cx">
</span><span class="cx"> function wpmu_create_user( $user_name, $password, $email) {
</span><span class="cx">         $user_name = preg_replace( "/\s+/", '', sanitize_user( $user_name, true ) );
</span><del>-        if ( username_exists($user_name) )
-                return false;
</del><span class="cx">
</span><del>-        // Check if the email address has been used already.
-        if ( email_exists($email) )
</del><ins>+        $user_id = wp_create_user( $user_name, $password, $email );
+        if ( is_wp_error($user_id) )
</ins><span class="cx">                 return false;
</span><span class="cx">
</span><del>-        $user_id = wp_create_user( $user_name, $password, $email );
</del><span class="cx">         $user = new WP_User($user_id);
</span><span class="cx">
</span><span class="cx">         // Newly created users have no roles or caps until they are added to a blog.
</span></span></pre></div>
<a id="trunkwpincludesregistrationphp"></a>
<div class="modfile"><h4>Modified: trunk/wp-includes/registration.php (12777 => 12778)</h4>
<pre class="diff"><span>
<span class="info">--- trunk/wp-includes/registration.php        2010-01-20 20:42:20 UTC (rev 12777)
+++ trunk/wp-includes/registration.php        2010-01-20 21:58:13 UTC (rev 12778)
</span><span class="lines">@@ -122,10 +122,12 @@
</span><span class="cx">         //Remove any non-printable chars from the login string to see if we have ended up with an empty username
</span><span class="cx">         $user_login = trim($user_login);
</span><span class="cx">
</span><del>-        if ( empty($user_login) ) {
</del><ins>+        if ( empty($user_login) )
</ins><span class="cx">                 return new WP_Error('empty_user_login', __('Cannot create a user with an empty login name.') );
</span><del>-        }
</del><span class="cx">
</span><ins>+        if ( !$update && username_exists( $user_login ) )
+                return new WP_Error('existing_user_login', __('This username is already registered.') );
+
</ins><span class="cx">         if ( empty($user_nicename) )
</span><span class="cx">                 $user_nicename = sanitize_title( $user_login );
</span><span class="cx">         $user_nicename = apply_filters('pre_user_nicename', $user_nicename);
</span><span class="lines">@@ -138,6 +140,9 @@
</span><span class="cx">                 $user_email = '';
</span><span class="cx">         $user_email = apply_filters('pre_user_email', $user_email);
</span><span class="cx">
</span><ins>+        if ( !$update && email_exists($user_email) )
+                return new WP_Error('existing_user_email', __('This email address is already registered.') );
+
</ins><span class="cx">         if ( empty($display_name) )
</span><span class="cx">                 $display_name = $user_login;
</span><span class="cx">         $display_name = apply_filters('pre_user_display_name', $display_name);
</span></span></pre>
</div>
</div>
</body>
</html>