<!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;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') . ' &lt;strong&gt;*&lt;/strong&gt;', '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-&gt;user_login;
</span><span class="cx">                         $user_id = $user_details-&gt;ID;
</span><del>-                        if ( ($username != null &amp;&amp; is_site_admin( $username ) == false ) &amp;&amp; ( array_key_exists($blog_id, get_blogs_of_user($user_id)) ) ) {
</del><ins>+                        if ( ( $username != null &amp;&amp; !is_super_admin( $user_id ) ) &amp;&amp; ( array_key_exists($blog_id, get_blogs_of_user($user_id)) ) ) {
</ins><span class="cx">                                 $redirect = add_query_arg( array('update' =&gt; 'addexisting'), 'user-new.php' );
</span><span class="cx">                         } else {
</span><del>-                                if ( isset( $_POST[ 'noconfirmation' ] ) &amp;&amp; is_site_admin() ) {
</del><ins>+                                if ( isset( $_POST[ 'noconfirmation' ] ) &amp;&amp; is_super_admin() ) {
</ins><span class="cx">                                         add_existing_user_to_blog( array( 'user_id' =&gt; $user_id, 'role' =&gt; $_REQUEST[ 'role' ] ) );
</span><span class="cx">                                         $redirect = add_query_arg( array('update' =&gt; '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"> ?&gt;
</span><span class="cx"> &lt;table class=&quot;form-table&quot;&gt;
</span><del>-&lt;?php if ( !is_multisite() ) { ?&gt;
</del><span class="cx">         &lt;tr class=&quot;form-field form-required&quot;&gt;
</span><span class="cx">                 &lt;th scope=&quot;row&quot;&gt;&lt;label for=&quot;user_login&quot;&gt;&lt;?php _e('Username'); ?&gt; &lt;span class=&quot;description&quot;&gt;&lt;?php _e('(required)'); ?&gt;&lt;/span&gt;&lt;/label&gt;
</span><span class="cx">                 &lt;input name=&quot;action&quot; type=&quot;hidden&quot; id=&quot;action&quot; value=&quot;adduser&quot; /&gt;&lt;/th&gt;
</span><span class="cx">                 &lt;td&gt;&lt;input name=&quot;user_login&quot; type=&quot;text&quot; id=&quot;user_login&quot; value=&quot;&lt;?php echo esc_attr($new_user_login); ?&gt;&quot; aria-required=&quot;true&quot; /&gt;&lt;/td&gt;
</span><span class="cx">         &lt;/tr&gt;
</span><ins>+        &lt;tr class=&quot;form-field form-required&quot;&gt;
+                &lt;th scope=&quot;row&quot;&gt;&lt;label for=&quot;email&quot;&gt;&lt;?php _e('E-mail'); ?&gt; &lt;span class=&quot;description&quot;&gt;&lt;?php _e('(required)'); ?&gt;&lt;/span&gt;&lt;/label&gt;&lt;/th&gt;
+                &lt;td&gt;&lt;input name=&quot;email&quot; type=&quot;text&quot; id=&quot;email&quot; value=&quot;&lt;?php echo esc_attr($new_user_email); ?&gt;&quot; /&gt;&lt;/td&gt;
+        &lt;/tr&gt;
+&lt;?php if ( !is_multisite() ) { ?&gt;
</ins><span class="cx">         &lt;tr class=&quot;form-field&quot;&gt;
</span><span class="cx">                 &lt;th scope=&quot;row&quot;&gt;&lt;label for=&quot;first_name&quot;&gt;&lt;?php _e('First Name') ?&gt; &lt;/label&gt;&lt;/th&gt;
</span><span class="cx">                 &lt;td&gt;&lt;input name=&quot;first_name&quot; type=&quot;text&quot; id=&quot;first_name&quot; value=&quot;&lt;?php echo esc_attr($new_user_firstname); ?&gt;&quot; /&gt;&lt;/td&gt;
</span><span class="lines">@@ -198,15 +202,10 @@
</span><span class="cx">                 &lt;th scope=&quot;row&quot;&gt;&lt;label for=&quot;last_name&quot;&gt;&lt;?php _e('Last Name') ?&gt; &lt;/label&gt;&lt;/th&gt;
</span><span class="cx">                 &lt;td&gt;&lt;input name=&quot;last_name&quot; type=&quot;text&quot; id=&quot;last_name&quot; value=&quot;&lt;?php echo esc_attr($new_user_lastname); ?&gt;&quot; /&gt;&lt;/td&gt;
</span><span class="cx">         &lt;/tr&gt;
</span><del>-        &lt;tr class=&quot;form-field form-required&quot;&gt;
-                &lt;th scope=&quot;row&quot;&gt;&lt;label for=&quot;email&quot;&gt;&lt;?php _e('E-mail'); ?&gt; &lt;span class=&quot;description&quot;&gt;&lt;?php _e('(required)'); ?&gt;&lt;/span&gt;&lt;/label&gt;&lt;/th&gt;
-                &lt;td&gt;&lt;input name=&quot;email&quot; type=&quot;text&quot; id=&quot;email&quot; value=&quot;&lt;?php echo esc_attr($new_user_email); ?&gt;&quot; /&gt;&lt;/td&gt;
-        &lt;/tr&gt;
</del><span class="cx">         &lt;tr class=&quot;form-field&quot;&gt;
</span><span class="cx">                 &lt;th scope=&quot;row&quot;&gt;&lt;label for=&quot;url&quot;&gt;&lt;?php _e('Website') ?&gt;&lt;/label&gt;&lt;/th&gt;
</span><span class="cx">                 &lt;td&gt;&lt;input name=&quot;url&quot; type=&quot;text&quot; id=&quot;url&quot; class=&quot;code&quot; value=&quot;&lt;?php echo esc_attr($new_user_uri); ?&gt;&quot; /&gt;&lt;/td&gt;
</span><span class="cx">         &lt;/tr&gt;
</span><del>-
</del><span class="cx"> &lt;?php if ( apply_filters('show_password_fields', true) ) : ?&gt;
</span><span class="cx">         &lt;tr class=&quot;form-field form-required&quot;&gt;
</span><span class="cx">                 &lt;th scope=&quot;row&quot;&gt;&lt;label for=&quot;pass1&quot;&gt;&lt;?php _e('Password'); ?&gt; &lt;span class=&quot;description&quot;&gt;&lt;?php _e('(twice, required)'); ?&gt;&lt;/span&gt;&lt;/label&gt;&lt;/th&gt;
</span><span class="lines">@@ -223,16 +222,7 @@
</span><span class="cx">                 &lt;td&gt;&lt;label for=&quot;send_password&quot;&gt;&lt;input type=&quot;checkbox&quot; name=&quot;send_password&quot; id=&quot;send_password&quot; &lt;?php checked($new_user_send_password, true); ?&gt; /&gt; &lt;?php _e('Send this password to the new user by email.'); ?&gt;&lt;/label&gt;&lt;/td&gt;
</span><span class="cx">         &lt;/tr&gt;
</span><span class="cx"> &lt;?php endif; ?&gt;
</span><del>-&lt;?php } else { // multisite ?&gt;
-        &lt;tr class=&quot;form-field form-required&quot;&gt;
-                &lt;th scope=&quot;row&quot;&gt;&lt;label for=&quot;user_login&quot;&gt;&lt;?php _e('Username (required)') ?&gt;&lt;/label&gt;&lt;input name=&quot;action&quot; type=&quot;hidden&quot; id=&quot;action&quot; value=&quot;adduser&quot; /&gt;&lt;/th&gt;
-                &lt;td &gt;&lt;input name=&quot;user_login&quot; type=&quot;text&quot; id=&quot;user_login&quot; value=&quot;&lt;?php echo $new_user_login; ?&gt;&quot; aria-required=&quot;true&quot; /&gt;&lt;/td&gt;
-        &lt;/tr&gt;
-        &lt;tr class=&quot;form-field form-required&quot;&gt;
-                &lt;th scope=&quot;row&quot;&gt;&lt;label for=&quot;email&quot;&gt;&lt;?php _e('E-mail (required)') ?&gt;&lt;/label&gt;&lt;/th&gt;
-                &lt;td&gt;&lt;input name=&quot;email&quot; type=&quot;text&quot; id=&quot;email&quot; value=&quot;&lt;?php echo $new_user_email; ?&gt;&quot; /&gt;&lt;/td&gt;
-        &lt;/tr&gt;
-&lt;?php } ?&gt;
</del><ins>+&lt;?php } // !is_multisite ?&gt;
</ins><span class="cx">         &lt;tr class=&quot;form-field&quot;&gt;
</span><span class="cx">                 &lt;th scope=&quot;row&quot;&gt;&lt;label for=&quot;role&quot;&gt;&lt;?php _e('Role'); ?&gt;&lt;/label&gt;&lt;/th&gt;
</span><span class="cx">                 &lt;td&gt;&lt;select name=&quot;role&quot; id=&quot;role&quot;&gt;
</span><span class="lines">@@ -244,11 +234,10 @@
</span><span class="cx">                         &lt;/select&gt;
</span><span class="cx">                 &lt;/td&gt;
</span><span class="cx">         &lt;/tr&gt;
</span><del>-
</del><span class="cx">         &lt;?php if ( is_multisite() &amp;&amp; is_super_admin() ) { ?&gt;
</span><del>-        &lt;tr class=&quot;form-field&quot;&gt;
</del><ins>+        &lt;tr&gt;
</ins><span class="cx">                 &lt;th scope=&quot;row&quot;&gt;&lt;label for=&quot;noconfirmation&quot;&gt;&lt;?php _e('Skip Confirmation Email') ?&gt;&lt;/label&gt;&lt;/th&gt;
</span><del>-                &lt;td&gt;&lt;input name=&quot;noconfirmation&quot; type=&quot;checkbox&quot; id=&quot;noconfirmation&quot; value=&quot;1&quot; /&gt; &lt;label for=&quot;noconfirmation&quot;&gt;&lt;?php _e( 'Site administrators can add a user without sending the confirmation email.' ); ?&gt;&lt;/label&gt;&lt;/td&gt;
</del><ins>+                &lt;td&gt;&lt;label for=&quot;noconfirmation&quot;&gt;&lt;input type=&quot;checkbox&quot; name=&quot;noconfirmation&quot; id=&quot;noconfirmation&quot; value=&quot;1&quot; /&gt; &lt;?php _e( 'Site administrators can add a user without sending the confirmation email.' ); ?&gt;&lt;/label&gt;&lt;/td&gt;
</ins><span class="cx">         &lt;/tr&gt;
</span><span class="cx">         &lt;?php } ?&gt;
</span><span class="cx"> &lt;/table&gt;
</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() &amp;&amp; !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( &quot;/\s+/&quot;, '', 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 &amp;&amp; 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 &amp;&amp; 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>