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 / 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.6, at src/ai/generator/user-interface/get-suggestions-route.php

176 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\No_Conditionals;
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 No_Conditionals;
28 use Route_Permission_Trait;
29
30 /**
31 * The namespace for this route.
32 *
33 * @var string
34 */
35 public const ROUTE_NAMESPACE = Main::API_V1_NAMESPACE;
36
37 /**
38 * The prefix for this route.
39 *
40 * @var string
41 */
42 public const ROUTE_PREFIX = '/ai_generator/get_suggestions';
43
44 /**
45 * The suggestions provider instance.
46 *
47 * @var Suggestions_Provider
48 */
49 private $suggestions_provider;
50
51 /**
52 * Returns the conditionals based in which this loadable should be active.
53 *
54 * @return array<string> The conditionals.
55 */
56 public static function get_conditionals() {
57 return [ AI_Conditional::class ];
58 }
59
60 /**
61 * Class constructor.
62 *
63 * @param Suggestions_Provider $suggestions_provider The suggestions provider instance.
64 */
65 public function __construct( Suggestions_Provider $suggestions_provider ) {
66 $this->suggestions_provider = $suggestions_provider;
67 }
68
69 /**
70 * Registers routes with WordPress.
71 *
72 * @return void
73 */
74 public function register_routes() {
75 \register_rest_route(
76 self::ROUTE_NAMESPACE,
77 self::ROUTE_PREFIX,
78 [
79 'methods' => 'POST',
80 'args' => [
81 'type' => [
82 'required' => true,
83 'type' => 'string',
84 'enum' => [
85 'seo-title',
86 'meta-description',
87 'product-seo-title',
88 'product-meta-description',
89 'product-taxonomy-seo-title',
90 'product-taxonomy-meta-description',
91 'taxonomy-seo-title',
92 'taxonomy-meta-description',
93 ],
94 'description' => 'The type of suggestion requested.',
95 ],
96 'prompt_content' => [
97 'required' => true,
98 'type' => 'string',
99 'description' => 'The content needed by the prompt to ask for suggestions.',
100 ],
101 'focus_keyphrase' => [
102 'required' => true,
103 'type' => 'string',
104 'description' => 'The focus keyphrase associated to the post.',
105 ],
106 'language' => [
107 'required' => true,
108 'type' => 'string',
109 'description' => 'The language the post is written in.',
110 ],
111 'platform' => [
112 'required' => true,
113 'type' => 'string',
114 'enum' => [
115 'Google',
116 'Facebook',
117 'Twitter',
118 ],
119 'description' => 'The platform the post is intended for.',
120 ],
121 'editor' => [
122 'required' => true,
123 'type' => 'string',
124 'enum' => [
125 'classic',
126 'elementor',
127 'gutenberg',
128 ],
129 'description' => 'The current editor.',
130 ],
131 ],
132 'callback' => [ $this, 'get_suggestions' ],
133 'permission_callback' => [ $this, 'check_permissions' ],
134 ],
135 );
136 }
137
138 /**
139 * Runs the callback to get AI-generated suggestions.
140 *
141 * @param WP_REST_Request $request The request object.
142 *
143 * @return WP_REST_Response The response of the get_suggestions action.
144 */
145 public function get_suggestions( WP_REST_Request $request ): WP_REST_Response {
146 try {
147 $user = \wp_get_current_user();
148 $data = $this->suggestions_provider->get_suggestions(
149 $user,
150 $request->get_param( 'type' ),
151 $request->get_param( 'prompt_content' ),
152 $request->get_param( 'focus_keyphrase' ),
153 $request->get_param( 'language' ),
154 $request->get_param( 'platform' ),
155 $request->get_param( 'editor' ),
156 );
157 } catch ( Remote_Request_Exception $e ) {
158 $message = [
159 'message' => $e->getMessage(),
160 'errorIdentifier' => $e->get_error_identifier(),
161 ];
162 if ( $e instanceof Payment_Required_Exception || $e instanceof Too_Many_Requests_Exception ) {
163 $message['missingLicenses'] = $e->get_missing_licenses();
164 }
165 return new WP_REST_Response(
166 $message,
167 $e->getCode(),
168 );
169 } catch ( RuntimeException $e ) {
170 return new WP_REST_Response( 'Failed to get suggestions.', 500 );
171 }
172
173 return new WP_REST_Response( $data );
174 }
175 }
176