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