General: Restore usage of $wpdb, instead of $this->db.

Hiding the `$wpdb` global behind a property decreases the readability of the code, as well as causing irrelevant output when dumping an object.

Reverts [38275], [38278], [38279], [38280], [38387].
See #37699.


Built from https://develop.svn.wordpress.org/trunk@38768


git-svn-id: http://core.svn.wordpress.org/trunk@38711 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Gary Pendergast
2016-10-10 06:38:31 +00:00
parent 2064e34833
commit af69f4ab1a
13 changed files with 402 additions and 371 deletions

View File

@@ -70,13 +70,6 @@ class WP_User_Query {
public $query_orderby;
public $query_limit;
/**
* @since 4.7.0
* @access protected
* @var wpdb
*/
protected $db;
/**
* PHP5 constructor.
*
@@ -85,8 +78,6 @@ class WP_User_Query {
* @param null|string|array $query Optional. The query variables.
*/
public function __construct( $query = null ) {
$this->db = $GLOBALS['wpdb'];
if ( ! empty( $query ) ) {
$this->prepare_query( $query );
$this->query();
@@ -151,6 +142,7 @@ class WP_User_Query {
*
* @access public
*
* @global wpdb $wpdb WordPress database abstraction object.
* @global int $blog_id
*
* @param string|array $query {
@@ -224,6 +216,8 @@ class WP_User_Query {
* }
*/
public function prepare_query( $query = array() ) {
global $wpdb;
if ( empty( $this->query_vars ) || ! empty( $query ) ) {
$this->query_limit = null;
$this->query_vars = $this->fill_query_vars( $query );
@@ -252,19 +246,19 @@ class WP_User_Query {
$this->query_fields = array();
foreach ( $qv['fields'] as $field ) {
$field = 'ID' === $field ? 'ID' : sanitize_key( $field );
$this->query_fields[] = "{$this->db->users}.$field";
$this->query_fields[] = "$wpdb->users.$field";
}
$this->query_fields = implode( ',', $this->query_fields );
} elseif ( 'all' == $qv['fields'] ) {
$this->query_fields = "{$this->db->users}.*";
$this->query_fields = "$wpdb->users.*";
} else {
$this->query_fields = "{$this->db->users}.ID";
$this->query_fields = "$wpdb->users.ID";
}
if ( isset( $qv['count_total'] ) && $qv['count_total'] )
$this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields;
$this->query_from = "FROM {$this->db->users}";
$this->query_from = "FROM $wpdb->users";
$this->query_where = "WHERE 1=1";
// Parse and sanitize 'include', for use by 'orderby' as well as 'include' below.
@@ -287,16 +281,16 @@ class WP_User_Query {
}
foreach ( $post_types as &$post_type ) {
$post_type = $this->db->prepare( '%s', $post_type );
$post_type = $wpdb->prepare( '%s', $post_type );
}
$posts_table = $this->db->get_blog_prefix( $blog_id ) . 'posts';
$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 ) . " ) )";
$posts_table = $wpdb->get_blog_prefix( $blog_id ) . 'posts';
$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 ) . " ) )";
}
// nicename
if ( '' !== $qv['nicename']) {
$this->query_where .= $this->db->prepare( ' AND user_nicename = %s', $qv['nicename'] );
$this->query_where .= $wpdb->prepare( ' AND user_nicename = %s', $qv['nicename'] );
}
if ( ! empty( $qv['nicename__in'] ) ) {
@@ -313,7 +307,7 @@ class WP_User_Query {
// login
if ( '' !== $qv['login']) {
$this->query_where .= $this->db->prepare( ' AND user_login = %s', $qv['login'] );
$this->query_where .= $wpdb->prepare( ' AND user_login = %s', $qv['login'] );
}
if ( ! empty( $qv['login__in'] ) ) {
@@ -334,7 +328,7 @@ class WP_User_Query {
if ( isset( $qv['who'] ) && 'authors' == $qv['who'] && $blog_id ) {
$who_query = array(
'key' => $this->db->get_blog_prefix( $blog_id ) . 'user_level',
'key' => $wpdb->get_blog_prefix( $blog_id ) . 'user_level',
'value' => 0,
'compare' => '!=',
);
@@ -381,7 +375,7 @@ class WP_User_Query {
if ( ! empty( $roles ) ) {
foreach ( $roles as $role ) {
$roles_clauses[] = array(
'key' => $this->db->get_blog_prefix( $blog_id ) . 'capabilities',
'key' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
'value' => '"' . $role . '"',
'compare' => 'LIKE',
);
@@ -394,7 +388,7 @@ class WP_User_Query {
if ( ! empty( $role__in ) ) {
foreach ( $role__in as $role ) {
$role__in_clauses[] = array(
'key' => $this->db->get_blog_prefix( $blog_id ) . 'capabilities',
'key' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
'value' => '"' . $role . '"',
'compare' => 'LIKE',
);
@@ -407,7 +401,7 @@ class WP_User_Query {
if ( ! empty( $role__not_in ) ) {
foreach ( $role__not_in as $role ) {
$role__not_in_clauses[] = array(
'key' => $this->db->get_blog_prefix( $blog_id ) . 'capabilities',
'key' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
'value' => '"' . $role . '"',
'compare' => 'NOT LIKE',
);
@@ -419,7 +413,7 @@ class WP_User_Query {
// If there are no specific roles named, make sure the user is a member of the site.
if ( empty( $role_queries ) ) {
$role_queries[] = array(
'key' => $this->db->get_blog_prefix( $blog_id ) . 'capabilities',
'key' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
'compare' => 'EXISTS',
);
}
@@ -441,7 +435,7 @@ class WP_User_Query {
}
if ( ! empty( $this->meta_query->queries ) ) {
$clauses = $this->meta_query->get_sql( 'user', $this->db->users, 'ID', $this );
$clauses = $this->meta_query->get_sql( 'user', $wpdb->users, 'ID', $this );
$this->query_from .= $clauses['join'];
$this->query_where .= $clauses['where'];
@@ -503,9 +497,9 @@ class WP_User_Query {
// limit
if ( isset( $qv['number'] ) && $qv['number'] > 0 ) {
if ( $qv['offset'] ) {
$this->query_limit = $this->db->prepare("LIMIT %d, %d", $qv['offset'], $qv['number']);
$this->query_limit = $wpdb->prepare("LIMIT %d, %d", $qv['offset'], $qv['number']);
} else {
$this->query_limit = $this->db->prepare( "LIMIT %d, %d", $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] );
$this->query_limit = $wpdb->prepare( "LIMIT %d, %d", $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] );
}
}
@@ -561,10 +555,10 @@ class WP_User_Query {
if ( ! empty( $include ) ) {
// Sanitized earlier.
$ids = implode( ',', $include );
$this->query_where .= " AND {$this->db->users}.ID IN ($ids)";
$this->query_where .= " AND $wpdb->users.ID IN ($ids)";
} elseif ( ! empty( $qv['exclude'] ) ) {
$ids = implode( ',', wp_parse_id_list( $qv['exclude'] ) );
$this->query_where .= " AND {$this->db->users}.ID NOT IN ($ids)";
$this->query_where .= " AND $wpdb->users.ID NOT IN ($ids)";
}
// Date queries are allowed for the user_registered field.
@@ -592,16 +586,20 @@ class WP_User_Query {
* Execute the query, with the current variables.
*
* @since 3.1.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*/
public function query() {
global $wpdb;
$qv =& $this->query_vars;
$this->request = "SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit";
if ( is_array( $qv['fields'] ) || 'all' == $qv['fields'] ) {
$this->results = $this->db->get_results( $this->request );
$this->results = $wpdb->get_results( $this->request );
} else {
$this->results = $this->db->get_col( $this->request );
$this->results = $wpdb->get_col( $this->request );
}
/**
@@ -609,15 +607,15 @@ class WP_User_Query {
*
* @since 3.2.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $sql The SELECT FOUND_ROWS() query for the current WP_User_Query.
*/
if ( isset( $qv['count_total'] ) && $qv['count_total'] ) {
$this->total_users = $this->db->get_var( apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()' ) );
}
if ( isset( $qv['count_total'] ) && $qv['count_total'] )
$this->total_users = $wpdb->get_var( apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()' ) );
if ( ! $this->results ) {
if ( !$this->results )
return;
}
if ( 'all_with_meta' == $qv['fields'] ) {
cache_users( $this->results );
@@ -669,6 +667,8 @@ class WP_User_Query {
* @access protected
* @since 3.1.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $string
* @param array $cols
* @param bool $wild Whether to allow wildcard searches. Default is false for Network Admin, true for single site.
@@ -676,16 +676,18 @@ class WP_User_Query {
* @return string
*/
protected function get_search_sql( $string, $cols, $wild = false ) {
global $wpdb;
$searches = array();
$leading_wild = ( 'leading' == $wild || 'both' == $wild ) ? '%' : '';
$trailing_wild = ( 'trailing' == $wild || 'both' == $wild ) ? '%' : '';
$like = $leading_wild . $this->db->esc_like( $string ) . $trailing_wild;
$like = $leading_wild . $wpdb->esc_like( $string ) . $trailing_wild;
foreach ( $cols as $col ) {
if ( 'ID' == $col ) {
$searches[] = $this->db->prepare( "$col = %s", $string );
$searches[] = $wpdb->prepare( "$col = %s", $string );
} else {
$searches[] = $this->db->prepare( "$col LIKE %s", $like );
$searches[] = $wpdb->prepare( "$col LIKE %s", $like );
}
}
@@ -722,10 +724,14 @@ class WP_User_Query {
* @since 4.2.0
* @access protected
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $orderby Alias for the field to order by.
* @return string Value to used in the ORDER clause, if `$orderby` is valid.
*/
protected function parse_orderby( $orderby ) {
global $wpdb;
$meta_query_clauses = $this->meta_query->get_clauses();
$_orderby = '';
@@ -740,22 +746,22 @@ class WP_User_Query {
$where = get_posts_by_author_sql( 'post' );
$this->query_from .= " LEFT OUTER JOIN (
SELECT post_author, COUNT(*) as post_count
FROM {$this->db->posts}
FROM $wpdb->posts
$where
GROUP BY post_author
) p ON ({$this->db->users}.ID = p.post_author)
) p ON ({$wpdb->users}.ID = p.post_author)
";
$_orderby = 'post_count';
} elseif ( 'ID' == $orderby || 'id' == $orderby ) {
$_orderby = 'ID';
} elseif ( 'meta_value' == $orderby || $this->get( 'meta_key' ) == $orderby ) {
$_orderby = "{$this->db->usermeta}.meta_value";
$_orderby = "$wpdb->usermeta.meta_value";
} elseif ( 'meta_value_num' == $orderby ) {
$_orderby = "{$this->db->usermeta}.meta_value+0";
$_orderby = "$wpdb->usermeta.meta_value+0";
} elseif ( 'include' === $orderby && ! empty( $this->query_vars['include'] ) ) {
$include = wp_parse_id_list( $this->query_vars['include'] );
$include_sql = implode( ',', $include );
$_orderby = "FIELD( {$this->db->users}.ID, $include_sql )";
$_orderby = "FIELD( $wpdb->users.ID, $include_sql )";
} elseif ( 'nicename__in' === $orderby ) {
$sanitized_nicename__in = array_map( 'esc_sql', $this->query_vars['nicename__in'] );
$nicename__in = implode( "','", $sanitized_nicename__in );