[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

title

Body

[close]

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

   1  <?php
   2  /**
   3   * WordPress user administration API.
   4   *
   5   * @package WordPress
   6   * @subpackage Administration
   7   */
   8  
   9  /**
  10   * Creates a new user from the "Users" form using $_POST information.
  11   *
  12   * @since 2.0.0
  13   *
  14   * @return int|WP_Error WP_Error or User ID.
  15   */
  16  function add_user() {
  17      return edit_user();
  18  }
  19  
  20  /**
  21   * Edit user settings based on contents of $_POST
  22   *
  23   * Used on user-edit.php and profile.php to manage and process user options, passwords etc.
  24   *
  25   * @since 2.0.0
  26   *
  27   * @param int $user_id Optional. User ID.
  28   * @return int|WP_Error user id of the updated user.
  29   */
  30  function edit_user( $user_id = 0 ) {
  31      $wp_roles = wp_roles();
  32      $user     = new stdClass;
  33      $user_id  = (int) $user_id;
  34      if ( $user_id ) {
  35          $update           = true;
  36          $user->ID         = $user_id;
  37          $userdata         = get_userdata( $user_id );
  38          $user->user_login = wp_slash( $userdata->user_login );
  39      } else {
  40          $update = false;
  41      }
  42  
  43      if ( ! $update && isset( $_POST['user_login'] ) ) {
  44          $user->user_login = sanitize_user( $_POST['user_login'], true );
  45      }
  46  
  47      $pass1 = '';
  48      $pass2 = '';
  49      if ( isset( $_POST['pass1'] ) ) {
  50          $pass1 = $_POST['pass1'];
  51      }
  52      if ( isset( $_POST['pass2'] ) ) {
  53          $pass2 = $_POST['pass2'];
  54      }
  55  
  56      if ( isset( $_POST['role'] ) && current_user_can( 'promote_users' ) && ( ! $user_id || current_user_can( 'promote_user', $user_id ) ) ) {
  57          $new_role = sanitize_text_field( $_POST['role'] );
  58  
  59          // If the new role isn't editable by the logged-in user die with error.
  60          $editable_roles = get_editable_roles();
  61          if ( ! empty( $new_role ) && empty( $editable_roles[ $new_role ] ) ) {
  62              wp_die( __( 'Sorry, you are not allowed to give users that role.' ), 403 );
  63          }
  64  
  65          $potential_role = isset( $wp_roles->role_objects[ $new_role ] ) ? $wp_roles->role_objects[ $new_role ] : false;
  66  
  67          /*
  68           * Don't let anyone with 'promote_users' edit their own role to something without it.
  69           * Multisite super admins can freely edit their roles, they possess all caps.
  70           */
  71          if (
  72              ( is_multisite() && current_user_can( 'manage_network_users' ) ) ||
  73              $user_id !== get_current_user_id() ||
  74              ( $potential_role && $potential_role->has_cap( 'promote_users' ) )
  75          ) {
  76              $user->role = $new_role;
  77          }
  78      }
  79  
  80      if ( isset( $_POST['email'] ) ) {
  81          $user->user_email = sanitize_text_field( wp_unslash( $_POST['email'] ) );
  82      }
  83      if ( isset( $_POST['url'] ) ) {
  84          if ( empty( $_POST['url'] ) || $_POST['url'] == 'http://' ) {
  85              $user->user_url = '';
  86          } else {
  87              $user->user_url = esc_url_raw( $_POST['url'] );
  88              $protocols      = implode( '|', array_map( 'preg_quote', wp_allowed_protocols() ) );
  89              $user->user_url = preg_match( '/^(' . $protocols . '):/is', $user->user_url ) ? $user->user_url : 'http://' . $user->user_url;
  90          }
  91      }
  92      if ( isset( $_POST['first_name'] ) ) {
  93          $user->first_name = sanitize_text_field( $_POST['first_name'] );
  94      }
  95      if ( isset( $_POST['last_name'] ) ) {
  96          $user->last_name = sanitize_text_field( $_POST['last_name'] );
  97      }
  98      if ( isset( $_POST['nickname'] ) ) {
  99          $user->nickname = sanitize_text_field( $_POST['nickname'] );
 100      }
 101      if ( isset( $_POST['display_name'] ) ) {
 102          $user->display_name = sanitize_text_field( $_POST['display_name'] );
 103      }
 104  
 105      if ( isset( $_POST['description'] ) ) {
 106          $user->description = trim( $_POST['description'] );
 107      }
 108  
 109      foreach ( wp_get_user_contact_methods( $user ) as $method => $name ) {
 110          if ( isset( $_POST[ $method ] ) ) {
 111              $user->$method = sanitize_text_field( $_POST[ $method ] );
 112          }
 113      }
 114  
 115      if ( $update ) {
 116          $user->rich_editing         = isset( $_POST['rich_editing'] ) && 'false' === $_POST['rich_editing'] ? 'false' : 'true';
 117          $user->syntax_highlighting  = isset( $_POST['syntax_highlighting'] ) && 'false' === $_POST['syntax_highlighting'] ? 'false' : 'true';
 118          $user->admin_color          = isset( $_POST['admin_color'] ) ? sanitize_text_field( $_POST['admin_color'] ) : 'fresh';
 119          $user->show_admin_bar_front = isset( $_POST['admin_bar_front'] ) ? 'true' : 'false';
 120          $user->locale               = '';
 121  
 122          if ( isset( $_POST['locale'] ) ) {
 123              $locale = sanitize_text_field( $_POST['locale'] );
 124              if ( 'site-default' === $locale ) {
 125                  $locale = '';
 126              } elseif ( '' === $locale ) {
 127                  $locale = 'en_US';
 128              } elseif ( ! in_array( $locale, get_available_languages(), true ) ) {
 129                  $locale = '';
 130              }
 131  
 132              $user->locale = $locale;
 133          }
 134      }
 135  
 136      $user->comment_shortcuts = isset( $_POST['comment_shortcuts'] ) && 'true' == $_POST['comment_shortcuts'] ? 'true' : '';
 137  
 138      $user->use_ssl = 0;
 139      if ( ! empty( $_POST['use_ssl'] ) ) {
 140          $user->use_ssl = 1;
 141      }
 142  
 143      $errors = new WP_Error();
 144  
 145      /* checking that username has been typed */
 146      if ( $user->user_login == '' ) {
 147          $errors->add( 'user_login', __( '<strong>ERROR</strong>: Please enter a username.' ) );
 148      }
 149  
 150      /* checking that nickname has been typed */
 151      if ( $update && empty( $user->nickname ) ) {
 152          $errors->add( 'nickname', __( '<strong>ERROR</strong>: Please enter a nickname.' ) );
 153      }
 154  
 155      /**
 156       * Fires before the password and confirm password fields are checked for congruity.
 157       *
 158       * @since 1.5.1
 159       *
 160       * @param string $user_login The username.
 161       * @param string $pass1     The password (passed by reference).
 162       * @param string $pass2     The confirmed password (passed by reference).
 163       */
 164      do_action_ref_array( 'check_passwords', array( $user->user_login, &$pass1, &$pass2 ) );
 165  
 166      // Check for blank password when adding a user.
 167      if ( ! $update && empty( $pass1 ) ) {
 168          $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter a password.' ), array( 'form-field' => 'pass1' ) );
 169      }
 170  
 171      // Check for "\" in password.
 172      if ( false !== strpos( wp_unslash( $pass1 ), '\\' ) ) {
 173          $errors->add( 'pass', __( '<strong>ERROR</strong>: Passwords may not contain the character "\\".' ), array( 'form-field' => 'pass1' ) );
 174      }
 175  
 176      // Checking the password has been typed twice the same.
 177      if ( ( $update || ! empty( $pass1 ) ) && $pass1 != $pass2 ) {
 178          $errors->add( 'pass', __( '<strong>ERROR</strong>: Please enter the same password in both password fields.' ), array( 'form-field' => 'pass1' ) );
 179      }
 180  
 181      if ( ! empty( $pass1 ) ) {
 182          $user->user_pass = $pass1;
 183      }
 184  
 185      if ( ! $update && isset( $_POST['user_login'] ) && ! validate_username( $_POST['user_login'] ) ) {
 186          $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is invalid because it uses illegal characters. Please enter a valid username.' ) );
 187      }
 188  
 189      if ( ! $update && username_exists( $user->user_login ) ) {
 190          $errors->add( 'user_login', __( '<strong>ERROR</strong>: This username is already registered. Please choose another one.' ) );
 191      }
 192  
 193      /** This filter is documented in wp-includes/user.php */
 194      $illegal_logins = (array) apply_filters( 'illegal_user_logins', array() );
 195  
 196      if ( in_array( strtolower( $user->user_login ), array_map( 'strtolower', $illegal_logins ) ) ) {
 197          $errors->add( 'invalid_username', __( '<strong>ERROR</strong>: Sorry, that username is not allowed.' ) );
 198      }
 199  
 200      /* checking email address */
 201      if ( empty( $user->user_email ) ) {
 202          $errors->add( 'empty_email', __( '<strong>ERROR</strong>: Please enter an email address.' ), array( 'form-field' => 'email' ) );
 203      } elseif ( ! is_email( $user->user_email ) ) {
 204          $errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The email address isn&#8217;t correct.' ), array( 'form-field' => 'email' ) );
 205      } else {
 206          $owner_id = email_exists( $user->user_email );
 207          if ( $owner_id && ( ! $update || ( $owner_id != $user->ID ) ) ) {
 208              $errors->add( 'email_exists', __( '<strong>ERROR</strong>: This email is already registered, please choose another one.' ), array( 'form-field' => 'email' ) );
 209          }
 210      }
 211  
 212      /**
 213       * Fires before user profile update errors are returned.
 214       *
 215       * @since 2.8.0
 216       *
 217       * @param WP_Error $errors WP_Error object (passed by reference).
 218       * @param bool     $update  Whether this is a user update.
 219       * @param stdClass $user   User object (passed by reference).
 220       */
 221      do_action_ref_array( 'user_profile_update_errors', array( &$errors, $update, &$user ) );
 222  
 223      if ( $errors->has_errors() ) {
 224          return $errors;
 225      }
 226  
 227      if ( $update ) {
 228          $user_id = wp_update_user( $user );
 229      } else {
 230          $user_id = wp_insert_user( $user );
 231          $notify  = isset( $_POST['send_user_notification'] ) ? 'both' : 'admin';
 232  
 233          /**
 234           * Fires after a new user has been created.
 235           *
 236           * @since 4.4.0
 237           *
 238           * @param int    $user_id ID of the newly created user.
 239           * @param string $notify  Type of notification that should happen. See wp_send_new_user_notifications()
 240           *                        for more information on possible values.
 241           */
 242          do_action( 'edit_user_created_user', $user_id, $notify );
 243      }
 244      return $user_id;
 245  }
 246  
 247  /**
 248   * Fetch a filtered list of user roles that the current user is
 249   * allowed to edit.
 250   *
 251   * Simple function whose main purpose is to allow filtering of the
 252   * list of roles in the $wp_roles object so that plugins can remove
 253   * inappropriate ones depending on the situation or user making edits.
 254   * Specifically because without filtering anyone with the edit_users
 255   * capability can edit others to be administrators, even if they are
 256   * only editors or authors. This filter allows admins to delegate
 257   * user management.
 258   *
 259   * @since 2.8.0
 260   *
 261   * @return array[] Array of arrays containing role information.
 262   */
 263  function get_editable_roles() {
 264      $all_roles = wp_roles()->roles;
 265  
 266      /**
 267       * Filters the list of editable roles.
 268       *
 269       * @since 2.8.0
 270       *
 271       * @param array[] $all_roles Array of arrays containing role information.
 272       */
 273      $editable_roles = apply_filters( 'editable_roles', $all_roles );
 274  
 275      return $editable_roles;
 276  }
 277  
 278  /**
 279   * Retrieve user data and filter it.
 280   *
 281   * @since 2.0.5
 282   *
 283   * @param int $user_id User ID.
 284   * @return WP_User|bool WP_User object on success, false on failure.
 285   */
 286  function get_user_to_edit( $user_id ) {
 287      $user = get_userdata( $user_id );
 288  
 289      if ( $user ) {
 290          $user->filter = 'edit';
 291      }
 292  
 293      return $user;
 294  }
 295  
 296  /**
 297   * Retrieve the user's drafts.
 298   *
 299   * @since 2.0.0
 300   *
 301   * @global wpdb $wpdb WordPress database abstraction object.
 302   *
 303   * @param int $user_id User ID.
 304   * @return array
 305   */
 306  function get_users_drafts( $user_id ) {
 307      global $wpdb;
 308      $query = $wpdb->prepare( "SELECT ID, post_title FROM $wpdb->posts WHERE post_type = 'post' AND post_status = 'draft' AND post_author = %d ORDER BY post_modified DESC", $user_id );
 309  
 310      /**
 311       * Filters the user's drafts query string.
 312       *
 313       * @since 2.0.0
 314       *
 315       * @param string $query The user's drafts query string.
 316       */
 317      $query = apply_filters( 'get_users_drafts', $query );
 318      return $wpdb->get_results( $query );
 319  }
 320  
 321  /**
 322   * Remove user and optionally reassign posts and links to another user.
 323   *
 324   * If the $reassign parameter is not assigned to a User ID, then all posts will
 325   * be deleted of that user. The action {@see 'delete_user'} that is passed the User ID
 326   * being deleted will be run after the posts are either reassigned or deleted.
 327   * The user meta will also be deleted that are for that User ID.
 328   *
 329   * @since 2.0.0
 330   *
 331   * @global wpdb $wpdb WordPress database abstraction object.
 332   *
 333   * @param int $id User ID.
 334   * @param int $reassign Optional. Reassign posts and links to new User ID.
 335   * @return bool True when finished.
 336   */
 337  function wp_delete_user( $id, $reassign = null ) {
 338      global $wpdb;
 339  
 340      if ( ! is_numeric( $id ) ) {
 341          return false;
 342      }
 343  
 344      $id   = (int) $id;
 345      $user = new WP_User( $id );
 346  
 347      if ( ! $user->exists() ) {
 348          return false;
 349      }
 350  
 351      // Normalize $reassign to null or a user ID. 'novalue' was an older default.
 352      if ( 'novalue' === $reassign ) {
 353          $reassign = null;
 354      } elseif ( null !== $reassign ) {
 355          $reassign = (int) $reassign;
 356      }
 357  
 358      /**
 359       * Fires immediately before a user is deleted from the database.
 360       *
 361       * @since 2.0.0
 362       *
 363       * @param int      $id       ID of the user to delete.
 364       * @param int|null $reassign ID of the user to reassign posts and links to.
 365       *                           Default null, for no reassignment.
 366       */
 367      do_action( 'delete_user', $id, $reassign );
 368  
 369      if ( null === $reassign ) {
 370          $post_types_to_delete = array();
 371          foreach ( get_post_types( array(), 'objects' ) as $post_type ) {
 372              if ( $post_type->delete_with_user ) {
 373                  $post_types_to_delete[] = $post_type->name;
 374              } elseif ( null === $post_type->delete_with_user && post_type_supports( $post_type->name, 'author' ) ) {
 375                  $post_types_to_delete[] = $post_type->name;
 376              }
 377          }
 378  
 379          /**
 380           * Filters the list of post types to delete with a user.
 381           *
 382           * @since 3.4.0
 383           *
 384           * @param string[] $post_types_to_delete Array of post types to delete.
 385           * @param int      $id                   User ID.
 386           */
 387          $post_types_to_delete = apply_filters( 'post_types_to_delete_with_user', $post_types_to_delete, $id );
 388          $post_types_to_delete = implode( "', '", $post_types_to_delete );
 389          $post_ids             = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d AND post_type IN ('$post_types_to_delete')", $id ) );
 390          if ( $post_ids ) {
 391              foreach ( $post_ids as $post_id ) {
 392                  wp_delete_post( $post_id );
 393              }
 394          }
 395  
 396          // Clean links
 397          $link_ids = $wpdb->get_col( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id ) );
 398  
 399          if ( $link_ids ) {
 400              foreach ( $link_ids as $link_id ) {
 401                  wp_delete_link( $link_id );
 402              }
 403          }
 404      } else {
 405          $post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id ) );
 406          $wpdb->update( $wpdb->posts, array( 'post_author' => $reassign ), array( 'post_author' => $id ) );
 407          if ( ! empty( $post_ids ) ) {
 408              foreach ( $post_ids as $post_id ) {
 409                  clean_post_cache( $post_id );
 410              }
 411          }
 412          $link_ids = $wpdb->get_col( $wpdb->prepare( "SELECT link_id FROM $wpdb->links WHERE link_owner = %d", $id ) );
 413          $wpdb->update( $wpdb->links, array( 'link_owner' => $reassign ), array( 'link_owner' => $id ) );
 414          if ( ! empty( $link_ids ) ) {
 415              foreach ( $link_ids as $link_id ) {
 416                  clean_bookmark_cache( $link_id );
 417              }
 418          }
 419      }
 420  
 421      // FINALLY, delete user
 422      if ( is_multisite() ) {
 423          remove_user_from_blog( $id, get_current_blog_id() );
 424      } else {
 425          $meta = $wpdb->get_col( $wpdb->prepare( "SELECT umeta_id FROM $wpdb->usermeta WHERE user_id = %d", $id ) );
 426          foreach ( $meta as $mid ) {
 427              delete_metadata_by_mid( 'user', $mid );
 428          }
 429  
 430          $wpdb->delete( $wpdb->users, array( 'ID' => $id ) );
 431      }
 432  
 433      clean_user_cache( $user );
 434  
 435      /**
 436       * Fires immediately after a user is deleted from the database.
 437       *
 438       * @since 2.9.0
 439       *
 440       * @param int      $id       ID of the deleted user.
 441       * @param int|null $reassign ID of the user to reassign posts and links to.
 442       *                           Default null, for no reassignment.
 443       */
 444      do_action( 'deleted_user', $id, $reassign );
 445  
 446      return true;
 447  }
 448  
 449  /**
 450   * Remove all capabilities from user.
 451   *
 452   * @since 2.1.0
 453   *
 454   * @param int $id User ID.
 455   */
 456  function wp_revoke_user( $id ) {
 457      $id = (int) $id;
 458  
 459      $user = new WP_User( $id );
 460      $user->remove_all_caps();
 461  }
 462  
 463  /**
 464   * @since 2.8.0
 465   *
 466   * @global int $user_ID
 467   *
 468   * @param false $errors Deprecated.
 469   */
 470  function default_password_nag_handler( $errors = false ) {
 471      global $user_ID;
 472      // Short-circuit it.
 473      if ( ! get_user_option( 'default_password_nag' ) ) {
 474          return;
 475      }
 476  
 477      // get_user_setting = JS saved UI setting. else no-js-fallback code.
 478      if ( 'hide' == get_user_setting( 'default_password_nag' ) || isset( $_GET['default_password_nag'] ) && '0' == $_GET['default_password_nag'] ) {
 479          delete_user_setting( 'default_password_nag' );
 480          update_user_option( $user_ID, 'default_password_nag', false, true );
 481      }
 482  }
 483  
 484  /**
 485   * @since 2.8.0
 486   *
 487   * @param int    $user_ID
 488   * @param object $old_data
 489   */
 490  function default_password_nag_edit_user( $user_ID, $old_data ) {
 491      // Short-circuit it.
 492      if ( ! get_user_option( 'default_password_nag', $user_ID ) ) {
 493          return;
 494      }
 495  
 496      $new_data = get_userdata( $user_ID );
 497  
 498      // Remove the nag if the password has been changed.
 499      if ( $new_data->user_pass != $old_data->user_pass ) {
 500          delete_user_setting( 'default_password_nag' );
 501          update_user_option( $user_ID, 'default_password_nag', false, true );
 502      }
 503  }
 504  
 505  /**
 506   * @since 2.8.0
 507   *
 508   * @global string $pagenow
 509   */
 510  function default_password_nag() {
 511      global $pagenow;
 512      // Short-circuit it.
 513      if ( 'profile.php' == $pagenow || ! get_user_option( 'default_password_nag' ) ) {
 514          return;
 515      }
 516  
 517      echo '<div class="error default-password-nag">';
 518      echo '<p>';
 519      echo '<strong>' . __( 'Notice:' ) . '</strong> ';
 520      _e( 'You&rsquo;re using the auto-generated password for your account. Would you like to change it?' );
 521      echo '</p><p>';
 522      printf( '<a href="%s">' . __( 'Yes, take me to my profile page' ) . '</a> | ', get_edit_profile_url() . '#password' );
 523      printf( '<a href="%s" id="default-password-nag-no">' . __( 'No thanks, do not remind me again' ) . '</a>', '?default_password_nag=0' );
 524      echo '</p></div>';
 525  }
 526  
 527  /**
 528   * @since 3.5.0
 529   * @access private
 530   */
 531  function delete_users_add_js() {
 532      ?>
 533  <script>
 534  jQuery(document).ready( function($) {
 535      var submit = $('#submit').prop('disabled', true);
 536      $('input[name="delete_option"]').one('change', function() {
 537          submit.prop('disabled', false);
 538      });
 539      $('#reassign_user').focus( function() {
 540          $('#delete_option1').prop('checked', true).trigger('change');
 541      });
 542  });
 543  </script>
 544      <?php
 545  }
 546  
 547  /**
 548   * Optional SSL preference that can be turned on by hooking to the 'personal_options' action.
 549   *
 550   * See the {@see 'personal_options'} action.
 551   *
 552   * @since 2.7.0
 553   *
 554   * @param object $user User data object.
 555   */
 556  function use_ssl_preference( $user ) {
 557      ?>
 558      <tr class="user-use-ssl-wrap">
 559          <th scope="row"><?php _e( 'Use https' ); ?></th>
 560          <td><label for="use_ssl"><input name="use_ssl" type="checkbox" id="use_ssl" value="1" <?php checked( '1', $user->use_ssl ); ?> /> <?php _e( 'Always use https when visiting the admin' ); ?></label></td>
 561      </tr>
 562      <?php
 563  }
 564  
 565  /**
 566   * @since MU (3.0.0)
 567   *
 568   * @param string $text
 569   * @return string
 570   */
 571  function admin_created_user_email( $text ) {
 572      $roles = get_editable_roles();
 573      $role  = $roles[ $_REQUEST['role'] ];
 574  
 575      return sprintf(
 576          /* translators: 1: Site title, 2: Site URL, 3: User role. */
 577          __(
 578              'Hi,
 579  You\'ve been invited to join \'%1$s\' at
 580  %2$s with the role of %3$s.
 581  If you do not want to join this site please ignore
 582  this email. This invitation will expire in a few days.
 583  
 584  Please click the following link to activate your user account:
 585  %%s'
 586          ),
 587          wp_specialchars_decode( get_bloginfo( 'name' ), ENT_QUOTES ),
 588          home_url(),
 589          wp_specialchars_decode( translate_user_role( $role['name'] ) )
 590      );
 591  }


Generated: Tue Oct 22 08:20:01 2019 Cross-referenced by PHPXref 0.7