| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * Core User API 4 * 5 * @package WordPress 6 * @subpackage Users 7 */ 8 9 /** 10 * Authenticates and logs a user in with 'remember' capability. 11 * 12 * The credentials is an array that has 'user_login', 'user_password', and 13 * 'remember' indices. If the credentials is not given, then the log in form 14 * will be assumed and used if set. 15 * 16 * The various authentication cookies will be set by this function and will be 17 * set for a longer period depending on if the 'remember' credential is set to 18 * true. 19 * 20 * Note: wp_signon() doesn't handle setting the current user. This means that if the 21 * function is called before the {@see 'init'} hook is fired, is_user_logged_in() will 22 * evaluate as false until that point. If is_user_logged_in() is needed in conjunction 23 * with wp_signon(), wp_set_current_user() should be called explicitly. 24 * 25 * @since 2.5.0 26 * 27 * @global string $auth_secure_cookie 28 * @global wpdb $wpdb WordPress database abstraction object. 29 * 30 * @param array $credentials { 31 * Optional. User info in order to sign on. 32 * 33 * @type string $user_login Username. 34 * @type string $user_password User password. 35 * @type bool $remember Whether to 'remember' the user. Increases the time 36 * that the cookie will be kept. Default false. 37 * } 38 * @param string|bool $secure_cookie Optional. Whether to use secure cookie. 39 * @return WP_User|WP_Error WP_User on success, WP_Error on failure. 40 */ 41 function wp_signon( $credentials = array(), $secure_cookie = '' ) { 42 global $auth_secure_cookie, $wpdb; 43 44 if ( empty( $credentials ) ) { 45 $credentials = array( 46 'user_login' => '', 47 'user_password' => '', 48 'remember' => false, 49 ); 50 51 if ( ! empty( $_POST['log'] ) && is_string( $_POST['log'] ) ) { 52 $credentials['user_login'] = wp_unslash( $_POST['log'] ); 53 } 54 if ( ! empty( $_POST['pwd'] ) && is_string( $_POST['pwd'] ) ) { 55 $credentials['user_password'] = $_POST['pwd']; 56 } 57 if ( ! empty( $_POST['rememberme'] ) ) { 58 $credentials['remember'] = $_POST['rememberme']; 59 } 60 } 61 62 if ( ! empty( $credentials['remember'] ) ) { 63 $credentials['remember'] = true; 64 } else { 65 $credentials['remember'] = false; 66 } 67 68 /** 69 * Fires before the user is authenticated. 70 * 71 * The variables passed to the callbacks are passed by reference, 72 * and can be modified by callback functions. 73 * 74 * @since 1.5.1 75 * 76 * @todo Decide whether to deprecate the wp_authenticate action. 77 * 78 * @param string $user_login Username (passed by reference). 79 * @param string $user_password User password (passed by reference). 80 */ 81 do_action_ref_array( 'wp_authenticate', array( &$credentials['user_login'], &$credentials['user_password'] ) ); 82 83 if ( '' === $secure_cookie ) { 84 $secure_cookie = is_ssl(); 85 } 86 87 /** 88 * Filters whether to use a secure sign-on cookie. 89 * 90 * @since 3.1.0 91 * 92 * @param bool $secure_cookie Whether to use a secure sign-on cookie. 93 * @param array $credentials { 94 * Array of entered sign-on data. 95 * 96 * @type string $user_login Username. 97 * @type string $user_password Password entered. 98 * @type bool $remember Whether to 'remember' the user. Increases the time 99 * that the cookie will be kept. Default false. 100 * } 101 */ 102 $secure_cookie = apply_filters( 'secure_signon_cookie', $secure_cookie, $credentials ); 103 104 // XXX ugly hack to pass this to wp_authenticate_cookie(). 105 $auth_secure_cookie = $secure_cookie; 106 107 add_filter( 'authenticate', 'wp_authenticate_cookie', 30, 3 ); 108 109 $user = wp_authenticate( $credentials['user_login'], $credentials['user_password'] ); 110 111 if ( is_wp_error( $user ) ) { 112 return $user; 113 } 114 115 wp_set_auth_cookie( $user->ID, $credentials['remember'], $secure_cookie ); 116 117 // Clear `user_activation_key` after a successful login. 118 if ( ! empty( $user->user_activation_key ) ) { 119 $wpdb->update( 120 $wpdb->users, 121 array( 122 'user_activation_key' => '', 123 ), 124 array( 'ID' => $user->ID ) 125 ); 126 127 $user->user_activation_key = ''; 128 } 129 130 /** 131 * Fires after the user has successfully logged in. 132 * 133 * @since 1.5.0 134 * 135 * @param string $user_login Username. 136 * @param WP_User $user WP_User object of the logged-in user. 137 */ 138 do_action( 'wp_login', $user->user_login, $user ); 139 140 return $user; 141 } 142 143 /** 144 * Authenticates a user, confirming the username and password are valid. 145 * 146 * @since 2.8.0 147 * 148 * @param WP_User|WP_Error|null $user WP_User or WP_Error object from a previous callback. Default null. 149 * @param string $username Username for authentication. 150 * @param string $password Password for authentication. 151 * @return WP_User|WP_Error WP_User on success, WP_Error on failure. 152 */ 153 function wp_authenticate_username_password( 154 $user, 155 $username, 156 #[\SensitiveParameter] 157 $password 158 ) { 159 if ( $user instanceof WP_User ) { 160 return $user; 161 } 162 163 if ( empty( $username ) || empty( $password ) ) { 164 if ( is_wp_error( $user ) ) { 165 return $user; 166 } 167 168 $error = new WP_Error(); 169 170 if ( empty( $username ) ) { 171 $error->add( 'empty_username', __( '<strong>Error:</strong> The username field is empty.' ) ); 172 } 173 174 if ( empty( $password ) ) { 175 $error->add( 'empty_password', __( '<strong>Error:</strong> The password field is empty.' ) ); 176 } 177 178 return $error; 179 } 180 181 $user = get_user_by( 'login', $username ); 182 183 if ( ! $user ) { 184 return new WP_Error( 185 'invalid_username', 186 sprintf( 187 /* translators: %s: User name. */ 188 __( '<strong>Error:</strong> The username <strong>%s</strong> is not registered on this site. If you are unsure of your username, try your email address instead.' ), 189 $username 190 ) 191 ); 192 } 193 194 /** 195 * Filters whether the given user can be authenticated with the provided password. 196 * 197 * @since 2.5.0 198 * 199 * @param WP_User|WP_Error $user WP_User or WP_Error object if a previous 200 * callback failed authentication. 201 * @param string $password Password to check against the user. 202 */ 203 $user = apply_filters( 'wp_authenticate_user', $user, $password ); 204 if ( is_wp_error( $user ) ) { 205 return $user; 206 } 207 208 $valid = wp_check_password( $password, $user->user_pass, $user->ID ); 209 210 if ( ! $valid ) { 211 return new WP_Error( 212 'incorrect_password', 213 sprintf( 214 /* translators: %s: User name. */ 215 __( '<strong>Error:</strong> The password you entered for the username %s is incorrect.' ), 216 '<strong>' . $username . '</strong>' 217 ) . 218 ' <a href="' . wp_lostpassword_url() . '">' . 219 __( 'Lost your password?' ) . 220 '</a>' 221 ); 222 } 223 224 if ( wp_password_needs_rehash( $user->user_pass, $user->ID ) ) { 225 wp_set_password( $password, $user->ID ); 226 } 227 228 return $user; 229 } 230 231 /** 232 * Authenticates a user using the email and password. 233 * 234 * @since 4.5.0 235 * 236 * @param WP_User|WP_Error|null $user WP_User or WP_Error object if a previous 237 * callback failed authentication. 238 * @param string $email Email address for authentication. 239 * @param string $password Password for authentication. 240 * @return WP_User|WP_Error WP_User on success, WP_Error on failure. 241 */ 242 function wp_authenticate_email_password( 243 $user, 244 $email, 245 #[\SensitiveParameter] 246 $password 247 ) { 248 if ( $user instanceof WP_User ) { 249 return $user; 250 } 251 252 if ( empty( $email ) || empty( $password ) ) { 253 if ( is_wp_error( $user ) ) { 254 return $user; 255 } 256 257 $error = new WP_Error(); 258 259 if ( empty( $email ) ) { 260 // Uses 'empty_username' for back-compat with wp_signon(). 261 $error->add( 'empty_username', __( '<strong>Error:</strong> The email field is empty.' ) ); 262 } 263 264 if ( empty( $password ) ) { 265 $error->add( 'empty_password', __( '<strong>Error:</strong> The password field is empty.' ) ); 266 } 267 268 return $error; 269 } 270 271 if ( ! is_email( $email ) ) { 272 return $user; 273 } 274 275 $user = get_user_by( 'email', $email ); 276 277 if ( ! $user ) { 278 return new WP_Error( 279 'invalid_email', 280 __( 'Unknown email address. Check again or try your username.' ) 281 ); 282 } 283 284 /** This filter is documented in wp-includes/user.php */ 285 $user = apply_filters( 'wp_authenticate_user', $user, $password ); 286 287 if ( is_wp_error( $user ) ) { 288 return $user; 289 } 290 291 $valid = wp_check_password( $password, $user->user_pass, $user->ID ); 292 293 if ( ! $valid ) { 294 return new WP_Error( 295 'incorrect_password', 296 sprintf( 297 /* translators: %s: Email address. */ 298 __( '<strong>Error:</strong> The password you entered for the email address %s is incorrect.' ), 299 '<strong>' . $email . '</strong>' 300 ) . 301 ' <a href="' . wp_lostpassword_url() . '">' . 302 __( 'Lost your password?' ) . 303 '</a>' 304 ); 305 } 306 307 if ( wp_password_needs_rehash( $user->user_pass, $user->ID ) ) { 308 wp_set_password( $password, $user->ID ); 309 } 310 311 return $user; 312 } 313 314 /** 315 * Authenticates the user using the WordPress auth cookie. 316 * 317 * @since 2.8.0 318 * 319 * @global string $auth_secure_cookie 320 * 321 * @param WP_User|WP_Error|null $user WP_User or WP_Error object from a previous callback. Default null. 322 * @param string $username Username. If not empty, cancels the cookie authentication. 323 * @param string $password Password. If not empty, cancels the cookie authentication. 324 * @return WP_User|WP_Error WP_User on success, WP_Error on failure. 325 */ 326 function wp_authenticate_cookie( 327 $user, 328 $username, 329 #[\SensitiveParameter] 330 $password 331 ) { 332 global $auth_secure_cookie; 333 334 if ( $user instanceof WP_User ) { 335 return $user; 336 } 337 338 if ( empty( $username ) && empty( $password ) ) { 339 $user_id = wp_validate_auth_cookie(); 340 if ( $user_id ) { 341 return new WP_User( $user_id ); 342 } 343 344 if ( $auth_secure_cookie ) { 345 $auth_cookie = SECURE_AUTH_COOKIE; 346 } else { 347 $auth_cookie = AUTH_COOKIE; 348 } 349 350 if ( ! empty( $_COOKIE[ $auth_cookie ] ) ) { 351 return new WP_Error( 'expired_session', __( 'Please log in again.' ) ); 352 } 353 354 // If the cookie is not set, be silent. 355 } 356 357 return $user; 358 } 359 360 /** 361 * Authenticates the user using an application password. 362 * 363 * @since 5.6.0 364 * 365 * @param WP_User|WP_Error|null $input_user WP_User or WP_Error object if a previous 366 * callback failed authentication. 367 * @param string $username Username for authentication. 368 * @param string $password Password for authentication. 369 * @return WP_User|WP_Error|null WP_User on success, WP_Error on failure, null if 370 * null is passed in and this isn't an API request. 371 */ 372 function wp_authenticate_application_password( 373 $input_user, 374 $username, 375 #[\SensitiveParameter] 376 $password 377 ) { 378 if ( $input_user instanceof WP_User ) { 379 return $input_user; 380 } 381 382 if ( ! WP_Application_Passwords::is_in_use() ) { 383 return $input_user; 384 } 385 386 // The 'REST_REQUEST' check here may happen too early for the constant to be available. 387 $is_api_request = ( ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) || ( defined( 'REST_REQUEST' ) && REST_REQUEST ) ); 388 389 /** 390 * Filters whether this is an API request that Application Passwords can be used on. 391 * 392 * By default, Application Passwords is available for the REST API and XML-RPC. 393 * 394 * @since 5.6.0 395 * 396 * @param bool $is_api_request If this is an acceptable API request. 397 */ 398 $is_api_request = apply_filters( 'application_password_is_api_request', $is_api_request ); 399 400 if ( ! $is_api_request ) { 401 return $input_user; 402 } 403 404 $error = null; 405 $user = get_user_by( 'login', $username ); 406 407 if ( ! $user && is_email( $username ) ) { 408 $user = get_user_by( 'email', $username ); 409 } 410 411 // If the login name is invalid, short circuit. 412 if ( ! $user ) { 413 if ( is_email( $username ) ) { 414 $error = new WP_Error( 415 'invalid_email', 416 __( '<strong>Error:</strong> Unknown email address. Check again or try your username.' ) 417 ); 418 } else { 419 $error = new WP_Error( 420 'invalid_username', 421 __( '<strong>Error:</strong> Unknown username. Check again or try your email address.' ) 422 ); 423 } 424 } elseif ( ! wp_is_application_passwords_available() ) { 425 $error = new WP_Error( 426 'application_passwords_disabled', 427 __( 'Application passwords are not available.' ) 428 ); 429 } elseif ( ! wp_is_application_passwords_available_for_user( $user ) ) { 430 $error = new WP_Error( 431 'application_passwords_disabled_for_user', 432 __( 'Application passwords are not available for your account. Please contact the site administrator for assistance.' ) 433 ); 434 } 435 436 if ( $error ) { 437 /** 438 * Fires when an application password failed to authenticate the user. 439 * 440 * @since 5.6.0 441 * 442 * @param WP_Error $error The authentication error. 443 */ 444 do_action( 'application_password_failed_authentication', $error ); 445 446 return $error; 447 } 448 449 /* 450 * Strips out anything non-alphanumeric. This is so passwords can be used with 451 * or without spaces to indicate the groupings for readability. 452 * 453 * Generated application passwords are exclusively alphanumeric. 454 */ 455 $password = preg_replace( '/[^a-z\d]/i', '', $password ); 456 457 $hashed_passwords = WP_Application_Passwords::get_user_application_passwords( $user->ID ); 458 459 foreach ( $hashed_passwords as $key => $item ) { 460 if ( ! WP_Application_Passwords::check_password( $password, $item['password'] ) ) { 461 continue; 462 } 463 464 $error = new WP_Error(); 465 466 /** 467 * Fires when an application password has been successfully checked as valid. 468 * 469 * This allows for plugins to add additional constraints to prevent an application password from being used. 470 * 471 * @since 5.6.0 472 * 473 * @param WP_Error $error The error object. 474 * @param WP_User $user The user authenticating. 475 * @param array $item The details about the application password. 476 * @param string $password The raw supplied password. 477 */ 478 do_action( 'wp_authenticate_application_password_errors', $error, $user, $item, $password ); 479 480 if ( $error->has_errors() ) { 481 /** This action is documented in wp-includes/user.php */ 482 do_action( 'application_password_failed_authentication', $error ); 483 484 return $error; 485 } 486 487 WP_Application_Passwords::record_application_password_usage( $user->ID, $item['uuid'] ); 488 489 /** 490 * Fires after an application password was used for authentication. 491 * 492 * @since 5.6.0 493 * 494 * @param WP_User $user The user who was authenticated. 495 * @param array $item The application password used. 496 */ 497 do_action( 'application_password_did_authenticate', $user, $item ); 498 499 return $user; 500 } 501 502 $error = new WP_Error( 503 'incorrect_password', 504 __( 'The provided password is an invalid application password.' ) 505 ); 506 507 /** This action is documented in wp-includes/user.php */ 508 do_action( 'application_password_failed_authentication', $error ); 509 510 return $error; 511 } 512 513 /** 514 * Validates the application password credentials passed via Basic Authentication. 515 * 516 * @since 5.6.0 517 * 518 * @param int|false $input_user User ID if one has been determined, false otherwise. 519 * @return int|false The authenticated user ID if successful, false otherwise. 520 */ 521 function wp_validate_application_password( $input_user ) { 522 // Don't authenticate twice. 523 if ( ! empty( $input_user ) ) { 524 return $input_user; 525 } 526 527 if ( ! wp_is_application_passwords_available() ) { 528 return $input_user; 529 } 530 531 // Both $_SERVER['PHP_AUTH_USER'] and $_SERVER['PHP_AUTH_PW'] must be set in order to attempt authentication. 532 if ( ! isset( $_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'] ) ) { 533 return $input_user; 534 } 535 536 $authenticated = wp_authenticate_application_password( null, $_SERVER['PHP_AUTH_USER'], $_SERVER['PHP_AUTH_PW'] ); 537 538 if ( $authenticated instanceof WP_User ) { 539 return $authenticated->ID; 540 } 541 542 // If it wasn't a user what got returned, just pass on what we had received originally. 543 return $input_user; 544 } 545 546 /** 547 * For Multisite blogs, checks if the authenticated user has been marked as a 548 * spammer, or if the user's primary blog has been marked as spam. 549 * 550 * @since 3.7.0 551 * 552 * @param WP_User|WP_Error|null $user WP_User or WP_Error object from a previous callback. Default null. 553 * @return WP_User|WP_Error WP_User on success, WP_Error if the user is considered a spammer. 554 */ 555 function wp_authenticate_spam_check( $user ) { 556 if ( $user instanceof WP_User && is_multisite() ) { 557 /** 558 * Filters whether the user has been marked as a spammer. 559 * 560 * @since 3.7.0 561 * 562 * @param bool $spammed Whether the user is considered a spammer. 563 * @param WP_User $user User to check against. 564 */ 565 $spammed = apply_filters( 'check_is_user_spammed', is_user_spammy( $user ), $user ); 566 567 if ( $spammed ) { 568 return new WP_Error( 'spammer_account', __( '<strong>Error:</strong> Your account has been marked as a spammer.' ) ); 569 } 570 } 571 return $user; 572 } 573 574 /** 575 * Validates the logged-in cookie. 576 * 577 * Checks the logged-in cookie if the previous auth cookie could not be 578 * validated and parsed. 579 * 580 * This is a callback for the {@see 'determine_current_user'} filter, rather than API. 581 * 582 * @since 3.9.0 583 * 584 * @param int|false $user_id The user ID (or false) as received from 585 * the `determine_current_user` filter. 586 * @return int|false User ID if validated, false otherwise. If a user ID from 587 * an earlier filter callback is received, that value is returned. 588 */ 589 function wp_validate_logged_in_cookie( $user_id ) { 590 if ( $user_id ) { 591 return $user_id; 592 } 593 594 if ( is_blog_admin() || is_network_admin() || empty( $_COOKIE[ LOGGED_IN_COOKIE ] ) ) { 595 return false; 596 } 597 598 return wp_validate_auth_cookie( $_COOKIE[ LOGGED_IN_COOKIE ], 'logged_in' ); 599 } 600 601 /** 602 * Gets the number of posts a user has written. 603 * 604 * @since 3.0.0 605 * @since 4.1.0 Added `$post_type` argument. 606 * @since 4.3.0 Added `$public_only` argument. Added the ability to pass an array 607 * of post types to `$post_type`. 608 * 609 * @global wpdb $wpdb WordPress database abstraction object. 610 * 611 * @param int $userid User ID. 612 * @param array|string $post_type Optional. Single post type or array of post types to count the number of posts for. Default 'post'. 613 * @param bool $public_only Optional. Whether to only return counts for public posts. Default false. 614 * @return string Number of posts the user has written in this post type. 615 */ 616 function count_user_posts( $userid, $post_type = 'post', $public_only = false ) { 617 global $wpdb; 618 619 $post_type = array_unique( (array) $post_type ); 620 sort( $post_type ); 621 622 $where = get_posts_by_author_sql( $post_type, true, $userid, $public_only ); 623 $query = "SELECT COUNT(*) FROM $wpdb->posts $where"; 624 625 $last_changed = wp_cache_get_last_changed( 'posts' ); 626 $cache_key = 'count_user_posts:' . md5( $query ); 627 $count = wp_cache_get_salted( $cache_key, 'post-queries', $last_changed ); 628 if ( false === $count ) { 629 $count = $wpdb->get_var( $query ); 630 wp_cache_set_salted( $cache_key, $count, 'post-queries', $last_changed ); 631 } 632 633 /** 634 * Filters the number of posts a user has written. 635 * 636 * @since 2.7.0 637 * @since 4.1.0 Added `$post_type` argument. 638 * @since 4.3.1 Added `$public_only` argument. 639 * 640 * @param string $count The user's post count as a numeric string. 641 * @param int $userid User ID. 642 * @param string|array $post_type Single post type or array of post types to count the number of posts for. 643 * @param bool $public_only Whether to limit counted posts to public posts. 644 */ 645 return apply_filters( 'get_usernumposts', $count, $userid, $post_type, $public_only ); 646 } 647 648 /** 649 * Gets the number of posts written by a list of users. 650 * 651 * @since 3.0.0 652 * @since 6.9.0 The results are now cached. 653 * 654 * @global wpdb $wpdb WordPress database abstraction object. 655 * 656 * @param int[] $users Array of user IDs. 657 * @param string|string[] $post_type Optional. Single post type or array of post types to check. Defaults to 'post'. 658 * @param bool $public_only Optional. Only return counts for public posts. Defaults to false. 659 * @return array<int, string> Amount of posts each user has written, as strings, keyed by user ID. 660 */ 661 function count_many_users_posts( $users, $post_type = 'post', $public_only = false ) { 662 global $wpdb; 663 664 if ( empty( $users ) || ! is_array( $users ) ) { 665 return array(); 666 } 667 668 /** 669 * Filters whether to short-circuit performing the post counts. 670 * 671 * When filtering, return an array of posts counts as strings, keyed 672 * by the user ID. 673 * 674 * @since 6.8.0 675 * 676 * @param string[]|null $count The post counts. Return a non-null value to short-circuit. 677 * @param int[] $users Array of user IDs. 678 * @param string|string[] $post_type Single post type or array of post types to check. 679 * @param bool $public_only Whether to only return counts for public posts. 680 */ 681 $pre = apply_filters( 'pre_count_many_users_posts', null, $users, $post_type, $public_only ); 682 if ( null !== $pre ) { 683 return $pre; 684 } 685 686 // Cleanup the users array. Remove duplicates and sort for consistent ordering. 687 $users = array_unique( array_filter( array_map( 'intval', $users ) ) ); 688 sort( $users ); 689 690 // Cleanup the post type argument. Remove duplicates and sort for consistent ordering. 691 $post_type = array_unique( (array) $post_type ); 692 sort( $post_type ); 693 694 $userlist = implode( ',', $users ); 695 $where = get_posts_by_author_sql( $post_type, true, null, $public_only ); 696 $query = "SELECT post_author, COUNT(*) FROM $wpdb->posts $where AND post_author IN ($userlist) GROUP BY post_author"; 697 $cache_key = 'count_many_users_posts:' . md5( $query ); 698 $cache_salts = array( wp_cache_get_last_changed( 'posts' ), wp_cache_get_last_changed( 'users' ) ); 699 $count = wp_cache_get_salted( $cache_key, 'post-queries', $cache_salts ); 700 701 if ( false === $count ) { 702 $result = $wpdb->get_results( $query, ARRAY_N ); 703 704 $count = array_fill_keys( $users, 0 ); 705 foreach ( $result as $row ) { 706 $count[ $row[0] ] = $row[1]; 707 } 708 709 wp_cache_set_salted( $cache_key, $count, 'post-queries', $cache_salts, HOUR_IN_SECONDS ); 710 } 711 712 return $count; 713 } 714 715 // 716 // User option functions. 717 // 718 719 /** 720 * Gets the current user's ID. 721 * 722 * @since MU (3.0.0) 723 * 724 * @return int The current user's ID, or 0 if no user is logged in. 725 */ 726 function get_current_user_id() { 727 if ( ! function_exists( 'wp_get_current_user' ) ) { 728 return 0; 729 } 730 $user = wp_get_current_user(); 731 return (int) ( $user->ID ?? 0 ); 732 } 733 734 /** 735 * Retrieves user option that can be either per Site or per Network. 736 * 737 * If the user ID is not given, then the current user will be used instead. If 738 * the user ID is given, then the user data will be retrieved. The filter for 739 * the result, will also pass the original option name and finally the user data 740 * object as the third parameter. 741 * 742 * The option will first check for the per site name and then the per Network name. 743 * 744 * @since 2.0.0 745 * 746 * @global wpdb $wpdb WordPress database abstraction object. 747 * 748 * @param string $option User option name. 749 * @param int $user Optional. User ID. 750 * @param string $deprecated Use get_option() to check for an option in the options table. 751 * @return mixed User option value on success, false on failure. 752 */ 753 function get_user_option( $option, $user = 0, $deprecated = '' ) { 754 global $wpdb; 755 756 if ( ! empty( $deprecated ) ) { 757 _deprecated_argument( __FUNCTION__, '3.0.0' ); 758 } 759 760 if ( empty( $user ) ) { 761 $user = get_current_user_id(); 762 } 763 764 $user = get_userdata( $user ); 765 if ( ! $user ) { 766 return false; 767 } 768 769 $prefix = $wpdb->get_blog_prefix(); 770 if ( $user->has_prop( $prefix . $option ) ) { // Blog-specific. 771 $result = $user->get( $prefix . $option ); 772 } elseif ( $user->has_prop( $option ) ) { // User-specific and cross-blog. 773 $result = $user->get( $option ); 774 } else { 775 $result = false; 776 } 777 778 /** 779 * Filters a specific user option value. 780 * 781 * The dynamic portion of the hook name, `$option`, refers to the user option name. 782 * 783 * @since 2.5.0 784 * 785 * @param mixed $result Value for the user's option. 786 * @param string $option Name of the option being retrieved. 787 * @param WP_User $user WP_User object of the user whose option is being retrieved. 788 */ 789 return apply_filters( "get_user_option_{$option}", $result, $option, $user ); 790 } 791 792 /** 793 * Updates user option with global blog capability. 794 * 795 * User options are just like user metadata except that they have support for 796 * global blog options. If the 'is_global' parameter is false, which it is by default, 797 * it will prepend the WordPress table prefix to the option name. 798 * 799 * Deletes the user option if $newvalue is empty. 800 * 801 * @since 2.0.0 802 * 803 * @global wpdb $wpdb WordPress database abstraction object. 804 * 805 * @param int $user_id User ID. 806 * @param string $option_name User option name. 807 * @param mixed $newvalue User option value. 808 * @param bool $is_global Optional. Whether option name is global or blog specific. 809 * Default false (blog specific). 810 * @return int|bool User meta ID if the option didn't exist, true on successful update, 811 * false on failure. 812 */ 813 function update_user_option( $user_id, $option_name, $newvalue, $is_global = false ) { 814 global $wpdb; 815 816 if ( ! $is_global ) { 817 $option_name = $wpdb->get_blog_prefix() . $option_name; 818 } 819 820 return update_user_meta( $user_id, $option_name, $newvalue ); 821 } 822 823 /** 824 * Deletes user option with global blog capability. 825 * 826 * User options are just like user metadata except that they have support for 827 * global blog options. If the 'is_global' parameter is false, which it is by default, 828 * it will prepend the WordPress table prefix to the option name. 829 * 830 * @since 3.0.0 831 * 832 * @global wpdb $wpdb WordPress database abstraction object. 833 * 834 * @param int $user_id User ID 835 * @param string $option_name User option name. 836 * @param bool $is_global Optional. Whether option name is global or blog specific. 837 * Default false (blog specific). 838 * @return bool True on success, false on failure. 839 */ 840 function delete_user_option( $user_id, $option_name, $is_global = false ) { 841 global $wpdb; 842 843 if ( ! $is_global ) { 844 $option_name = $wpdb->get_blog_prefix() . $option_name; 845 } 846 847 return delete_user_meta( $user_id, $option_name ); 848 } 849 850 /** 851 * Retrieves user info by user ID. 852 * 853 * @since 6.7.0 854 * 855 * @param int $user_id User ID. 856 * 857 * @return WP_User|false WP_User object on success, false on failure. 858 */ 859 function get_user( $user_id ) { 860 return get_user_by( 'id', $user_id ); 861 } 862 863 /** 864 * Retrieves list of users matching criteria. 865 * 866 * @since 3.1.0 867 * 868 * @see WP_User_Query 869 * 870 * @param array $args Optional. Arguments to retrieve users. See WP_User_Query::prepare_query() 871 * for more information on accepted arguments. 872 * @return array List of users. 873 */ 874 function get_users( $args = array() ) { 875 876 $args = wp_parse_args( $args ); 877 $args['count_total'] = false; 878 879 $user_search = new WP_User_Query( $args ); 880 881 return (array) $user_search->get_results(); 882 } 883 884 /** 885 * Lists all the users of the site, with several options available. 886 * 887 * @since 5.9.0 888 * 889 * @param string|array $args { 890 * Optional. Array or string of default arguments. 891 * 892 * @type string $orderby How to sort the users. Accepts 'nicename', 'email', 'url', 'registered', 893 * 'user_nicename', 'user_email', 'user_url', 'user_registered', 'name', 894 * 'display_name', 'post_count', 'ID', 'meta_value', 'user_login'. Default 'name'. 895 * @type string $order Sorting direction for $orderby. Accepts 'ASC', 'DESC'. Default 'ASC'. 896 * @type int $number Maximum users to return or display. Default empty (all users). 897 * @type bool $exclude_admin Whether to exclude the 'admin' account, if it exists. Default true. 898 * @type bool $show_fullname Whether to show the user's full name. Default false. 899 * @type string $feed If not empty, show a link to the user's feed and use this text as the alt 900 * parameter of the link. Default empty. 901 * @type string $feed_image If not empty, show a link to the user's feed and use this image URL as 902 * clickable anchor. Default empty. 903 * @type string $feed_type The feed type to link to, such as 'rss2'. Defaults to default feed type. 904 * @type bool $echo Whether to output the result or instead return it. Default true. 905 * @type string $style If 'list', each user is wrapped in an `<li>` element, otherwise the users 906 * will be separated by commas. 907 * @type bool $html Whether to list the items in HTML form or plaintext. Default true. 908 * @type string $exclude An array, comma-, or space-separated list of user IDs to exclude. Default empty. 909 * @type string $include An array, comma-, or space-separated list of user IDs to include. Default empty. 910 * } 911 * @return string|null The output if echo is false. Otherwise null. 912 */ 913 function wp_list_users( $args = array() ) { 914 $defaults = array( 915 'orderby' => 'name', 916 'order' => 'ASC', 917 'number' => '', 918 'exclude_admin' => true, 919 'show_fullname' => false, 920 'feed' => '', 921 'feed_image' => '', 922 'feed_type' => '', 923 'echo' => true, 924 'style' => 'list', 925 'html' => true, 926 'exclude' => '', 927 'include' => '', 928 ); 929 930 $parsed_args = wp_parse_args( $args, $defaults ); 931 932 $return = ''; 933 934 $query_args = wp_array_slice_assoc( $parsed_args, array( 'orderby', 'order', 'number', 'exclude', 'include' ) ); 935 $query_args['fields'] = 'ids'; 936 937 /** 938 * Filters the query arguments for the list of all users of the site. 939 * 940 * @since 6.1.0 941 * 942 * @param array $query_args The query arguments for get_users(). 943 * @param array $parsed_args The arguments passed to wp_list_users() combined with the defaults. 944 */ 945 $query_args = apply_filters( 'wp_list_users_args', $query_args, $parsed_args ); 946 947 $users = get_users( $query_args ); 948 949 foreach ( $users as $user_id ) { 950 $user = get_userdata( $user_id ); 951 952 if ( $parsed_args['exclude_admin'] && 'admin' === $user->display_name ) { 953 continue; 954 } 955 956 if ( $parsed_args['show_fullname'] && '' !== $user->first_name && '' !== $user->last_name ) { 957 $name = sprintf( 958 /* translators: 1: User's first name, 2: Last name. */ 959 _x( '%1$s %2$s', 'Display name based on first name and last name' ), 960 $user->first_name, 961 $user->last_name 962 ); 963 } else { 964 $name = $user->display_name; 965 } 966 967 if ( ! $parsed_args['html'] ) { 968 $return .= $name . ', '; 969 970 continue; // No need to go further to process HTML. 971 } 972 973 if ( 'list' === $parsed_args['style'] ) { 974 $return .= '<li>'; 975 } 976 977 $row = $name; 978 979 if ( ! empty( $parsed_args['feed_image'] ) || ! empty( $parsed_args['feed'] ) ) { 980 $row .= ' '; 981 if ( empty( $parsed_args['feed_image'] ) ) { 982 $row .= '('; 983 } 984 985 $row .= '<a href="' . get_author_feed_link( $user->ID, $parsed_args['feed_type'] ) . '"'; 986 987 $alt = ''; 988 if ( ! empty( $parsed_args['feed'] ) ) { 989 $alt = ' alt="' . esc_attr( $parsed_args['feed'] ) . '"'; 990 $name = $parsed_args['feed']; 991 } 992 993 $row .= '>'; 994 995 if ( ! empty( $parsed_args['feed_image'] ) ) { 996 $row .= '<img src="' . esc_url( $parsed_args['feed_image'] ) . '" style="border: none;"' . $alt . ' />'; 997 } else { 998 $row .= $name; 999 } 1000 1001 $row .= '</a>'; 1002 1003 if ( empty( $parsed_args['feed_image'] ) ) { 1004 $row .= ')'; 1005 } 1006 } 1007 1008 $return .= $row; 1009 $return .= ( 'list' === $parsed_args['style'] ) ? '</li>' : ', '; 1010 } 1011 1012 $return = rtrim( $return, ', ' ); 1013 1014 if ( ! $parsed_args['echo'] ) { 1015 return $return; 1016 } 1017 echo $return; 1018 } 1019 1020 /** 1021 * Gets the sites a user belongs to. 1022 * 1023 * @since 3.0.0 1024 * @since 4.7.0 Converted to use `get_sites()`. 1025 * 1026 * @global wpdb $wpdb WordPress database abstraction object. 1027 * 1028 * @param int $user_id User ID 1029 * @param bool $all Whether to retrieve all sites, or only sites that are not 1030 * marked as deleted, archived, or spam. 1031 * @return object[] A list of the user's sites. An empty array if the user doesn't exist 1032 * or belongs to no sites. 1033 */ 1034 function get_blogs_of_user( $user_id, $all = false ) { 1035 global $wpdb; 1036 1037 $user_id = (int) $user_id; 1038 1039 // Logged out users can't have sites. 1040 if ( empty( $user_id ) ) { 1041 return array(); 1042 } 1043 1044 /** 1045 * Filters the list of a user's sites before it is populated. 1046 * 1047 * Returning a non-null value from the filter will effectively short circuit 1048 * get_blogs_of_user(), returning that value instead. 1049 * 1050 * @since 4.6.0 1051 * 1052 * @param null|object[] $sites An array of site objects of which the user is a member. 1053 * @param int $user_id User ID. 1054 * @param bool $all Whether the returned array should contain all sites, including 1055 * those marked 'deleted', 'archived', or 'spam'. Default false. 1056 */ 1057 $sites = apply_filters( 'pre_get_blogs_of_user', null, $user_id, $all ); 1058 1059 if ( null !== $sites ) { 1060 return $sites; 1061 } 1062 1063 $keys = get_user_meta( $user_id ); 1064 if ( empty( $keys ) ) { 1065 return array(); 1066 } 1067 1068 if ( ! is_multisite() ) { 1069 $site_id = get_current_blog_id(); 1070 $sites = array( $site_id => new stdClass() ); 1071 $sites[ $site_id ]->userblog_id = $site_id; 1072 $sites[ $site_id ]->blogname = get_option( 'blogname' ); 1073 $sites[ $site_id ]->domain = ''; 1074 $sites[ $site_id ]->path = ''; 1075 $sites[ $site_id ]->site_id = 1; 1076 $sites[ $site_id ]->siteurl = get_option( 'siteurl' ); 1077 $sites[ $site_id ]->archived = 0; 1078 $sites[ $site_id ]->spam = 0; 1079 $sites[ $site_id ]->deleted = 0; 1080 return $sites; 1081 } 1082 1083 $site_ids = array(); 1084 1085 if ( isset( $keys[ $wpdb->base_prefix . 'capabilities' ] ) && defined( 'MULTISITE' ) ) { 1086 $site_ids[] = 1; 1087 unset( $keys[ $wpdb->base_prefix . 'capabilities' ] ); 1088 } 1089 1090 $keys = array_keys( $keys ); 1091 1092 foreach ( $keys as $key ) { 1093 if ( ! str_ends_with( $key, 'capabilities' ) ) { 1094 continue; 1095 } 1096 if ( $wpdb->base_prefix && ! str_starts_with( $key, $wpdb->base_prefix ) ) { 1097 continue; 1098 } 1099 $site_id = str_replace( array( $wpdb->base_prefix, '_capabilities' ), '', $key ); 1100 if ( ! is_numeric( $site_id ) ) { 1101 continue; 1102 } 1103 1104 $site_ids[] = (int) $site_id; 1105 } 1106 1107 $sites = array(); 1108 1109 if ( ! empty( $site_ids ) ) { 1110 $args = array( 1111 'number' => '', 1112 'site__in' => $site_ids, 1113 ); 1114 if ( ! $all ) { 1115 $args['archived'] = 0; 1116 $args['spam'] = 0; 1117 $args['deleted'] = 0; 1118 } 1119 1120 $_sites = get_sites( $args ); 1121 1122 foreach ( $_sites as $site ) { 1123 $sites[ $site->id ] = (object) array( 1124 'userblog_id' => $site->id, 1125 'blogname' => $site->blogname, 1126 'domain' => $site->domain, 1127 'path' => $site->path, 1128 'site_id' => $site->network_id, 1129 'siteurl' => $site->siteurl, 1130 'archived' => $site->archived, 1131 'mature' => $site->mature, 1132 'spam' => $site->spam, 1133 'deleted' => $site->deleted, 1134 ); 1135 } 1136 } 1137 1138 /** 1139 * Filters the list of sites a user belongs to. 1140 * 1141 * @since MU (3.0.0) 1142 * 1143 * @param object[] $sites An array of site objects belonging to the user. 1144 * @param int $user_id User ID. 1145 * @param bool $all Whether the returned sites array should contain all sites, including 1146 * those flagged for deletion, archived, or marked as spam. 1147 */ 1148 return apply_filters( 'get_blogs_of_user', $sites, $user_id, $all ); 1149 } 1150 1151 /** 1152 * Finds out whether a user is a member of a given blog. 1153 * 1154 * @since MU (3.0.0) 1155 * @since 7.1.0 Introduced the {@see 'is_user_member_of_blog'} filter. 1156 * 1157 * @global wpdb $wpdb WordPress database abstraction object. 1158 * 1159 * @param int $user_id Optional. The unique ID of the user. Defaults to the current user. 1160 * @param int $blog_id Optional. ID of the blog to check. Defaults to the current site. 1161 * @return bool 1162 */ 1163 function is_user_member_of_blog( $user_id = 0, $blog_id = 0 ) { 1164 global $wpdb; 1165 1166 $user_id = (int) $user_id; 1167 $blog_id = (int) $blog_id; 1168 1169 if ( empty( $user_id ) ) { 1170 $user_id = get_current_user_id(); 1171 } 1172 1173 /* 1174 * Technically not needed, but does save calls to get_site() and get_user_meta() 1175 * in the event that the function is called when a user isn't logged in. 1176 */ 1177 if ( empty( $user_id ) ) { 1178 return false; 1179 } else { 1180 $user = get_userdata( $user_id ); 1181 if ( ! $user instanceof WP_User ) { 1182 return false; 1183 } 1184 } 1185 1186 if ( ! is_multisite() ) { 1187 return true; 1188 } 1189 1190 if ( empty( $blog_id ) ) { 1191 $blog_id = get_current_blog_id(); 1192 } 1193 1194 $blog = get_site( $blog_id ); 1195 1196 if ( ! $blog || ! isset( $blog->domain ) || $blog->archived || $blog->spam || $blog->deleted ) { 1197 return false; 1198 } 1199 1200 if ( 1 === $blog_id ) { 1201 $capabilities_key = $wpdb->base_prefix . 'capabilities'; 1202 } else { 1203 $capabilities_key = $wpdb->base_prefix . $blog_id . '_capabilities'; 1204 } 1205 1206 $has_cap = get_user_meta( $user_id, $capabilities_key, true ); 1207 $is_member = is_array( $has_cap ); 1208 1209 /** 1210 * Filters whether the user is a member of a given blog. 1211 * 1212 * This filter only runs when the user and blog have both been resolved 1213 * to valid records on a multisite installation; it is not invoked for 1214 * logged-out requests, unknown users, or archived/spammed/deleted sites. 1215 * 1216 * @since 7.1.0 1217 * 1218 * @param bool $is_member Whether the user is a member of the blog. 1219 * @param int $user_id The user ID being checked. 1220 * @param int $blog_id The blog ID being checked. 1221 */ 1222 return (bool) apply_filters( 'is_user_member_of_blog', $is_member, $user_id, $blog_id ); 1223 } 1224 1225 /** 1226 * Adds meta data to a user. 1227 * 1228 * For historical reasons both the meta key and the meta value are expected to be "slashed" (slashes escaped) on input. 1229 * 1230 * @since 3.0.0 1231 * 1232 * @param int $user_id User ID. 1233 * @param string $meta_key Metadata name. 1234 * @param mixed $meta_value Metadata value. Arrays and objects are stored as serialized data and 1235 * will be returned as the same type when retrieved. Other data types will 1236 * be stored as strings in the database: 1237 * - false is stored and retrieved as an empty string ('') 1238 * - true is stored and retrieved as '1' 1239 * - numbers (both integer and float) are stored and retrieved as strings 1240 * Must be serializable if non-scalar. 1241 * @param bool $unique Optional. Whether the same key should not be added. 1242 * Default false. 1243 * @return int|false Meta ID on success, false on failure. 1244 */ 1245 function add_user_meta( $user_id, $meta_key, $meta_value, $unique = false ) { 1246 return add_metadata( 'user', $user_id, $meta_key, $meta_value, $unique ); 1247 } 1248 1249 /** 1250 * Removes metadata matching criteria from a user. 1251 * 1252 * You can match based on the key, or key and value. Removing based on key and 1253 * value, will keep from removing duplicate metadata with the same key. It also 1254 * allows removing all metadata matching key, if needed. 1255 * 1256 * For historical reasons both the meta key and the meta value are expected to be "slashed" (slashes escaped) on input. 1257 * 1258 * @since 3.0.0 1259 * 1260 * @link https://developer.wordpress.org/reference/functions/delete_user_meta/ 1261 * 1262 * @param int $user_id User ID 1263 * @param string $meta_key Metadata name. 1264 * @param mixed $meta_value Optional. Metadata value. If provided, 1265 * rows will only be removed that match the value. 1266 * Must be serializable if non-scalar. Default empty. 1267 * @return bool True on success, false on failure. 1268 */ 1269 function delete_user_meta( $user_id, $meta_key, $meta_value = '' ) { 1270 return delete_metadata( 'user', $user_id, $meta_key, $meta_value ); 1271 } 1272 1273 /** 1274 * Retrieves user meta field for a user. 1275 * 1276 * @since 3.0.0 1277 * 1278 * @link https://developer.wordpress.org/reference/functions/get_user_meta/ 1279 * 1280 * @param int $user_id User ID. 1281 * @param string $key Optional. The meta key to retrieve. By default, 1282 * returns data for all keys. 1283 * @param bool $single Optional. Whether to return a single value. 1284 * This parameter has no effect if `$key` is not specified. 1285 * Default false. 1286 * @return mixed An array of values if `$single` is false. 1287 * The value of meta data field if `$single` is true. 1288 * False for an invalid `$user_id` (non-numeric, zero, or negative value). 1289 * An empty array if a valid but non-existing user ID is passed and `$single` is false. 1290 * An empty string if a valid but non-existing user ID is passed and `$single` is true. 1291 * Note: Non-serialized values are returned as strings: 1292 * - false values are returned as empty strings ('') 1293 * - true values are returned as '1' 1294 * - numbers (both integer and float) are returned as strings 1295 * Arrays and objects retain their original type. 1296 */ 1297 function get_user_meta( $user_id, $key = '', $single = false ) { 1298 return get_metadata( 'user', $user_id, $key, $single ); 1299 } 1300 1301 /** 1302 * Updates user meta field based on user ID. 1303 * 1304 * Use the $prev_value parameter to differentiate between meta fields with the 1305 * same key and user ID. 1306 * 1307 * If the meta field for the user does not exist, it will be added. 1308 * 1309 * For historical reasons both the meta key and the meta value are expected to be "slashed" (slashes escaped) on input. 1310 * 1311 * @since 3.0.0 1312 * 1313 * @link https://developer.wordpress.org/reference/functions/update_user_meta/ 1314 * 1315 * @param int $user_id User ID. 1316 * @param string $meta_key Metadata key. 1317 * @param mixed $meta_value Metadata value. Must be serializable if non-scalar. 1318 * @param mixed $prev_value Optional. Previous value to check before updating. 1319 * If specified, only update existing metadata entries with 1320 * this value. Otherwise, update all entries. Default empty. 1321 * @return int|bool Meta ID if the key didn't exist, true on successful update, 1322 * false on failure or if the value passed to the function 1323 * is the same as the one that is already in the database. 1324 */ 1325 function update_user_meta( $user_id, $meta_key, $meta_value, $prev_value = '' ) { 1326 return update_metadata( 'user', $user_id, $meta_key, $meta_value, $prev_value ); 1327 } 1328 1329 /** 1330 * Counts number of users who have each of the user roles. 1331 * 1332 * Assumes there are neither duplicated nor orphaned capabilities meta_values. 1333 * Assumes role names are unique phrases. Same assumption made by WP_User_Query::prepare_query() 1334 * Using $strategy = 'time' this is CPU-intensive and should handle around 10^7 users. 1335 * Using $strategy = 'memory' this is memory-intensive and should handle around 10^5 users, but see WP Bug #12257. 1336 * 1337 * @since 3.0.0 1338 * @since 4.4.0 The number of users with no role is now included in the `none` element. 1339 * @since 4.9.0 The `$site_id` parameter was added to support multisite. 1340 * 1341 * @global wpdb $wpdb WordPress database abstraction object. 1342 * 1343 * @param string $strategy Optional. The computational strategy to use when counting the users. 1344 * Accepts either 'time' or 'memory'. Default 'time'. 1345 * @param int|null $site_id Optional. The site ID to count users for. Defaults to the current site. 1346 * @return array { 1347 * User counts. 1348 * 1349 * @type int $total_users Total number of users on the site. 1350 * @type int[] $avail_roles Array of user counts keyed by user role. 1351 * } 1352 */ 1353 function count_users( $strategy = 'time', $site_id = null ) { 1354 global $wpdb; 1355 1356 // Initialize. 1357 if ( ! $site_id ) { 1358 $site_id = get_current_blog_id(); 1359 } 1360 1361 /** 1362 * Filters the user count before queries are run. 1363 * 1364 * Return a non-null value to cause count_users() to return early. 1365 * 1366 * @since 5.1.0 1367 * 1368 * @param null|array $result The value to return instead. Default null to continue with the query. 1369 * @param string $strategy Optional. The computational strategy to use when counting the users. 1370 * Accepts either 'time' or 'memory'. Default 'time'. 1371 * @param int $site_id The site ID to count users for. 1372 */ 1373 $pre = apply_filters( 'pre_count_users', null, $strategy, $site_id ); 1374 1375 if ( null !== $pre ) { 1376 return $pre; 1377 } 1378 1379 $blog_prefix = $wpdb->get_blog_prefix( $site_id ); 1380 $result = array(); 1381 1382 if ( 'time' === $strategy ) { 1383 if ( is_multisite() && get_current_blog_id() !== $site_id ) { 1384 switch_to_blog( $site_id ); 1385 $avail_roles = wp_roles()->get_names(); 1386 restore_current_blog(); 1387 } else { 1388 $avail_roles = wp_roles()->get_names(); 1389 } 1390 1391 // Build a CPU-intensive query that will return concise information. 1392 $select_count = array(); 1393 foreach ( $avail_roles as $this_role => $name ) { 1394 $select_count[] = $wpdb->prepare( 'COUNT(NULLIF(`meta_value` LIKE %s, false))', '%' . $wpdb->esc_like( '"' . $this_role . '"' ) . '%' ); 1395 } 1396 $select_count[] = "COUNT(NULLIF(`meta_value` = 'a:0:{}', false))"; 1397 $select_count = implode( ', ', $select_count ); 1398 1399 // Add the meta_value index to the selection list, then run the query. 1400 $row = $wpdb->get_row( 1401 " 1402 SELECT {$select_count}, COUNT(*) 1403 FROM {$wpdb->usermeta} 1404 INNER JOIN {$wpdb->users} ON user_id = ID 1405 WHERE meta_key = '{$blog_prefix}capabilities' 1406 ", 1407 ARRAY_N 1408 ); 1409 1410 // Run the previous loop again to associate results with role names. 1411 $col = 0; 1412 $role_counts = array(); 1413 foreach ( $avail_roles as $this_role => $name ) { 1414 $count = (int) $row[ $col++ ]; 1415 if ( $count > 0 ) { 1416 $role_counts[ $this_role ] = $count; 1417 } 1418 } 1419 1420 $role_counts['none'] = (int) $row[ $col++ ]; 1421 1422 // Get the meta_value index from the end of the result set. 1423 $total_users = (int) $row[ $col ]; 1424 1425 $result['total_users'] = $total_users; 1426 $result['avail_roles'] =& $role_counts; 1427 } else { 1428 $avail_roles = array( 1429 'none' => 0, 1430 ); 1431 1432 $users_of_blog = $wpdb->get_col( 1433 " 1434 SELECT meta_value 1435 FROM {$wpdb->usermeta} 1436 INNER JOIN {$wpdb->users} ON user_id = ID 1437 WHERE meta_key = '{$blog_prefix}capabilities' 1438 " 1439 ); 1440 1441 foreach ( $users_of_blog as $caps_meta ) { 1442 $b_roles = maybe_unserialize( $caps_meta ); 1443 if ( ! is_array( $b_roles ) ) { 1444 continue; 1445 } 1446 if ( empty( $b_roles ) ) { 1447 ++$avail_roles['none']; 1448 } 1449 foreach ( $b_roles as $b_role => $val ) { 1450 if ( isset( $avail_roles[ $b_role ] ) ) { 1451 ++$avail_roles[ $b_role ]; 1452 } else { 1453 $avail_roles[ $b_role ] = 1; 1454 } 1455 } 1456 } 1457 1458 $result['total_users'] = count( $users_of_blog ); 1459 $result['avail_roles'] =& $avail_roles; 1460 } 1461 1462 return $result; 1463 } 1464 1465 /** 1466 * Returns the number of active users in your installation. 1467 * 1468 * Note that on a large site the count may be cached and only updated twice daily. 1469 * 1470 * @since MU (3.0.0) 1471 * @since 4.8.0 The `$network_id` parameter has been added. 1472 * @since 6.0.0 Moved to wp-includes/user.php. 1473 * 1474 * @param int|null $network_id ID of the network. Defaults to the current network. 1475 * @return int Number of active users on the network. 1476 */ 1477 function get_user_count( $network_id = null ) { 1478 if ( ! is_multisite() && null !== $network_id ) { 1479 _doing_it_wrong( 1480 __FUNCTION__, 1481 sprintf( 1482 /* translators: %s: $network_id */ 1483 __( 'Unable to pass %s if not using multisite.' ), 1484 '<code>$network_id</code>' 1485 ), 1486 '6.0.0' 1487 ); 1488 } 1489 1490 return (int) get_network_option( $network_id, 'user_count', -1 ); 1491 } 1492 1493 /** 1494 * Updates the total count of users on the site if live user counting is enabled. 1495 * 1496 * @since 6.0.0 1497 * 1498 * @param int|null $network_id ID of the network. Defaults to the current network. 1499 * @return bool Whether the update was successful. 1500 */ 1501 function wp_maybe_update_user_counts( $network_id = null ) { 1502 if ( ! is_multisite() && null !== $network_id ) { 1503 _doing_it_wrong( 1504 __FUNCTION__, 1505 sprintf( 1506 /* translators: %s: $network_id */ 1507 __( 'Unable to pass %s if not using multisite.' ), 1508 '<code>$network_id</code>' 1509 ), 1510 '6.0.0' 1511 ); 1512 } 1513 1514 $is_small_network = ! wp_is_large_user_count( $network_id ); 1515 /** This filter is documented in wp-includes/ms-functions.php */ 1516 if ( ! apply_filters( 'enable_live_network_counts', $is_small_network, 'users' ) ) { 1517 return false; 1518 } 1519 1520 return wp_update_user_counts( $network_id ); 1521 } 1522 1523 /** 1524 * Updates the total count of users on the site. 1525 * 1526 * @global wpdb $wpdb WordPress database abstraction object. 1527 * @since 6.0.0 1528 * 1529 * @param int|null $network_id ID of the network. Defaults to the current network. 1530 * @return bool Whether the update was successful. 1531 */ 1532 function wp_update_user_counts( $network_id = null ) { 1533 global $wpdb; 1534 1535 if ( ! is_multisite() && null !== $network_id ) { 1536 _doing_it_wrong( 1537 __FUNCTION__, 1538 sprintf( 1539 /* translators: %s: $network_id */ 1540 __( 'Unable to pass %s if not using multisite.' ), 1541 '<code>$network_id</code>' 1542 ), 1543 '6.0.0' 1544 ); 1545 } 1546 1547 $query = "SELECT COUNT(ID) as c FROM $wpdb->users"; 1548 if ( is_multisite() ) { 1549 $query .= " WHERE spam = '0' AND deleted = '0'"; 1550 } 1551 1552 $count = $wpdb->get_var( $query ); 1553 1554 return update_network_option( $network_id, 'user_count', $count ); 1555 } 1556 1557 /** 1558 * Schedules a recurring recalculation of the total count of users. 1559 * 1560 * @since 6.0.0 1561 */ 1562 function wp_schedule_update_user_counts() { 1563 if ( ! is_main_site() ) { 1564 return; 1565 } 1566 1567 if ( ! wp_next_scheduled( 'wp_update_user_counts' ) && ! wp_installing() ) { 1568 wp_schedule_event( time(), 'twicedaily', 'wp_update_user_counts' ); 1569 } 1570 } 1571 1572 /** 1573 * Determines whether the site has a large number of users. 1574 * 1575 * The default criteria for a large site is more than 10,000 users. 1576 * 1577 * @since 6.0.0 1578 * 1579 * @param int|null $network_id ID of the network. Defaults to the current network. 1580 * @return bool Whether the site has a large number of users. 1581 */ 1582 function wp_is_large_user_count( $network_id = null ) { 1583 if ( ! is_multisite() && null !== $network_id ) { 1584 _doing_it_wrong( 1585 __FUNCTION__, 1586 sprintf( 1587 /* translators: %s: $network_id */ 1588 __( 'Unable to pass %s if not using multisite.' ), 1589 '<code>$network_id</code>' 1590 ), 1591 '6.0.0' 1592 ); 1593 } 1594 1595 $count = get_user_count( $network_id ); 1596 1597 /** 1598 * Filters whether the site is considered large, based on its number of users. 1599 * 1600 * @since 6.0.0 1601 * 1602 * @param bool $is_large_user_count Whether the site has a large number of users. 1603 * @param int $count The total number of users. 1604 * @param int|null $network_id ID of the network. `null` represents the current network. 1605 */ 1606 return apply_filters( 'wp_is_large_user_count', $count > 10000, $count, $network_id ); 1607 } 1608 1609 // 1610 // Private helper functions. 1611 // 1612 1613 /** 1614 * Sets up global user vars. 1615 * 1616 * Used by wp_set_current_user() for back compat. Might be deprecated in the future. 1617 * 1618 * @since 2.0.4 1619 * 1620 * @global string $user_login The user username for logging in 1621 * @global WP_User $userdata User data. 1622 * @global int $user_level The level of the user 1623 * @global int $user_ID The ID of the user 1624 * @global string $user_email The email address of the user 1625 * @global string $user_url The url in the user's profile 1626 * @global string $user_identity The display name of the user 1627 * 1628 * @param int $for_user_id Optional. User ID to set up global data. Default 0. 1629 */ 1630 function setup_userdata( $for_user_id = 0 ) { 1631 global $user_login, $userdata, $user_level, $user_ID, $user_email, $user_url, $user_identity; 1632 1633 if ( ! $for_user_id ) { 1634 $for_user_id = get_current_user_id(); 1635 } 1636 $user = get_userdata( $for_user_id ); 1637 1638 if ( ! $user ) { 1639 $user_ID = 0; 1640 $user_level = 0; 1641 $userdata = null; 1642 $user_login = ''; 1643 $user_email = ''; 1644 $user_url = ''; 1645 $user_identity = ''; 1646 return; 1647 } 1648 1649 $user_ID = (int) $user->ID; 1650 $user_level = (int) $user->user_level; 1651 $userdata = $user; 1652 $user_login = $user->user_login; 1653 $user_email = $user->user_email; 1654 $user_url = $user->user_url; 1655 $user_identity = $user->display_name; 1656 } 1657 1658 /** 1659 * Creates dropdown HTML content of users. 1660 * 1661 * The content can either be displayed, which it is by default, or retrieved by 1662 * setting the 'echo' argument to false. The 'include' and 'exclude' arguments 1663 * are optional; if they are not specified, all users will be displayed. Only one 1664 * can be used in a single call, either 'include' or 'exclude', but not both. 1665 * 1666 * @since 2.3.0 1667 * @since 4.5.0 Added the 'display_name_with_login' value for 'show'. 1668 * @since 4.7.0 Added the 'role', 'role__in', and 'role__not_in' parameters. 1669 * @since 5.9.0 Added the 'capability', 'capability__in', and 'capability__not_in' parameters. 1670 * Deprecated the 'who' parameter. 1671 * 1672 * @param array|string $args { 1673 * Optional. Array or string of arguments to generate a drop-down of users. 1674 * See WP_User_Query::prepare_query() for additional available arguments. 1675 * 1676 * @type string $show_option_all Text to show as the drop-down default (all). 1677 * Default empty. 1678 * @type string $show_option_none Text to show as the drop-down default when no 1679 * users were found. Default empty. 1680 * @type int|string $option_none_value Value to use for `$show_option_none` when no users 1681 * were found. Default -1. 1682 * @type string $hide_if_only_one_author Whether to skip generating the drop-down 1683 * if only one user was found. Default empty. 1684 * @type string $orderby Field to order found users by. Accepts user fields. 1685 * Default 'display_name'. 1686 * @type string $order Whether to order users in ascending or descending 1687 * order. Accepts 'ASC' (ascending) or 'DESC' (descending). 1688 * Default 'ASC'. 1689 * @type int[]|string $include Array or comma-separated list of user IDs to include. 1690 * Default empty. 1691 * @type int[]|string $exclude Array or comma-separated list of user IDs to exclude. 1692 * Default empty. 1693 * @type bool|int $multi Whether to skip the ID attribute on the 'select' element. 1694 * Accepts 1|true or 0|false. Default 0|false. 1695 * @type string $show User data to display. If the selected item is empty 1696 * then the 'user_login' will be displayed in parentheses. 1697 * Accepts any user field, or 'display_name_with_login' to show 1698 * the display name with user_login in parentheses. 1699 * Default 'display_name'. 1700 * @type int|bool $echo Whether to echo or return the drop-down. Accepts 1|true (echo) 1701 * or 0|false (return). Default 1|true. 1702 * @type int $selected Which user ID should be selected. Default 0. 1703 * @type bool $include_selected Whether to always include the selected user ID in the drop- 1704 * down. Default false. 1705 * @type string $name Name attribute of select element. Default 'user'. 1706 * @type string $id ID attribute of the select element. Default is the value of `$name`. 1707 * @type string $class Class attribute of the select element. Default empty. 1708 * @type int $blog_id ID of blog (Multisite only). Default is ID of the current blog. 1709 * @type string $who Deprecated, use `$capability` instead. 1710 * Which type of users to query. Accepts only an empty string or 1711 * 'authors'. Default empty (all users). 1712 * @type string|string[] $role An array or a comma-separated list of role names that users 1713 * must match to be included in results. Note that this is 1714 * an inclusive list: users must match *each* role. Default empty. 1715 * @type string[] $role__in An array of role names. Matched users must have at least one 1716 * of these roles. Default empty array. 1717 * @type string[] $role__not_in An array of role names to exclude. Users matching one or more 1718 * of these roles will not be included in results. Default empty array. 1719 * @type string|string[] $capability An array or a comma-separated list of capability names that users 1720 * must match to be included in results. Note that this is 1721 * an inclusive list: users must match *each* capability. 1722 * Does NOT work for capabilities not in the database or filtered 1723 * via {@see 'map_meta_cap'}. Default empty. 1724 * @type string[] $capability__in An array of capability names. Matched users must have at least one 1725 * of these capabilities. 1726 * Does NOT work for capabilities not in the database or filtered 1727 * via {@see 'map_meta_cap'}. Default empty array. 1728 * @type string[] $capability__not_in An array of capability names to exclude. Users matching one or more 1729 * of these capabilities will not be included in results. 1730 * Does NOT work for capabilities not in the database or filtered 1731 * via {@see 'map_meta_cap'}. Default empty array. 1732 * } 1733 * @return string HTML dropdown list of users. 1734 */ 1735 function wp_dropdown_users( $args = '' ) { 1736 $defaults = array( 1737 'show_option_all' => '', 1738 'show_option_none' => '', 1739 'hide_if_only_one_author' => '', 1740 'orderby' => 'display_name', 1741 'order' => 'ASC', 1742 'include' => '', 1743 'exclude' => '', 1744 'multi' => 0, 1745 'show' => 'display_name', 1746 'echo' => 1, 1747 'selected' => 0, 1748 'name' => 'user', 1749 'class' => '', 1750 'id' => '', 1751 'blog_id' => get_current_blog_id(), 1752 'who' => '', 1753 'include_selected' => false, 1754 'option_none_value' => -1, 1755 'role' => '', 1756 'role__in' => array(), 1757 'role__not_in' => array(), 1758 'capability' => '', 1759 'capability__in' => array(), 1760 'capability__not_in' => array(), 1761 ); 1762 1763 $defaults['selected'] = is_author() ? get_query_var( 'author' ) : 0; 1764 1765 $parsed_args = wp_parse_args( $args, $defaults ); 1766 1767 $query_args = wp_array_slice_assoc( 1768 $parsed_args, 1769 array( 1770 'blog_id', 1771 'include', 1772 'exclude', 1773 'orderby', 1774 'order', 1775 'who', 1776 'role', 1777 'role__in', 1778 'role__not_in', 1779 'capability', 1780 'capability__in', 1781 'capability__not_in', 1782 ) 1783 ); 1784 1785 $fields = array( 'ID', 'user_login' ); 1786 1787 $show = ! empty( $parsed_args['show'] ) ? $parsed_args['show'] : 'display_name'; 1788 if ( 'display_name_with_login' === $show ) { 1789 $fields[] = 'display_name'; 1790 } else { 1791 $fields[] = $show; 1792 } 1793 1794 $query_args['fields'] = $fields; 1795 1796 $show_option_all = $parsed_args['show_option_all']; 1797 $show_option_none = $parsed_args['show_option_none']; 1798 $option_none_value = $parsed_args['option_none_value']; 1799 1800 /** 1801 * Filters the query arguments for the list of users in the dropdown. 1802 * 1803 * @since 4.4.0 1804 * 1805 * @param array $query_args The query arguments for get_users(). 1806 * @param array $parsed_args The arguments passed to wp_dropdown_users() combined with the defaults. 1807 */ 1808 $query_args = apply_filters( 'wp_dropdown_users_args', $query_args, $parsed_args ); 1809 1810 $users = get_users( $query_args ); 1811 1812 $output = ''; 1813 if ( ! empty( $users ) && ( empty( $parsed_args['hide_if_only_one_author'] ) || count( $users ) > 1 ) ) { 1814 $name = esc_attr( $parsed_args['name'] ); 1815 if ( $parsed_args['multi'] && ! $parsed_args['id'] ) { 1816 $id = ''; 1817 } else { 1818 $id = $parsed_args['id'] ? " id='" . esc_attr( $parsed_args['id'] ) . "'" : " id='$name'"; 1819 } 1820 $output = "<select name='{$name}'{$id} class='" . $parsed_args['class'] . "'>\n"; 1821 1822 if ( $show_option_all ) { 1823 $output .= "\t<option value='0'>$show_option_all</option>\n"; 1824 } 1825 1826 if ( $show_option_none ) { 1827 $_selected = selected( $option_none_value, $parsed_args['selected'], false ); 1828 $output .= "\t<option value='" . esc_attr( $option_none_value ) . "'$_selected>$show_option_none</option>\n"; 1829 } 1830 1831 if ( $parsed_args['include_selected'] && ( $parsed_args['selected'] > 0 ) ) { 1832 $found_selected = false; 1833 $parsed_args['selected'] = (int) $parsed_args['selected']; 1834 1835 foreach ( (array) $users as $user ) { 1836 $user->ID = (int) $user->ID; 1837 if ( $user->ID === $parsed_args['selected'] ) { 1838 $found_selected = true; 1839 } 1840 } 1841 1842 if ( ! $found_selected ) { 1843 $selected_user = get_userdata( $parsed_args['selected'] ); 1844 if ( $selected_user ) { 1845 $users[] = $selected_user; 1846 } 1847 } 1848 } 1849 1850 foreach ( (array) $users as $user ) { 1851 if ( 'display_name_with_login' === $show ) { 1852 /* translators: 1: User's display name, 2: User login. */ 1853 $display = sprintf( _x( '%1$s (%2$s)', 'user dropdown' ), $user->display_name, $user->user_login ); 1854 } elseif ( ! empty( $user->$show ) ) { 1855 $display = $user->$show; 1856 } else { 1857 $display = '(' . $user->user_login . ')'; 1858 } 1859 1860 $_selected = selected( $user->ID, $parsed_args['selected'], false ); 1861 $output .= "\t<option value='$user->ID'$_selected>" . esc_html( $display ) . "</option>\n"; 1862 } 1863 1864 $output .= '</select>'; 1865 } 1866 1867 /** 1868 * Filters the wp_dropdown_users() HTML output. 1869 * 1870 * @since 2.3.0 1871 * 1872 * @param string $output HTML output generated by wp_dropdown_users(). 1873 */ 1874 $html = apply_filters( 'wp_dropdown_users', $output ); 1875 1876 if ( $parsed_args['echo'] ) { 1877 echo $html; 1878 } 1879 return $html; 1880 } 1881 1882 /** 1883 * Sanitizes user field based on context. 1884 * 1885 * Possible context values are: 'raw', 'edit', 'db', 'display', 'attribute' and 'js'. The 1886 * 'display' context is used by default. 'attribute' and 'js' contexts are treated like 'display' 1887 * when calling filters. 1888 * 1889 * @since 2.3.0 1890 * 1891 * @param string $field The user Object field name. 1892 * @param mixed $value The user Object value. 1893 * @param int $user_id User ID. 1894 * @param string $context How to sanitize user fields. Looks for 'raw', 'edit', 'db', 'display', 1895 * 'attribute' and 'js'. 1896 * @return mixed Sanitized value. 1897 */ 1898 function sanitize_user_field( $field, $value, $user_id, $context ) { 1899 $int_fields = array( 'ID' ); 1900 if ( in_array( $field, $int_fields, true ) ) { 1901 $value = (int) $value; 1902 } 1903 1904 if ( 'raw' === $context ) { 1905 return $value; 1906 } 1907 1908 if ( ! is_string( $value ) && ! is_numeric( $value ) ) { 1909 return $value; 1910 } 1911 1912 $prefixed = str_contains( $field, 'user_' ); 1913 1914 if ( 'edit' === $context ) { 1915 if ( $prefixed ) { 1916 1917 /** This filter is documented in wp-includes/post.php */ 1918 $value = apply_filters( "edit_{$field}", $value, $user_id ); 1919 } else { 1920 1921 /** 1922 * Filters a user field value in the 'edit' context. 1923 * 1924 * The dynamic portion of the hook name, `$field`, refers to the prefixed user 1925 * field being filtered, such as 'user_login', 'user_email', 'first_name', etc. 1926 * 1927 * @since 2.9.0 1928 * 1929 * @param mixed $value Value of the prefixed user field. 1930 * @param int $user_id User ID. 1931 */ 1932 $value = apply_filters( "edit_user_{$field}", $value, $user_id ); 1933 } 1934 1935 if ( 'description' === $field ) { 1936 $value = esc_html( $value ); // textarea_escaped? 1937 } else { 1938 $value = esc_attr( $value ); 1939 } 1940 } elseif ( 'db' === $context ) { 1941 if ( $prefixed ) { 1942 /** This filter is documented in wp-includes/post.php */ 1943 $value = apply_filters( "pre_{$field}", $value ); 1944 } else { 1945 1946 /** 1947 * Filters the value of a user field in the 'db' context. 1948 * 1949 * The dynamic portion of the hook name, `$field`, refers to the prefixed user 1950 * field being filtered, such as 'user_login', 'user_email', 'first_name', etc. 1951 * 1952 * @since 2.9.0 1953 * 1954 * @param mixed $value Value of the prefixed user field. 1955 */ 1956 $value = apply_filters( "pre_user_{$field}", $value ); 1957 } 1958 } else { 1959 // Use display filters by default. 1960 if ( $prefixed ) { 1961 1962 /** This filter is documented in wp-includes/post.php */ 1963 $value = apply_filters( "{$field}", $value, $user_id, $context ); 1964 } else { 1965 1966 /** 1967 * Filters the value of a user field in a standard context. 1968 * 1969 * The dynamic portion of the hook name, `$field`, refers to the prefixed user 1970 * field being filtered, such as 'user_login', 'user_email', 'first_name', etc. 1971 * 1972 * @since 2.9.0 1973 * 1974 * @param mixed $value The user object value to sanitize. 1975 * @param int $user_id User ID. 1976 * @param string $context The context to filter within. 1977 */ 1978 $value = apply_filters( "user_{$field}", $value, $user_id, $context ); 1979 } 1980 } 1981 1982 if ( 'user_url' === $field ) { 1983 $value = esc_url( $value ); 1984 } 1985 1986 if ( 'attribute' === $context ) { 1987 $value = esc_attr( $value ); 1988 } elseif ( 'js' === $context ) { 1989 $value = esc_js( $value ); 1990 } 1991 1992 // Restore the type for integer fields after esc_attr(). 1993 if ( in_array( $field, $int_fields, true ) ) { 1994 $value = (int) $value; 1995 } 1996 1997 return $value; 1998 } 1999 2000 /** 2001 * Updates all user caches. 2002 * 2003 * @since 3.0.0 2004 * 2005 * @param object|WP_User $user User object or database row to be cached 2006 * @return void|false Void on success, false on failure. 2007 */ 2008 function update_user_caches( $user ) { 2009 if ( $user instanceof WP_User ) { 2010 if ( ! $user->exists() ) { 2011 return false; 2012 } 2013 2014 $user = $user->data; 2015 } 2016 2017 wp_cache_add( $user->ID, $user, 'users' ); 2018 wp_cache_add( $user->user_login, $user->ID, 'userlogins' ); 2019 wp_cache_add( $user->user_nicename, $user->ID, 'userslugs' ); 2020 2021 if ( ! empty( $user->user_email ) ) { 2022 wp_cache_add( $user->user_email, $user->ID, 'useremail' ); 2023 } 2024 } 2025 2026 /** 2027 * Cleans all user caches. 2028 * 2029 * @since 3.0.0 2030 * @since 4.4.0 'clean_user_cache' action was added. 2031 * @since 6.2.0 User metadata caches are now cleared. 2032 * 2033 * @param WP_User|int $user User object or ID to be cleaned from the cache 2034 */ 2035 function clean_user_cache( $user ) { 2036 if ( is_numeric( $user ) ) { 2037 $user = new WP_User( $user ); 2038 } 2039 2040 if ( ! $user->exists() ) { 2041 return; 2042 } 2043 2044 wp_cache_delete( $user->ID, 'users' ); 2045 wp_cache_delete( $user->user_login, 'userlogins' ); 2046 wp_cache_delete( $user->user_nicename, 'userslugs' ); 2047 2048 if ( ! empty( $user->user_email ) ) { 2049 wp_cache_delete( $user->user_email, 'useremail' ); 2050 } 2051 2052 wp_cache_delete( $user->ID, 'user_meta' ); 2053 wp_cache_set_users_last_changed(); 2054 2055 /** 2056 * Fires immediately after the given user's cache is cleaned. 2057 * 2058 * @since 4.4.0 2059 * 2060 * @param int $user_id User ID. 2061 * @param WP_User $user User object. 2062 */ 2063 do_action( 'clean_user_cache', $user->ID, $user ); 2064 } 2065 2066 /** 2067 * Determines whether the given username exists. 2068 * 2069 * For more information on this and similar theme functions, check out 2070 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 2071 * Conditional Tags} article in the Theme Developer Handbook. 2072 * 2073 * @since 2.0.0 2074 * 2075 * @param string $username The username to check for existence. 2076 * @return int|false The user ID on success, false on failure. 2077 */ 2078 function username_exists( $username ) { 2079 $user = get_user_by( 'login', $username ); 2080 if ( $user ) { 2081 $user_id = $user->ID; 2082 } else { 2083 $user_id = false; 2084 } 2085 2086 /** 2087 * Filters whether the given username exists. 2088 * 2089 * @since 4.9.0 2090 * 2091 * @param int|false $user_id The user ID associated with the username, 2092 * or false if the username does not exist. 2093 * @param string $username The username to check for existence. 2094 */ 2095 return apply_filters( 'username_exists', $user_id, $username ); 2096 } 2097 2098 /** 2099 * Determines whether the given email exists. 2100 * 2101 * For more information on this and similar theme functions, check out 2102 * the {@link https://developer.wordpress.org/themes/basics/conditional-tags/ 2103 * Conditional Tags} article in the Theme Developer Handbook. 2104 * 2105 * @since 2.1.0 2106 * 2107 * @param string $email The email to check for existence. 2108 * @return int|false The user ID on success, false on failure. 2109 */ 2110 function email_exists( $email ) { 2111 $user = get_user_by( 'email', $email ); 2112 if ( $user ) { 2113 $user_id = $user->ID; 2114 } else { 2115 $user_id = false; 2116 } 2117 2118 /** 2119 * Filters whether the given email exists. 2120 * 2121 * @since 5.6.0 2122 * 2123 * @param int|false $user_id The user ID associated with the email, 2124 * or false if the email does not exist. 2125 * @param string $email The email to check for existence. 2126 */ 2127 return apply_filters( 'email_exists', $user_id, $email ); 2128 } 2129 2130 /** 2131 * Checks whether a username is valid. 2132 * 2133 * @since 2.0.1 2134 * @since 4.4.0 Empty sanitized usernames are now considered invalid. 2135 * 2136 * @param string $username Username. 2137 * @return bool Whether username given is valid. 2138 */ 2139 function validate_username( $username ) { 2140 $sanitized = sanitize_user( $username, true ); 2141 $valid = ( $sanitized === $username && ! empty( $sanitized ) ); 2142 2143 /** 2144 * Filters whether the provided username is valid. 2145 * 2146 * @since 2.0.1 2147 * 2148 * @param bool $valid Whether given username is valid. 2149 * @param string $username Username to check. 2150 */ 2151 return apply_filters( 'validate_username', $valid, $username ); 2152 } 2153 2154 /** 2155 * Inserts a user into the database. 2156 * 2157 * Most of the `$userdata` array fields have filters associated with the values. Exceptions are 2158 * 'ID', 'rich_editing', 'syntax_highlighting', 'comment_shortcuts', 'admin_color', 'use_ssl', 2159 * 'user_registered', 'user_activation_key', 'spam', and 'role'. The filters have the prefix 2160 * 'pre_user_' followed by the field name. An example using 'description' would have the filter 2161 * called 'pre_user_description' that can be hooked into. 2162 * 2163 * @since 2.0.0 2164 * @since 3.6.0 The `aim`, `jabber`, and `yim` fields were removed as default user contact 2165 * methods for new installations. See wp_get_user_contact_methods(). 2166 * @since 4.7.0 The `locale` field can be passed to `$userdata`. 2167 * @since 5.3.0 The `user_activation_key` field can be passed to `$userdata`. 2168 * @since 5.3.0 The `spam` field can be passed to `$userdata` (Multisite only). 2169 * @since 5.9.0 The `meta_input` field can be passed to `$userdata` to allow addition of user meta data. 2170 * 2171 * @global wpdb $wpdb WordPress database abstraction object. 2172 * 2173 * @param array|object|WP_User $userdata { 2174 * An array, object, or WP_User object of user data arguments. 2175 * 2176 * @type int $ID User ID. If supplied, the user will be updated. 2177 * @type string $user_pass The plain-text user password for new users. 2178 * Hashed password for existing users. 2179 * @type string $user_login The user's login username. 2180 * @type string $user_nicename The URL-friendly user name. 2181 * @type string $user_url The user URL. 2182 * @type string $user_email The user email address. 2183 * @type string $display_name The user's display name. 2184 * Default is the user's username. 2185 * @type string $nickname The user's nickname. 2186 * Default is the user's username. 2187 * @type string $first_name The user's first name. For new users, will be used 2188 * to build the first part of the user's display name 2189 * if `$display_name` is not specified. 2190 * @type string $last_name The user's last name. For new users, will be used 2191 * to build the second part of the user's display name 2192 * if `$display_name` is not specified. 2193 * @type string $description The user's biographical description. 2194 * @type string $rich_editing Whether to enable the rich-editor for the user. 2195 * Accepts 'true' or 'false' as a string literal, 2196 * not boolean. Default 'true'. 2197 * @type string $syntax_highlighting Whether to enable the rich code editor for the user. 2198 * Accepts 'true' or 'false' as a string literal, 2199 * not boolean. Default 'true'. 2200 * @type string $comment_shortcuts Whether to enable comment moderation keyboard 2201 * shortcuts for the user. Accepts 'true' or 'false' 2202 * as a string literal, not boolean. Default 'false'. 2203 * @type string $admin_color Admin color scheme for the user. Default 'modern'. 2204 * @type bool $use_ssl Whether the user should always access the admin over 2205 * https. Default false. 2206 * @type string $user_registered Date the user registered in UTC. Format is 'Y-m-d H:i:s'. 2207 * @type string $user_activation_key Password reset key. Default empty. 2208 * @type bool $spam Multisite only. Whether the user is marked as spam. 2209 * Default false. 2210 * @type string $show_admin_bar_front Whether to display the Admin Bar for the user 2211 * on the site's front end. Accepts 'true' or 'false' 2212 * as a string literal, not boolean. Default 'true'. 2213 * @type string $role User's role. 2214 * @type string $locale User's locale. Default empty. 2215 * @type array $meta_input Array of custom user meta values keyed by meta key. 2216 * Default empty. 2217 * } 2218 * @return int|WP_Error The newly created user's ID or a WP_Error object if the user could not 2219 * be created. 2220 */ 2221 function wp_insert_user( $userdata ) { 2222 global $wpdb; 2223 2224 if ( $userdata instanceof stdClass ) { 2225 $userdata = get_object_vars( $userdata ); 2226 } elseif ( $userdata instanceof WP_User ) { 2227 $userdata = $userdata->to_array(); 2228 } elseif ( $userdata instanceof Traversable ) { 2229 $userdata = iterator_to_array( $userdata ); 2230 } elseif ( $userdata instanceof ArrayAccess ) { 2231 $userdata_obj = $userdata; 2232 $userdata = array(); 2233 foreach ( 2234 array( 2235 'ID', 2236 'user_pass', 2237 'user_login', 2238 'user_nicename', 2239 'user_url', 2240 'user_email', 2241 'display_name', 2242 'nickname', 2243 'first_name', 2244 'last_name', 2245 'description', 2246 'rich_editing', 2247 'syntax_highlighting', 2248 'infinite_scrolling', 2249 'comment_shortcuts', 2250 'admin_color', 2251 'use_ssl', 2252 'user_registered', 2253 'user_activation_key', 2254 'spam', 2255 'show_admin_bar_front', 2256 'role', 2257 'locale', 2258 'meta_input', 2259 ) as $key 2260 ) { 2261 if ( isset( $userdata_obj[ $key ] ) ) { 2262 $userdata[ $key ] = $userdata_obj[ $key ]; 2263 } 2264 } 2265 } else { 2266 $userdata = (array) $userdata; 2267 } 2268 2269 // Are we updating or creating? 2270 if ( ! empty( $userdata['ID'] ) ) { 2271 $user_id = (int) $userdata['ID']; 2272 $update = true; 2273 $old_user_data = get_userdata( $user_id ); 2274 2275 if ( ! $old_user_data ) { 2276 return new WP_Error( 'invalid_user_id', __( 'Invalid user ID.' ) ); 2277 } 2278 2279 // Slash current user email to compare it later with slashed new user email. 2280 $old_user_data->user_email = wp_slash( $old_user_data->user_email ); 2281 2282 // Hashed in wp_update_user(), plaintext if called directly. 2283 $user_pass = ! empty( $userdata['user_pass'] ) ? $userdata['user_pass'] : $old_user_data->user_pass; 2284 } else { 2285 $update = false; 2286 2287 if ( empty( $userdata['user_pass'] ) ) { 2288 wp_trigger_error( 2289 __FUNCTION__, 2290 __( 'The user_pass field is required when creating a new user. The user will need to reset their password before logging in.' ), 2291 E_USER_WARNING 2292 ); 2293 2294 // Set the password as an empty string to force the password reset flow. 2295 $userdata['user_pass'] = ''; 2296 } 2297 2298 // Hash the password. 2299 $user_pass = wp_hash_password( $userdata['user_pass'] ); 2300 } 2301 2302 $sanitized_user_login = sanitize_user( $userdata['user_login'] ?? '', true ); 2303 2304 /** 2305 * Filters a username after it has been sanitized. 2306 * 2307 * This filter is called before the user is created or updated. 2308 * 2309 * @since 2.0.3 2310 * 2311 * @param string $sanitized_user_login Username after it has been sanitized. 2312 */ 2313 $pre_user_login = apply_filters( 'pre_user_login', $sanitized_user_login ); 2314 2315 // Remove any non-printable chars from the login string to see if we have ended up with an empty username. 2316 $user_login = trim( $pre_user_login ); 2317 2318 // user_login must be between 0 and 60 characters. 2319 if ( empty( $user_login ) ) { 2320 return new WP_Error( 'empty_user_login', __( 'Cannot create a user with an empty login name.' ) ); 2321 } elseif ( mb_strlen( $user_login ) > 60 ) { 2322 return new WP_Error( 'user_login_too_long', __( 'Username may not be longer than 60 characters.' ) ); 2323 } 2324 2325 if ( ! $update && username_exists( $user_login ) ) { 2326 return new WP_Error( 'existing_user_login', __( 'Sorry, that username already exists!' ) ); 2327 } 2328 2329 /** 2330 * Filters the list of disallowed usernames. 2331 * 2332 * @since 4.4.0 2333 * 2334 * @param array $usernames Array of disallowed usernames. 2335 */ 2336 $illegal_logins = (array) apply_filters( 'illegal_user_logins', array() ); 2337 2338 if ( in_array( strtolower( $user_login ), array_map( 'strtolower', $illegal_logins ), true ) ) { 2339 return new WP_Error( 'invalid_username', __( 'Sorry, that username is not allowed.' ) ); 2340 } 2341 2342 /* 2343 * If a nicename is provided, remove unsafe user characters before using it. 2344 * Otherwise build a nicename from the user_login. 2345 */ 2346 if ( ! empty( $userdata['user_nicename'] ) ) { 2347 $user_nicename = sanitize_user( $userdata['user_nicename'], true ); 2348 } else { 2349 $user_nicename = mb_substr( $user_login, 0, 50 ); 2350 } 2351 2352 $user_nicename = sanitize_title( $user_nicename ); 2353 2354 /** 2355 * Filters a user's nicename before the user is created or updated. 2356 * 2357 * @since 2.0.3 2358 * 2359 * @param string $user_nicename The user's nicename. 2360 */ 2361 $user_nicename = apply_filters( 'pre_user_nicename', $user_nicename ); 2362 2363 // Check if the sanitized nicename is empty. 2364 if ( empty( $user_nicename ) ) { 2365 return new WP_Error( 'empty_user_nicename', __( 'Cannot create a user with an empty nicename.' ) ); 2366 } elseif ( mb_strlen( $user_nicename ) > 50 ) { 2367 return new WP_Error( 'user_nicename_too_long', __( 'Nicename may not be longer than 50 characters.' ) ); 2368 } 2369 2370 $user_nicename_check = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1", $user_nicename, $user_login ) ); 2371 2372 if ( $user_nicename_check ) { 2373 $suffix = 2; 2374 while ( $user_nicename_check ) { 2375 // user_nicename allows 50 chars. Subtract one for a hyphen, plus the length of the suffix. 2376 $base_length = 49 - mb_strlen( $suffix ); 2377 $alt_user_nicename = mb_substr( $user_nicename, 0, $base_length ) . "-$suffix"; 2378 $user_nicename_check = $wpdb->get_var( $wpdb->prepare( "SELECT ID FROM $wpdb->users WHERE user_nicename = %s AND user_login != %s LIMIT 1", $alt_user_nicename, $user_login ) ); 2379 ++$suffix; 2380 } 2381 $user_nicename = $alt_user_nicename; 2382 } 2383 2384 $raw_user_email = empty( $userdata['user_email'] ) ? '' : $userdata['user_email']; 2385 2386 /** 2387 * Filters a user's email before the user is created or updated. 2388 * 2389 * @since 2.0.3 2390 * 2391 * @param string $raw_user_email The user's email. 2392 */ 2393 $user_email = apply_filters( 'pre_user_email', $raw_user_email ); 2394 2395 /* 2396 * If there is no update, just check for `email_exists`. If there is an update, 2397 * check if current email and new email are the same, and check `email_exists` 2398 * accordingly. 2399 */ 2400 if ( ( ! $update || ( ! empty( $old_user_data ) && 0 !== strcasecmp( $user_email, $old_user_data->user_email ) ) ) 2401 && ! defined( 'WP_IMPORTING' ) 2402 && email_exists( $user_email ) 2403 ) { 2404 return new WP_Error( 'existing_user_email', __( 'Sorry, that email address is already used!' ) ); 2405 } 2406 2407 $raw_user_url = empty( $userdata['user_url'] ) ? '' : $userdata['user_url']; 2408 2409 /** 2410 * Filters a user's URL before the user is created or updated. 2411 * 2412 * @since 2.0.3 2413 * 2414 * @param string $raw_user_url The user's URL. 2415 */ 2416 $user_url = apply_filters( 'pre_user_url', $raw_user_url ); 2417 2418 if ( mb_strlen( $user_url ) > 100 ) { 2419 return new WP_Error( 'user_url_too_long', __( 'User URL may not be longer than 100 characters.' ) ); 2420 } 2421 2422 $user_registered = empty( $userdata['user_registered'] ) ? gmdate( 'Y-m-d H:i:s' ) : $userdata['user_registered']; 2423 2424 $user_activation_key = empty( $userdata['user_activation_key'] ) ? '' : $userdata['user_activation_key']; 2425 2426 if ( ! empty( $userdata['spam'] ) && ! is_multisite() ) { 2427 return new WP_Error( 'no_spam', __( 'Sorry, marking a user as spam is only supported on Multisite.' ) ); 2428 } 2429 2430 $spam = empty( $userdata['spam'] ) ? 0 : (bool) $userdata['spam']; 2431 2432 // Store values to save in user meta. 2433 $meta = array(); 2434 2435 $nickname = empty( $userdata['nickname'] ) ? $user_login : $userdata['nickname']; 2436 2437 /** 2438 * Filters a user's nickname before the user is created or updated. 2439 * 2440 * @since 2.0.3 2441 * 2442 * @param string $nickname The user's nickname. 2443 */ 2444 $meta['nickname'] = apply_filters( 'pre_user_nickname', $nickname ); 2445 2446 $first_name = empty( $userdata['first_name'] ) ? '' : $userdata['first_name']; 2447 2448 /** 2449 * Filters a user's first name before the user is created or updated. 2450 * 2451 * @since 2.0.3 2452 * 2453 * @param string $first_name The user's first name. 2454 */ 2455 $meta['first_name'] = apply_filters( 'pre_user_first_name', $first_name ); 2456 2457 $last_name = empty( $userdata['last_name'] ) ? '' : $userdata['last_name']; 2458 2459 /** 2460 * Filters a user's last name before the user is created or updated. 2461 * 2462 * @since 2.0.3 2463 * 2464 * @param string $last_name The user's last name. 2465 */ 2466 $meta['last_name'] = apply_filters( 'pre_user_last_name', $last_name ); 2467 2468 if ( empty( $userdata['display_name'] ) ) { 2469 if ( $update ) { 2470 $display_name = $user_login; 2471 } elseif ( $meta['first_name'] && $meta['last_name'] ) { 2472 $display_name = sprintf( 2473 /* translators: 1: User's first name, 2: Last name. */ 2474 _x( '%1$s %2$s', 'Display name based on first name and last name' ), 2475 $meta['first_name'], 2476 $meta['last_name'] 2477 ); 2478 } elseif ( $meta['first_name'] ) { 2479 $display_name = $meta['first_name']; 2480 } elseif ( $meta['last_name'] ) { 2481 $display_name = $meta['last_name']; 2482 } else { 2483 $display_name = $user_login; 2484 } 2485 } else { 2486 $display_name = $userdata['display_name']; 2487 } 2488 2489 /** 2490 * Filters a user's display name before the user is created or updated. 2491 * 2492 * @since 2.0.3 2493 * 2494 * @param string $display_name The user's display name. 2495 */ 2496 $display_name = apply_filters( 'pre_user_display_name', $display_name ); 2497 2498 $description = empty( $userdata['description'] ) ? '' : $userdata['description']; 2499 2500 /** 2501 * Filters a user's description before the user is created or updated. 2502 * 2503 * @since 2.0.3 2504 * 2505 * @param string $description The user's description. 2506 */ 2507 $meta['description'] = apply_filters( 'pre_user_description', $description ); 2508 2509 $meta['rich_editing'] = empty( $userdata['rich_editing'] ) ? 'true' : $userdata['rich_editing']; 2510 2511 $meta['syntax_highlighting'] = empty( $userdata['syntax_highlighting'] ) ? 'true' : $userdata['syntax_highlighting']; 2512 2513 $meta['infinite_scrolling'] = empty( $userdata['infinite_scrolling'] ) ? 'true' : $userdata['infinite_scrolling']; 2514 2515 $meta['comment_shortcuts'] = empty( $userdata['comment_shortcuts'] ) || 'false' === $userdata['comment_shortcuts'] ? 'false' : 'true'; 2516 2517 $admin_color = empty( $userdata['admin_color'] ) ? 'modern' : $userdata['admin_color']; 2518 $meta['admin_color'] = preg_replace( '|[^a-z0-9 _.\-@]|i', '', $admin_color ); 2519 2520 $meta['use_ssl'] = empty( $userdata['use_ssl'] ) ? '0' : '1'; 2521 2522 $meta['show_admin_bar_front'] = empty( $userdata['show_admin_bar_front'] ) ? 'true' : $userdata['show_admin_bar_front']; 2523 2524 $meta['locale'] = $userdata['locale'] ?? ''; 2525 2526 $compacted = compact( 'user_pass', 'user_nicename', 'user_email', 'user_url', 'user_registered', 'user_activation_key', 'display_name' ); 2527 $data = wp_unslash( $compacted ); 2528 2529 if ( ! $update ) { 2530 $data = $data + compact( 'user_login' ); 2531 } 2532 2533 if ( is_multisite() ) { 2534 $data = $data + compact( 'spam' ); 2535 } 2536 2537 /** 2538 * Filters user data before the record is created or updated. 2539 * 2540 * It only includes data in the users table, not any user metadata. 2541 * 2542 * @since 4.9.0 2543 * @since 5.8.0 The `$userdata` parameter was added. 2544 * @since 6.8.0 The user's password is now hashed using bcrypt by default instead of phpass. 2545 * 2546 * @param array $data { 2547 * Values and keys for the user. 2548 * 2549 * @type string $user_login The user's login. Only included if $update == false 2550 * @type string $user_pass The user's password. 2551 * @type string $user_email The user's email. 2552 * @type string $user_url The user's url. 2553 * @type string $user_nicename The user's nice name. Defaults to a URL-safe version of user's login. 2554 * @type string $display_name The user's display name. 2555 * @type string $user_registered MySQL timestamp describing the moment when the user registered. Defaults to 2556 * the current UTC timestamp. 2557 * } 2558 * @param bool $update Whether the user is being updated rather than created. 2559 * @param int|null $user_id ID of the user to be updated, or NULL if the user is being created. 2560 * @param array $userdata The raw array of data passed to wp_insert_user(). 2561 */ 2562 $data = apply_filters( 'wp_pre_insert_user_data', $data, $update, ( $update ? $user_id : null ), $userdata ); 2563 2564 if ( empty( $data ) || ! is_array( $data ) ) { 2565 return new WP_Error( 'empty_data', __( 'Not enough data to create this user.' ) ); 2566 } 2567 2568 if ( $update ) { 2569 if ( $user_email !== $old_user_data->user_email || $user_pass !== $old_user_data->user_pass ) { 2570 $data['user_activation_key'] = ''; 2571 } 2572 $wpdb->update( $wpdb->users, $data, array( 'ID' => $user_id ) ); 2573 } else { 2574 $wpdb->insert( $wpdb->users, $data ); 2575 $user_id = (int) $wpdb->insert_id; 2576 } 2577 2578 $user = new WP_User( $user_id ); 2579 2580 if ( ! $update ) { 2581 /** This action is documented in wp-includes/pluggable.php */ 2582 do_action( 'wp_set_password', $userdata['user_pass'], $user_id, $user ); 2583 } 2584 2585 /** 2586 * Filters a user's meta values and keys immediately after the user is created or updated 2587 * and before any user meta is inserted or updated. 2588 * 2589 * Does not include contact methods. These are added using `wp_get_user_contact_methods( $user )`. 2590 * 2591 * For custom meta fields, see the {@see 'insert_custom_user_meta'} filter. 2592 * 2593 * @since 4.4.0 2594 * @since 5.8.0 The `$userdata` parameter was added. 2595 * 2596 * @param array $meta { 2597 * Default meta values and keys for the user. 2598 * 2599 * @type string $nickname The user's nickname. Default is the user's username. 2600 * @type string $first_name The user's first name. 2601 * @type string $last_name The user's last name. 2602 * @type string $description The user's description. 2603 * @type string $rich_editing Whether to enable the rich-editor for the user. Default 'true'. 2604 * @type string $syntax_highlighting Whether to enable the rich code editor for the user. Default 'true'. 2605 * @type string $comment_shortcuts Whether to enable keyboard shortcuts for the user. Default 'false'. 2606 * @type string $admin_color The color scheme for a user's admin screen. Default 'modern'. 2607 * @type int|bool $use_ssl Whether to force SSL on the user's admin area. 0|false if SSL 2608 * is not forced. 2609 * @type string $show_admin_bar_front Whether to show the admin bar on the front end for the user. 2610 * Default 'true'. 2611 * @type string $locale User's locale. Default empty. 2612 * } 2613 * @param WP_User $user User object. 2614 * @param bool $update Whether the user is being updated rather than created. 2615 * @param array $userdata The raw array of data passed to wp_insert_user(). 2616 */ 2617 $meta = apply_filters( 'insert_user_meta', $meta, $user, $update, $userdata ); 2618 2619 $custom_meta = array(); 2620 if ( array_key_exists( 'meta_input', $userdata ) && is_array( $userdata['meta_input'] ) && ! empty( $userdata['meta_input'] ) ) { 2621 $custom_meta = $userdata['meta_input']; 2622 } 2623 2624 /** 2625 * Filters a user's custom meta values and keys immediately after the user is created or updated 2626 * and before any user meta is inserted or updated. 2627 * 2628 * For non-custom meta fields, see the {@see 'insert_user_meta'} filter. 2629 * 2630 * @since 5.9.0 2631 * 2632 * @param array $custom_meta Array of custom user meta values keyed by meta key. 2633 * @param WP_User $user User object. 2634 * @param bool $update Whether the user is being updated rather than created. 2635 * @param array $userdata The raw array of data passed to wp_insert_user(). 2636 */ 2637 $custom_meta = apply_filters( 'insert_custom_user_meta', $custom_meta, $user, $update, $userdata ); 2638 2639 $meta = array_merge( $meta, $custom_meta ); 2640 2641 if ( $update ) { 2642 // Update user meta. 2643 foreach ( $meta as $key => $value ) { 2644 update_user_meta( $user_id, $key, $value ); 2645 } 2646 } else { 2647 // Add user meta. 2648 foreach ( $meta as $key => $value ) { 2649 add_user_meta( $user_id, $key, $value ); 2650 } 2651 } 2652 2653 foreach ( wp_get_user_contact_methods( $user ) as $key => $value ) { 2654 if ( isset( $userdata[ $key ] ) ) { 2655 update_user_meta( $user_id, $key, $userdata[ $key ] ); 2656 } 2657 } 2658 2659 if ( isset( $userdata['role'] ) ) { 2660 $user->set_role( $userdata['role'] ); 2661 } elseif ( ! $update ) { 2662 $user->set_role( get_option( 'default_role' ) ); 2663 } 2664 2665 clean_user_cache( $user_id ); 2666 2667 if ( $update ) { 2668 /** 2669 * Fires immediately after an existing user is updated. 2670 * 2671 * @since 2.0.0 2672 * @since 5.8.0 The `$userdata` parameter was added. 2673 * 2674 * @param int $user_id User ID. 2675 * @param WP_User $old_user_data Object containing user's data prior to update. 2676 * @param array $userdata The raw array of data passed to wp_insert_user(). 2677 */ 2678 do_action( 'profile_update', $user_id, $old_user_data, $userdata ); 2679 2680 if ( isset( $userdata['spam'] ) && $userdata['spam'] !== $old_user_data->spam ) { 2681 if ( '1' === $userdata['spam'] ) { 2682 /** 2683 * Fires after the user is marked as a SPAM user. 2684 * 2685 * @since 3.0.0 2686 * 2687 * @param int $user_id ID of the user marked as SPAM. 2688 */ 2689 do_action( 'make_spam_user', $user_id ); 2690 } else { 2691 /** 2692 * Fires after the user is marked as a HAM user. Opposite of SPAM. 2693 * 2694 * @since 3.0.0 2695 * 2696 * @param int $user_id ID of the user marked as HAM. 2697 */ 2698 do_action( 'make_ham_user', $user_id ); 2699 } 2700 } 2701 } else { 2702 /** 2703 * Fires immediately after a new user is registered. 2704 * 2705 * @since 1.5.0 2706 * @since 5.8.0 The `$userdata` parameter was added. 2707 * 2708 * @param int $user_id User ID. 2709 * @param array $userdata The raw array of data passed to wp_insert_user(). 2710 */ 2711 do_action( 'user_register', $user_id, $userdata ); 2712 } 2713 2714 return $user_id; 2715 } 2716 2717 /** 2718 * Updates a user in the database. 2719 * 2720 * It is possible to update a user's password by specifying the 'user_pass' 2721 * value in the $userdata parameter array. 2722 * 2723 * If current user's password is being updated, then the cookies will be 2724 * cleared. 2725 * 2726 * @since 2.0.0 2727 * 2728 * @see wp_insert_user() For what fields can be set in $userdata. 2729 * 2730 * @param array|object|WP_User $userdata An array of user data or a user object of type stdClass or WP_User. 2731 * @return int|WP_Error The updated user's ID or a WP_Error object if the user could not be updated. 2732 */ 2733 function wp_update_user( $userdata ) { 2734 if ( $userdata instanceof stdClass ) { 2735 $userdata = get_object_vars( $userdata ); 2736 } elseif ( $userdata instanceof WP_User ) { 2737 $userdata = $userdata->to_array(); 2738 } 2739 2740 $userdata_raw = $userdata; 2741 2742 $user_id = (int) ( $userdata['ID'] ?? 0 ); 2743 if ( ! $user_id ) { 2744 return new WP_Error( 'invalid_user_id', __( 'Invalid user ID.' ) ); 2745 } 2746 2747 // First, get all of the original fields. 2748 $user_obj = get_userdata( $user_id ); 2749 if ( ! $user_obj ) { 2750 return new WP_Error( 'invalid_user_id', __( 'Invalid user ID.' ) ); 2751 } 2752 2753 $user = $user_obj->to_array(); 2754 2755 // Add additional custom fields. 2756 foreach ( _get_additional_user_keys( $user_obj ) as $key ) { 2757 $user[ $key ] = get_user_meta( $user_id, $key, true ); 2758 } 2759 2760 // Escape data pulled from DB. 2761 $user = add_magic_quotes( $user ); 2762 2763 if ( ! empty( $userdata['user_pass'] ) && $userdata['user_pass'] !== $user_obj->user_pass ) { 2764 // If password is changing, hash it now. 2765 $plaintext_pass = $userdata['user_pass']; 2766 $userdata['user_pass'] = wp_hash_password( $userdata['user_pass'] ); 2767 2768 /** This action is documented in wp-includes/pluggable.php */ 2769 do_action( 'wp_set_password', $plaintext_pass, $user_id, $user_obj ); 2770 2771 /** 2772 * Filters whether to send the password change email. 2773 * 2774 * @since 4.3.0 2775 * 2776 * @see wp_insert_user() For `$user` and `$userdata` fields. 2777 * 2778 * @param bool $send Whether to send the email. 2779 * @param array $user The original user array. 2780 * @param array $userdata The updated user array. 2781 */ 2782 $send_password_change_email = apply_filters( 'send_password_change_email', true, $user, $userdata ); 2783 } 2784 2785 if ( isset( $userdata['user_email'] ) && $user['user_email'] !== $userdata['user_email'] ) { 2786 /** 2787 * Filters whether to send the email change email. 2788 * 2789 * @since 4.3.0 2790 * 2791 * @see wp_insert_user() For `$user` and `$userdata` fields. 2792 * 2793 * @param bool $send Whether to send the email. 2794 * @param array $user The original user array. 2795 * @param array $userdata The updated user array. 2796 */ 2797 $send_email_change_email = apply_filters( 'send_email_change_email', true, $user, $userdata ); 2798 } 2799 2800 clean_user_cache( $user_obj ); 2801 2802 // Merge old and new fields with new fields overwriting old ones. 2803 $userdata = array_merge( $user, $userdata ); 2804 $user_id = wp_insert_user( $userdata ); 2805 2806 if ( is_wp_error( $user_id ) ) { 2807 return $user_id; 2808 } 2809 2810 $blog_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); 2811 2812 $switched_locale = false; 2813 if ( ! empty( $send_password_change_email ) || ! empty( $send_email_change_email ) ) { 2814 $switched_locale = switch_to_user_locale( $user_id ); 2815 } 2816 2817 if ( ! empty( $send_password_change_email ) ) { 2818 /* translators: Do not translate USERNAME, ADMIN_EMAIL, EMAIL, SITENAME, SITEURL: those are placeholders. */ 2819 $pass_change_text = __( 2820 'Hi ###USERNAME###, 2821 2822 This notice confirms that your password was changed on ###SITENAME###. 2823 2824 If you did not change your password, please contact the Site Administrator at 2825 ###ADMIN_EMAIL### 2826 2827 This email has been sent to ###EMAIL### 2828 2829 Regards, 2830 All at ###SITENAME### 2831 ###SITEURL###' 2832 ); 2833 2834 $pass_change_email = array( 2835 'to' => $user['user_email'], 2836 /* translators: Password change notification email subject. %s: Site title. */ 2837 'subject' => __( '[%s] Password Changed' ), 2838 'message' => $pass_change_text, 2839 'headers' => '', 2840 ); 2841 2842 /** 2843 * Filters the contents of the email sent when the user's password is changed. 2844 * 2845 * @since 4.3.0 2846 * 2847 * @param array $pass_change_email { 2848 * Used to build wp_mail(). 2849 * 2850 * @type string $to The intended recipients. Add emails in a comma separated string. 2851 * @type string $subject The subject of the email. 2852 * @type string $message The content of the email. 2853 * The following strings have a special meaning and will get replaced dynamically: 2854 * - `###USERNAME###` The current user's username. 2855 * - `###ADMIN_EMAIL###` The admin email in case this was unexpected. 2856 * - `###EMAIL###` The user's email address. 2857 * - `###SITENAME###` The name of the site. 2858 * - `###SITEURL###` The URL to the site. 2859 * @type string $headers Headers. Add headers in a newline (\r\n) separated string. 2860 * } 2861 * @param array $user The original user array. 2862 * @param array $userdata The updated user array. 2863 */ 2864 $pass_change_email = apply_filters( 'password_change_email', $pass_change_email, $user, $userdata ); 2865 2866 $pass_change_email['message'] = str_replace( '###USERNAME###', $user['user_login'], $pass_change_email['message'] ); 2867 $pass_change_email['message'] = str_replace( '###ADMIN_EMAIL###', get_option( 'admin_email' ), $pass_change_email['message'] ); 2868 $pass_change_email['message'] = str_replace( '###EMAIL###', $user['user_email'], $pass_change_email['message'] ); 2869 $pass_change_email['message'] = str_replace( '###SITENAME###', $blog_name, $pass_change_email['message'] ); 2870 $pass_change_email['message'] = str_replace( '###SITEURL###', home_url(), $pass_change_email['message'] ); 2871 2872 wp_mail( $pass_change_email['to'], sprintf( $pass_change_email['subject'], $blog_name ), $pass_change_email['message'], $pass_change_email['headers'] ); 2873 } 2874 2875 if ( ! empty( $send_email_change_email ) ) { 2876 /* translators: Do not translate USERNAME, ADMIN_EMAIL, NEW_EMAIL, EMAIL, SITENAME, SITEURL: those are placeholders. */ 2877 $email_change_text = __( 2878 'Hi ###USERNAME###, 2879 2880 This notice confirms that your email address on ###SITENAME### was changed to ###NEW_EMAIL###. 2881 2882 If you did not change your email, please contact the Site Administrator at 2883 ###ADMIN_EMAIL### 2884 2885 This email has been sent to ###EMAIL### 2886 2887 Regards, 2888 All at ###SITENAME### 2889 ###SITEURL###' 2890 ); 2891 2892 $email_change_email = array( 2893 'to' => $user['user_email'], 2894 /* translators: Email change notification email subject. %s: Site title. */ 2895 'subject' => __( '[%s] Email Changed' ), 2896 'message' => $email_change_text, 2897 'headers' => '', 2898 ); 2899 2900 /** 2901 * Filters the contents of the email sent when the user's email is changed. 2902 * 2903 * @since 4.3.0 2904 * 2905 * @param array $email_change_email { 2906 * Used to build wp_mail(). 2907 * 2908 * @type string $to The intended recipients. 2909 * @type string $subject The subject of the email. 2910 * @type string $message The content of the email. 2911 * The following strings have a special meaning and will get replaced dynamically: 2912 * - `###USERNAME###` The current user's username. 2913 * - `###ADMIN_EMAIL###` The admin email in case this was unexpected. 2914 * - `###NEW_EMAIL###` The new email address. 2915 * - `###EMAIL###` The old email address. 2916 * - `###SITENAME###` The name of the site. 2917 * - `###SITEURL###` The URL to the site. 2918 * @type string $headers Headers. 2919 * } 2920 * @param array $user The original user array. 2921 * @param array $userdata The updated user array. 2922 */ 2923 $email_change_email = apply_filters( 'email_change_email', $email_change_email, $user, $userdata ); 2924 2925 $email_change_email['message'] = str_replace( '###USERNAME###', $user['user_login'], $email_change_email['message'] ); 2926 $email_change_email['message'] = str_replace( '###ADMIN_EMAIL###', get_option( 'admin_email' ), $email_change_email['message'] ); 2927 $email_change_email['message'] = str_replace( '###NEW_EMAIL###', $userdata['user_email'], $email_change_email['message'] ); 2928 $email_change_email['message'] = str_replace( '###EMAIL###', $user['user_email'], $email_change_email['message'] ); 2929 $email_change_email['message'] = str_replace( '###SITENAME###', $blog_name, $email_change_email['message'] ); 2930 $email_change_email['message'] = str_replace( '###SITEURL###', home_url(), $email_change_email['message'] ); 2931 2932 wp_mail( $email_change_email['to'], sprintf( $email_change_email['subject'], $blog_name ), $email_change_email['message'], $email_change_email['headers'] ); 2933 } 2934 2935 if ( $switched_locale ) { 2936 restore_previous_locale(); 2937 } 2938 2939 // Update the cookies if the password changed. 2940 $current_user = wp_get_current_user(); 2941 if ( $current_user->ID === $user_id ) { 2942 if ( isset( $plaintext_pass ) ) { 2943 /* 2944 * Here we calculate the expiration length of the current auth cookie and compare it to the default expiration. 2945 * If it's greater than this, then we know the user checked 'Remember Me' when they logged in. 2946 */ 2947 $logged_in_cookie = wp_parse_auth_cookie( '', 'logged_in' ); 2948 /** This filter is documented in wp-includes/pluggable.php */ 2949 $default_cookie_life = apply_filters( 'auth_cookie_expiration', ( 2 * DAY_IN_SECONDS ), $user_id, false ); 2950 2951 wp_clear_auth_cookie(); 2952 2953 $remember = false; 2954 $token = ''; 2955 2956 if ( false !== $logged_in_cookie ) { 2957 $token = $logged_in_cookie['token']; 2958 } 2959 2960 if ( false !== $logged_in_cookie && ( (int) $logged_in_cookie['expiration'] - time() ) > $default_cookie_life ) { 2961 $remember = true; 2962 } 2963 2964 wp_set_auth_cookie( $user_id, $remember, '', $token ); 2965 } 2966 } 2967 2968 /** 2969 * Fires after the user has been updated and emails have been sent. 2970 * 2971 * @since 6.3.0 2972 * 2973 * @param int $user_id The ID of the user that was just updated. 2974 * @param array $userdata The array of user data that was updated. 2975 * @param array $userdata_raw The unedited array of user data that was updated. 2976 */ 2977 do_action( 'wp_update_user', $user_id, $userdata, $userdata_raw ); 2978 2979 return $user_id; 2980 } 2981 2982 /** 2983 * Provides a simpler way of inserting a user into the database. 2984 * 2985 * Creates a new user with just the username, password, and email. For more 2986 * complex user creation use wp_insert_user() to specify more information. 2987 * 2988 * @since 2.0.0 2989 * 2990 * @see wp_insert_user() More complete way to create a new user. 2991 * 2992 * @param string $username The user's username. 2993 * @param string $password The user's password. 2994 * @param string $email Optional. The user's email. Default empty. 2995 * @return int|WP_Error The newly created user's ID or a WP_Error object if the user could not 2996 * be created. 2997 */ 2998 function wp_create_user( 2999 $username, 3000 #[\SensitiveParameter] 3001 $password, 3002 $email = '' 3003 ) { 3004 $user_login = wp_slash( $username ); 3005 $user_email = wp_slash( $email ); 3006 $user_pass = $password; 3007 3008 $userdata = compact( 'user_login', 'user_email', 'user_pass' ); 3009 return wp_insert_user( $userdata ); 3010 } 3011 3012 /** 3013 * Returns a list of meta keys to be (maybe) populated in wp_update_user(). 3014 * 3015 * The list of keys returned via this function are dependent on the presence 3016 * of those keys in the user meta data to be set. 3017 * 3018 * @since 3.3.0 3019 * @access private 3020 * 3021 * @param WP_User $user WP_User instance. 3022 * @return string[] List of user keys to be populated in wp_update_user(). 3023 */ 3024 function _get_additional_user_keys( $user ) { 3025 $keys = array( 'first_name', 'last_name', 'nickname', 'description', 'rich_editing', 'syntax_highlighting', 'infinite_scrolling', 'comment_shortcuts', 'admin_color', 'use_ssl', 'show_admin_bar_front', 'locale' ); 3026 return array_merge( $keys, array_keys( wp_get_user_contact_methods( $user ) ) ); 3027 } 3028 3029 /** 3030 * Sets up the user contact methods. 3031 * 3032 * Default contact methods were removed for new installations in WordPress 3.6 3033 * and completely removed from the codebase in WordPress 6.9. 3034 * 3035 * Use the {@see 'user_contactmethods'} filter to add or remove contact methods. 3036 * 3037 * @since 3.7.0 3038 * @since 6.9.0 Removed references to `aim`, `jabber`, and `yim` contact methods. 3039 * 3040 * @param WP_User|null $user Optional. WP_User object. 3041 * @return string[] Array of contact method labels keyed by contact method. 3042 */ 3043 function wp_get_user_contact_methods( $user = null ) { 3044 $methods = array(); 3045 3046 /** 3047 * Filters the user contact methods. 3048 * 3049 * @since 2.9.0 3050 * 3051 * @param string[] $methods Array of contact method labels keyed by contact method. 3052 * @param WP_User|null $user WP_User object or null if none was provided. 3053 */ 3054 return apply_filters( 'user_contactmethods', $methods, $user ); 3055 } 3056 3057 /** 3058 * The old private function for setting up user contact methods. 3059 * 3060 * Use wp_get_user_contact_methods() instead. 3061 * 3062 * @since 2.9.0 3063 * @access private 3064 * 3065 * @param WP_User|null $user Optional. WP_User object. Default null. 3066 * @return string[] Array of contact method labels keyed by contact method. 3067 */ 3068 function _wp_get_user_contactmethods( $user = null ) { 3069 return wp_get_user_contact_methods( $user ); 3070 } 3071 3072 /** 3073 * Gets the text suggesting how to create strong passwords. 3074 * 3075 * @since 4.1.0 3076 * 3077 * @return string The password hint text. 3078 */ 3079 function wp_get_password_hint() { 3080 $hint = __( 'Hint: The password should be at least twelve characters long. To make it stronger, use upper and lower case letters, numbers, and symbols like ! " ? $ % ^ & ).' ); 3081 3082 /** 3083 * Filters the text describing the site's password complexity policy. 3084 * 3085 * @since 4.1.0 3086 * 3087 * @param string $hint The password hint text. 3088 */ 3089 return apply_filters( 'password_hint', $hint ); 3090 } 3091 3092 /** 3093 * Creates, stores, then returns a password reset key for user. 3094 * 3095 * @since 4.4.0 3096 * 3097 * @param WP_User $user User to retrieve password reset key for. 3098 * @return string|WP_Error Password reset key on success. WP_Error on error. 3099 */ 3100 function get_password_reset_key( $user ) { 3101 if ( ! ( $user instanceof WP_User ) ) { 3102 return new WP_Error( 'invalidcombo', __( '<strong>Error:</strong> There is no account with that username or email address.' ) ); 3103 } 3104 3105 /** 3106 * Fires before a new password is retrieved. 3107 * 3108 * Use the {@see 'retrieve_password'} hook instead. 3109 * 3110 * @since 1.5.0 3111 * @deprecated 1.5.1 Misspelled. Use {@see 'retrieve_password'} hook instead. 3112 * 3113 * @param string $user_login The user login name. 3114 */ 3115 do_action_deprecated( 'retreive_password', array( $user->user_login ), '1.5.1', 'retrieve_password' ); 3116 3117 /** 3118 * Fires before a new password is retrieved. 3119 * 3120 * @since 1.5.1 3121 * 3122 * @param string $user_login The user login name. 3123 */ 3124 do_action( 'retrieve_password', $user->user_login ); 3125 3126 $password_reset_allowed = wp_is_password_reset_allowed_for_user( $user ); 3127 if ( ! $password_reset_allowed ) { 3128 return new WP_Error( 'no_password_reset', __( 'Password reset is not allowed for this user' ) ); 3129 } elseif ( is_wp_error( $password_reset_allowed ) ) { 3130 return $password_reset_allowed; 3131 } 3132 3133 // Generate something random for a password reset key. 3134 $key = wp_generate_password( 20, false ); 3135 3136 /** 3137 * Fires when a password reset key is generated. 3138 * 3139 * @since 2.5.0 3140 * 3141 * @param string $user_login The username for the user. 3142 * @param string $key The generated password reset key. 3143 */ 3144 do_action( 'retrieve_password_key', $user->user_login, $key ); 3145 3146 $hashed = time() . ':' . wp_fast_hash( $key ); 3147 3148 $key_saved = wp_update_user( 3149 array( 3150 'ID' => $user->ID, 3151 'user_activation_key' => $hashed, 3152 ) 3153 ); 3154 3155 if ( is_wp_error( $key_saved ) ) { 3156 return $key_saved; 3157 } 3158 3159 return $key; 3160 } 3161 3162 /** 3163 * Retrieves a user row based on password reset key and login. 3164 * 3165 * A key is considered 'expired' if it exactly matches the value of the 3166 * user_activation_key field, rather than being matched after going through the 3167 * hashing process. This field is now hashed; old values are no longer accepted 3168 * but have a different WP_Error code so good user feedback can be provided. 3169 * 3170 * @since 3.1.0 3171 * 3172 * @param string $key The password reset key. 3173 * @param string $login The user login. 3174 * @return WP_User|WP_Error WP_User object on success, WP_Error object for invalid or expired keys. 3175 */ 3176 function check_password_reset_key( 3177 #[\SensitiveParameter] 3178 $key, 3179 $login 3180 ) { 3181 $key = preg_replace( '/[^a-z0-9]/i', '', $key ); 3182 3183 if ( empty( $key ) || ! is_string( $key ) ) { 3184 return new WP_Error( 'invalid_key', __( 'Invalid key.' ) ); 3185 } 3186 3187 if ( empty( $login ) || ! is_string( $login ) ) { 3188 return new WP_Error( 'invalid_key', __( 'Invalid key.' ) ); 3189 } 3190 3191 $user = get_user_by( 'login', $login ); 3192 3193 if ( ! $user ) { 3194 return new WP_Error( 'invalid_key', __( 'Invalid key.' ) ); 3195 } 3196 3197 /** 3198 * Filters the expiration time of password reset keys. 3199 * 3200 * @since 4.3.0 3201 * 3202 * @param int $expiration The expiration time in seconds. 3203 */ 3204 $expiration_duration = apply_filters( 'password_reset_expiration', DAY_IN_SECONDS ); 3205 3206 if ( str_contains( $user->user_activation_key, ':' ) ) { 3207 list( $pass_request_time, $pass_key ) = explode( ':', $user->user_activation_key, 2 ); 3208 $expiration_time = $pass_request_time + $expiration_duration; 3209 } else { 3210 $pass_key = $user->user_activation_key; 3211 $expiration_time = false; 3212 } 3213 3214 if ( ! $pass_key ) { 3215 return new WP_Error( 'invalid_key', __( 'Invalid key.' ) ); 3216 } 3217 3218 $hash_is_correct = wp_verify_fast_hash( $key, $pass_key ); 3219 3220 if ( $hash_is_correct && $expiration_time && time() < $expiration_time ) { 3221 return $user; 3222 } elseif ( $hash_is_correct && $expiration_time ) { 3223 // Key has an expiration time that's passed. 3224 return new WP_Error( 'expired_key', __( 'Invalid key.' ) ); 3225 } 3226 3227 if ( hash_equals( $user->user_activation_key, $key ) || ( $hash_is_correct && ! $expiration_time ) ) { 3228 $return = new WP_Error( 'expired_key', __( 'Invalid key.' ) ); 3229 $user_id = $user->ID; 3230 3231 /** 3232 * Filters the return value of check_password_reset_key() when an 3233 * old-style key or an expired key is used. 3234 * 3235 * Prior to 3.7, plain-text keys were stored in the database. 3236 * 3237 * @since 3.7.0 3238 * @since 4.3.0 Previously key hashes were stored without an expiration time. 3239 * 3240 * @param WP_Error $return A WP_Error object denoting an expired key. 3241 * Return a WP_User object to validate the key. 3242 * @param int $user_id The matched user ID. 3243 */ 3244 return apply_filters( 'password_reset_key_expired', $return, $user_id ); 3245 } 3246 3247 return new WP_Error( 'invalid_key', __( 'Invalid key.' ) ); 3248 } 3249 3250 /** 3251 * Handles sending a password retrieval email to a user. 3252 * 3253 * @since 2.5.0 3254 * @since 5.7.0 Added `$user_login` parameter. 3255 * 3256 * @global wpdb $wpdb WordPress database abstraction object. 3257 * 3258 * @param string $user_login Optional. Username to send a password retrieval email for. 3259 * Defaults to `$_POST['user_login']` if not set. 3260 * @return true|WP_Error True when finished, WP_Error object on error. 3261 */ 3262 function retrieve_password( $user_login = '' ) { 3263 $errors = new WP_Error(); 3264 $user_data = false; 3265 3266 // Use the passed $user_login if available, otherwise use $_POST['user_login']. 3267 if ( ! $user_login && ! empty( $_POST['user_login'] ) && is_string( $_POST['user_login'] ) ) { 3268 $user_login = $_POST['user_login']; 3269 } 3270 3271 $user_login = trim( wp_unslash( $user_login ) ); 3272 3273 if ( empty( $user_login ) ) { 3274 $errors->add( 'empty_username', __( '<strong>Error:</strong> Please enter a username or email address.' ) ); 3275 } elseif ( strpos( $user_login, '@' ) ) { 3276 $user_data = get_user_by( 'email', $user_login ); 3277 3278 if ( empty( $user_data ) ) { 3279 $user_data = get_user_by( 'login', $user_login ); 3280 } 3281 3282 if ( empty( $user_data ) ) { 3283 $errors->add( 'invalid_email', __( '<strong>Error:</strong> There is no account with that username or email address.' ) ); 3284 } 3285 } else { 3286 $user_data = get_user_by( 'login', $user_login ); 3287 } 3288 3289 /** 3290 * Filters the user data during a password reset request. 3291 * 3292 * Allows, for example, custom validation using data other than username or email address. 3293 * 3294 * @since 5.7.0 3295 * 3296 * @param WP_User|false $user_data WP_User object if found, false if the user does not exist. 3297 * @param WP_Error $errors A WP_Error object containing any errors generated 3298 * by using invalid credentials. 3299 */ 3300 $user_data = apply_filters( 'lostpassword_user_data', $user_data, $errors ); 3301 3302 /** 3303 * Fires before errors are returned from a password reset request. 3304 * 3305 * @since 2.1.0 3306 * @since 4.4.0 Added the `$errors` parameter. 3307 * @since 5.4.0 Added the `$user_data` parameter. 3308 * 3309 * @param WP_Error $errors A WP_Error object containing any errors generated 3310 * by using invalid credentials. 3311 * @param WP_User|false $user_data WP_User object if found, false if the user does not exist. 3312 */ 3313 do_action( 'lostpassword_post', $errors, $user_data ); 3314 3315 /** 3316 * Filters the errors encountered on a password reset request. 3317 * 3318 * The filtered WP_Error object may, for example, contain errors for an invalid 3319 * username or email address. A WP_Error object should always be returned, 3320 * but may or may not contain errors. 3321 * 3322 * If any errors are present in $errors, this will abort the password reset request. 3323 * 3324 * @since 5.5.0 3325 * 3326 * @param WP_Error $errors A WP_Error object containing any errors generated 3327 * by using invalid credentials. 3328 * @param WP_User|false $user_data WP_User object if found, false if the user does not exist. 3329 */ 3330 $errors = apply_filters( 'lostpassword_errors', $errors, $user_data ); 3331 3332 if ( $errors->has_errors() ) { 3333 return $errors; 3334 } 3335 3336 if ( ! $user_data ) { 3337 $errors->add( 'invalidcombo', __( '<strong>Error:</strong> There is no account with that username or email address.' ) ); 3338 return $errors; 3339 } 3340 3341 /** 3342 * Filters whether to send the retrieve password email. 3343 * 3344 * Return false to disable sending the email. 3345 * 3346 * @since 6.0.0 3347 * 3348 * @param bool $send Whether to send the email. 3349 * @param string $user_login The username for the user. 3350 * @param WP_User $user_data WP_User object. 3351 */ 3352 if ( ! apply_filters( 'send_retrieve_password_email', true, $user_login, $user_data ) ) { 3353 return true; 3354 } 3355 3356 // Redefining user_login ensures we return the right case in the email. 3357 $user_login = $user_data->user_login; 3358 $user_email = $user_data->user_email; 3359 $key = get_password_reset_key( $user_data ); 3360 3361 if ( is_wp_error( $key ) ) { 3362 return $key; 3363 } 3364 3365 // Localize password reset message content for user. 3366 $locale = get_user_locale( $user_data ); 3367 3368 $switched_locale = switch_to_user_locale( $user_data->ID ); 3369 3370 if ( is_multisite() ) { 3371 $site_name = get_network()->site_name; 3372 } else { 3373 /* 3374 * The blogname option is escaped with esc_html on the way into the database 3375 * in sanitize_option. We want to reverse this for the plain text arena of emails. 3376 */ 3377 $site_name = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); 3378 } 3379 3380 $message = __( 'Someone has requested a password reset for the following account:' ) . "\r\n\r\n"; 3381 /* translators: %s: Site name. */ 3382 $message .= sprintf( __( 'Site Name: %s' ), $site_name ) . "\r\n\r\n"; 3383 /* translators: %s: User login. */ 3384 $message .= sprintf( __( 'Username: %s' ), $user_login ) . "\r\n\r\n"; 3385 $message .= __( 'If this was a mistake, ignore this email and nothing will happen.' ) . "\r\n\r\n"; 3386 $message .= __( 'To reset your password, visit the following address:' ) . "\r\n\r\n"; 3387 3388 /* 3389 * Since some user login names end in a period, this could produce ambiguous URLs that 3390 * end in a period. To avoid the ambiguity, ensure that the login is not the last query 3391 * arg in the URL. If moving it to the end, a trailing period will need to be escaped. 3392 * 3393 * @see https://core.trac.wordpress.org/tickets/42957 3394 */ 3395 $message .= network_site_url( 'wp-login.php?login=' . rawurlencode( $user_login ) . "&key=$key&action=rp", 'login' ) . '&wp_lang=' . $locale . "\r\n\r\n"; 3396 3397 if ( ! is_user_logged_in() ) { 3398 $requester_ip = $_SERVER['REMOTE_ADDR']; 3399 if ( $requester_ip ) { 3400 $message .= sprintf( 3401 /* translators: %s: IP address of password reset requester. */ 3402 __( 'This password reset request originated from the IP address %s.' ), 3403 $requester_ip 3404 ) . "\r\n"; 3405 } 3406 } 3407 3408 /* translators: Password reset notification email subject. %s: Site title. */ 3409 $title = sprintf( __( '[%s] Password Reset' ), $site_name ); 3410 3411 /** 3412 * Filters the subject of the password reset email. 3413 * 3414 * @since 2.8.0 3415 * @since 4.4.0 Added the `$user_login` and `$user_data` parameters. 3416 * 3417 * @param string $title Email subject. 3418 * @param string $user_login The username for the user. 3419 * @param WP_User $user_data WP_User object. 3420 */ 3421 $title = apply_filters( 'retrieve_password_title', $title, $user_login, $user_data ); 3422 3423 /** 3424 * Filters the message body of the password reset mail. 3425 * 3426 * If the filtered message is empty, the password reset email will not be sent. 3427 * 3428 * @since 2.8.0 3429 * @since 4.1.0 Added `$user_login` and `$user_data` parameters. 3430 * 3431 * @param string $message Email message. 3432 * @param string $key The activation key. 3433 * @param string $user_login The username for the user. 3434 * @param WP_User $user_data WP_User object. 3435 */ 3436 $message = apply_filters( 'retrieve_password_message', $message, $key, $user_login, $user_data ); 3437 3438 // Short-circuit on falsey $message value for backwards compatibility. 3439 if ( ! $message ) { 3440 return true; 3441 } 3442 3443 /* 3444 * Wrap the single notification email arguments in an array 3445 * to pass them to the retrieve_password_notification_email filter. 3446 */ 3447 $defaults = array( 3448 'to' => $user_email, 3449 'subject' => $title, 3450 'message' => $message, 3451 'headers' => '', 3452 ); 3453 3454 /** 3455 * Filters the contents of the reset password notification email sent to the user. 3456 * 3457 * @since 6.0.0 3458 * 3459 * @param array $defaults { 3460 * The default notification email arguments. Used to build wp_mail(). 3461 * 3462 * @type string $to The intended recipient - user email address. 3463 * @type string $subject The subject of the email. 3464 * @type string $message The body of the email. 3465 * @type string $headers The headers of the email. 3466 * } 3467 * @param string $key The activation key. 3468 * @param string $user_login The username for the user. 3469 * @param WP_User $user_data WP_User object. 3470 */ 3471 $notification_email = apply_filters( 'retrieve_password_notification_email', $defaults, $key, $user_login, $user_data ); 3472 3473 if ( $switched_locale ) { 3474 restore_previous_locale(); 3475 } 3476 3477 if ( is_array( $notification_email ) ) { 3478 // Force key order and merge defaults in case any value is missing in the filtered array. 3479 $notification_email = array_merge( $defaults, $notification_email ); 3480 } else { 3481 $notification_email = $defaults; 3482 } 3483 3484 list( $to, $subject, $message, $headers ) = array_values( $notification_email ); 3485 3486 $subject = wp_specialchars_decode( $subject ); 3487 3488 if ( ! wp_mail( $to, $subject, $message, $headers ) ) { 3489 $errors->add( 3490 'retrieve_password_email_failure', 3491 sprintf( 3492 /* translators: %s: Documentation URL. */ 3493 __( '<strong>Error:</strong> The email could not be sent. Your site may not be correctly configured to send emails. <a href="%s">Get support for resetting your password</a>.' ), 3494 esc_url( __( 'https://wordpress.org/documentation/article/reset-your-password/' ) ) 3495 ) 3496 ); 3497 return $errors; 3498 } 3499 3500 return true; 3501 } 3502 3503 /** 3504 * Handles resetting the user's password. 3505 * 3506 * @since 2.5.0 3507 * 3508 * @param WP_User $user The user 3509 * @param string $new_pass New password for the user in plaintext 3510 */ 3511 function reset_password( 3512 $user, 3513 #[\SensitiveParameter] 3514 $new_pass 3515 ) { 3516 /** 3517 * Fires before the user's password is reset. 3518 * 3519 * @since 1.5.0 3520 * 3521 * @param WP_User $user The user. 3522 * @param string $new_pass New user password. 3523 */ 3524 do_action( 'password_reset', $user, $new_pass ); 3525 3526 wp_set_password( $new_pass, $user->ID ); 3527 update_user_meta( $user->ID, 'default_password_nag', false ); 3528 3529 /** 3530 * Fires after the user's password is reset. 3531 * 3532 * @since 4.4.0 3533 * 3534 * @param WP_User $user The user. 3535 * @param string $new_pass New user password. 3536 */ 3537 do_action( 'after_password_reset', $user, $new_pass ); 3538 } 3539 3540 /** 3541 * Handles registering a new user. 3542 * 3543 * @since 2.5.0 3544 * 3545 * @param string $user_login User's username for logging in 3546 * @param string $user_email User's email address to send password and add 3547 * @return int|WP_Error Either user's ID or error on failure. 3548 */ 3549 function register_new_user( $user_login, $user_email ) { 3550 $errors = new WP_Error(); 3551 3552 $sanitized_user_login = sanitize_user( $user_login ); 3553 /** 3554 * Filters the email address of a user being registered. 3555 * 3556 * @since 2.1.0 3557 * 3558 * @param string $user_email The email address of the new user. 3559 */ 3560 $user_email = apply_filters( 'user_registration_email', $user_email ); 3561 3562 // Check the username. 3563 if ( '' === $sanitized_user_login ) { 3564 $errors->add( 'empty_username', __( '<strong>Error:</strong> Please enter a username.' ) ); 3565 } elseif ( ! validate_username( $user_login ) ) { 3566 $errors->add( 'invalid_username', __( '<strong>Error:</strong> This username is invalid because it uses illegal characters. Please enter a valid username.' ) ); 3567 $sanitized_user_login = ''; 3568 } elseif ( username_exists( $sanitized_user_login ) ) { 3569 $errors->add( 'username_exists', __( '<strong>Error:</strong> This username is already registered. Please choose another one.' ) ); 3570 } else { 3571 /** This filter is documented in wp-includes/user.php */ 3572 $illegal_user_logins = (array) apply_filters( 'illegal_user_logins', array() ); 3573 if ( in_array( strtolower( $sanitized_user_login ), array_map( 'strtolower', $illegal_user_logins ), true ) ) { 3574 $errors->add( 'invalid_username', __( '<strong>Error:</strong> Sorry, that username is not allowed.' ) ); 3575 } 3576 } 3577 3578 // Check the email address. 3579 if ( '' === $user_email ) { 3580 $errors->add( 'empty_email', __( '<strong>Error:</strong> Please type your email address.' ) ); 3581 } elseif ( ! is_email( $user_email ) ) { 3582 $errors->add( 'invalid_email', __( '<strong>Error:</strong> The email address is not correct.' ) ); 3583 $user_email = ''; 3584 } elseif ( email_exists( $user_email ) ) { 3585 $errors->add( 3586 'email_exists', 3587 sprintf( 3588 /* translators: %s: Link to the login page. */ 3589 __( '<strong>Error:</strong> This email address is already registered. <a href="%s">Log in</a> with this address or choose another one.' ), 3590 wp_login_url() 3591 ) 3592 ); 3593 } 3594 3595 /** 3596 * Fires when submitting registration form data, before the user is created. 3597 * 3598 * @since 2.1.0 3599 * 3600 * @param string $sanitized_user_login The submitted username after being sanitized. 3601 * @param string $user_email The submitted email. 3602 * @param WP_Error $errors Contains any errors with submitted username and email, 3603 * e.g., an empty field, an invalid username or email, 3604 * or an existing username or email. 3605 */ 3606 do_action( 'register_post', $sanitized_user_login, $user_email, $errors ); 3607 3608 /** 3609 * Filters the errors encountered when a new user is being registered. 3610 * 3611 * The filtered WP_Error object may, for example, contain errors for an invalid 3612 * or existing username or email address. A WP_Error object should always be returned, 3613 * but may or may not contain errors. 3614 * 3615 * If any errors are present in $errors, this will abort the user's registration. 3616 * 3617 * @since 2.1.0 3618 * 3619 * @param WP_Error $errors A WP_Error object containing any errors encountered 3620 * during registration. 3621 * @param string $sanitized_user_login User's username after it has been sanitized. 3622 * @param string $user_email User's email. 3623 */ 3624 $errors = apply_filters( 'registration_errors', $errors, $sanitized_user_login, $user_email ); 3625 3626 if ( $errors->has_errors() ) { 3627 return $errors; 3628 } 3629 3630 $user_pass = wp_generate_password( 12, false ); 3631 $user_id = wp_create_user( $sanitized_user_login, $user_pass, $user_email ); 3632 if ( ! $user_id || is_wp_error( $user_id ) ) { 3633 $errors->add( 3634 'registerfail', 3635 sprintf( 3636 /* translators: %s: Admin email address. */ 3637 __( '<strong>Error:</strong> Could not register you… please contact the <a href="mailto:%s">site admin</a>!' ), 3638 get_option( 'admin_email' ) 3639 ) 3640 ); 3641 return $errors; 3642 } 3643 3644 update_user_meta( $user_id, 'default_password_nag', true ); // Set up the password change nag. 3645 3646 if ( ! empty( $_COOKIE['wp_lang'] ) ) { 3647 $wp_lang = sanitize_text_field( $_COOKIE['wp_lang'] ); 3648 if ( in_array( $wp_lang, get_available_languages(), true ) ) { 3649 update_user_meta( $user_id, 'locale', $wp_lang ); // Set user locale if defined on registration. 3650 } 3651 } 3652 3653 /** 3654 * Fires after a new user registration has been recorded. 3655 * 3656 * @since 4.4.0 3657 * 3658 * @param int $user_id ID of the newly registered user. 3659 */ 3660 do_action( 'register_new_user', $user_id ); 3661 3662 return $user_id; 3663 } 3664 3665 /** 3666 * Initiates email notifications related to the creation of new users. 3667 * 3668 * Notifications are sent both to the site admin and to the newly created user. 3669 * 3670 * @since 4.4.0 3671 * @since 4.6.0 Converted the `$notify` parameter to accept 'user' for sending 3672 * notifications only to the user created. 3673 * 3674 * @param int $user_id ID of the newly created user. 3675 * @param string $notify Optional. Type of notification that should happen. Accepts 'admin' 3676 * or an empty string (admin only), 'user', or 'both' (admin and user). 3677 * Default 'both'. 3678 */ 3679 function wp_send_new_user_notifications( $user_id, $notify = 'both' ) { 3680 wp_new_user_notification( $user_id, null, $notify ); 3681 } 3682 3683 /** 3684 * Retrieves the current session token from the logged_in cookie. 3685 * 3686 * @since 4.0.0 3687 * 3688 * @return string Token. 3689 */ 3690 function wp_get_session_token() { 3691 $cookie = wp_parse_auth_cookie( '', 'logged_in' ); 3692 return ! empty( $cookie['token'] ) ? $cookie['token'] : ''; 3693 } 3694 3695 /** 3696 * Retrieves a list of sessions for the current user. 3697 * 3698 * @since 4.0.0 3699 * 3700 * @return array Array of sessions. 3701 */ 3702 function wp_get_all_sessions() { 3703 $manager = WP_Session_Tokens::get_instance( get_current_user_id() ); 3704 return $manager->get_all(); 3705 } 3706 3707 /** 3708 * Removes the current session token from the database. 3709 * 3710 * @since 4.0.0 3711 */ 3712 function wp_destroy_current_session() { 3713 $token = wp_get_session_token(); 3714 if ( $token ) { 3715 $manager = WP_Session_Tokens::get_instance( get_current_user_id() ); 3716 $manager->destroy( $token ); 3717 } 3718 } 3719 3720 /** 3721 * Removes all but the current session token for the current user for the database. 3722 * 3723 * @since 4.0.0 3724 */ 3725 function wp_destroy_other_sessions() { 3726 $token = wp_get_session_token(); 3727 if ( $token ) { 3728 $manager = WP_Session_Tokens::get_instance( get_current_user_id() ); 3729 $manager->destroy_others( $token ); 3730 } 3731 } 3732 3733 /** 3734 * Removes all session tokens for the current user from the database. 3735 * 3736 * @since 4.0.0 3737 */ 3738 function wp_destroy_all_sessions() { 3739 $manager = WP_Session_Tokens::get_instance( get_current_user_id() ); 3740 $manager->destroy_all(); 3741 } 3742 3743 /** 3744 * Gets the user IDs of all users with no role on this site. 3745 * 3746 * @since 4.4.0 3747 * @since 4.9.0 The `$site_id` parameter was added to support multisite. 3748 * 3749 * @global wpdb $wpdb WordPress database abstraction object. 3750 * 3751 * @param int|null $site_id Optional. The site ID to get users with no role for. Defaults to the current site. 3752 * @return string[] Array of user IDs as strings. 3753 */ 3754 function wp_get_users_with_no_role( $site_id = null ) { 3755 global $wpdb; 3756 3757 if ( ! $site_id ) { 3758 $site_id = get_current_blog_id(); 3759 } 3760 3761 $prefix = $wpdb->get_blog_prefix( $site_id ); 3762 3763 if ( is_multisite() && get_current_blog_id() !== $site_id ) { 3764 switch_to_blog( $site_id ); 3765 $role_names = wp_roles()->get_names(); 3766 restore_current_blog(); 3767 } else { 3768 $role_names = wp_roles()->get_names(); 3769 } 3770 3771 $regex = implode( '|', array_keys( $role_names ) ); 3772 $regex = preg_replace( '/[^a-zA-Z_\|-]/', '', $regex ); 3773 $users = $wpdb->get_col( 3774 $wpdb->prepare( 3775 "SELECT user_id 3776 FROM $wpdb->usermeta 3777 WHERE meta_key = '{$prefix}capabilities' 3778 AND meta_value NOT REGEXP %s", 3779 $regex 3780 ) 3781 ); 3782 3783 return $users; 3784 } 3785 3786 /** 3787 * Retrieves the current user object. 3788 * 3789 * Will set the current user, if the current user is not set. The current user 3790 * will be set to the logged-in person. If no user is logged-in, then it will 3791 * set the current user to 0, which is invalid and won't have any permissions. 3792 * 3793 * This function is used by the pluggable functions wp_get_current_user() and 3794 * get_currentuserinfo(), the latter of which is deprecated but used for backward 3795 * compatibility. 3796 * 3797 * @since 4.5.0 3798 * @access private 3799 * 3800 * @see wp_get_current_user() 3801 * @global WP_User $current_user Checks if the current user is set. 3802 * 3803 * @return WP_User Current WP_User instance. 3804 */ 3805 function _wp_get_current_user() { 3806 global $current_user; 3807 3808 if ( ! empty( $current_user ) ) { 3809 if ( $current_user instanceof WP_User ) { 3810 return $current_user; 3811 } 3812 3813 // Upgrade stdClass to WP_User. 3814 if ( is_object( $current_user ) && isset( $current_user->ID ) ) { 3815 $cur_id = $current_user->ID; 3816 $current_user = null; 3817 wp_set_current_user( $cur_id ); 3818 return $current_user; 3819 } 3820 3821 // $current_user has a junk value. Force to WP_User with ID 0. 3822 $current_user = null; 3823 wp_set_current_user( 0 ); 3824 return $current_user; 3825 } 3826 3827 if ( defined( 'XMLRPC_REQUEST' ) && XMLRPC_REQUEST ) { 3828 wp_set_current_user( 0 ); 3829 return $current_user; 3830 } 3831 3832 /** 3833 * Filters the current user. 3834 * 3835 * The default filters use this to determine the current user from the 3836 * request's cookies, if available. 3837 * 3838 * Returning a value of false will effectively short-circuit setting 3839 * the current user. 3840 * 3841 * @since 3.9.0 3842 * 3843 * @param int|false $user_id User ID if one has been determined, false otherwise. 3844 */ 3845 $user_id = apply_filters( 'determine_current_user', false ); 3846 if ( ! $user_id ) { 3847 wp_set_current_user( 0 ); 3848 return $current_user; 3849 } 3850 3851 wp_set_current_user( $user_id ); 3852 3853 return $current_user; 3854 } 3855 3856 /** 3857 * Sends a confirmation request email when a change of user email address is attempted. 3858 * 3859 * @since 3.0.0 3860 * @since 4.9.0 This function was moved from wp-admin/includes/ms.php so it's no longer Multisite specific. 3861 * 3862 * @global WP_Error $errors WP_Error object. 3863 */ 3864 function send_confirmation_on_profile_email() { 3865 global $errors; 3866 3867 $current_user = wp_get_current_user(); 3868 if ( ! is_object( $errors ) ) { 3869 $errors = new WP_Error(); 3870 } 3871 3872 if ( $current_user->ID !== (int) $_POST['user_id'] ) { 3873 return false; 3874 } 3875 3876 if ( $current_user->user_email !== $_POST['email'] ) { 3877 if ( ! is_email( $_POST['email'] ) ) { 3878 $errors->add( 3879 'user_email', 3880 __( '<strong>Error:</strong> The email address is not correct.' ), 3881 array( 3882 'form-field' => 'email', 3883 ) 3884 ); 3885 3886 return; 3887 } 3888 3889 if ( email_exists( $_POST['email'] ) ) { 3890 $errors->add( 3891 'user_email', 3892 __( '<strong>Error:</strong> The email address is already used.' ), 3893 array( 3894 'form-field' => 'email', 3895 ) 3896 ); 3897 delete_user_meta( $current_user->ID, '_new_email' ); 3898 3899 return; 3900 } 3901 3902 $hash = md5( $_POST['email'] . time() . wp_rand() ); 3903 $new_user_email = array( 3904 'hash' => $hash, 3905 'newemail' => $_POST['email'], 3906 ); 3907 update_user_meta( $current_user->ID, '_new_email', $new_user_email ); 3908 3909 $sitename = wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ); 3910 3911 /* translators: Do not translate USERNAME, ADMIN_URL, EMAIL, SITENAME, SITEURL: those are placeholders. */ 3912 $email_text = __( 3913 'Howdy ###USERNAME###, 3914 3915 You recently requested to have the email address on your account changed. 3916 3917 If this is correct, please click on the following link to change it: 3918 ###ADMIN_URL### 3919 3920 You can safely ignore and delete this email if you do not want to 3921 take this action. 3922 3923 This email has been sent to ###EMAIL### 3924 3925 Regards, 3926 All at ###SITENAME### 3927 ###SITEURL###' 3928 ); 3929 3930 /** 3931 * Filters the text of the email sent when a change of user email address is attempted. 3932 * 3933 * The following strings have a special meaning and will get replaced dynamically: 3934 * 3935 * - `###USERNAME###` The current user's username. 3936 * - `###ADMIN_URL###` The link to click on to confirm the email change. 3937 * - `###EMAIL###` The new email. 3938 * - `###SITENAME###` The name of the site. 3939 * - `###SITEURL###` The URL to the site. 3940 * 3941 * @since MU (3.0.0) 3942 * @since 4.9.0 This filter is no longer Multisite specific. 3943 * 3944 * @param string $email_text Text in the email. 3945 * @param array $new_user_email { 3946 * Data relating to the new user email address. 3947 * 3948 * @type string $hash The secure hash used in the confirmation link URL. 3949 * @type string $newemail The proposed new email address. 3950 * } 3951 */ 3952 $content = apply_filters( 'new_user_email_content', $email_text, $new_user_email ); 3953 3954 $content = str_replace( '###USERNAME###', $current_user->user_login, $content ); 3955 $content = str_replace( '###ADMIN_URL###', esc_url( self_admin_url( 'profile.php?newuseremail=' . $hash ) ), $content ); 3956 $content = str_replace( '###EMAIL###', $_POST['email'], $content ); 3957 $content = str_replace( '###SITENAME###', $sitename, $content ); 3958 $content = str_replace( '###SITEURL###', home_url(), $content ); 3959 3960 /* translators: New email address notification email subject. %s: Site title. */ 3961 wp_mail( $_POST['email'], sprintf( __( '[%s] Email Change Request' ), $sitename ), $content ); 3962 3963 $_POST['email'] = $current_user->user_email; 3964 } 3965 } 3966 3967 /** 3968 * Adds an admin notice alerting the user to check for confirmation request email 3969 * after email address change. 3970 * 3971 * @since 3.0.0 3972 * @since 4.9.0 This function was moved from wp-admin/includes/ms.php so it's no longer Multisite specific. 3973 * 3974 * @global string $pagenow The filename of the current screen. 3975 */ 3976 function new_user_email_admin_notice() { 3977 global $pagenow; 3978 3979 if ( 'profile.php' === $pagenow && isset( $_GET['updated'] ) ) { 3980 $email = get_user_meta( get_current_user_id(), '_new_email', true ); 3981 if ( $email ) { 3982 $message = sprintf( 3983 /* translators: %s: New email address. */ 3984 __( 'Your email address has not been updated yet. Please check your inbox at %s for a confirmation email.' ), 3985 '<code>' . esc_html( $email['newemail'] ) . '</code>' 3986 ); 3987 wp_admin_notice( $message, array( 'type' => 'info' ) ); 3988 } 3989 } 3990 } 3991 3992 /** 3993 * Gets all personal data request types. 3994 * 3995 * @since 4.9.6 3996 * @access private 3997 * 3998 * @return string[] List of core privacy action types. 3999 */ 4000 function _wp_privacy_action_request_types() { 4001 return array( 4002 'export_personal_data', 4003 'remove_personal_data', 4004 ); 4005 } 4006 4007 /** 4008 * Registers the personal data exporter for users. 4009 * 4010 * @since 4.9.6 4011 * 4012 * @param array[] $exporters An array of personal data exporters. 4013 * @return array[] An array of personal data exporters. 4014 */ 4015 function wp_register_user_personal_data_exporter( $exporters ) { 4016 $exporters['wordpress-user'] = array( 4017 'exporter_friendly_name' => __( 'WordPress User' ), 4018 'callback' => 'wp_user_personal_data_exporter', 4019 ); 4020 4021 return $exporters; 4022 } 4023 4024 /** 4025 * Finds and exports personal data associated with an email address from the user and user_meta table. 4026 * 4027 * @since 4.9.6 4028 * @since 5.4.0 Added 'Community Events Location' group to the export data. 4029 * @since 5.4.0 Added 'Session Tokens' group to the export data. 4030 * 4031 * @param string $email_address The user's email address. 4032 * @return array { 4033 * An array of personal data. 4034 * 4035 * @type array[] $data An array of personal data arrays. 4036 * @type bool $done Whether the exporter is finished. 4037 * } 4038 */ 4039 function wp_user_personal_data_exporter( $email_address ) { 4040 $email_address = trim( $email_address ); 4041 4042 $data_to_export = array(); 4043 4044 $user = get_user_by( 'email', $email_address ); 4045 4046 if ( ! $user ) { 4047 return array( 4048 'data' => array(), 4049 'done' => true, 4050 ); 4051 } 4052 4053 $user_meta = get_user_meta( $user->ID ); 4054 4055 $user_props_to_export = array( 4056 'ID' => __( 'User ID' ), 4057 'user_login' => __( 'User Login Name' ), 4058 'user_nicename' => __( 'User Nice Name' ), 4059 'user_email' => __( 'User Email' ), 4060 'user_url' => __( 'User URL' ), 4061 'user_registered' => __( 'User Registration Date' ), 4062 'display_name' => __( 'User Display Name' ), 4063 'nickname' => __( 'User Nickname' ), 4064 'first_name' => __( 'User First Name' ), 4065 'last_name' => __( 'User Last Name' ), 4066 'description' => __( 'User Description' ), 4067 ); 4068 4069 $user_data_to_export = array(); 4070 4071 foreach ( $user_props_to_export as $key => $name ) { 4072 $value = ''; 4073 4074 switch ( $key ) { 4075 case 'ID': 4076 case 'user_login': 4077 case 'user_nicename': 4078 case 'user_email': 4079 case 'user_url': 4080 case 'user_registered': 4081 case 'display_name': 4082 $value = $user->data->$key; 4083 break; 4084 case 'nickname': 4085 case 'first_name': 4086 case 'last_name': 4087 case 'description': 4088 $value = $user_meta[ $key ][0]; 4089 break; 4090 } 4091 4092 if ( ! empty( $value ) ) { 4093 $user_data_to_export[] = array( 4094 'name' => $name, 4095 'value' => $value, 4096 ); 4097 } 4098 } 4099 4100 // Get the list of reserved names. 4101 $reserved_names = array_values( $user_props_to_export ); 4102 4103 /** 4104 * Filters the user's profile data for the privacy exporter. 4105 * 4106 * @since 5.4.0 4107 * 4108 * @param array $additional_user_profile_data { 4109 * An array of name-value pairs of additional user data items. Default empty array. 4110 * 4111 * @type string $name The user-facing name of an item name-value pair,e.g. 'IP Address'. 4112 * @type string $value The user-facing value of an item data pair, e.g. '50.60.70.0'. 4113 * } 4114 * @param WP_User $user The user whose data is being exported. 4115 * @param string[] $reserved_names An array of reserved names. Any item in `$additional_user_data` 4116 * that uses one of these for its `name` will not be included in the export. 4117 */ 4118 $_extra_data = apply_filters( 'wp_privacy_additional_user_profile_data', array(), $user, $reserved_names ); 4119 4120 if ( is_array( $_extra_data ) && ! empty( $_extra_data ) ) { 4121 // Remove items that use reserved names. 4122 $extra_data = array_filter( 4123 $_extra_data, 4124 static function ( $item ) use ( $reserved_names ) { 4125 return ! in_array( $item['name'], $reserved_names, true ); 4126 } 4127 ); 4128 4129 if ( count( $extra_data ) !== count( $_extra_data ) ) { 4130 _doing_it_wrong( 4131 __FUNCTION__, 4132 sprintf( 4133 /* translators: %s: wp_privacy_additional_user_profile_data */ 4134 __( 'Filter %s returned items with reserved names.' ), 4135 '<code>wp_privacy_additional_user_profile_data</code>' 4136 ), 4137 '5.4.0' 4138 ); 4139 } 4140 4141 if ( ! empty( $extra_data ) ) { 4142 $user_data_to_export = array_merge( $user_data_to_export, $extra_data ); 4143 } 4144 } 4145 4146 $data_to_export[] = array( 4147 'group_id' => 'user', 4148 'group_label' => __( 'User' ), 4149 'group_description' => __( 'User’s profile data.' ), 4150 'item_id' => "user-{$user->ID}", 4151 'data' => $user_data_to_export, 4152 ); 4153 4154 if ( isset( $user_meta['community-events-location'] ) ) { 4155 $location = maybe_unserialize( $user_meta['community-events-location'][0] ); 4156 4157 $location_props_to_export = array( 4158 'description' => __( 'City' ), 4159 'country' => __( 'Country' ), 4160 'latitude' => __( 'Latitude' ), 4161 'longitude' => __( 'Longitude' ), 4162 'ip' => __( 'IP' ), 4163 ); 4164 4165 $location_data_to_export = array(); 4166 4167 foreach ( $location_props_to_export as $key => $name ) { 4168 if ( ! empty( $location[ $key ] ) ) { 4169 $location_data_to_export[] = array( 4170 'name' => $name, 4171 'value' => $location[ $key ], 4172 ); 4173 } 4174 } 4175 4176 $data_to_export[] = array( 4177 'group_id' => 'community-events-location', 4178 'group_label' => __( 'Community Events Location' ), 4179 'group_description' => __( 'User’s location data used for the Community Events in the WordPress Events and News dashboard widget.' ), 4180 'item_id' => "community-events-location-{$user->ID}", 4181 'data' => $location_data_to_export, 4182 ); 4183 } 4184 4185 if ( isset( $user_meta['session_tokens'] ) ) { 4186 $session_tokens = maybe_unserialize( $user_meta['session_tokens'][0] ); 4187 4188 $session_tokens_props_to_export = array( 4189 'expiration' => __( 'Expiration' ), 4190 'ip' => __( 'IP' ), 4191 'ua' => __( 'User Agent' ), 4192 'login' => __( 'Last Login' ), 4193 ); 4194 4195 foreach ( $session_tokens as $token_key => $session_token ) { 4196 $session_tokens_data_to_export = array(); 4197 4198 foreach ( $session_tokens_props_to_export as $key => $name ) { 4199 if ( ! empty( $session_token[ $key ] ) ) { 4200 $value = $session_token[ $key ]; 4201 if ( in_array( $key, array( 'expiration', 'login' ), true ) ) { 4202 $value = date_i18n( 'F d, Y H:i A', $value ); 4203 } 4204 $session_tokens_data_to_export[] = array( 4205 'name' => $name, 4206 'value' => $value, 4207 ); 4208 } 4209 } 4210 4211 $data_to_export[] = array( 4212 'group_id' => 'session-tokens', 4213 'group_label' => __( 'Session Tokens' ), 4214 'group_description' => __( 'User’s Session Tokens data.' ), 4215 'item_id' => "session-tokens-{$user->ID}-{$token_key}", 4216 'data' => $session_tokens_data_to_export, 4217 ); 4218 } 4219 } 4220 4221 return array( 4222 'data' => $data_to_export, 4223 'done' => true, 4224 ); 4225 } 4226 4227 /** 4228 * Updates log when privacy request is confirmed. 4229 * 4230 * @since 4.9.6 4231 * @access private 4232 * 4233 * @param int $request_id ID of the request. 4234 */ 4235 function _wp_privacy_account_request_confirmed( $request_id ) { 4236 $request = wp_get_user_request( $request_id ); 4237 4238 if ( ! $request ) { 4239 return; 4240 } 4241 4242 if ( ! in_array( $request->status, array( 'request-pending', 'request-failed' ), true ) ) { 4243 return; 4244 } 4245 4246 update_post_meta( $request_id, '_wp_user_request_confirmed_timestamp', time() ); 4247 wp_update_post( 4248 array( 4249 'ID' => $request_id, 4250 'post_status' => 'request-confirmed', 4251 ) 4252 ); 4253 } 4254 4255 /** 4256 * Notifies the site administrator via email when a request is confirmed. 4257 * 4258 * Without this, the admin would have to manually check the site to see if any 4259 * action was needed on their part yet. 4260 * 4261 * @since 4.9.6 4262 * 4263 * @param int $request_id The ID of the request. 4264 */ 4265 function _wp_privacy_send_request_confirmation_notification( $request_id ) { 4266 $request = wp_get_user_request( $request_id ); 4267 4268 if ( ! ( $request instanceof WP_User_Request ) || 'request-confirmed' !== $request->status ) { 4269 return; 4270 } 4271 4272 $already_notified = (bool) get_post_meta( $request_id, '_wp_admin_notified', true ); 4273 4274 if ( $already_notified ) { 4275 return; 4276 } 4277 4278 if ( 'export_personal_data' === $request->action_name ) { 4279 $manage_url = admin_url( 'export-personal-data.php' ); 4280 } elseif ( 'remove_personal_data' === $request->action_name ) { 4281 $manage_url = admin_url( 'erase-personal-data.php' ); 4282 } 4283 $action_description = wp_user_request_action_description( $request->action_name ); 4284 4285 /** 4286 * Filters the recipient of the data request confirmation notification. 4287 * 4288 * In a Multisite environment, this will default to the email address of the 4289 * network admin because, by default, single site admins do not have the 4290 * capabilities required to process requests. Some networks may wish to 4291 * delegate those capabilities to a single-site admin, or a dedicated person 4292 * responsible for managing privacy requests. 4293 * 4294 * @since 4.9.6 4295 * 4296 * @param string $admin_email The email address of the notification recipient. 4297 * @param WP_User_Request $request The request that is initiating the notification. 4298 */ 4299 $admin_email = apply_filters( 'user_request_confirmed_email_to', get_site_option( 'admin_email' ), $request ); 4300 4301 $email_data = array( 4302 'request' => $request, 4303 'user_email' => $request->email, 4304 'description' => $action_description, 4305 'manage_url' => $manage_url, 4306 'sitename' => wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ), 4307 'siteurl' => home_url(), 4308 'admin_email' => $admin_email, 4309 ); 4310 4311 $subject = sprintf( 4312 /* translators: Privacy data request confirmed notification email subject. 1: Site title, 2: Name of the confirmed action. */ 4313 __( '[%1$s] Action Confirmed: %2$s' ), 4314 $email_data['sitename'], 4315 $action_description 4316 ); 4317 4318 /** 4319 * Filters the subject of the user request confirmation email. 4320 * 4321 * @since 4.9.8 4322 * 4323 * @param string $subject The email subject. 4324 * @param string $sitename The name of the site. 4325 * @param array $email_data { 4326 * Data relating to the account action email. 4327 * 4328 * @type WP_User_Request $request User request object. 4329 * @type string $user_email The email address confirming a request. 4330 * @type string $description Description of the action being performed so the user knows what the email is for. 4331 * @type string $manage_url The link to click manage privacy requests of this type. 4332 * @type string $sitename The site name sending the mail. 4333 * @type string $siteurl The site URL sending the mail. 4334 * @type string $admin_email The administrator email receiving the mail. 4335 * } 4336 */ 4337 $subject = apply_filters( 'user_request_confirmed_email_subject', $subject, $email_data['sitename'], $email_data ); 4338 4339 /* translators: Do not translate SITENAME, USER_EMAIL, DESCRIPTION, MANAGE_URL, SITEURL; those are placeholders. */ 4340 $content = __( 4341 'Howdy, 4342 4343 A user data privacy request has been confirmed on ###SITENAME###: 4344 4345 User: ###USER_EMAIL### 4346 Request: ###DESCRIPTION### 4347 4348 You can view and manage these data privacy requests here: 4349 4350 ###MANAGE_URL### 4351 4352 Regards, 4353 All at ###SITENAME### 4354 ###SITEURL###' 4355 ); 4356 4357 /** 4358 * Filters the body of the user request confirmation email. 4359 * 4360 * The email is sent to an administrator when a user request is confirmed. 4361 * 4362 * The following strings have a special meaning and will get replaced dynamically: 4363 * 4364 * - `###SITENAME###` The name of the site. 4365 * - `###USER_EMAIL###` The user email for the request. 4366 * - `###DESCRIPTION###` Description of the action being performed so the user knows what the email is for. 4367 * - `###MANAGE_URL###` The URL to manage requests. 4368 * - `###SITEURL###` The URL to the site. 4369 * 4370 * @since 4.9.6 4371 * @deprecated 5.8.0 Use {@see 'user_request_confirmed_email_content'} instead. 4372 * For user erasure fulfillment email content 4373 * use {@see 'user_erasure_fulfillment_email_content'} instead. 4374 * 4375 * @param string $content The email content. 4376 * @param array $email_data { 4377 * Data relating to the account action email. 4378 * 4379 * @type WP_User_Request $request User request object. 4380 * @type string $user_email The email address confirming a request. 4381 * @type string $description Description of the action being performed 4382 * so the user knows what the email is for. 4383 * @type string $manage_url The link to click manage privacy requests of this type. 4384 * @type string $sitename The site name sending the mail. 4385 * @type string $siteurl The site URL sending the mail. 4386 * @type string $admin_email The administrator email receiving the mail. 4387 * } 4388 */ 4389 $content = apply_filters_deprecated( 4390 'user_confirmed_action_email_content', 4391 array( $content, $email_data ), 4392 '5.8.0', 4393 sprintf( 4394 /* translators: 1 & 2: Deprecation replacement options. */ 4395 __( '%1$s or %2$s' ), 4396 'user_request_confirmed_email_content', 4397 'user_erasure_fulfillment_email_content' 4398 ) 4399 ); 4400 4401 /** 4402 * Filters the body of the user request confirmation email. 4403 * 4404 * The email is sent to an administrator when a user request is confirmed. 4405 * The following strings have a special meaning and will get replaced dynamically: 4406 * 4407 * - `###SITENAME###` The name of the site. 4408 * - `###USER_EMAIL###` The user email for the request. 4409 * - `###DESCRIPTION###` Description of the action being performed so the user knows what the email is for. 4410 * - `###MANAGE_URL###` The URL to manage requests. 4411 * - `###SITEURL###` The URL to the site. 4412 * 4413 * @since 5.8.0 4414 * 4415 * @param string $content The email content. 4416 * @param array $email_data { 4417 * Data relating to the account action email. 4418 * 4419 * @type WP_User_Request $request User request object. 4420 * @type string $user_email The email address confirming a request. 4421 * @type string $description Description of the action being performed so the user knows what the email is for. 4422 * @type string $manage_url The link to click manage privacy requests of this type. 4423 * @type string $sitename The site name sending the mail. 4424 * @type string $siteurl The site URL sending the mail. 4425 * @type string $admin_email The administrator email receiving the mail. 4426 * } 4427 */ 4428 $content = apply_filters( 'user_request_confirmed_email_content', $content, $email_data ); 4429 4430 $content = str_replace( '###SITENAME###', $email_data['sitename'], $content ); 4431 $content = str_replace( '###USER_EMAIL###', $email_data['user_email'], $content ); 4432 $content = str_replace( '###DESCRIPTION###', $email_data['description'], $content ); 4433 $content = str_replace( '###MANAGE_URL###', sanitize_url( $email_data['manage_url'] ), $content ); 4434 $content = str_replace( '###SITEURL###', sanitize_url( $email_data['siteurl'] ), $content ); 4435 4436 $headers = ''; 4437 4438 /** 4439 * Filters the headers of the user request confirmation email. 4440 * 4441 * @since 5.4.0 4442 * 4443 * @param string|array $headers The email headers. 4444 * @param string $subject The email subject. 4445 * @param string $content The email content. 4446 * @param int $request_id The request ID. 4447 * @param array $email_data { 4448 * Data relating to the account action email. 4449 * 4450 * @type WP_User_Request $request User request object. 4451 * @type string $user_email The email address confirming a request. 4452 * @type string $description Description of the action being performed so the user knows what the email is for. 4453 * @type string $manage_url The link to click manage privacy requests of this type. 4454 * @type string $sitename The site name sending the mail. 4455 * @type string $siteurl The site URL sending the mail. 4456 * @type string $admin_email The administrator email receiving the mail. 4457 * } 4458 */ 4459 $headers = apply_filters( 'user_request_confirmed_email_headers', $headers, $subject, $content, $request_id, $email_data ); 4460 4461 $email_sent = wp_mail( $email_data['admin_email'], $subject, $content, $headers ); 4462 4463 if ( $email_sent ) { 4464 update_post_meta( $request_id, '_wp_admin_notified', true ); 4465 } 4466 } 4467 4468 /** 4469 * Notifies the user when their erasure request is fulfilled. 4470 * 4471 * Without this, the user would never know if their data was actually erased. 4472 * 4473 * @since 4.9.6 4474 * 4475 * @param int $request_id The privacy request post ID associated with this request. 4476 */ 4477 function _wp_privacy_send_erasure_fulfillment_notification( $request_id ) { 4478 $request = wp_get_user_request( $request_id ); 4479 4480 if ( ! ( $request instanceof WP_User_Request ) || 'request-completed' !== $request->status ) { 4481 return; 4482 } 4483 4484 $already_notified = (bool) get_post_meta( $request_id, '_wp_user_notified', true ); 4485 4486 if ( $already_notified ) { 4487 return; 4488 } 4489 4490 // Localize message content for user; fallback to site default for visitors. 4491 if ( ! empty( $request->user_id ) ) { 4492 $switched_locale = switch_to_user_locale( $request->user_id ); 4493 } else { 4494 $switched_locale = switch_to_locale( get_locale() ); 4495 } 4496 4497 /** 4498 * Filters the recipient of the data erasure fulfillment notification. 4499 * 4500 * @since 4.9.6 4501 * 4502 * @param string $user_email The email address of the notification recipient. 4503 * @param WP_User_Request $request The request that is initiating the notification. 4504 */ 4505 $user_email = apply_filters( 'user_erasure_fulfillment_email_to', $request->email, $request ); 4506 4507 $email_data = array( 4508 'request' => $request, 4509 'message_recipient' => $user_email, 4510 'privacy_policy_url' => get_privacy_policy_url(), 4511 'sitename' => wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ), 4512 'siteurl' => home_url(), 4513 ); 4514 4515 $subject = sprintf( 4516 /* translators: Erasure request fulfilled notification email subject. %s: Site title. */ 4517 __( '[%s] Erasure Request Fulfilled' ), 4518 $email_data['sitename'] 4519 ); 4520 4521 /** 4522 * Filters the subject of the email sent when an erasure request is completed. 4523 * 4524 * @since 4.9.8 4525 * @deprecated 5.8.0 Use {@see 'user_erasure_fulfillment_email_subject'} instead. 4526 * 4527 * @param string $subject The email subject. 4528 * @param string $sitename The name of the site. 4529 * @param array $email_data { 4530 * Data relating to the account action email. 4531 * 4532 * @type WP_User_Request $request User request object. 4533 * @type string $message_recipient The address that the email will be sent to. Defaults 4534 * to the value of `$request->email`, but can be changed 4535 * by the `user_erasure_fulfillment_email_to` filter. 4536 * @type string $privacy_policy_url Privacy policy URL. 4537 * @type string $sitename The site name sending the mail. 4538 * @type string $siteurl The site URL sending the mail. 4539 * } 4540 */ 4541 $subject = apply_filters_deprecated( 4542 'user_erasure_complete_email_subject', 4543 array( $subject, $email_data['sitename'], $email_data ), 4544 '5.8.0', 4545 'user_erasure_fulfillment_email_subject' 4546 ); 4547 4548 /** 4549 * Filters the subject of the email sent when an erasure request is completed. 4550 * 4551 * @since 5.8.0 4552 * 4553 * @param string $subject The email subject. 4554 * @param string $sitename The name of the site. 4555 * @param array $email_data { 4556 * Data relating to the account action email. 4557 * 4558 * @type WP_User_Request $request User request object. 4559 * @type string $message_recipient The address that the email will be sent to. Defaults 4560 * to the value of `$request->email`, but can be changed 4561 * by the `user_erasure_fulfillment_email_to` filter. 4562 * @type string $privacy_policy_url Privacy policy URL. 4563 * @type string $sitename The site name sending the mail. 4564 * @type string $siteurl The site URL sending the mail. 4565 * } 4566 */ 4567 $subject = apply_filters( 'user_erasure_fulfillment_email_subject', $subject, $email_data['sitename'], $email_data ); 4568 4569 /* translators: Do not translate SITENAME, SITEURL; those are placeholders. */ 4570 $content = __( 4571 'Howdy, 4572 4573 Your request to erase your personal data on ###SITENAME### has been completed. 4574 4575 If you have any follow-up questions or concerns, please contact the site administrator. 4576 4577 Regards, 4578 All at ###SITENAME### 4579 ###SITEURL###' 4580 ); 4581 4582 if ( ! empty( $email_data['privacy_policy_url'] ) ) { 4583 /* translators: Do not translate SITENAME, SITEURL, PRIVACY_POLICY_URL; those are placeholders. */ 4584 $content = __( 4585 'Howdy, 4586 4587 Your request to erase your personal data on ###SITENAME### has been completed. 4588 4589 If you have any follow-up questions or concerns, please contact the site administrator. 4590 4591 For more information, you can also read our privacy policy: ###PRIVACY_POLICY_URL### 4592 4593 Regards, 4594 All at ###SITENAME### 4595 ###SITEURL###' 4596 ); 4597 } 4598 4599 /** 4600 * Filters the body of the data erasure fulfillment notification. 4601 * 4602 * The email is sent to a user when their data erasure request is fulfilled 4603 * by an administrator. 4604 * 4605 * The following strings have a special meaning and will get replaced dynamically: 4606 * 4607 * - `###SITENAME###` The name of the site. 4608 * - `###PRIVACY_POLICY_URL###` Privacy policy page URL. 4609 * - `###SITEURL###` The URL to the site. 4610 * 4611 * @since 4.9.6 4612 * @deprecated 5.8.0 Use {@see 'user_erasure_fulfillment_email_content'} instead. 4613 * For user request confirmation email content 4614 * use {@see 'user_request_confirmed_email_content'} instead. 4615 * 4616 * @param string $content The email content. 4617 * @param array $email_data { 4618 * Data relating to the account action email. 4619 * 4620 * @type WP_User_Request $request User request object. 4621 * @type string $message_recipient The address that the email will be sent to. Defaults 4622 * to the value of `$request->email`, but can be changed 4623 * by the `user_erasure_fulfillment_email_to` filter. 4624 * @type string $privacy_policy_url Privacy policy URL. 4625 * @type string $sitename The site name sending the mail. 4626 * @type string $siteurl The site URL sending the mail. 4627 * } 4628 */ 4629 $content = apply_filters_deprecated( 4630 'user_confirmed_action_email_content', 4631 array( $content, $email_data ), 4632 '5.8.0', 4633 sprintf( 4634 /* translators: 1 & 2: Deprecation replacement options. */ 4635 __( '%1$s or %2$s' ), 4636 'user_erasure_fulfillment_email_content', 4637 'user_request_confirmed_email_content' 4638 ) 4639 ); 4640 4641 /** 4642 * Filters the body of the data erasure fulfillment notification. 4643 * 4644 * The email is sent to a user when their data erasure request is fulfilled 4645 * by an administrator. 4646 * 4647 * The following strings have a special meaning and will get replaced dynamically: 4648 * 4649 * - `###SITENAME###` The name of the site. 4650 * - `###PRIVACY_POLICY_URL###` Privacy policy page URL. 4651 * - `###SITEURL###` The URL to the site. 4652 * 4653 * @since 5.8.0 4654 * 4655 * @param string $content The email content. 4656 * @param array $email_data { 4657 * Data relating to the account action email. 4658 * 4659 * @type WP_User_Request $request User request object. 4660 * @type string $message_recipient The address that the email will be sent to. Defaults 4661 * to the value of `$request->email`, but can be changed 4662 * by the `user_erasure_fulfillment_email_to` filter. 4663 * @type string $privacy_policy_url Privacy policy URL. 4664 * @type string $sitename The site name sending the mail. 4665 * @type string $siteurl The site URL sending the mail. 4666 * } 4667 */ 4668 $content = apply_filters( 'user_erasure_fulfillment_email_content', $content, $email_data ); 4669 4670 $content = str_replace( '###SITENAME###', $email_data['sitename'], $content ); 4671 $content = str_replace( '###PRIVACY_POLICY_URL###', $email_data['privacy_policy_url'], $content ); 4672 $content = str_replace( '###SITEURL###', sanitize_url( $email_data['siteurl'] ), $content ); 4673 4674 $headers = ''; 4675 4676 /** 4677 * Filters the headers of the data erasure fulfillment notification. 4678 * 4679 * @since 5.4.0 4680 * @deprecated 5.8.0 Use {@see 'user_erasure_fulfillment_email_headers'} instead. 4681 * 4682 * @param string|array $headers The email headers. 4683 * @param string $subject The email subject. 4684 * @param string $content The email content. 4685 * @param int $request_id The request ID. 4686 * @param array $email_data { 4687 * Data relating to the account action email. 4688 * 4689 * @type WP_User_Request $request User request object. 4690 * @type string $message_recipient The address that the email will be sent to. Defaults 4691 * to the value of `$request->email`, but can be changed 4692 * by the `user_erasure_fulfillment_email_to` filter. 4693 * @type string $privacy_policy_url Privacy policy URL. 4694 * @type string $sitename The site name sending the mail. 4695 * @type string $siteurl The site URL sending the mail. 4696 * } 4697 */ 4698 $headers = apply_filters_deprecated( 4699 'user_erasure_complete_email_headers', 4700 array( $headers, $subject, $content, $request_id, $email_data ), 4701 '5.8.0', 4702 'user_erasure_fulfillment_email_headers' 4703 ); 4704 4705 /** 4706 * Filters the headers of the data erasure fulfillment notification. 4707 * 4708 * @since 5.8.0 4709 * 4710 * @param string|array $headers The email headers. 4711 * @param string $subject The email subject. 4712 * @param string $content The email content. 4713 * @param int $request_id The request ID. 4714 * @param array $email_data { 4715 * Data relating to the account action email. 4716 * 4717 * @type WP_User_Request $request User request object. 4718 * @type string $message_recipient The address that the email will be sent to. Defaults 4719 * to the value of `$request->email`, but can be changed 4720 * by the `user_erasure_fulfillment_email_to` filter. 4721 * @type string $privacy_policy_url Privacy policy URL. 4722 * @type string $sitename The site name sending the mail. 4723 * @type string $siteurl The site URL sending the mail. 4724 * } 4725 */ 4726 $headers = apply_filters( 'user_erasure_fulfillment_email_headers', $headers, $subject, $content, $request_id, $email_data ); 4727 4728 $email_sent = wp_mail( $user_email, $subject, $content, $headers ); 4729 4730 if ( $switched_locale ) { 4731 restore_previous_locale(); 4732 } 4733 4734 if ( $email_sent ) { 4735 update_post_meta( $request_id, '_wp_user_notified', true ); 4736 } 4737 } 4738 4739 /** 4740 * Returns request confirmation message HTML. 4741 * 4742 * @since 4.9.6 4743 * @access private 4744 * 4745 * @param int $request_id The request ID being confirmed. 4746 * @return string The confirmation message. 4747 */ 4748 function _wp_privacy_account_request_confirmed_message( $request_id ) { 4749 $request = wp_get_user_request( $request_id ); 4750 4751 $message = '<p class="success">' . __( 'Action has been confirmed.' ) . '</p>'; 4752 $message .= '<p>' . __( 'The site administrator has been notified and will fulfill your request as soon as possible.' ) . '</p>'; 4753 4754 if ( $request && in_array( $request->action_name, _wp_privacy_action_request_types(), true ) ) { 4755 if ( 'export_personal_data' === $request->action_name ) { 4756 $message = '<p class="success">' . __( 'Thanks for confirming your export request.' ) . '</p>'; 4757 $message .= '<p>' . __( 'The site administrator has been notified. You will receive a link to download your export via email when they fulfill your request.' ) . '</p>'; 4758 } elseif ( 'remove_personal_data' === $request->action_name ) { 4759 $message = '<p class="success">' . __( 'Thanks for confirming your erasure request.' ) . '</p>'; 4760 $message .= '<p>' . __( 'The site administrator has been notified. You will receive an email confirmation when they erase your data.' ) . '</p>'; 4761 } 4762 } 4763 4764 /** 4765 * Filters the message displayed to a user when they confirm a data request. 4766 * 4767 * @since 4.9.6 4768 * 4769 * @param string $message The message to the user. 4770 * @param int $request_id The ID of the request being confirmed. 4771 */ 4772 $message = apply_filters( 'user_request_action_confirmed_message', $message, $request_id ); 4773 4774 return $message; 4775 } 4776 4777 /** 4778 * Creates and logs a user request to perform a specific action. 4779 * 4780 * Requests are stored inside a post type named `user_request` since they can apply to both 4781 * users on the site, or guests without a user account. 4782 * 4783 * @since 4.9.6 4784 * @since 5.7.0 Added the `$status` parameter. 4785 * 4786 * @param string $email_address User email address. This can be the address of a registered 4787 * or non-registered user. 4788 * @param string $action_name Name of the action that is being confirmed. Required. 4789 * @param array $request_data Misc data you want to send with the verification request and pass 4790 * to the actions once the request is confirmed. 4791 * @param string $status Optional request status (pending or confirmed). Default 'pending'. 4792 * @return int|WP_Error Returns the request ID if successful, or a WP_Error object on failure. 4793 */ 4794 function wp_create_user_request( $email_address = '', $action_name = '', $request_data = array(), $status = 'pending' ) { 4795 $email_address = sanitize_email( $email_address ); 4796 $action_name = sanitize_key( $action_name ); 4797 4798 if ( ! is_email( $email_address ) ) { 4799 return new WP_Error( 'invalid_email', __( 'Invalid email address.' ) ); 4800 } 4801 4802 if ( ! in_array( $action_name, _wp_privacy_action_request_types(), true ) ) { 4803 return new WP_Error( 'invalid_action', __( 'Invalid action name.' ) ); 4804 } 4805 4806 if ( ! in_array( $status, array( 'pending', 'confirmed' ), true ) ) { 4807 return new WP_Error( 'invalid_status', __( 'Invalid request status.' ) ); 4808 } 4809 4810 $user = get_user_by( 'email', $email_address ); 4811 $user_id = $user && ! is_wp_error( $user ) ? $user->ID : 0; 4812 4813 // Check for duplicates. 4814 $requests_query = new WP_Query( 4815 array( 4816 'post_type' => 'user_request', 4817 'post_name__in' => array( $action_name ), // Action name stored in post_name column. 4818 'title' => $email_address, // Email address stored in post_title column. 4819 'post_status' => array( 4820 'request-pending', 4821 'request-confirmed', 4822 ), 4823 'fields' => 'ids', 4824 ) 4825 ); 4826 4827 if ( $requests_query->found_posts ) { 4828 return new WP_Error( 'duplicate_request', __( 'An incomplete personal data request for this email address already exists.' ) ); 4829 } 4830 4831 $request_id = wp_insert_post( 4832 array( 4833 'post_author' => $user_id, 4834 'post_name' => $action_name, 4835 'post_title' => $email_address, 4836 'post_content' => wp_json_encode( $request_data ), 4837 'post_status' => 'request-' . $status, 4838 'post_type' => 'user_request', 4839 'post_date' => current_time( 'mysql', false ), 4840 'post_date_gmt' => current_time( 'mysql', true ), 4841 ), 4842 true 4843 ); 4844 4845 return $request_id; 4846 } 4847 4848 /** 4849 * Gets action description from the name and return a string. 4850 * 4851 * @since 4.9.6 4852 * 4853 * @param string $action_name Action name of the request. 4854 * @return string Human readable action name. 4855 */ 4856 function wp_user_request_action_description( $action_name ) { 4857 switch ( $action_name ) { 4858 case 'export_personal_data': 4859 $description = __( 'Export Personal Data' ); 4860 break; 4861 case 'remove_personal_data': 4862 $description = __( 'Erase Personal Data' ); 4863 break; 4864 default: 4865 /* translators: %s: Action name. */ 4866 $description = sprintf( __( 'Confirm the "%s" action' ), $action_name ); 4867 break; 4868 } 4869 4870 /** 4871 * Filters the user action description. 4872 * 4873 * @since 4.9.6 4874 * 4875 * @param string $description The default description. 4876 * @param string $action_name The name of the request. 4877 */ 4878 return apply_filters( 'user_request_action_description', $description, $action_name ); 4879 } 4880 4881 /** 4882 * Send a confirmation request email to confirm an action. 4883 * 4884 * If the request is not already pending, it will be updated. 4885 * 4886 * @since 4.9.6 4887 * 4888 * @param int $request_id ID of the request created via wp_create_user_request(). 4889 * @return true|WP_Error True on success, `WP_Error` on failure. 4890 */ 4891 function wp_send_user_request( $request_id ) { 4892 $request_id = absint( $request_id ); 4893 $request = wp_get_user_request( $request_id ); 4894 4895 if ( ! $request ) { 4896 return new WP_Error( 'invalid_request', __( 'Invalid personal data request.' ) ); 4897 } 4898 4899 // Localize message content for user; fallback to site default for visitors. 4900 if ( ! empty( $request->user_id ) ) { 4901 $switched_locale = switch_to_user_locale( $request->user_id ); 4902 } else { 4903 $switched_locale = switch_to_locale( get_locale() ); 4904 } 4905 4906 /* 4907 * Generate the new user request key first, as it is used by both the $request 4908 * object and the confirm_url array. 4909 * See https://core.trac.wordpress.org/ticket/44940 4910 */ 4911 $request->confirm_key = wp_generate_user_request_key( $request_id ); 4912 4913 $email_data = array( 4914 'request' => $request, 4915 'email' => $request->email, 4916 'description' => wp_user_request_action_description( $request->action_name ), 4917 'confirm_url' => add_query_arg( 4918 array( 4919 'action' => 'confirmaction', 4920 'request_id' => $request_id, 4921 'confirm_key' => $request->confirm_key, 4922 ), 4923 wp_login_url() 4924 ), 4925 'sitename' => wp_specialchars_decode( get_option( 'blogname' ), ENT_QUOTES ), 4926 'siteurl' => home_url(), 4927 ); 4928 4929 /* translators: Confirm privacy data request notification email subject. 1: Site title, 2: Name of the action. */ 4930 $subject = sprintf( __( '[%1$s] Confirm Action: %2$s' ), $email_data['sitename'], $email_data['description'] ); 4931 4932 /** 4933 * Filters the subject of the email sent when an account action is attempted. 4934 * 4935 * @since 4.9.6 4936 * 4937 * @param string $subject The email subject. 4938 * @param string $sitename The name of the site. 4939 * @param array $email_data { 4940 * Data relating to the account action email. 4941 * 4942 * @type WP_User_Request $request User request object. 4943 * @type string $email The email address this is being sent to. 4944 * @type string $description Description of the action being performed so the user knows what the email is for. 4945 * @type string $confirm_url The link to click on to confirm the account action. 4946 * @type string $sitename The site name sending the mail. 4947 * @type string $siteurl The site URL sending the mail. 4948 * } 4949 */ 4950 $subject = apply_filters( 'user_request_action_email_subject', $subject, $email_data['sitename'], $email_data ); 4951 4952 /* translators: Do not translate DESCRIPTION, CONFIRM_URL, SITENAME, SITEURL: those are placeholders. */ 4953 $content = __( 4954 'Howdy, 4955 4956 A request has been made to perform the following action on your account: 4957 4958 ###DESCRIPTION### 4959 4960 To confirm this, please click on the following link: 4961 ###CONFIRM_URL### 4962 4963 You can safely ignore and delete this email if you do not want to 4964 take this action. 4965 4966 Regards, 4967 All at ###SITENAME### 4968 ###SITEURL###' 4969 ); 4970 4971 /** 4972 * Filters the text of the email sent when an account action is attempted. 4973 * 4974 * The following strings have a special meaning and will get replaced dynamically: 4975 * 4976 * - `###DESCRIPTION###` Description of the action being performed so the user knows what the email is for. 4977 * - `###CONFIRM_URL###` The link to click on to confirm the account action. 4978 * - `###SITENAME###` The name of the site. 4979 * - `###SITEURL###` The URL to the site. 4980 * 4981 * @since 4.9.6 4982 * 4983 * @param string $content Text in the email. 4984 * @param array $email_data { 4985 * Data relating to the account action email. 4986 * 4987 * @type WP_User_Request $request User request object. 4988 * @type string $email The email address this is being sent to. 4989 * @type string $description Description of the action being performed so the user knows what the email is for. 4990 * @type string $confirm_url The link to click on to confirm the account action. 4991 * @type string $sitename The site name sending the mail. 4992 * @type string $siteurl The site URL sending the mail. 4993 * } 4994 */ 4995 $content = apply_filters( 'user_request_action_email_content', $content, $email_data ); 4996 4997 $content = str_replace( '###DESCRIPTION###', $email_data['description'], $content ); 4998 $content = str_replace( '###CONFIRM_URL###', sanitize_url( $email_data['confirm_url'] ), $content ); 4999 $content = str_replace( '###EMAIL###', $email_data['email'], $content ); 5000 $content = str_replace( '###SITENAME###', $email_data['sitename'], $content ); 5001 $content = str_replace( '###SITEURL###', sanitize_url( $email_data['siteurl'] ), $content ); 5002 5003 $headers = ''; 5004 5005 /** 5006 * Filters the headers of the email sent when an account action is attempted. 5007 * 5008 * @since 5.4.0 5009 * 5010 * @param string|array $headers The email headers. 5011 * @param string $subject The email subject. 5012 * @param string $content The email content. 5013 * @param int $request_id The request ID. 5014 * @param array $email_data { 5015 * Data relating to the account action email. 5016 * 5017 * @type WP_User_Request $request User request object. 5018 * @type string $email The email address this is being sent to. 5019 * @type string $description Description of the action being performed so the user knows what the email is for. 5020 * @type string $confirm_url The link to click on to confirm the account action. 5021 * @type string $sitename The site name sending the mail. 5022 * @type string $siteurl The site URL sending the mail. 5023 * } 5024 */ 5025 $headers = apply_filters( 'user_request_action_email_headers', $headers, $subject, $content, $request_id, $email_data ); 5026 5027 $email_sent = wp_mail( $email_data['email'], $subject, $content, $headers ); 5028 5029 if ( $switched_locale ) { 5030 restore_previous_locale(); 5031 } 5032 5033 if ( ! $email_sent ) { 5034 return new WP_Error( 'privacy_email_error', __( 'Unable to send personal data export confirmation email.' ) ); 5035 } 5036 5037 return true; 5038 } 5039 5040 /** 5041 * Returns a confirmation key for a user action and stores the hashed version for future comparison. 5042 * 5043 * @since 4.9.6 5044 * 5045 * @param int $request_id Request ID. 5046 * @return string Confirmation key. 5047 */ 5048 function wp_generate_user_request_key( $request_id ) { 5049 // Generate something random for a confirmation key. 5050 $key = wp_generate_password( 20, false ); 5051 5052 // Save the key, hashed. 5053 wp_update_post( 5054 array( 5055 'ID' => $request_id, 5056 'post_status' => 'request-pending', 5057 'post_password' => wp_fast_hash( $key ), 5058 ) 5059 ); 5060 5061 return $key; 5062 } 5063 5064 /** 5065 * Validates a user request by comparing the key with the request's key. 5066 * 5067 * @since 4.9.6 5068 * 5069 * @param int $request_id ID of the request being confirmed. 5070 * @param string $key Provided key to validate. 5071 * @return true|WP_Error True on success, WP_Error on failure. 5072 */ 5073 function wp_validate_user_request_key( 5074 $request_id, 5075 #[\SensitiveParameter] 5076 $key 5077 ) { 5078 $request_id = absint( $request_id ); 5079 $request = wp_get_user_request( $request_id ); 5080 $saved_key = $request->confirm_key; 5081 $key_request_time = $request->modified_timestamp; 5082 5083 if ( ! $request || ! $saved_key || ! $key_request_time ) { 5084 return new WP_Error( 'invalid_request', __( 'Invalid personal data request.' ) ); 5085 } 5086 5087 if ( ! in_array( $request->status, array( 'request-pending', 'request-failed' ), true ) ) { 5088 return new WP_Error( 'expired_request', __( 'This personal data request has expired.' ) ); 5089 } 5090 5091 if ( empty( $key ) ) { 5092 return new WP_Error( 'missing_key', __( 'The confirmation key is missing from this personal data request.' ) ); 5093 } 5094 5095 /** 5096 * Filters the expiration time of confirm keys. 5097 * 5098 * @since 4.9.6 5099 * 5100 * @param int $expiration The expiration time in seconds. 5101 */ 5102 $expiration_duration = (int) apply_filters( 'user_request_key_expiration', DAY_IN_SECONDS ); 5103 $expiration_time = $key_request_time + $expiration_duration; 5104 5105 if ( ! wp_verify_fast_hash( $key, $saved_key ) ) { 5106 return new WP_Error( 'invalid_key', __( 'The confirmation key is invalid for this personal data request.' ) ); 5107 } 5108 5109 if ( ! $expiration_time || time() > $expiration_time ) { 5110 return new WP_Error( 'expired_key', __( 'The confirmation key has expired for this personal data request.' ) ); 5111 } 5112 5113 return true; 5114 } 5115 5116 /** 5117 * Returns the user request object for the specified request ID. 5118 * 5119 * @since 4.9.6 5120 * 5121 * @param int $request_id The ID of the user request. 5122 * @return WP_User_Request|false 5123 */ 5124 function wp_get_user_request( $request_id ) { 5125 $request_id = absint( $request_id ); 5126 $post = get_post( $request_id ); 5127 5128 if ( ! $post || 'user_request' !== $post->post_type ) { 5129 return false; 5130 } 5131 5132 return new WP_User_Request( $post ); 5133 } 5134 5135 /** 5136 * Checks if Application Passwords is supported. 5137 * 5138 * Application Passwords is supported only by sites using SSL or local environments 5139 * but may be made available using the {@see 'wp_is_application_passwords_available'} filter. 5140 * 5141 * @since 5.9.0 5142 * 5143 * @return bool 5144 */ 5145 function wp_is_application_passwords_supported() { 5146 return is_ssl() || 'local' === wp_get_environment_type(); 5147 } 5148 5149 /** 5150 * Checks if Application Passwords is globally available. 5151 * 5152 * By default, Application Passwords is available to all sites using SSL or to local environments. 5153 * Use the {@see 'wp_is_application_passwords_available'} filter to adjust its availability. 5154 * 5155 * @since 5.6.0 5156 * 5157 * @return bool 5158 */ 5159 function wp_is_application_passwords_available() { 5160 /** 5161 * Filters whether Application Passwords is available. 5162 * 5163 * @since 5.6.0 5164 * 5165 * @param bool $available True if available, false otherwise. 5166 */ 5167 return apply_filters( 'wp_is_application_passwords_available', wp_is_application_passwords_supported() ); 5168 } 5169 5170 /** 5171 * Checks if Application Passwords is available for a specific user. 5172 * 5173 * By default all users can use Application Passwords. Use {@see 'wp_is_application_passwords_available_for_user'} 5174 * to restrict availability to certain users. 5175 * 5176 * @since 5.6.0 5177 * 5178 * @param int|WP_User $user The user to check. 5179 * @return bool 5180 */ 5181 function wp_is_application_passwords_available_for_user( $user ) { 5182 if ( ! wp_is_application_passwords_available() ) { 5183 return false; 5184 } 5185 5186 if ( ! is_object( $user ) ) { 5187 $user = get_userdata( $user ); 5188 } 5189 5190 if ( ! $user || ! $user->exists() ) { 5191 return false; 5192 } 5193 5194 /** 5195 * Filters whether Application Passwords is available for a specific user. 5196 * 5197 * @since 5.6.0 5198 * 5199 * @param bool $available True if available, false otherwise. 5200 * @param WP_User $user The user to check. 5201 */ 5202 return apply_filters( 'wp_is_application_passwords_available_for_user', true, $user ); 5203 } 5204 5205 /** 5206 * Registers the user meta property for persisted preferences. 5207 * 5208 * This property is used to store user preferences across page reloads and is 5209 * currently used by the block editor for preferences like 'fullscreenMode' and 5210 * 'fixedToolbar'. 5211 * 5212 * @since 6.1.0 5213 * @access private 5214 * 5215 * @global wpdb $wpdb WordPress database abstraction object. 5216 */ 5217 function wp_register_persisted_preferences_meta() { 5218 /* 5219 * Create a meta key that incorporates the blog prefix so that each site 5220 * on a multisite can have distinct user preferences. 5221 */ 5222 global $wpdb; 5223 $meta_key = $wpdb->get_blog_prefix() . 'persisted_preferences'; 5224 5225 register_meta( 5226 'user', 5227 $meta_key, 5228 array( 5229 'type' => 'object', 5230 'single' => true, 5231 'show_in_rest' => array( 5232 'name' => 'persisted_preferences', 5233 'type' => 'object', 5234 'schema' => array( 5235 'type' => 'object', 5236 'context' => array( 'edit' ), 5237 'properties' => array( 5238 '_modified' => array( 5239 'description' => __( 'The date and time the preferences were updated.' ), 5240 'type' => 'string', 5241 'format' => 'date-time', 5242 'readonly' => false, 5243 ), 5244 ), 5245 'additionalProperties' => true, 5246 ), 5247 ), 5248 ) 5249 ); 5250 } 5251 5252 /** 5253 * Sets the last changed time for the 'users' cache group. 5254 * 5255 * @since 6.3.0 5256 */ 5257 function wp_cache_set_users_last_changed() { 5258 wp_cache_set_last_changed( 'users' ); 5259 } 5260 5261 /** 5262 * Checks if password reset is allowed for a specific user. 5263 * 5264 * @since 6.3.0 5265 * 5266 * @param int|WP_User $user The user to check. 5267 * @return bool|WP_Error True if allowed, false or WP_Error otherwise. 5268 */ 5269 function wp_is_password_reset_allowed_for_user( $user ) { 5270 if ( ! is_object( $user ) ) { 5271 $user = get_userdata( $user ); 5272 } 5273 5274 if ( ! $user || ! $user->exists() ) { 5275 return false; 5276 } 5277 $allow = true; 5278 if ( is_multisite() && is_user_spammy( $user ) ) { 5279 $allow = false; 5280 } 5281 5282 /** 5283 * Filters whether to allow a password to be reset. 5284 * 5285 * @since 2.7.0 5286 * 5287 * @param bool $allow Whether to allow the password to be reset. Default true. 5288 * @param int $user_id The ID of the user attempting to reset a password. 5289 */ 5290 return apply_filters( 'allow_password_reset', $allow, $user->ID ); 5291 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Sat Jul 4 08:20:12 2026 | Cross-referenced by PHPXref |