Conditions | 69 |
Paths | > 20000 |
Total Lines | 326 |
Code Lines | 179 |
Lines | 56 |
Ratio | 17.18 % |
Changes | 2 | ||
Bugs | 0 | Features | 1 |
Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.
For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.
Commonly applied refactorings include:
If many parameters/temporary variables are present:
1 | <?php |
||
208 | public function prepare_query( $query = array() ) { |
||
209 | if ( empty( $this->query_vars ) || ! empty( $query ) ) { |
||
210 | $this->query_limit = null; |
||
211 | $this->query_vars = $this->fill_query_vars( $query ); |
||
|
|||
212 | } |
||
213 | |||
214 | /** |
||
215 | * Fires before the WP_User_Query has been parsed. |
||
216 | * |
||
217 | * The passed WP_User_Query object contains the query variables, not |
||
218 | * yet passed into SQL. |
||
219 | * |
||
220 | * @since 4.0.0 |
||
221 | * |
||
222 | * @param WP_User_Query $this The current WP_User_Query instance, |
||
223 | * passed by reference. |
||
224 | */ |
||
225 | do_action( 'pre_get_users', $this ); |
||
226 | |||
227 | // Ensure that query vars are filled after 'pre_get_users'. |
||
228 | $qv =& $this->query_vars; |
||
229 | $qv = $this->fill_query_vars( $qv ); |
||
230 | |||
231 | if ( is_array( $qv['fields'] ) ) { |
||
232 | $qv['fields'] = array_unique( $qv['fields'] ); |
||
233 | |||
234 | $this->query_fields = array(); |
||
235 | foreach ( $qv['fields'] as $field ) { |
||
236 | $field = 'ID' === $field ? 'ID' : sanitize_key( $field ); |
||
237 | $this->query_fields[] = "{$this->db->users}.$field"; |
||
238 | } |
||
239 | $this->query_fields = implode( ',', $this->query_fields ); |
||
240 | } elseif ( 'all' == $qv['fields'] ) { |
||
241 | $this->query_fields = "{$this->db->users}.*"; |
||
242 | } else { |
||
243 | $this->query_fields = "{$this->db->users}.ID"; |
||
244 | } |
||
245 | |||
246 | if ( isset( $qv['count_total'] ) && $qv['count_total'] ) |
||
247 | $this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields; |
||
248 | |||
249 | $this->query_from = "FROM {$this->db->users}"; |
||
250 | $this->query_where = "WHERE 1=1"; |
||
251 | |||
252 | // Parse and sanitize 'include', for use by 'orderby' as well as 'include' below. |
||
253 | if ( ! empty( $qv['include'] ) ) { |
||
254 | $include = wp_parse_id_list( $qv['include'] ); |
||
255 | } else { |
||
256 | $include = false; |
||
257 | } |
||
258 | |||
259 | $blog_id = 0; |
||
260 | if ( isset( $qv['blog_id'] ) ) { |
||
261 | $blog_id = absint( $qv['blog_id'] ); |
||
262 | } |
||
263 | |||
264 | if ( $qv['has_published_posts'] && $blog_id ) { |
||
265 | if ( true === $qv['has_published_posts'] ) { |
||
266 | $post_types = get_post_types( array( 'public' => true ) ); |
||
267 | } else { |
||
268 | $post_types = (array) $qv['has_published_posts']; |
||
269 | } |
||
270 | |||
271 | foreach ( $post_types as &$post_type ) { |
||
272 | $post_type = $this->db->prepare( '%s', $post_type ); |
||
273 | } |
||
274 | |||
275 | $posts_table = $this->db->get_blog_prefix( $blog_id ) . 'posts'; |
||
276 | $this->query_where .= " AND {$this->db->users}.ID IN ( SELECT DISTINCT $posts_table.post_author FROM $posts_table WHERE $posts_table.post_status = 'publish' AND $posts_table.post_type IN ( " . join( ", ", $post_types ) . " ) )"; |
||
277 | } |
||
278 | |||
279 | // Meta query. |
||
280 | $this->meta_query = new WP_Meta_Query(); |
||
281 | $this->meta_query->parse_query_vars( $qv ); |
||
282 | |||
283 | if ( isset( $qv['who'] ) && 'authors' == $qv['who'] && $blog_id ) { |
||
284 | $who_query = array( |
||
285 | 'key' => $this->db->get_blog_prefix( $blog_id ) . 'user_level', |
||
286 | 'value' => 0, |
||
287 | 'compare' => '!=', |
||
288 | ); |
||
289 | |||
290 | // Prevent extra meta query. |
||
291 | $qv['blog_id'] = $blog_id = 0; |
||
292 | |||
293 | View Code Duplication | if ( empty( $this->meta_query->queries ) ) { |
|
294 | $this->meta_query->queries = array( $who_query ); |
||
295 | } else { |
||
296 | // Append the cap query to the original queries and reparse the query. |
||
297 | $this->meta_query->queries = array( |
||
298 | 'relation' => 'AND', |
||
299 | array( $this->meta_query->queries, $who_query ), |
||
300 | ); |
||
301 | } |
||
302 | |||
303 | $this->meta_query->parse_query_vars( $this->meta_query->queries ); |
||
304 | } |
||
305 | |||
306 | $roles = array(); |
||
307 | if ( isset( $qv['role'] ) ) { |
||
308 | View Code Duplication | if ( is_array( $qv['role'] ) ) { |
|
309 | $roles = $qv['role']; |
||
310 | } elseif ( is_string( $qv['role'] ) && ! empty( $qv['role'] ) ) { |
||
311 | $roles = array_map( 'trim', explode( ',', $qv['role'] ) ); |
||
312 | } |
||
313 | } |
||
314 | |||
315 | $role__in = array(); |
||
316 | if ( isset( $qv['role__in'] ) ) { |
||
317 | $role__in = (array) $qv['role__in']; |
||
318 | } |
||
319 | |||
320 | $role__not_in = array(); |
||
321 | if ( isset( $qv['role__not_in'] ) ) { |
||
322 | $role__not_in = (array) $qv['role__not_in']; |
||
323 | } |
||
324 | |||
325 | if ( $blog_id && ( ! empty( $roles ) || ! empty( $role__in ) || ! empty( $role__not_in ) || is_multisite() ) ) { |
||
326 | $role_queries = array(); |
||
327 | |||
328 | $roles_clauses = array( 'relation' => 'AND' ); |
||
329 | View Code Duplication | if ( ! empty( $roles ) ) { |
|
330 | foreach ( $roles as $role ) { |
||
331 | $roles_clauses[] = array( |
||
332 | 'key' => $this->db->get_blog_prefix( $blog_id ) . 'capabilities', |
||
333 | 'value' => '"' . $role . '"', |
||
334 | 'compare' => 'LIKE', |
||
335 | ); |
||
336 | } |
||
337 | |||
338 | $role_queries[] = $roles_clauses; |
||
339 | } |
||
340 | |||
341 | $role__in_clauses = array( 'relation' => 'OR' ); |
||
342 | View Code Duplication | if ( ! empty( $role__in ) ) { |
|
343 | foreach ( $role__in as $role ) { |
||
344 | $role__in_clauses[] = array( |
||
345 | 'key' => $this->db->get_blog_prefix( $blog_id ) . 'capabilities', |
||
346 | 'value' => '"' . $role . '"', |
||
347 | 'compare' => 'LIKE', |
||
348 | ); |
||
349 | } |
||
350 | |||
351 | $role_queries[] = $role__in_clauses; |
||
352 | } |
||
353 | |||
354 | $role__not_in_clauses = array( 'relation' => 'AND' ); |
||
355 | View Code Duplication | if ( ! empty( $role__not_in ) ) { |
|
356 | foreach ( $role__not_in as $role ) { |
||
357 | $role__not_in_clauses[] = array( |
||
358 | 'key' => $this->db->get_blog_prefix( $blog_id ) . 'capabilities', |
||
359 | 'value' => '"' . $role . '"', |
||
360 | 'compare' => 'NOT LIKE', |
||
361 | ); |
||
362 | } |
||
363 | |||
364 | $role_queries[] = $role__not_in_clauses; |
||
365 | } |
||
366 | |||
367 | // If there are no specific roles named, make sure the user is a member of the site. |
||
368 | if ( empty( $role_queries ) ) { |
||
369 | $role_queries[] = array( |
||
370 | 'key' => $this->db->get_blog_prefix( $blog_id ) . 'capabilities', |
||
371 | 'compare' => 'EXISTS', |
||
372 | ); |
||
373 | } |
||
374 | |||
375 | // Specify that role queries should be joined with AND. |
||
376 | $role_queries['relation'] = 'AND'; |
||
377 | |||
378 | View Code Duplication | if ( empty( $this->meta_query->queries ) ) { |
|
379 | $this->meta_query->queries = $role_queries; |
||
380 | } else { |
||
381 | // Append the cap query to the original queries and reparse the query. |
||
382 | $this->meta_query->queries = array( |
||
383 | 'relation' => 'AND', |
||
384 | array( $this->meta_query->queries, $role_queries ), |
||
385 | ); |
||
386 | } |
||
387 | |||
388 | $this->meta_query->parse_query_vars( $this->meta_query->queries ); |
||
389 | } |
||
390 | |||
391 | if ( ! empty( $this->meta_query->queries ) ) { |
||
392 | $clauses = $this->meta_query->get_sql( 'user', $this->db->users, 'ID', $this ); |
||
393 | $this->query_from .= $clauses['join']; |
||
394 | $this->query_where .= $clauses['where']; |
||
395 | |||
396 | if ( $this->meta_query->has_or_relation() ) { |
||
397 | $this->query_fields = 'DISTINCT ' . $this->query_fields; |
||
398 | } |
||
399 | } |
||
400 | |||
401 | // sorting |
||
402 | $qv['order'] = isset( $qv['order'] ) ? strtoupper( $qv['order'] ) : ''; |
||
403 | $order = $this->parse_order( $qv['order'] ); |
||
404 | |||
405 | if ( empty( $qv['orderby'] ) ) { |
||
406 | // Default order is by 'user_login'. |
||
407 | $ordersby = array( 'user_login' => $order ); |
||
408 | } elseif ( is_array( $qv['orderby'] ) ) { |
||
409 | $ordersby = $qv['orderby']; |
||
410 | } else { |
||
411 | // 'orderby' values may be a comma- or space-separated list. |
||
412 | $ordersby = preg_split( '/[,\s]+/', $qv['orderby'] ); |
||
413 | } |
||
414 | |||
415 | $orderby_array = array(); |
||
416 | foreach ( $ordersby as $_key => $_value ) { |
||
417 | if ( ! $_value ) { |
||
418 | continue; |
||
419 | } |
||
420 | |||
421 | if ( is_int( $_key ) ) { |
||
422 | // Integer key means this is a flat array of 'orderby' fields. |
||
423 | $_orderby = $_value; |
||
424 | $_order = $order; |
||
425 | } else { |
||
426 | // Non-integer key means this the key is the field and the value is ASC/DESC. |
||
427 | $_orderby = $_key; |
||
428 | $_order = $_value; |
||
429 | } |
||
430 | |||
431 | $parsed = $this->parse_orderby( $_orderby ); |
||
432 | |||
433 | if ( ! $parsed ) { |
||
434 | continue; |
||
435 | } |
||
436 | |||
437 | $orderby_array[] = $parsed . ' ' . $this->parse_order( $_order ); |
||
438 | } |
||
439 | |||
440 | // If no valid clauses were found, order by user_login. |
||
441 | if ( empty( $orderby_array ) ) { |
||
442 | $orderby_array[] = "user_login $order"; |
||
443 | } |
||
444 | |||
445 | $this->query_orderby = 'ORDER BY ' . implode( ', ', $orderby_array ); |
||
446 | |||
447 | // limit |
||
448 | if ( isset( $qv['number'] ) && $qv['number'] > 0 ) { |
||
449 | if ( $qv['offset'] ) { |
||
450 | $this->query_limit = $this->db->prepare("LIMIT %d, %d", $qv['offset'], $qv['number']); |
||
451 | } else { |
||
452 | $this->query_limit = $this->db->prepare( "LIMIT %d, %d", $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] ); |
||
453 | } |
||
454 | } |
||
455 | |||
456 | $search = ''; |
||
457 | if ( isset( $qv['search'] ) ) |
||
458 | $search = trim( $qv['search'] ); |
||
459 | |||
460 | if ( $search ) { |
||
461 | $leading_wild = ( ltrim($search, '*') != $search ); |
||
462 | $trailing_wild = ( rtrim($search, '*') != $search ); |
||
463 | if ( $leading_wild && $trailing_wild ) |
||
464 | $wild = 'both'; |
||
465 | elseif ( $leading_wild ) |
||
466 | $wild = 'leading'; |
||
467 | elseif ( $trailing_wild ) |
||
468 | $wild = 'trailing'; |
||
469 | else |
||
470 | $wild = false; |
||
471 | if ( $wild ) |
||
472 | $search = trim($search, '*'); |
||
473 | |||
474 | $search_columns = array(); |
||
475 | if ( $qv['search_columns'] ) |
||
476 | $search_columns = array_intersect( $qv['search_columns'], array( 'ID', 'user_login', 'user_email', 'user_url', 'user_nicename' ) ); |
||
477 | if ( ! $search_columns ) { |
||
478 | if ( false !== strpos( $search, '@') ) |
||
479 | $search_columns = array('user_email'); |
||
480 | elseif ( is_numeric($search) ) |
||
481 | $search_columns = array('user_login', 'ID'); |
||
482 | elseif ( preg_match('|^https?://|', $search) && ! ( is_multisite() && wp_is_large_network( 'users' ) ) ) |
||
483 | $search_columns = array('user_url'); |
||
484 | else |
||
485 | $search_columns = array('user_login', 'user_url', 'user_email', 'user_nicename', 'display_name'); |
||
486 | } |
||
487 | |||
488 | /** |
||
489 | * Filters the columns to search in a WP_User_Query search. |
||
490 | * |
||
491 | * The default columns depend on the search term, and include 'user_email', |
||
492 | * 'user_login', 'ID', 'user_url', 'display_name', and 'user_nicename'. |
||
493 | * |
||
494 | * @since 3.6.0 |
||
495 | * |
||
496 | * @param array $search_columns Array of column names to be searched. |
||
497 | * @param string $search Text being searched. |
||
498 | * @param WP_User_Query $this The current WP_User_Query instance. |
||
499 | */ |
||
500 | $search_columns = apply_filters( 'user_search_columns', $search_columns, $search, $this ); |
||
501 | |||
502 | $this->query_where .= $this->get_search_sql( $search, $search_columns, $wild ); |
||
503 | } |
||
504 | |||
505 | if ( ! empty( $include ) ) { |
||
506 | // Sanitized earlier. |
||
507 | $ids = implode( ',', $include ); |
||
508 | $this->query_where .= " AND {$this->db->users}.ID IN ($ids)"; |
||
509 | } elseif ( ! empty( $qv['exclude'] ) ) { |
||
510 | $ids = implode( ',', wp_parse_id_list( $qv['exclude'] ) ); |
||
511 | $this->query_where .= " AND {$this->db->users}.ID NOT IN ($ids)"; |
||
512 | } |
||
513 | |||
514 | // Date queries are allowed for the user_registered field. |
||
515 | if ( ! empty( $qv['date_query'] ) && is_array( $qv['date_query'] ) ) { |
||
516 | $date_query = new WP_Date_Query( $qv['date_query'], 'user_registered' ); |
||
517 | $this->query_where .= $date_query->get_sql(); |
||
518 | } |
||
519 | |||
520 | /** |
||
521 | * Fires after the WP_User_Query has been parsed, and before |
||
522 | * the query is executed. |
||
523 | * |
||
524 | * The passed WP_User_Query object contains SQL parts formed |
||
525 | * from parsing the given query. |
||
526 | * |
||
527 | * @since 3.1.0 |
||
528 | * |
||
529 | * @param WP_User_Query $this The current WP_User_Query instance, |
||
530 | * passed by reference. |
||
531 | */ |
||
532 | do_action_ref_array( 'pre_user_query', array( &$this ) ); |
||
533 | } |
||
534 | |||
809 |
This check looks at variables that have been passed in as parameters and are passed out again to other methods.
If the outgoing method call has stricter type requirements than the method itself, an issue is raised.
An additional type check may prevent trouble.