class-wp-rest-search-handler.php 2.1 KB

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