[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/ -> user.php (source)

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


Generated : Thu Aug 14 08:20:01 2025 Cross-referenced by PHPXref