2005-07-23 12:16:03 -04:00
< ? php
2008-01-06 03:21:58 -05:00
/**
* User Registration API
*
* @ package WordPress
*/
2005-07-23 12:16:03 -04:00
2007-06-01 19:13:41 -04:00
/**
2008-08-30 17:23:43 -04:00
* Checks whether the given username exists .
2008-01-06 03:21:58 -05:00
*
* @ since 2.0 . 0
*
2007-06-01 19:13:41 -04:00
* @ param string $username Username .
2008-01-06 03:21:58 -05:00
* @ return null | int The user ' s ID on success , and null on failure .
2007-06-01 19:13:41 -04:00
*/
2005-07-23 12:16:03 -04:00
function username_exists ( $username ) {
2007-11-27 17:14:53 -05:00
if ( $user = get_userdatabylogin ( $username ) ) {
2005-12-23 18:16:01 -05:00
return $user -> ID ;
2007-06-01 19:13:41 -04:00
} else {
return null ;
}
2005-07-23 12:16:03 -04:00
}
2007-06-01 19:13:41 -04:00
/**
2008-08-30 17:23:43 -04:00
* Checks whether the given email exists .
2008-01-06 03:21:58 -05:00
*
* @ since 2.1 . 0
* @ uses $wpdb
*
2007-06-01 19:13:41 -04:00
* @ param string $email Email .
2008-01-06 03:21:58 -05:00
* @ return bool | int The user ' s ID on success , and false on failure .
2007-06-01 19:13:41 -04:00
*/
2006-02-09 03:11:26 -05:00
function email_exists ( $email ) {
2007-11-27 17:14:53 -05:00
if ( $user = get_user_by_email ( $email ) )
return $user -> ID ;
return false ;
2006-02-09 03:11:26 -05:00
}
2007-06-01 19:13:41 -04:00
/**
2008-08-30 17:23:43 -04:00
* Checks whether an username is valid .
2008-01-06 03:21:58 -05:00
*
* @ since 2.0 . 1
* @ uses apply_filters () Calls 'validate_username' hook on $valid check and $username as parameters
*
2007-06-01 19:13:41 -04:00
* @ param string $username Username .
2008-01-06 03:21:58 -05:00
* @ return bool Whether username given is valid
2007-06-01 19:13:41 -04:00
*/
2006-01-24 22:09:16 -05:00
function validate_username ( $username ) {
2007-06-01 19:13:41 -04:00
$sanitized = sanitize_user ( $username , true );
$valid = ( $sanitized == $username );
return apply_filters ( 'validate_username' , $valid , $username );
2006-01-24 22:09:16 -05:00
}
2007-06-01 19:13:41 -04:00
/**
2008-08-30 17:23:43 -04:00
* Insert an user into the database .
2008-01-06 03:21:58 -05:00
*
2008-08-30 17:23:43 -04:00
* Can update a current user or insert a new user based on whether the user ' s ID
* is present .
2008-01-06 03:21:58 -05:00
*
2008-08-30 17:23:43 -04:00
* Can be used to update the user 's info (see below), set the user' s role , and
* set the user ' s preference on whether they want the rich editor on .
2008-01-06 03:21:58 -05:00
*
2008-08-30 17:23:43 -04:00
* Most of the $userdata array fields have filters associated with the values .
* The exceptions are 'rich_editing' , 'role' , 'jabber' , 'aim' , 'yim' ,
* 'user_registered' , and 'ID' . The filters have the prefix 'pre_user_' followed
* by the field name . An example using 'description' would have the filter
* called , 'pre_user_description' that can be hooked into .
2008-01-06 03:21:58 -05:00
*
* The $userdata array can contain the following fields :
* 'ID' - An integer that will be used for updating an existing user .
* 'user_pass' - A string that contains the plain text password for the user .
* 'user_login' - A string that contains the user ' s username for logging in .
* 'user_nicename' - A string that contains a nicer looking name for the user .
* The default is the user ' s username .
* 'user_url' - A string containing the user 's URL for the user' s web site .
* 'user_email' - A string containing the user ' s email address .
2008-08-30 17:23:43 -04:00
* 'display_name' - A string that will be shown on the site . Defaults to user ' s
* username . It is likely that you will want to change this , for both
* appearance and security through obscurity ( that is if you don ' t use and
* delete the default 'admin' user ) .
2008-01-06 03:21:58 -05:00
* 'nickname' - The user 's nickname, defaults to the user' s username .
* 'first_name' - The user ' s first name .
* 'last_name' - The user ' s last name .
* 'description' - A string containing content about the user .
2010-02-24 15:13:23 -05:00
* 'rich_editing' - A string for whether to enable the rich editor . False
2008-08-30 17:23:43 -04:00
* if not empty .
2008-01-06 03:21:58 -05:00
* 'user_registered' - The date the user registered . Format is 'Y-m-d H:i:s' .
* 'role' - A string used to set the user ' s role .
* 'jabber' - User ' s Jabber account .
* 'aim' - User ' s AOL IM account .
* 'yim' - User ' s Yahoo IM account .
*
* @ since 2.0 . 0
* @ uses $wpdb WordPress database layer .
* @ uses apply_filters () Calls filters for most of the $userdata fields with the prefix 'pre_user' . See note above .
* @ uses do_action () Calls 'profile_update' hook when updating giving the user ' s ID
* @ uses do_action () Calls 'user_register' hook when creating a new user giving the user ' s ID
*
2007-06-01 19:13:41 -04:00
* @ param array $userdata An array of user data .
2009-12-21 10:25:00 -05:00
* @ return int | WP_Error The newly created user ' s ID or a WP_Error object if the user could not be created .
2007-06-01 19:13:41 -04:00
*/
2005-09-13 20:03:02 -04:00
function wp_insert_user ( $userdata ) {
2005-07-23 12:16:03 -04:00
global $wpdb ;
2005-09-13 20:03:02 -04:00
2007-06-14 18:45:40 -04:00
extract ( $userdata , EXTR_SKIP );
2005-09-13 20:03:02 -04:00
// Are we updating or creating?
if ( ! empty ( $ID ) ) {
2006-05-26 18:47:13 -04:00
$ID = ( int ) $ID ;
2005-09-13 20:03:02 -04:00
$update = true ;
2008-09-24 12:05:21 -04:00
$old_user_data = get_userdata ( $ID );
2005-09-13 20:03:02 -04:00
} else {
$update = false ;
2007-12-02 00:14:11 -05:00
// Hash the password
$user_pass = wp_hash_password ( $user_pass );
2005-09-13 20:03:02 -04:00
}
2006-02-12 02:53:23 -05:00
2006-01-24 22:09:16 -05:00
$user_login = sanitize_user ( $user_login , true );
2006-05-26 18:47:13 -04:00
$user_login = apply_filters ( 'pre_user_login' , $user_login );
2010-01-15 17:11:12 -05:00
2009-12-21 10:25:00 -05:00
//Remove any non-printable chars from the login string to see if we have ended up with an empty username
$user_login = trim ( $user_login );
2010-01-15 17:11:12 -05:00
2010-01-20 16:58:13 -05:00
if ( empty ( $user_login ) )
2009-12-21 10:25:00 -05:00
return new WP_Error ( 'empty_user_login' , __ ( 'Cannot create a user with an empty login name.' ) );
2010-01-20 16:58:13 -05:00
if ( ! $update && username_exists ( $user_login ) )
return new WP_Error ( 'existing_user_login' , __ ( 'This username is already registered.' ) );
2010-01-15 17:11:12 -05:00
2005-09-13 20:03:02 -04:00
if ( empty ( $user_nicename ) )
$user_nicename = sanitize_title ( $user_login );
2006-05-26 18:47:13 -04:00
$user_nicename = apply_filters ( 'pre_user_nicename' , $user_nicename );
if ( empty ( $user_url ) )
$user_url = '' ;
$user_url = apply_filters ( 'pre_user_url' , $user_url );
if ( empty ( $user_email ) )
$user_email = '' ;
$user_email = apply_filters ( 'pre_user_email' , $user_email );
2005-09-13 20:03:02 -04:00
2010-01-20 16:58:13 -05:00
if ( ! $update && email_exists ( $user_email ) )
return new WP_Error ( 'existing_user_email' , __ ( 'This email address is already registered.' ) );
2005-09-13 20:03:02 -04:00
if ( empty ( $display_name ) )
$display_name = $user_login ;
2006-05-26 18:47:13 -04:00
$display_name = apply_filters ( 'pre_user_display_name' , $display_name );
2006-02-12 02:53:23 -05:00
2005-09-13 20:03:02 -04:00
if ( empty ( $nickname ) )
$nickname = $user_login ;
2006-05-26 18:47:13 -04:00
$nickname = apply_filters ( 'pre_user_nickname' , $nickname );
if ( empty ( $first_name ) )
$first_name = '' ;
$first_name = apply_filters ( 'pre_user_first_name' , $first_name );
if ( empty ( $last_name ) )
$last_name = '' ;
$last_name = apply_filters ( 'pre_user_last_name' , $last_name );
if ( empty ( $description ) )
$description = '' ;
$description = apply_filters ( 'pre_user_description' , $description );
2006-02-12 02:53:23 -05:00
2006-12-19 18:19:12 -05:00
if ( empty ( $rich_editing ) )
$rich_editing = 'true' ;
2008-10-16 18:23:32 -04:00
if ( empty ( $comment_shortcuts ) )
$comment_shortcuts = 'false' ;
2008-03-11 17:06:03 -04:00
if ( empty ( $admin_color ) )
2008-03-15 13:02:16 -04:00
$admin_color = 'fresh' ;
2008-03-11 17:06:03 -04:00
$admin_color = preg_replace ( '|[^a-z0-9 _.\-@]|i' , '' , $admin_color );
2008-08-21 13:40:38 -04:00
if ( empty ( $use_ssl ) )
$use_ssl = 0 ;
2005-09-13 20:03:02 -04:00
if ( empty ( $user_registered ) )
$user_registered = gmdate ( 'Y-m-d H:i:s' );
2009-05-14 14:55:32 -04:00
$user_nicename_check = $wpdb -> get_var ( $wpdb -> prepare ( " SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1 " , $user_nicename , $user_login ));
2009-04-18 19:21:20 -04:00
2009-09-14 09:57:48 -04:00
if ( $user_nicename_check ) {
2009-04-18 19:21:20 -04:00
$suffix = 2 ;
while ( $user_nicename_check ) {
$alt_user_nicename = $user_nicename . " - $suffix " ;
2009-05-14 14:55:32 -04:00
$user_nicename_check = $wpdb -> get_var ( $wpdb -> prepare ( " SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1 " , $alt_user_nicename , $user_login ));
2009-04-18 19:21:20 -04:00
$suffix ++ ;
}
$user_nicename = $alt_user_nicename ;
}
2009-04-20 14:18:39 -04:00
2007-11-01 01:49:23 -04:00
$data = compact ( 'user_pass' , 'user_email' , 'user_url' , 'user_nicename' , 'display_name' , 'user_registered' );
2008-01-24 21:21:59 -05:00
$data = stripslashes_deep ( $data );
2007-10-12 23:51:11 -04:00
2005-09-13 20:03:02 -04:00
if ( $update ) {
2007-10-12 23:51:11 -04:00
$wpdb -> update ( $wpdb -> users , $data , compact ( 'ID' ) );
2007-03-22 20:59:21 -04:00
$user_id = ( int ) $ID ;
2005-09-13 20:03:02 -04:00
} else {
2007-10-12 23:51:11 -04:00
$wpdb -> insert ( $wpdb -> users , $data + compact ( 'user_login' ) );
2007-03-22 20:59:21 -04:00
$user_id = ( int ) $wpdb -> insert_id ;
2005-09-13 20:03:02 -04:00
}
2006-02-12 02:53:23 -05:00
2010-02-22 16:25:32 -05:00
update_user_meta ( $user_id , 'first_name' , $first_name );
update_user_meta ( $user_id , 'last_name' , $last_name );
update_user_meta ( $user_id , 'nickname' , $nickname );
update_user_meta ( $user_id , 'description' , $description );
update_user_meta ( $user_id , 'rich_editing' , $rich_editing );
update_user_meta ( $user_id , 'comment_shortcuts' , $comment_shortcuts );
update_user_meta ( $user_id , 'admin_color' , $admin_color );
update_user_meta ( $user_id , 'use_ssl' , $use_ssl );
2009-09-14 09:57:48 -04:00
foreach ( _wp_get_user_contactmethods () as $method => $name ) {
2009-08-06 17:59:52 -04:00
if ( empty ( $$method ) )
$$method = '' ;
2009-09-14 09:57:48 -04:00
2010-02-22 16:25:32 -05:00
update_user_meta ( $user_id , $method , $$method );
2009-08-06 17:59:52 -04:00
}
2005-11-05 22:58:52 -05:00
2009-05-23 17:48:36 -04:00
if ( isset ( $role ) ) {
2005-11-05 22:58:52 -05:00
$user = new WP_User ( $user_id );
$user -> set_role ( $role );
2009-05-23 17:48:36 -04:00
} elseif ( ! $update ) {
2005-09-13 20:03:02 -04:00
$user = new WP_User ( $user_id );
2006-08-30 17:46:31 -04:00
$user -> set_role ( get_option ( 'default_role' ));
2005-09-13 20:03:02 -04:00
}
2005-11-07 16:56:03 -05:00
wp_cache_delete ( $user_id , 'users' );
2005-12-19 14:14:22 -05:00
wp_cache_delete ( $user_login , 'userlogins' );
2006-02-12 02:53:23 -05:00
2005-09-13 20:03:02 -04:00
if ( $update )
2008-09-24 12:05:21 -04:00
do_action ( 'profile_update' , $user_id , $old_user_data );
2005-09-13 20:03:02 -04:00
else
do_action ( 'user_register' , $user_id );
2006-02-12 02:53:23 -05:00
return $user_id ;
2005-09-13 20:03:02 -04:00
}
2007-06-01 19:13:41 -04:00
/**
2008-08-30 17:23:43 -04:00
* Update an user in the database .
2008-01-06 03:21:58 -05:00
*
2008-08-30 17:23:43 -04:00
* It is possible to update a user 's password by specifying the ' user_pass '
* value in the $userdata parameter array .
2008-01-06 03:21:58 -05:00
*
2008-08-30 17:23:43 -04:00
* If $userdata does not contain an 'ID' key , then a new user will be created
* and the new user ' s ID will be returned .
2008-01-06 03:21:58 -05:00
*
2008-08-30 17:23:43 -04:00
* If current user ' s password is being updated , then the cookies will be
* cleared .
2008-01-06 03:21:58 -05:00
*
* @ since 2.0 . 0
* @ see wp_insert_user () For what fields can be set in $userdata
* @ uses wp_insert_user () Used to update existing user or add new one if user doesn ' t exist already
*
2007-06-01 19:13:41 -04:00
* @ param array $userdata An array of user data .
* @ return int The updated user ' s ID .
*/
2005-09-13 20:03:02 -04:00
function wp_update_user ( $userdata ) {
$ID = ( int ) $userdata [ 'ID' ];
2006-02-12 02:53:23 -05:00
2005-09-13 20:03:02 -04:00
// First, get all of the original fields
2006-02-12 02:53:23 -05:00
$user = get_userdata ( $ID );
2005-09-13 20:03:02 -04:00
// Escape data pulled from DB.
$user = add_magic_quotes ( get_object_vars ( $user ));
// If password is changing, hash it now.
if ( ! empty ( $userdata [ 'user_pass' ]) ) {
$plaintext_pass = $userdata [ 'user_pass' ];
2007-12-02 00:14:11 -05:00
$userdata [ 'user_pass' ] = wp_hash_password ( $userdata [ 'user_pass' ]);
2005-09-13 20:03:02 -04:00
}
2005-11-05 22:40:43 -05:00
2010-01-22 11:06:31 -05:00
wp_cache_delete ( $user [ 'user_email' ], 'useremail' );
2005-09-13 20:03:02 -04:00
// Merge old and new fields with new fields overwriting old ones.
$userdata = array_merge ( $user , $userdata );
$user_id = wp_insert_user ( $userdata );
2005-07-23 12:16:03 -04:00
2006-02-12 02:53:23 -05:00
// Update the cookies if the password changed.
2006-02-22 14:08:55 -05:00
$current_user = wp_get_current_user ();
2006-12-06 22:57:23 -05:00
if ( $current_user -> id == $ID ) {
2005-09-20 13:55:16 -04:00
if ( isset ( $plaintext_pass ) ) {
2007-12-16 12:41:59 -05:00
wp_clear_auth_cookie ();
wp_set_auth_cookie ( $ID );
2005-09-20 13:55:16 -04:00
}
2005-09-13 20:03:02 -04:00
}
2006-02-12 02:53:23 -05:00
2005-07-23 12:16:03 -04:00
return $user_id ;
}
2007-06-01 19:13:41 -04:00
/**
2008-08-30 17:23:43 -04:00
* A simpler way of inserting an user into the database .
2008-01-06 03:21:58 -05:00
*
* Creates a new user with just the username , password , and email . For a more
* detail creation of a user , use wp_insert_user () to specify more infomation .
*
* @ since 2.0 . 0
* @ see wp_insert_user () More complete way to create a new user
*
2007-06-01 19:13:41 -04:00
* @ param string $username The user ' s username .
* @ param string $password The user ' s password .
* @ param string $email The user ' s email ( optional ) .
* @ return int The new user ' s ID .
*/
2006-12-06 22:57:23 -05:00
function wp_create_user ( $username , $password , $email = '' ) {
2009-09-27 01:33:56 -04:00
$user_login = esc_sql ( $username );
$user_email = esc_sql ( $email );
2005-09-13 20:03:02 -04:00
$user_pass = $password ;
$userdata = compact ( 'user_login' , 'user_email' , 'user_pass' );
return wp_insert_user ( $userdata );
}
2009-08-20 15:51:43 -04:00
/**
* Setup the default contact methods
*
* @ access private
2009-09-14 10:03:32 -04:00
* @ since
2009-08-20 15:51:43 -04:00
*
* @ return array $user_contactmethods Array of contact methods and their labels .
*/
function _wp_get_user_contactmethods () {
$user_contactmethods = array (
'aim' => __ ( 'AIM' ),
'yim' => __ ( 'Yahoo IM' ),
'jabber' => __ ( 'Jabber / Google Talk' )
);
return apply_filters ( 'user_contactmethods' , $user_contactmethods );
}
2007-12-02 00:14:11 -05:00
?>