Changeset 38275 for trunk/src/wp-includes/class-wp-user-query.php
- Timestamp:
- 08/18/2016 06:20:55 PM (8 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-user-query.php
r37492 r38275 72 72 73 73 /** 74 * @since 4.7.0 75 * @access protected 76 * @var wpdb 77 */ 78 protected $db; 79 80 /** 74 81 * PHP5 constructor. 75 82 * … … 79 86 */ 80 87 public function __construct( $query = null ) { 88 $this->db = $GLOBALS['wpdb']; 89 81 90 if ( ! empty( $query ) ) { 82 91 $this->prepare_query( $query ); … … 135 144 * @access public 136 145 * 137 * @global wpdb $wpdb WordPress database abstraction object.138 146 * @global int $blog_id 139 147 * … … 199 207 */ 200 208 public function prepare_query( $query = array() ) { 201 global $wpdb;202 203 209 if ( empty( $this->query_vars ) || ! empty( $query ) ) { 204 210 $this->query_limit = null; … … 229 235 foreach ( $qv['fields'] as $field ) { 230 236 $field = 'ID' === $field ? 'ID' : sanitize_key( $field ); 231 $this->query_fields[] = " $wpdb->users.$field";237 $this->query_fields[] = "{$this->db->users}.$field"; 232 238 } 233 239 $this->query_fields = implode( ',', $this->query_fields ); 234 240 } elseif ( 'all' == $qv['fields'] ) { 235 $this->query_fields = " $wpdb->users.*";241 $this->query_fields = "{$this->db->users}.*"; 236 242 } else { 237 $this->query_fields = " $wpdb->users.ID";243 $this->query_fields = "{$this->db->users}.ID"; 238 244 } 239 245 … … 241 247 $this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields; 242 248 243 $this->query_from = "FROM $wpdb->users";249 $this->query_from = "FROM {$this->db->users}"; 244 250 $this->query_where = "WHERE 1=1"; 245 251 … … 264 270 265 271 foreach ( $post_types as &$post_type ) { 266 $post_type = $ wpdb->prepare( '%s', $post_type );267 } 268 269 $posts_table = $ wpdb->get_blog_prefix( $blog_id ) . 'posts';270 $this->query_where .= " AND $wpdb->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 ) . " ) )";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 ) . " ) )"; 271 277 } 272 278 … … 277 283 if ( isset( $qv['who'] ) && 'authors' == $qv['who'] && $blog_id ) { 278 284 $who_query = array( 279 'key' => $ wpdb->get_blog_prefix( $blog_id ) . 'user_level',285 'key' => $this->db->get_blog_prefix( $blog_id ) . 'user_level', 280 286 'value' => 0, 281 287 'compare' => '!=', … … 324 330 foreach ( $roles as $role ) { 325 331 $roles_clauses[] = array( 326 'key' => $ wpdb->get_blog_prefix( $blog_id ) . 'capabilities',332 'key' => $this->db->get_blog_prefix( $blog_id ) . 'capabilities', 327 333 'value' => '"' . $role . '"', 328 334 'compare' => 'LIKE', … … 337 343 foreach ( $role__in as $role ) { 338 344 $role__in_clauses[] = array( 339 'key' => $ wpdb->get_blog_prefix( $blog_id ) . 'capabilities',345 'key' => $this->db->get_blog_prefix( $blog_id ) . 'capabilities', 340 346 'value' => '"' . $role . '"', 341 347 'compare' => 'LIKE', … … 350 356 foreach ( $role__not_in as $role ) { 351 357 $role__not_in_clauses[] = array( 352 'key' => $ wpdb->get_blog_prefix( $blog_id ) . 'capabilities',358 'key' => $this->db->get_blog_prefix( $blog_id ) . 'capabilities', 353 359 'value' => '"' . $role . '"', 354 360 'compare' => 'NOT LIKE', … … 362 368 if ( empty( $role_queries ) ) { 363 369 $role_queries[] = array( 364 'key' => $ wpdb->get_blog_prefix( $blog_id ) . 'capabilities',370 'key' => $this->db->get_blog_prefix( $blog_id ) . 'capabilities', 365 371 'compare' => 'EXISTS', 366 372 ); … … 384 390 385 391 if ( ! empty( $this->meta_query->queries ) ) { 386 $clauses = $this->meta_query->get_sql( 'user', $ wpdb->users, 'ID', $this );392 $clauses = $this->meta_query->get_sql( 'user', $this->db->users, 'ID', $this ); 387 393 $this->query_from .= $clauses['join']; 388 394 $this->query_where .= $clauses['where']; … … 442 448 if ( isset( $qv['number'] ) && $qv['number'] > 0 ) { 443 449 if ( $qv['offset'] ) { 444 $this->query_limit = $ wpdb->prepare("LIMIT %d, %d", $qv['offset'], $qv['number']);450 $this->query_limit = $this->db->prepare("LIMIT %d, %d", $qv['offset'], $qv['number']); 445 451 } else { 446 $this->query_limit = $ wpdb->prepare( "LIMIT %d, %d", $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] );452 $this->query_limit = $this->db->prepare( "LIMIT %d, %d", $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] ); 447 453 } 448 454 } … … 500 506 // Sanitized earlier. 501 507 $ids = implode( ',', $include ); 502 $this->query_where .= " AND $wpdb->users.ID IN ($ids)";508 $this->query_where .= " AND {$this->db->users}.ID IN ($ids)"; 503 509 } elseif ( ! empty( $qv['exclude'] ) ) { 504 510 $ids = implode( ',', wp_parse_id_list( $qv['exclude'] ) ); 505 $this->query_where .= " AND $wpdb->users.ID NOT IN ($ids)";511 $this->query_where .= " AND {$this->db->users}.ID NOT IN ($ids)"; 506 512 } 507 513 … … 531 537 * 532 538 * @since 3.1.0 533 *534 * @global wpdb $wpdb WordPress database abstraction object.535 539 */ 536 540 public function query() { 537 global $wpdb;538 539 541 $qv =& $this->query_vars; 540 542 … … 542 544 543 545 if ( is_array( $qv['fields'] ) || 'all' == $qv['fields'] ) { 544 $this->results = $ wpdb->get_results( $this->request );546 $this->results = $this->db->get_results( $this->request ); 545 547 } else { 546 $this->results = $ wpdb->get_col( $this->request );548 $this->results = $this->db->get_col( $this->request ); 547 549 } 548 550 … … 552 554 * @since 3.2.0 553 555 * 554 * @global wpdb $wpdb WordPress database abstraction object.555 *556 556 * @param string $sql The SELECT FOUND_ROWS() query for the current WP_User_Query. 557 557 */ 558 if ( isset( $qv['count_total'] ) && $qv['count_total'] ) 559 $this->total_users = $wpdb->get_var( apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()' ) ); 560 561 if ( !$this->results ) 558 if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { 559 $this->total_users = $this->db->get_var( apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()' ) ); 560 } 561 562 if ( ! $this->results ) { 562 563 return; 564 } 563 565 564 566 if ( 'all_with_meta' == $qv['fields'] ) { … … 611 613 * @access protected 612 614 * @since 3.1.0 613 *614 * @global wpdb $wpdb WordPress database abstraction object.615 615 * 616 616 * @param string $string … … 621 621 */ 622 622 protected function get_search_sql( $string, $cols, $wild = false ) { 623 global $wpdb;624 625 623 $searches = array(); 626 624 $leading_wild = ( 'leading' == $wild || 'both' == $wild ) ? '%' : ''; 627 625 $trailing_wild = ( 'trailing' == $wild || 'both' == $wild ) ? '%' : ''; 628 $like = $leading_wild . $ wpdb->esc_like( $string ) . $trailing_wild;626 $like = $leading_wild . $this->db->esc_like( $string ) . $trailing_wild; 629 627 630 628 foreach ( $cols as $col ) { 631 629 if ( 'ID' == $col ) { 632 $searches[] = $ wpdb->prepare( "$col = %s", $string );630 $searches[] = $this->db->prepare( "$col = %s", $string ); 633 631 } else { 634 $searches[] = $ wpdb->prepare( "$col LIKE %s", $like );632 $searches[] = $this->db->prepare( "$col LIKE %s", $like ); 635 633 } 636 634 } … … 669 667 * @access protected 670 668 * 671 * @global wpdb $wpdb WordPress database abstraction object.672 *673 669 * @param string $orderby Alias for the field to order by. 674 670 * @return string Value to used in the ORDER clause, if `$orderby` is valid. 675 671 */ 676 672 protected function parse_orderby( $orderby ) { 677 global $wpdb;678 679 673 $meta_query_clauses = $this->meta_query->get_clauses(); 680 674 … … 691 685 $this->query_from .= " LEFT OUTER JOIN ( 692 686 SELECT post_author, COUNT(*) as post_count 693 FROM $wpdb->posts687 FROM {$this->db->posts} 694 688 $where 695 689 GROUP BY post_author 696 ) p ON ({$ wpdb->users}.ID = p.post_author)690 ) p ON ({$this->db->users}.ID = p.post_author) 697 691 "; 698 692 $_orderby = 'post_count'; … … 700 694 $_orderby = 'ID'; 701 695 } elseif ( 'meta_value' == $orderby || $this->get( 'meta_key' ) == $orderby ) { 702 $_orderby = " $wpdb->usermeta.meta_value";696 $_orderby = "{$this->db->usermeta}.meta_value"; 703 697 } elseif ( 'meta_value_num' == $orderby ) { 704 $_orderby = " $wpdb->usermeta.meta_value+0";698 $_orderby = "{$this->db->usermeta}.meta_value+0"; 705 699 } elseif ( 'include' === $orderby && ! empty( $this->query_vars['include'] ) ) { 706 700 $include = wp_parse_id_list( $this->query_vars['include'] ); 707 701 $include_sql = implode( ',', $include ); 708 $_orderby = "FIELD( $wpdb->users.ID, $include_sql )";702 $_orderby = "FIELD( {$this->db->users}.ID, $include_sql )"; 709 703 } elseif ( isset( $meta_query_clauses[ $orderby ] ) ) { 710 704 $meta_clause = $meta_query_clauses[ $orderby ];
Note: See TracChangeset
for help on using the changeset viewer.