) ) { return new WP_Error( 'rest_invalid_arguments', esc_html__( 'The arguments passed in are invalid.', 'jetpack-search-pkg' ), array( 'status' => 400 ) ); } return true; } /** * GET `jetpack/v4/search/settings` */ public function get_settings() { return rest_ensure_response( array( 'module_active' => $this->search_module->is_active(), 'instant_search_enabled' => $this->search_module->is_instant_search_enabled(), ) ); } /** * Proxy the request to WPCOM and return the response. * * GET `jetpack/v4/search/stats` */ public function get_stats() { $response = ( new Stats() )->get_stats_from_wpcom(); return $this->make_proper_response( $response ); } /** * Search Endpoint for private sites. * * GET `jetpack/v4/search` * * @param WP_REST_Request $request - REST request. */ public function get_search_results( $request ) { $blog_id = $this->get_blog_id(); $path = sprintf( '/sites/%d/search', absint( $blog_id ) ); $path = add_query_arg( $request->get_query_params(), sprintf( '/sites/%d/search', absint( $blog_id ) ) ); $response = Client::wpcom_json_api_request_as_blog( $path, '1.3', array(), null, 'rest' ); return rest_ensure_response( $this->make_proper_response( $response ) ); } /** * Activate plan: activate the search module, instant search and do initial configuration. * Typically called from WPCOM. * * POST `jetpack/v4/search/plan/activate` * * @param WP_REST_Request $request - REST request. */ public function activate_plan( $request ) { $default_options = array( 'search_plan_info' => null, 'enable_search' => true, 'enable_instant_search' => true, 'auto_config_search' => true, ); $payload = $request->get_json_params(); $payload = wp_parse_args( $payload, $default_options ); // Update plan data, plan info is in the request body. // We do this to avoid another call to WPCOM and reduce latency. if ( $payload['search_plan_info'] === null || ! $this->plan->set_plan_options( $payload['search_plan_info'] ) ) { $this->plan->get_plan_info_from_wpcom(); } // Enable search module by default, unless `enable_search` is explicitly set to boolean `false`. if ( false !== $payload['enable_search'] ) { // Eligibility is checked in `activate` function. $ret = $this->search_module->activate(); if ( is_wp_error( $ret ) ) { return $ret; } } // Enable instant search by default, unless `enable_instant_search` is explicitly set to boolean `false`. if ( false !== $payload['enable_instant_search'] ) { // Eligibility is checked in `enable_instant_search` function. $ret = $this->search_module->enable_instant_search(); if ( is_wp_error( $ret ) ) { return $ret; } } // Automatically configure necessary settings for instant search, unless `auto_config_search` is explicitly set to boolean `false`. if ( false !== $payload['auto_config_search'] ) { Instant_Search::instance( $this->get_blog_id() )->auto_config_search(); } return rest_ensure_response( array( 'code' => 'success', ) ); } /** * Deactivate plan: turn off search module and instant search. * If the plan is still valid then the function would simply deactivate the search module. * Typically called from WPCOM. * * POST `jetpack/v4/search/plan/deactivate` */ public function deactivate_plan() { // Instant Search would be disabled along with search module. $this->search_module->deactivate(); return rest_ensure_response( array( 'code' => 'success', ) ); } /** * Return post type breakdown for the site. */ public function get_local_stats() { return array( 'post_count' => Search_Product_Stats::estimate_count(), 'post_type_breakdown' => Search_Product_Stats::get_post_type_breakdown(), ); } /** * Pricing for record count of the site */ public function product_pricing() { $tier_pricing = Search_Product::get_pricing_for_ui(); // we can force the plugin to use the new pricing by appending `new_pricing_202208=1` to URL. if ( Helper::is_forced_new_pricing_202208() ) { $tier_pricing['pricing_version'] = Plan::JETPACK_SEARCH_NEW_PRICING_VERSION; } return rest_ensure_response( $tier_pricing ); } /** * Forward remote response to client with error handling. * * @param array|WP_Error $response - Response from WPCOM. */ protected function make_proper_response( $response ) { if ( is_wp_error( $response ) ) { return $response; } $body = json_decode( wp_remote_retrieve_body( $response ), true ); $status_code = wp_remote_retrieve_response_code( $response ); if ( 200 === $status_code ) { return $body; } return new WP_Error( isset( $body['error'] ) ? 'remote-error-' . $body['error'] : 'remote-error', isset( $body['message'] ) ? $body['message'] : 'unknown remote error', array( 'status' => $status_code ) ); } /** * Get blog id */ protected function get_blog_id() { return $this->is_wpcom ? get_current_blog_id() : Jetpack_Options::get_option( 'id' ); } }