Changeset 38768 for trunk/src/wp-includes/class-wp-user-query.php
- Timestamp:
- 10/10/2016 06:37:02 AM (9 years ago)
- File:
-
- 1 edited
-
trunk/src/wp-includes/class-wp-user-query.php (modified) (24 diffs)
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/class-wp-user-query.php
r38715 r38768 72 72 73 73 /** 74 * @since 4.7.075 * @access protected76 * @var wpdb77 */78 protected $db;79 80 /**81 74 * PHP5 constructor. 82 75 * … … 86 79 */ 87 80 public function __construct( $query = null ) { 88 $this->db = $GLOBALS['wpdb'];89 90 81 if ( ! empty( $query ) ) { 91 82 $this->prepare_query( $query ); … … 152 143 * @access public 153 144 * 145 * @global wpdb $wpdb WordPress database abstraction object. 154 146 * @global int $blog_id 155 147 * … … 225 217 */ 226 218 public function prepare_query( $query = array() ) { 219 global $wpdb; 220 227 221 if ( empty( $this->query_vars ) || ! empty( $query ) ) { 228 222 $this->query_limit = null; … … 253 247 foreach ( $qv['fields'] as $field ) { 254 248 $field = 'ID' === $field ? 'ID' : sanitize_key( $field ); 255 $this->query_fields[] = " {$this->db->users}.$field";249 $this->query_fields[] = "$wpdb->users.$field"; 256 250 } 257 251 $this->query_fields = implode( ',', $this->query_fields ); 258 252 } elseif ( 'all' == $qv['fields'] ) { 259 $this->query_fields = " {$this->db->users}.*";253 $this->query_fields = "$wpdb->users.*"; 260 254 } else { 261 $this->query_fields = " {$this->db->users}.ID";255 $this->query_fields = "$wpdb->users.ID"; 262 256 } 263 257 … … 265 259 $this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields; 266 260 267 $this->query_from = "FROM {$this->db->users}";261 $this->query_from = "FROM $wpdb->users"; 268 262 $this->query_where = "WHERE 1=1"; 269 263 … … 288 282 289 283 foreach ( $post_types as &$post_type ) { 290 $post_type = $ this->db->prepare( '%s', $post_type );291 } 292 293 $posts_table = $ this->db->get_blog_prefix( $blog_id ) . 'posts';294 $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 ) . " ) )";284 $post_type = $wpdb->prepare( '%s', $post_type ); 285 } 286 287 $posts_table = $wpdb->get_blog_prefix( $blog_id ) . 'posts'; 288 $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 ) . " ) )"; 295 289 } 296 290 297 291 // nicename 298 292 if ( '' !== $qv['nicename']) { 299 $this->query_where .= $ this->db->prepare( ' AND user_nicename = %s', $qv['nicename'] );293 $this->query_where .= $wpdb->prepare( ' AND user_nicename = %s', $qv['nicename'] ); 300 294 } 301 295 … … 314 308 // login 315 309 if ( '' !== $qv['login']) { 316 $this->query_where .= $ this->db->prepare( ' AND user_login = %s', $qv['login'] );310 $this->query_where .= $wpdb->prepare( ' AND user_login = %s', $qv['login'] ); 317 311 } 318 312 … … 335 329 if ( isset( $qv['who'] ) && 'authors' == $qv['who'] && $blog_id ) { 336 330 $who_query = array( 337 'key' => $ this->db->get_blog_prefix( $blog_id ) . 'user_level',331 'key' => $wpdb->get_blog_prefix( $blog_id ) . 'user_level', 338 332 'value' => 0, 339 333 'compare' => '!=', … … 382 376 foreach ( $roles as $role ) { 383 377 $roles_clauses[] = array( 384 'key' => $ this->db->get_blog_prefix( $blog_id ) . 'capabilities',378 'key' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities', 385 379 'value' => '"' . $role . '"', 386 380 'compare' => 'LIKE', … … 395 389 foreach ( $role__in as $role ) { 396 390 $role__in_clauses[] = array( 397 'key' => $ this->db->get_blog_prefix( $blog_id ) . 'capabilities',391 'key' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities', 398 392 'value' => '"' . $role . '"', 399 393 'compare' => 'LIKE', … … 408 402 foreach ( $role__not_in as $role ) { 409 403 $role__not_in_clauses[] = array( 410 'key' => $ this->db->get_blog_prefix( $blog_id ) . 'capabilities',404 'key' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities', 411 405 'value' => '"' . $role . '"', 412 406 'compare' => 'NOT LIKE', … … 420 414 if ( empty( $role_queries ) ) { 421 415 $role_queries[] = array( 422 'key' => $ this->db->get_blog_prefix( $blog_id ) . 'capabilities',416 'key' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities', 423 417 'compare' => 'EXISTS', 424 418 ); … … 442 436 443 437 if ( ! empty( $this->meta_query->queries ) ) { 444 $clauses = $this->meta_query->get_sql( 'user', $ this->db->users, 'ID', $this );438 $clauses = $this->meta_query->get_sql( 'user', $wpdb->users, 'ID', $this ); 445 439 $this->query_from .= $clauses['join']; 446 440 $this->query_where .= $clauses['where']; … … 504 498 if ( isset( $qv['number'] ) && $qv['number'] > 0 ) { 505 499 if ( $qv['offset'] ) { 506 $this->query_limit = $ this->db->prepare("LIMIT %d, %d", $qv['offset'], $qv['number']);500 $this->query_limit = $wpdb->prepare("LIMIT %d, %d", $qv['offset'], $qv['number']); 507 501 } else { 508 $this->query_limit = $ this->db->prepare( "LIMIT %d, %d", $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] );502 $this->query_limit = $wpdb->prepare( "LIMIT %d, %d", $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] ); 509 503 } 510 504 } … … 562 556 // Sanitized earlier. 563 557 $ids = implode( ',', $include ); 564 $this->query_where .= " AND {$this->db->users}.ID IN ($ids)";558 $this->query_where .= " AND $wpdb->users.ID IN ($ids)"; 565 559 } elseif ( ! empty( $qv['exclude'] ) ) { 566 560 $ids = implode( ',', wp_parse_id_list( $qv['exclude'] ) ); 567 $this->query_where .= " AND {$this->db->users}.ID NOT IN ($ids)";561 $this->query_where .= " AND $wpdb->users.ID NOT IN ($ids)"; 568 562 } 569 563 … … 593 587 * 594 588 * @since 3.1.0 589 * 590 * @global wpdb $wpdb WordPress database abstraction object. 595 591 */ 596 592 public function query() { 593 global $wpdb; 594 597 595 $qv =& $this->query_vars; 598 596 … … 600 598 601 599 if ( is_array( $qv['fields'] ) || 'all' == $qv['fields'] ) { 602 $this->results = $ this->db->get_results( $this->request );600 $this->results = $wpdb->get_results( $this->request ); 603 601 } else { 604 $this->results = $ this->db->get_col( $this->request );602 $this->results = $wpdb->get_col( $this->request ); 605 603 } 606 604 … … 610 608 * @since 3.2.0 611 609 * 610 * @global wpdb $wpdb WordPress database abstraction object. 611 * 612 612 * @param string $sql The SELECT FOUND_ROWS() query for the current WP_User_Query. 613 613 */ 614 if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { 615 $this->total_users = $this->db->get_var( apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()' ) ); 616 } 617 618 if ( ! $this->results ) { 614 if ( isset( $qv['count_total'] ) && $qv['count_total'] ) 615 $this->total_users = $wpdb->get_var( apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()' ) ); 616 617 if ( !$this->results ) 619 618 return; 620 }621 619 622 620 if ( 'all_with_meta' == $qv['fields'] ) { … … 669 667 * @access protected 670 668 * @since 3.1.0 669 * 670 * @global wpdb $wpdb WordPress database abstraction object. 671 671 * 672 672 * @param string $string … … 677 677 */ 678 678 protected function get_search_sql( $string, $cols, $wild = false ) { 679 global $wpdb; 680 679 681 $searches = array(); 680 682 $leading_wild = ( 'leading' == $wild || 'both' == $wild ) ? '%' : ''; 681 683 $trailing_wild = ( 'trailing' == $wild || 'both' == $wild ) ? '%' : ''; 682 $like = $leading_wild . $ this->db->esc_like( $string ) . $trailing_wild;684 $like = $leading_wild . $wpdb->esc_like( $string ) . $trailing_wild; 683 685 684 686 foreach ( $cols as $col ) { 685 687 if ( 'ID' == $col ) { 686 $searches[] = $ this->db->prepare( "$col = %s", $string );688 $searches[] = $wpdb->prepare( "$col = %s", $string ); 687 689 } else { 688 $searches[] = $ this->db->prepare( "$col LIKE %s", $like );690 $searches[] = $wpdb->prepare( "$col LIKE %s", $like ); 689 691 } 690 692 } … … 723 725 * @access protected 724 726 * 727 * @global wpdb $wpdb WordPress database abstraction object. 728 * 725 729 * @param string $orderby Alias for the field to order by. 726 730 * @return string Value to used in the ORDER clause, if `$orderby` is valid. 727 731 */ 728 732 protected function parse_orderby( $orderby ) { 733 global $wpdb; 734 729 735 $meta_query_clauses = $this->meta_query->get_clauses(); 730 736 … … 741 747 $this->query_from .= " LEFT OUTER JOIN ( 742 748 SELECT post_author, COUNT(*) as post_count 743 FROM {$this->db->posts}749 FROM $wpdb->posts 744 750 $where 745 751 GROUP BY post_author 746 ) p ON ({$ this->db->users}.ID = p.post_author)752 ) p ON ({$wpdb->users}.ID = p.post_author) 747 753 "; 748 754 $_orderby = 'post_count'; … … 750 756 $_orderby = 'ID'; 751 757 } elseif ( 'meta_value' == $orderby || $this->get( 'meta_key' ) == $orderby ) { 752 $_orderby = " {$this->db->usermeta}.meta_value";758 $_orderby = "$wpdb->usermeta.meta_value"; 753 759 } elseif ( 'meta_value_num' == $orderby ) { 754 $_orderby = " {$this->db->usermeta}.meta_value+0";760 $_orderby = "$wpdb->usermeta.meta_value+0"; 755 761 } elseif ( 'include' === $orderby && ! empty( $this->query_vars['include'] ) ) { 756 762 $include = wp_parse_id_list( $this->query_vars['include'] ); 757 763 $include_sql = implode( ',', $include ); 758 $_orderby = "FIELD( {$this->db->users}.ID, $include_sql )";764 $_orderby = "FIELD( $wpdb->users.ID, $include_sql )"; 759 765 } elseif ( 'nicename__in' === $orderby ) { 760 766 $sanitized_nicename__in = array_map( 'esc_sql', $this->query_vars['nicename__in'] );
Note: See TracChangeset
for help on using the changeset viewer.