PluginProbe
Yoast SEO – Advanced SEO with real-time guidance and built-in AI / trunk
Yoast SEO – Advanced SEO with real-time guidance and built-in AI vtrunk
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 trunk, at src/ai/generator/user-interface/get-suggestions-route.php

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