暂无描述

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

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. 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. * @since 5.6.0 The `$id` parameter can accept a string.
  73. *
  74. * @param int|string $id Item ID.
  75. * @param array $fields Fields to include for the item.
  76. * @return array Associative array containing all fields for the item.
  77. */
  78. abstract public function prepare_item( $id, array $fields );
  79. /**
  80. * Prepares links for the search result of a given ID.
  81. *
  82. * @since 5.0.0
  83. * @since 5.6.0 The `$id` parameter can accept a string.
  84. *
  85. * @param int|string $id Item ID.
  86. * @return array Links for the given item.
  87. */
  88. abstract public function prepare_item_links( $id );
  89. }