class-wp-rest-post-format-search-handler.php 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128
  1. <?php
  2. /**
  3. * REST API: WP_REST_Post_Format_Search_Handler class
  4. *
  5. * @package WordPress
  6. * @subpackage REST_API
  7. * @since 5.6.0
  8. */
  9. /**
  10. * Core class representing a search handler for post formats in the REST API.
  11. *
  12. * @since 5.6.0
  13. *
  14. * @see WP_REST_Search_Handler
  15. */
  16. class WP_REST_Post_Format_Search_Handler extends WP_REST_Search_Handler {
  17. /**
  18. * Constructor.
  19. *
  20. * @since 5.6.0
  21. */
  22. public function __construct() {
  23. $this->type = 'post-format';
  24. }
  25. /**
  26. * Searches the object type content for a given search request.
  27. *
  28. * @since 5.6.0
  29. *
  30. * @param WP_REST_Request $request Full REST request.
  31. * @return array Associative array containing an `WP_REST_Search_Handler::RESULT_IDS` containing
  32. * an array of found IDs and `WP_REST_Search_Handler::RESULT_TOTAL` containing the
  33. * total count for the matching search results.
  34. */
  35. public function search_items( WP_REST_Request $request ) {
  36. $format_strings = get_post_format_strings();
  37. $format_slugs = array_keys( $format_strings );
  38. $query_args = array();
  39. if ( ! empty( $request['search'] ) ) {
  40. $query_args['search'] = $request['search'];
  41. }
  42. /**
  43. * Filters the query arguments for a REST API search request.
  44. *
  45. * Enables adding extra arguments or setting defaults for a post format search request.
  46. *
  47. * @since 5.6.0
  48. *
  49. * @param array $query_args Key value array of query var to query value.
  50. * @param WP_REST_Request $request The request used.
  51. */
  52. $query_args = apply_filters( 'rest_post_format_search_query', $query_args, $request );
  53. $found_ids = array();
  54. foreach ( $format_slugs as $index => $format_slug ) {
  55. if ( ! empty( $query_args['search'] ) ) {
  56. $format_string = get_post_format_string( $format_slug );
  57. $format_slug_match = stripos( $format_slug, $query_args['search'] ) !== false;
  58. $format_string_match = stripos( $format_string, $query_args['search'] ) !== false;
  59. if ( ! $format_slug_match && ! $format_string_match ) {
  60. continue;
  61. }
  62. }
  63. $format_link = get_post_format_link( $format_slug );
  64. if ( $format_link ) {
  65. $found_ids[] = $format_slug;
  66. }
  67. }
  68. $page = (int) $request['page'];
  69. $per_page = (int) $request['per_page'];
  70. return array(
  71. self::RESULT_IDS => array_slice( $found_ids, ( $page - 1 ) * $per_page, $per_page ),
  72. self::RESULT_TOTAL => count( $found_ids ),
  73. );
  74. }
  75. /**
  76. * Prepares the search result for a given ID.
  77. *
  78. * @since 5.6.0
  79. *
  80. * @param string $id Item ID, the post format slug.
  81. * @param array $fields Fields to include for the item.
  82. * @return array Associative array containing all fields for the item.
  83. */
  84. public function prepare_item( $id, array $fields ) {
  85. $data = array();
  86. if ( in_array( WP_REST_Search_Controller::PROP_ID, $fields, true ) ) {
  87. $data[ WP_REST_Search_Controller::PROP_ID ] = $id;
  88. }
  89. if ( in_array( WP_REST_Search_Controller::PROP_TITLE, $fields, true ) ) {
  90. $data[ WP_REST_Search_Controller::PROP_TITLE ] = get_post_format_string( $id );
  91. }
  92. if ( in_array( WP_REST_Search_Controller::PROP_URL, $fields, true ) ) {
  93. $data[ WP_REST_Search_Controller::PROP_URL ] = get_post_format_link( $id );
  94. }
  95. if ( in_array( WP_REST_Search_Controller::PROP_TYPE, $fields, true ) ) {
  96. $data[ WP_REST_Search_Controller::PROP_TYPE ] = $this->type;
  97. }
  98. return $data;
  99. }
  100. /**
  101. * Prepares links for the search result.
  102. *
  103. * @since 5.6.0
  104. *
  105. * @param string $id Item ID, the post format slug.
  106. * @return array Links for the given item.
  107. */
  108. public function prepare_item_links( $id ) {
  109. return array();
  110. }
  111. }