Yoast\WP\SEO\AI\Content_Planner\Application
Content_Suggestion_Command_Handler::handle
Handles the content suggestion command by collecting recent content and requesting suggestions from the AI API.
Метод класса: Content_Suggestion_Command_Handler{}
Хуков нет.
Возвращает
Content_Suggestion_List. A list of content suggestions.
Использование
$Content_Suggestion_Command_Handler = new Content_Suggestion_Command_Handler(); $Content_Suggestion_Command_Handler->handle( $command, $retry_on_unauthorized ): Content_Suggestion_List;
- $command(Content_Suggestion_Command) (обязательный)
- The content suggestion command.
- $retry_on_unauthorized(true|false)
- Whether to retry on unauthorized response.
По умолчанию:true
Код Content_Suggestion_Command_Handler::handle() Content Suggestion Command Handler::handle Yoast 27.7
public function handle(
Content_Suggestion_Command $command,
bool $retry_on_unauthorized = true
): Content_Suggestion_List {
$recent_content = $this->recent_content_collector->collect( $command->get_post_type() );
$about_page = $this->recent_content_collector->collect_about_page( $command->get_post_type() );
$token = $this->token_manager->get_or_request_access_token( $command->get_user() );
$recent_content = $recent_content->to_array();
$content = [
'posts' => $recent_content,
];
if ( $about_page ) {
$content['about_page'] = $about_page;
}
$request_body = [
'subject' => [
'language' => $command->get_language(),
'content' => $content,
],
];
$request_headers = [
'Authorization' => "Bearer $token",
'X-Yst-Cohort' => $command->get_editor(),
];
try {
$response = $this->request_handler->handle( new Request( '/content-planner/next-post-suggestions', $request_body, $request_headers ) );
} catch ( Unauthorized_Exception $exception ) {
// Delete the stored JWT tokens, as they appear to be no longer valid.
$this->token_manager->clear_tokens( (string) $command->get_user()->ID );
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->handle( $command, 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( $command->get_user()->ID );
throw new Forbidden_Exception( 'CONSENT_REVOKED', $exception->getCode() );
// phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped
}
return $this->build_suggestions_array( $response );
}