WP_REST_Application_Passwords_Controller::get_user()protectedWP 5.6.0

Gets the requested user.

Method of the class: WP_REST_Application_Passwords_Controller{}

No Hooks.

Return

WP_User|WP_Error. The WordPress user associated with the request, or a WP_Error if none found.

Usage

// protected - for code of main (parent) or child class
$result = $this->get_user( $request );
$request(WP_REST_Request) (required)
The request object.

Changelog

Since 5.6.0 Introduced.

WP_REST_Application_Passwords_Controller::get_user() code WP 6.5.2

protected function get_user( $request ) {
	if ( ! wp_is_application_passwords_available() ) {
		return new WP_Error(
			'application_passwords_disabled',
			__( 'Application passwords are not available.' ),
			array( 'status' => 501 )
		);
	}

	$error = new WP_Error(
		'rest_user_invalid_id',
		__( 'Invalid user ID.' ),
		array( 'status' => 404 )
	);

	$id = $request['user_id'];

	if ( 'me' === $id ) {
		if ( ! is_user_logged_in() ) {
			return new WP_Error(
				'rest_not_logged_in',
				__( 'You are not currently logged in.' ),
				array( 'status' => 401 )
			);
		}

		$user = wp_get_current_user();
	} else {
		$id = (int) $id;

		if ( $id <= 0 ) {
			return $error;
		}

		$user = get_userdata( $id );
	}

	if ( empty( $user ) || ! $user->exists() ) {
		return $error;
	}

	if ( is_multisite() && ! user_can( $user->ID, 'manage_sites' ) && ! is_user_member_of_blog( $user->ID ) ) {
		return $error;
	}

	if ( ! wp_is_application_passwords_available_for_user( $user ) ) {
		return new WP_Error(
			'application_passwords_disabled_for_user',
			__( 'Application passwords are not available for your account. Please contact the site administrator for assistance.' ),
			array( 'status' => 501 )
		);
	}

	return $user;
}