PluginProbe
Jetpack – WP Security, Backup, Speed, & Growth / 16.3-a.5
Jetpack – WP Security, Backup, Speed, & Growth v16.3-a.5
16.3-a.5 16.3-a.7 16.3-a.3 16.3-a.1 16.2 16.2-beta 12.0.3 12.1.3 12.2.3 12.3.2 12.4.2 12.5.2 12.6.4 12.7.3 12.8.3 12.9.5 13.0.2 13.1.5 13.2.4 13.3.3 13.4.5 13.5.2 13.6.2 13.7.2 13.8.3 All 506 releases
← All changes | _inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-ai.php +29 -8 12.8.3 → 16.3-a.5 View file →
@@ -7,8 +7,12 @@
7 7 */
8 8
9 9 use Automattic\Jetpack\Connection\Client;
10 10
11 +if ( ! defined( 'ABSPATH' ) ) {
12 + exit( 0 );
13 +}
14 +
11 15 /**
12 16 * Class WPCOM_REST_API_V2_Endpoint_AI
13 17 */
14 18 class WPCOM_REST_API_V2_Endpoint_AI extends WP_REST_Controller {
@@ -32,17 +36,29 @@
32 36 public function __construct() {
33 37 $this->is_wpcom = true;
34 38 $this->wpcom_is_wpcom_only_endpoint = true;
35 39
40 + add_action( 'rest_api_init', array( $this, 'maybe_register_routes' ) );
41 + }
42 +
43 + /**
44 + * Register routes on `rest_api_init`, gating on the AI feature state.
45 + *
46 + * The Jetpack_AI_Helper checks (which load the helper and instantiate
47 + * Search/Connection classes) run here rather than in the constructor so that
48 + * code is only loaded when the REST API is actually in use, not on every
49 + * front-end, cron, or login request.
50 + */
51 + public function maybe_register_routes() {
36 52 if ( ! class_exists( 'Jetpack_AI_Helper' ) ) {
37 53 require_once JETPACK__PLUGIN_DIR . '_inc/lib/class-jetpack-ai-helper.php';
38 54 }
39 55
40 56 // Register routes that don't require Jetpack AI to be enabled.
41 - add_action( 'rest_api_init', array( $this, 'register_basic_routes' ) );
57 + $this->register_basic_routes();
42 58
43 59 if ( Jetpack_AI_Helper::is_ai_chat_enabled() ) {
44 - add_action( 'rest_api_init', array( $this, 'register_ai_chat_routes' ) );
60 + $this->register_ai_chat_routes();
45 61 }
46 62
47 63 if ( ! \Jetpack_AI_Helper::is_enabled() ) {
48 64 return;
@@ -48,9 +64,9 @@
48 64 return;
49 65 }
50 66
51 67 // Register routes that require Jetpack AI to be enabled.
52 - add_action( 'rest_api_init', array( $this, 'register_routes' ) );
68 + $this->register_routes();
53 69 }
54 70
55 71 /**
56 72 * Register routes.
@@ -220,12 +236,17 @@
220 236 if ( is_wp_error( $response ) ) {
221 237 return $response;
222 238 }
223 239
224 - $data = json_decode( wp_remote_retrieve_body( $response ) );
240 + $status_code = wp_remote_retrieve_response_code( $response );
241 + $data = json_decode( wp_remote_retrieve_body( $response ) );
225 242
226 - if ( empty( $data->cache_key ) ) {
227 - return new WP_Error( 'invalid_ask_response', __( 'Invalid response from the server.', 'jetpack' ), 400 );
243 + // Forward the real upstream error instead of a generic 500. See SEARCH-351.
244 + if ( $status_code >= 400 || empty( $data->cache_key ) ) {
245 + $code = isset( $data->code ) && is_scalar( $data->code ) ? (string) $data->code : 'invalid_ask_response';
246 + $message = isset( $data->message ) && is_scalar( $data->message ) ? (string) $data->message : __( 'Invalid response from the server.', 'jetpack' );
247 + $status = $status_code >= 400 ? $status_code : 400;
248 + return new WP_Error( $code, $message, array( 'status' => $status ) );
228 249 }
229 250
230 251 return $data;
231 252 }
@@ -241,9 +262,9 @@
241 262 $comment = $request->get_param( 'comment' );
242 263 $cache_key = $request->get_param( 'cache_key' );
243 264
244 265 if ( strpos( $cache_key, 'jp-search-ai-' ) !== 0 ) {
245 - return new WP_Error( 'invalid_cache_key', __( 'Invalid cached context for the answer feedback.', 'jetpack' ), 400 );
266 + return new WP_Error( 'invalid_cache_key', __( 'Invalid cached context for the answer feedback.', 'jetpack' ), array( 'status' => 400 ) );
246 267 }
247 268
248 269 $blog_id = \Jetpack_Options::get_option( 'id' );
249 270 $response = Client::wpcom_json_api_request_as_blog(
@@ -268,9 +289,9 @@
268 289
269 290 $data = json_decode( wp_remote_retrieve_body( $response ) );
270 291
271 292 if ( 'ok' !== $data ) {
272 - return new WP_Error( 'invalid_feedback_response', __( 'Invalid response from the server.', 'jetpack' ), 400 );
293 + return new WP_Error( 'invalid_feedback_response', __( 'Invalid response from the server.', 'jetpack' ), array( 'status' => 400 ) );
273 294 }
274 295
275 296 return $data;
276 297 }