Ticket #20279: 20279.diff
| File 20279.diff, 24.3 KB (added by , 14 years ago) |
|---|
-
wp-login.php
22 22 } 23 23 } 24 24 25 /** 26 * Outputs the header for the login page. 27 * 28 * @uses do_action() Calls the 'login_head' for outputting HTML in the Log In 29 * header. 30 * @uses apply_filters() Calls 'login_headerurl' for the top login link. 31 * @uses apply_filters() Calls 'login_headertitle' for the top login title. 32 * @uses apply_filters() Calls 'login_message' on the message to display in the 33 * header. 34 * @uses $error The error global, which is checked for displaying errors. 35 * 36 * @param string $title Optional. WordPress Log In Page title to display in 37 * <title/> element. 38 * @param string $message Optional. Message to display in header. 39 * @param WP_Error $wp_error Optional. WordPress Error Object 40 */ 41 function login_header($title = 'Log In', $message = '', $wp_error = '') { 42 global $error, $is_iphone, $interim_login, $current_site; 25 require ABSPATH . WPINC . '/functions.wp-login.php'; 43 26 44 // Don't index any of these forms45 add_action( 'login_head', 'wp_no_robots' );46 47 if ( empty($wp_error) )48 $wp_error = new WP_Error();49 50 // Shake it!51 $shake_error_codes = array( 'empty_password', 'empty_email', 'invalid_email', 'invalidcombo', 'empty_username', 'invalid_username', 'incorrect_password' );52 $shake_error_codes = apply_filters( 'shake_error_codes', $shake_error_codes );53 54 if ( $shake_error_codes && $wp_error->get_error_code() && in_array( $wp_error->get_error_code(), $shake_error_codes ) )55 add_action( 'login_head', 'wp_shake_js', 12 );56 57 ?>58 <!DOCTYPE html>59 <html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>>60 <head>61 <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" />62 <title><?php bloginfo('name'); ?> › <?php echo $title; ?></title>63 <?php64 wp_admin_css( 'wp-admin', true );65 wp_admin_css( 'colors-fresh', true );66 67 if ( $is_iphone ) { ?>68 <meta name="viewport" content="width=320; initial-scale=0.9; maximum-scale=1.0; user-scalable=0;" />69 <style type="text/css" media="screen">70 .login form, .login .message, #login_error { margin-left: 0px; }71 .login #nav, .login #backtoblog { margin-left: 8px; }72 .login h1 a { width: auto; }73 #login { padding: 20px 0; }74 </style>75 <?php76 }77 78 do_action( 'login_enqueue_scripts' );79 do_action( 'login_head' );80 81 if ( is_multisite() ) {82 $login_header_url = network_home_url();83 $login_header_title = $current_site->site_name;84 } else {85 $login_header_url = __( 'http://wordpress.org/' );86 $login_header_title = __( 'Powered by WordPress' );87 }88 89 $login_header_url = apply_filters( 'login_headerurl', $login_header_url );90 $login_header_title = apply_filters( 'login_headertitle', $login_header_title );91 ?>92 </head>93 <body class="login">94 <div id="login">95 <h1><a href="<?php echo esc_url( $login_header_url ); ?>" title="<?php echo esc_attr( $login_header_title ); ?>"><?php bloginfo( 'name' ); ?></a></h1>96 <?php97 unset( $login_header_url, $login_header_title );98 99 $message = apply_filters('login_message', $message);100 if ( !empty( $message ) ) echo $message . "\n";101 102 // In case a plugin uses $error rather than the $wp_errors object103 if ( !empty( $error ) ) {104 $wp_error->add('error', $error);105 unset($error);106 }107 108 if ( $wp_error->get_error_code() ) {109 $errors = '';110 $messages = '';111 foreach ( $wp_error->get_error_codes() as $code ) {112 $severity = $wp_error->get_error_data($code);113 foreach ( $wp_error->get_error_messages($code) as $error ) {114 if ( 'message' == $severity )115 $messages .= ' ' . $error . "<br />\n";116 else117 $errors .= ' ' . $error . "<br />\n";118 }119 }120 if ( !empty($errors) )121 echo '<div id="login_error">' . apply_filters('login_errors', $errors) . "</div>\n";122 if ( !empty($messages) )123 echo '<p class="message">' . apply_filters('login_messages', $messages) . "</p>\n";124 }125 } // End of login_header()126 127 /**128 * Outputs the footer for the login page.129 *130 * @param string $input_id Which input to auto-focus131 */132 function login_footer($input_id = '') {133 ?>134 <p id="backtoblog"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" title="<?php esc_attr_e( 'Are you lost?' ); ?>"><?php printf( __( '← Back to %s' ), get_bloginfo( 'title', 'display' ) ); ?></a></p>135 </div>136 137 <?php if ( !empty($input_id) ) : ?>138 <script type="text/javascript">139 try{document.getElementById('<?php echo $input_id; ?>').focus();}catch(e){}140 if(typeof wpOnload=='function')wpOnload();141 </script>142 <?php endif; ?>143 144 <?php do_action('login_footer'); ?>145 <div class="clear"></div>146 </body>147 </html>148 <?php149 }150 151 function wp_shake_js() {152 global $is_iphone;153 if ( $is_iphone )154 return;155 ?>156 <script type="text/javascript">157 addLoadEvent = function(func){if(typeof jQuery!="undefined")jQuery(document).ready(func);else if(typeof wpOnload!='function'){wpOnload=func;}else{var oldonload=wpOnload;wpOnload=function(){oldonload();func();}}};158 function s(id,pos){g(id).left=pos+'px';}159 function g(id){return document.getElementById(id).style;}160 function shake(id,a,d){c=a.shift();s(id,c);if(a.length>0){setTimeout(function(){shake(id,a,d);},d);}else{try{g(id).position='static';wp_attempt_focus();}catch(e){}}}161 addLoadEvent(function(){ var p=new Array(15,30,15,0,-15,-30,-15,0);p=p.concat(p.concat(p));var i=document.forms[0].id;g(i).position='relative';shake(i,p,20);});162 </script>163 <?php164 }165 166 /**167 * Handles sending password retrieval email to user.168 *169 * @uses $wpdb WordPress Database object170 *171 * @return bool|WP_Error True: when finish. WP_Error on error172 */173 function retrieve_password() {174 global $wpdb, $current_site;175 176 $errors = new WP_Error();177 178 if ( empty( $_POST['user_login'] ) ) {179 $errors->add('empty_username', __('<strong>ERROR</strong>: Enter a username or e-mail address.'));180 } else if ( strpos( $_POST['user_login'], '@' ) ) {181 $user_data = get_user_by( 'email', trim( $_POST['user_login'] ) );182 if ( empty( $user_data ) )183 $errors->add('invalid_email', __('<strong>ERROR</strong>: There is no user registered with that email address.'));184 } else {185 $login = trim($_POST['user_login']);186 $user_data = get_user_by('login', $login);187 }188 189 do_action('lostpassword_post');190 191 if ( $errors->get_error_code() )192 return $errors;193 194 if ( !$user_data ) {195 $errors->add('invalidcombo', __('<strong>ERROR</strong>: Invalid username or e-mail.'));196 return $errors;197 }198 199 // redefining user_login ensures we return the right case in the email200 $user_login = $user_data->user_login;201 $user_email = $user_data->user_email;202 203 do_action('retreive_password', $user_login); // Misspelled and deprecated204 do_action('retrieve_password', $user_login);205 206 $allow = apply_filters('allow_password_reset', true, $user_data->ID);207 208 if ( ! $allow )209 return new WP_Error('no_password_reset', __('Password reset is not allowed for this user'));210 else if ( is_wp_error($allow) )211 return $allow;212 213 $key = $wpdb->get_var($wpdb->prepare("SELECT user_activation_key FROM $wpdb->users WHERE user_login = %s", $user_login));214 if ( empty($key) ) {215 // Generate something random for a key...216 $key = wp_generate_password(20, false);217 do_action('retrieve_password_key', $user_login, $key);218 // Now insert the new md5 key into the db219 $wpdb->update($wpdb->users, array('user_activation_key' => $key), array('user_login' => $user_login));220 }221 $message = __('Someone requested that the password be reset for the following account:') . "\r\n\r\n";222 $message .= network_home_url( '/' ) . "\r\n\r\n";223 $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n";224 $message .= __('If this was a mistake, just ignore this email and nothing will happen.') . "\r\n\r\n";225 $message .= __('To reset your password, visit the following address:') . "\r\n\r\n";226 $message .= '<' . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login') . ">\r\n";227 228 if ( is_multisite() )229 $blogname = $GLOBALS['current_site']->site_name;230 else231 // The blogname option is escaped with esc_html on the way into the database in sanitize_option232 // we want to reverse this for the plain text arena of emails.233 $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES);234 235 $title = sprintf( __('[%s] Password Reset'), $blogname );236 237 $title = apply_filters('retrieve_password_title', $title);238 $message = apply_filters('retrieve_password_message', $message, $key);239 240 if ( $message && !wp_mail($user_email, $title, $message) )241 wp_die( __('The e-mail could not be sent.') . "<br />\n" . __('Possible reason: your host may have disabled the mail() function...') );242 243 return true;244 }245 246 /**247 * Retrieves a user row based on password reset key and login248 *249 * @uses $wpdb WordPress Database object250 *251 * @param string $key Hash to validate sending user's password252 * @param string $login The user login253 * @return object|WP_Error User's database row on success, error object for invalid keys254 */255 function check_password_reset_key($key, $login) {256 global $wpdb;257 258 $key = preg_replace('/[^a-z0-9]/i', '', $key);259 260 if ( empty( $key ) || !is_string( $key ) )261 return new WP_Error('invalid_key', __('Invalid key'));262 263 if ( empty($login) || !is_string($login) )264 return new WP_Error('invalid_key', __('Invalid key'));265 266 $user = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->users WHERE user_activation_key = %s AND user_login = %s", $key, $login));267 268 if ( empty( $user ) )269 return new WP_Error('invalid_key', __('Invalid key'));270 271 return $user;272 }273 274 /**275 * Handles resetting the user's password.276 *277 * @param object $user The user278 * @param string $new_pass New password for the user in plaintext279 */280 function reset_password($user, $new_pass) {281 do_action('password_reset', $user, $new_pass);282 283 wp_set_password($new_pass, $user->ID);284 285 wp_password_change_notification($user);286 }287 288 /**289 * Handles registering a new user.290 *291 * @param string $user_login User's username for logging in292 * @param string $user_email User's email address to send password and add293 * @return int|WP_Error Either user's ID or error on failure.294 */295 function register_new_user( $user_login, $user_email ) {296 $errors = new WP_Error();297 298 $sanitized_user_login = sanitize_user( $user_login );299 $user_email = apply_filters( 'user_registration_email', $user_email );300 301 // Check the username302 if ( $sanitized_user_login == '' ) {303 $errors->add( 'empty_username', __( '<strong>ERROR</strong>: Please enter a username.' ) );304 } elseif ( ! validate_username( $user_login ) ) {305 $errors->add( 'invalid_username', __( '<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' ) );306 $sanitized_user_login = '';307 } elseif ( username_exists( $sanitized_user_login ) ) {308 $errors->add( 'username_exists', __( '<strong>ERROR</strong>: This username is already registered, please choose another one.' ) );309 }310 311 // Check the e-mail address312 if ( $user_email == '' ) {313 $errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please type your e-mail address.' ) );314 } elseif ( ! is_email( $user_email ) ) {315 $errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The email address isn’t correct.' ) );316 $user_email = '';317 } elseif ( email_exists( $user_email ) ) {318 $errors->add( 'email_exists', __( '<strong>ERROR</strong>: This email is already registered, please choose another one.' ) );319 }320 321 do_action( 'register_post', $sanitized_user_login, $user_email, $errors );322 323 $errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email );324 325 if ( $errors->get_error_code() )326 return $errors;327 328 $user_pass = wp_generate_password( 12, false);329 $user_id = wp_create_user( $sanitized_user_login, $user_pass, $user_email );330 if ( ! $user_id ) {331 $errors->add( 'registerfail', sprintf( __( '<strong>ERROR</strong>: Couldn’t register you... please contact the <a href="mailto:%s">webmaster</a> !' ), get_option( 'admin_email' ) ) );332 return $errors;333 }334 335 update_user_option( $user_id, 'default_password_nag', true, true ); //Set up the Password change nag.336 337 wp_new_user_notification( $user_id, $user_pass );338 339 return $user_id;340 }341 342 27 // 343 28 // Main 344 29 // -
wp-includes/functions.wp-login.php
1 <?php 2 3 /** 4 * Outputs the header for the login page. 5 * 6 * @uses do_action() Calls the 'login_head' for outputting HTML in the Log In 7 * header. 8 * @uses apply_filters() Calls 'login_headerurl' for the top login link. 9 * @uses apply_filters() Calls 'login_headertitle' for the top login title. 10 * @uses apply_filters() Calls 'login_message' on the message to display in the 11 * header. 12 * @uses $error The error global, which is checked for displaying errors. 13 * 14 * @param string $title Optional. WordPress Log In Page title to display in 15 * <title/> element. 16 * @param string $message Optional. Message to display in header. 17 * @param WP_Error $wp_error Optional. WordPress Error Object 18 */ 19 function login_header($title = 'Log In', $message = '', $wp_error = '') { 20 global $error, $is_iphone, $interim_login, $current_site; 21 22 // Don't index any of these forms 23 add_action( 'login_head', 'wp_no_robots' ); 24 25 if ( empty($wp_error) ) 26 $wp_error = new WP_Error(); 27 28 // Shake it! 29 $shake_error_codes = array( 'empty_password', 'empty_email', 'invalid_email', 'invalidcombo', 'empty_username', 'invalid_username', 'incorrect_password' ); 30 $shake_error_codes = apply_filters( 'shake_error_codes', $shake_error_codes ); 31 32 if ( $shake_error_codes && $wp_error->get_error_code() && in_array( $wp_error->get_error_code(), $shake_error_codes ) ) 33 add_action( 'login_head', 'wp_shake_js', 12 ); 34 35 ?> 36 <!DOCTYPE html> 37 <html xmlns="http://www.w3.org/1999/xhtml" <?php language_attributes(); ?>> 38 <head> 39 <meta http-equiv="Content-Type" content="<?php bloginfo('html_type'); ?>; charset=<?php bloginfo('charset'); ?>" /> 40 <title><?php bloginfo('name'); ?> › <?php echo $title; ?></title> 41 <?php 42 wp_admin_css( 'wp-admin', true ); 43 wp_admin_css( 'colors-fresh', true ); 44 45 if ( $is_iphone ) { ?> 46 <meta name="viewport" content="width=320; initial-scale=0.9; maximum-scale=1.0; user-scalable=0;" /> 47 <style type="text/css" media="screen"> 48 .login form, .login .message, #login_error { margin-left: 0px; } 49 .login #nav, .login #backtoblog { margin-left: 8px; } 50 .login h1 a { width: auto; } 51 #login { padding: 20px 0; } 52 </style> 53 <?php 54 } 55 56 do_action( 'login_enqueue_scripts' ); 57 do_action( 'login_head' ); 58 59 if ( is_multisite() ) { 60 $login_header_url = network_home_url(); 61 $login_header_title = $current_site->site_name; 62 } else { 63 $login_header_url = __( 'http://wordpress.org/' ); 64 $login_header_title = __( 'Powered by WordPress' ); 65 } 66 67 $login_header_url = apply_filters( 'login_headerurl', $login_header_url ); 68 $login_header_title = apply_filters( 'login_headertitle', $login_header_title ); 69 ?> 70 </head> 71 <body class="login"> 72 <div id="login"> 73 <h1><a href="<?php echo esc_url( $login_header_url ); ?>" title="<?php echo esc_attr( $login_header_title ); ?>"><?php bloginfo( 'name' ); ?></a></h1> 74 <?php 75 unset( $login_header_url, $login_header_title ); 76 77 $message = apply_filters('login_message', $message); 78 if ( !empty( $message ) ) echo $message . "\n"; 79 80 // In case a plugin uses $error rather than the $wp_errors object 81 if ( !empty( $error ) ) { 82 $wp_error->add('error', $error); 83 unset($error); 84 } 85 86 if ( $wp_error->get_error_code() ) { 87 $errors = ''; 88 $messages = ''; 89 foreach ( $wp_error->get_error_codes() as $code ) { 90 $severity = $wp_error->get_error_data($code); 91 foreach ( $wp_error->get_error_messages($code) as $error ) { 92 if ( 'message' == $severity ) 93 $messages .= ' ' . $error . "<br />\n"; 94 else 95 $errors .= ' ' . $error . "<br />\n"; 96 } 97 } 98 if ( !empty($errors) ) 99 echo '<div id="login_error">' . apply_filters('login_errors', $errors) . "</div>\n"; 100 if ( !empty($messages) ) 101 echo '<p class="message">' . apply_filters('login_messages', $messages) . "</p>\n"; 102 } 103 } // End of login_header() 104 105 /** 106 * Outputs the footer for the login page. 107 * 108 * @param string $input_id Which input to auto-focus 109 */ 110 function login_footer($input_id = '') { 111 ?> 112 <p id="backtoblog"><a href="<?php echo esc_url( home_url( '/' ) ); ?>" title="<?php esc_attr_e( 'Are you lost?' ); ?>"><?php printf( __( '← Back to %s' ), get_bloginfo( 'title', 'display' ) ); ?></a></p> 113 </div> 114 115 <?php if ( !empty($input_id) ) : ?> 116 <script type="text/javascript"> 117 try{document.getElementById('<?php echo $input_id; ?>').focus();}catch(e){} 118 if(typeof wpOnload=='function')wpOnload(); 119 </script> 120 <?php endif; ?> 121 122 <?php do_action('login_footer'); ?> 123 <div class="clear"></div> 124 </body> 125 </html> 126 <?php 127 } 128 129 function wp_shake_js() { 130 global $is_iphone; 131 if ( $is_iphone ) 132 return; 133 ?> 134 <script type="text/javascript"> 135 addLoadEvent = function(func){if(typeof jQuery!="undefined")jQuery(document).ready(func);else if(typeof wpOnload!='function'){wpOnload=func;}else{var oldonload=wpOnload;wpOnload=function(){oldonload();func();}}}; 136 function s(id,pos){g(id).left=pos+'px';} 137 function g(id){return document.getElementById(id).style;} 138 function shake(id,a,d){c=a.shift();s(id,c);if(a.length>0){setTimeout(function(){shake(id,a,d);},d);}else{try{g(id).position='static';wp_attempt_focus();}catch(e){}}} 139 addLoadEvent(function(){ var p=new Array(15,30,15,0,-15,-30,-15,0);p=p.concat(p.concat(p));var i=document.forms[0].id;g(i).position='relative';shake(i,p,20);}); 140 </script> 141 <?php 142 } 143 144 /** 145 * Handles sending password retrieval email to user. 146 * 147 * @uses $wpdb WordPress Database object 148 * 149 * @return bool|WP_Error True: when finish. WP_Error on error 150 */ 151 function retrieve_password() { 152 global $wpdb, $current_site; 153 154 $errors = new WP_Error(); 155 156 if ( empty( $_POST['user_login'] ) ) { 157 $errors->add('empty_username', __('<strong>ERROR</strong>: Enter a username or e-mail address.')); 158 } else if ( strpos( $_POST['user_login'], '@' ) ) { 159 $user_data = get_user_by( 'email', trim( $_POST['user_login'] ) ); 160 if ( empty( $user_data ) ) 161 $errors->add('invalid_email', __('<strong>ERROR</strong>: There is no user registered with that email address.')); 162 } else { 163 $login = trim($_POST['user_login']); 164 $user_data = get_user_by('login', $login); 165 } 166 167 do_action('lostpassword_post'); 168 169 if ( $errors->get_error_code() ) 170 return $errors; 171 172 if ( !$user_data ) { 173 $errors->add('invalidcombo', __('<strong>ERROR</strong>: Invalid username or e-mail.')); 174 return $errors; 175 } 176 177 // redefining user_login ensures we return the right case in the email 178 $user_login = $user_data->user_login; 179 $user_email = $user_data->user_email; 180 181 do_action('retreive_password', $user_login); // Misspelled and deprecated 182 do_action('retrieve_password', $user_login); 183 184 $allow = apply_filters('allow_password_reset', true, $user_data->ID); 185 186 if ( ! $allow ) 187 return new WP_Error('no_password_reset', __('Password reset is not allowed for this user')); 188 else if ( is_wp_error($allow) ) 189 return $allow; 190 191 $key = $wpdb->get_var($wpdb->prepare("SELECT user_activation_key FROM $wpdb->users WHERE user_login = %s", $user_login)); 192 if ( empty($key) ) { 193 // Generate something random for a key... 194 $key = wp_generate_password(20, false); 195 do_action('retrieve_password_key', $user_login, $key); 196 // Now insert the new md5 key into the db 197 $wpdb->update($wpdb->users, array('user_activation_key' => $key), array('user_login' => $user_login)); 198 } 199 $message = __('Someone requested that the password be reset for the following account:') . "\r\n\r\n"; 200 $message .= network_home_url( '/' ) . "\r\n\r\n"; 201 $message .= sprintf(__('Username: %s'), $user_login) . "\r\n\r\n"; 202 $message .= __('If this was a mistake, just ignore this email and nothing will happen.') . "\r\n\r\n"; 203 $message .= __('To reset your password, visit the following address:') . "\r\n\r\n"; 204 $message .= '<' . network_site_url("wp-login.php?action=rp&key=$key&login=" . rawurlencode($user_login), 'login') . ">\r\n"; 205 206 if ( is_multisite() ) 207 $blogname = $GLOBALS['current_site']->site_name; 208 else 209 // The blogname option is escaped with esc_html on the way into the database in sanitize_option 210 // we want to reverse this for the plain text arena of emails. 211 $blogname = wp_specialchars_decode(get_option('blogname'), ENT_QUOTES); 212 213 $title = sprintf( __('[%s] Password Reset'), $blogname ); 214 215 $title = apply_filters('retrieve_password_title', $title); 216 $message = apply_filters('retrieve_password_message', $message, $key); 217 218 if ( $message && !wp_mail($user_email, $title, $message) ) 219 wp_die( __('The e-mail could not be sent.') . "<br />\n" . __('Possible reason: your host may have disabled the mail() function...') ); 220 221 return true; 222 } 223 224 /** 225 * Retrieves a user row based on password reset key and login 226 * 227 * @uses $wpdb WordPress Database object 228 * 229 * @param string $key Hash to validate sending user's password 230 * @param string $login The user login 231 * @return object|WP_Error User's database row on success, error object for invalid keys 232 */ 233 function check_password_reset_key($key, $login) { 234 global $wpdb; 235 236 $key = preg_replace('/[^a-z0-9]/i', '', $key); 237 238 if ( empty( $key ) || !is_string( $key ) ) 239 return new WP_Error('invalid_key', __('Invalid key')); 240 241 if ( empty($login) || !is_string($login) ) 242 return new WP_Error('invalid_key', __('Invalid key')); 243 244 $user = $wpdb->get_row($wpdb->prepare("SELECT * FROM $wpdb->users WHERE user_activation_key = %s AND user_login = %s", $key, $login)); 245 246 if ( empty( $user ) ) 247 return new WP_Error('invalid_key', __('Invalid key')); 248 249 return $user; 250 } 251 252 /** 253 * Handles resetting the user's password. 254 * 255 * @param object $user The user 256 * @param string $new_pass New password for the user in plaintext 257 */ 258 function reset_password($user, $new_pass) { 259 do_action('password_reset', $user, $new_pass); 260 261 wp_set_password($new_pass, $user->ID); 262 263 wp_password_change_notification($user); 264 } 265 266 /** 267 * Handles registering a new user. 268 * 269 * @param string $user_login User's username for logging in 270 * @param string $user_email User's email address to send password and add 271 * @return int|WP_Error Either user's ID or error on failure. 272 */ 273 function register_new_user( $user_login, $user_email ) { 274 $errors = new WP_Error(); 275 276 $sanitized_user_login = sanitize_user( $user_login ); 277 $user_email = apply_filters( 'user_registration_email', $user_email ); 278 279 // Check the username 280 if ( $sanitized_user_login == '' ) { 281 $errors->add( 'empty_username', __( '<strong>ERROR</strong>: Please enter a username.' ) ); 282 } elseif ( ! validate_username( $user_login ) ) { 283 $errors->add( 'invalid_username', __( '<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' ) ); 284 $sanitized_user_login = ''; 285 } elseif ( username_exists( $sanitized_user_login ) ) { 286 $errors->add( 'username_exists', __( '<strong>ERROR</strong>: This username is already registered, please choose another one.' ) ); 287 } 288 289 // Check the e-mail address 290 if ( $user_email == '' ) { 291 $errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please type your e-mail address.' ) ); 292 } elseif ( ! is_email( $user_email ) ) { 293 $errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The email address isn’t correct.' ) ); 294 $user_email = ''; 295 } elseif ( email_exists( $user_email ) ) { 296 $errors->add( 'email_exists', __( '<strong>ERROR</strong>: This email is already registered, please choose another one.' ) ); 297 } 298 299 do_action( 'register_post', $sanitized_user_login, $user_email, $errors ); 300 301 $errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email ); 302 303 if ( $errors->get_error_code() ) 304 return $errors; 305 306 $user_pass = wp_generate_password( 12, false); 307 $user_id = wp_create_user( $sanitized_user_login, $user_pass, $user_email ); 308 if ( ! $user_id ) { 309 $errors->add( 'registerfail', sprintf( __( '<strong>ERROR</strong>: Couldn’t register you... please contact the <a href="mailto:%s">webmaster</a> !' ), get_option( 'admin_email' ) ) ); 310 return $errors; 311 } 312 313 update_user_option( $user_id, 'default_password_nag', true, true ); //Set up the Password change nag. 314 315 wp_new_user_notification( $user_id, $user_pass ); 316 317 return $user_id; 318 }