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

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