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, $format ): void;
$assoc_args(массив) (обязательный)
.
$scopes(string[]) (обязательный)
The scopes to request.
$format(строка) (обязательный)
The output format.

Код Auth_Command::authorize_user() Yoast 27.7

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

	$has_code  = isset( $assoc_args['code'] );
	$has_state = isset( $assoc_args['state'] );

	// Phase 2: exchange the code.
	if ( $has_code && $has_state ) {
		try {
			$token_set = $this->myyoast_client->exchange_authorization_code(
				$user_id,
				$assoc_args['code'],
				$assoc_args['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 ( $has_code || $has_state ) {
		WP_CLI::error( 'Both --code and --state are required for code exchange.' );
	}

	// Phase 1: generate the authorization URL.
	$redirect_uri = \get_admin_url( null, 'admin.php?page=' . General_Page_Integration::PAGE . '&yoast_myyoast_oauth_callback=1' );

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

	if ( isset( $assoc_args['url-only'] ) ) {
		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,
		),
	);
}