Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
81.25% covered (warning)
81.25%
13 / 16
100.00% covered (success)
100.00%
2 / 2
CRAP
100.00% covered (success)
100.00%
1 / 1
WPCOM_REST_API_V2_Endpoint_Search
100.00% covered (success)
100.00%
13 / 13
100.00% covered (success)
100.00%
2 / 2
3
100.00% covered (success)
100.00%
1 / 1
 __construct
100.00% covered (success)
100.00%
3 / 3
100.00% covered (success)
100.00%
1 / 1
1
 register_routes
100.00% covered (success)
100.00%
10 / 10
100.00% covered (success)
100.00%
1 / 1
2
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
35        add_action( 'rest_api_init', array( $this, 'register_routes' ) );
36    }
37
38    /**
39     * Called automatically on `rest_api_init()`.
40     *
41     * The Search package controller is instantiated here rather than in the
42     * constructor so that the package class is only loaded when the REST API is
43     * actually in use, not on every front-end, cron, or login request.
44     */
45    public function register_routes() {
46        $this->controller = new REST_Controller( defined( 'IS_WPCOM' ) && IS_WPCOM );
47
48        register_rest_route(
49            $this->namespace,
50            $this->rest_base,
51            array(
52                'methods'             => WP_REST_Server::READABLE,
53                'callback'            => array( $this->controller, 'get_search_results' ),
54                'permission_callback' => 'is_user_logged_in',
55            )
56        );
57    }
58}
59
60wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Search' );