PluginProbe ʕ •ᴥ•ʔ
Secure Custom Fields / 6.4.1-beta6
Secure Custom Fields v6.4.1-beta6
6.9.5 6.9.4 6.9.3 6.9.2 6.9.1 6.9.0 6.8.9 6.8.7 6.8.8 6.8.6 6.8.4 6.8.5 trunk 6.4.0-beta1 6.4.0-beta2 6.4.1 6.4.1-beta3 6.4.1-beta4 6.4.1-beta5 6.4.1-beta6 6.4.1-beta7 6.4.2 6.5.0 6.5.1 6.5.2 6.5.3 6.5.4 6.5.5 6.5.6 6.5.7 6.6.0 6.7.0 6.7.1 6.8.0 6.8.1 6.8.2 6.8.3
secure-custom-fields / includes / ajax / class-acf-ajax-query-users.php
secure-custom-fields / includes / ajax Last commit date
class-acf-ajax-check-screen.php 1 year ago class-acf-ajax-local-json-diff.php 1 year ago class-acf-ajax-query-users.php 1 year ago class-acf-ajax-query.php 1 year ago class-acf-ajax-upgrade.php 1 year ago class-acf-ajax-user-setting.php 1 year ago class-acf-ajax.php 1 year ago index.php 1 year ago
class-acf-ajax-query-users.php
307 lines
1 <?php
2
3 if ( ! defined( 'ABSPATH' ) ) {
4 exit; // Exit if accessed directly
5 }
6
7 if ( ! class_exists( 'ACF_Ajax_Query_Users' ) ) :
8
9 class ACF_Ajax_Query_Users extends ACF_Ajax_Query {
10
11 /** @var string The AJAX action name. */
12 var $action = 'acf/ajax/query_users';
13
14 /**
15 * Verifies the request.
16 *
17 * @since ACF 6.3.2
18 *
19 * @param array $request The request args.
20 * @return (bool|WP_Error) True on success, WP_Error on fail.
21 */
22 public function verify_request( $request ) {
23 if ( empty( $request['nonce'] ) || empty( $request['field_key'] ) ) {
24 return new WP_Error( 'acf_invalid_args', __( 'Invalid request args.', 'secure-custom-fields' ), array( 'status' => 404 ) );
25 }
26
27 $nonce = $request['nonce'];
28 $action = $request['field_key'];
29
30 if ( isset( $request['conditional_logic'] ) && true === (bool) $request['conditional_logic'] ) {
31 if ( ! acf_current_user_can_admin() ) {
32 return new WP_Error( 'acf_invalid_permissions', __( 'Sorry, you do not have permission to do that.', 'secure-custom-fields' ) );
33 }
34
35 // Use the standard ACF admin nonce.
36 $nonce = '';
37 $action = '';
38 }
39
40 if ( ! acf_verify_ajax( $nonce, $action ) ) {
41 return new WP_Error( 'acf_invalid_nonce', __( 'Invalid nonce.', 'secure-custom-fields' ), array( 'status' => 404 ) );
42 }
43
44 return true;
45 }
46
47 /**
48 * init_request
49 *
50 * Called at the beginning of a request to setup properties.
51 *
52 * @date 23/5/19
53 * @since ACF 5.8.1
54 *
55 * @param array $request The request args.
56 * @return void
57 */
58 function init_request( $request ) {
59 parent::init_request( $request );
60
61 // Customize query.
62 add_filter( 'user_search_columns', array( $this, 'filter_search_columns' ), 10, 3 );
63
64 /**
65 * Fires when a request is made.
66 *
67 * @date 21/5/19
68 * @since ACF 5.8.1
69 *
70 * @param array $request The query request.
71 * @param ACF_Ajax_Query $query The query object.
72 */
73 do_action( 'acf/ajax/query_users/init', $request, $this );
74 }
75
76 /**
77 * get_args
78 *
79 * Returns an array of args for this query.
80 *
81 * @date 31/7/18
82 * @since ACF 5.7.2
83 *
84 * @param array $request The request args.
85 * @return array
86 */
87 function get_args( $request ) {
88 $args = parent::get_args( $request );
89 $args['number'] = $this->per_page;
90 $args['paged'] = $this->page;
91 if ( $this->is_search ) {
92 $args['search'] = "*{$this->search}*";
93 }
94
95 /**
96 * Filters the query args.
97 *
98 * @date 21/5/19
99 * @since ACF 5.8.1
100 *
101 * @param array $args The query args.
102 * @param array $request The query request.
103 * @param ACF_Ajax_Query $query The query object.
104 */
105 return apply_filters( 'acf/ajax/query_users/args', $args, $request, $this );
106 }
107
108 /**
109 * Prepares args for the get_results() method.
110 *
111 * @date 23/3/20
112 * @since ACF 5.8.9
113 *
114 * @param array args The query args.
115 * @return array
116 */
117 function prepare_args( $args ) {
118
119 // Parse pagination args that may have been modified.
120 if ( isset( $args['users_per_page'] ) ) {
121 $this->per_page = intval( $args['users_per_page'] );
122 unset( $args['users_per_page'] );
123 } elseif ( isset( $args['number'] ) ) {
124 $this->per_page = intval( $args['number'] );
125 }
126
127 if ( isset( $args['paged'] ) ) {
128 $this->page = intval( $args['paged'] );
129 unset( $args['paged'] );
130 }
131
132 // Set pagination args for fine control.
133 $args['number'] = $this->per_page;
134 $args['offset'] = $this->per_page * ( $this->page - 1 );
135 $args['count_total'] = true;
136 return $args;
137 }
138
139 /**
140 * get_results
141 *
142 * Returns an array of results for the given args.
143 *
144 * @date 31/7/18
145 * @since ACF 5.7.2
146 *
147 * @param array args The query args.
148 * @return array
149 */
150 function get_results( $args ) {
151 $results = array();
152
153 // Prepare args for quey.
154 $args = $this->prepare_args( $args );
155
156 // Get result groups.
157 if ( ! empty( $args['role__in'] ) ) {
158 $roles = acf_get_user_role_labels( $args['role__in'] );
159 } else {
160 $roles = acf_get_user_role_labels();
161 }
162
163 // Return a flat array of results when searching or when queriying one group only.
164 if ( $this->is_search || count( $roles ) === 1 ) {
165
166 // Query users and append to results.
167 $wp_user_query = new WP_User_Query( $args );
168 $users = (array) $wp_user_query->get_results();
169 $total_users = $wp_user_query->get_total();
170 foreach ( $users as $user ) {
171 $results[] = $this->get_result( $user );
172 }
173
174 // Determine if more results exist.
175 // As this query does not return grouped results, the calculation can be exact (">").
176 $this->more = ( $total_users > count( $users ) + $args['offset'] );
177 // Otherwise, group results via role.
178 } else {
179
180 // Unset args that will interfer with query results.
181 unset( $args['role__in'], $args['role__not_in'] );
182
183 $args['search'] = $this->search ? $this->search : '';
184
185 // Loop over each role.
186 foreach ( $roles as $role => $role_label ) {
187
188 // Query users (for this role only).
189 $args['role'] = $role;
190 $wp_user_query = new WP_User_Query( $args );
191 $users = (array) $wp_user_query->get_results();
192 $total_users = $wp_user_query->get_total();
193
194 // acf_log( $args );
195 // acf_log( '- ', count($users) );
196 // acf_log( '- ', $total_users );
197 // If users were found for this query...
198 if ( $users ) {
199
200 // Append optgroup of results.
201 $role_results = array();
202 foreach ( $users as $user ) {
203 $role_results[] = $this->get_result( $user );
204 }
205 $results[] = array(
206 'text' => $role_label,
207 'children' => $role_results,
208 );
209
210 // End loop when enough results have been found.
211 if ( count( $users ) === $args['number'] ) {
212
213 // Determine if more results exist.
214 // As this query does return grouped results, the calculation is best left fuzzy to avoid querying the next group (">=").
215 $this->more = ( $total_users >= count( $users ) + $args['offset'] );
216 break;
217
218 // Otherwise, modify the args so that the next query can continue on correctly.
219 } else {
220 $args['offset'] = 0;
221 $args['number'] -= count( $users );
222 }
223
224 // If no users were found (for the current pagination args), but there were users found for previous pages...
225 // Modify the args so that the next query is offset slightly less (the number of total users) and can continue on correctly.
226 } elseif ( $total_users ) {
227 $args['offset'] -= $total_users;
228 continue;
229
230 // Ignore roles that will never return a result.
231 } else {
232 continue;
233 }
234 }
235 }
236
237 /**
238 * Filters the query results.
239 *
240 * @date 21/5/19
241 * @since ACF 5.8.1
242 *
243 * @param array $results The query results.
244 * @param array $args The query args.
245 * @param ACF_Ajax_Query $query The query object.
246 */
247 return apply_filters( 'acf/ajax/query_users/results', $results, $args, $this );
248 }
249
250 /**
251 * get_result
252 *
253 * Returns a single result for the given item object.
254 *
255 * @date 31/7/18
256 * @since ACF 5.7.2
257 *
258 * @param mixed $item A single item from the queried results.
259 * @return string
260 */
261 function get_result( $user ) {
262 $item = acf_get_user_result( $user );
263
264 /**
265 * Filters the result item.
266 *
267 * @date 21/5/19
268 * @since ACF 5.8.1
269 *
270 * @param array $item The choice id and text.
271 * @param ACF_User $user The user object.
272 * @param ACF_Ajax_Query $query The query object.
273 */
274 return apply_filters( 'acf/ajax/query_users/result', $item, $user, $this );
275 }
276
277 /**
278 * Filters the WP_User_Query search columns.
279 *
280 * @date 9/3/20
281 * @since ACF 5.8.8
282 *
283 * @param array $columns An array of column names to be searched.
284 * @param string $search The search term.
285 * @param WP_User_Query $WP_User_Query The WP_User_Query instance.
286 * @return array
287 */
288 function filter_search_columns( $columns, $search, $WP_User_Query ) {
289
290 /**
291 * Filters the column names to be searched.
292 *
293 * @date 21/5/19
294 * @since ACF 5.8.1
295 *
296 * @param array $columns An array of column names to be searched.
297 * @param string $search The search term.
298 * @param WP_User_Query $WP_User_Query The WP_User_Query instance.
299 * @param ACF_Ajax_Query $query The query object.
300 */
301 return apply_filters( 'acf/ajax/query_users/search_columns', $columns, $search, $WP_User_Query, $this );
302 }
303 }
304
305 acf_new_instance( 'ACF_Ajax_Query_Users' );
306 endif; // class_exists check
307