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.



git-svn-id: https://develop.svn.wordpress.org/trunk@38768 602fd350-edb4-49c9-b593-d223f7449a82
This commit is contained in:
Gary Pendergast 2016-10-10 06:37:02 +00:00
parent 81ae08cf40
commit 6774e27ae7
13 changed files with 401 additions and 373 deletions

View File

@ -141,13 +141,6 @@ class WP_Comment_Query {
return false;
}
/**
* @since 4.7.0
* @access protected
* @var wpdb
*/
protected $db;
/**
* Constructor.
*
@ -267,8 +260,6 @@ class WP_Comment_Query {
* }
*/
public function __construct( $query = '' ) {
$this->db = $GLOBALS['wpdb'];
$this->query_var_defaults = array(
'author_email' => '',
'author_url' => '',
@ -372,9 +363,13 @@ class WP_Comment_Query {
* @since 4.2.0
* @access public
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @return int|array List of comments or number of found comments if `$count` argument is true.
*/
public function get_comments() {
global $wpdb;
$this->parse_query();
// Parse meta query
@ -393,7 +388,7 @@ class WP_Comment_Query {
// Reparse query vars, in case they were modified in a 'pre_get_comments' callback.
$this->meta_query->parse_query_vars( $this->query_vars );
if ( ! empty( $this->meta_query->queries ) ) {
$this->meta_query_clauses = $this->meta_query->get_sql( 'comment', $this->db->comments, 'comment_ID', $this );
$this->meta_query_clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this );
}
// $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
@ -485,8 +480,12 @@ class WP_Comment_Query {
*
* @since 4.4.0
* @access protected
*
* @global wpdb $wpdb WordPress database abstraction object.
*/
protected function get_comment_ids() {
global $wpdb;
// Assemble clauses related to 'comment_approved'.
$approved_clauses = array();
@ -515,7 +514,7 @@ class WP_Comment_Query {
break;
default :
$status_clauses[] = $this->db->prepare( "comment_approved = %s", $status );
$status_clauses[] = $wpdb->prepare( "comment_approved = %s", $status );
break;
}
}
@ -538,11 +537,11 @@ class WP_Comment_Query {
foreach ( $include_unapproved as $unapproved_identifier ) {
// Numeric values are assumed to be user ids.
if ( is_numeric( $unapproved_identifier ) ) {
$approved_clauses[] = $this->db->prepare( "( user_id = %d AND comment_approved = '0' )", $unapproved_identifier );
$approved_clauses[] = $wpdb->prepare( "( user_id = %d AND comment_approved = '0' )", $unapproved_identifier );
// Otherwise we match against email addresses.
} else {
$approved_clauses[] = $this->db->prepare( "( comment_author_email = %s AND comment_approved = '0' )", $unapproved_identifier );
$approved_clauses[] = $wpdb->prepare( "( comment_author_email = %s AND comment_approved = '0' )", $unapproved_identifier );
}
}
}
@ -601,7 +600,7 @@ class WP_Comment_Query {
// If no valid clauses were found, order by comment_date_gmt.
if ( empty( $orderby_array ) ) {
$orderby_array[] = "{$this->db->comments}.comment_date_gmt $order";
$orderby_array[] = "$wpdb->comments.comment_date_gmt $order";
}
// To ensure determinate sorting, always include a comment_ID clause.
@ -634,12 +633,12 @@ class WP_Comment_Query {
$comment_ID_order = 'DESC';
}
$orderby_array[] = "{$this->db->comments}.comment_ID $comment_ID_order";
$orderby_array[] = "$wpdb->comments.comment_ID $comment_ID_order";
}
$orderby = implode( ', ', $orderby_array );
} else {
$orderby = "{$this->db->comments}.comment_date_gmt $order";
$orderby = "$wpdb->comments.comment_date_gmt $order";
}
$number = absint( $this->query_vars['number'] );
@ -656,22 +655,22 @@ class WP_Comment_Query {
if ( $this->query_vars['count'] ) {
$fields = 'COUNT(*)';
} else {
$fields = "{$this->db->comments}.comment_ID";
$fields = "$wpdb->comments.comment_ID";
}
$post_id = absint( $this->query_vars['post_id'] );
if ( ! empty( $post_id ) ) {
$this->sql_clauses['where']['post_id'] = $this->db->prepare( 'comment_post_ID = %d', $post_id );
$this->sql_clauses['where']['post_id'] = $wpdb->prepare( 'comment_post_ID = %d', $post_id );
}
// Parse comment IDs for an IN clause.
if ( ! empty( $this->query_vars['comment__in'] ) ) {
$this->sql_clauses['where']['comment__in'] = "{$this->db->comments}.comment_ID IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__in'] ) ) . ' )';
$this->sql_clauses['where']['comment__in'] = "$wpdb->comments.comment_ID IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__in'] ) ) . ' )';
}
// Parse comment IDs for a NOT IN clause.
if ( ! empty( $this->query_vars['comment__not_in'] ) ) {
$this->sql_clauses['where']['comment__not_in'] = "{$this->db->comments}.comment_ID NOT IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__not_in'] ) ) . ' )';
$this->sql_clauses['where']['comment__not_in'] = "$wpdb->comments.comment_ID NOT IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__not_in'] ) ) . ' )';
}
// Parse comment parent IDs for an IN clause.
@ -695,15 +694,15 @@ class WP_Comment_Query {
}
if ( '' !== $this->query_vars['author_email'] ) {
$this->sql_clauses['where']['author_email'] = $this->db->prepare( 'comment_author_email = %s', $this->query_vars['author_email'] );
$this->sql_clauses['where']['author_email'] = $wpdb->prepare( 'comment_author_email = %s', $this->query_vars['author_email'] );
}
if ( '' !== $this->query_vars['author_url'] ) {
$this->sql_clauses['where']['author_url'] = $this->db->prepare( 'comment_author_url = %s', $this->query_vars['author_url'] );
$this->sql_clauses['where']['author_url'] = $wpdb->prepare( 'comment_author_url = %s', $this->query_vars['author_url'] );
}
if ( '' !== $this->query_vars['karma'] ) {
$this->sql_clauses['where']['karma'] = $this->db->prepare( 'comment_karma = %d', $this->query_vars['karma'] );
$this->sql_clauses['where']['karma'] = $wpdb->prepare( 'comment_karma = %d', $this->query_vars['karma'] );
}
// Filtering by comment_type: 'type', 'type__in', 'type__not_in'.
@ -734,7 +733,7 @@ class WP_Comment_Query {
break;
default:
$comment_types[ $operator ][] = $this->db->prepare( '%s', $type );
$comment_types[ $operator ][] = $wpdb->prepare( '%s', $type );
break;
}
}
@ -751,13 +750,13 @@ class WP_Comment_Query {
}
if ( '' !== $parent ) {
$this->sql_clauses['where']['parent'] = $this->db->prepare( 'comment_parent = %d', $parent );
$this->sql_clauses['where']['parent'] = $wpdb->prepare( 'comment_parent = %d', $parent );
}
if ( is_array( $this->query_vars['user_id'] ) ) {
$this->sql_clauses['where']['user_id'] = 'user_id IN (' . implode( ',', array_map( 'absint', $this->query_vars['user_id'] ) ) . ')';
} elseif ( '' !== $this->query_vars['user_id'] ) {
$this->sql_clauses['where']['user_id'] = $this->db->prepare( 'user_id = %d', $this->query_vars['user_id'] );
$this->sql_clauses['where']['user_id'] = $wpdb->prepare( 'user_id = %d', $this->query_vars['user_id'] );
}
// Falsy search strings are ignored.
@ -781,7 +780,7 @@ class WP_Comment_Query {
foreach ( $post_fields as $field_name => $field_value ) {
// $field_value may be an array.
$esses = array_fill( 0, count( (array) $field_value ), '%s' );
$this->sql_clauses['where'][ $field_name ] = $this->db->prepare( " {$this->db->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value );
$this->sql_clauses['where'][ $field_name ] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value );
}
}
@ -802,7 +801,7 @@ class WP_Comment_Query {
$join_posts_table = true;
$esses = array_fill( 0, count( $q_values ), '%s' );
$this->sql_clauses['where'][ $field_name ] = $this->db->prepare( " {$this->db->posts}.{$field_name} IN (" . implode( ',', $esses ) . ")", $q_values );
$this->sql_clauses['where'][ $field_name ] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ")", $q_values );
}
}
@ -831,7 +830,7 @@ class WP_Comment_Query {
$join = '';
if ( $join_posts_table ) {
$join .= "JOIN {$this->db->posts} ON {$this->db->posts}.ID = {$this->db->comments}.comment_post_ID";
$join .= "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
}
if ( ! empty( $this->meta_query_clauses ) ) {
@ -841,7 +840,7 @@ class WP_Comment_Query {
$this->sql_clauses['where']['meta_query'] = preg_replace( '/^\s*AND\s*/', '', $this->meta_query_clauses['where'] );
if ( ! $this->query_vars['count'] ) {
$groupby = "{$this->db->comments}.comment_ID";
$groupby = "{$wpdb->comments}.comment_ID";
}
}
@ -890,7 +889,7 @@ class WP_Comment_Query {
}
$this->sql_clauses['select'] = "SELECT $found_rows $fields";
$this->sql_clauses['from'] = "FROM {$this->db->comments} $join";
$this->sql_clauses['from'] = "FROM $wpdb->comments $join";
$this->sql_clauses['groupby'] = $groupby;
$this->sql_clauses['orderby'] = $orderby;
$this->sql_clauses['limits'] = $limits;
@ -898,9 +897,9 @@ class WP_Comment_Query {
$this->request = "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} {$this->sql_clauses['orderby']} {$this->sql_clauses['limits']}";
if ( $this->query_vars['count'] ) {
return intval( $this->db->get_var( $this->request ) );
return intval( $wpdb->get_var( $this->request ) );
} else {
$comment_ids = $this->db->get_col( $this->request );
$comment_ids = $wpdb->get_col( $this->request );
return array_map( 'intval', $comment_ids );
}
}
@ -911,8 +910,12 @@ class WP_Comment_Query {
*
* @since 4.6.0
* @access private
*
* @global wpdb $wpdb WordPress database abstraction object.
*/
private function set_found_comments() {
global $wpdb;
if ( $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
/**
* Filters the query used to retrieve found comment count.
@ -924,7 +927,7 @@ class WP_Comment_Query {
*/
$found_comments_query = apply_filters( 'found_comments_query', 'SELECT FOUND_ROWS()', $this );
$this->found_comments = (int) $this->db->get_var( $found_comments_query );
$this->found_comments = (int) $wpdb->get_var( $found_comments_query );
}
}
@ -940,6 +943,8 @@ class WP_Comment_Query {
* @return array
*/
protected function fill_descendants( $comments ) {
global $wpdb;
$levels = array(
0 => wp_list_pluck( $comments, 'comment_ID' ),
);
@ -1070,16 +1075,20 @@ class WP_Comment_Query {
* @since 3.1.0
* @access protected
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $string
* @param array $cols
* @return string
*/
protected function get_search_sql( $string, $cols ) {
$like = '%' . $this->db->esc_like( $string ) . '%';
global $wpdb;
$like = '%' . $wpdb->esc_like( $string ) . '%';
$searches = array();
foreach ( $cols as $col ) {
$searches[] = $this->db->prepare( "$col LIKE %s", $like );
$searches[] = $wpdb->prepare( "$col LIKE %s", $like );
}
return ' AND (' . implode(' OR ', $searches) . ')';
@ -1091,10 +1100,14 @@ class WP_Comment_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|false Value to used in the ORDER clause. False otherwise.
*/
protected function parse_orderby( $orderby ) {
global $wpdb;
$allowed_keys = array(
'comment_agent',
'comment_approved',
@ -1126,19 +1139,19 @@ class WP_Comment_Query {
$parsed = false;
if ( $orderby == $this->query_vars['meta_key'] || $orderby == 'meta_value' ) {
$parsed = "{$this->db->commentmeta}.meta_value";
$parsed = "$wpdb->commentmeta.meta_value";
} elseif ( $orderby == 'meta_value_num' ) {
$parsed = "{$this->db->commentmeta}.meta_value+0";
$parsed = "$wpdb->commentmeta.meta_value+0";
} elseif ( $orderby == 'comment__in' ) {
$comment__in = implode( ',', array_map( 'absint', $this->query_vars['comment__in'] ) );
$parsed = "FIELD( {$this->db->comments}.comment_ID, $comment__in )";
$parsed = "FIELD( {$wpdb->comments}.comment_ID, $comment__in )";
} elseif ( in_array( $orderby, $allowed_keys ) ) {
if ( isset( $meta_query_clauses[ $orderby ] ) ) {
$meta_clause = $meta_query_clauses[ $orderby ];
$parsed = sprintf( "CAST(%s.meta_value AS %s)", esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) );
} else {
$parsed = "{$this->db->comments}.$orderby";
$parsed = "$wpdb->comments.$orderby";
}
}

View File

@ -105,13 +105,6 @@ class WP_Meta_Query {
*/
protected $has_or_relation = false;
/**
* @since 4.7.0
* @access protected
* @var wpdb
*/
protected $db;
/**
* Constructor.
*
@ -144,11 +137,8 @@ class WP_Meta_Query {
* }
*/
public function __construct( $meta_query = false ) {
$this->db = $GLOBALS['wpdb'];
if ( ! $meta_query ) {
if ( !$meta_query )
return;
}
if ( isset( $meta_query['relation'] ) && strtoupper( $meta_query['relation'] ) == 'OR' ) {
$this->relation = 'OR';
@ -494,6 +484,8 @@ class WP_Meta_Query {
* @since 4.1.0
* @access public
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param array $clause Query clause, passed by reference.
* @param array $parent_query Parent query array.
* @param string $clause_key Optional. The array key used to name the clause in the original `$meta_query`
@ -506,6 +498,8 @@ class WP_Meta_Query {
* }
*/
public function get_sql_for_clause( &$clause, $parent_query, $clause_key = '' ) {
global $wpdb;
$sql_chunks = array(
'where' => array(),
'join' => array(),
@ -543,7 +537,7 @@ class WP_Meta_Query {
if ( 'NOT EXISTS' === $meta_compare ) {
$join .= " LEFT JOIN $this->meta_table";
$join .= $i ? " AS $alias" : '';
$join .= $this->db->prepare( " ON ($this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column AND $alias.meta_key = %s )", $clause['key'] );
$join .= $wpdb->prepare( " ON ($this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column AND $alias.meta_key = %s )", $clause['key'] );
// All other JOIN clauses.
} else {
@ -587,7 +581,7 @@ class WP_Meta_Query {
if ( 'NOT EXISTS' === $meta_compare ) {
$sql_chunks['where'][] = $alias . '.' . $this->meta_id_column . ' IS NULL';
} else {
$sql_chunks['where'][] = $this->db->prepare( "$alias.meta_key = %s", trim( $clause['key'] ) );
$sql_chunks['where'][] = $wpdb->prepare( "$alias.meta_key = %s", trim( $clause['key'] ) );
}
}
@ -607,25 +601,25 @@ class WP_Meta_Query {
case 'IN' :
case 'NOT IN' :
$meta_compare_string = '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')';
$where = $this->db->prepare( $meta_compare_string, $meta_value );
$where = $wpdb->prepare( $meta_compare_string, $meta_value );
break;
case 'BETWEEN' :
case 'NOT BETWEEN' :
$meta_value = array_slice( $meta_value, 0, 2 );
$where = $this->db->prepare( '%s AND %s', $meta_value );
$where = $wpdb->prepare( '%s AND %s', $meta_value );
break;
case 'LIKE' :
case 'NOT LIKE' :
$meta_value = '%' . $this->db->esc_like( $meta_value ) . '%';
$where = $this->db->prepare( '%s', $meta_value );
$meta_value = '%' . $wpdb->esc_like( $meta_value ) . '%';
$where = $wpdb->prepare( '%s', $meta_value );
break;
// EXISTS with a value is interpreted as '='.
case 'EXISTS' :
$meta_compare = '=';
$where = $this->db->prepare( '%s', $meta_value );
$where = $wpdb->prepare( '%s', $meta_value );
break;
// 'value' is ignored for NOT EXISTS.
@ -634,7 +628,7 @@ class WP_Meta_Query {
break;
default :
$where = $this->db->prepare( '%s', $meta_value );
$where = $wpdb->prepare( '%s', $meta_value );
break;
}

View File

@ -86,13 +86,6 @@ class WP_Network_Query {
*/
public $max_num_pages = 0;
/**
* @since 4.7.0
* @access protected
* @var wpdb
*/
protected $db;
/**
* Constructor.
*
@ -129,8 +122,6 @@ class WP_Network_Query {
* }
*/
public function __construct( $query = '' ) {
$this->db = $GLOBALS['wpdb'];
$this->query_var_defaults = array(
'network__in' => '',
'network__not_in' => '',
@ -297,6 +288,8 @@ class WP_Network_Query {
* @return int|array A single count of network IDs if a count query. An array of network IDs if a full query.
*/
protected function get_network_ids() {
global $wpdb;
$order = $this->parse_order( $this->query_vars['order'] );
// Disable ORDER BY with 'none', an empty array, or boolean false.
@ -337,7 +330,7 @@ class WP_Network_Query {
$orderby = implode( ', ', $orderby_array );
} else {
$orderby = "{$this->db->site}.id $order";
$orderby = "$wpdb->site.id $order";
}
$number = absint( $this->query_vars['number'] );
@ -354,52 +347,52 @@ class WP_Network_Query {
if ( $this->query_vars['count'] ) {
$fields = 'COUNT(*)';
} else {
$fields = "{$this->db->site}.id";
$fields = "$wpdb->site.id";
}
// Parse network IDs for an IN clause.
if ( ! empty( $this->query_vars['network__in'] ) ) {
$this->sql_clauses['where']['network__in'] = "{$this->db->site}.id IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['network__in'] ) ) . ' )';
$this->sql_clauses['where']['network__in'] = "$wpdb->site.id IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['network__in'] ) ) . ' )';
}
// Parse network IDs for a NOT IN clause.
if ( ! empty( $this->query_vars['network__not_in'] ) ) {
$this->sql_clauses['where']['network__not_in'] = "{$this->db->site}.id NOT IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['network__not_in'] ) ) . ' )';
$this->sql_clauses['where']['network__not_in'] = "$wpdb->site.id NOT IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['network__not_in'] ) ) . ' )';
}
if ( ! empty( $this->query_vars['domain'] ) ) {
$this->sql_clauses['where']['domain'] = $this->db->prepare( "{$this->db->site}.domain = %s", $this->query_vars['domain'] );
$this->sql_clauses['where']['domain'] = $wpdb->prepare( "$wpdb->site.domain = %s", $this->query_vars['domain'] );
}
// Parse network domain for an IN clause.
if ( is_array( $this->query_vars['domain__in'] ) ) {
$this->sql_clauses['where']['domain__in'] = "{$this->db->site}.domain IN ( '" . implode( "', '", $this->db->_escape( $this->query_vars['domain__in'] ) ) . "' )";
$this->sql_clauses['where']['domain__in'] = "$wpdb->site.domain IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['domain__in'] ) ) . "' )";
}
// Parse network domain for a NOT IN clause.
if ( is_array( $this->query_vars['domain__not_in'] ) ) {
$this->sql_clauses['where']['domain__not_in'] = "{$this->db->site}.domain NOT IN ( '" . implode( "', '", $this->db->_escape( $this->query_vars['domain__not_in'] ) ) . "' )";
$this->sql_clauses['where']['domain__not_in'] = "$wpdb->site.domain NOT IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['domain__not_in'] ) ) . "' )";
}
if ( ! empty( $this->query_vars['path'] ) ) {
$this->sql_clauses['where']['path'] = $this->db->prepare( "{$this->db->site}.path = %s", $this->query_vars['path'] );
$this->sql_clauses['where']['path'] = $wpdb->prepare( "$wpdb->site.path = %s", $this->query_vars['path'] );
}
// Parse network path for an IN clause.
if ( is_array( $this->query_vars['path__in'] ) ) {
$this->sql_clauses['where']['path__in'] = "{$this->db->site}.path IN ( '" . implode( "', '", $this->db->_escape( $this->query_vars['path__in'] ) ) . "' )";
$this->sql_clauses['where']['path__in'] = "$wpdb->site.path IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['path__in'] ) ) . "' )";
}
// Parse network path for a NOT IN clause.
if ( is_array( $this->query_vars['path__not_in'] ) ) {
$this->sql_clauses['where']['path__not_in'] = "{$this->db->site}.path NOT IN ( '" . implode( "', '", $this->db->_escape( $this->query_vars['path__not_in'] ) ) . "' )";
$this->sql_clauses['where']['path__not_in'] = "$wpdb->site.path NOT IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['path__not_in'] ) ) . "' )";
}
// Falsey search strings are ignored.
if ( strlen( $this->query_vars['search'] ) ) {
$this->sql_clauses['where']['search'] = $this->get_search_sql(
$this->query_vars['search'],
array( "{$this->db->site}.domain", "{$this->db->site}.path" )
array( "$wpdb->site.domain", "$wpdb->site.path" )
);
}
@ -444,7 +437,7 @@ class WP_Network_Query {
}
$this->sql_clauses['select'] = "SELECT $found_rows $fields";
$this->sql_clauses['from'] = "FROM {$this->db->site} $join";
$this->sql_clauses['from'] = "FROM $wpdb->site $join";
$this->sql_clauses['groupby'] = $groupby;
$this->sql_clauses['orderby'] = $orderby;
$this->sql_clauses['limits'] = $limits;
@ -452,10 +445,10 @@ class WP_Network_Query {
$this->request = "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} {$this->sql_clauses['orderby']} {$this->sql_clauses['limits']}";
if ( $this->query_vars['count'] ) {
return intval( $this->db->get_var( $this->request ) );
return intval( $wpdb->get_var( $this->request ) );
}
$network_ids = $this->db->get_col( $this->request );
$network_ids = $wpdb->get_col( $this->request );
return array_map( 'intval', $network_ids );
}
@ -466,8 +459,12 @@ class WP_Network_Query {
*
* @since 4.6.0
* @access private
*
* @global wpdb $wpdb WordPress database abstraction object.
*/
private function set_found_networks() {
global $wpdb;
if ( $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
/**
* Filters the query used to retrieve found network count.
@ -479,7 +476,7 @@ class WP_Network_Query {
*/
$found_networks_query = apply_filters( 'found_networks_query', 'SELECT FOUND_ROWS()', $this );
$this->found_networks = (int) $this->db->get_var( $found_networks_query );
$this->found_networks = (int) $wpdb->get_var( $found_networks_query );
}
}
@ -489,17 +486,21 @@ class WP_Network_Query {
* @since 4.6.0
* @access protected
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $string Search string.
* @param array $columns Columns to search.
*
* @return string Search SQL.
*/
protected function get_search_sql( $string, $columns ) {
$like = '%' . $this->db->esc_like( $string ) . '%';
global $wpdb;
$like = '%' . $wpdb->esc_like( $string ) . '%';
$searches = array();
foreach ( $columns as $column ) {
$searches[] = $this->db->prepare( "$column LIKE %s", $like );
$searches[] = $wpdb->prepare( "$column LIKE %s", $like );
}
return '(' . implode( ' OR ', $searches ) . ')';
@ -511,10 +512,14 @@ class WP_Network_Query {
* @since 4.6.0
* @access protected
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $orderby Alias for the field to order by.
* @return string|false Value to used in the ORDER clause. False otherwise.
*/
protected function parse_orderby( $orderby ) {
global $wpdb;
$allowed_keys = array(
'id',
'domain',
@ -524,12 +529,12 @@ class WP_Network_Query {
$parsed = false;
if ( $orderby == 'network__in' ) {
$network__in = implode( ',', array_map( 'absint', $this->query_vars['network__in'] ) );
$parsed = "FIELD( {$this->db->site}.id, $network__in )";
$parsed = "FIELD( {$wpdb->site}.id, $network__in )";
} elseif ( $orderby == 'domain_length' || $orderby == 'path_length' ) {
$field = substr( $orderby, 0, -7 );
$parsed = "CHAR_LENGTH({$this->db->site}.$field)";
$parsed = "CHAR_LENGTH($wpdb->site.$field)";
} elseif ( in_array( $orderby, $allowed_keys ) ) {
$parsed = "{$this->db->site}.$orderby";
$parsed = "$wpdb->site.$orderby";
}
return $parsed;

View File

@ -486,13 +486,6 @@ class WP_Query {
private $compat_methods = array( 'init_query_flags', 'parse_tax_query' );
/**
* @since 4.7.0
* @access protected
* @var wpdb
*/
protected $db;
/**
* Resets query flags to false.
*
@ -1297,6 +1290,8 @@ class WP_Query {
* @return string WHERE clause.
*/
protected function parse_search( &$q ) {
global $wpdb;
$search = '';
// added slashes screw with quote grouping when done early, so done later
@ -1336,19 +1331,19 @@ class WP_Query {
}
if ( $n && $include ) {
$like = '%' . $this->db->esc_like( $term ) . '%';
$q['search_orderby_title'][] = $this->db->prepare( "{$this->db->posts}.post_title LIKE %s", $like );
$like = '%' . $wpdb->esc_like( $term ) . '%';
$q['search_orderby_title'][] = $wpdb->prepare( "{$wpdb->posts}.post_title LIKE %s", $like );
}
$like = $n . $this->db->esc_like( $term ) . $n;
$search .= $this->db->prepare( "{$searchand}(({$this->db->posts}.post_title $like_op %s) $andor_op ({$this->db->posts}.post_excerpt $like_op %s) $andor_op ({$this->db->posts}.post_content $like_op %s))", $like, $like, $like );
$like = $n . $wpdb->esc_like( $term ) . $n;
$search .= $wpdb->prepare( "{$searchand}(({$wpdb->posts}.post_title $like_op %s) $andor_op ({$wpdb->posts}.post_excerpt $like_op %s) $andor_op ({$wpdb->posts}.post_content $like_op %s))", $like, $like, $like );
$searchand = ' AND ';
}
if ( ! empty( $search ) ) {
$search = " AND ({$search}) ";
if ( ! is_user_logged_in() ) {
$search .= " AND ({$this->db->posts}.post_password = '') ";
$search .= " AND ({$wpdb->posts}.post_password = '') ";
}
}
@ -1436,20 +1431,22 @@ class WP_Query {
* @return string ORDER BY clause.
*/
protected function parse_search_order( &$q ) {
global $wpdb;
if ( $q['search_terms_count'] > 1 ) {
$num_terms = count( $q['search_orderby_title'] );
// If the search terms contain negative queries, don't bother ordering by sentence matches.
$like = '';
if ( ! preg_match( '/(?:\s|^)\-/', $q['s'] ) ) {
$like = '%' . $this->db->esc_like( $q['s'] ) . '%';
$like = '%' . $wpdb->esc_like( $q['s'] ) . '%';
}
$search_orderby = '';
// sentence match in 'post_title'
if ( $like ) {
$search_orderby .= $this->db->prepare( "WHEN {$this->db->posts}.post_title LIKE %s THEN 1 ", $like );
$search_orderby .= $wpdb->prepare( "WHEN {$wpdb->posts}.post_title LIKE %s THEN 1 ", $like );
}
// sanity limit, sort as sentence when more than 6 terms
@ -1464,8 +1461,8 @@ class WP_Query {
// Sentence match in 'post_content' and 'post_excerpt'.
if ( $like ) {
$search_orderby .= $this->db->prepare( "WHEN {$this->db->posts}.post_excerpt LIKE %s THEN 4 ", $like );
$search_orderby .= $this->db->prepare( "WHEN {$this->db->posts}.post_content LIKE %s THEN 5 ", $like );
$search_orderby .= $wpdb->prepare( "WHEN {$wpdb->posts}.post_excerpt LIKE %s THEN 4 ", $like );
$search_orderby .= $wpdb->prepare( "WHEN {$wpdb->posts}.post_content LIKE %s THEN 5 ", $like );
}
if ( $search_orderby ) {
@ -1490,6 +1487,8 @@ class WP_Query {
* @return string|false Table-prefixed value to used in the ORDER clause. False otherwise.
*/
protected function parse_orderby( $orderby ) {
global $wpdb;
// Used to filter values.
$allowed_keys = array(
'post_name', 'post_author', 'post_date', 'post_title', 'post_modified',
@ -1536,7 +1535,7 @@ class WP_Query {
case 'ID':
case 'menu_order':
case 'comment_count':
$orderby_clause = "{$this->db->posts}.{$orderby}";
$orderby_clause = "{$wpdb->posts}.{$orderby}";
break;
case 'rand':
$orderby_clause = 'RAND()';
@ -1561,7 +1560,7 @@ class WP_Query {
$orderby_clause = $orderby;
} else {
// Default: order by post field.
$orderby_clause = "{$this->db->posts}.post_" . sanitize_key( $orderby );
$orderby_clause = "{$wpdb->posts}.post_" . sanitize_key( $orderby );
}
break;
@ -1651,6 +1650,8 @@ class WP_Query {
* @return array List of posts.
*/
public function get_posts() {
global $wpdb;
$this->parse_query();
/**
@ -1786,35 +1787,35 @@ class WP_Query {
switch ( $q['fields'] ) {
case 'ids':
$fields = "{$this->db->posts}.ID";
$fields = "{$wpdb->posts}.ID";
break;
case 'id=>parent':
$fields = "{$this->db->posts}.ID, {$this->db->posts}.post_parent";
$fields = "{$wpdb->posts}.ID, {$wpdb->posts}.post_parent";
break;
default:
$fields = "{$this->db->posts}.*";
$fields = "{$wpdb->posts}.*";
}
if ( '' !== $q['menu_order'] ) {
$where .= " AND {$this->db->posts}.menu_order = " . $q['menu_order'];
$where .= " AND {$wpdb->posts}.menu_order = " . $q['menu_order'];
}
// The "m" parameter is meant for months but accepts datetimes of varying specificity
if ( $q['m'] ) {
$where .= " AND YEAR({$this->db->posts}.post_date)=" . substr($q['m'], 0, 4);
$where .= " AND YEAR({$wpdb->posts}.post_date)=" . substr($q['m'], 0, 4);
if ( strlen($q['m']) > 5 ) {
$where .= " AND MONTH({$this->db->posts}.post_date)=" . substr($q['m'], 4, 2);
$where .= " AND MONTH({$wpdb->posts}.post_date)=" . substr($q['m'], 4, 2);
}
if ( strlen($q['m']) > 7 ) {
$where .= " AND DAYOFMONTH({$this->db->posts}.post_date)=" . substr($q['m'], 6, 2);
$where .= " AND DAYOFMONTH({$wpdb->posts}.post_date)=" . substr($q['m'], 6, 2);
}
if ( strlen($q['m']) > 9 ) {
$where .= " AND HOUR({$this->db->posts}.post_date)=" . substr($q['m'], 8, 2);
$where .= " AND HOUR({$wpdb->posts}.post_date)=" . substr($q['m'], 8, 2);
}
if ( strlen($q['m']) > 11 ) {
$where .= " AND MINUTE({$this->db->posts}.post_date)=" . substr($q['m'], 10, 2);
$where .= " AND MINUTE({$wpdb->posts}.post_date)=" . substr($q['m'], 10, 2);
}
if ( strlen($q['m']) > 13 ) {
$where .= " AND SECOND({$this->db->posts}.post_date)=" . substr($q['m'], 12, 2);
$where .= " AND SECOND({$wpdb->posts}.post_date)=" . substr($q['m'], 12, 2);
}
}
@ -1878,13 +1879,13 @@ class WP_Query {
}
if ( '' !== $q['title'] ) {
$where .= $this->db->prepare( " AND {$this->db->posts}.post_title = %s", stripslashes( $q['title'] ) );
$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_title = %s", stripslashes( $q['title'] ) );
}
// Parameters related to 'post_name'.
if ( '' != $q['name'] ) {
$q['name'] = sanitize_title_for_query( $q['name'] );
$where .= " AND {$this->db->posts}.post_name = '" . $q['name'] . "'";
$where .= " AND {$wpdb->posts}.post_name = '" . $q['name'] . "'";
} elseif ( '' != $q['pagename'] ) {
if ( isset($this->queried_object_id) ) {
$reqpage = $this->queried_object_id;
@ -1913,7 +1914,7 @@ class WP_Query {
if ( ('page' != get_option('show_on_front') ) || empty($page_for_posts) || ( $reqpage != $page_for_posts ) ) {
$q['pagename'] = sanitize_title_for_query( wp_basename( $q['pagename'] ) );
$q['name'] = $q['pagename'];
$where .= " AND ({$this->db->posts}.ID = '$reqpage')";
$where .= " AND ({$wpdb->posts}.ID = '$reqpage')";
$reqpage_obj = get_post( $reqpage );
if ( is_object($reqpage_obj) && 'attachment' == $reqpage_obj->post_type ) {
$this->is_attachment = true;
@ -1925,11 +1926,11 @@ class WP_Query {
} elseif ( '' != $q['attachment'] ) {
$q['attachment'] = sanitize_title_for_query( wp_basename( $q['attachment'] ) );
$q['name'] = $q['attachment'];
$where .= " AND {$this->db->posts}.post_name = '" . $q['attachment'] . "'";
$where .= " AND {$wpdb->posts}.post_name = '" . $q['attachment'] . "'";
} elseif ( is_array( $q['post_name__in'] ) && ! empty( $q['post_name__in'] ) ) {
$q['post_name__in'] = array_map( 'sanitize_title_for_query', $q['post_name__in'] );
$post_name__in = "'" . implode( "','", $q['post_name__in'] ) . "'";
$where .= " AND {$this->db->posts}.post_name IN ($post_name__in)";
$where .= " AND {$wpdb->posts}.post_name IN ($post_name__in)";
}
// If an attachment is requested by number, let it supersede any post number.
@ -1938,29 +1939,29 @@ class WP_Query {
// If a post number is specified, load that post
if ( $q['p'] ) {
$where .= " AND {$this->db->posts}.ID = " . $q['p'];
$where .= " AND {$wpdb->posts}.ID = " . $q['p'];
} elseif ( $q['post__in'] ) {
$post__in = implode(',', array_map( 'absint', $q['post__in'] ));
$where .= " AND {$this->db->posts}.ID IN ($post__in)";
$where .= " AND {$wpdb->posts}.ID IN ($post__in)";
} elseif ( $q['post__not_in'] ) {
$post__not_in = implode(',', array_map( 'absint', $q['post__not_in'] ));
$where .= " AND {$this->db->posts}.ID NOT IN ($post__not_in)";
$where .= " AND {$wpdb->posts}.ID NOT IN ($post__not_in)";
}
if ( is_numeric( $q['post_parent'] ) ) {
$where .= $this->db->prepare( " AND {$this->db->posts}.post_parent = %d ", $q['post_parent'] );
$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_parent = %d ", $q['post_parent'] );
} elseif ( $q['post_parent__in'] ) {
$post_parent__in = implode( ',', array_map( 'absint', $q['post_parent__in'] ) );
$where .= " AND {$this->db->posts}.post_parent IN ($post_parent__in)";
$where .= " AND {$wpdb->posts}.post_parent IN ($post_parent__in)";
} elseif ( $q['post_parent__not_in'] ) {
$post_parent__not_in = implode( ',', array_map( 'absint', $q['post_parent__not_in'] ) );
$where .= " AND {$this->db->posts}.post_parent NOT IN ($post_parent__not_in)";
$where .= " AND {$wpdb->posts}.post_parent NOT IN ($post_parent__not_in)";
}
if ( $q['page_id'] ) {
if ( ('page' != get_option('show_on_front') ) || ( $q['page_id'] != get_option('page_for_posts') ) ) {
$q['p'] = $q['page_id'];
$where = " AND {$this->db->posts}.ID = " . $q['page_id'];
$where = " AND {$wpdb->posts}.ID = " . $q['page_id'];
}
}
@ -1985,7 +1986,7 @@ class WP_Query {
if ( !$this->is_singular ) {
$this->parse_tax_query( $q );
$clauses = $this->tax_query->get_sql( $this->db->posts, 'ID' );
$clauses = $this->tax_query->get_sql( $wpdb->posts, 'ID' );
$join .= $clauses['join'];
$where .= $clauses['where'];
@ -2069,7 +2070,7 @@ class WP_Query {
}
if ( !empty( $this->tax_query->queries ) || !empty( $this->meta_query->queries ) ) {
$groupby = "{$this->db->posts}.ID";
$groupby = "{$wpdb->posts}.ID";
}
// Author/user stuff
@ -2086,10 +2087,10 @@ class WP_Query {
if ( ! empty( $q['author__not_in'] ) ) {
$author__not_in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__not_in'] ) ) );
$where .= " AND {$this->db->posts}.post_author NOT IN ($author__not_in) ";
$where .= " AND {$wpdb->posts}.post_author NOT IN ($author__not_in) ";
} elseif ( ! empty( $q['author__in'] ) ) {
$author__in = implode( ',', array_map( 'absint', array_unique( (array) $q['author__in'] ) ) );
$where .= " AND {$this->db->posts}.post_author IN ($author__in) ";
$where .= " AND {$wpdb->posts}.post_author IN ($author__in) ";
}
// Author stuff for nice URLs
@ -2107,18 +2108,18 @@ class WP_Query {
$q['author'] = get_user_by('slug', $q['author_name']);
if ( $q['author'] )
$q['author'] = $q['author']->ID;
$whichauthor .= " AND ({$this->db->posts}.post_author = " . absint($q['author']) . ')';
$whichauthor .= " AND ({$wpdb->posts}.post_author = " . absint($q['author']) . ')';
}
// MIME-Type stuff for attachment browsing
if ( isset( $q['post_mime_type'] ) && '' != $q['post_mime_type'] ) {
$whichmimetype = wp_post_mime_type_where( $q['post_mime_type'], $this->db->posts );
$whichmimetype = wp_post_mime_type_where( $q['post_mime_type'], $wpdb->posts );
}
$where .= $search . $whichauthor . $whichmimetype;
if ( ! empty( $this->meta_query->queries ) ) {
$clauses = $this->meta_query->get_sql( 'post', $this->db->posts, 'ID', $this );
$clauses = $this->meta_query->get_sql( 'post', $wpdb->posts, 'ID', $this );
$join .= $clauses['join'];
$where .= $clauses['where'];
}
@ -2139,16 +2140,16 @@ class WP_Query {
if ( isset( $q['orderby'] ) && ( is_array( $q['orderby'] ) || false === $q['orderby'] ) ) {
$orderby = '';
} else {
$orderby = "{$this->db->posts}.post_date " . $q['order'];
$orderby = "{$wpdb->posts}.post_date " . $q['order'];
}
} elseif ( 'none' == $q['orderby'] ) {
$orderby = '';
} elseif ( $q['orderby'] == 'post__in' && ! empty( $post__in ) ) {
$orderby = "FIELD( {$this->db->posts}.ID, $post__in )";
$orderby = "FIELD( {$wpdb->posts}.ID, $post__in )";
} elseif ( $q['orderby'] == 'post_parent__in' && ! empty( $post_parent__in ) ) {
$orderby = "FIELD( {$this->db->posts}.post_parent, $post_parent__in )";
$orderby = "FIELD( {$wpdb->posts}.post_parent, $post_parent__in )";
} elseif ( $q['orderby'] == 'post_name__in' && ! empty( $post_name__in ) ) {
$orderby = "FIELD( {$this->db->posts}.post_name, $post_name__in )";
$orderby = "FIELD( {$wpdb->posts}.post_name, $post_name__in )";
} else {
$orderby_array = array();
if ( is_array( $q['orderby'] ) ) {
@ -2180,7 +2181,7 @@ class WP_Query {
$orderby = implode( ' ' . $q['order'] . ', ', $orderby_array );
if ( empty( $orderby ) ) {
$orderby = "{$this->db->posts}.post_date " . $q['order'];
$orderby = "{$wpdb->posts}.post_date " . $q['order'];
} elseif ( ! empty( $q['order'] ) ) {
$orderby .= " {$q['order']}";
}
@ -2220,20 +2221,20 @@ class WP_Query {
}
if ( isset( $q['post_password'] ) ) {
$where .= $this->db->prepare( " AND {$this->db->posts}.post_password = %s", $q['post_password'] );
$where .= $wpdb->prepare( " AND {$wpdb->posts}.post_password = %s", $q['post_password'] );
if ( empty( $q['perm'] ) ) {
$q['perm'] = 'readable';
}
} elseif ( isset( $q['has_password'] ) ) {
$where .= sprintf( " AND {$this->db->posts}.post_password %s ''", $q['has_password'] ? '!=' : '=' );
$where .= sprintf( " AND {$wpdb->posts}.post_password %s ''", $q['has_password'] ? '!=' : '=' );
}
if ( ! empty( $q['comment_status'] ) ) {
$where .= $this->db->prepare( " AND {$this->db->posts}.comment_status = %s ", $q['comment_status'] );
$where .= $wpdb->prepare( " AND {$wpdb->posts}.comment_status = %s ", $q['comment_status'] );
}
if ( ! empty( $q['ping_status'] ) ) {
$where .= $this->db->prepare( " AND {$this->db->posts}.ping_status = %s ", $q['ping_status'] );
$where .= $wpdb->prepare( " AND {$wpdb->posts}.ping_status = %s ", $q['ping_status'] );
}
if ( 'any' == $post_type ) {
@ -2241,21 +2242,21 @@ class WP_Query {
if ( empty( $in_search_post_types ) ) {
$where .= ' AND 1=0 ';
} else {
$where .= " AND {$this->db->posts}.post_type IN ('" . join("', '", $in_search_post_types ) . "')";
$where .= " AND {$wpdb->posts}.post_type IN ('" . join("', '", $in_search_post_types ) . "')";
}
} elseif ( !empty( $post_type ) && is_array( $post_type ) ) {
$where .= " AND {$this->db->posts}.post_type IN ('" . join("', '", $post_type) . "')";
$where .= " AND {$wpdb->posts}.post_type IN ('" . join("', '", $post_type) . "')";
} elseif ( ! empty( $post_type ) ) {
$where .= " AND {$this->db->posts}.post_type = '$post_type'";
$where .= " AND {$wpdb->posts}.post_type = '$post_type'";
$post_type_object = get_post_type_object ( $post_type );
} elseif ( $this->is_attachment ) {
$where .= " AND {$this->db->posts}.post_type = 'attachment'";
$where .= " AND {$wpdb->posts}.post_type = 'attachment'";
$post_type_object = get_post_type_object ( 'attachment' );
} elseif ( $this->is_page ) {
$where .= " AND {$this->db->posts}.post_type = 'page'";
$where .= " AND {$wpdb->posts}.post_type = 'page'";
$post_type_object = get_post_type_object ( 'page' );
} else {
$where .= " AND {$this->db->posts}.post_type = 'post'";
$where .= " AND {$wpdb->posts}.post_type = 'post'";
$post_type_object = get_post_type_object ( 'post' );
}
@ -2284,16 +2285,16 @@ class WP_Query {
if ( in_array( 'any', $q_status ) ) {
foreach ( get_post_stati( array( 'exclude_from_search' => true ) ) as $status ) {
if ( ! in_array( $status, $q_status ) ) {
$e_status[] = "{$this->db->posts}.post_status <> '$status'";
$e_status[] = "{$wpdb->posts}.post_status <> '$status'";
}
}
} else {
foreach ( get_post_stati() as $status ) {
if ( in_array( $status, $q_status ) ) {
if ( 'private' == $status ) {
$p_status[] = "{$this->db->posts}.post_status = '$status'";
$p_status[] = "{$wpdb->posts}.post_status = '$status'";
} else {
$r_status[] = "{$this->db->posts}.post_status = '$status'";
$r_status[] = "{$wpdb->posts}.post_status = '$status'";
}
}
}
@ -2309,22 +2310,22 @@ class WP_Query {
}
if ( !empty($r_status) ) {
if ( !empty($q['perm'] ) && 'editable' == $q['perm'] && !current_user_can($edit_others_cap) ) {
$statuswheres[] = "({$this->db->posts}.post_author = $user_id " . "AND (" . join( ' OR ', $r_status ) . "))";
$statuswheres[] = "({$wpdb->posts}.post_author = $user_id " . "AND (" . join( ' OR ', $r_status ) . "))";
} else {
$statuswheres[] = "(" . join( ' OR ', $r_status ) . ")";
}
}
if ( !empty($p_status) ) {
if ( !empty($q['perm'] ) && 'readable' == $q['perm'] && !current_user_can($read_private_cap) ) {
$statuswheres[] = "({$this->db->posts}.post_author = $user_id " . "AND (" . join( ' OR ', $p_status ) . "))";
$statuswheres[] = "({$wpdb->posts}.post_author = $user_id " . "AND (" . join( ' OR ', $p_status ) . "))";
} else {
$statuswheres[] = "(" . join( ' OR ', $p_status ) . ")";
}
}
if ( $post_status_join ) {
$join .= " LEFT JOIN {$this->db->posts} AS p2 ON ({$this->db->posts}.post_parent = p2.ID) ";
$join .= " LEFT JOIN {$wpdb->posts} AS p2 ON ({$wpdb->posts}.post_parent = p2.ID) ";
foreach ( $statuswheres as $index => $statuswhere ) {
$statuswheres[$index] = "($statuswhere OR ({$this->db->posts}.post_status = 'inherit' AND " . str_replace( $this->db->posts, 'p2', $statuswhere ) . "))";
$statuswheres[$index] = "($statuswhere OR ({$wpdb->posts}.post_status = 'inherit' AND " . str_replace( $wpdb->posts, 'p2', $statuswhere ) . "))";
}
}
$where_status = implode( ' OR ', $statuswheres );
@ -2332,21 +2333,21 @@ class WP_Query {
$where .= " AND ($where_status)";
}
} elseif ( !$this->is_singular ) {
$where .= " AND ({$this->db->posts}.post_status = 'publish'";
$where .= " AND ({$wpdb->posts}.post_status = 'publish'";
// Add public states.
$public_states = get_post_stati( array('public' => true) );
foreach ( (array) $public_states as $state ) {
if ( 'publish' == $state ) // Publish is hard-coded above.
continue;
$where .= " OR {$this->db->posts}.post_status = '$state'";
$where .= " OR {$wpdb->posts}.post_status = '$state'";
}
if ( $this->is_admin ) {
// Add protected states that should show in the admin all list.
$admin_all_states = get_post_stati( array('protected' => true, 'show_in_admin_all_list' => true) );
foreach ( (array) $admin_all_states as $state ) {
$where .= " OR {$this->db->posts}.post_status = '$state'";
$where .= " OR {$wpdb->posts}.post_status = '$state'";
}
}
@ -2354,7 +2355,7 @@ class WP_Query {
// Add private states that are limited to viewing by the author of a post or someone who has caps to read private states.
$private_states = get_post_stati( array('private' => true) );
foreach ( (array) $private_states as $state ) {
$where .= current_user_can( $read_private_cap ) ? " OR {$this->db->posts}.post_status = '$state'" : " OR {$this->db->posts}.post_author = $user_id AND {$this->db->posts}.post_status = '$state'";
$where .= current_user_can( $read_private_cap ) ? " OR {$wpdb->posts}.post_status = '$state'" : " OR {$wpdb->posts}.post_author = $user_id AND {$wpdb->posts}.post_status = '$state'";
}
}
@ -2406,11 +2407,11 @@ class WP_Query {
// Comments feeds
if ( $this->is_comment_feed && ! $this->is_singular ) {
if ( $this->is_archive || $this->is_search ) {
$cjoin = "JOIN {$this->db->posts} ON ({$this->db->comments}.comment_post_ID = {$this->db->posts}.ID) $join ";
$cjoin = "JOIN {$wpdb->posts} ON ({$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID) $join ";
$cwhere = "WHERE comment_approved = '1' $where";
$cgroupby = "{$this->db->comments}.comment_id";
$cgroupby = "{$wpdb->comments}.comment_id";
} else { // Other non singular e.g. front
$cjoin = "JOIN {$this->db->posts} ON ( {$this->db->comments}.comment_post_ID = {$this->db->posts}.ID )";
$cjoin = "JOIN {$wpdb->posts} ON ( {$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID )";
$cwhere = "WHERE ( post_status = 'publish' OR ( post_status = 'inherit' AND post_type = 'attachment' ) ) AND comment_approved = '1'";
$cgroupby = '';
}
@ -2469,7 +2470,7 @@ class WP_Query {
$cgroupby = ( ! empty( $cgroupby ) ) ? 'GROUP BY ' . $cgroupby : '';
$corderby = ( ! empty( $corderby ) ) ? 'ORDER BY ' . $corderby : '';
$comments = (array) $this->db->get_results("SELECT $distinct {$this->db->comments}.* FROM {$this->db->comments} $cjoin $cwhere $cgroupby $corderby $climits");
$comments = (array) $wpdb->get_results("SELECT $distinct {$wpdb->comments}.* FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits");
// Convert to WP_Comment
$this->comments = array_map( 'get_comment', $comments );
$this->comment_count = count($this->comments);
@ -2482,7 +2483,7 @@ class WP_Query {
$post_ids = join(',', $post_ids);
$join = '';
if ( $post_ids ) {
$where = "AND {$this->db->posts}.ID IN ($post_ids) ";
$where = "AND {$wpdb->posts}.ID IN ($post_ids) ";
} else {
$where = "AND 0";
}
@ -2724,7 +2725,7 @@ class WP_Query {
if ( !$q['no_found_rows'] && !empty($limits) )
$found_rows = 'SQL_CALC_FOUND_ROWS';
$this->request = $old_request = "SELECT $found_rows $distinct $fields FROM {$this->db->posts} $join WHERE 1=1 $where $groupby $orderby $limits";
$this->request = $old_request = "SELECT $found_rows $distinct $fields FROM {$wpdb->posts} $join WHERE 1=1 $where $groupby $orderby $limits";
if ( !$q['suppress_filters'] ) {
/**
@ -2758,7 +2759,7 @@ class WP_Query {
if ( 'ids' == $q['fields'] ) {
if ( null === $this->posts ) {
$this->posts = $this->db->get_col( $this->request );
$this->posts = $wpdb->get_col( $this->request );
}
$this->posts = array_map( 'intval', $this->posts );
@ -2770,7 +2771,7 @@ class WP_Query {
if ( 'id=>parent' == $q['fields'] ) {
if ( null === $this->posts ) {
$this->posts = $this->db->get_results( $this->request );
$this->posts = $wpdb->get_results( $this->request );
}
$this->post_count = count( $this->posts );
@ -2788,7 +2789,7 @@ class WP_Query {
}
if ( null === $this->posts ) {
$split_the_query = ( $old_request == $this->request && "{$this->db->posts}.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 );
$split_the_query = ( $old_request == $this->request && "{$wpdb->posts}.*" == $fields && !empty( $limits ) && $q['posts_per_page'] < 500 );
/**
* Filters whether to split the query.
@ -2807,7 +2808,7 @@ class WP_Query {
if ( $split_the_query ) {
// First get the IDs and then fill in the objects
$this->request = "SELECT $found_rows $distinct {$this->db->posts}.ID FROM {$this->db->posts} $join WHERE 1=1 $where $groupby $orderby $limits";
$this->request = "SELECT $found_rows $distinct {$wpdb->posts}.ID FROM {$wpdb->posts} $join WHERE 1=1 $where $groupby $orderby $limits";
/**
* Filters the Post IDs SQL request before sending.
@ -2819,7 +2820,7 @@ class WP_Query {
*/
$this->request = apply_filters( 'posts_request_ids', $this->request, $this );
$ids = $this->db->get_col( $this->request );
$ids = $wpdb->get_col( $this->request );
if ( $ids ) {
$this->posts = $ids;
@ -2829,7 +2830,7 @@ class WP_Query {
$this->posts = array();
}
} else {
$this->posts = $this->db->get_results( $this->request );
$this->posts = $wpdb->get_results( $this->request );
$this->set_found_posts( $q, $limits );
}
}
@ -2869,8 +2870,8 @@ class WP_Query {
/** This filter is documented in wp-includes/query.php */
$climits = apply_filters_ref_array( 'comment_feed_limits', array( 'LIMIT ' . get_option('posts_per_rss'), &$this ) );
$comments_request = "SELECT {$this->db->comments}.* FROM {$this->db->comments} $cjoin $cwhere $cgroupby $corderby $climits";
$comments = $this->db->get_results($comments_request);
$comments_request = "SELECT {$wpdb->comments}.* FROM {$wpdb->comments} $cjoin $cwhere $cgroupby $corderby $climits";
$comments = $wpdb->get_results($comments_request);
// Convert to WP_Comment
$this->comments = array_map( 'get_comment', $comments );
$this->comment_count = count($this->comments);
@ -3017,6 +3018,7 @@ class WP_Query {
* @param string $limits LIMIT clauses of the query.
*/
private function set_found_posts( $q, $limits ) {
global $wpdb;
// Bail if posts is an empty array. Continue if posts is an empty string,
// null, or false to accommodate caching plugins that fill posts later.
if ( $q['no_found_rows'] || ( is_array( $this->posts ) && ! $this->posts ) )
@ -3031,7 +3033,7 @@ class WP_Query {
* @param string $found_posts The query to run to find the found posts.
* @param WP_Query &$this The WP_Query instance (passed by reference).
*/
$this->found_posts = $this->db->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
$this->found_posts = $wpdb->get_var( apply_filters_ref_array( 'found_posts_query', array( 'SELECT FOUND_ROWS()', &$this ) ) );
} else {
$this->found_posts = count( $this->posts );
}
@ -3328,8 +3330,6 @@ class WP_Query {
* @param string|array $query URL query string or array of vars.
*/
public function __construct( $query = '' ) {
$this->db = $GLOBALS['wpdb'];
if ( ! empty( $query ) ) {
$this->query( $query );
}

View File

@ -69,21 +69,12 @@ class WP_Roles {
*/
public $use_db = true;
/**
* @since 4.7.0
* @access protected
* @var wpdb
*/
protected $db;
/**
* Constructor
*
* @since 2.0.0
*/
public function __construct() {
$this->db = $GLOBALS['wpdb'];
$this->_init();
}
@ -117,8 +108,9 @@ class WP_Roles {
* @global array $wp_user_roles Used to set the 'roles' property value.
*/
protected function _init() {
global $wp_user_roles;
$this->role_key = $this->db->get_blog_prefix() . 'user_roles';
global $wp_user_roles, $wpdb;
$this->role_key = $wpdb->get_blog_prefix() . 'user_roles';
if ( ! empty( $wp_user_roles ) ) {
$this->roles = $wp_user_roles;
$this->use_db = false;
@ -147,13 +139,15 @@ class WP_Roles {
* @access public
*/
public function reinit() {
global $wpdb;
// There is no need to reinit if using the wp_user_roles global.
if ( ! $this->use_db ) {
return;
}
// Duplicated from _init() to avoid an extra function call.
$this->role_key = $this->db->get_blog_prefix() . 'user_roles';
$this->role_key = $wpdb->get_blog_prefix() . 'user_roles';
$this->roles = get_option( $this->role_key );
if ( empty( $this->roles ) )
return;

View File

@ -95,13 +95,6 @@ class WP_Site_Query {
*/
public $max_num_pages = 0;
/**
* @since 4.7.0
* @access protected
* @var wpdb
*/
protected $db;
/**
* Sets up the site query, based on the query vars passed.
*
@ -152,8 +145,6 @@ class WP_Site_Query {
* }
*/
public function __construct( $query = '' ) {
$this->db = $GLOBALS['wpdb'];
$this->query_var_defaults = array(
'fields' => '',
'ID' => '',
@ -332,9 +323,13 @@ class WP_Site_Query {
* @since 4.6.0
* @access protected
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @return int|array A single count of site IDs if a count query. An array of site IDs if a full query.
*/
protected function get_site_ids() {
global $wpdb;
$order = $this->parse_order( $this->query_vars['order'] );
// Disable ORDER BY with 'none', an empty array, or boolean false.
@ -398,7 +393,7 @@ class WP_Site_Query {
// Parse site IDs for an IN clause.
$site_id = absint( $this->query_vars['ID'] );
if ( ! empty( $site_id ) ) {
$this->sql_clauses['where']['ID'] = $this->db->prepare( 'blog_id = %d', $site_id );
$this->sql_clauses['where']['ID'] = $wpdb->prepare( 'blog_id = %d', $site_id );
}
// Parse site IDs for an IN clause.
@ -414,7 +409,7 @@ class WP_Site_Query {
$network_id = absint( $this->query_vars['network_id'] );
if ( ! empty( $network_id ) ) {
$this->sql_clauses['where']['network_id'] = $this->db->prepare( 'site_id = %d', $network_id );
$this->sql_clauses['where']['network_id'] = $wpdb->prepare( 'site_id = %d', $network_id );
}
// Parse site network IDs for an IN clause.
@ -428,56 +423,56 @@ class WP_Site_Query {
}
if ( ! empty( $this->query_vars['domain'] ) ) {
$this->sql_clauses['where']['domain'] = $this->db->prepare( 'domain = %s', $this->query_vars['domain'] );
$this->sql_clauses['where']['domain'] = $wpdb->prepare( 'domain = %s', $this->query_vars['domain'] );
}
// Parse site domain for an IN clause.
if ( is_array( $this->query_vars['domain__in'] ) ) {
$this->sql_clauses['where']['domain__in'] = "domain IN ( '" . implode( "', '", $this->db->_escape( $this->query_vars['domain__in'] ) ) . "' )";
$this->sql_clauses['where']['domain__in'] = "domain IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['domain__in'] ) ) . "' )";
}
// Parse site domain for a NOT IN clause.
if ( is_array( $this->query_vars['domain__not_in'] ) ) {
$this->sql_clauses['where']['domain__not_in'] = "domain NOT IN ( '" . implode( "', '", $this->db->_escape( $this->query_vars['domain__not_in'] ) ) . "' )";
$this->sql_clauses['where']['domain__not_in'] = "domain NOT IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['domain__not_in'] ) ) . "' )";
}
if ( ! empty( $this->query_vars['path'] ) ) {
$this->sql_clauses['where']['path'] = $this->db->prepare( 'path = %s', $this->query_vars['path'] );
$this->sql_clauses['where']['path'] = $wpdb->prepare( 'path = %s', $this->query_vars['path'] );
}
// Parse site path for an IN clause.
if ( is_array( $this->query_vars['path__in'] ) ) {
$this->sql_clauses['where']['path__in'] = "path IN ( '" . implode( "', '", $this->db->_escape( $this->query_vars['path__in'] ) ) . "' )";
$this->sql_clauses['where']['path__in'] = "path IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['path__in'] ) ) . "' )";
}
// Parse site path for a NOT IN clause.
if ( is_array( $this->query_vars['path__not_in'] ) ) {
$this->sql_clauses['where']['path__not_in'] = "path NOT IN ( '" . implode( "', '", $this->db->_escape( $this->query_vars['path__not_in'] ) ) . "' )";
$this->sql_clauses['where']['path__not_in'] = "path NOT IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['path__not_in'] ) ) . "' )";
}
if ( is_numeric( $this->query_vars['archived'] ) ) {
$archived = absint( $this->query_vars['archived'] );
$this->sql_clauses['where']['archived'] = $this->db->prepare( "archived = %d ", $archived );
$this->sql_clauses['where']['archived'] = $wpdb->prepare( "archived = %d ", $archived );
}
if ( is_numeric( $this->query_vars['mature'] ) ) {
$mature = absint( $this->query_vars['mature'] );
$this->sql_clauses['where']['mature'] = $this->db->prepare( "mature = %d ", $mature );
$this->sql_clauses['where']['mature'] = $wpdb->prepare( "mature = %d ", $mature );
}
if ( is_numeric( $this->query_vars['spam'] ) ) {
$spam = absint( $this->query_vars['spam'] );
$this->sql_clauses['where']['spam'] = $this->db->prepare( "spam = %d ", $spam );
$this->sql_clauses['where']['spam'] = $wpdb->prepare( "spam = %d ", $spam );
}
if ( is_numeric( $this->query_vars['deleted'] ) ) {
$deleted = absint( $this->query_vars['deleted'] );
$this->sql_clauses['where']['deleted'] = $this->db->prepare( "deleted = %d ", $deleted );
$this->sql_clauses['where']['deleted'] = $wpdb->prepare( "deleted = %d ", $deleted );
}
if ( is_numeric( $this->query_vars['public'] ) ) {
$public = absint( $this->query_vars['public'] );
$this->sql_clauses['where']['public'] = $this->db->prepare( "public = %d ", $public );
$this->sql_clauses['where']['public'] = $wpdb->prepare( "public = %d ", $public );
}
// Falsey search strings are ignored.
@ -555,7 +550,7 @@ class WP_Site_Query {
}
$this->sql_clauses['select'] = "SELECT $found_rows $fields";
$this->sql_clauses['from'] = "FROM {$this->db->blogs} $join";
$this->sql_clauses['from'] = "FROM $wpdb->blogs $join";
$this->sql_clauses['groupby'] = $groupby;
$this->sql_clauses['orderby'] = $orderby;
$this->sql_clauses['limits'] = $limits;
@ -563,10 +558,10 @@ class WP_Site_Query {
$this->request = "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} {$this->sql_clauses['orderby']} {$this->sql_clauses['limits']}";
if ( $this->query_vars['count'] ) {
return intval( $this->db->get_var( $this->request ) );
return intval( $wpdb->get_var( $this->request ) );
}
$site_ids = $this->db->get_col( $this->request );
$site_ids = $wpdb->get_col( $this->request );
return array_map( 'intval', $site_ids );
}
@ -577,8 +572,12 @@ class WP_Site_Query {
*
* @since 4.6.0
* @access private
*
* @global wpdb $wpdb WordPress database abstraction object.
*/
private function set_found_sites() {
global $wpdb;
if ( $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
/**
* Filters the query used to retrieve found site count.
@ -590,7 +589,7 @@ class WP_Site_Query {
*/
$found_sites_query = apply_filters( 'found_sites_query', 'SELECT FOUND_ROWS()', $this );
$this->found_sites = (int) $this->db->get_var( $found_sites_query );
$this->found_sites = (int) $wpdb->get_var( $found_sites_query );
}
}
@ -600,20 +599,24 @@ class WP_Site_Query {
* @since 4.6.0
* @access protected
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $string Search string.
* @param array $columns Columns to search.
* @return string Search SQL.
*/
protected function get_search_sql( $string, $columns ) {
global $wpdb;
if ( false !== strpos( $string, '*' ) ) {
$like = '%' . implode( '%', array_map( array( $this->db, 'esc_like' ), explode( '*', $string ) ) ) . '%';
$like = '%' . implode( '%', array_map( array( $wpdb, 'esc_like' ), explode( '*', $string ) ) ) . '%';
} else {
$like = '%' . $this->db->esc_like( $string ) . '%';
$like = '%' . $wpdb->esc_like( $string ) . '%';
}
$searches = array();
foreach ( $columns as $column ) {
$searches[] = $this->db->prepare( "$column LIKE %s", $like );
$searches[] = $wpdb->prepare( "$column LIKE %s", $like );
}
return '(' . implode( ' OR ', $searches ) . ')';
@ -625,20 +628,24 @@ class WP_Site_Query {
* @since 4.6.0
* @access protected
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $orderby Alias for the field to order by.
* @return string|false Value to used in the ORDER clause. False otherwise.
*/
protected function parse_orderby( $orderby ) {
global $wpdb;
$parsed = false;
switch ( $orderby ) {
case 'site__in':
$site__in = implode( ',', array_map( 'absint', $this->query_vars['site__in'] ) );
$parsed = "FIELD( {$this->db->blogs}.blog_id, $site__in )";
$parsed = "FIELD( {$wpdb->blogs}.blog_id, $site__in )";
break;
case 'network__in':
$network__in = implode( ',', array_map( 'absint', $this->query_vars['network__in'] ) );
$parsed = "FIELD( {$this->db->blogs}.site_id, $network__in )";
$parsed = "FIELD( {$wpdb->blogs}.site_id, $network__in )";
break;
case 'domain':
case 'last_updated':

View File

@ -91,13 +91,6 @@ class WP_Tax_Query {
*/
public $primary_id_column;
/**
* @since 4.7.0
* @access protected
* @var wpdb
*/
protected $db;
/**
* Constructor.
*
@ -126,8 +119,6 @@ class WP_Tax_Query {
* }
*/
public function __construct( $tax_query ) {
$this->db = $GLOBALS['wpdb'];
if ( isset( $tax_query['relation'] ) ) {
$this->relation = $this->sanitize_relation( $tax_query['relation'] );
} else {
@ -396,6 +387,8 @@ class WP_Tax_Query {
* @since 4.1.0
* @access public
*
* @global wpdb $wpdb The WordPress database abstraction object.
*
* @param array $clause Query clause, passed by reference.
* @param array $parent_query Parent query array.
* @return array {
@ -406,6 +399,8 @@ class WP_Tax_Query {
* }
*/
public function get_sql_for_clause( &$clause, $parent_query ) {
global $wpdb;
$sql = array(
'where' => array(),
'join' => array(),
@ -437,7 +432,7 @@ class WP_Tax_Query {
$alias = $this->find_compatible_table_alias( $clause, $parent_query );
if ( false === $alias ) {
$i = count( $this->table_aliases );
$alias = $i ? 'tt' . $i : $this->db->term_relationships;
$alias = $i ? 'tt' . $i : $wpdb->term_relationships;
// Store the alias as part of a flat array to build future iterators.
$this->table_aliases[] = $alias;
@ -445,7 +440,7 @@ class WP_Tax_Query {
// Store the alias with this clause, so later siblings can use it.
$clause['alias'] = $alias;
$join .= " LEFT JOIN {$this->db->term_relationships}";
$join .= " LEFT JOIN $wpdb->term_relationships";
$join .= $i ? " AS $alias" : '';
$join .= " ON ($this->primary_table.$this->primary_id_column = $alias.object_id)";
}
@ -463,7 +458,7 @@ class WP_Tax_Query {
$where = "$this->primary_table.$this->primary_id_column NOT IN (
SELECT object_id
FROM {$this->db->term_relationships}
FROM $wpdb->term_relationships
WHERE term_taxonomy_id IN ($terms)
)";
@ -479,20 +474,20 @@ class WP_Tax_Query {
$where = "(
SELECT COUNT(1)
FROM {$this->db->term_relationships}
FROM $wpdb->term_relationships
WHERE term_taxonomy_id IN ($terms)
AND object_id = $this->primary_table.$this->primary_id_column
) = $num_terms";
} elseif ( 'NOT EXISTS' === $operator || 'EXISTS' === $operator ) {
$where = $this->db->prepare( "$operator (
$where = $wpdb->prepare( "$operator (
SELECT 1
FROM {$this->db->term_relationships}
INNER JOIN {$this->db->term_taxonomy}
ON {$this->db->term_taxonomy}.term_taxonomy_id = {$this->db->term_relationships}.term_taxonomy_id
WHERE {$this->db->term_taxonomy}.taxonomy = %s
AND {$this->db->term_relationships}.object_id = $this->primary_table.$this->primary_id_column
FROM $wpdb->term_relationships
INNER JOIN $wpdb->term_taxonomy
ON $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id
WHERE $wpdb->term_taxonomy.taxonomy = %s
AND $wpdb->term_relationships.object_id = $this->primary_table.$this->primary_id_column
)", $clause['taxonomy'] );
}
@ -602,11 +597,15 @@ class WP_Tax_Query {
*
* @since 3.2.0
*
* @global wpdb $wpdb The WordPress database abstraction object.
*
* @param array $query The single query. Passed by reference.
* @param string $resulting_field The resulting field. Accepts 'slug', 'name', 'term_taxonomy_id',
* or 'term_id'. Default 'term_id'.
*/
public function transform_query( &$query, $resulting_field ) {
global $wpdb;
if ( empty( $query['terms'] ) )
return;
@ -629,27 +628,27 @@ class WP_Tax_Query {
$terms = implode( ",", $query['terms'] );
$terms = $this->db->get_col( "
SELECT {$this->db->term_taxonomy}.$resulting_field
FROM {$this->db->term_taxonomy}
INNER JOIN {$this->db->terms} USING (term_id)
$terms = $wpdb->get_col( "
SELECT $wpdb->term_taxonomy.$resulting_field
FROM $wpdb->term_taxonomy
INNER JOIN $wpdb->terms USING (term_id)
WHERE taxonomy = '{$query['taxonomy']}'
AND {$this->db->terms}.{$query['field']} IN ($terms)
AND $wpdb->terms.{$query['field']} IN ($terms)
" );
break;
case 'term_taxonomy_id':
$terms = implode( ',', array_map( 'intval', $query['terms'] ) );
$terms = $this->db->get_col( "
$terms = $wpdb->get_col( "
SELECT $resulting_field
FROM {$this->db->term_taxonomy}
FROM $wpdb->term_taxonomy
WHERE term_taxonomy_id IN ($terms)
" );
break;
default:
$terms = implode( ',', array_map( 'intval', $query['terms'] ) );
$terms = $this->db->get_col( "
$terms = $wpdb->get_col( "
SELECT $resulting_field
FROM {$this->db->term_taxonomy}
FROM $wpdb->term_taxonomy
WHERE taxonomy = '{$query['taxonomy']}'
AND term_id IN ($terms)
" );

View File

@ -86,13 +86,6 @@ class WP_Term_Query {
*/
public $terms;
/**
* @since 4.7.0
* @access protected
* @var wpdb
*/
protected $db;
/**
* Constructor.
*
@ -186,8 +179,6 @@ class WP_Term_Query {
* }
*/
public function __construct( $query = '' ) {
$this->db = $GLOBALS['wpdb'];
$this->query_var_defaults = array(
'taxonomy' => null,
'object_ids' => null,
@ -314,9 +305,13 @@ class WP_Term_Query {
* @param 4.6.0
* @access public
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @return array
*/
public function get_terms() {
global $wpdb;
$this->parse_query( $this->query_vars );
$args = $this->query_vars;
@ -509,16 +504,16 @@ class WP_Term_Query {
$tt_ids = implode( ',', array_map( 'intval', $args['term_taxonomy_id'] ) );
$this->sql_clauses['where']['term_taxonomy_id'] = "tt.term_taxonomy_id IN ({$tt_ids})";
} else {
$this->sql_clauses['where']['term_taxonomy_id'] = $this->db->prepare( "tt.term_taxonomy_id = %d", $args['term_taxonomy_id'] );
$this->sql_clauses['where']['term_taxonomy_id'] = $wpdb->prepare( "tt.term_taxonomy_id = %d", $args['term_taxonomy_id'] );
}
}
if ( ! empty( $args['name__like'] ) ) {
$this->sql_clauses['where']['name__like'] = $this->db->prepare( "t.name LIKE %s", '%' . $this->db->esc_like( $args['name__like'] ) . '%' );
$this->sql_clauses['where']['name__like'] = $wpdb->prepare( "t.name LIKE %s", '%' . $wpdb->esc_like( $args['name__like'] ) . '%' );
}
if ( ! empty( $args['description__like'] ) ) {
$this->sql_clauses['where']['description__like'] = $this->db->prepare( "tt.description LIKE %s", '%' . $this->db->esc_like( $args['description__like'] ) . '%' );
$this->sql_clauses['where']['description__like'] = $wpdb->prepare( "tt.description LIKE %s", '%' . $wpdb->esc_like( $args['description__like'] ) . '%' );
}
if ( ! empty( $args['object_ids'] ) ) {
@ -638,10 +633,10 @@ class WP_Term_Query {
*/
$fields = implode( ', ', apply_filters( 'get_terms_fields', $selects, $args, $taxonomies ) );
$join .= " INNER JOIN {$this->db->term_taxonomy} AS tt ON t.term_id = tt.term_id";
$join .= " INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id";
if ( ! empty( $this->query_vars['object_ids'] ) ) {
$join .= " INNER JOIN {$this->db->term_relationships} AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id";
$join .= " INNER JOIN {$wpdb->term_relationships} AS tr ON tr.term_taxonomy_id = tt.term_taxonomy_id";
}
$where = implode( ' AND ', $this->sql_clauses['where'] );
@ -670,7 +665,7 @@ class WP_Term_Query {
}
$this->sql_clauses['select'] = "SELECT $distinct $fields";
$this->sql_clauses['from'] = "FROM {$this->db->terms} AS t $join";
$this->sql_clauses['from'] = "FROM $wpdb->terms AS t $join";
$this->sql_clauses['orderby'] = $orderby ? "$orderby $order" : '';
$this->sql_clauses['limits'] = $limits;
@ -695,10 +690,10 @@ class WP_Term_Query {
}
if ( 'count' == $_fields ) {
return $this->db->get_var( $this->request );
return $wpdb->get_var( $this->request );
}
$terms = $this->db->get_results( $this->request );
$terms = $wpdb->get_results( $this->request );
if ( 'all' == $_fields || 'all_with_object_id' === $_fields ) {
update_term_cache( $terms );
}
@ -830,6 +825,8 @@ class WP_Term_Query {
* @since 4.6.0
* @access protected
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $orderby_raw Alias for the field to order by.
* @return string|false Value to used in the ORDER clause. False otherwise.
*/
@ -966,12 +963,16 @@ class WP_Term_Query {
* @since 4.6.0
* @access protected
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $string
* @return string
*/
protected function get_search_sql( $string ) {
$like = '%' . $this->db->esc_like( $string ) . '%';
global $wpdb;
return $this->db->prepare( '((t.name LIKE %s) OR (t.slug LIKE %s))', $like, $like );
$like = '%' . $wpdb->esc_like( $string ) . '%';
return $wpdb->prepare( '((t.name LIKE %s) OR (t.slug LIKE %s))', $like, $like );
}
}

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 );

View File

@ -103,13 +103,6 @@ class WP_User {
*/
private static $back_compat_keys;
/**
* @since 4.7.0
* @access protected
* @var wpdb
*/
protected $db;
/**
* Constructor.
*
@ -118,15 +111,15 @@ class WP_User {
* @since 2.0.0
* @access public
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB.
* @param string $name Optional. User's username
* @param int $blog_id Optional Site ID, defaults to current site.
*/
public function __construct( $id = 0, $name = '', $blog_id = '' ) {
$this->db = $GLOBALS['wpdb'];
if ( ! isset( self::$back_compat_keys ) ) {
$prefix = $this->db->prefix;
$prefix = $GLOBALS['wpdb']->prefix;
self::$back_compat_keys = array(
'user_firstname' => 'first_name',
'user_lastname' => 'last_name',
@ -241,10 +234,10 @@ class WP_User {
}
if ( !$user = $wpdb->get_row( $wpdb->prepare(
"SELECT * FROM {$wpdb->users} WHERE $db_field = %s", $value
) ) ) {
"SELECT * FROM $wpdb->users WHERE $db_field = %s", $value
) ) )
return false;
}
update_user_caches( $user );
return $user;
@ -451,14 +444,18 @@ class WP_User {
* @access protected
* @since 2.1.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $cap_key Optional capability key
*/
protected function _init_caps( $cap_key = '' ) {
if ( empty( $cap_key ) ) {
$this->cap_key = $this->db->get_blog_prefix() . 'capabilities';
} else {
global $wpdb;
if ( empty($cap_key) )
$this->cap_key = $wpdb->get_blog_prefix() . 'capabilities';
else
$this->cap_key = $cap_key;
}
$this->caps = get_user_meta( $this->ID, $this->cap_key, true );
if ( ! is_array( $this->caps ) )
@ -636,10 +633,13 @@ class WP_User {
*
* @since 2.0.0
* @access public
*
* @global wpdb $wpdb WordPress database abstraction object.
*/
public function update_user_level_from_caps() {
global $wpdb;
$this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 );
update_user_meta( $this->ID, $this->db->get_blog_prefix() . 'user_level', $this->user_level );
update_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level', $this->user_level );
}
/**
@ -681,11 +681,14 @@ class WP_User {
*
* @since 2.1.0
* @access public
*
* @global wpdb $wpdb WordPress database abstraction object.
*/
public function remove_all_caps() {
global $wpdb;
$this->caps = array();
delete_user_meta( $this->ID, $this->cap_key );
delete_user_meta( $this->ID, $this->db->get_blog_prefix() . 'user_level' );
delete_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level' );
$this->get_role_caps();
}
@ -771,14 +774,16 @@ class WP_User {
*
* @since 3.0.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param int $blog_id Optional. Site ID, defaults to current site.
*/
public function for_blog( $blog_id = '' ) {
if ( ! empty( $blog_id ) ) {
$cap_key = $this->db->get_blog_prefix( $blog_id ) . 'capabilities';
} else {
global $wpdb;
if ( ! empty( $blog_id ) )
$cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
else
$cap_key = '';
}
$this->_init_caps( $cap_key );
}
}

View File

@ -53,13 +53,6 @@ class wp_xmlrpc_server extends IXR_Server {
*/
protected $auth_failed = false;
/**
* @since 4.7.0
* @access protected
* @var wpdb
*/
protected $db;
/**
* Registers all of the XMLRPC methods that XMLRPC server understands.
*
@ -70,8 +63,6 @@ class wp_xmlrpc_server extends IXR_Server {
* @since 1.5.0
*/
public function __construct() {
$this->db = $GLOBALS['wpdb'];
$this->methods = array(
// WordPress API
'wp.getUsersBlogs' => 'this:wp_getUsersBlogs',
@ -2894,6 +2885,8 @@ class wp_xmlrpc_server extends IXR_Server {
*
* @since 2.2.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param array $args {
* Method arguments. Note: arguments must be ordered as documented.
*
@ -2904,6 +2897,8 @@ class wp_xmlrpc_server extends IXR_Server {
* @return array|IXR_Error
*/
public function wp_getPageList( $args ) {
global $wpdb;
$this->escape( $args );
$username = $args[1];
@ -2919,14 +2914,14 @@ class wp_xmlrpc_server extends IXR_Server {
do_action( 'xmlrpc_call', 'wp.getPageList' );
// Get list of pages ids and titles
$page_list = $this->db->get_results("
$page_list = $wpdb->get_results("
SELECT ID page_id,
post_title page_title,
post_parent page_parent_id,
post_date_gmt,
post_date,
post_status
FROM {$this->db->posts}
FROM {$wpdb->posts}
WHERE post_type = 'page'
ORDER BY ID
");
@ -5143,17 +5138,20 @@ class wp_xmlrpc_server extends IXR_Server {
*
* @since 2.1.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param int $post_ID Post ID.
* @param string $post_content Post Content for attachment.
*/
public function attach_uploads( $post_ID, $post_content ) {
global $wpdb;
// find any unattached files
$attachments = $this->db->get_results( "SELECT ID, guid FROM {$this->db->posts} WHERE post_parent = '0' AND post_type = 'attachment'" );
$attachments = $wpdb->get_results( "SELECT ID, guid FROM {$wpdb->posts} WHERE post_parent = '0' AND post_type = 'attachment'" );
if ( is_array( $attachments ) ) {
foreach ( $attachments as $file ) {
if ( ! empty( $file->guid ) && strpos( $post_content, $file->guid ) !== false ) {
$this->db->update( $this->db->posts, array( 'post_parent' => $post_ID ), array( 'ID' => $file->ID ) );
}
if ( ! empty( $file->guid ) && strpos( $post_content, $file->guid ) !== false )
$wpdb->update($wpdb->posts, array('post_parent' => $post_ID), array('ID' => $file->ID) );
}
}
}
@ -5773,6 +5771,8 @@ class wp_xmlrpc_server extends IXR_Server {
*
* @since 1.5.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param array $args {
* Method arguments. Note: arguments must be ordered as documented.
*
@ -5784,6 +5784,8 @@ class wp_xmlrpc_server extends IXR_Server {
* @return array|IXR_Error
*/
public function mw_newMediaObject( $args ) {
global $wpdb;
$username = $this->escape( $args[1] );
$password = $this->escape( $args[2] );
$data = $args[3];
@ -6109,10 +6111,14 @@ class wp_xmlrpc_server extends IXR_Server {
*
* @since 1.5.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param int $post_ID
* @return array|IXR_Error
*/
public function mt_getTrackbackPings( $post_ID ) {
global $wpdb;
/** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
do_action( 'xmlrpc_call', 'mt.getTrackbackPings' );
@ -6121,7 +6127,7 @@ class wp_xmlrpc_server extends IXR_Server {
if ( !$actual_post )
return new IXR_Error(404, __('Sorry, no such post.'));
$comments = $this->db->get_results( $this->db->prepare("SELECT comment_author_url, comment_content, comment_author_IP, comment_type FROM {$this->db->comments} WHERE comment_post_ID = %d", $post_ID) );
$comments = $wpdb->get_results( $wpdb->prepare("SELECT comment_author_url, comment_content, comment_author_IP, comment_type FROM $wpdb->comments WHERE comment_post_ID = %d", $post_ID) );
if ( !$comments )
return array();
@ -6256,8 +6262,8 @@ class wp_xmlrpc_server extends IXR_Server {
} elseif ( is_string($urltest['fragment']) ) {
// ...or a string #title, a little more complicated
$title = preg_replace('/[^a-z0-9]/i', '.', $urltest['fragment']);
$sql = $this->db->prepare("SELECT ID FROM {$this->db->posts} WHERE post_title RLIKE %s", $title );
if (! ($post_ID = $this->db->get_var($sql)) ) {
$sql = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title RLIKE %s", $title );
if (! ($post_ID = $wpdb->get_var($sql)) ) {
// returning unknown error '0' is better than die()ing
return $this->pingback_error( 0, '' );
}
@ -6281,7 +6287,7 @@ class wp_xmlrpc_server extends IXR_Server {
return $this->pingback_error( 33, __( 'The specified target URL cannot be used as a target. It either doesn&#8217;t exist, or it is not a pingback-enabled resource.' ) );
// Let's check that the remote site didn't already pingback this entry
if ( $this->db->get_results( $this->db->prepare("SELECT * FROM {$this->db->comments} WHERE comment_post_ID = %d AND comment_author_url = %s", $post_ID, $pagelinkedfrom) ) )
if ( $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_author_url = %s", $post_ID, $pagelinkedfrom) ) )
return $this->pingback_error( 48, __( 'The pingback has already been registered.' ) );
// very stupid, but gives time to the 'from' server to publish !
@ -6408,10 +6414,14 @@ class wp_xmlrpc_server extends IXR_Server {
*
* @since 1.5.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $url
* @return array|IXR_Error
*/
public function pingback_extensions_getPingbacks( $url ) {
global $wpdb;
/** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
do_action( 'xmlrpc_call', 'pingback.extensions.getPingbacks' );
@ -6430,7 +6440,7 @@ class wp_xmlrpc_server extends IXR_Server {
return $this->pingback_error( 32, __('The specified target URL does not exist.' ) );
}
$comments = $this->db->get_results( $this->db->prepare("SELECT comment_author_url, comment_content, comment_author_IP, comment_type FROM {$this->db->comments} WHERE comment_post_ID = %d", $post_ID) );
$comments = $wpdb->get_results( $wpdb->prepare("SELECT comment_author_url, comment_content, comment_author_IP, comment_type FROM $wpdb->comments WHERE comment_post_ID = %d", $post_ID) );
if ( !$comments )
return array();

View File

@ -62,13 +62,6 @@ class WP_Date_Query {
*/
public $time_keys = array( 'after', 'before', 'year', 'month', 'monthnum', 'week', 'w', 'dayofyear', 'day', 'dayofweek', 'dayofweek_iso', 'hour', 'minute', 'second' );
/**
* @since 4.7.0
* @access protected
* @var wpdb
*/
protected $db;
/**
* Constructor.
*
@ -158,8 +151,6 @@ class WP_Date_Query {
* 'comment_date', 'comment_date_gmt'.
*/
public function __construct( $date_query, $default_column = 'post_date' ) {
$this->db = $GLOBALS['wpdb'];
if ( isset( $date_query['relation'] ) && 'OR' === strtoupper( $date_query['relation'] ) ) {
$this->relation = 'OR';
} else {
@ -494,6 +485,8 @@ class WP_Date_Query {
* @return string A validated column name value.
*/
public function validate_column( $column ) {
global $wpdb;
$valid_columns = array(
'post_date', 'post_date_gmt', 'post_modified',
'post_modified_gmt', 'comment_date', 'comment_date_gmt',
@ -518,20 +511,20 @@ class WP_Date_Query {
}
$known_columns = array(
$this->db->posts => array(
$wpdb->posts => array(
'post_date',
'post_date_gmt',
'post_modified',
'post_modified_gmt',
),
$this->db->comments => array(
$wpdb->comments => array(
'comment_date',
'comment_date_gmt',
),
$this->db->users => array(
$wpdb->users => array(
'user_registered',
),
$this->db->blogs => array(
$wpdb->blogs => array(
'registered',
'last_updated',
),
@ -723,6 +716,8 @@ class WP_Date_Query {
* }
*/
protected function get_sql_for_clause( $query, $parent_query ) {
global $wpdb;
// The sub-parts of a $where part.
$where_parts = array();
@ -745,10 +740,10 @@ class WP_Date_Query {
// Range queries.
if ( ! empty( $query['after'] ) ) {
$where_parts[] = $this->db->prepare( "$column $gt %s", $this->build_mysql_datetime( $query['after'], ! $inclusive ) );
$where_parts[] = $wpdb->prepare( "$column $gt %s", $this->build_mysql_datetime( $query['after'], ! $inclusive ) );
}
if ( ! empty( $query['before'] ) ) {
$where_parts[] = $this->db->prepare( "$column $lt %s", $this->build_mysql_datetime( $query['before'], $inclusive ) );
$where_parts[] = $wpdb->prepare( "$column $lt %s", $this->build_mysql_datetime( $query['before'], $inclusive ) );
}
// Specific value queries.
@ -962,6 +957,8 @@ class WP_Date_Query {
* @return string|false A query part or false on failure.
*/
public function build_time_query( $column, $compare, $hour = null, $minute = null, $second = null ) {
global $wpdb;
// Have to have at least one
if ( ! isset( $hour ) && ! isset( $minute ) && ! isset( $second ) )
return false;
@ -1015,6 +1012,6 @@ class WP_Date_Query {
$time .= sprintf( '%02d', $second );
}
return $this->db->prepare( "DATE_FORMAT( $column, %s ) $compare %f", $format, $time );
return $wpdb->prepare( "DATE_FORMAT( $column, %s ) $compare %f", $format, $time );
}
}

View File

@ -181,9 +181,6 @@ class Tests_User extends WP_UnitTestCase {
$this->assertEquals( 'foo', $user->data->$key ); // This will fail with WP < 3.3
foreach ( (array) $user as $key => $value ) {
if ( $value instanceof wpdb ) {
continue;
}
$this->assertEquals( $value, $user->$key );
}
}