Code Coverage |
||||||||||
Lines |
Functions and Methods |
Classes and Traits |
||||||||
| Total | |
93.02% |
40 / 43 |
|
66.67% |
2 / 3 |
CRAP | |
0.00% |
0 / 1 |
| WP_REST_Help_Center_Search | |
93.02% |
40 / 43 |
|
66.67% |
2 / 3 |
6.01 | |
0.00% |
0 / 1 |
| __construct | |
100.00% |
3 / 3 |
|
100.00% |
1 / 1 |
1 | |||
| register_rest_route | |
100.00% |
21 / 21 |
|
100.00% |
1 / 1 |
1 | |||
| get_search_results | |
84.21% |
16 / 19 |
|
0.00% |
0 / 1 |
4.06 | |||
| 1 | <?php |
| 2 | /** |
| 3 | * WP_REST_Help_Center_Search file. |
| 4 | * |
| 5 | * @package automattic/jetpack-help-center |
| 6 | */ |
| 7 | |
| 8 | namespace Automattic\Jetpack\Help_Center; |
| 9 | |
| 10 | /** |
| 11 | * Class WP_REST_Help_Center_Search. |
| 12 | */ |
| 13 | class WP_REST_Help_Center_Search extends WP_REST_Help_Center_Controller { |
| 14 | /** |
| 15 | * WP_REST_Help_Center_Search constructor. |
| 16 | * |
| 17 | * @param Wpcom_Request_Client|null $wpcom_request_client WP.com request client. |
| 18 | */ |
| 19 | public function __construct( ?Wpcom_Request_Client $wpcom_request_client = null ) { |
| 20 | parent::__construct( $wpcom_request_client ); |
| 21 | $this->namespace = 'help-center'; |
| 22 | $this->rest_base = '/search'; |
| 23 | } |
| 24 | |
| 25 | /** |
| 26 | * Register available routes. |
| 27 | */ |
| 28 | public function register_rest_route() { |
| 29 | register_rest_route( |
| 30 | $this->namespace, |
| 31 | $this->rest_base, |
| 32 | array( |
| 33 | 'methods' => \WP_REST_Server::READABLE, |
| 34 | 'callback' => array( $this, 'get_search_results' ), |
| 35 | 'permission_callback' => '__return_true', |
| 36 | 'args' => array( |
| 37 | 'query' => array( |
| 38 | 'type' => 'string', |
| 39 | ), |
| 40 | 'locale' => array( |
| 41 | 'type' => 'string', |
| 42 | 'default' => 'en', |
| 43 | ), |
| 44 | 'section' => array( |
| 45 | 'type' => 'string', |
| 46 | ), |
| 47 | ), |
| 48 | ) |
| 49 | ); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Should return the search results |
| 54 | * |
| 55 | * @param \WP_REST_Request $request The request sent to the API. |
| 56 | */ |
| 57 | public function get_search_results( \WP_REST_Request $request ) { |
| 58 | $query = $request['query']; |
| 59 | $locale = $request['locale']; |
| 60 | $section = $request['section']; |
| 61 | $source = $request['source']; |
| 62 | |
| 63 | $query_parameters = array( |
| 64 | 'query' => $query, |
| 65 | 'locale' => $locale, |
| 66 | ); |
| 67 | |
| 68 | if ( ! empty( $section ) ) { |
| 69 | $query_parameters['section'] = $section; |
| 70 | } |
| 71 | |
| 72 | if ( ! empty( $source ) ) { |
| 73 | $query_parameters['source'] = $source; |
| 74 | } |
| 75 | |
| 76 | $body = $this->wpcom_request_client->request( |
| 77 | '/help/search?' . http_build_query( $query_parameters ) |
| 78 | ); |
| 79 | |
| 80 | if ( is_wp_error( $body ) ) { |
| 81 | return $body; |
| 82 | } |
| 83 | |
| 84 | $response = json_decode( wp_remote_retrieve_body( $body ) ); |
| 85 | |
| 86 | return rest_ensure_response( $response ); |
| 87 | } |
| 88 | } |