web/wp-includes/user.php
changeset 204 09a1c134465b
parent 194 32102edaa81b
equal deleted inserted replaced
203:f507feede89a 204:09a1c134465b
    82 			$error->add('empty_password', __('<strong>ERROR</strong>: The password field is empty.'));
    82 			$error->add('empty_password', __('<strong>ERROR</strong>: The password field is empty.'));
    83 
    83 
    84 		return $error;
    84 		return $error;
    85 	}
    85 	}
    86 
    86 
    87 	$userdata = get_user_by('login', $username);
    87 	$user = get_user_by('login', $username);
    88 
    88 
    89 	if ( !$userdata )
    89 	if ( !$user )
    90 		return new WP_Error('invalid_username', sprintf(__('<strong>ERROR</strong>: Invalid username. <a href="%s" title="Password Lost and Found">Lost your password</a>?'), wp_lostpassword_url()));
    90 		return new WP_Error('invalid_username', sprintf(__('<strong>ERROR</strong>: Invalid username. <a href="%s" title="Password Lost and Found">Lost your password</a>?'), wp_lostpassword_url()));
    91 
    91 
    92 	if ( is_multisite() ) {
    92 	if ( is_multisite() ) {
    93 		// Is user marked as spam?
    93 		// Is user marked as spam?
    94 		if ( 1 == $userdata->spam)
    94 		if ( 1 == $user->spam)
    95 			return new WP_Error('invalid_username', __('<strong>ERROR</strong>: Your account has been marked as a spammer.'));
    95 			return new WP_Error('invalid_username', __('<strong>ERROR</strong>: Your account has been marked as a spammer.'));
    96 
    96 
    97 		// Is a user's blog marked as spam?
    97 		// Is a user's blog marked as spam?
    98 		if ( !is_super_admin( $userdata->ID ) && isset($userdata->primary_blog) ) {
    98 		if ( !is_super_admin( $user->ID ) && isset($user->primary_blog) ) {
    99 			$details = get_blog_details( $userdata->primary_blog );
    99 			$details = get_blog_details( $user->primary_blog );
   100 			if ( is_object( $details ) && $details->spam == 1 )
   100 			if ( is_object( $details ) && $details->spam == 1 )
   101 				return new WP_Error('blog_suspended', __('Site Suspended.'));
   101 				return new WP_Error('blog_suspended', __('Site Suspended.'));
   102 		}
   102 		}
   103 	}
   103 	}
   104 
   104 
   105 	$userdata = apply_filters('wp_authenticate_user', $userdata, $password);
   105 	$user = apply_filters('wp_authenticate_user', $user, $password);
   106 	if ( is_wp_error($userdata) )
   106 	if ( is_wp_error($user) )
   107 		return $userdata;
   107 		return $user;
   108 
   108 
   109 	if ( !wp_check_password($password, $userdata->user_pass, $userdata->ID) )
   109 	if ( !wp_check_password($password, $user->user_pass, $user->ID) )
   110 		return new WP_Error( 'incorrect_password', sprintf( __( '<strong>ERROR</strong>: The password you entered for the username <strong>%1$s</strong> is incorrect. <a href="%2$s" title="Password Lost and Found">Lost your password</a>?' ),
   110 		return new WP_Error( 'incorrect_password', sprintf( __( '<strong>ERROR</strong>: The password you entered for the username <strong>%1$s</strong> is incorrect. <a href="%2$s" title="Password Lost and Found">Lost your password</a>?' ),
   111 		$username, wp_lostpassword_url() ) );
   111 		$username, wp_lostpassword_url() ) );
   112 
   112 
   113 	$user =  new WP_User($userdata->ID);
       
   114 	return $user;
   113 	return $user;
   115 }
   114 }
   116 
   115 
   117 /**
   116 /**
   118  * Authenticate the user using the WordPress auth cookie.
   117  * Authenticate the user using the WordPress auth cookie.
   164  * Number of posts written by a list of users.
   163  * Number of posts written by a list of users.
   165  *
   164  *
   166  * @since 3.0.0
   165  * @since 3.0.0
   167  *
   166  *
   168  * @param array $users Array of user IDs.
   167  * @param array $users Array of user IDs.
   169  * @param string|array $post_type Optional. Post type to check. Defaults to post.
   168  * @param string $post_type Optional. Post type to check. Defaults to post.
       
   169  * @param bool $public_only Optional. Only return counts for public posts.  Defaults to false.
   170  * @return array Amount of posts each user has written.
   170  * @return array Amount of posts each user has written.
   171  */
   171  */
   172 function count_many_users_posts( $users, $post_type = 'post' ) {
   172 function count_many_users_posts( $users, $post_type = 'post', $public_only = false ) {
   173 	global $wpdb;
   173 	global $wpdb;
   174 
   174 
   175 	$count = array();
   175 	$count = array();
   176 	if ( empty( $users ) || ! is_array( $users ) )
   176 	if ( empty( $users ) || ! is_array( $users ) )
   177 		return $count;
   177 		return $count;
   178 
   178 
   179 	$userlist = implode( ',', array_map( 'absint', $users ) );
   179 	$userlist = implode( ',', array_map( 'absint', $users ) );
   180 	$where = get_posts_by_author_sql( $post_type );
   180 	$where = get_posts_by_author_sql( $post_type, true, null, $public_only );
   181 
   181 
   182 	$result = $wpdb->get_results( "SELECT post_author, COUNT(*) FROM $wpdb->posts $where AND post_author IN ($userlist) GROUP BY post_author", ARRAY_N );
   182 	$result = $wpdb->get_results( "SELECT post_author, COUNT(*) FROM $wpdb->posts $where AND post_author IN ($userlist) GROUP BY post_author", ARRAY_N );
   183 	foreach ( $result as $row ) {
   183 	foreach ( $result as $row ) {
   184 		$count[ $row[0] ] = $row[1];
   184 		$count[ $row[0] ] = $row[1];
   185 	}
   185 	}
   188 		if ( ! isset( $count[ $id ] ) )
   188 		if ( ! isset( $count[ $id ] ) )
   189 			$count[ $id ] = 0;
   189 			$count[ $id ] = 0;
   190 	}
   190 	}
   191 
   191 
   192 	return $count;
   192 	return $count;
   193 }
       
   194 
       
   195 /**
       
   196  * Check that the user login name and password is correct.
       
   197  *
       
   198  * @since 0.71
       
   199  * @todo xmlrpc only. Maybe move to xmlrpc.php.
       
   200  *
       
   201  * @param string $user_login User name.
       
   202  * @param string $user_pass User password.
       
   203  * @return bool False if does not authenticate, true if username and password authenticates.
       
   204  */
       
   205 function user_pass_ok($user_login, $user_pass) {
       
   206 	$user = wp_authenticate($user_login, $user_pass);
       
   207 	if ( is_wp_error($user) )
       
   208 		return false;
       
   209 
       
   210 	return true;
       
   211 }
   193 }
   212 
   194 
   213 //
   195 //
   214 // User option functions
   196 // User option functions
   215 //
   197 //
   253 
   235 
   254 	if ( !empty( $deprecated ) )
   236 	if ( !empty( $deprecated ) )
   255 		_deprecated_argument( __FUNCTION__, '3.0' );
   237 		_deprecated_argument( __FUNCTION__, '3.0' );
   256 
   238 
   257 	if ( empty( $user ) )
   239 	if ( empty( $user ) )
   258 		$user = wp_get_current_user();
   240 		$user = get_current_user_id();
   259 	else
   241 
   260 		$user = new WP_User( $user );
   242 	if ( ! $user = get_userdata( $user ) )
   261 
       
   262 	if ( ! $user->exists() )
       
   263 		return false;
   243 		return false;
   264 
   244 
   265 	if ( $user->has_prop( $wpdb->prefix . $option ) ) // Blog specific
   245 	if ( $user->has_prop( $wpdb->prefix . $option ) ) // Blog specific
   266 		$result = $user->get( $wpdb->prefix . $option );
   246 		$result = $user->get( $wpdb->prefix . $option );
   267 	elseif ( $user->has_prop( $option ) ) // User specific and cross-blog
   247 	elseif ( $user->has_prop( $option ) ) // User specific and cross-blog
   331  * WordPress User Query class.
   311  * WordPress User Query class.
   332  *
   312  *
   333  * @since 3.1.0
   313  * @since 3.1.0
   334  */
   314  */
   335 class WP_User_Query {
   315 class WP_User_Query {
       
   316 
       
   317 	/**
       
   318 	 * Query vars, after parsing
       
   319 	 *
       
   320 	 * @since 3.5.0
       
   321 	 * @access public
       
   322 	 * @var array
       
   323 	 */
       
   324 	var $query_vars = array();
   336 
   325 
   337 	/**
   326 	/**
   338 	 * List of found user ids
   327 	 * List of found user ids
   339 	 *
   328 	 *
   340 	 * @since 3.1.0
   329 	 * @since 3.1.0
   400 	 * @access private
   389 	 * @access private
   401 	 */
   390 	 */
   402 	function prepare_query() {
   391 	function prepare_query() {
   403 		global $wpdb;
   392 		global $wpdb;
   404 
   393 
   405 		$qv = &$this->query_vars;
   394 		$qv =& $this->query_vars;
   406 
   395 
   407 		if ( is_array( $qv['fields'] ) ) {
   396 		if ( is_array( $qv['fields'] ) ) {
   408 			$qv['fields'] = array_unique( $qv['fields'] );
   397 			$qv['fields'] = array_unique( $qv['fields'] );
   409 
   398 
   410 			$this->query_fields = array();
   399 			$this->query_fields = array();
   415 			$this->query_fields = "$wpdb->users.*";
   404 			$this->query_fields = "$wpdb->users.*";
   416 		} else {
   405 		} else {
   417 			$this->query_fields = "$wpdb->users.ID";
   406 			$this->query_fields = "$wpdb->users.ID";
   418 		}
   407 		}
   419 
   408 
   420 		if ( $this->query_vars['count_total'] )
   409 		if ( $qv['count_total'] )
   421 			$this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields;
   410 			$this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields;
   422 
   411 
   423 		$this->query_from = "FROM $wpdb->users";
   412 		$this->query_from = "FROM $wpdb->users";
   424 		$this->query_where = "WHERE 1=1";
   413 		$this->query_where = "WHERE 1=1";
   425 
   414 
   547 	 * @access private
   536 	 * @access private
   548 	 */
   537 	 */
   549 	function query() {
   538 	function query() {
   550 		global $wpdb;
   539 		global $wpdb;
   551 
   540 
   552 		if ( is_array( $this->query_vars['fields'] ) || 'all' == $this->query_vars['fields'] ) {
   541 		$qv =& $this->query_vars;
       
   542 
       
   543 		if ( is_array( $qv['fields'] ) || 'all' == $qv['fields'] ) {
   553 			$this->results = $wpdb->get_results("SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit");
   544 			$this->results = $wpdb->get_results("SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit");
   554 		} else {
   545 		} else {
   555 			$this->results = $wpdb->get_col("SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit");
   546 			$this->results = $wpdb->get_col("SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit");
   556 		}
   547 		}
   557 
   548 
   558 		if ( $this->query_vars['count_total'] )
   549 		if ( $qv['count_total'] )
   559 			$this->total_users = $wpdb->get_var( apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()' ) );
   550 			$this->total_users = $wpdb->get_var( apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()' ) );
   560 
   551 
   561 		if ( !$this->results )
   552 		if ( !$this->results )
   562 			return;
   553 			return;
   563 
   554 
   564 		if ( 'all_with_meta' == $this->query_vars['fields'] ) {
   555 		if ( 'all_with_meta' == $qv['fields'] ) {
   565 			cache_users( $this->results );
   556 			cache_users( $this->results );
   566 
   557 
   567 			$r = array();
   558 			$r = array();
   568 			foreach ( $this->results as $userid )
   559 			foreach ( $this->results as $userid )
   569 				$r[ $userid ] = new WP_User( $userid, '', $this->query_vars['blog_id'] );
   560 				$r[ $userid ] = new WP_User( $userid, '', $qv['blog_id'] );
   570 
   561 
   571 			$this->results = $r;
   562 			$this->results = $r;
   572 		}
   563 		} elseif ( 'all' == $qv['fields'] ) {
       
   564 			foreach ( $this->results as $key => $user ) {
       
   565 				$this->results[ $key ] = new WP_User( $user );
       
   566 			}
       
   567 		}
       
   568 	}
       
   569 
       
   570 	/**
       
   571 	 * Retrieve query variable.
       
   572 	 *
       
   573 	 * @since 3.5.0
       
   574 	 * @access public
       
   575 	 *
       
   576 	 * @param string $query_var Query variable key.
       
   577 	 * @return mixed
       
   578 	 */
       
   579 	function get( $query_var ) {
       
   580 		if ( isset( $this->query_vars[$query_var] ) )
       
   581 			return $this->query_vars[$query_var];
       
   582 
       
   583 		return null;
       
   584 	}
       
   585 
       
   586 	/**
       
   587 	 * Set query variable.
       
   588 	 *
       
   589 	 * @since 3.5.0
       
   590 	 * @access public
       
   591 	 *
       
   592 	 * @param string $query_var Query variable key.
       
   593 	 * @param mixed $value Query variable value.
       
   594 	 */
       
   595 	function set( $query_var, $value ) {
       
   596 		$this->query_vars[$query_var] = $value;
   573 	}
   597 	}
   574 
   598 
   575 	/*
   599 	/*
   576 	 * Used internally to generate an SQL string for searching across multiple columns
   600 	 * Used internally to generate an SQL string for searching across multiple columns
   577 	 *
   601 	 *
   674 		$blogs[ $blog_id ]->blogname = get_option('blogname');
   698 		$blogs[ $blog_id ]->blogname = get_option('blogname');
   675 		$blogs[ $blog_id ]->domain = '';
   699 		$blogs[ $blog_id ]->domain = '';
   676 		$blogs[ $blog_id ]->path = '';
   700 		$blogs[ $blog_id ]->path = '';
   677 		$blogs[ $blog_id ]->site_id = 1;
   701 		$blogs[ $blog_id ]->site_id = 1;
   678 		$blogs[ $blog_id ]->siteurl = get_option('siteurl');
   702 		$blogs[ $blog_id ]->siteurl = get_option('siteurl');
       
   703 		$blogs[ $blog_id ]->archived = 0;
       
   704 		$blogs[ $blog_id ]->spam = 0;
       
   705 		$blogs[ $blog_id ]->deleted = 0;
   679 		return $blogs;
   706 		return $blogs;
   680 	}
   707 	}
   681 
   708 
   682 	$blogs = array();
   709 	$blogs = array();
   683 
   710 
   689 				'blogname'    => $blog->blogname,
   716 				'blogname'    => $blog->blogname,
   690 				'domain'      => $blog->domain,
   717 				'domain'      => $blog->domain,
   691 				'path'        => $blog->path,
   718 				'path'        => $blog->path,
   692 				'site_id'     => $blog->site_id,
   719 				'site_id'     => $blog->site_id,
   693 				'siteurl'     => $blog->siteurl,
   720 				'siteurl'     => $blog->siteurl,
       
   721 				'archived'    => 0,
       
   722 				'spam'        => 0,
       
   723 				'deleted'     => 0
   694 			);
   724 			);
   695 		}
   725 		}
   696 		unset( $keys[ $wpdb->base_prefix . 'capabilities' ] );
   726 		unset( $keys[ $wpdb->base_prefix . 'capabilities' ] );
   697 	}
   727 	}
   698 
   728 
   715 				'blogname'    => $blog->blogname,
   745 				'blogname'    => $blog->blogname,
   716 				'domain'      => $blog->domain,
   746 				'domain'      => $blog->domain,
   717 				'path'        => $blog->path,
   747 				'path'        => $blog->path,
   718 				'site_id'     => $blog->site_id,
   748 				'site_id'     => $blog->site_id,
   719 				'siteurl'     => $blog->siteurl,
   749 				'siteurl'     => $blog->siteurl,
       
   750 				'archived'    => 0,
       
   751 				'spam'        => 0,
       
   752 				'deleted'     => 0
   720 			);
   753 			);
   721 		}
   754 		}
   722 	}
   755 	}
   723 
   756 
   724 	return apply_filters( 'get_blogs_of_user', $blogs, $user_id, $all );
   757 	return apply_filters( 'get_blogs_of_user', $blogs, $user_id, $all );
   918  * @global string $user_login The user username for logging in
   951  * @global string $user_login The user username for logging in
   919  * @global int $user_level The level of the user
   952  * @global int $user_level The level of the user
   920  * @global int $user_ID The ID of the user
   953  * @global int $user_ID The ID of the user
   921  * @global string $user_email The email address of the user
   954  * @global string $user_email The email address of the user
   922  * @global string $user_url The url in the user's profile
   955  * @global string $user_url The url in the user's profile
   923  * @global string $user_pass_md5 MD5 of the user's password
       
   924  * @global string $user_identity The display name of the user
   956  * @global string $user_identity The display name of the user
   925  *
   957  *
   926  * @param int $for_user_id Optional. User ID to set up global data.
   958  * @param int $for_user_id Optional. User ID to set up global data.
   927  */
   959  */
   928 function setup_userdata($for_user_id = '') {
   960 function setup_userdata($for_user_id = '') {
   929 	global $user_login, $userdata, $user_level, $user_ID, $user_email, $user_url, $user_pass_md5, $user_identity;
   961 	global $user_login, $userdata, $user_level, $user_ID, $user_email, $user_url, $user_identity;
   930 
   962 
   931 	if ( '' == $for_user_id )
   963 	if ( '' == $for_user_id )
   932 		$user = wp_get_current_user();
   964 		$for_user_id = get_current_user_id();
   933 	else
   965 	$user = get_userdata( $for_user_id );
   934 		$user = new WP_User($for_user_id);
   966 
   935 
   967 	if ( ! $user ) {
   936 	$userdata   = null;
   968 		$user_ID = 0;
       
   969 		$user_level = 0;
       
   970 		$userdata = null;
       
   971 		$user_login = $user_email = $user_url = $user_identity = '';
       
   972 		return;
       
   973 	}
       
   974 
   937 	$user_ID    = (int) $user->ID;
   975 	$user_ID    = (int) $user->ID;
   938 	$user_level = (int) isset($user->user_level) ? $user->user_level : 0;
   976 	$user_level = (int) $user->user_level;
   939 
       
   940 	if ( ! $user->exists() ) {
       
   941 		$user_login = $user_email = $user_url = $user_pass_md5 = $user_identity = '';
       
   942 		return;
       
   943 	}
       
   944 
       
   945 	$userdata   = $user;
   977 	$userdata   = $user;
   946 	$user_login = $user->user_login;
   978 	$user_login = $user->user_login;
   947 	$user_email = $user->user_email;
   979 	$user_email = $user->user_email;
   948 	$user_url   = $user->user_url;
   980 	$user_url   = $user->user_url;
   949 	$user_pass_md5 = md5( $user->user_pass );
       
   950 	$user_identity = $user->display_name;
   981 	$user_identity = $user->display_name;
   951 }
   982 }
   952 
   983 
   953 /**
   984 /**
   954  * Create dropdown HTML content of users.
   985  * Create dropdown HTML content of users.
  1219  *
  1250  *
  1220  * The $userdata array can contain the following fields:
  1251  * The $userdata array can contain the following fields:
  1221  * 'ID' - An integer that will be used for updating an existing user.
  1252  * 'ID' - An integer that will be used for updating an existing user.
  1222  * 'user_pass' - A string that contains the plain text password for the user.
  1253  * 'user_pass' - A string that contains the plain text password for the user.
  1223  * 'user_login' - A string that contains the user's username for logging in.
  1254  * 'user_login' - A string that contains the user's username for logging in.
  1224  * 'user_nicename' - A string that contains a nicer looking name for the user.
  1255  * 'user_nicename' - A string that contains a URL-friendly name for the user.
  1225  *		The default is the user's username.
  1256  *		The default is the user's username.
  1226  * 'user_url' - A string containing the user's URL for the user's web site.
  1257  * 'user_url' - A string containing the user's URL for the user's web site.
  1227  * 'user_email' - A string containing the user's email address.
  1258  * 'user_email' - A string containing the user's email address.
  1228  * 'display_name' - A string that will be shown on the site. Defaults to user's
  1259  * 'display_name' - A string that will be shown on the site. Defaults to user's
  1229  *		username. It is likely that you will want to change this, for appearance.
  1260  *		username. It is likely that you will want to change this, for appearance.
  1243  * @uses $wpdb WordPress database layer.
  1274  * @uses $wpdb WordPress database layer.
  1244  * @uses apply_filters() Calls filters for most of the $userdata fields with the prefix 'pre_user'. See note above.
  1275  * @uses apply_filters() Calls filters for most of the $userdata fields with the prefix 'pre_user'. See note above.
  1245  * @uses do_action() Calls 'profile_update' hook when updating giving the user's ID
  1276  * @uses do_action() Calls 'profile_update' hook when updating giving the user's ID
  1246  * @uses do_action() Calls 'user_register' hook when creating a new user giving the user's ID
  1277  * @uses do_action() Calls 'user_register' hook when creating a new user giving the user's ID
  1247  *
  1278  *
  1248  * @param array $userdata An array of user data.
  1279  * @param mixed $userdata An array of user data or a user object of type stdClass or WP_User.
  1249  * @return int|WP_Error The newly created user's ID or a WP_Error object if the user could not be created.
  1280  * @return int|WP_Error The newly created user's ID or a WP_Error object if the user could not be created.
  1250  */
  1281  */
  1251 function wp_insert_user($userdata) {
  1282 function wp_insert_user( $userdata ) {
  1252 	global $wpdb;
  1283 	global $wpdb;
  1253 
  1284 
  1254 	extract($userdata, EXTR_SKIP);
  1285 	if ( is_a( $userdata, 'stdClass' ) )
       
  1286 		$userdata = get_object_vars( $userdata );
       
  1287 	elseif ( is_a( $userdata, 'WP_User' ) )
       
  1288 		$userdata = $userdata->to_array();
       
  1289 
       
  1290 	extract( $userdata, EXTR_SKIP );
  1255 
  1291 
  1256 	// Are we updating or creating?
  1292 	// Are we updating or creating?
  1257 	if ( !empty($ID) ) {
  1293 	if ( !empty($ID) ) {
  1258 		$ID = (int) $ID;
  1294 		$ID = (int) $ID;
  1259 		$update = true;
  1295 		$update = true;
  1272 
  1308 
  1273 	if ( empty($user_login) )
  1309 	if ( empty($user_login) )
  1274 		return new WP_Error('empty_user_login', __('Cannot create a user with an empty login name.') );
  1310 		return new WP_Error('empty_user_login', __('Cannot create a user with an empty login name.') );
  1275 
  1311 
  1276 	if ( !$update && username_exists( $user_login ) )
  1312 	if ( !$update && username_exists( $user_login ) )
  1277 		return new WP_Error('existing_user_login', __('This username is already registered.') );
  1313 		return new WP_Error( 'existing_user_login', __( 'Sorry, that username already exists!' ) );
  1278 
  1314 
  1279 	if ( empty($user_nicename) )
  1315 	if ( empty($user_nicename) )
  1280 		$user_nicename = sanitize_title( $user_login );
  1316 		$user_nicename = sanitize_title( $user_login );
  1281 	$user_nicename = apply_filters('pre_user_nicename', $user_nicename);
  1317 	$user_nicename = apply_filters('pre_user_nicename', $user_nicename);
  1282 
  1318 
  1287 	if ( empty($user_email) )
  1323 	if ( empty($user_email) )
  1288 		$user_email = '';
  1324 		$user_email = '';
  1289 	$user_email = apply_filters('pre_user_email', $user_email);
  1325 	$user_email = apply_filters('pre_user_email', $user_email);
  1290 
  1326 
  1291 	if ( !$update && ! defined( 'WP_IMPORTING' ) && email_exists($user_email) )
  1327 	if ( !$update && ! defined( 'WP_IMPORTING' ) && email_exists($user_email) )
  1292 		return new WP_Error('existing_user_email', __('This email address is already registered.') );
  1328 		return new WP_Error( 'existing_user_email', __( 'Sorry, that email address is already used!' ) );
  1293 
       
  1294 	if ( empty($display_name) )
       
  1295 		$display_name = $user_login;
       
  1296 	$display_name = apply_filters('pre_user_display_name', $display_name);
       
  1297 
  1329 
  1298 	if ( empty($nickname) )
  1330 	if ( empty($nickname) )
  1299 		$nickname = $user_login;
  1331 		$nickname = $user_login;
  1300 	$nickname = apply_filters('pre_user_nickname', $nickname);
  1332 	$nickname = apply_filters('pre_user_nickname', $nickname);
  1301 
  1333 
  1304 	$first_name = apply_filters('pre_user_first_name', $first_name);
  1336 	$first_name = apply_filters('pre_user_first_name', $first_name);
  1305 
  1337 
  1306 	if ( empty($last_name) )
  1338 	if ( empty($last_name) )
  1307 		$last_name = '';
  1339 		$last_name = '';
  1308 	$last_name = apply_filters('pre_user_last_name', $last_name);
  1340 	$last_name = apply_filters('pre_user_last_name', $last_name);
       
  1341 
       
  1342 	if ( empty( $display_name ) ) {
       
  1343 		if ( $update )
       
  1344 			$display_name = $user_login;
       
  1345 		elseif ( $first_name && $last_name )
       
  1346 			/* translators: 1: first name, 2: last name */
       
  1347 			$display_name = sprintf( _x( '%1$s %2$s', 'Display name based on first name and last name' ), $first_name, $last_name );
       
  1348 		elseif ( $first_name )
       
  1349 			$display_name = $first_name;
       
  1350 		elseif ( $last_name )
       
  1351 			$display_name = $last_name;
       
  1352 		else
       
  1353 			$display_name = $user_login;
       
  1354 	}
       
  1355 	$display_name = apply_filters( 'pre_user_display_name', $display_name );
  1309 
  1356 
  1310 	if ( empty($description) )
  1357 	if ( empty($description) )
  1311 		$description = '';
  1358 		$description = '';
  1312 	$description = apply_filters('pre_user_description', $description);
  1359 	$description = apply_filters('pre_user_description', $description);
  1313 
  1360 
  1390  *
  1437  *
  1391  * @since 2.0.0
  1438  * @since 2.0.0
  1392  * @see wp_insert_user() For what fields can be set in $userdata
  1439  * @see wp_insert_user() For what fields can be set in $userdata
  1393  * @uses wp_insert_user() Used to update existing user or add new one if user doesn't exist already
  1440  * @uses wp_insert_user() Used to update existing user or add new one if user doesn't exist already
  1394  *
  1441  *
  1395  * @param array $userdata An array of user data.
  1442  * @param mixed $userdata An array of user data or a user object of type stdClass or WP_User.
  1396  * @return int The updated user's ID.
  1443  * @return int|WP_Error The updated user's ID or a WP_Error object if the user could not be updated.
  1397  */
  1444  */
  1398 function wp_update_user($userdata) {
  1445 function wp_update_user($userdata) {
       
  1446 	if ( is_a( $userdata, 'stdClass' ) )
       
  1447 		$userdata = get_object_vars( $userdata );
       
  1448 	elseif ( is_a( $userdata, 'WP_User' ) )
       
  1449 		$userdata = $userdata->to_array();
       
  1450 
  1399 	$ID = (int) $userdata['ID'];
  1451 	$ID = (int) $userdata['ID'];
  1400 
  1452 
  1401 	// First, get all of the original fields
  1453 	// First, get all of the original fields
  1402 	$user_obj = get_userdata( $ID );
  1454 	$user_obj = get_userdata( $ID );
  1403 
  1455 
  1404 	$user = get_object_vars( $user_obj->data );
  1456 	$user = $user_obj->to_array();
  1405 
  1457 
  1406 	// Add additional custom fields
  1458 	// Add additional custom fields
  1407 	foreach ( _get_additional_user_keys( $user_obj ) as $key ) {
  1459 	foreach ( _get_additional_user_keys( $user_obj ) as $key ) {
  1408 		$user[ $key ] = get_user_meta( $ID, $key, true );
  1460 		$user[ $key ] = get_user_meta( $ID, $key, true );
  1409 	}
  1461 	}
  1459 }
  1511 }
  1460 
  1512 
  1461 /**
  1513 /**
  1462  * Return a list of meta keys that wp_insert_user() is supposed to set.
  1514  * Return a list of meta keys that wp_insert_user() is supposed to set.
  1463  *
  1515  *
       
  1516  * @since 3.3.0
  1464  * @access private
  1517  * @access private
  1465  * @since 3.3.0
  1518  *
  1466  *
  1519  * @param object $user WP_User instance.
  1467  * @param object $user WP_User instance
       
  1468  * @return array
  1520  * @return array
  1469  */
  1521  */
  1470 function _get_additional_user_keys( $user ) {
  1522 function _get_additional_user_keys( $user ) {
  1471 	$keys = array( 'first_name', 'last_name', 'nickname', 'description', 'rich_editing', 'comment_shortcuts', 'admin_color', 'use_ssl', 'show_admin_bar_front' );
  1523 	$keys = array( 'first_name', 'last_name', 'nickname', 'description', 'rich_editing', 'comment_shortcuts', 'admin_color', 'use_ssl', 'show_admin_bar_front' );
  1472 	return array_merge( $keys, array_keys( _wp_get_user_contactmethods( $user ) ) );
  1524 	return array_merge( $keys, array_keys( _wp_get_user_contactmethods( $user ) ) );
  1473 }
  1525 }
  1474 
  1526 
  1475 /**
  1527 /**
  1476  * Set up the default contact methods
  1528  * Set up the default contact methods.
  1477  *
  1529  *
       
  1530  * @since 2.9.0
  1478  * @access private
  1531  * @access private
  1479  * @since
  1532  *
  1480  *
  1533  * @param object $user User data object (optional).
  1481  * @param object $user User data object (optional)
       
  1482  * @return array $user_contactmethods Array of contact methods and their labels.
  1534  * @return array $user_contactmethods Array of contact methods and their labels.
  1483  */
  1535  */
  1484 function _wp_get_user_contactmethods( $user = null ) {
  1536 function _wp_get_user_contactmethods( $user = null ) {
  1485 	$user_contactmethods = array(
  1537 	$user_contactmethods = array(
  1486 		'aim' => __('AIM'),
  1538 		'aim' => __('AIM'),