PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / 27.9
Yoast SEO – Advanced SEO with real-time guidance and built-in AI v27.9
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 / user-interface / get-suggestions-route.php

get-suggestions-route.php in Yoast SEO – Advanced SEO with real-time guidance and built-in AI 27.9, at src/ai/generator/user-interface/get-suggestions-route.php

175 lines 4.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // phpcs:disable Yoast.NamingConventions.NamespaceName.TooLong -- Needed in the folder structure.
4 namespace Yoast\WP\SEO\AI\Generator\User_Interface;
5
6 use RuntimeException;
7 use WP_REST_Request;
8 use WP_REST_Response;
9 use Yoast\WP\SEO\AI\Generator\Application\Suggestions_Provider;
10 use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Payment_Required_Exception;
11 use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Remote_Request_Exception;
12 use Yoast\WP\SEO\AI\HTTP_Request\Domain\Exceptions\Too_Many_Requests_Exception;
13 use Yoast\WP\SEO\Conditionals\AI_Conditional;
14 use Yoast\WP\SEO\Conditionals\New_Premium_Or_Free_AI_Conditional;
15 use Yoast\WP\SEO\Main;
16 use Yoast\WP\SEO\Routes\Route_Interface;
17
18 /**
19 * Registers a route to get suggestions from the AI API
20 *
21 * @makePublic
22 *
23 * @phpcs:disable Yoast.NamingConventions.ObjectNameDepth.MaxExceeded
24 */
25 class Get_Suggestions_Route implements Route_Interface {
26
27 use Route_Permission_Trait;
28
29 /**
30 * The namespace for this route.
31 *
32 * @var string
33 */
34 public const ROUTE_NAMESPACE = Main::API_V1_NAMESPACE;
35
36 /**
37 * The prefix for this route.
38 *
39 * @var string
40 */
41 public const ROUTE_PREFIX = '/ai_generator/get_suggestions';
42
43 /**
44 * The suggestions provider instance.
45 *
46 * @var Suggestions_Provider
47 */
48 private $suggestions_provider;
49
50 /**
51 * Returns the conditionals based in which this loadable should be active.
52 *
53 * @return array<string> The conditionals.
54 */
55 public static function get_conditionals() {
56 return [ AI_Conditional::class, New_Premium_Or_Free_AI_Conditional::class ];
57 }
58
59 /**
60 * Class constructor.
61 *
62 * @param Suggestions_Provider $suggestions_provider The suggestions provider instance.
63 */
64 public function __construct( Suggestions_Provider $suggestions_provider ) {
65 $this->suggestions_provider = $suggestions_provider;
66 }
67
68 /**
69 * Registers routes with WordPress.
70 *
71 * @return void
72 */
73 public function register_routes() {
74 \register_rest_route(
75 self::ROUTE_NAMESPACE,
76 self::ROUTE_PREFIX,
77 [
78 'methods' => 'POST',
79 'args' => [
80 'type' => [
81 'required' => true,
82 'type' => 'string',
83 'enum' => [
84 'seo-title',
85 'meta-description',
86 'product-seo-title',
87 'product-meta-description',
88 'product-taxonomy-seo-title',
89 'product-taxonomy-meta-description',
90 'taxonomy-seo-title',
91 'taxonomy-meta-description',
92 ],
93 'description' => 'The type of suggestion requested.',
94 ],
95 'prompt_content' => [
96 'required' => true,
97 'type' => 'string',
98 'description' => 'The content needed by the prompt to ask for suggestions.',
99 ],
100 'focus_keyphrase' => [
101 'required' => true,
102 'type' => 'string',
103 'description' => 'The focus keyphrase associated to the post.',
104 ],
105 'language' => [
106 'required' => true,
107 'type' => 'string',
108 'description' => 'The language the post is written in.',
109 ],
110 'platform' => [
111 'required' => true,
112 'type' => 'string',
113 'enum' => [
114 'Google',
115 'Facebook',
116 'Twitter',
117 ],
118 'description' => 'The platform the post is intended for.',
119 ],
120 'editor' => [
121 'required' => true,
122 'type' => 'string',
123 'enum' => [
124 'classic',
125 'elementor',
126 'gutenberg',
127 ],
128 'description' => 'The current editor.',
129 ],
130 ],
131 'callback' => [ $this, 'get_suggestions' ],
132 'permission_callback' => [ $this, 'check_permissions' ],
133 ],
134 );
135 }
136
137 /**
138 * Runs the callback to get AI-generated suggestions.
139 *
140 * @param WP_REST_Request $request The request object.
141 *
142 * @return WP_REST_Response The response of the get_suggestions action.
143 */
144 public function get_suggestions( WP_REST_Request $request ): WP_REST_Response {
145 try {
146 $user = \wp_get_current_user();
147 $data = $this->suggestions_provider->get_suggestions(
148 $user,
149 $request->get_param( 'type' ),
150 $request->get_param( 'prompt_content' ),
151 $request->get_param( 'focus_keyphrase' ),
152 $request->get_param( 'language' ),
153 $request->get_param( 'platform' ),
154 $request->get_param( 'editor' ),
155 );
156 } catch ( Remote_Request_Exception $e ) {
157 $message = [
158 'message' => $e->getMessage(),
159 'errorIdentifier' => $e->get_error_identifier(),
160 ];
161 if ( $e instanceof Payment_Required_Exception || $e instanceof Too_Many_Requests_Exception ) {
162 $message['missingLicenses'] = $e->get_missing_licenses();
163 }
164 return new WP_REST_Response(
165 $message,
166 $e->getCode(),
167 );
168 } catch ( RuntimeException $e ) {
169 return new WP_REST_Response( 'Failed to get suggestions.', 500 );
170 }
171
172 return new WP_REST_Response( $data );
173 }
174 }
175