class-acf-ajax-query.php 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. if ( ! class_exists( 'ACF_Ajax_Query' ) ) :
  6. class ACF_Ajax_Query extends ACF_Ajax {
  7. /** @var bool Prevents access for non-logged in users. */
  8. var $public = true;
  9. /** @var int The page of results to return. */
  10. var $page = 1;
  11. /** @var int The number of results per page. */
  12. var $per_page = 20;
  13. /** @var bool Signifies whether or not this AJAX query has more pages to load. */
  14. var $more = false;
  15. /** @var string The searched term. */
  16. var $search = '';
  17. /** @var bool Signifies whether the current query is a search. */
  18. var $is_search = false;
  19. /** @var (int|string) The post_id being edited. */
  20. var $post_id = 0;
  21. /** @var array The ACF field related to this query. */
  22. var $field = false;
  23. /**
  24. * get_response
  25. *
  26. * Returns the response data to sent back.
  27. *
  28. * @date 31/7/18
  29. * @since 5.7.2
  30. *
  31. * @param array $request The request args.
  32. * @return (array|WP_Error) The response data or WP_Error.
  33. */
  34. function get_response( $request ) {
  35. // Init request.
  36. $this->init_request( $request );
  37. // Get query args.
  38. $args = $this->get_args( $request );
  39. // Get query results.
  40. $results = $this->get_results( $args );
  41. if ( is_wp_error( $results ) ) {
  42. return $results;
  43. }
  44. // Return response.
  45. return array(
  46. 'results' => $results,
  47. 'more' => $this->more,
  48. );
  49. }
  50. /**
  51. * init_request
  52. *
  53. * Called at the beginning of a request to setup properties.
  54. *
  55. * @date 23/5/19
  56. * @since 5.8.1
  57. *
  58. * @param array $request The request args.
  59. * @return void
  60. */
  61. function init_request( $request ) {
  62. // Get field for this query.
  63. if ( isset( $request['field_key'] ) ) {
  64. $this->field = acf_get_field( $request['field_key'] );
  65. }
  66. // Update query properties.
  67. if ( isset( $request['page'] ) ) {
  68. $this->page = intval( $request['page'] );
  69. }
  70. if ( isset( $request['per_page'] ) ) {
  71. $this->per_page = intval( $request['per_page'] );
  72. }
  73. if ( isset( $request['search'] ) && acf_not_empty( $request['search'] ) ) {
  74. $this->search = sanitize_text_field( $request['search'] );
  75. $this->is_search = true;
  76. }
  77. if ( isset( $request['post_id'] ) ) {
  78. $this->post_id = $request['post_id'];
  79. }
  80. }
  81. /**
  82. * get_args
  83. *
  84. * Returns an array of args for this query.
  85. *
  86. * @date 31/7/18
  87. * @since 5.7.2
  88. *
  89. * @param array $request The request args.
  90. * @return array
  91. */
  92. function get_args( $request ) {
  93. // Allow for custom "query" arg.
  94. if ( isset( $request['query'] ) ) {
  95. return (array) $request['query'];
  96. }
  97. return array();
  98. }
  99. /**
  100. * get_items
  101. *
  102. * Returns an array of results for the given args.
  103. *
  104. * @date 31/7/18
  105. * @since 5.7.2
  106. *
  107. * @param array args The query args.
  108. * @return array
  109. */
  110. function get_results( $args ) {
  111. return array();
  112. }
  113. /**
  114. * get_item
  115. *
  116. * Returns a single result for the given item object.
  117. *
  118. * @date 31/7/18
  119. * @since 5.7.2
  120. *
  121. * @param mixed $item A single item from the queried results.
  122. * @return array An array containing "id" and "text".
  123. */
  124. function get_result( $item ) {
  125. return false;
  126. }
  127. }
  128. endif; // class_exists check