136
|
1 |
<?php |
|
2 |
/** |
|
3 |
* WordPress User API |
|
4 |
* |
|
5 |
* @package WordPress |
|
6 |
*/ |
|
7 |
|
|
8 |
/** |
|
9 |
* Authenticate user with remember capability. |
|
10 |
* |
|
11 |
* The credentials is an array that has 'user_login', 'user_password', and |
|
12 |
* 'remember' indices. If the credentials is not given, then the log in form |
|
13 |
* will be assumed and used if set. |
|
14 |
* |
|
15 |
* The various authentication cookies will be set by this function and will be |
|
16 |
* set for a longer period depending on if the 'remember' credential is set to |
|
17 |
* true. |
|
18 |
* |
|
19 |
* @since 2.5.0 |
|
20 |
* |
|
21 |
* @param array $credentials Optional. User info in order to sign on. |
|
22 |
* @param bool $secure_cookie Optional. Whether to use secure cookie. |
|
23 |
* @return object Either WP_Error on failure, or WP_User on success. |
|
24 |
*/ |
|
25 |
function wp_signon( $credentials = '', $secure_cookie = '' ) { |
|
26 |
if ( empty($credentials) ) { |
|
27 |
if ( ! empty($_POST['log']) ) |
|
28 |
$credentials['user_login'] = $_POST['log']; |
|
29 |
if ( ! empty($_POST['pwd']) ) |
|
30 |
$credentials['user_password'] = $_POST['pwd']; |
|
31 |
if ( ! empty($_POST['rememberme']) ) |
|
32 |
$credentials['remember'] = $_POST['rememberme']; |
|
33 |
} |
|
34 |
|
|
35 |
if ( !empty($credentials['remember']) ) |
|
36 |
$credentials['remember'] = true; |
|
37 |
else |
|
38 |
$credentials['remember'] = false; |
|
39 |
|
|
40 |
// TODO do we deprecate the wp_authentication action? |
|
41 |
do_action_ref_array('wp_authenticate', array(&$credentials['user_login'], &$credentials['user_password'])); |
|
42 |
|
|
43 |
if ( '' === $secure_cookie ) |
|
44 |
$secure_cookie = is_ssl() ? true : false; |
|
45 |
|
|
46 |
global $auth_secure_cookie; // XXX ugly hack to pass this to wp_authenticate_cookie |
|
47 |
$auth_secure_cookie = $secure_cookie; |
|
48 |
|
|
49 |
add_filter('authenticate', 'wp_authenticate_cookie', 30, 3); |
|
50 |
|
|
51 |
$user = wp_authenticate($credentials['user_login'], $credentials['user_password']); |
|
52 |
|
|
53 |
if ( is_wp_error($user) ) { |
|
54 |
if ( $user->get_error_codes() == array('empty_username', 'empty_password') ) { |
|
55 |
$user = new WP_Error('', ''); |
|
56 |
} |
|
57 |
|
|
58 |
return $user; |
|
59 |
} |
|
60 |
|
|
61 |
wp_set_auth_cookie($user->ID, $credentials['remember'], $secure_cookie); |
|
62 |
do_action('wp_login', $credentials['user_login']); |
|
63 |
return $user; |
|
64 |
} |
|
65 |
|
|
66 |
|
|
67 |
/** |
|
68 |
* Authenticate the user using the username and password. |
|
69 |
*/ |
|
70 |
add_filter('authenticate', 'wp_authenticate_username_password', 20, 3); |
|
71 |
function wp_authenticate_username_password($user, $username, $password) { |
|
72 |
if ( is_a($user, 'WP_User') ) { return $user; } |
|
73 |
|
|
74 |
if ( empty($username) || empty($password) ) { |
|
75 |
$error = new WP_Error(); |
|
76 |
|
|
77 |
if ( empty($username) ) |
|
78 |
$error->add('empty_username', __('<strong>ERROR</strong>: The username field is empty.')); |
|
79 |
|
|
80 |
if ( empty($password) ) |
|
81 |
$error->add('empty_password', __('<strong>ERROR</strong>: The password field is empty.')); |
|
82 |
|
|
83 |
return $error; |
|
84 |
} |
|
85 |
|
|
86 |
$userdata = get_userdatabylogin($username); |
|
87 |
|
|
88 |
if ( !$userdata ) { |
|
89 |
return new WP_Error('invalid_username', sprintf(__('<strong>ERROR</strong>: Invalid username. <a href="%s" title="Password Lost and Found">Lost your password</a>?'), site_url('wp-login.php?action=lostpassword', 'login'))); |
|
90 |
} |
|
91 |
|
|
92 |
$userdata = apply_filters('wp_authenticate_user', $userdata, $password); |
|
93 |
if ( is_wp_error($userdata) ) { |
|
94 |
return $userdata; |
|
95 |
} |
|
96 |
|
|
97 |
if ( !wp_check_password($password, $userdata->user_pass, $userdata->ID) ) { |
|
98 |
return new WP_Error('incorrect_password', sprintf(__('<strong>ERROR</strong>: Incorrect password. <a href="%s" title="Password Lost and Found">Lost your password</a>?'), site_url('wp-login.php?action=lostpassword', 'login'))); |
|
99 |
} |
|
100 |
|
|
101 |
$user = new WP_User($userdata->ID); |
|
102 |
return $user; |
|
103 |
} |
|
104 |
|
|
105 |
/** |
|
106 |
* Authenticate the user using the WordPress auth cookie. |
|
107 |
*/ |
|
108 |
function wp_authenticate_cookie($user, $username, $password) { |
|
109 |
if ( is_a($user, 'WP_User') ) { return $user; } |
|
110 |
|
|
111 |
if ( empty($username) && empty($password) ) { |
|
112 |
$user_id = wp_validate_auth_cookie(); |
|
113 |
if ( $user_id ) |
|
114 |
return new WP_User($user_id); |
|
115 |
|
|
116 |
global $auth_secure_cookie; |
|
117 |
|
|
118 |
if ( $auth_secure_cookie ) |
|
119 |
$auth_cookie = SECURE_AUTH_COOKIE; |
|
120 |
else |
|
121 |
$auth_cookie = AUTH_COOKIE; |
|
122 |
|
|
123 |
if ( !empty($_COOKIE[$auth_cookie]) ) |
|
124 |
return new WP_Error('expired_session', __('Please log in again.')); |
|
125 |
|
|
126 |
// If the cookie is not set, be silent. |
|
127 |
} |
|
128 |
|
|
129 |
return $user; |
|
130 |
} |
|
131 |
|
|
132 |
/** |
|
133 |
* Retrieve user data based on field. |
|
134 |
* |
|
135 |
* Use get_profile() will make a database query to get the value of the table |
|
136 |
* column. The value might be cached using the query cache, but care should be |
|
137 |
* taken when using the function to not make a lot of queries for retrieving |
|
138 |
* user profile information. |
|
139 |
* |
|
140 |
* If the $user parameter is not used, then the user will be retrieved from a |
|
141 |
* cookie of the user. Therefore, if the cookie does not exist, then no value |
|
142 |
* might be returned. Sanity checking must be done to ensure that when using |
|
143 |
* get_profile() that empty/null/false values are handled and that something is |
|
144 |
* at least displayed. |
|
145 |
* |
|
146 |
* @since 1.5.0 |
|
147 |
* @uses $wpdb WordPress database object to create queries. |
|
148 |
* |
|
149 |
* @param string $field User field to retrieve. |
|
150 |
* @param string $user Optional. User username. |
|
151 |
* @return string The value in the field. |
|
152 |
*/ |
|
153 |
function get_profile($field, $user = false) { |
|
154 |
global $wpdb; |
|
155 |
if ( !$user ) |
|
156 |
$user = esc_sql( $_COOKIE[USER_COOKIE] ); |
|
157 |
return $wpdb->get_var( $wpdb->prepare("SELECT $field FROM $wpdb->users WHERE user_login = %s", $user) ); |
|
158 |
} |
|
159 |
|
|
160 |
/** |
|
161 |
* Number of posts user has written. |
|
162 |
* |
|
163 |
* @since 0.71 |
|
164 |
* @uses $wpdb WordPress database object for queries. |
|
165 |
* |
|
166 |
* @param int $userid User ID. |
|
167 |
* @return int Amount of posts user has written. |
|
168 |
*/ |
|
169 |
function get_usernumposts($userid) { |
|
170 |
global $wpdb; |
|
171 |
$userid = (int) $userid; |
|
172 |
$count = $wpdb->get_var( $wpdb->prepare("SELECT COUNT(*) FROM $wpdb->posts WHERE post_author = %d AND post_type = 'post' AND ", $userid) . get_private_posts_cap_sql('post')); |
|
173 |
return apply_filters('get_usernumposts', $count, $userid); |
|
174 |
} |
|
175 |
|
|
176 |
/** |
|
177 |
* Check that the user login name and password is correct. |
|
178 |
* |
|
179 |
* @since 0.71 |
|
180 |
* @todo xmlrpc only. Maybe move to xmlrpc.php. |
|
181 |
* |
|
182 |
* @param string $user_login User name. |
|
183 |
* @param string $user_pass User password. |
|
184 |
* @return bool False if does not authenticate, true if username and password authenticates. |
|
185 |
*/ |
|
186 |
function user_pass_ok($user_login, $user_pass) { |
|
187 |
$user = wp_authenticate($user_login, $user_pass); |
|
188 |
if ( is_wp_error($user) ) |
|
189 |
return false; |
|
190 |
|
|
191 |
return true; |
|
192 |
} |
|
193 |
|
|
194 |
// |
|
195 |
// User option functions |
|
196 |
// |
|
197 |
|
|
198 |
/** |
|
199 |
* Retrieve user option that can be either global, user, or blog. |
|
200 |
* |
|
201 |
* If the user ID is not given, then the current user will be used instead. If |
|
202 |
* the user ID is given, then the user data will be retrieved. The filter for |
|
203 |
* the result, will also pass the original option name and finally the user data |
|
204 |
* object as the third parameter. |
|
205 |
* |
|
206 |
* The option will first check for the non-global name, then the global name, |
|
207 |
* and if it still doesn't find it, it will try the blog option. The option can |
|
208 |
* either be modified or set by a plugin. |
|
209 |
* |
|
210 |
* @since 2.0.0 |
|
211 |
* @uses $wpdb WordPress database object for queries. |
|
212 |
* @uses apply_filters() Calls 'get_user_option_$option' hook with result, |
|
213 |
* option parameter, and user data object. |
|
214 |
* |
|
215 |
* @param string $option User option name. |
|
216 |
* @param int $user Optional. User ID. |
|
217 |
* @param bool $check_blog_options Whether to check for an option in the options table if a per-user option does not exist. Default is true. |
|
218 |
* @return mixed |
|
219 |
*/ |
|
220 |
function get_user_option( $option, $user = 0, $check_blog_options = true ) { |
|
221 |
global $wpdb; |
|
222 |
|
|
223 |
$option = preg_replace('|[^a-z0-9_]|i', '', $option); |
|
224 |
if ( empty($user) ) |
|
225 |
$user = wp_get_current_user(); |
|
226 |
else |
|
227 |
$user = get_userdata($user); |
|
228 |
|
|
229 |
if ( isset( $user->{$wpdb->prefix . $option} ) ) // Blog specific |
|
230 |
$result = $user->{$wpdb->prefix . $option}; |
|
231 |
elseif ( isset( $user->{$option} ) ) // User specific and cross-blog |
|
232 |
$result = $user->{$option}; |
|
233 |
elseif ( $check_blog_options ) // Blog global |
|
234 |
$result = get_option( $option ); |
|
235 |
else |
|
236 |
$result = false; |
|
237 |
|
|
238 |
return apply_filters("get_user_option_{$option}", $result, $option, $user); |
|
239 |
} |
|
240 |
|
|
241 |
/** |
|
242 |
* Update user option with global blog capability. |
|
243 |
* |
|
244 |
* User options are just like user metadata except that they have support for |
|
245 |
* global blog options. If the 'global' parameter is false, which it is by default |
|
246 |
* it will prepend the WordPress table prefix to the option name. |
|
247 |
* |
|
248 |
* @since 2.0.0 |
|
249 |
* @uses $wpdb WordPress database object for queries |
|
250 |
* |
|
251 |
* @param int $user_id User ID |
|
252 |
* @param string $option_name User option name. |
|
253 |
* @param mixed $newvalue User option value. |
|
254 |
* @param bool $global Optional. Whether option name is blog specific or not. |
|
255 |
* @return unknown |
|
256 |
*/ |
|
257 |
function update_user_option( $user_id, $option_name, $newvalue, $global = false ) { |
|
258 |
global $wpdb; |
|
259 |
if ( !$global ) |
|
260 |
$option_name = $wpdb->prefix . $option_name; |
|
261 |
return update_usermeta( $user_id, $option_name, $newvalue ); |
|
262 |
} |
|
263 |
|
|
264 |
/** |
|
265 |
* Get users for the blog. |
|
266 |
* |
|
267 |
* For setups that use the multi-blog feature. Can be used outside of the |
|
268 |
* multi-blog feature. |
|
269 |
* |
|
270 |
* @since 2.2.0 |
|
271 |
* @uses $wpdb WordPress database object for queries |
|
272 |
* @uses $blog_id The Blog id of the blog for those that use more than one blog |
|
273 |
* |
|
274 |
* @param int $id Blog ID. |
|
275 |
* @return array List of users that are part of that Blog ID |
|
276 |
*/ |
|
277 |
function get_users_of_blog( $id = '' ) { |
|
278 |
global $wpdb, $blog_id; |
|
279 |
if ( empty($id) ) |
|
280 |
$id = (int) $blog_id; |
|
281 |
$users = $wpdb->get_results( "SELECT user_id, user_id AS ID, user_login, display_name, user_email, meta_value FROM $wpdb->users, $wpdb->usermeta WHERE {$wpdb->users}.ID = {$wpdb->usermeta}.user_id AND meta_key = '{$wpdb->prefix}capabilities' ORDER BY {$wpdb->usermeta}.user_id" ); |
|
282 |
return $users; |
|
283 |
} |
|
284 |
|
|
285 |
// |
|
286 |
// User meta functions |
|
287 |
// |
|
288 |
|
|
289 |
/** |
|
290 |
* Remove user meta data. |
|
291 |
* |
|
292 |
* @since 2.0.0 |
|
293 |
* @uses $wpdb WordPress database object for queries. |
|
294 |
* |
|
295 |
* @param int $user_id User ID. |
|
296 |
* @param string $meta_key Metadata key. |
|
297 |
* @param mixed $meta_value Metadata value. |
|
298 |
* @return bool True deletion completed and false if user_id is not a number. |
|
299 |
*/ |
|
300 |
function delete_usermeta( $user_id, $meta_key, $meta_value = '' ) { |
|
301 |
global $wpdb; |
|
302 |
if ( !is_numeric( $user_id ) ) |
|
303 |
return false; |
|
304 |
$meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key); |
|
305 |
|
|
306 |
if ( is_array($meta_value) || is_object($meta_value) ) |
|
307 |
$meta_value = serialize($meta_value); |
|
308 |
$meta_value = trim( $meta_value ); |
|
309 |
|
|
310 |
$cur = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s", $user_id, $meta_key) ); |
|
311 |
|
|
312 |
if ( $cur && $cur->umeta_id ) |
|
313 |
do_action( 'delete_usermeta', $cur->umeta_id, $user_id, $meta_key, $meta_value ); |
|
314 |
|
|
315 |
if ( ! empty($meta_value) ) |
|
316 |
$wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s AND meta_value = %s", $user_id, $meta_key, $meta_value) ); |
|
317 |
else |
|
318 |
$wpdb->query( $wpdb->prepare("DELETE FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s", $user_id, $meta_key) ); |
|
319 |
|
|
320 |
wp_cache_delete($user_id, 'users'); |
|
321 |
|
|
322 |
if ( $cur && $cur->umeta_id ) |
|
323 |
do_action( 'deleted_usermeta', $cur->umeta_id, $user_id, $meta_key, $meta_value ); |
|
324 |
|
|
325 |
return true; |
|
326 |
} |
|
327 |
|
|
328 |
/** |
|
329 |
* Retrieve user metadata. |
|
330 |
* |
|
331 |
* If $user_id is not a number, then the function will fail over with a 'false' |
|
332 |
* boolean return value. Other returned values depend on whether there is only |
|
333 |
* one item to be returned, which be that single item type. If there is more |
|
334 |
* than one metadata value, then it will be list of metadata values. |
|
335 |
* |
|
336 |
* @since 2.0.0 |
|
337 |
* @uses $wpdb WordPress database object for queries. |
|
338 |
* |
|
339 |
* @param int $user_id User ID |
|
340 |
* @param string $meta_key Optional. Metadata key. |
|
341 |
* @return mixed |
|
342 |
*/ |
|
343 |
function get_usermeta( $user_id, $meta_key = '') { |
|
344 |
global $wpdb; |
|
345 |
$user_id = (int) $user_id; |
|
346 |
|
|
347 |
if ( !$user_id ) |
|
348 |
return false; |
|
349 |
|
|
350 |
if ( !empty($meta_key) ) { |
|
351 |
$meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key); |
|
352 |
$user = wp_cache_get($user_id, 'users'); |
|
353 |
// Check the cached user object |
|
354 |
if ( false !== $user && isset($user->$meta_key) ) |
|
355 |
$metas = array($user->$meta_key); |
|
356 |
else |
|
357 |
$metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s", $user_id, $meta_key) ); |
|
358 |
} else { |
|
359 |
$metas = $wpdb->get_col( $wpdb->prepare("SELECT meta_value FROM $wpdb->usermeta WHERE user_id = %d", $user_id) ); |
|
360 |
} |
|
361 |
|
|
362 |
if ( empty($metas) ) { |
|
363 |
if ( empty($meta_key) ) |
|
364 |
return array(); |
|
365 |
else |
|
366 |
return ''; |
|
367 |
} |
|
368 |
|
|
369 |
$metas = array_map('maybe_unserialize', $metas); |
|
370 |
|
|
371 |
if ( count($metas) == 1 ) |
|
372 |
return $metas[0]; |
|
373 |
else |
|
374 |
return $metas; |
|
375 |
} |
|
376 |
|
|
377 |
/** |
|
378 |
* Update metadata of user. |
|
379 |
* |
|
380 |
* There is no need to serialize values, they will be serialized if it is |
|
381 |
* needed. The metadata key can only be a string with underscores. All else will |
|
382 |
* be removed. |
|
383 |
* |
|
384 |
* Will remove the metadata, if the meta value is empty. |
|
385 |
* |
|
386 |
* @since 2.0.0 |
|
387 |
* @uses $wpdb WordPress database object for queries |
|
388 |
* |
|
389 |
* @param int $user_id User ID |
|
390 |
* @param string $meta_key Metadata key. |
|
391 |
* @param mixed $meta_value Metadata value. |
|
392 |
* @return bool True on successful update, false on failure. |
|
393 |
*/ |
|
394 |
function update_usermeta( $user_id, $meta_key, $meta_value ) { |
|
395 |
global $wpdb; |
|
396 |
if ( !is_numeric( $user_id ) ) |
|
397 |
return false; |
|
398 |
$meta_key = preg_replace('|[^a-z0-9_]|i', '', $meta_key); |
|
399 |
|
|
400 |
/** @todo Might need fix because usermeta data is assumed to be already escaped */ |
|
401 |
if ( is_string($meta_value) ) |
|
402 |
$meta_value = stripslashes($meta_value); |
|
403 |
$meta_value = maybe_serialize($meta_value); |
|
404 |
|
|
405 |
if (empty($meta_value)) { |
|
406 |
return delete_usermeta($user_id, $meta_key); |
|
407 |
} |
|
408 |
|
|
409 |
$cur = $wpdb->get_row( $wpdb->prepare("SELECT * FROM $wpdb->usermeta WHERE user_id = %d AND meta_key = %s", $user_id, $meta_key) ); |
|
410 |
|
|
411 |
if ( $cur ) |
|
412 |
do_action( 'update_usermeta', $cur->umeta_id, $user_id, $meta_key, $meta_value ); |
|
413 |
|
|
414 |
if ( !$cur ) |
|
415 |
$wpdb->insert($wpdb->usermeta, compact('user_id', 'meta_key', 'meta_value') ); |
|
416 |
else if ( $cur->meta_value != $meta_value ) |
|
417 |
$wpdb->update($wpdb->usermeta, compact('meta_value'), compact('user_id', 'meta_key') ); |
|
418 |
else |
|
419 |
return false; |
|
420 |
|
|
421 |
wp_cache_delete($user_id, 'users'); |
|
422 |
|
|
423 |
if ( !$cur ) |
|
424 |
do_action( 'added_usermeta', $wpdb->insert_id, $user_id, $meta_key, $meta_value ); |
|
425 |
else |
|
426 |
do_action( 'updated_usermeta', $cur->umeta_id, $user_id, $meta_key, $meta_value ); |
|
427 |
|
|
428 |
return true; |
|
429 |
} |
|
430 |
|
|
431 |
// |
|
432 |
// Private helper functions |
|
433 |
// |
|
434 |
|
|
435 |
/** |
|
436 |
* Setup global user vars. |
|
437 |
* |
|
438 |
* Used by set_current_user() for back compat. Might be deprecated in the |
|
439 |
* future. |
|
440 |
* |
|
441 |
* @since 2.0.4 |
|
442 |
* @global string $userdata User description. |
|
443 |
* @global string $user_login The user username for logging in |
|
444 |
* @global int $user_level The level of the user |
|
445 |
* @global int $user_ID The ID of the user |
|
446 |
* @global string $user_email The email address of the user |
|
447 |
* @global string $user_url The url in the user's profile |
|
448 |
* @global string $user_pass_md5 MD5 of the user's password |
|
449 |
* @global string $user_identity The display name of the user |
|
450 |
* |
|
451 |
* @param int $for_user_id Optional. User ID to setup global data. |
|
452 |
*/ |
|
453 |
function setup_userdata($for_user_id = '') { |
|
454 |
global $user_login, $userdata, $user_level, $user_ID, $user_email, $user_url, $user_pass_md5, $user_identity; |
|
455 |
|
|
456 |
if ( '' == $for_user_id ) |
|
457 |
$user = wp_get_current_user(); |
|
458 |
else |
|
459 |
$user = new WP_User($for_user_id); |
|
460 |
|
|
461 |
if ( 0 == $user->ID ) |
|
462 |
return; |
|
463 |
|
|
464 |
$userdata = $user->data; |
|
465 |
$user_login = $user->user_login; |
|
466 |
$user_level = (int) isset($user->user_level) ? $user->user_level : 0; |
|
467 |
$user_ID = (int) $user->ID; |
|
468 |
$user_email = $user->user_email; |
|
469 |
$user_url = $user->user_url; |
|
470 |
$user_pass_md5 = md5($user->user_pass); |
|
471 |
$user_identity = $user->display_name; |
|
472 |
} |
|
473 |
|
|
474 |
/** |
|
475 |
* Create dropdown HTML content of users. |
|
476 |
* |
|
477 |
* The content can either be displayed, which it is by default or retrieved by |
|
478 |
* setting the 'echo' argument. The 'include' and 'exclude' arguments do not |
|
479 |
* need to be used; all users will be displayed in that case. Only one can be |
|
480 |
* used, either 'include' or 'exclude', but not both. |
|
481 |
* |
|
482 |
* The available arguments are as follows: |
|
483 |
* <ol> |
|
484 |
* <li>show_option_all - Text to show all and whether HTML option exists.</li> |
|
485 |
* <li>show_option_none - Text for show none and whether HTML option exists. |
|
486 |
* </li> |
|
487 |
* <li>orderby - SQL order by clause for what order the users appear. Default is |
|
488 |
* 'display_name'.</li> |
|
489 |
* <li>order - Default is 'ASC'. Can also be 'DESC'.</li> |
|
490 |
* <li>include - User IDs to include.</li> |
|
491 |
* <li>exclude - User IDs to exclude.</li> |
|
492 |
* <li>multi - Default is 'false'. Whether to skip the ID attribute on the 'select' element.</li> |
|
493 |
* <li>show - Default is 'display_name'. User table column to display. If the selected item is empty then the user_login will be displayed in parentesis</li> |
|
494 |
* <li>echo - Default is '1'. Whether to display or retrieve content.</li> |
|
495 |
* <li>selected - Which User ID is selected.</li> |
|
496 |
* <li>name - Default is 'user'. Name attribute of select element.</li> |
|
497 |
* <li>class - Class attribute of select element.</li> |
|
498 |
* </ol> |
|
499 |
* |
|
500 |
* @since 2.3.0 |
|
501 |
* @uses $wpdb WordPress database object for queries |
|
502 |
* |
|
503 |
* @param string|array $args Optional. Override defaults. |
|
504 |
* @return string|null Null on display. String of HTML content on retrieve. |
|
505 |
*/ |
|
506 |
function wp_dropdown_users( $args = '' ) { |
|
507 |
global $wpdb; |
|
508 |
$defaults = array( |
|
509 |
'show_option_all' => '', 'show_option_none' => '', |
|
510 |
'orderby' => 'display_name', 'order' => 'ASC', |
|
511 |
'include' => '', 'exclude' => '', 'multi' => 0, |
|
512 |
'show' => 'display_name', 'echo' => 1, |
|
513 |
'selected' => 0, 'name' => 'user', 'class' => '' |
|
514 |
); |
|
515 |
|
|
516 |
$defaults['selected'] = is_author() ? get_query_var( 'author' ) : 0; |
|
517 |
|
|
518 |
$r = wp_parse_args( $args, $defaults ); |
|
519 |
extract( $r, EXTR_SKIP ); |
|
520 |
|
|
521 |
$query = "SELECT * FROM $wpdb->users"; |
|
522 |
|
|
523 |
$query_where = array(); |
|
524 |
|
|
525 |
if ( is_array($include) ) |
|
526 |
$include = join(',', $include); |
|
527 |
$include = preg_replace('/[^0-9,]/', '', $include); // (int) |
|
528 |
if ( $include ) |
|
529 |
$query_where[] = "ID IN ($include)"; |
|
530 |
|
|
531 |
if ( is_array($exclude) ) |
|
532 |
$exclude = join(',', $exclude); |
|
533 |
$exclude = preg_replace('/[^0-9,]/', '', $exclude); // (int) |
|
534 |
if ( $exclude ) |
|
535 |
$query_where[] = "ID NOT IN ($exclude)"; |
|
536 |
|
|
537 |
if ( $query_where ) |
|
538 |
$query .= " WHERE " . join(' AND', $query_where); |
|
539 |
|
|
540 |
$query .= " ORDER BY $orderby $order"; |
|
541 |
|
|
542 |
$users = $wpdb->get_results( $query ); |
|
543 |
|
|
544 |
$output = ''; |
|
545 |
if ( !empty($users) ) { |
|
546 |
$id = $multi ? "" : "id='$name'"; |
|
547 |
|
|
548 |
$output = "<select name='$name' $id class='$class'>\n"; |
|
549 |
|
|
550 |
if ( $show_option_all ) |
|
551 |
$output .= "\t<option value='0'>$show_option_all</option>\n"; |
|
552 |
|
|
553 |
if ( $show_option_none ) |
|
554 |
$output .= "\t<option value='-1'>$show_option_none</option>\n"; |
|
555 |
|
|
556 |
foreach ( (array) $users as $user ) { |
|
557 |
$user->ID = (int) $user->ID; |
|
558 |
$_selected = $user->ID == $selected ? " selected='selected'" : ''; |
|
559 |
$display = !empty($user->$show) ? $user->$show : '('. $user->user_login . ')'; |
|
560 |
$output .= "\t<option value='$user->ID'$_selected>" . esc_html($display) . "</option>\n"; |
|
561 |
} |
|
562 |
|
|
563 |
$output .= "</select>"; |
|
564 |
} |
|
565 |
|
|
566 |
$output = apply_filters('wp_dropdown_users', $output); |
|
567 |
|
|
568 |
if ( $echo ) |
|
569 |
echo $output; |
|
570 |
|
|
571 |
return $output; |
|
572 |
} |
|
573 |
|
|
574 |
/** |
|
575 |
* Add user meta data as properties to given user object. |
|
576 |
* |
|
577 |
* The finished user data is cached, but the cache is not used to fill in the |
|
578 |
* user data for the given object. Once the function has been used, the cache |
|
579 |
* should be used to retrieve user data. The purpose seems then to be to ensure |
|
580 |
* that the data in the object is always fresh. |
|
581 |
* |
|
582 |
* @access private |
|
583 |
* @since 2.5.0 |
|
584 |
* @uses $wpdb WordPress database object for queries |
|
585 |
* |
|
586 |
* @param object $user The user data object. |
|
587 |
*/ |
|
588 |
function _fill_user( &$user ) { |
|
589 |
global $wpdb; |
|
590 |
|
|
591 |
$show = $wpdb->hide_errors(); |
|
592 |
$metavalues = $wpdb->get_results($wpdb->prepare("SELECT meta_key, meta_value FROM $wpdb->usermeta WHERE user_id = %d", $user->ID)); |
|
593 |
$wpdb->show_errors($show); |
|
594 |
|
|
595 |
if ( $metavalues ) { |
|
596 |
foreach ( (array) $metavalues as $meta ) { |
|
597 |
$value = maybe_unserialize($meta->meta_value); |
|
598 |
$user->{$meta->meta_key} = $value; |
|
599 |
} |
|
600 |
} |
|
601 |
|
|
602 |
$level = $wpdb->prefix . 'user_level'; |
|
603 |
if ( isset( $user->{$level} ) ) |
|
604 |
$user->user_level = $user->{$level}; |
|
605 |
|
|
606 |
// For backwards compat. |
|
607 |
if ( isset($user->first_name) ) |
|
608 |
$user->user_firstname = $user->first_name; |
|
609 |
if ( isset($user->last_name) ) |
|
610 |
$user->user_lastname = $user->last_name; |
|
611 |
if ( isset($user->description) ) |
|
612 |
$user->user_description = $user->description; |
|
613 |
|
|
614 |
wp_cache_add($user->ID, $user, 'users'); |
|
615 |
wp_cache_add($user->user_login, $user->ID, 'userlogins'); |
|
616 |
wp_cache_add($user->user_email, $user->ID, 'useremail'); |
|
617 |
wp_cache_add($user->user_nicename, $user->ID, 'userslugs'); |
|
618 |
} |
|
619 |
|
|
620 |
/** |
|
621 |
* Sanitize every user field. |
|
622 |
* |
|
623 |
* If the context is 'raw', then the user object or array will get minimal santization of the int fields. |
|
624 |
* |
|
625 |
* @since 2.3.0 |
|
626 |
* @uses sanitize_user_field() Used to sanitize the fields. |
|
627 |
* |
|
628 |
* @param object|array $user The User Object or Array |
|
629 |
* @param string $context Optional, default is 'display'. How to sanitize user fields. |
|
630 |
* @return object|array The now sanitized User Object or Array (will be the same type as $user) |
|
631 |
*/ |
|
632 |
function sanitize_user_object($user, $context = 'display') { |
|
633 |
if ( is_object($user) ) { |
|
634 |
if ( !isset($user->ID) ) |
|
635 |
$user->ID = 0; |
|
636 |
if ( isset($user->data) ) |
|
637 |
$vars = get_object_vars( $user->data ); |
|
638 |
else |
|
639 |
$vars = get_object_vars($user); |
|
640 |
foreach ( array_keys($vars) as $field ) { |
|
641 |
if ( is_string($user->$field) || is_numeric($user->$field) ) |
|
642 |
$user->$field = sanitize_user_field($field, $user->$field, $user->ID, $context); |
|
643 |
} |
|
644 |
$user->filter = $context; |
|
645 |
} else { |
|
646 |
if ( !isset($user['ID']) ) |
|
647 |
$user['ID'] = 0; |
|
648 |
foreach ( array_keys($user) as $field ) |
|
649 |
$user[$field] = sanitize_user_field($field, $user[$field], $user['ID'], $context); |
|
650 |
$user['filter'] = $context; |
|
651 |
} |
|
652 |
|
|
653 |
return $user; |
|
654 |
} |
|
655 |
|
|
656 |
/** |
|
657 |
* Sanitize user field based on context. |
|
658 |
* |
|
659 |
* Possible context values are: 'raw', 'edit', 'db', 'display', 'attribute' and 'js'. The |
|
660 |
* 'display' context is used by default. 'attribute' and 'js' contexts are treated like 'display' |
|
661 |
* when calling filters. |
|
662 |
* |
|
663 |
* @since 2.3.0 |
|
664 |
* @uses apply_filters() Calls 'edit_$field' and '${field_no_prefix}_edit_pre' passing $value and |
|
665 |
* $user_id if $context == 'edit' and field name prefix == 'user_'. |
|
666 |
* |
|
667 |
* @uses apply_filters() Calls 'edit_user_$field' passing $value and $user_id if $context == 'db'. |
|
668 |
* @uses apply_filters() Calls 'pre_$field' passing $value if $context == 'db' and field name prefix == 'user_'. |
|
669 |
* @uses apply_filters() Calls '${field}_pre' passing $value if $context == 'db' and field name prefix != 'user_'. |
|
670 |
* |
|
671 |
* @uses apply_filters() Calls '$field' passing $value, $user_id and $context if $context == anything |
|
672 |
* other than 'raw', 'edit' and 'db' and field name prefix == 'user_'. |
|
673 |
* @uses apply_filters() Calls 'user_$field' passing $value if $context == anything other than 'raw', |
|
674 |
* 'edit' and 'db' and field name prefix != 'user_'. |
|
675 |
* |
|
676 |
* @param string $field The user Object field name. |
|
677 |
* @param mixed $value The user Object value. |
|
678 |
* @param int $user_id user ID. |
|
679 |
* @param string $context How to sanitize user fields. Looks for 'raw', 'edit', 'db', 'display', |
|
680 |
* 'attribute' and 'js'. |
|
681 |
* @return mixed Sanitized value. |
|
682 |
*/ |
|
683 |
function sanitize_user_field($field, $value, $user_id, $context) { |
|
684 |
$int_fields = array('ID'); |
|
685 |
if ( in_array($field, $int_fields) ) |
|
686 |
$value = (int) $value; |
|
687 |
|
|
688 |
if ( 'raw' == $context ) |
|
689 |
return $value; |
|
690 |
|
|
691 |
if ( !is_string($value) && !is_numeric($value) ) |
|
692 |
return $value; |
|
693 |
|
|
694 |
$prefixed = false; |
|
695 |
if ( false !== strpos($field, 'user_') ) { |
|
696 |
$prefixed = true; |
|
697 |
$field_no_prefix = str_replace('user_', '', $field); |
|
698 |
} |
|
699 |
|
|
700 |
if ( 'edit' == $context ) { |
|
701 |
if ( $prefixed ) { |
|
702 |
$value = apply_filters("edit_$field", $value, $user_id); |
|
703 |
} else { |
|
704 |
$value = apply_filters("edit_user_$field", $value, $user_id); |
|
705 |
} |
|
706 |
|
|
707 |
if ( 'description' == $field ) |
|
708 |
$value = esc_html($value); |
|
709 |
else |
|
710 |
$value = esc_attr($value); |
|
711 |
} else if ( 'db' == $context ) { |
|
712 |
if ( $prefixed ) { |
|
713 |
$value = apply_filters("pre_$field", $value); |
|
714 |
} else { |
|
715 |
$value = apply_filters("pre_user_$field", $value); |
|
716 |
} |
|
717 |
} else { |
|
718 |
// Use display filters by default. |
|
719 |
if ( $prefixed ) |
|
720 |
$value = apply_filters($field, $value, $user_id, $context); |
|
721 |
else |
|
722 |
$value = apply_filters("user_$field", $value, $user_id, $context); |
|
723 |
} |
|
724 |
|
|
725 |
if ( 'user_url' == $field ) |
|
726 |
$value = esc_url($value); |
|
727 |
|
|
728 |
if ( 'attribute' == $context ) |
|
729 |
$value = esc_attr($value); |
|
730 |
else if ( 'js' == $context ) |
|
731 |
$value = esc_js($value); |
|
732 |
|
|
733 |
return $value; |
|
734 |
} |
|
735 |
|
|
736 |
?> |