Yoast\WP\SEO\MyYoast_Client\User_Interface

Auth_Command::authorize_userprivateYoast 1.0

Handles the user authorization code flow.

Метод класса: Auth_Command{}

Хуков нет.

Возвращает

null. Ничего (null).

Использование

// private - только в коде основоного (родительского) класса
$result = $this->authorize_user( $assoc_args, $scopes, ?string $resource_indicator, $format ): void;
$assoc_args(массив) (обязательный)
.
$scopes(string[]) (обязательный)
The scopes to request.
?string $resource_indicator(обязательный)
.
$format(строка) (обязательный)
The output format.

Код Auth_Command::authorize_user() Yoast 28.3

private function authorize_user( array $assoc_args, array $scopes, ?string $resource_indicator, string $format ): void {
	$user_id = \get_current_user_id();
	if ( $user_id <= 0 ) {
		WP_CLI::error( 'User authorization requires the global --user flag.' );
	}

	$code  = Utils\get_flag_value( $assoc_args, 'code' );
	$state = Utils\get_flag_value( $assoc_args, 'state' );

	// Phase 2: exchange the code. The resource was persisted in the flow state during phase 1.
	if ( $code !== null && $state !== null ) {
		try {
			$token_set = $this->myyoast_client->exchange_authorization_code(
				$user_id,
				(string) $code,
				(string) $state,
			);
		} catch ( Exception $e ) {
			WP_CLI::error( 'Code exchange failed: ' . $e->getMessage() );
			return;
		}

		$this->output( $this->build_token_info( $token_set ), $format );

		WP_CLI::success( 'User authorized.' );
		return;
	}

	if ( $code !== null || $state !== null ) {
		WP_CLI::error( 'Both --code and --state are required for code exchange.' );
	}

	// Phase 1: generate the authorization URL. Registration is a prerequisite.
	if ( ! $this->myyoast_client->is_registered() ) {
		WP_CLI::error( 'Not registered. Run "wp yoast auth register" first.' );
	}

	try {
		$url = $this->myyoast_client->get_authorization_url( $user_id, $scopes, $resource_indicator );
	} catch ( Exception $e ) {
		WP_CLI::error( 'Failed to generate authorization URL: ' . $e->getMessage() );
		return;
	}

	if ( Utils\get_flag_value( $assoc_args, 'url-only', false ) ) {
		WP_CLI::log( $url );
		return;
	}

	WP_CLI::log( 'Visit this URL to authorize:' );
	WP_CLI::log( '' );
	WP_CLI::log( $url );
	WP_CLI::log( '' );
	WP_CLI::log( 'After authorizing, you will be redirected to a callback URL.' );
	WP_CLI::log( 'If you need to manually complete authorization, copy the "code" and "state" parameters from the URL, then run:' );
	WP_CLI::log( '' );
	WP_CLI::log(
		\sprintf(
			'  wp yoast auth authorize --user=%s --code=<CODE> --state=<STATE>',
			$user_id,
		),
	);
}