PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.8
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.8
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-generator / application / suggestions-provider.php

suggestions-provider.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.8, at src/ai-generator/application/suggestions-provider.php

177 lines 6.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace Yoast\WP\SEO\AI_Generator\Application;
4
5 use RuntimeException;
6 use WP_User;
7 use Yoast\WP\SEO\AI_Authorization\Application\Token_Manager;
8 use Yoast\WP\SEO\AI_Consent\Application\Consent_Handler;
9 use Yoast\WP\SEO\AI_Generator\Domain\Suggestion;
10 use Yoast\WP\SEO\AI_Generator\Domain\Suggestions_Bucket;
11 use Yoast\WP\SEO\AI_HTTP_Request\Application\Request_Handler;
12 use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Bad_Request_Exception;
13 use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Forbidden_Exception;
14 use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Internal_Server_Error_Exception;
15 use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Not_Found_Exception;
16 use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Payment_Required_Exception;
17 use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Request_Timeout_Exception;
18 use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Service_Unavailable_Exception;
19 use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Too_Many_Requests_Exception;
20 use Yoast\WP\SEO\AI_HTTP_Request\Domain\Exceptions\Unauthorized_Exception;
21 use Yoast\WP\SEO\AI_HTTP_Request\Domain\Request;
22 use Yoast\WP\SEO\AI_HTTP_Request\Domain\Response;
23 use Yoast\WP\SEO\Helpers\User_Helper;
24
25 /**
26 * The class that handles the suggestions from the AI API.
27 */
28 class Suggestions_Provider {
29
30 /**
31 * The consent handler instance.
32 *
33 * @var Consent_Handler
34 */
35 private $consent_handler;
36
37 /**
38 * The request handler instance.
39 *
40 * @var Request_Handler
41 */
42 private $request_handler;
43
44 /**
45 * The token manager instance.
46 *
47 * @var Token_Manager
48 */
49 private $token_manager;
50
51 /**
52 * The user helper instance.
53 *
54 * @var User_Helper
55 */
56 private $user_helper;
57
58 /**
59 * Class constructor.
60 *
61 * @param Consent_Handler $consent_handler The consent handler instance.
62 * @param Request_Handler $request_handler The request handler instance.
63 * @param Token_Manager $token_manager The token manager instance.
64 * @param User_Helper $user_helper The user helper instance.
65 */
66 public function __construct(
67 Consent_Handler $consent_handler,
68 Request_Handler $request_handler,
69 Token_Manager $token_manager,
70 User_Helper $user_helper
71 ) {
72 $this->consent_handler = $consent_handler;
73 $this->request_handler = $request_handler;
74 $this->token_manager = $token_manager;
75 $this->user_helper = $user_helper;
76 }
77
78 // phpcs:disable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber -- PHPCS doesn't take into account exceptions thrown in called methods.
79
80 /**
81 * Method used to generate suggestions through AI.
82 *
83 * @param WP_User $user The WP user.
84 * @param string $suggestion_type The type of the requested suggestion.
85 * @param string $prompt_content The excerpt taken from the post.
86 * @param string $focus_keyphrase The focus keyphrase associated to the post.
87 * @param string $language The language of the post.
88 * @param string $platform The platform the post is intended for.
89 * @param string $editor The current editor.
90 * @param bool $retry_on_unauthorized Whether to retry when unauthorized (mechanism to retry once).
91 *
92 * @throws Bad_Request_Exception Bad_Request_Exception.
93 * @throws Forbidden_Exception Forbidden_Exception.
94 * @throws Internal_Server_Error_Exception Internal_Server_Error_Exception.
95 * @throws Not_Found_Exception Not_Found_Exception.
96 * @throws Payment_Required_Exception Payment_Required_Exception.
97 * @throws Request_Timeout_Exception Request_Timeout_Exception.
98 * @throws Service_Unavailable_Exception Service_Unavailable_Exception.
99 * @throws Too_Many_Requests_Exception Too_Many_Requests_Exception.
100 * @throws Unauthorized_Exception Unauthorized_Exception.
101 * @throws RuntimeException Unable to retrieve the access token.
102 * @return string[] The suggestions.
103 */
104 public function get_suggestions(
105 WP_User $user,
106 string $suggestion_type,
107 string $prompt_content,
108 string $focus_keyphrase,
109 string $language,
110 string $platform,
111 string $editor,
112 bool $retry_on_unauthorized = true
113 ): array {
114 $token = $this->token_manager->get_or_request_access_token( $user );
115
116 $request_body = [
117 'service' => 'openai',
118 'user_id' => (string) $user->ID,
119 'subject' => [
120 'content' => $prompt_content,
121 'focus_keyphrase' => $focus_keyphrase,
122 'language' => $language,
123 'platform' => $platform,
124 ],
125 ];
126 $request_headers = [
127 'Authorization' => "Bearer $token",
128 'X-Yst-Cohort' => $editor,
129 ];
130
131 try {
132 $response = $this->request_handler->handle( new Request( "/openai/suggestions/$suggestion_type", $request_body, $request_headers ) );
133 } catch ( Unauthorized_Exception $exception ) {
134 // Delete the stored JWT tokens, as they appear to be no longer valid.
135 $this->user_helper->delete_meta( $user->ID, '_yoast_wpseo_ai_generator_access_jwt' );
136 $this->user_helper->delete_meta( $user->ID, '_yoast_wpseo_ai_generator_refresh_jwt' );
137
138 if ( ! $retry_on_unauthorized ) {
139 throw $exception;
140 }
141
142 // Try again once more by fetching a new set of tokens and trying the suggestions endpoint again.
143 return $this->get_suggestions( $user, $suggestion_type, $prompt_content, $focus_keyphrase, $language, $platform, $editor, false );
144 } catch ( Forbidden_Exception $exception ) {
145 // Follow the API in the consent being revoked (Use case: user sent an e-mail to revoke?).
146 // phpcs:disable WordPress.Security.EscapeOutput.ExceptionNotEscaped -- false positive.
147 $this->consent_handler->revoke_consent( $user->ID );
148 throw new Forbidden_Exception( 'CONSENT_REVOKED', $exception->getCode() );
149 // phpcs:enable WordPress.Security.EscapeOutput.ExceptionNotEscaped
150 }
151
152 return $this->build_suggestions_array( $response )->to_array();
153 }
154
155 // phpcs:enable Squiz.Commenting.FunctionCommentThrowTag.WrongNumber
156
157 /**
158 * Generates the list of 5 suggestions to return.
159 *
160 * @param Response $response The response from the API.
161 *
162 * @return Suggestions_Bucket The array of suggestions.
163 */
164 public function build_suggestions_array( Response $response ): Suggestions_Bucket {
165 $suggestions_bucket = new Suggestions_Bucket();
166 $json = \json_decode( $response->get_body() );
167 if ( $json === null || ! isset( $json->choices ) ) {
168 return $suggestions_bucket;
169 }
170 foreach ( $json->choices as $suggestion ) {
171 $suggestions_bucket->add_suggestion( new Suggestion( $suggestion->text ) );
172 }
173
174 return $suggestions_bucket;
175 }
176 }
177