Code Coverage
 
Lines
Functions and Methods
Classes and Traits
Total
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 3
CRAP
0.00% covered (danger)
0.00%
0 / 1
WP_REST_Content_Research_Search
0.00% covered (danger)
0.00%
0 / 47
0.00% covered (danger)
0.00%
0 / 3
42
0.00% covered (danger)
0.00%
0 / 1
 __construct
0.00% covered (danger)
0.00%
0 / 2
0.00% covered (danger)
0.00%
0 / 1
2
 register_rest_route
0.00% covered (danger)
0.00%
0 / 29
0.00% covered (danger)
0.00%
0 / 1
2
 get_search_results
0.00% covered (danger)
0.00%
0 / 16
0.00% covered (danger)
0.00%
0 / 1
20
1<?php
2/**
3 * WP_REST_Content_Research_Search file.
4 *
5 * @package automattic/jetpack-mu-wpcom
6 */
7
8namespace A8C\FSE;
9
10use Automattic\Jetpack\Connection\Client;
11
12/**
13 * Class WP_REST_Content_Research_Search.
14 *
15 * Proxies search requests to the WPcom platform endpoint.
16 */
17class WP_REST_Content_Research_Search extends \WP_REST_Controller {
18
19    /**
20     * WP_REST_Content_Research_Search constructor.
21     */
22    public function __construct() {
23        $this->namespace = 'content-research';
24        $this->rest_base = '/search';
25    }
26
27    /**
28     * Register available routes.
29     */
30    public function register_rest_route() {
31        register_rest_route(
32            $this->namespace,
33            $this->rest_base,
34            array(
35                'methods'             => \WP_REST_Server::READABLE,
36                'callback'            => array( $this, 'get_search_results' ),
37                'permission_callback' => 'is_user_logged_in',
38                'args'                => array(
39                    'topic'   => array(
40                        'type'              => 'string',
41                        'required'          => true,
42                        'sanitize_callback' => 'sanitize_text_field',
43                    ),
44                    'sources' => array(
45                        'type'    => 'array',
46                        'default' => array( 'hn', 'reader', 'googlenews' ),
47                        'items'   => array(
48                            'type' => 'string',
49                        ),
50                    ),
51                    'count'   => array(
52                        'type'    => 'integer',
53                        'default' => 10,
54                        'minimum' => 1,
55                        'maximum' => 20,
56                    ),
57                ),
58            )
59        );
60    }
61
62    /**
63     * Proxy the search request to the WPcom platform endpoint.
64     *
65     * @param \WP_REST_Request $request The incoming request.
66     * @return \WP_REST_Response|\WP_Error
67     */
68    public function get_search_results( \WP_REST_Request $request ) {
69        $query_parameters = array(
70            'topic' => $request['topic'],
71        );
72
73        $sources = $request->get_param( 'sources' );
74        if ( ! empty( $sources ) ) {
75            $query_parameters['sources'] = $sources;
76        }
77
78        $count = $request->get_param( 'count' );
79        if ( ! empty( $count ) ) {
80            $query_parameters['count'] = $count;
81        }
82
83        $body = Client::wpcom_json_api_request_as_user(
84            '/content-research/search?' . http_build_query( $query_parameters )
85        );
86
87        if ( is_wp_error( $body ) ) {
88            return $body;
89        }
90
91        $response = json_decode( wp_remote_retrieve_body( $body ) );
92
93        return rest_ensure_response( $response );
94    }
95}