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