wp/wp-admin/includes/user.php
changeset 21 48c4eec2b7e6
parent 19 3d72ae0968f4
child 22 8c2e4d02f4ef
equal deleted inserted replaced
20:7b1b88e27a20 21:48c4eec2b7e6
    27  * @param int $user_id Optional. User ID.
    27  * @param int $user_id Optional. User ID.
    28  * @return int|WP_Error User ID of the updated user or WP_Error on failure.
    28  * @return int|WP_Error User ID of the updated user or WP_Error on failure.
    29  */
    29  */
    30 function edit_user( $user_id = 0 ) {
    30 function edit_user( $user_id = 0 ) {
    31 	$wp_roles = wp_roles();
    31 	$wp_roles = wp_roles();
    32 	$user     = new stdClass;
    32 	$user     = new stdClass();
    33 	$user_id  = (int) $user_id;
    33 	$user_id  = (int) $user_id;
    34 	if ( $user_id ) {
    34 	if ( $user_id ) {
    35 		$update           = true;
    35 		$update           = true;
    36 		$user->ID         = $user_id;
    36 		$user->ID         = $user_id;
    37 		$userdata         = get_userdata( $user_id );
    37 		$userdata         = get_userdata( $user_id );
    82 	}
    82 	}
    83 	if ( isset( $_POST['url'] ) ) {
    83 	if ( isset( $_POST['url'] ) ) {
    84 		if ( empty( $_POST['url'] ) || 'http://' === $_POST['url'] ) {
    84 		if ( empty( $_POST['url'] ) || 'http://' === $_POST['url'] ) {
    85 			$user->user_url = '';
    85 			$user->user_url = '';
    86 		} else {
    86 		} else {
    87 			$user->user_url = esc_url_raw( $_POST['url'] );
    87 			$user->user_url = sanitize_url( $_POST['url'] );
    88 			$protocols      = implode( '|', array_map( 'preg_quote', wp_allowed_protocols() ) );
    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;
    89 			$user->user_url = preg_match( '/^(' . $protocols . '):/is', $user->user_url ) ? $user->user_url : 'http://' . $user->user_url;
    90 		}
    90 		}
    91 	}
    91 	}
    92 	if ( isset( $_POST['first_name'] ) ) {
    92 	if ( isset( $_POST['first_name'] ) ) {
   117 		if ( 'site-default' === $locale ) {
   117 		if ( 'site-default' === $locale ) {
   118 			$locale = '';
   118 			$locale = '';
   119 		} elseif ( '' === $locale ) {
   119 		} elseif ( '' === $locale ) {
   120 			$locale = 'en_US';
   120 			$locale = 'en_US';
   121 		} elseif ( ! in_array( $locale, get_available_languages(), true ) ) {
   121 		} elseif ( ! in_array( $locale, get_available_languages(), true ) ) {
   122 			$locale = '';
   122 			if ( current_user_can( 'install_languages' ) && wp_can_install_language_pack() ) {
       
   123 				if ( ! wp_download_language_pack( $locale ) ) {
       
   124 					$locale = '';
       
   125 				}
       
   126 			} else {
       
   127 				$locale = '';
       
   128 			}
   123 		}
   129 		}
   124 
   130 
   125 		$user->locale = $locale;
   131 		$user->locale = $locale;
   126 	}
   132 	}
   127 
   133 
   141 
   147 
   142 	$errors = new WP_Error();
   148 	$errors = new WP_Error();
   143 
   149 
   144 	/* checking that username has been typed */
   150 	/* checking that username has been typed */
   145 	if ( '' === $user->user_login ) {
   151 	if ( '' === $user->user_login ) {
   146 		$errors->add( 'user_login', __( '<strong>Error</strong>: Please enter a username.' ) );
   152 		$errors->add( 'user_login', __( '<strong>Error:</strong> Please enter a username.' ) );
   147 	}
   153 	}
   148 
   154 
   149 	/* checking that nickname has been typed */
   155 	/* checking that nickname has been typed */
   150 	if ( $update && empty( $user->nickname ) ) {
   156 	if ( $update && empty( $user->nickname ) ) {
   151 		$errors->add( 'nickname', __( '<strong>Error</strong>: Please enter a nickname.' ) );
   157 		$errors->add( 'nickname', __( '<strong>Error:</strong> Please enter a nickname.' ) );
   152 	}
   158 	}
   153 
   159 
   154 	/**
   160 	/**
   155 	 * Fires before the password and confirm password fields are checked for congruity.
   161 	 * Fires before the password and confirm password fields are checked for congruity.
   156 	 *
   162 	 *
   162 	 */
   168 	 */
   163 	do_action_ref_array( 'check_passwords', array( $user->user_login, &$pass1, &$pass2 ) );
   169 	do_action_ref_array( 'check_passwords', array( $user->user_login, &$pass1, &$pass2 ) );
   164 
   170 
   165 	// Check for blank password when adding a user.
   171 	// Check for blank password when adding a user.
   166 	if ( ! $update && empty( $pass1 ) ) {
   172 	if ( ! $update && empty( $pass1 ) ) {
   167 		$errors->add( 'pass', __( '<strong>Error</strong>: Please enter a password.' ), array( 'form-field' => 'pass1' ) );
   173 		$errors->add( 'pass', __( '<strong>Error:</strong> Please enter a password.' ), array( 'form-field' => 'pass1' ) );
   168 	}
   174 	}
   169 
   175 
   170 	// Check for "\" in password.
   176 	// Check for "\" in password.
   171 	if ( false !== strpos( wp_unslash( $pass1 ), '\\' ) ) {
   177 	if ( str_contains( wp_unslash( $pass1 ), '\\' ) ) {
   172 		$errors->add( 'pass', __( '<strong>Error</strong>: Passwords may not contain the character "\\".' ), array( 'form-field' => 'pass1' ) );
   178 		$errors->add( 'pass', __( '<strong>Error:</strong> Passwords may not contain the character "\\".' ), array( 'form-field' => 'pass1' ) );
   173 	}
   179 	}
   174 
   180 
   175 	// Checking the password has been typed twice the same.
   181 	// Checking the password has been typed twice the same.
   176 	if ( ( $update || ! empty( $pass1 ) ) && $pass1 != $pass2 ) {
   182 	if ( ( $update || ! empty( $pass1 ) ) && $pass1 !== $pass2 ) {
   177 		$errors->add( 'pass', __( '<strong>Error</strong>: Passwords do not match. Please enter the same password in both password fields.' ), array( 'form-field' => 'pass1' ) );
   183 		$errors->add( 'pass', __( '<strong>Error:</strong> Passwords do not match. Please enter the same password in both password fields.' ), array( 'form-field' => 'pass1' ) );
   178 	}
   184 	}
   179 
   185 
   180 	if ( ! empty( $pass1 ) ) {
   186 	if ( ! empty( $pass1 ) ) {
   181 		$user->user_pass = $pass1;
   187 		$user->user_pass = $pass1;
   182 	}
   188 	}
   183 
   189 
   184 	if ( ! $update && isset( $_POST['user_login'] ) && ! validate_username( $_POST['user_login'] ) ) {
   190 	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.' ) );
   191 		$errors->add( 'user_login', __( '<strong>Error:</strong> This username is invalid because it uses illegal characters. Please enter a valid username.' ) );
   186 	}
   192 	}
   187 
   193 
   188 	if ( ! $update && username_exists( $user->user_login ) ) {
   194 	if ( ! $update && username_exists( $user->user_login ) ) {
   189 		$errors->add( 'user_login', __( '<strong>Error</strong>: This username is already registered. Please choose another one.' ) );
   195 		$errors->add( 'user_login', __( '<strong>Error:</strong> This username is already registered. Please choose another one.' ) );
   190 	}
   196 	}
   191 
   197 
   192 	/** This filter is documented in wp-includes/user.php */
   198 	/** This filter is documented in wp-includes/user.php */
   193 	$illegal_logins = (array) apply_filters( 'illegal_user_logins', array() );
   199 	$illegal_logins = (array) apply_filters( 'illegal_user_logins', array() );
   194 
   200 
   195 	if ( in_array( strtolower( $user->user_login ), array_map( 'strtolower', $illegal_logins ), true ) ) {
   201 	if ( in_array( strtolower( $user->user_login ), array_map( 'strtolower', $illegal_logins ), true ) ) {
   196 		$errors->add( 'invalid_username', __( '<strong>Error</strong>: Sorry, that username is not allowed.' ) );
   202 		$errors->add( 'invalid_username', __( '<strong>Error:</strong> Sorry, that username is not allowed.' ) );
   197 	}
   203 	}
   198 
   204 
   199 	/* checking email address */
   205 	// Checking email address.
   200 	if ( empty( $user->user_email ) ) {
   206 	if ( empty( $user->user_email ) ) {
   201 		$errors->add( 'empty_email', __( '<strong>Error</strong>: Please enter an email address.' ), array( 'form-field' => 'email' ) );
   207 		$errors->add( 'empty_email', __( '<strong>Error:</strong> Please enter an email address.' ), array( 'form-field' => 'email' ) );
   202 	} elseif ( ! is_email( $user->user_email ) ) {
   208 	} elseif ( ! is_email( $user->user_email ) ) {
   203 		$errors->add( 'invalid_email', __( '<strong>Error</strong>: The email address is not correct.' ), array( 'form-field' => 'email' ) );
   209 		$errors->add( 'invalid_email', __( '<strong>Error:</strong> The email address is not correct.' ), array( 'form-field' => 'email' ) );
   204 	} else {
   210 	} else {
   205 		$owner_id = email_exists( $user->user_email );
   211 		$owner_id = email_exists( $user->user_email );
   206 		if ( $owner_id && ( ! $update || ( $owner_id != $user->ID ) ) ) {
   212 		if ( $owner_id && ( ! $update || ( $owner_id !== $user->ID ) ) ) {
   207 			$errors->add( 'email_exists', __( '<strong>Error</strong>: This email is already registered. Please choose another one.' ), array( 'form-field' => 'email' ) );
   213 			$errors->add( 'email_exists', __( '<strong>Error:</strong> This email is already registered. Please choose another one.' ), array( 'form-field' => 'email' ) );
   208 		}
   214 		}
   209 	}
   215 	}
   210 
   216 
   211 	/**
   217 	/**
   212 	 * Fires before user profile update errors are returned.
   218 	 * Fires before user profile update errors are returned.
   316 	$query = apply_filters( 'get_users_drafts', $query );
   322 	$query = apply_filters( 'get_users_drafts', $query );
   317 	return $wpdb->get_results( $query );
   323 	return $wpdb->get_results( $query );
   318 }
   324 }
   319 
   325 
   320 /**
   326 /**
   321  * Remove user and optionally reassign posts and links to another user.
   327  * Delete user and optionally reassign posts and links to another user.
   322  *
   328  *
   323  * If the $reassign parameter is not assigned to a User ID, then all posts will
   329  * Note that on a Multisite installation the user only gets removed from the site
   324  * be deleted of that user. The action {@see 'delete_user'} that is passed the User ID
   330  * and does not get deleted from the database.
       
   331  *
       
   332  * If the `$reassign` parameter is not assigned to a user ID, then all posts will
       
   333  * be deleted of that user. The action {@see 'delete_user'} that is passed the user ID
   325  * being deleted will be run after the posts are either reassigned or deleted.
   334  * being deleted will be run after the posts are either reassigned or deleted.
   326  * The user meta will also be deleted that are for that User ID.
   335  * The user meta will also be deleted that are for that user ID.
   327  *
   336  *
   328  * @since 2.0.0
   337  * @since 2.0.0
   329  *
   338  *
   330  * @global wpdb $wpdb WordPress database abstraction object.
   339  * @global wpdb $wpdb WordPress database abstraction object.
   331  *
   340  *
   332  * @param int $id User ID.
   341  * @param int $id       User ID.
   333  * @param int $reassign Optional. Reassign posts and links to new User ID.
   342  * @param int $reassign Optional. Reassign posts and links to new User ID.
   334  * @return bool True when finished.
   343  * @return bool True when finished.
   335  */
   344  */
   336 function wp_delete_user( $id, $reassign = null ) {
   345 function wp_delete_user( $id, $reassign = null ) {
   337 	global $wpdb;
   346 	global $wpdb;
   353 	} elseif ( null !== $reassign ) {
   362 	} elseif ( null !== $reassign ) {
   354 		$reassign = (int) $reassign;
   363 		$reassign = (int) $reassign;
   355 	}
   364 	}
   356 
   365 
   357 	/**
   366 	/**
   358 	 * Fires immediately before a user is deleted from the database.
   367 	 * Fires immediately before a user is deleted from the site.
       
   368 	 *
       
   369 	 * Note that on a Multisite installation the user only gets removed from the site
       
   370 	 * and does not get deleted from the database.
   359 	 *
   371 	 *
   360 	 * @since 2.0.0
   372 	 * @since 2.0.0
   361 	 * @since 5.5.0 Added the `$user` parameter.
   373 	 * @since 5.5.0 Added the `$user` parameter.
   362 	 *
   374 	 *
   363 	 * @param int      $id       ID of the user to delete.
   375 	 * @param int      $id       ID of the user to delete.
   432 	}
   444 	}
   433 
   445 
   434 	clean_user_cache( $user );
   446 	clean_user_cache( $user );
   435 
   447 
   436 	/**
   448 	/**
   437 	 * Fires immediately after a user is deleted from the database.
   449 	 * Fires immediately after a user is deleted from the site.
       
   450 	 *
       
   451 	 * Note that on a Multisite installation the user may not have been deleted from
       
   452 	 * the database depending on whether `wp_delete_user()` or `wpmu_delete_user()`
       
   453 	 * was called.
   438 	 *
   454 	 *
   439 	 * @since 2.9.0
   455 	 * @since 2.9.0
   440 	 * @since 5.5.0 Added the `$user` parameter.
   456 	 * @since 5.5.0 Added the `$user` parameter.
   441 	 *
   457 	 *
   442 	 * @param int      $id       ID of the deleted user.
   458 	 * @param int      $id       ID of the deleted user.
   477 		return;
   493 		return;
   478 	}
   494 	}
   479 
   495 
   480 	// get_user_setting() = JS-saved UI setting. Else no-js-fallback code.
   496 	// get_user_setting() = JS-saved UI setting. Else no-js-fallback code.
   481 	if ( 'hide' === get_user_setting( 'default_password_nag' )
   497 	if ( 'hide' === get_user_setting( 'default_password_nag' )
   482 		|| isset( $_GET['default_password_nag'] ) && '0' == $_GET['default_password_nag']
   498 		|| isset( $_GET['default_password_nag'] ) && '0' === $_GET['default_password_nag']
   483 	) {
   499 	) {
   484 		delete_user_setting( 'default_password_nag' );
   500 		delete_user_setting( 'default_password_nag' );
   485 		update_user_meta( $user_ID, 'default_password_nag', false );
   501 		update_user_meta( $user_ID, 'default_password_nag', false );
   486 	}
   502 	}
   487 }
   503 }
   499 	}
   515 	}
   500 
   516 
   501 	$new_data = get_userdata( $user_ID );
   517 	$new_data = get_userdata( $user_ID );
   502 
   518 
   503 	// Remove the nag if the password has been changed.
   519 	// Remove the nag if the password has been changed.
   504 	if ( $new_data->user_pass != $old_data->user_pass ) {
   520 	if ( $new_data->user_pass !== $old_data->user_pass ) {
   505 		delete_user_setting( 'default_password_nag' );
   521 		delete_user_setting( 'default_password_nag' );
   506 		update_user_meta( $user_ID, 'default_password_nag', false );
   522 		update_user_meta( $user_ID, 'default_password_nag', false );
   507 	}
   523 	}
   508 }
   524 }
   509 
   525 
   518 	// Short-circuit it.
   534 	// Short-circuit it.
   519 	if ( 'profile.php' === $pagenow || ! get_user_option( 'default_password_nag' ) ) {
   535 	if ( 'profile.php' === $pagenow || ! get_user_option( 'default_password_nag' ) ) {
   520 		return;
   536 		return;
   521 	}
   537 	}
   522 
   538 
   523 	echo '<div class="error default-password-nag">';
   539 	$default_password_nag_message  = sprintf(
   524 	echo '<p>';
   540 		'<p><strong>%1$s</strong> %2$s</p>',
   525 	echo '<strong>' . __( 'Notice:' ) . '</strong> ';
   541 		__( 'Notice:' ),
   526 	_e( 'You&rsquo;re using the auto-generated password for your account. Would you like to change it?' );
   542 		__( 'You are using the auto-generated password for your account. Would you like to change it?' )
   527 	echo '</p><p>';
   543 	);
   528 	printf( '<a href="%s">' . __( 'Yes, take me to my profile page' ) . '</a> | ', get_edit_profile_url() . '#password' );
   544 	$default_password_nag_message .= sprintf(
   529 	printf( '<a href="%s" id="default-password-nag-no">' . __( 'No thanks, do not remind me again' ) . '</a>', '?default_password_nag=0' );
   545 		'<p><a href="%1$s">%2$s</a> | ',
   530 	echo '</p></div>';
   546 		esc_url( get_edit_profile_url() . '#password' ),
       
   547 		__( 'Yes, take me to my profile page' )
       
   548 	);
       
   549 	$default_password_nag_message .= sprintf(
       
   550 		'<a href="%1$s" id="default-password-nag-no">%2$s</a></p>',
       
   551 		'?default_password_nag=0',
       
   552 		__( 'No thanks, do not remind me again' )
       
   553 	);
       
   554 
       
   555 	wp_admin_notice(
       
   556 		$default_password_nag_message,
       
   557 		array(
       
   558 			'additional_classes' => array( 'error', 'default-password-nag' ),
       
   559 			'paragraph_wrap'     => false,
       
   560 		)
       
   561 	);
   531 }
   562 }
   532 
   563 
   533 /**
   564 /**
   534  * @since 3.5.0
   565  * @since 3.5.0
   535  * @access private
   566  * @access private
   604 
   635 
   605 /**
   636 /**
   606  * Checks if the Authorize Application Password request is valid.
   637  * Checks if the Authorize Application Password request is valid.
   607  *
   638  *
   608  * @since 5.6.0
   639  * @since 5.6.0
       
   640  * @since 6.2.0 Allow insecure HTTP connections for the local environment.
       
   641  * @since 6.3.2 Validates the success and reject URLs to prevent `javascript` pseudo protocol from being executed.
   609  *
   642  *
   610  * @param array   $request {
   643  * @param array   $request {
   611  *     The array of request data. All arguments are optional and may be empty.
   644  *     The array of request data. All arguments are optional and may be empty.
   612  *
   645  *
   613  *     @type string $app_name    The suggested name of the application.
   646  *     @type string $app_name    The suggested name of the application.
   619  * @return true|WP_Error True if the request is valid, a WP_Error object contains errors if not.
   652  * @return true|WP_Error True if the request is valid, a WP_Error object contains errors if not.
   620  */
   653  */
   621 function wp_is_authorize_application_password_request_valid( $request, $user ) {
   654 function wp_is_authorize_application_password_request_valid( $request, $user ) {
   622 	$error = new WP_Error();
   655 	$error = new WP_Error();
   623 
   656 
   624 	if ( ! empty( $request['success_url'] ) ) {
   657 	if ( isset( $request['success_url'] ) ) {
   625 		$scheme = wp_parse_url( $request['success_url'], PHP_URL_SCHEME );
   658 		$validated_success_url = wp_is_authorize_application_redirect_url_valid( $request['success_url'] );
   626 
   659 		if ( is_wp_error( $validated_success_url ) ) {
   627 		if ( 'http' === $scheme ) {
       
   628 			$error->add(
   660 			$error->add(
   629 				'invalid_redirect_scheme',
   661 				$validated_success_url->get_error_code(),
   630 				__( 'The success URL must be served over a secure connection.' )
   662 				$validated_success_url->get_error_message()
   631 			);
   663 			);
   632 		}
   664 		}
   633 	}
   665 	}
   634 
   666 
   635 	if ( ! empty( $request['reject_url'] ) ) {
   667 	if ( isset( $request['reject_url'] ) ) {
   636 		$scheme = wp_parse_url( $request['reject_url'], PHP_URL_SCHEME );
   668 		$validated_reject_url = wp_is_authorize_application_redirect_url_valid( $request['reject_url'] );
   637 
   669 		if ( is_wp_error( $validated_reject_url ) ) {
   638 		if ( 'http' === $scheme ) {
       
   639 			$error->add(
   670 			$error->add(
   640 				'invalid_redirect_scheme',
   671 				$validated_reject_url->get_error_code(),
   641 				__( 'The rejection URL must be served over a secure connection.' )
   672 				$validated_reject_url->get_error_message()
   642 			);
   673 			);
   643 		}
   674 		}
   644 	}
   675 	}
   645 
   676 
   646 	if ( ! empty( $request['app_id'] ) && ! wp_is_uuid( $request['app_id'] ) ) {
   677 	if ( ! empty( $request['app_id'] ) && ! wp_is_uuid( $request['app_id'] ) ) {
   665 		return $error;
   696 		return $error;
   666 	}
   697 	}
   667 
   698 
   668 	return true;
   699 	return true;
   669 }
   700 }
       
   701 
       
   702 /**
       
   703  * Validates the redirect URL protocol scheme. The protocol can be anything except `http` and `javascript`.
       
   704  *
       
   705  * @since 6.3.2
       
   706  *
       
   707  * @param string $url The redirect URL to be validated.
       
   708  * @return true|WP_Error True if the redirect URL is valid, a WP_Error object otherwise.
       
   709  */
       
   710 function wp_is_authorize_application_redirect_url_valid( $url ) {
       
   711 	$bad_protocols = array( 'javascript', 'data' );
       
   712 	if ( empty( $url ) ) {
       
   713 		return true;
       
   714 	}
       
   715 
       
   716 	// Based on https://www.rfc-editor.org/rfc/rfc2396#section-3.1
       
   717 	$valid_scheme_regex = '/^[a-zA-Z][a-zA-Z0-9+.-]*:/';
       
   718 	if ( ! preg_match( $valid_scheme_regex, $url ) ) {
       
   719 		return new WP_Error(
       
   720 			'invalid_redirect_url_format',
       
   721 			__( 'Invalid URL format.' )
       
   722 		);
       
   723 	}
       
   724 
       
   725 	/**
       
   726 	 * Filters the list of invalid protocols used in applications redirect URLs.
       
   727 	 *
       
   728 	 * @since 6.3.2
       
   729 	 *
       
   730 	 * @param string[] $bad_protocols Array of invalid protocols.
       
   731 	 * @param string   $url The redirect URL to be validated.
       
   732 	 */
       
   733 	$invalid_protocols = apply_filters( 'wp_authorize_application_redirect_url_invalid_protocols', $bad_protocols, $url );
       
   734 	$invalid_protocols = array_map( 'strtolower', $invalid_protocols );
       
   735 
       
   736 	$scheme   = wp_parse_url( $url, PHP_URL_SCHEME );
       
   737 	$host     = wp_parse_url( $url, PHP_URL_HOST );
       
   738 	$is_local = 'local' === wp_get_environment_type();
       
   739 
       
   740 	// Validates if the proper URI format is applied to the URL.
       
   741 	if ( empty( $host ) || empty( $scheme ) || in_array( strtolower( $scheme ), $invalid_protocols, true ) ) {
       
   742 		return new WP_Error(
       
   743 			'invalid_redirect_url_format',
       
   744 			__( 'Invalid URL format.' )
       
   745 		);
       
   746 	}
       
   747 
       
   748 	if ( 'http' === $scheme && ! $is_local ) {
       
   749 		return new WP_Error(
       
   750 			'invalid_redirect_scheme',
       
   751 			__( 'The URL must be served over a secure connection.' )
       
   752 		);
       
   753 	}
       
   754 
       
   755 	return true;
       
   756 }