PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.6
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.6
28.5 28.4 28.3 28.2 28.1 28.0 27.9 27.8 27.7 27.6 27.5 trunk 18.0 18.1 18.2 18.3 18.4 18.4.1 18.5 18.5.1 18.6 18.7 18.8 18.9 19.0 All 129 releases
wordpress-seo / src / ai / content-planner / application / content-outline-command-handler.php

content-outline-command-handler.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.6, at src/ai/content-planner/application/content-outline-command-handler.php

176 lines 5.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 // phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
3
4 namespace Yoast\WP\SEO\AI\Content_Planner\Application;
5
6 use Yoast\WP\SEO\AI\Authorization\Application\Token_Manager;
7 use Yoast\WP\SEO\AI\Consent\Application\Consent_Handler;
8 use Yoast\WP\SEO\AI\Content_Planner\Domain\Section;
9 use Yoast\WP\SEO\AI\Content_Planner\Domain\Section_List;
10 use Yoast\WP\SEO\AI\Content_Planner\Infrastructure\Recent_Content\Recent_Content_Collector;
11 use Yoast\WP\SEO\AI\HTTP_Request\Application\Request_Handler;
12 use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Forbidden_Exception;
13 use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Unauthorized_Exception;
14 use Yoast\WP\SEO\AI\HTTP_Request\Domain\Request;
15 use Yoast\WP\SEO\AI\HTTP_Request\Domain\Response;
16
17 /**
18 * Handles the content outline command.
19 */
20 class Content_Outline_Command_Handler {
21
22 /**
23 * The recent content collector.
24 *
25 * @var Recent_Content_Collector
26 */
27 private $recent_content_collector;
28
29 /**
30 * The token manager.
31 *
32 * @var Token_Manager
33 */
34 private $token_manager;
35
36 /**
37 * The request handler.
38 *
39 * @var Request_Handler
40 */
41 private $request_handler;
42
43 /**
44 * The consent handler.
45 *
46 * @var Consent_Handler
47 */
48 private $consent_handler;
49
50 /**
51 * The constructor.
52 *
53 * @param Recent_Content_Collector $recent_content_collector The recent content collector.
54 * @param Token_Manager $token_manager The token manager.
55 * @param Request_Handler $request_handler The request handler.
56 * @param Consent_Handler $consent_handler The consent handler.
57 */
58 public function __construct(
59 Recent_Content_Collector $recent_content_collector,
60 Token_Manager $token_manager,
61 Request_Handler $request_handler,
62 Consent_Handler $consent_handler
63 ) {
64 $this->recent_content_collector = $recent_content_collector;
65 $this->token_manager = $token_manager;
66 $this->request_handler = $request_handler;
67 $this->consent_handler = $consent_handler;
68 }
69
70 /**
71 * Handles the content outline command by collecting recent content and requesting an outline from the AI API.
72 *
73 * @param Content_Outline_Command $command The content outline command.
74 * @param bool $retry_on_unauthorized Whether to retry on unauthorized response.
75 *
76 * @throws Unauthorized_Exception When the API returns an unauthorized response and retry is exhausted.
77 * @throws Forbidden_Exception When consent has been revoked.
78 *
79 * @return Section_List A list of outline sections.
80 */
81 public function handle(
82 Content_Outline_Command $command,
83 bool $retry_on_unauthorized = true
84 ): Section_List {
85 $recent_content = $this->recent_content_collector->collect( $command->get_post_type() );
86 $about_page = $this->recent_content_collector->collect_about_page( $command->get_post_type() );
87 $token = $this->token_manager->get_or_request_access_token( $command->get_user() );
88 $recent_content = $recent_content->to_array();
89
90 $existing_posts = \array_map(
91 static function ( $post ) {
92 return [
93 'title' => $post['title'],
94 'description' => $post['description'],
95 ];
96 },
97 $recent_content,
98 );
99
100 $content = [
101 'new_post_metadata' => [
102 'title' => $command->get_title(),
103 'intent' => $command->get_intent(),
104 'explanation' => $command->get_explanation(),
105 'keyphrase' => $command->get_keyphrase(),
106 'meta_description' => $command->get_meta_description(),
107 'category' => $command->get_category()->to_array(),
108 ],
109 'existing_posts' => $existing_posts,
110 ];
111 if ( $about_page ) {
112 $content['about_page'] = $about_page;
113 }
114
115 $request_body = [
116 'subject' => [
117 'language' => $command->get_language(),
118 'content' => $content,
119 ],
120 ];
121
122 $request_headers = [
123 'Authorization' => "Bearer $token",
124 'X-Yst-Cohort' => $command->get_editor(),
125 ];
126
127 try {
128 $response = $this->request_handler->handle( new Request( '/content-planner/next-post-outline', $request_body, $request_headers ) );
129 } catch ( Unauthorized_Exception $exception ) {
130 // Delete the stored JWT tokens, as they appear to be no longer valid.
131 $this->token_manager->clear_tokens( (string) $command->get_user()->ID );
132
133 if ( ! $retry_on_unauthorized ) {
134 throw $exception;
135 }
136
137 // Try again once more by fetching a new set of tokens and trying the outline endpoint again.
138 return $this->handle( $command, false );
139 } catch ( Forbidden_Exception $exception ) {
140 // Follow the API in the consent being revoked (Use case: user sent an e-mail to revoke?).
141 // phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped -- false positive.
142 $this->consent_handler->revoke_consent( $command->get_user()->ID );
143 throw new Forbidden_Exception( 'CONSENT_REVOKED', $exception->getCode() );
144 // phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped
145 }
146
147 return $this->build_outline( $response );
148 }
149
150 /**
151 * Builds a list of outline sections from the API response.
152 *
153 * @param Response $response The API response.
154 *
155 * @return Section_List The list of outline sections.
156 */
157 private function build_outline( Response $response ): Section_List {
158 $section_list = new Section_List();
159 $json = \json_decode( $response->get_body() );
160
161 if ( $json === null || ! isset( $json->choices ) ) {
162 return $section_list;
163 }
164 foreach ( $json->choices as $choice ) {
165 $section_list->add(
166 new Section(
167 ( $choice->content_notes ?? [] ),
168 ( $choice->subheading_text ?? null ),
169 ),
170 );
171 }
172
173 return $section_list;
174 }
175 }
176