Projects : mp-wp : mp-wp_svg-screenshots-and-errorreporting-r2

mp-wp/wp-includes/registration.php

Dir - Raw

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