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