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


Generated: Mon Jun 17 08:20:02 2019 Cross-referenced by PHPXref 0.7