← All changes
|
_inc/lib/core-api/wpcom-endpoints/class-wpcom-rest-api-v2-endpoint-search.php
+60
-0
16.2-beta
→
16.3
View file →
| @@ -1,0 +1,60 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * Proxy endpoint for Jetpack Search | |
| 4 | + * | |
| 5 | + * @package automattic/jetpack | |
| 6 | + */ | |
| 7 | + | |
| 8 | +use Automattic\Jetpack\Search\REST_Controller; | |
| 9 | + | |
| 10 | +if ( ! 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 | + */ | |
| 20 | +class 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 | + | |
| 60 | +wpcom_rest_api_v2_load_plugin( 'WPCOM_REST_API_V2_Endpoint_Search' ); | |