| 1 |
<?php |
| 2 |
/** |
| 3 |
* REST API: WP_REST_Search_Handler class |
| 4 |
* |
| 5 |
* @package gutenberg |
| 6 |
* @since 3.3.0 |
| 7 |
*/ |
| 8 |
|
| 9 |
/** |
| 10 |
* Core base class representing a search handler for an object type in the REST API. |
| 11 |
* |
| 12 |
* @since 3.3.0 |
| 13 |
*/ |
| 14 |
abstract class WP_REST_Search_Handler { |
| 15 |
|
| 16 |
/** |
| 17 |
* Field containing the IDs in the search result. |
| 18 |
*/ |
| 19 |
const RESULT_IDS = 'ids'; |
| 20 |
|
| 21 |
/** |
| 22 |
* Field containing the total count in the search result. |
| 23 |
*/ |
| 24 |
const RESULT_TOTAL = 'total'; |
| 25 |
|
| 26 |
/** |
| 27 |
* Object type managed by this search handler. |
| 28 |
* |
| 29 |
* @since 3.3.0 |
| 30 |
* @var string |
| 31 |
*/ |
| 32 |
protected $type = ''; |
| 33 |
|
| 34 |
/** |
| 35 |
* Object subtypes managed by this search handler. |
| 36 |
* |
| 37 |
* @since 3.3.0 |
| 38 |
* @var array |
| 39 |
*/ |
| 40 |
protected $subtypes = array(); |
| 41 |
|
| 42 |
/** |
| 43 |
* Gets the object type managed by this search handler. |
| 44 |
* |
| 45 |
* @since 3.3.0 |
| 46 |
* |
| 47 |
* @return string Object type identifier. |
| 48 |
*/ |
| 49 |
public function get_type() { |
| 50 |
return $this->type; |
| 51 |
} |
| 52 |
|
| 53 |
/** |
| 54 |
* Gets the object subtypes managed by this search handler. |
| 55 |
* |
| 56 |
* @since 3.3.0 |
| 57 |
* |
| 58 |
* @return array Array of object subtype identifiers. |
| 59 |
*/ |
| 60 |
public function get_subtypes() { |
| 61 |
return $this->subtypes; |
| 62 |
} |
| 63 |
|
| 64 |
/** |
| 65 |
* Searches the object type content for a given search request. |
| 66 |
* |
| 67 |
* @since 3.3.0 |
| 68 |
* |
| 69 |
* @param WP_REST_Request $request Full REST request. |
| 70 |
* @return array Associative array containing an `WP_REST_Search_Handler::RESULT_IDS` containing |
| 71 |
* an array of found IDs and `WP_REST_Search_Handler::RESULT_TOTAL` containing the |
| 72 |
* total count for the matching search results. |
| 73 |
*/ |
| 74 |
abstract public function search_items( WP_REST_Request $request ); |
| 75 |
|
| 76 |
/** |
| 77 |
* Prepares the search result for a given ID. |
| 78 |
* |
| 79 |
* @since 3.3.0 |
| 80 |
* |
| 81 |
* @param int $id Item ID. |
| 82 |
* @param array $fields Fields to include for the item. |
| 83 |
* @return array Associative array containing all fields for the item. |
| 84 |
*/ |
| 85 |
abstract public function prepare_item( $id, array $fields ); |
| 86 |
|
| 87 |
/** |
| 88 |
* Prepares links for the search result of a given ID. |
| 89 |
* |
| 90 |
* @since 3.3.0 |
| 91 |
* |
| 92 |
* @param int $id Item ID. |
| 93 |
* @return array Links for the given item. |
| 94 |
*/ |
| 95 |
abstract public function prepare_item_links( $id ); |
| 96 |
} |
| 97 |
|