wp/wp-admin/includes/user.php
changeset 5 5e2f62d02dcd
parent 0 d970ebf37754
child 7 cf61fcea0001
equal deleted inserted replaced
4:346c88efed21 5:5e2f62d02dcd
     7  */
     7  */
     8 
     8 
     9 /**
     9 /**
    10  * Creates a new user from the "Users" form using $_POST information.
    10  * Creates a new user from the "Users" form using $_POST information.
    11  *
    11  *
    12  * @since 2.0
    12  * @since 2.0.0
    13  *
    13  *
    14  * @return null|WP_Error|int Null when adding user, WP_Error or User ID integer when no parameters.
    14  * @return null|WP_Error|int Null when adding user, WP_Error or User ID integer when no parameters.
    15  */
    15  */
    16 function add_user() {
    16 function add_user() {
    17 	return edit_user();
    17 	return edit_user();
    20 /**
    20 /**
    21  * Edit user settings based on contents of $_POST
    21  * Edit user settings based on contents of $_POST
    22  *
    22  *
    23  * Used on user-edit.php and profile.php to manage and process user options, passwords etc.
    23  * Used on user-edit.php and profile.php to manage and process user options, passwords etc.
    24  *
    24  *
    25  * @since 2.0
    25  * @since 2.0.0
    26  *
    26  *
    27  * @param int $user_id Optional. User ID.
    27  * @param int $user_id Optional. User ID.
    28  * @return int user id of the updated user
    28  * @return int user id of the updated user
    29  */
    29  */
    30 function edit_user( $user_id = 0 ) {
    30 function edit_user( $user_id = 0 ) {
    31 	global $wp_roles, $wpdb;
    31 	global $wp_roles;
    32 	$user = new stdClass;
    32 	$user = new stdClass;
    33 	if ( $user_id ) {
    33 	if ( $user_id ) {
    34 		$update = true;
    34 		$update = true;
    35 		$user->ID = (int) $user_id;
    35 		$user->ID = (int) $user_id;
    36 		$userdata = get_userdata( $user_id );
    36 		$userdata = get_userdata( $user_id );
    61 		if ( ! empty( $new_role ) && empty( $editable_roles[$new_role] ) )
    61 		if ( ! empty( $new_role ) && empty( $editable_roles[$new_role] ) )
    62 			wp_die(__('You can’t give users that role.'));
    62 			wp_die(__('You can’t give users that role.'));
    63 	}
    63 	}
    64 
    64 
    65 	if ( isset( $_POST['email'] ))
    65 	if ( isset( $_POST['email'] ))
    66 		$user->user_email = sanitize_text_field( $_POST['email'] );
    66 		$user->user_email = sanitize_text_field( wp_unslash( $_POST['email'] ) );
    67 	if ( isset( $_POST['url'] ) ) {
    67 	if ( isset( $_POST['url'] ) ) {
    68 		if ( empty ( $_POST['url'] ) || $_POST['url'] == 'http://' ) {
    68 		if ( empty ( $_POST['url'] ) || $_POST['url'] == 'http://' ) {
    69 			$user->user_url = '';
    69 			$user->user_url = '';
    70 		} else {
    70 		} else {
    71 			$user->user_url = esc_url_raw( $_POST['url'] );
    71 			$user->user_url = esc_url_raw( $_POST['url'] );
   107 	/* checking that username has been typed */
   107 	/* checking that username has been typed */
   108 	if ( $user->user_login == '' )
   108 	if ( $user->user_login == '' )
   109 		$errors->add( 'user_login', __( '<strong>ERROR</strong>: Please enter a username.' ) );
   109 		$errors->add( 'user_login', __( '<strong>ERROR</strong>: Please enter a username.' ) );
   110 
   110 
   111 	/* checking the password has been typed twice */
   111 	/* checking the password has been typed twice */
       
   112 	/**
       
   113 	 * Fires before the password and confirm password fields are checked for congruity.
       
   114 	 *
       
   115 	 * @since 1.5.1
       
   116 	 *
       
   117 	 * @param string $user_login The username.
       
   118 	 * @param string &$pass1     The password, passed by reference.
       
   119 	 * @param string &$pass2     The confirmed password, passed by reference.
       
   120 	 */
   112 	do_action_ref_array( 'check_passwords', array( $user->user_login, &$pass1, &$pass2 ) );
   121 	do_action_ref_array( 'check_passwords', array( $user->user_login, &$pass1, &$pass2 ) );
   113 
   122 
   114 	if ( $update ) {
   123 	if ( $update ) {
   115 		if ( empty($pass1) && !empty($pass2) )
   124 		if ( empty($pass1) && !empty($pass2) )
   116 			$errors->add( 'pass', __( '<strong>ERROR</strong>: You entered your new password only once.' ), array( 'form-field' => 'pass1' ) );
   125 			$errors->add( 'pass', __( '<strong>ERROR</strong>: You entered your new password only once.' ), array( 'form-field' => 'pass1' ) );
   147 		$errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The email address isn&#8217;t correct.' ), array( 'form-field' => 'email' ) );
   156 		$errors->add( 'invalid_email', __( '<strong>ERROR</strong>: The email address isn&#8217;t correct.' ), array( 'form-field' => 'email' ) );
   148 	} elseif ( ( $owner_id = email_exists($user->user_email) ) && ( !$update || ( $owner_id != $user->ID ) ) ) {
   157 	} elseif ( ( $owner_id = email_exists($user->user_email) ) && ( !$update || ( $owner_id != $user->ID ) ) ) {
   149 		$errors->add( 'email_exists', __('<strong>ERROR</strong>: This email is already registered, please choose another one.'), array( 'form-field' => 'email' ) );
   158 		$errors->add( 'email_exists', __('<strong>ERROR</strong>: This email is already registered, please choose another one.'), array( 'form-field' => 'email' ) );
   150 	}
   159 	}
   151 
   160 
   152 	// Allow plugins to return their own errors.
   161 	/**
       
   162 	 * Fires before user profile update errors are returned.
       
   163 	 *
       
   164 	 * @since 2.8.0
       
   165 	 *
       
   166 	 * @param array   &$errors An array of user profile update errors, passed by reference.
       
   167 	 * @param bool    $update  Whether this is a user update.
       
   168 	 * @param WP_User &$user   WP_User object, passed by reference.
       
   169 	 */
   153 	do_action_ref_array( 'user_profile_update_errors', array( &$errors, $update, &$user ) );
   170 	do_action_ref_array( 'user_profile_update_errors', array( &$errors, $update, &$user ) );
   154 
   171 
   155 	if ( $errors->get_error_codes() )
   172 	if ( $errors->get_error_codes() )
   156 		return $errors;
   173 		return $errors;
   157 
   174 
   174  * Specifically because without filtering anyone with the edit_users
   191  * Specifically because without filtering anyone with the edit_users
   175  * capability can edit others to be administrators, even if they are
   192  * capability can edit others to be administrators, even if they are
   176  * only editors or authors. This filter allows admins to delegate
   193  * only editors or authors. This filter allows admins to delegate
   177  * user management.
   194  * user management.
   178  *
   195  *
   179  * @since 2.8
   196  * @since 2.8.0
   180  *
   197  *
   181  * @return unknown
   198  * @return array
   182  */
   199  */
   183 function get_editable_roles() {
   200 function get_editable_roles() {
   184 	global $wp_roles;
   201 	global $wp_roles;
   185 
   202 
   186 	$all_roles = $wp_roles->roles;
   203 	$all_roles = $wp_roles->roles;
   187 	$editable_roles = apply_filters('editable_roles', $all_roles);
   204 
       
   205 	/**
       
   206 	 * Filter the list of editable roles.
       
   207 	 *
       
   208 	 * @since 2.8.0
       
   209 	 *
       
   210 	 * @param array $all_roles List of roles.
       
   211 	 */
       
   212 	$editable_roles = apply_filters( 'editable_roles', $all_roles );
   188 
   213 
   189 	return $editable_roles;
   214 	return $editable_roles;
   190 }
   215 }
   191 
   216 
   192 /**
   217 /**
   215  * @return array
   240  * @return array
   216  */
   241  */
   217 function get_users_drafts( $user_id ) {
   242 function get_users_drafts( $user_id ) {
   218 	global $wpdb;
   243 	global $wpdb;
   219 	$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);
   244 	$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);
   220 	$query = apply_filters('get_users_drafts', $query);
   245 
       
   246 	/**
       
   247 	 * Filter the user's drafts query string.
       
   248 	 *
       
   249 	 * @since 2.0.0
       
   250 	 *
       
   251 	 * @param string $query The user's drafts query string.
       
   252 	 */
       
   253 	$query = apply_filters( 'get_users_drafts', $query );
   221 	return $wpdb->get_results( $query );
   254 	return $wpdb->get_results( $query );
   222 }
   255 }
   223 
   256 
   224 /**
   257 /**
   225  * Remove user and optionally reassign posts and links to another user.
   258  * Remove user and optionally reassign posts and links to another user.
   226  *
   259  *
   227  * If the $reassign parameter is not assigned to an User ID, then all posts will
   260  * If the $reassign parameter is not assigned to a User ID, then all posts will
   228  * be deleted of that user. The action 'delete_user' that is passed the User ID
   261  * be deleted of that user. The action 'delete_user' that is passed the User ID
   229  * being deleted will be run after the posts are either reassigned or deleted.
   262  * being deleted will be run after the posts are either reassigned or deleted.
   230  * The user meta will also be deleted that are for that User ID.
   263  * The user meta will also be deleted that are for that User ID.
   231  *
   264  *
   232  * @since 2.0.0
   265  * @since 2.0.0
   233  *
   266  *
   234  * @param int $id User ID.
   267  * @param int $id User ID.
   235  * @param int $reassign Optional. Reassign posts and links to new User ID.
   268  * @param int $reassign Optional. Reassign posts and links to new User ID.
   236  * @return bool True when finished.
   269  * @return bool True when finished.
   237  */
   270  */
   238 function wp_delete_user( $id, $reassign = 'novalue' ) {
   271 function wp_delete_user( $id, $reassign = null ) {
   239 	global $wpdb;
   272 	global $wpdb;
   240 
   273 
   241 	$id = (int) $id;
   274 	$id = (int) $id;
   242 	$user = new WP_User( $id );
   275 	$user = new WP_User( $id );
   243 
   276 
   244 	if ( !$user->exists() )
   277 	if ( !$user->exists() )
   245 		return false;
   278 		return false;
   246 
   279 
   247 	// allow for transaction statement
   280 	// Normalize $reassign to null or a user ID. 'novalue' was an older default.
   248 	do_action('delete_user', $id);
   281 	if ( 'novalue' === $reassign ) {
   249 
   282 		$reassign = null;
   250 	if ( 'novalue' === $reassign || null === $reassign ) {
   283 	} elseif ( null !== $reassign ) {
       
   284 		$reassign = (int) $reassign;
       
   285 	}
       
   286 
       
   287 	/**
       
   288 	 * Fires immediately before a user is deleted from the database.
       
   289 	 *
       
   290 	 * @since 2.0.0
       
   291 	 *
       
   292 	 * @param int      $id       ID of the user to delete.
       
   293 	 * @param int|null $reassign ID of the user to reassign posts and links to.
       
   294 	 *                           Default null, for no reassignment.
       
   295 	 */
       
   296 	do_action( 'delete_user', $id, $reassign );
       
   297 
       
   298 	if ( null === $reassign ) {
   251 		$post_types_to_delete = array();
   299 		$post_types_to_delete = array();
   252 		foreach ( get_post_types( array(), 'objects' ) as $post_type ) {
   300 		foreach ( get_post_types( array(), 'objects' ) as $post_type ) {
   253 			if ( $post_type->delete_with_user ) {
   301 			if ( $post_type->delete_with_user ) {
   254 				$post_types_to_delete[] = $post_type->name;
   302 				$post_types_to_delete[] = $post_type->name;
   255 			} elseif ( null === $post_type->delete_with_user && post_type_supports( $post_type->name, 'author' ) ) {
   303 			} elseif ( null === $post_type->delete_with_user && post_type_supports( $post_type->name, 'author' ) ) {
   256 				$post_types_to_delete[] = $post_type->name;
   304 				$post_types_to_delete[] = $post_type->name;
   257 			}
   305 			}
   258 		}
   306 		}
   259 
   307 
       
   308 		/**
       
   309 		 * Filter the list of post types to delete with a user.
       
   310 		 *
       
   311 		 * @since 3.4.0
       
   312 		 *
       
   313 		 * @param array $post_types_to_delete Post types to delete.
       
   314 		 * @param int   $id                   User ID.
       
   315 		 */
   260 		$post_types_to_delete = apply_filters( 'post_types_to_delete_with_user', $post_types_to_delete, $id );
   316 		$post_types_to_delete = apply_filters( 'post_types_to_delete_with_user', $post_types_to_delete, $id );
   261 		$post_types_to_delete = implode( "', '", $post_types_to_delete );
   317 		$post_types_to_delete = implode( "', '", $post_types_to_delete );
   262 		$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 ) );
   318 		$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 ) );
   263 		if ( $post_ids ) {
   319 		if ( $post_ids ) {
   264 			foreach ( $post_ids as $post_id )
   320 			foreach ( $post_ids as $post_id )
   271 		if ( $link_ids ) {
   327 		if ( $link_ids ) {
   272 			foreach ( $link_ids as $link_id )
   328 			foreach ( $link_ids as $link_id )
   273 				wp_delete_link($link_id);
   329 				wp_delete_link($link_id);
   274 		}
   330 		}
   275 	} else {
   331 	} else {
   276 		$reassign = (int) $reassign;
       
   277 		$post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id ) );
   332 		$post_ids = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM $wpdb->posts WHERE post_author = %d", $id ) );
   278 		$wpdb->update( $wpdb->posts, array('post_author' => $reassign), array('post_author' => $id) );
   333 		$wpdb->update( $wpdb->posts, array('post_author' => $reassign), array('post_author' => $id) );
   279 		if ( ! empty( $post_ids ) ) {
   334 		if ( ! empty( $post_ids ) ) {
   280 			foreach ( $post_ids as $post_id )
   335 			foreach ( $post_ids as $post_id )
   281 				clean_post_cache( $post_id );
   336 				clean_post_cache( $post_id );
   299 		$wpdb->delete( $wpdb->users, array( 'ID' => $id ) );
   354 		$wpdb->delete( $wpdb->users, array( 'ID' => $id ) );
   300 	}
   355 	}
   301 
   356 
   302 	clean_user_cache( $user );
   357 	clean_user_cache( $user );
   303 
   358 
   304 	// allow for commit transaction
   359 	/**
   305 	do_action('deleted_user', $id);
   360 	 * Fires immediately after a user is deleted from the database.
       
   361 	 *
       
   362 	 * @since 2.9.0
       
   363 	 *
       
   364 	 * @param int      $id       ID of the deleted user.
       
   365 	 * @param int|null $reassign ID of the user to reassign posts and links to.
       
   366 	 *                           Default null, for no reassignment.
       
   367 	 */
       
   368 	do_action( 'deleted_user', $id, $reassign );
   306 
   369 
   307 	return true;
   370 	return true;
   308 }
   371 }
   309 
   372 
   310 /**
   373 /**
   325 /**
   388 /**
   326  * @since 2.8.0
   389  * @since 2.8.0
   327  */
   390  */
   328 function default_password_nag_handler($errors = false) {
   391 function default_password_nag_handler($errors = false) {
   329 	global $user_ID;
   392 	global $user_ID;
   330 	if ( ! get_user_option('default_password_nag') ) //Short circuit it.
   393 	// Short-circuit it.
       
   394 	if ( ! get_user_option('default_password_nag') )
   331 		return;
   395 		return;
   332 
   396 
   333 	//get_user_setting = JS saved UI setting. else no-js-fallback code.
   397 	// get_user_setting = JS saved UI setting. else no-js-fallback code.
   334 	if ( 'hide' == get_user_setting('default_password_nag') || isset($_GET['default_password_nag']) && '0' == $_GET['default_password_nag'] ) {
   398 	if ( 'hide' == get_user_setting('default_password_nag') || isset($_GET['default_password_nag']) && '0' == $_GET['default_password_nag'] ) {
   335 		delete_user_setting('default_password_nag');
   399 		delete_user_setting('default_password_nag');
   336 		update_user_option($user_ID, 'default_password_nag', false, true);
   400 		update_user_option($user_ID, 'default_password_nag', false, true);
   337 	}
   401 	}
   338 }
   402 }
   339 
   403 
   340 add_action('profile_update', 'default_password_nag_edit_user', 10, 2);
   404 add_action('profile_update', 'default_password_nag_edit_user', 10, 2);
       
   405 
   341 /**
   406 /**
   342  * @since 2.8.0
   407  * @since 2.8.0
   343  */
   408  */
   344 function default_password_nag_edit_user($user_ID, $old_data) {
   409 function default_password_nag_edit_user($user_ID, $old_data) {
   345 	if ( ! get_user_option('default_password_nag', $user_ID) ) //Short circuit it.
   410 	// Short-circuit it.
       
   411 	if ( ! get_user_option('default_password_nag', $user_ID) )
   346 		return;
   412 		return;
   347 
   413 
   348 	$new_data = get_userdata($user_ID);
   414 	$new_data = get_userdata($user_ID);
   349 
   415 
   350 	if ( $new_data->user_pass != $old_data->user_pass ) { //Remove the nag if the password has been changed.
   416 	// Remove the nag if the password has been changed.
       
   417 	if ( $new_data->user_pass != $old_data->user_pass ) {
   351 		delete_user_setting('default_password_nag');
   418 		delete_user_setting('default_password_nag');
   352 		update_user_option($user_ID, 'default_password_nag', false, true);
   419 		update_user_option($user_ID, 'default_password_nag', false, true);
   353 	}
   420 	}
   354 }
   421 }
   355 
   422 
   356 add_action('admin_notices', 'default_password_nag');
   423 add_action('admin_notices', 'default_password_nag');
       
   424 
   357 /**
   425 /**
   358  * @since 2.8.0
   426  * @since 2.8.0
   359  */
   427  */
   360 function default_password_nag() {
   428 function default_password_nag() {
   361 	global $pagenow;
   429 	global $pagenow;
   362 	if ( 'profile.php' == $pagenow || ! get_user_option('default_password_nag') ) //Short circuit it.
   430 	// Short-circuit it.
       
   431 	if ( 'profile.php' == $pagenow || ! get_user_option('default_password_nag') )
   363 		return;
   432 		return;
   364 
   433 
   365 	echo '<div class="error default-password-nag">';
   434 	echo '<div class="error default-password-nag">';
   366 	echo '<p>';
   435 	echo '<p>';
   367 	echo '<strong>' . __('Notice:') . '</strong> ';
   436 	echo '<strong>' . __('Notice:') . '</strong> ';
   368 	_e('You&rsquo;re using the auto-generated password for your account. Would you like to change it to something easier to remember?');
   437 	_e('You&rsquo;re using the auto-generated password for your account. Would you like to change it to something easier to remember?');
   369 	echo '</p><p>';
   438 	echo '</p><p>';
   370 	printf( '<a href="%s">' . __('Yes, take me to my profile page') . '</a> | ', get_edit_profile_url( get_current_user_id() ) . '#password' );
   439 	printf( '<a href="%s">' . __('Yes, take me to my profile page') . '</a> | ', get_edit_profile_url() . '#password' );
   371 	printf( '<a href="%s" id="default-password-nag-no">' . __('No thanks, do not remind me again') . '</a>', '?default_password_nag=0' );
   440 	printf( '<a href="%s" id="default-password-nag-no">' . __('No thanks, do not remind me again') . '</a>', '?default_password_nag=0' );
   372 	echo '</p></div>';
   441 	echo '</p></div>';
   373 }
   442 }