Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
88.13% covered (warning)
88.13%
297 / 337
58.33% covered (warning)
58.33%
14 / 24
CRAP
0.00% covered (danger)
0.00%
0 / 1
REST_Controller
88.36% covered (warning)
88.36%
296 / 335
58.33% covered (warning)
58.33%
14 / 24
129.09
0.00% covered (danger)
0.00%
0 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
3
 register
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 register_rest_routes
75.00% covered (warning)
75.00%
3 / 4
0.00% covered (danger)
0.00%
0 / 1
2.06
 register_common_rest_routes
100.00% covered (success)
100.00%
60 / 60
100.00% covered (success)
100.00%
1 / 1
1
 register_jetpack_only_rest_routes
100.00% covered (success)
100.00%
36 / 36
100.00% covered (success)
100.00%
1 / 1
1
 register_wpcom_only_rest_routes
0.00% covered (danger)
0.00%
0 / 1
0.00% covered (danger)
0.00%
0 / 1
2
 require_admin_privilege_callback
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 require_valid_blog_token_callback
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
2
 get_forbidden_error
100.00% covered (success)
100.00%
5 / 5
100.00% covered (success)
100.00%
1 / 1
1
 get_search_plan
100.00% covered (success)
100.00%
2 / 2
100.00% covered (success)
100.00%
1 / 1
1
 update_settings
74.55% covered (warning)
74.55%
41 / 55
0.00% covered (danger)
0.00%
0 / 1
37.15
 validate_search_settings
97.67% covered (success)
97.67%
42 / 43
0.00% covered (danger)
0.00%
0 / 1
36
 get_settings
100.00% covered (success)
100.00%
14 / 14
100.00% covered (success)
100.00%
1 / 1
2
 is_reader_chat_setting_registered
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
1
 get_stats
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 get_search_results
100.00% covered (success)
100.00%
8 / 8
100.00% covered (success)
100.00%
1 / 1
1
 activate_plan
92.50% covered (success)
92.50%
37 / 40
0.00% covered (danger)
0.00%
0 / 1
13.07
 deactivate_plan
0.00% covered (danger)
0.00%
0 / 6
0.00% covered (danger)
0.00%
0 / 1
2
 get_local_stats
100.00% covered (success)
100.00%
4 / 4
100.00% covered (success)
100.00%
1 / 1
1
 reset_singleton_template
100.00% covered (success)
100.00%
21 / 21
100.00% covered (success)
100.00%
1 / 1
4
 resolve_singleton_template_class
100.00% covered (success)
100.00%
7 / 7
100.00% covered (success)
100.00%
1 / 1
1
 product_pricing
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 make_proper_response
45.45% covered (danger)
45.45%
5 / 11
0.00% covered (danger)
0.00%
0 / 1
6.60
 get_blog_id
100.00% covered (success)
100.00%
1 / 1
100.00% covered (success)
100.00%
1 / 1
2
1<?php
2/**
3 * The Search Rest Controller class.
4 * Registers the REST routes for Search.
5 *
6 * @package automattic/jetpack-search
7 */
8
9namespace Automattic\Jetpack\Search;
10
11use Automattic\Jetpack\Connection\Client;
12use Automattic\Jetpack\Connection\Rest_Authentication;
13use Automattic\Jetpack\My_Jetpack\Products\Search as Search_Product;
14use Automattic\Jetpack\My_Jetpack\Products\Search_Stats as Search_Product_Stats;
15use Jetpack_Options;
16use WP_Error;
17use WP_REST_Request;
18use WP_REST_Response;
19use WP_REST_Server;
20
21if ( ! defined( 'ABSPATH' ) ) {
22    exit( 0 );
23}
24
25/**
26 * Registers the REST routes for Search.
27 */
28class REST_Controller {
29    /**
30     * Namespace for the REST API.
31     *
32     * This is overriden with value `wpcom-orgin/jetpack/v4` for WPCOM.
33     *
34     * @var string
35     */
36    public static $namespace = 'jetpack/v4';
37    /**
38     * Whether it's run on WPCOM.
39     *
40     * @var bool
41     */
42    protected $is_wpcom;
43
44    /**
45     * Module Control object.
46     *
47     * @var Module_Control
48     */
49    protected $search_module;
50
51    /**
52     * Plan object.
53     *
54     * @var Plan
55     */
56    public $plan;
57
58    /**
59     * Constructor
60     *
61     * @param bool                $is_wpcom - Whether it's run on WPCOM.
62     * @param Module_Control|null $module_control - Module_Control object if any.
63     * @param Plan|null           $plan - Plan object if any.
64     */
65    public function __construct( $is_wpcom = false, $module_control = null, $plan = null ) {
66        $this->is_wpcom      = $is_wpcom;
67        $this->search_module = $module_control === null ? new Module_Control() : $module_control;
68        $this->plan          = $plan === null ? new Plan() : $plan;
69    }
70
71    /**
72     * Registers the REST routes on the `rest_api_init` hook.
73     *
74     * Instantiated here, rather than eagerly, so the controller class only loads
75     * on requests that reach `rest_api_init`. Static so the callback can be
76     * unregistered.
77     *
78     * @access public
79     */
80    public static function register() {
81        ( new self() )->register_rest_routes();
82    }
83
84    /**
85     * Registers the REST routes for Search.
86     *
87     * @access public
88     * @static
89     */
90    public function register_rest_routes() {
91        $this->register_common_rest_routes();
92        if ( ! Helper::is_wpcom() ) {
93            $this->register_jetpack_only_rest_routes();
94        } else {
95            $this->register_wpcom_only_rest_routes();
96        }
97    }
98
99    /**
100     * Routes both existing in Jetpack and WPCOM simple sites.
101     */
102    protected function register_common_rest_routes() {
103        register_rest_route(
104            static::$namespace,
105            '/search/plan',
106            array(
107                'methods'             => WP_REST_Server::READABLE,
108                'callback'            => array( $this, 'get_search_plan' ),
109                'permission_callback' => array( $this, 'require_admin_privilege_callback' ),
110            )
111        );
112        register_rest_route(
113            static::$namespace,
114            '/search/settings',
115            array(
116                'methods'             => WP_REST_Server::EDITABLE,
117                'callback'            => array( $this, 'update_settings' ),
118                'permission_callback' => array( $this, 'require_admin_privilege_callback' ),
119            )
120        );
121        register_rest_route(
122            static::$namespace,
123            '/search/settings',
124            array(
125                'methods'             => WP_REST_Server::READABLE,
126                'callback'            => array( $this, 'get_settings' ),
127                'permission_callback' => array( $this, 'require_admin_privilege_callback' ),
128            )
129        );
130        register_rest_route(
131            static::$namespace,
132            '/search/stats',
133            array(
134                'methods'             => WP_REST_Server::READABLE,
135                'callback'            => array( $this, 'get_stats' ),
136                'permission_callback' => array( $this, 'require_admin_privilege_callback' ),
137            )
138        );
139        register_rest_route(
140            static::$namespace,
141            '/search/pricing',
142            array(
143                'methods'             => WP_REST_Server::READABLE,
144                'callback'            => array( $this, 'product_pricing' ),
145                'permission_callback' => 'is_user_logged_in',
146            )
147        );
148        // "Restore default" for the singleton-template CPTs. Lives on
149        // jetpack/v4 (not /wp/v2/<rest_base>) so wpcom-origin can proxy it
150        // on Simple sites — the Jetpack-registered CPT controller isn't on
151        // the wpcom REST surface. The allowed `<post_type>` slugs are
152        // enforced inside the handler (single source of truth) rather than
153        // duplicated into a route-level validate_callback.
154        register_rest_route(
155            static::$namespace,
156            '/search/templates/(?P<post_type>[a-z0-9_-]+)',
157            array(
158                'methods'             => WP_REST_Server::DELETABLE,
159                'callback'            => array( $this, 'reset_singleton_template' ),
160                'permission_callback' => array( $this, 'require_admin_privilege_callback' ),
161                'args'                => array(
162                    'post_type' => array(
163                        'required'          => true,
164                        'sanitize_callback' => 'sanitize_key',
165                    ),
166                ),
167            )
168        );
169    }
170
171    /**
172     * Routes only existing in Jetpack.
173     */
174    protected function register_jetpack_only_rest_routes() {
175        register_rest_route(
176            static::$namespace,
177            '/search/plan/activate',
178            array(
179                'methods'             => WP_REST_Server::EDITABLE,
180                'callback'            => array( $this, 'activate_plan' ),
181                'permission_callback' => array( $this, 'require_admin_privilege_callback' ),
182            )
183        );
184        register_rest_route(
185            static::$namespace,
186            '/search/plan/deactivate',
187            array(
188                'methods'             => WP_REST_Server::EDITABLE,
189                'callback'            => array( $this, 'deactivate_plan' ),
190                'permission_callback' => array( $this, 'require_admin_privilege_callback' ),
191            )
192        );
193        register_rest_route(
194            static::$namespace,
195            '/search',
196            array(
197                'methods'             => WP_REST_Server::READABLE,
198                'callback'            => array( $this, 'get_search_results' ),
199                'permission_callback' => 'is_user_logged_in',
200            )
201        );
202        register_rest_route(
203            static::$namespace,
204            '/search/local-stats',
205            array(
206                'methods'             => WP_REST_Server::READABLE,
207                'callback'            => array( $this, 'get_local_stats' ),
208                'permission_callback' => array( $this, 'require_valid_blog_token_callback' ),
209            )
210        );
211    }
212
213    /**
214     * Routes only existing in WPCOM.
215     *
216     * We currently don't have any.
217     */
218    protected function register_wpcom_only_rest_routes() {
219        return true;
220    }
221
222    /**
223     * Only administrators can access the API.
224     *
225     * @return bool|WP_Error True if a blog token was used to sign the request, WP_Error otherwise.
226     */
227    public function require_admin_privilege_callback() {
228        if ( current_user_can( 'manage_options' ) ) {
229            return true;
230        }
231
232        return $this->get_forbidden_error();
233    }
234
235    /**
236     * The corresponding endpoints can only be accessible from WPCOM.
237     *
238     * @access public
239     * @static
240     *
241     * @return bool|WP_Error True if a blog token was used to sign the request, WP_Error otherwise.
242     */
243    public function require_valid_blog_token_callback() {
244        if ( Rest_Authentication::is_signed_with_blog_token() ) {
245            return true;
246        }
247
248        return $this->get_forbidden_error();
249    }
250
251    /**
252     * Return a WP_Error object with a forbidden error.
253     */
254    protected function get_forbidden_error() {
255        $error_msg = esc_html__(
256            'You are not allowed to perform this action.',
257            'jetpack-search-pkg'
258        );
259
260        return new WP_Error( 'rest_forbidden', $error_msg, array( 'status' => rest_authorization_required_code() ) );
261    }
262
263    /**
264     * Proxy the request to WPCOM and return the response.
265     *
266     * GET `jetpack/v4/search/plan`
267     */
268    public function get_search_plan() {
269        $response = ( new Plan() )->get_plan_info_from_wpcom();
270        return $this->make_proper_response( $response );
271    }
272
273    /**
274     * POST `jetpack/v4/search/settings`
275     *
276     * @param WP_REST_Request $request - REST request.
277     */
278    public function update_settings( $request ) {
279        $request_body = $request->get_json_params();
280        if ( ! is_array( $request_body ) ) {
281            $request_body = array();
282        }
283
284        $module_active                 = isset( $request_body['module_active'] ) ? (bool) $request_body['module_active'] : null;
285        $instant_search_enabled        = isset( $request_body['instant_search_enabled'] ) ? (bool) $request_body['instant_search_enabled'] : null;
286        $swap_classic_to_inline_search = isset( $request_body['swap_classic_to_inline_search'] ) ? (bool) $request_body['swap_classic_to_inline_search'] : null;
287        $experience                    = isset( $request_body['experience'] ) && is_string( $request_body['experience'] )
288            ? sanitize_text_field( $request_body['experience'] )
289            : null;
290        $reader_chat                   = array_key_exists( 'reader_chat', $request_body ) ? (bool) $request_body['reader_chat'] : null;
291        $ai_answers_enabled            = isset( $request_body['ai_answers_enabled'] ) ? (bool) $request_body['ai_answers_enabled'] : null;
292
293        $search_suggestions_enabled = isset( $request_body['search_suggestions_enabled'] ) ? (bool) $request_body['search_suggestions_enabled'] : null;
294
295        $override_woocommerce_search_template = isset( $request_body['override_woocommerce_search_template'] ) ? (bool) $request_body['override_woocommerce_search_template'] : null;
296
297        $error = $this->validate_search_settings( $module_active, $instant_search_enabled, $swap_classic_to_inline_search, $experience, $reader_chat, $ai_answers_enabled, $search_suggestions_enabled, $override_woocommerce_search_template );
298
299        if ( is_wp_error( $error ) ) {
300            return $error;
301        }
302
303        // If an experience value was provided, delegate to Module_Control::update_experience(),
304        // which encapsulates the storage shape (off → module deactivate, inline → delete option,
305        // embedded/overlay → write affirmative value) and keeps the legacy booleans in lockstep.
306        if ( $experience !== null ) {
307            $result = $this->search_module->update_experience( $experience );
308            if ( is_wp_error( $result ) ) {
309                return $result;
310            }
311            return rest_ensure_response( $this->get_settings() );
312        }
313
314        // Enabling instant search should enable the module too.
315        if ( true === $instant_search_enabled && true !== $module_active ) {
316            $module_active = true;
317        }
318
319        $errors = array();
320        if ( $module_active !== null ) {
321            $module_active_updated = $this->search_module->update_status( $module_active );
322            if ( is_wp_error( $module_active_updated ) ) {
323                $errors['module_active'] = $module_active_updated;
324            }
325        }
326
327        if ( $instant_search_enabled !== null ) {
328            $instant_search_enabled_updated = $this->search_module->update_instant_search_status( $instant_search_enabled );
329            if ( is_wp_error( $instant_search_enabled_updated ) ) {
330                $errors['instant_search_enabled'] = $instant_search_enabled_updated;
331            }
332        }
333
334        if ( $swap_classic_to_inline_search !== null ) {
335            $this->search_module->update_swap_classic_to_inline_search( $swap_classic_to_inline_search );
336        }
337
338        if ( $reader_chat !== null ) {
339            update_option( 'reader_chat', $reader_chat );
340        }
341
342        if ( $ai_answers_enabled !== null ) {
343            update_option( 'jetpack_search_ai_answers_enabled', $ai_answers_enabled );
344        }
345        if ( $search_suggestions_enabled !== null ) {
346            update_option( 'jetpack_search_suggestions_enabled', $search_suggestions_enabled );
347        }
348        if ( $override_woocommerce_search_template !== null ) {
349            update_option( 'jetpack_search_override_woocommerce_search_template', $override_woocommerce_search_template );
350        }
351
352        if ( ! empty( $errors ) ) {
353            return new WP_Error(
354                'some_updated',
355                sprintf(
356                    /* translators: %s are the setting name that not updated. */
357                    __( 'Some settings ( %s ) not updated.', 'jetpack-search-pkg' ),
358                    implode(
359                        ',',
360                        array_keys( $errors )
361                    )
362                ),
363                array( 'status' => 400 )
364            );
365        }
366
367        return rest_ensure_response( $this->get_settings() );
368    }
369
370    /**
371     * Validate $module_active and $instant_search_enabled. Returns an WP_Error instance if invalid.
372     *
373     * @param boolean     $module_active - Module status.
374     * @param boolean     $instant_search_enabled - Instant Search status.
375     * @param boolean     $swap_classic_to_inline_search - New inline search status.
376     * @param string|null $experience - Experience value.
377     * @param bool|null   $reader_chat - Reader Chat status.
378     * @param bool|null   $ai_answers_enabled - Whether Jetpack Search AI answers is enabled.
379     * @param bool|null   $search_suggestions_enabled - New search suggestions status.
380     * @param bool|null   $override_woocommerce_search_template - New WooCommerce search-template override status.
381     */
382    protected function validate_search_settings( $module_active, $instant_search_enabled, $swap_classic_to_inline_search, $experience = null, $reader_chat = null, $ai_answers_enabled = null, $search_suggestions_enabled = null, $override_woocommerce_search_template = null ) {
383        if ( $reader_chat !== null && ! $this->is_reader_chat_setting_registered() ) {
384            return new WP_Error(
385                'rest_invalid_arguments',
386                esc_html__( 'The arguments passed in are invalid.', 'jetpack-search-pkg' ),
387                array( 'status' => 400 )
388            );
389        }
390
391        // AI Answers cannot be turned on while the site-wide Jetpack AI switch is
392        // off. Turning it off stays allowed, so a saved choice can still be cleared.
393        if ( true === $ai_answers_enabled && ! AI_Answers::is_master_enabled() ) {
394            return new WP_Error(
395                'rest_invalid_arguments',
396                esc_html__( 'AI Answers cannot be enabled while Jetpack AI is turned off for this site.', 'jetpack-search-pkg' ),
397                array( 'status' => 400 )
398            );
399        }
400
401        // AI Answers runs inside Instant Search, so enabling it requires Instant
402        // Search on — either already, or turned on by this same request.
403        if ( true === $ai_answers_enabled && true !== $instant_search_enabled && ! $this->search_module->is_instant_search_enabled() ) {
404            return new WP_Error(
405                'rest_invalid_arguments',
406                esc_html__( 'AI Answers cannot be enabled while Instant Search is off.', 'jetpack-search-pkg' ),
407                array( 'status' => 400 )
408            );
409        }
410
411        // `experience` is the canonical source of truth and writes the legacy booleans in lockstep.
412        // Reject requests that mix it with any other settings field so callers don't silently
413        // lose those fields — the `experience` branch in update_settings() early-returns and
414        // would otherwise drop them.
415        if ( $experience !== null ) {
416            if ( $module_active !== null || $instant_search_enabled !== null || $swap_classic_to_inline_search !== null || $reader_chat !== null || $ai_answers_enabled !== null || $search_suggestions_enabled !== null || $override_woocommerce_search_template !== null ) {
417                return new WP_Error(
418                    'rest_invalid_arguments',
419                    esc_html__( 'The `experience` field cannot be combined with `module_active`, `instant_search_enabled`, `swap_classic_to_inline_search`, `reader_chat`, `ai_answers_enabled`, `search_suggestions_enabled`, or `override_woocommerce_search_template`.', 'jetpack-search-pkg' ),
420                    array( 'status' => 400 )
421                );
422            }
423            return true;
424        }
425        if (
426            $module_active === null &&
427            $instant_search_enabled === null &&
428            ( $swap_classic_to_inline_search !== null || $reader_chat !== null )
429        ) {
430            // Allow updating auxiliary settings without updating/validating the module settings.
431            return true;
432        }
433        if ( $module_active === null && $instant_search_enabled === null && $swap_classic_to_inline_search === null && $ai_answers_enabled !== null ) {
434            // allow updating 'ai_answers_enabled' without updating/validating other settings.
435            return true;
436        }
437        if ( $module_active === null && $instant_search_enabled === null && $swap_classic_to_inline_search === null && $search_suggestions_enabled !== null ) {
438            // allow updating 'search_suggestions_enabled' without updating/validating other settings.
439            return true;
440        }
441        if ( $module_active === null && $instant_search_enabled === null && $swap_classic_to_inline_search === null && $override_woocommerce_search_template !== null ) {
442            // allow updating 'override_woocommerce_search_template' without updating/validating other settings.
443            return true;
444        }
445        if ( ( true === $instant_search_enabled && false === $module_active ) || ( $module_active === null && $instant_search_enabled === null ) ) {
446            return new WP_Error(
447                'rest_invalid_arguments',
448                esc_html__( 'The arguments passed in are invalid.', 'jetpack-search-pkg' ),
449                array( 'status' => 400 )
450            );
451        }
452        return true;
453    }
454
455        /**
456         *     GET `jetpack/v4/search/settings`
457         */
458    public function get_settings() {
459        $settings = array(
460            'module_active'                        => $this->search_module->is_active(),
461            'instant_search_enabled'               => $this->search_module->is_instant_search_enabled(),
462            'swap_classic_to_inline_search'        => $this->search_module->is_swap_classic_to_inline_search(),
463            'experience'                           => $this->search_module->get_experience(),
464            'ai_answers_enabled'                   => AI_Answers::is_enabled(),
465            'ai_answers_saved'                     => AI_Answers::is_saved_on(),
466            'ai_master_enabled'                    => AI_Answers::is_master_enabled(),
467            'search_suggestions_enabled'           => (bool) get_option( 'jetpack_search_suggestions_enabled', false ),
468            'override_woocommerce_search_template' => Search_Blocks::woocommerce_search_template_override_enabled(),
469        );
470
471        if ( $this->is_reader_chat_setting_registered() ) {
472            $settings['reader_chat'] = (bool) get_option( 'reader_chat', false );
473        }
474
475        return rest_ensure_response( $settings );
476    }
477
478    /**
479     * Check whether Reader Chat is available through REST settings in this request.
480     *
481     * Reader Chat registers `reader_chat` only for proxied rollout contexts, so the
482     * Search dashboard should expose the toggle only when that setting exists.
483     *
484     * @return bool True when reader_chat is registered.
485     */
486    protected function is_reader_chat_setting_registered() {
487        return array_key_exists( 'reader_chat', get_registered_settings() );
488    }
489
490    /**
491     * Proxy the request to WPCOM and return the response.
492     *
493     * GET `jetpack/v4/search/stats`
494     */
495    public function get_stats() {
496        $response = ( new Stats() )->get_stats_from_wpcom();
497        return $this->make_proper_response( $response );
498    }
499
500    /**
501     * Search Endpoint for private sites.
502     *
503     * GET `jetpack/v4/search`
504     *
505     * @param WP_REST_Request $request - REST request.
506     */
507    public function get_search_results( $request ) {
508        $blog_id  = $this->get_blog_id();
509        $path     = sprintf( '/sites/%d/search', absint( $blog_id ) );
510        $path     = add_query_arg(
511            $request->get_query_params(),
512            sprintf( '/sites/%d/search', absint( $blog_id ) )
513        );
514        $response = Client::wpcom_json_api_request_as_blog( $path, '1.3', array(), null, 'rest' );
515        return rest_ensure_response( $this->make_proper_response( $response ) );
516    }
517
518    /**
519     * Activate plan: activate the search module, instant search and do initial configuration.
520     * Typically called from WPCOM.
521     *
522     * POST `jetpack/v4/search/plan/activate`
523     *
524     * @param WP_REST_Request $request - REST request.
525     */
526    public function activate_plan( $request ) {
527        $default_options = array(
528            'search_plan_info'      => null,
529            'enable_search'         => true,
530            'enable_instant_search' => true,
531            'search_experience'     => null,
532            'auto_config_search'    => true,
533        );
534        $payload         = $request->get_json_params();
535        $payload         = wp_parse_args( $payload, $default_options );
536
537        // Update plan data, plan info is in the request body.
538        // We do this to avoid another call to WPCOM and reduce latency.
539        if ( $payload['search_plan_info'] === null || ! $this->plan->set_plan_options( $payload['search_plan_info'] ) ) {
540            $this->plan->get_plan_info_from_wpcom();
541        }
542
543        // Enable search module by default, unless `enable_search` is explicitly set to boolean `false`.
544        if ( false !== $payload['enable_search'] ) {
545            $ret = $this->search_module->activate();
546            if ( is_wp_error( $ret ) ) {
547                return $ret;
548            }
549        }
550
551        if ( $payload['search_experience'] !== null ) {
552            // Canonical path. Restrict to activate-able experiences — `off`
553            // belongs on `/plan/deactivate`, and a non-string payload would
554            // blow up `update_experience(string $experience)`.
555            $valid_experiences = array(
556                Module_Control::EXPERIENCE_OVERLAY,
557                Module_Control::EXPERIENCE_INLINE,
558                Module_Control::EXPERIENCE_EMBEDDED,
559            );
560            if ( ! is_string( $payload['search_experience'] )
561                || ! in_array( $payload['search_experience'], $valid_experiences, true )
562            ) {
563                return new WP_Error(
564                    'invalid_experience',
565                    __( 'Invalid experience value.', 'jetpack-search-pkg' ),
566                    array( 'status' => 400 )
567                );
568            }
569            $ret = $this->search_module->update_experience( sanitize_text_field( $payload['search_experience'] ) );
570            if ( is_wp_error( $ret ) ) {
571                return $ret;
572            }
573        }
574
575        if ( $payload['search_experience'] === null && false !== $payload['enable_instant_search'] ) {
576            // Legacy path: old WPCOM callers send `enable_instant_search`
577            // instead of `search_experience`. Gated on the canonical value
578            // being absent so it doesn't overwrite a non-overlay experience
579            // the caller just set.
580            // Error handling intentionally skipped — this is the legacy fallback.
581            $ret = $this->search_module->enable_instant_search();
582        }
583
584        // `auto_config_search` wires up Overlay sidebar widgets — only meaningful
585        // when Overlay is the resulting experience. For Inline / Embedded, the
586        // caller would otherwise get widget side effects they didn't ask for.
587        if ( false !== $payload['auto_config_search'] && $this->search_module->is_instant_search_enabled() ) {
588            Instant_Search::instance( $this->get_blog_id() )->auto_config_search();
589        }
590
591        return rest_ensure_response(
592            array(
593                'code' => 'success',
594            )
595        );
596    }
597
598    /**
599     * Deactivate plan: turn off search module and instant search.
600     * If the plan is still valid then the function would simply deactivate the search module.
601     * Typically called from WPCOM.
602     *
603     * POST `jetpack/v4/search/plan/deactivate`
604     */
605    public function deactivate_plan() {
606        // Instant Search would be disabled along with search module.
607        $this->search_module->deactivate();
608        return rest_ensure_response(
609            array(
610                'code' => 'success',
611            )
612        );
613    }
614
615    /**
616     * Return post type breakdown for the site.
617     */
618    public function get_local_stats() {
619        return array(
620            'post_count'          => Search_Product_Stats::estimate_count(),
621            'post_type_breakdown' => Search_Product_Stats::get_post_type_breakdown(),
622        );
623    }
624
625    /**
626     * Force-delete the {@see Singleton_Template_Cpt} customization for the
627     * requested post type, backing the dashboard's "Restore default" link.
628     * `before_delete_post` in the base class clears the option pointer +
629     * per-request cache so the next render falls back to the bundled template.
630     *
631     * DELETE `jetpack/v4/search/templates/<post_type>`
632     *
633     * @param WP_REST_Request $request - REST request.
634     * @return WP_REST_Response|WP_Error
635     */
636    public function reset_singleton_template( $request ) {
637        $cpt_class = $this->resolve_singleton_template_class( $request['post_type'] );
638        if ( ! $cpt_class ) {
639            return new WP_Error(
640                'jetpack_search_template_unknown',
641                __( 'Unknown search template.', 'jetpack-search-pkg' ),
642                array( 'status' => 404 )
643            );
644        }
645        if ( ! $cpt_class::is_customized() ) {
646            return new WP_Error(
647                'jetpack_search_template_not_customized',
648                __( 'No customization to restore.', 'jetpack-search-pkg' ),
649                array( 'status' => 404 )
650            );
651        }
652        $post_id = $cpt_class::get_post_id();
653        if ( ! wp_delete_post( $post_id, true ) ) {
654            return new WP_Error(
655                'jetpack_search_template_reset_failed',
656                __( 'Failed to restore the default template.', 'jetpack-search-pkg' ),
657                array( 'status' => 500 )
658            );
659        }
660        return rest_ensure_response( array( 'deleted' => true ) );
661    }
662
663    /**
664     * Map a CPT slug to its concrete `Singleton_Template_Cpt` subclass.
665     * Returns null when the slug isn't one of the registered singleton-template
666     * CPTs — the route only sanitizes the slug (via `sanitize_key`), so this
667     * lookup is the primary "is this a known CPT?" filter, not a backup check.
668     *
669     * @param string $post_type Post type slug from the request.
670     * @return class-string<Singleton_Template_Cpt>|null
671     */
672    protected function resolve_singleton_template_class( $post_type ) {
673        $map = array(
674            Overlay_Template::POST_TYPE         => Overlay_Template::class,
675            Product_Overlay_Template::POST_TYPE => Product_Overlay_Template::class,
676            Search_Template::POST_TYPE          => Search_Template::class,
677            Product_Search_Template::POST_TYPE  => Product_Search_Template::class,
678        );
679        return $map[ $post_type ] ?? null;
680    }
681
682    /**
683     * Pricing for record count of the site
684     */
685    public function product_pricing() {
686        $tier_pricing = Search_Product::get_pricing_for_ui();
687        // we can force the plugin to use the new pricing by appending `new_pricing_202208=1` to URL.
688        if ( Helper::is_forced_new_pricing_202208() ) {
689            $tier_pricing['pricing_version'] = Plan::JETPACK_SEARCH_NEW_PRICING_VERSION;
690        }
691        return rest_ensure_response( $tier_pricing );
692    }
693
694    /**
695     * Forward remote response to client with error handling.
696     *
697     * @param array|WP_Error $response - Response from WPCOM.
698     */
699    protected function make_proper_response( $response ) {
700        if ( is_wp_error( $response ) ) {
701            return $response;
702        }
703
704        $body        = json_decode( wp_remote_retrieve_body( $response ), true );
705        $status_code = wp_remote_retrieve_response_code( $response );
706
707        if ( 200 === $status_code ) {
708            return $body;
709        }
710
711        return new WP_Error(
712            isset( $body['error'] ) ? 'remote-error-' . $body['error'] : 'remote-error',
713            $body['message'] ?? 'unknown remote error',
714            array( 'status' => $status_code )
715        );
716    }
717
718    /**
719     * Get blog id
720     */
721    protected function get_blog_id() {
722        return $this->is_wpcom ? get_current_blog_id() : Jetpack_Options::get_option( 'id' );
723    }
724}