Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
56.25% covered (warning)
56.25%
9 / 16
50.00% covered (danger)
50.00%
1 / 2
CRAP
0.00% covered (danger)
0.00%
0 / 1
WPCOM_REST_API_V2_Endpoint_Search
69.23% covered (warning)
69.23%
9 / 13
50.00% covered (danger)
50.00%
1 / 2
3.26
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 4
0.00% covered (danger)
0.00%
0 / 1
6
 register_routes
100.00% covered (success)
100.00%
9 / 9
100.00% covered (success)
100.00%
1 / 1
1
1<?php
2/**
3 * Proxy endpoint for Jetpack Search
4 *
5 * @package automattic/jetpack
6 */
7
8use Automattic\Jetpack\Search\REST_Controller;
9
10if ( ! defined( 'ABSPATH' ) ) {
11    exit( 0 );
12}
13
14/**
15 * Jetpack Search: Makes authenticated requests to the site search API using blog tokens.
16 * This endpoint will only be used when trying to search private Jetpack and WordPress.com sites.
17 *
18 * @since 9.0.0
19 */
20class WPCOM_REST_API_V2_Endpoint_Search extends WP_REST_Controller {
21    /**
22     * Forward request to controller in Search package.
23     *
24     * @var REST_Controller
25     */
26    protected $controller;
27
28    /**
29     * Constructor.
30     */
31    public function __construct() {
32        $this->namespace  = 'wpcom/v2';
33        $this->rest_base  = 'search';
34        $this->controller = new REST_Controller( defined( 'IS_WPCOM' ) && IS_WPCOM );
35
36        add_action( 'rest_api_init', array( $this, 'register_routes' ) );
37    }
38
39    /**
40     * Called automatically on `rest_api_init()`.
41     */
42    public function register_routes() {
43        register_rest_route(
44            $this->namespace,
45            $this->rest_base,
46            array(
47                'methods'             => WP_REST_Server::READABLE,
48                'callback'            => array( $this->controller, 'get_search_results' ),
49                'permission_callback' => 'is_user_logged_in',
50            )
51        );
52    }
53}
54
55wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Search' );