Yoast\WP\SEO\AI_Generator\Application

Suggestions_Provider::get_suggestions │ public │ Yoast 1.0

Устарела с версии 28.4. Больше не поддерживается и может быть удалена. Рекомендуется заменить эту функцию на аналог.

Method used to generate suggestions through AI.

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

Хуков нет.

Возвращает

string[]. The suggestions.

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

$Suggestions_Provider = new Suggestions_Provider();
$Suggestions_Provider->get_suggestions( $user, $suggestion_type, $prompt_content, $focus_keyphrase, $language, $platform, $editor, $retry_on_unauthorized ): array;
$user(WP_User) (обязательный)
The WP user.
$suggestion_type(строка) (обязательный)
The type of the requested suggestion.
$prompt_content(строка) (обязательный)
The excerpt taken from the post.
$focus_keyphrase(строка) (обязательный)
The focus keyphrase associated to the post.
$language(строка) (обязательный)
The language of the post.
$platform(строка) (обязательный)
The platform the post is intended for.
$editor(строка) (обязательный)
The current editor.
$retry_on_unauthorized(true|false)
Whether to retry when unauthorized (mechanism to retry once).
По умолчанию: true

Список изменений

Устарела с 28.4

Код Suggestions_Provider::get_suggestions() Yoast 28.7

public function get_suggestions(
	WP_User $user,
	string $suggestion_type,
	string $prompt_content,
	string $focus_keyphrase,
	string $language,
	string $platform,
	string $editor,
	bool $retry_on_unauthorized = true
): array {
	\_deprecated_function( __METHOD__, 'Yoast SEO 28.4' );
	try {
		$token = $this->token_manager->get_or_request_access_token( $user );
	} catch ( Forbidden_Exception $exception ) {
		// Follow the API in the consent being revoked (Use case: user sent an e-mail to revoke?).
		// phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped -- false positive.
		$this->consent_handler->revoke_consent( $user->ID );
		throw new Forbidden_Exception( 'CONSENT_REVOKED', $exception->getCode() );
		// phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped
	}

	$request_body    = [
		'service' => 'openai',
		'user_id' => (string) $user->ID,
		'subject' => [
			'content'         => $prompt_content,
			'focus_keyphrase' => $focus_keyphrase,
			'language'        => $language,
			'platform'        => $platform,
		],
	];
	$request_headers = [
		'Authorization' => "Bearer $token",
		'X-Yst-Cohort'  => $editor,
	];

	try {
		$response = $this->request_handler->handle( new Request( "/openai/suggestions/$suggestion_type", $request_body, $request_headers ) );
	} catch ( Unauthorized_Exception $exception ) {
		// Delete the stored JWT tokens, as they appear to be no longer valid.
		$this->user_helper->delete_meta( $user->ID, '_yoast_wpseo_ai_generator_access_jwt' );
		$this->user_helper->delete_meta( $user->ID, '_yoast_wpseo_ai_generator_refresh_jwt' );

		if ( ! $retry_on_unauthorized ) {
			throw $exception;
		}

		// Try again once more by fetching a new set of tokens and trying the suggestions endpoint again.
		return $this->get_suggestions( $user, $suggestion_type, $prompt_content, $focus_keyphrase, $language, $platform, $editor, false );
	} catch ( Forbidden_Exception $exception ) {
		// Follow the API in the consent being revoked (Use case: user sent an e-mail to revoke?).
		// phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped -- false positive.
		$this->consent_handler->revoke_consent( $user->ID );
		throw new Forbidden_Exception( 'CONSENT_REVOKED', $exception->getCode() );
		// phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped
	}

	return $this->build_suggestions_array( $response )->to_array();
}