mp-wp_genesis 1 <?php
mp-wp_genesis 2 /**
mp-wp_genesis 3 * User Registration API
mp-wp_genesis 4 *
mp-wp_genesis 5 * @package WordPress
mp-wp_genesis 6 */
mp-wp_genesis 7
mp-wp_genesis 8 /**
mp-wp_genesis 9 * Checks whether the given username exists.
mp-wp_genesis 10 *
mp-wp_genesis 11 * @since 2.0.0
mp-wp_genesis 12 *
mp-wp_genesis 13 * @param string $username Username.
mp-wp_genesis 14 * @return null|int The user's ID on success, and null on failure.
mp-wp_genesis 15 */
mp-wp_genesis 16 function username_exists( $username ) {
mp-wp_genesis 17 if ( $user = get_userdatabylogin( $username ) ) {
mp-wp_genesis 18 return $user->ID;
mp-wp_genesis 19 } else {
mp-wp_genesis 20 return null;
mp-wp_genesis 21 }
mp-wp_genesis 22 }
mp-wp_genesis 23
mp-wp_genesis 24 /**
mp-wp_genesis 25 * Checks whether the given email exists.
mp-wp_genesis 26 *
mp-wp_genesis 27 * @since 2.1.0
mp-wp_genesis 28 * @uses $wpdb
mp-wp_genesis 29 *
mp-wp_genesis 30 * @param string $email Email.
mp-wp_genesis 31 * @return bool|int The user's ID on success, and false on failure.
mp-wp_genesis 32 */
mp-wp_genesis 33 function email_exists( $email ) {
mp-wp_genesis 34 if ( $user = get_user_by_email($email) )
mp-wp_genesis 35 return $user->ID;
mp-wp_genesis 36
mp-wp_genesis 37 return false;
mp-wp_genesis 38 }
mp-wp_genesis 39
mp-wp_genesis 40 /**
mp-wp_genesis 41 * Checks whether an username is valid.
mp-wp_genesis 42 *
mp-wp_genesis 43 * @since 2.0.1
mp-wp_genesis 44 * @uses apply_filters() Calls 'validate_username' hook on $valid check and $username as parameters
mp-wp_genesis 45 *
mp-wp_genesis 46 * @param string $username Username.
mp-wp_genesis 47 * @return bool Whether username given is valid
mp-wp_genesis 48 */
mp-wp_genesis 49 function validate_username( $username ) {
mp-wp_genesis 50 $sanitized = sanitize_user( $username, true );
mp-wp_genesis 51 $valid = ( $sanitized == $username );
mp-wp_genesis 52 return apply_filters( 'validate_username', $valid, $username );
mp-wp_genesis 53 }
mp-wp_genesis 54
mp-wp_genesis 55 /**
mp-wp_genesis 56 * Insert an user into the database.
mp-wp_genesis 57 *
mp-wp_genesis 58 * Can update a current user or insert a new user based on whether the user's ID
mp-wp_genesis 59 * is present.
mp-wp_genesis 60 *
mp-wp_genesis 61 * Can be used to update the user's info (see below), set the user's role, and
mp-wp_genesis 62 * set the user's preference on whether they want the rich editor on.
mp-wp_genesis 63 *
mp-wp_genesis 64 * Most of the $userdata array fields have filters associated with the values.
mp-wp_genesis 65 * The exceptions are 'rich_editing', 'role', 'jabber', 'aim', 'yim',
mp-wp_genesis 66 * 'user_registered', and 'ID'. The filters have the prefix 'pre_user_' followed
mp-wp_genesis 67 * by the field name. An example using 'description' would have the filter
mp-wp_genesis 68 * called, 'pre_user_description' that can be hooked into.
mp-wp_genesis 69 *
mp-wp_genesis 70 * The $userdata array can contain the following fields:
mp-wp_genesis 71 * 'ID' - An integer that will be used for updating an existing user.
mp-wp_genesis 72 * 'user_pass' - A string that contains the plain text password for the user.
mp-wp_genesis 73 * 'user_login' - A string that contains the user's username for logging in.
mp-wp_genesis 74 * 'user_nicename' - A string that contains a nicer looking name for the user.
mp-wp_genesis 75 * The default is the user's username.
mp-wp_genesis 76 * 'user_url' - A string containing the user's URL for the user's web site.
mp-wp_genesis 77 * 'user_email' - A string containing the user's email address.
mp-wp_genesis 78 * 'display_name' - A string that will be shown on the site. Defaults to user's
mp-wp_genesis 79 * username. It is likely that you will want to change this, for both
mp-wp_genesis 80 * appearance and security through obscurity (that is if you don't use and
mp-wp_genesis 81 * delete the default 'admin' user).
mp-wp_genesis 82 * 'nickname' - The user's nickname, defaults to the user's username.
mp-wp_genesis 83 * 'first_name' - The user's first name.
mp-wp_genesis 84 * 'last_name' - The user's last name.
mp-wp_genesis 85 * 'description' - A string containing content about the user.
mp-wp_genesis 86 * 'rich_editing' - A string for whether to enable the rich editor or not. False
mp-wp_genesis 87 * if not empty.
mp-wp_genesis 88 * 'user_registered' - The date the user registered. Format is 'Y-m-d H:i:s'.
mp-wp_genesis 89 * 'role' - A string used to set the user's role.
mp-wp_genesis 90 * 'jabber' - User's Jabber account.
mp-wp_genesis 91 * 'aim' - User's AOL IM account.
mp-wp_genesis 92 * 'yim' - User's Yahoo IM account.
mp-wp_genesis 93 *
mp-wp_genesis 94 * @since 2.0.0
mp-wp_genesis 95 * @uses $wpdb WordPress database layer.
mp-wp_genesis 96 * @uses apply_filters() Calls filters for most of the $userdata fields with the prefix 'pre_user'. See note above.
mp-wp_genesis 97 * @uses do_action() Calls 'profile_update' hook when updating giving the user's ID
mp-wp_genesis 98 * @uses do_action() Calls 'user_register' hook when creating a new user giving the user's ID
mp-wp_genesis 99 *
mp-wp_genesis 100 * @param array $userdata An array of user data.
mp-wp_genesis 101 * @return int The newly created user's ID.
mp-wp_genesis 102 */
mp-wp_genesis 103 function wp_insert_user($userdata) {
mp-wp_genesis 104 global $wpdb;
mp-wp_genesis 105
mp-wp_genesis 106 extract($userdata, EXTR_SKIP);
mp-wp_genesis 107
mp-wp_genesis 108 // Are we updating or creating?
mp-wp_genesis 109 if ( !empty($ID) ) {
mp-wp_genesis 110 $ID = (int) $ID;
mp-wp_genesis 111 $update = true;
mp-wp_genesis 112 $old_user_data = get_userdata($ID);
mp-wp_genesis 113 } else {
mp-wp_genesis 114 $update = false;
mp-wp_genesis 115 // Hash the password
mp-wp_genesis 116 $user_pass = wp_hash_password($user_pass);
mp-wp_genesis 117 }
mp-wp_genesis 118
mp-wp_genesis 119 $user_login = sanitize_user($user_login, true);
mp-wp_genesis 120 $user_login = apply_filters('pre_user_login', $user_login);
mp-wp_genesis 121
mp-wp_genesis 122 if ( empty($user_nicename) )
mp-wp_genesis 123 $user_nicename = sanitize_title( $user_login );
mp-wp_genesis 124 $user_nicename = apply_filters('pre_user_nicename', $user_nicename);
mp-wp_genesis 125
mp-wp_genesis 126 if ( empty($user_url) )
mp-wp_genesis 127 $user_url = '';
mp-wp_genesis 128 $user_url = apply_filters('pre_user_url', $user_url);
mp-wp_genesis 129
mp-wp_genesis 130 if ( empty($user_email) )
mp-wp_genesis 131 $user_email = '';
mp-wp_genesis 132 $user_email = apply_filters('pre_user_email', $user_email);
mp-wp_genesis 133
mp-wp_genesis 134 if ( empty($display_name) )
mp-wp_genesis 135 $display_name = $user_login;
mp-wp_genesis 136 $display_name = apply_filters('pre_user_display_name', $display_name);
mp-wp_genesis 137
mp-wp_genesis 138 if ( empty($nickname) )
mp-wp_genesis 139 $nickname = $user_login;
mp-wp_genesis 140 $nickname = apply_filters('pre_user_nickname', $nickname);
mp-wp_genesis 141
mp-wp_genesis 142 if ( empty($first_name) )
mp-wp_genesis 143 $first_name = '';
mp-wp_genesis 144 $first_name = apply_filters('pre_user_first_name', $first_name);
mp-wp_genesis 145
mp-wp_genesis 146 if ( empty($last_name) )
mp-wp_genesis 147 $last_name = '';
mp-wp_genesis 148 $last_name = apply_filters('pre_user_last_name', $last_name);
mp-wp_genesis 149
mp-wp_genesis 150 if ( empty($description) )
mp-wp_genesis 151 $description = '';
mp-wp_genesis 152 $description = apply_filters('pre_user_description', $description);
mp-wp_genesis 153
mp-wp_genesis 154 if ( empty($rich_editing) )
mp-wp_genesis 155 $rich_editing = 'true';
mp-wp_genesis 156
mp-wp_genesis 157 if ( empty($comment_shortcuts) )
mp-wp_genesis 158 $comment_shortcuts = 'false';
mp-wp_genesis 159
mp-wp_genesis 160 if ( empty($admin_color) )
mp-wp_genesis 161 $admin_color = 'fresh';
mp-wp_genesis 162 $admin_color = preg_replace('|[^a-z0-9 _.\-@]|i', '', $admin_color);
mp-wp_genesis 163
mp-wp_genesis 164 if ( empty($use_ssl) )
mp-wp_genesis 165 $use_ssl = 0;
mp-wp_genesis 166
mp-wp_genesis 167 if ( empty($jabber) )
mp-wp_genesis 168 $jabber = '';
mp-wp_genesis 169
mp-wp_genesis 170 if ( empty($aim) )
mp-wp_genesis 171 $aim = '';
mp-wp_genesis 172
mp-wp_genesis 173 if ( empty($yim) )
mp-wp_genesis 174 $yim = '';
mp-wp_genesis 175
mp-wp_genesis 176 if ( empty($user_registered) )
mp-wp_genesis 177 $user_registered = gmdate('Y-m-d H:i:s');
mp-wp_genesis 178
mp-wp_genesis 179 $data = compact( 'user_pass', 'user_email', 'user_url', 'user_nicename', 'display_name', 'user_registered' );
mp-wp_genesis 180 $data = stripslashes_deep( $data );
mp-wp_genesis 181
mp-wp_genesis 182 if ( $update ) {
mp-wp_genesis 183 $wpdb->update( $wpdb->users, $data, compact( 'ID' ) );
mp-wp_genesis 184 $user_id = (int) $ID;
mp-wp_genesis 185 } else {
mp-wp_genesis 186 $wpdb->insert( $wpdb->users, $data + compact( 'user_login' ) );
mp-wp_genesis 187 $user_id = (int) $wpdb->insert_id;
mp-wp_genesis 188 }
mp-wp_genesis 189
mp-wp_genesis 190 update_usermeta( $user_id, 'first_name', $first_name);
mp-wp_genesis 191 update_usermeta( $user_id, 'last_name', $last_name);
mp-wp_genesis 192 update_usermeta( $user_id, 'nickname', $nickname );
mp-wp_genesis 193 update_usermeta( $user_id, 'description', $description );
mp-wp_genesis 194 update_usermeta( $user_id, 'jabber', $jabber );
mp-wp_genesis 195 update_usermeta( $user_id, 'aim', $aim );
mp-wp_genesis 196 update_usermeta( $user_id, 'yim', $yim );
mp-wp_genesis 197 update_usermeta( $user_id, 'rich_editing', $rich_editing);
mp-wp_genesis 198 update_usermeta( $user_id, 'comment_shortcuts', $comment_shortcuts);
mp-wp_genesis 199 update_usermeta( $user_id, 'admin_color', $admin_color);
mp-wp_genesis 200 update_usermeta( $user_id, 'use_ssl', $use_ssl);
mp-wp_genesis 201
mp-wp_genesis 202 if ( $update && isset($role) ) {
mp-wp_genesis 203 $user = new WP_User($user_id);
mp-wp_genesis 204 $user->set_role($role);
mp-wp_genesis 205 }
mp-wp_genesis 206
mp-wp_genesis 207 if ( !$update ) {
mp-wp_genesis 208 $user = new WP_User($user_id);
mp-wp_genesis 209 $user->set_role(get_option('default_role'));
mp-wp_genesis 210 }
mp-wp_genesis 211
mp-wp_genesis 212 wp_cache_delete($user_id, 'users');
mp-wp_genesis 213 wp_cache_delete($user_login, 'userlogins');
mp-wp_genesis 214
mp-wp_genesis 215 if ( $update )
mp-wp_genesis 216 do_action('profile_update', $user_id, $old_user_data);
mp-wp_genesis 217 else
mp-wp_genesis 218 do_action('user_register', $user_id);
mp-wp_genesis 219
mp-wp_genesis 220 return $user_id;
mp-wp_genesis 221 }
mp-wp_genesis 222
mp-wp_genesis 223 /**
mp-wp_genesis 224 * Update an user in the database.
mp-wp_genesis 225 *
mp-wp_genesis 226 * It is possible to update a user's password by specifying the 'user_pass'
mp-wp_genesis 227 * value in the $userdata parameter array.
mp-wp_genesis 228 *
mp-wp_genesis 229 * If $userdata does not contain an 'ID' key, then a new user will be created
mp-wp_genesis 230 * and the new user's ID will be returned.
mp-wp_genesis 231 *
mp-wp_genesis 232 * If current user's password is being updated, then the cookies will be
mp-wp_genesis 233 * cleared.
mp-wp_genesis 234 *
mp-wp_genesis 235 * @since 2.0.0
mp-wp_genesis 236 * @see wp_insert_user() For what fields can be set in $userdata
mp-wp_genesis 237 * @uses wp_insert_user() Used to update existing user or add new one if user doesn't exist already
mp-wp_genesis 238 *
mp-wp_genesis 239 * @param array $userdata An array of user data.
mp-wp_genesis 240 * @return int The updated user's ID.
mp-wp_genesis 241 */
mp-wp_genesis 242 function wp_update_user($userdata) {
mp-wp_genesis 243 $ID = (int) $userdata['ID'];
mp-wp_genesis 244
mp-wp_genesis 245 // First, get all of the original fields
mp-wp_genesis 246 $user = get_userdata($ID);
mp-wp_genesis 247
mp-wp_genesis 248 // Escape data pulled from DB.
mp-wp_genesis 249 $user = add_magic_quotes(get_object_vars($user));
mp-wp_genesis 250
mp-wp_genesis 251 // If password is changing, hash it now.
mp-wp_genesis 252 if ( ! empty($userdata['user_pass']) ) {
mp-wp_genesis 253 $plaintext_pass = $userdata['user_pass'];
mp-wp_genesis 254 $userdata['user_pass'] = wp_hash_password($userdata['user_pass']);
mp-wp_genesis 255 }
mp-wp_genesis 256
mp-wp_genesis 257 // Merge old and new fields with new fields overwriting old ones.
mp-wp_genesis 258 $userdata = array_merge($user, $userdata);
mp-wp_genesis 259 $user_id = wp_insert_user($userdata);
mp-wp_genesis 260
mp-wp_genesis 261 // Update the cookies if the password changed.
mp-wp_genesis 262 $current_user = wp_get_current_user();
mp-wp_genesis 263 if ( $current_user->id == $ID ) {
mp-wp_genesis 264 if ( isset($plaintext_pass) ) {
mp-wp_genesis 265 wp_clear_auth_cookie();
mp-wp_genesis 266 wp_set_auth_cookie($ID);
mp-wp_genesis 267 }
mp-wp_genesis 268 }
mp-wp_genesis 269
mp-wp_genesis 270 return $user_id;
mp-wp_genesis 271 }
mp-wp_genesis 272
mp-wp_genesis 273 /**
mp-wp_genesis 274 * A simpler way of inserting an user into the database.
mp-wp_genesis 275 *
mp-wp_genesis 276 * Creates a new user with just the username, password, and email. For a more
mp-wp_genesis 277 * detail creation of a user, use wp_insert_user() to specify more infomation.
mp-wp_genesis 278 *
mp-wp_genesis 279 * @since 2.0.0
mp-wp_genesis 280 * @see wp_insert_user() More complete way to create a new user
mp-wp_genesis 281 * @uses $wpdb Escapes $username and $email parameters
mp-wp_genesis 282 *
mp-wp_genesis 283 * @param string $username The user's username.
mp-wp_genesis 284 * @param string $password The user's password.
mp-wp_genesis 285 * @param string $email The user's email (optional).
mp-wp_genesis 286 * @return int The new user's ID.
mp-wp_genesis 287 */
mp-wp_genesis 288 function wp_create_user($username, $password, $email = '') {
mp-wp_genesis 289 global $wpdb;
mp-wp_genesis 290
mp-wp_genesis 291 $user_login = $wpdb->escape($username);
mp-wp_genesis 292 $user_email = $wpdb->escape($email);
mp-wp_genesis 293 $user_pass = $password;
mp-wp_genesis 294
mp-wp_genesis 295 $userdata = compact('user_login', 'user_email', 'user_pass');
mp-wp_genesis 296 return wp_insert_user($userdata);
mp-wp_genesis 297 }
mp-wp_genesis 298
mp-wp_genesis 299 ?>