2005-07-23 12:16:03 -04:00
|
|
|
<?php
|
|
|
|
|
2007-06-01 19:13:41 -04:00
|
|
|
/**
|
|
|
|
* Checks whether the given username exists.
|
|
|
|
* @param string $username Username.
|
|
|
|
* @return mixed The user's ID on success, and null on failure.
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* Checks whether the given email exists.
|
|
|
|
* @global object $wpdb WordPress database layer.
|
|
|
|
* @param string $email Email.
|
|
|
|
* @return mixed The user's ID on success, and false on failure.
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* Checks whether an username is valid.
|
|
|
|
* @param string $username Username.
|
|
|
|
* @return bool A filtered boolean.
|
|
|
|
*/
|
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
|
|
|
/**
|
|
|
|
* Insert an user into the database.
|
|
|
|
* @global object $wpdb WordPress database layer.
|
|
|
|
* @param array $userdata An array of user data.
|
|
|
|
* @return int The newly created user's ID.
|
|
|
|
*/
|
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;
|
|
|
|
} 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);
|
2006-01-24 22:09:16 -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
|
|
|
|
|
|
|
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';
|
|
|
|
|
2005-09-13 20:03:02 -04:00
|
|
|
if ( empty($user_registered) )
|
|
|
|
$user_registered = gmdate('Y-m-d H:i:s');
|
|
|
|
|
2007-11-01 01:49:23 -04:00
|
|
|
$data = compact( 'user_pass', 'user_email', 'user_url', 'user_nicename', 'display_name', 'user_registered' );
|
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
|
|
|
|
2005-09-13 20:03:02 -04:00
|
|
|
update_usermeta( $user_id, 'first_name', $first_name);
|
|
|
|
update_usermeta( $user_id, 'last_name', $last_name);
|
|
|
|
update_usermeta( $user_id, 'nickname', $nickname );
|
|
|
|
update_usermeta( $user_id, 'description', $description );
|
|
|
|
update_usermeta( $user_id, 'jabber', $jabber );
|
|
|
|
update_usermeta( $user_id, 'aim', $aim );
|
|
|
|
update_usermeta( $user_id, 'yim', $yim );
|
2006-12-19 18:19:12 -05:00
|
|
|
update_usermeta( $user_id, 'rich_editing', $rich_editing);
|
2005-11-05 22:58:52 -05:00
|
|
|
|
2006-06-11 16:54:07 -04:00
|
|
|
if ( $update && isset($role) ) {
|
2005-11-05 22:58:52 -05:00
|
|
|
$user = new WP_User($user_id);
|
|
|
|
$user->set_role($role);
|
|
|
|
}
|
|
|
|
|
2005-09-13 20:03:02 -04:00
|
|
|
if ( !$update ) {
|
|
|
|
$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 )
|
|
|
|
do_action('profile_update', $user_id);
|
|
|
|
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
|
|
|
/**
|
|
|
|
* Update an user in the database.
|
|
|
|
* @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
|
|
|
|
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
|
|
|
/**
|
|
|
|
* A simpler way of inserting an user into the database.
|
|
|
|
* See also: wp_insert_user().
|
|
|
|
* @global object $wpdb WordPress database layer.
|
|
|
|
* @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 = '') {
|
2005-09-13 20:03:02 -04:00
|
|
|
global $wpdb;
|
2006-02-12 02:53:23 -05:00
|
|
|
|
2006-12-06 22:57:23 -05:00
|
|
|
$user_login = $wpdb->escape($username);
|
|
|
|
$user_email = $wpdb->escape($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);
|
|
|
|
}
|
|
|
|
|
2007-06-01 19:13:41 -04:00
|
|
|
/**
|
|
|
|
* An alias of wp_create_user().
|
|
|
|
* @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.
|
|
|
|
* @deprecated
|
|
|
|
*/
|
2006-12-06 22:57:23 -05:00
|
|
|
function create_user($username, $password, $email) {
|
|
|
|
return wp_create_user($username, $password, $email);
|
2005-09-13 20:03:02 -04:00
|
|
|
}
|
|
|
|
|
2007-12-02 00:14:11 -05:00
|
|
|
?>
|