1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
|
<?php
function username_exists( $username ) {
global $wpdb;
$username = sanitize_user( $username );
$user = get_userdatabylogin($username);
if ( $user )
return $user->ID;
return null;
}
function email_exists( $email ) {
global $wpdb;
$email = addslashes( $email );
return $wpdb->get_var("SELECT ID FROM $wpdb->users WHERE user_email = '$email'");
}
function validate_username( $username ) {
$name = sanitize_user($username, true);
$valid = true;
if ( $name != $username )
$valid = false;
return apply_filters('validate_username', $valid, $username);
}
function wp_insert_user($userdata) {
global $wpdb;
extract($userdata);
// Are we updating or creating?
if ( !empty($ID) ) {
$ID = (int) $ID;
$update = true;
} else {
$update = false;
// Password is not hashed when creating new user.
$user_pass = md5($user_pass);
}
$user_login = sanitize_user($user_login, true);
$user_login = apply_filters('pre_user_login', $user_login);
if ( empty($user_nicename) )
$user_nicename = sanitize_title( $user_login );
$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);
if ( empty($display_name) )
$display_name = $user_login;
$display_name = apply_filters('pre_user_display_name', $display_name);
if ( empty($nickname) )
$nickname = $user_login;
$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);
if ( empty($user_registered) )
$user_registered = gmdate('Y-m-d H:i:s');
if ( $update ) {
$query = "UPDATE $wpdb->users SET user_pass='$user_pass', user_email='$user_email', user_url='$user_url', user_nicename = '$user_nicename', display_name = '$display_name' WHERE ID = '$ID'";
$query = apply_filters('update_user_query', $query);
$wpdb->query( $query );
$user_id = $ID;
} else {
$query = "INSERT INTO $wpdb->users
(user_login, user_pass, user_email, user_url, user_registered, user_nicename, display_name)
VALUES
('$user_login', '$user_pass', '$user_email', '$user_url', '$user_registered', '$user_nicename', '$display_name')";
$query = apply_filters('create_user_query', $query);
$wpdb->query( $query );
$user_id = $wpdb->insert_id;
}
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 );
if ( $update && isset($role) ) {
$user = new WP_User($user_id);
$user->set_role($role);
}
if ( !$update ) {
$user = new WP_User($user_id);
$user->set_role(get_option('default_role'));
}
wp_cache_delete($user_id, 'users');
wp_cache_delete($user_login, 'userlogins');
if ( $update )
do_action('profile_update', $user_id);
else
do_action('user_register', $user_id);
return $user_id;
}
function wp_update_user($userdata) {
global $wpdb;
$ID = (int) $userdata['ID'];
// First, get all of the original fields
$user = get_userdata($ID);
// 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'];
$userdata['user_pass'] = md5($userdata['user_pass']);
}
// Merge old and new fields with new fields overwriting old ones.
$userdata = array_merge($user, $userdata);
$user_id = wp_insert_user($userdata);
// Update the cookies if the password changed.
$current_user = wp_get_current_user();
if( $current_user->id == $ID ) {
if ( isset($plaintext_pass) ) {
wp_clearcookie();
wp_setcookie($userdata['user_login'], $plaintext_pass);
}
}
return $user_id;
}
function wp_create_user( $username, $password, $email = '') {
global $wpdb;
$user_login = $wpdb->escape( $username );
$user_email = $wpdb->escape( $email );
$user_pass = $password;
$userdata = compact('user_login', 'user_email', 'user_pass');
return wp_insert_user($userdata);
}
function create_user( $username, $password, $email ) {
return wp_create_user( $username, $password, $email );
}
?>
|