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

@ -141,13 +141,6 @@ class WP_Comment_Query {
return false; return false;
} }
/**
* @since 4.7.0
* @access protected
* @var wpdb
*/
protected $db;
/** /**
* Constructor. * Constructor.
* *
@ -267,8 +260,6 @@ class WP_Comment_Query {
* } * }
*/ */
public function __construct( $query = '' ) { public function __construct( $query = '' ) {
$this->db = $GLOBALS['wpdb'];
$this->query_var_defaults = array( $this->query_var_defaults = array(
'author_email' => '', 'author_email' => '',
'author_url' => '', 'author_url' => '',
@ -372,9 +363,13 @@ class WP_Comment_Query {
* @since 4.2.0 * @since 4.2.0
* @access public * @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. * @return int|array List of comments or number of found comments if `$count` argument is true.
*/ */
public function get_comments() { public function get_comments() {
global $wpdb;
$this->parse_query(); $this->parse_query();
// Parse meta 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. // Reparse query vars, in case they were modified in a 'pre_get_comments' callback.
$this->meta_query->parse_query_vars( $this->query_vars ); $this->meta_query->parse_query_vars( $this->query_vars );
if ( ! empty( $this->meta_query->queries ) ) { 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. // $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 * @since 4.4.0
* @access protected * @access protected
*
* @global wpdb $wpdb WordPress database abstraction object.
*/ */
protected function get_comment_ids() { protected function get_comment_ids() {
global $wpdb;
// Assemble clauses related to 'comment_approved'. // Assemble clauses related to 'comment_approved'.
$approved_clauses = array(); $approved_clauses = array();
@ -515,7 +514,7 @@ class WP_Comment_Query {
break; break;
default : default :
$status_clauses[] = $this->db->prepare( "comment_approved = %s", $status ); $status_clauses[] = $wpdb->prepare( "comment_approved = %s", $status );
break; break;
} }
} }
@ -538,11 +537,11 @@ class WP_Comment_Query {
foreach ( $include_unapproved as $unapproved_identifier ) { foreach ( $include_unapproved as $unapproved_identifier ) {
// Numeric values are assumed to be user ids. // Numeric values are assumed to be user ids.
if ( is_numeric( $unapproved_identifier ) ) { 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. // Otherwise we match against email addresses.
} else { } 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 no valid clauses were found, order by comment_date_gmt.
if ( empty( $orderby_array ) ) { 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. // To ensure determinate sorting, always include a comment_ID clause.
@ -634,12 +633,12 @@ class WP_Comment_Query {
$comment_ID_order = 'DESC'; $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 ); $orderby = implode( ', ', $orderby_array );
} else { } else {
$orderby = "{$this->db->comments}.comment_date_gmt $order"; $orderby = "$wpdb->comments.comment_date_gmt $order";
} }
$number = absint( $this->query_vars['number'] ); $number = absint( $this->query_vars['number'] );
@ -656,22 +655,22 @@ class WP_Comment_Query {
if ( $this->query_vars['count'] ) { if ( $this->query_vars['count'] ) {
$fields = 'COUNT(*)'; $fields = 'COUNT(*)';
} else { } else {
$fields = "{$this->db->comments}.comment_ID"; $fields = "$wpdb->comments.comment_ID";
} }
$post_id = absint( $this->query_vars['post_id'] ); $post_id = absint( $this->query_vars['post_id'] );
if ( ! empty( $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. // Parse comment IDs for an IN clause.
if ( ! empty( $this->query_vars['comment__in'] ) ) { 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. // Parse comment IDs for a NOT IN clause.
if ( ! empty( $this->query_vars['comment__not_in'] ) ) { 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. // Parse comment parent IDs for an IN clause.
@ -695,15 +694,15 @@ class WP_Comment_Query {
} }
if ( '' !== $this->query_vars['author_email'] ) { 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'] ) { 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'] ) { 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'. // Filtering by comment_type: 'type', 'type__in', 'type__not_in'.
@ -734,7 +733,7 @@ class WP_Comment_Query {
break; break;
default: default:
$comment_types[ $operator ][] = $this->db->prepare( '%s', $type ); $comment_types[ $operator ][] = $wpdb->prepare( '%s', $type );
break; break;
} }
} }
@ -751,13 +750,13 @@ class WP_Comment_Query {
} }
if ( '' !== $parent ) { 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'] ) ) { 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'] ) ) . ')'; $this->sql_clauses['where']['user_id'] = 'user_id IN (' . implode( ',', array_map( 'absint', $this->query_vars['user_id'] ) ) . ')';
} elseif ( '' !== $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. // Falsy search strings are ignored.
@ -781,7 +780,7 @@ class WP_Comment_Query {
foreach ( $post_fields as $field_name => $field_value ) { foreach ( $post_fields as $field_name => $field_value ) {
// $field_value may be an array. // $field_value may be an array.
$esses = array_fill( 0, count( (array) $field_value ), '%s' ); $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; $join_posts_table = true;
$esses = array_fill( 0, count( $q_values ), '%s' ); $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 = ''; $join = '';
if ( $join_posts_table ) { 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 ) ) { 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'] ); $this->sql_clauses['where']['meta_query'] = preg_replace( '/^\s*AND\s*/', '', $this->meta_query_clauses['where'] );
if ( ! $this->query_vars['count'] ) { 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['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['groupby'] = $groupby;
$this->sql_clauses['orderby'] = $orderby; $this->sql_clauses['orderby'] = $orderby;
$this->sql_clauses['limits'] = $limits; $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']}"; $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'] ) { if ( $this->query_vars['count'] ) {
return intval( $this->db->get_var( $this->request ) ); return intval( $wpdb->get_var( $this->request ) );
} else { } else {
$comment_ids = $this->db->get_col( $this->request ); $comment_ids = $wpdb->get_col( $this->request );
return array_map( 'intval', $comment_ids ); return array_map( 'intval', $comment_ids );
} }
} }
@ -911,8 +910,12 @@ class WP_Comment_Query {
* *
* @since 4.6.0 * @since 4.6.0
* @access private * @access private
*
* @global wpdb $wpdb WordPress database abstraction object.
*/ */
private function set_found_comments() { private function set_found_comments() {
global $wpdb;
if ( $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) { if ( $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
/** /**
* Filters the query used to retrieve found comment count. * 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 ); $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 * @return array
*/ */
protected function fill_descendants( $comments ) { protected function fill_descendants( $comments ) {
global $wpdb;
$levels = array( $levels = array(
0 => wp_list_pluck( $comments, 'comment_ID' ), 0 => wp_list_pluck( $comments, 'comment_ID' ),
); );
@ -1070,16 +1075,20 @@ class WP_Comment_Query {
* @since 3.1.0 * @since 3.1.0
* @access protected * @access protected
* *
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $string * @param string $string
* @param array $cols * @param array $cols
* @return string * @return string
*/ */
protected function get_search_sql( $string, $cols ) { protected function get_search_sql( $string, $cols ) {
$like = '%' . $this->db->esc_like( $string ) . '%'; global $wpdb;
$like = '%' . $wpdb->esc_like( $string ) . '%';
$searches = array(); $searches = array();
foreach ( $cols as $col ) { foreach ( $cols as $col ) {
$searches[] = $this->db->prepare( "$col LIKE %s", $like ); $searches[] = $wpdb->prepare( "$col LIKE %s", $like );
} }
return ' AND (' . implode(' OR ', $searches) . ')'; return ' AND (' . implode(' OR ', $searches) . ')';
@ -1091,10 +1100,14 @@ class WP_Comment_Query {
* @since 4.2.0 * @since 4.2.0
* @access protected * @access protected
* *
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $orderby Alias for the field to order by. * @param string $orderby Alias for the field to order by.
* @return string|false Value to used in the ORDER clause. False otherwise. * @return string|false Value to used in the ORDER clause. False otherwise.
*/ */
protected function parse_orderby( $orderby ) { protected function parse_orderby( $orderby ) {
global $wpdb;
$allowed_keys = array( $allowed_keys = array(
'comment_agent', 'comment_agent',
'comment_approved', 'comment_approved',
@ -1126,19 +1139,19 @@ class WP_Comment_Query {
$parsed = false; $parsed = false;
if ( $orderby == $this->query_vars['meta_key'] || $orderby == 'meta_value' ) { 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' ) { } elseif ( $orderby == 'meta_value_num' ) {
$parsed = "{$this->db->commentmeta}.meta_value+0"; $parsed = "$wpdb->commentmeta.meta_value+0";
} elseif ( $orderby == 'comment__in' ) { } elseif ( $orderby == 'comment__in' ) {
$comment__in = implode( ',', array_map( 'absint', $this->query_vars['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 ) ) { } elseif ( in_array( $orderby, $allowed_keys ) ) {
if ( isset( $meta_query_clauses[ $orderby ] ) ) { if ( isset( $meta_query_clauses[ $orderby ] ) ) {
$meta_clause = $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'] ) ); $parsed = sprintf( "CAST(%s.meta_value AS %s)", esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) );
} else { } 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; protected $has_or_relation = false;
/**
* @since 4.7.0
* @access protected
* @var wpdb
*/
protected $db;
/** /**
* Constructor. * Constructor.
* *
@ -144,11 +137,8 @@ class WP_Meta_Query {
* } * }
*/ */
public function __construct( $meta_query = false ) { public function __construct( $meta_query = false ) {
$this->db = $GLOBALS['wpdb']; if ( !$meta_query )
if ( ! $meta_query ) {
return; return;
}
if ( isset( $meta_query['relation'] ) && strtoupper( $meta_query['relation'] ) == 'OR' ) { if ( isset( $meta_query['relation'] ) && strtoupper( $meta_query['relation'] ) == 'OR' ) {
$this->relation = 'OR'; $this->relation = 'OR';
@ -494,6 +484,8 @@ class WP_Meta_Query {
* @since 4.1.0 * @since 4.1.0
* @access public * @access public
* *
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param array $clause Query clause, passed by reference. * @param array $clause Query clause, passed by reference.
* @param array $parent_query Parent query array. * @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` * @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 = '' ) { public function get_sql_for_clause( &$clause, $parent_query, $clause_key = '' ) {
global $wpdb;
$sql_chunks = array( $sql_chunks = array(
'where' => array(), 'where' => array(),
'join' => array(), 'join' => array(),
@ -543,7 +537,7 @@ class WP_Meta_Query {
if ( 'NOT EXISTS' === $meta_compare ) { if ( 'NOT EXISTS' === $meta_compare ) {
$join .= " LEFT JOIN $this->meta_table"; $join .= " LEFT JOIN $this->meta_table";
$join .= $i ? " AS $alias" : ''; $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. // All other JOIN clauses.
} else { } else {
@ -587,7 +581,7 @@ class WP_Meta_Query {
if ( 'NOT EXISTS' === $meta_compare ) { if ( 'NOT EXISTS' === $meta_compare ) {
$sql_chunks['where'][] = $alias . '.' . $this->meta_id_column . ' IS NULL'; $sql_chunks['where'][] = $alias . '.' . $this->meta_id_column . ' IS NULL';
} else { } 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 'IN' :
case 'NOT IN' : case 'NOT IN' :
$meta_compare_string = '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')'; $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; break;
case 'BETWEEN' : case 'BETWEEN' :
case 'NOT BETWEEN' : case 'NOT BETWEEN' :
$meta_value = array_slice( $meta_value, 0, 2 ); $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; break;
case 'LIKE' : case 'LIKE' :
case 'NOT LIKE' : case 'NOT LIKE' :
$meta_value = '%' . $this->db->esc_like( $meta_value ) . '%'; $meta_value = '%' . $wpdb->esc_like( $meta_value ) . '%';
$where = $this->db->prepare( '%s', $meta_value ); $where = $wpdb->prepare( '%s', $meta_value );
break; break;
// EXISTS with a value is interpreted as '='. // EXISTS with a value is interpreted as '='.
case 'EXISTS' : case 'EXISTS' :
$meta_compare = '='; $meta_compare = '=';
$where = $this->db->prepare( '%s', $meta_value ); $where = $wpdb->prepare( '%s', $meta_value );
break; break;
// 'value' is ignored for NOT EXISTS. // 'value' is ignored for NOT EXISTS.
@ -634,7 +628,7 @@ class WP_Meta_Query {
break; break;
default : default :
$where = $this->db->prepare( '%s', $meta_value ); $where = $wpdb->prepare( '%s', $meta_value );
break; break;
} }

View File

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

View File

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

View File

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

View File

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

View File

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

View File

@ -86,13 +86,6 @@ class WP_Term_Query {
*/ */
public $terms; public $terms;
/**
* @since 4.7.0
* @access protected
* @var wpdb
*/
protected $db;
/** /**
* Constructor. * Constructor.
* *
@ -186,8 +179,6 @@ class WP_Term_Query {
* } * }
*/ */
public function __construct( $query = '' ) { public function __construct( $query = '' ) {
$this->db = $GLOBALS['wpdb'];
$this->query_var_defaults = array( $this->query_var_defaults = array(
'taxonomy' => null, 'taxonomy' => null,
'object_ids' => null, 'object_ids' => null,
@ -314,9 +305,13 @@ class WP_Term_Query {
* @param 4.6.0 * @param 4.6.0
* @access public * @access public
* *
* @global wpdb $wpdb WordPress database abstraction object.
*
* @return array * @return array
*/ */
public function get_terms() { public function get_terms() {
global $wpdb;
$this->parse_query( $this->query_vars ); $this->parse_query( $this->query_vars );
$args = $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'] ) ); $tt_ids = implode( ',', array_map( 'intval', $args['term_taxonomy_id'] ) );
$this->sql_clauses['where']['term_taxonomy_id'] = "tt.term_taxonomy_id IN ({$tt_ids})"; $this->sql_clauses['where']['term_taxonomy_id'] = "tt.term_taxonomy_id IN ({$tt_ids})";
} else { } 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'] ) ) { 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'] ) ) { 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'] ) ) { if ( ! empty( $args['object_ids'] ) ) {
@ -638,10 +633,10 @@ class WP_Term_Query {
*/ */
$fields = implode( ', ', apply_filters( 'get_terms_fields', $selects, $args, $taxonomies ) ); $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'] ) ) { 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'] ); $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['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['orderby'] = $orderby ? "$orderby $order" : '';
$this->sql_clauses['limits'] = $limits; $this->sql_clauses['limits'] = $limits;
@ -695,10 +690,10 @@ class WP_Term_Query {
} }
if ( 'count' == $_fields ) { 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 ) { if ( 'all' == $_fields || 'all_with_object_id' === $_fields ) {
update_term_cache( $terms ); update_term_cache( $terms );
} }
@ -830,6 +825,8 @@ class WP_Term_Query {
* @since 4.6.0 * @since 4.6.0
* @access protected * @access protected
* *
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $orderby_raw Alias for the field to order by. * @param string $orderby_raw Alias for the field to order by.
* @return string|false Value to used in the ORDER clause. False otherwise. * @return string|false Value to used in the ORDER clause. False otherwise.
*/ */
@ -966,12 +963,16 @@ class WP_Term_Query {
* @since 4.6.0 * @since 4.6.0
* @access protected * @access protected
* *
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $string * @param string $string
* @return string * @return string
*/ */
protected function get_search_sql( $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_orderby;
public $query_limit; public $query_limit;
/**
* @since 4.7.0
* @access protected
* @var wpdb
*/
protected $db;
/** /**
* PHP5 constructor. * PHP5 constructor.
* *
@ -85,8 +78,6 @@ class WP_User_Query {
* @param null|string|array $query Optional. The query variables. * @param null|string|array $query Optional. The query variables.
*/ */
public function __construct( $query = null ) { public function __construct( $query = null ) {
$this->db = $GLOBALS['wpdb'];
if ( ! empty( $query ) ) { if ( ! empty( $query ) ) {
$this->prepare_query( $query ); $this->prepare_query( $query );
$this->query(); $this->query();
@ -151,6 +142,7 @@ class WP_User_Query {
* *
* @access public * @access public
* *
* @global wpdb $wpdb WordPress database abstraction object.
* @global int $blog_id * @global int $blog_id
* *
* @param string|array $query { * @param string|array $query {
@ -224,6 +216,8 @@ class WP_User_Query {
* } * }
*/ */
public function prepare_query( $query = array() ) { public function prepare_query( $query = array() ) {
global $wpdb;
if ( empty( $this->query_vars ) || ! empty( $query ) ) { if ( empty( $this->query_vars ) || ! empty( $query ) ) {
$this->query_limit = null; $this->query_limit = null;
$this->query_vars = $this->fill_query_vars( $query ); $this->query_vars = $this->fill_query_vars( $query );
@ -252,19 +246,19 @@ class WP_User_Query {
$this->query_fields = array(); $this->query_fields = array();
foreach ( $qv['fields'] as $field ) { foreach ( $qv['fields'] as $field ) {
$field = 'ID' === $field ? 'ID' : sanitize_key( $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 ); $this->query_fields = implode( ',', $this->query_fields );
} elseif ( 'all' == $qv['fields'] ) { } elseif ( 'all' == $qv['fields'] ) {
$this->query_fields = "{$this->db->users}.*"; $this->query_fields = "$wpdb->users.*";
} else { } else {
$this->query_fields = "{$this->db->users}.ID"; $this->query_fields = "$wpdb->users.ID";
} }
if ( isset( $qv['count_total'] ) && $qv['count_total'] ) if ( isset( $qv['count_total'] ) && $qv['count_total'] )
$this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields; $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"; $this->query_where = "WHERE 1=1";
// Parse and sanitize 'include', for use by 'orderby' as well as 'include' below. // 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 ) { 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'; $posts_table = $wpdb->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 ) . " ) )"; $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 // nicename
if ( '' !== $qv['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'] ) ) { if ( ! empty( $qv['nicename__in'] ) ) {
@ -313,7 +307,7 @@ class WP_User_Query {
// login // login
if ( '' !== $qv['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'] ) ) { if ( ! empty( $qv['login__in'] ) ) {
@ -334,7 +328,7 @@ class WP_User_Query {
if ( isset( $qv['who'] ) && 'authors' == $qv['who'] && $blog_id ) { if ( isset( $qv['who'] ) && 'authors' == $qv['who'] && $blog_id ) {
$who_query = array( $who_query = array(
'key' => $this->db->get_blog_prefix( $blog_id ) . 'user_level', 'key' => $wpdb->get_blog_prefix( $blog_id ) . 'user_level',
'value' => 0, 'value' => 0,
'compare' => '!=', 'compare' => '!=',
); );
@ -381,7 +375,7 @@ class WP_User_Query {
if ( ! empty( $roles ) ) { if ( ! empty( $roles ) ) {
foreach ( $roles as $role ) { foreach ( $roles as $role ) {
$roles_clauses[] = array( $roles_clauses[] = array(
'key' => $this->db->get_blog_prefix( $blog_id ) . 'capabilities', 'key' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
'value' => '"' . $role . '"', 'value' => '"' . $role . '"',
'compare' => 'LIKE', 'compare' => 'LIKE',
); );
@ -394,7 +388,7 @@ class WP_User_Query {
if ( ! empty( $role__in ) ) { if ( ! empty( $role__in ) ) {
foreach ( $role__in as $role ) { foreach ( $role__in as $role ) {
$role__in_clauses[] = array( $role__in_clauses[] = array(
'key' => $this->db->get_blog_prefix( $blog_id ) . 'capabilities', 'key' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
'value' => '"' . $role . '"', 'value' => '"' . $role . '"',
'compare' => 'LIKE', 'compare' => 'LIKE',
); );
@ -407,7 +401,7 @@ class WP_User_Query {
if ( ! empty( $role__not_in ) ) { if ( ! empty( $role__not_in ) ) {
foreach ( $role__not_in as $role ) { foreach ( $role__not_in as $role ) {
$role__not_in_clauses[] = array( $role__not_in_clauses[] = array(
'key' => $this->db->get_blog_prefix( $blog_id ) . 'capabilities', 'key' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
'value' => '"' . $role . '"', 'value' => '"' . $role . '"',
'compare' => 'NOT LIKE', '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 there are no specific roles named, make sure the user is a member of the site.
if ( empty( $role_queries ) ) { if ( empty( $role_queries ) ) {
$role_queries[] = array( $role_queries[] = array(
'key' => $this->db->get_blog_prefix( $blog_id ) . 'capabilities', 'key' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
'compare' => 'EXISTS', 'compare' => 'EXISTS',
); );
} }
@ -441,7 +435,7 @@ class WP_User_Query {
} }
if ( ! empty( $this->meta_query->queries ) ) { 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_from .= $clauses['join'];
$this->query_where .= $clauses['where']; $this->query_where .= $clauses['where'];
@ -503,9 +497,9 @@ class WP_User_Query {
// limit // limit
if ( isset( $qv['number'] ) && $qv['number'] > 0 ) { if ( isset( $qv['number'] ) && $qv['number'] > 0 ) {
if ( $qv['offset'] ) { 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 { } 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 ) ) { if ( ! empty( $include ) ) {
// Sanitized earlier. // Sanitized earlier.
$ids = implode( ',', $include ); $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'] ) ) { } elseif ( ! empty( $qv['exclude'] ) ) {
$ids = implode( ',', wp_parse_id_list( $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. // Date queries are allowed for the user_registered field.
@ -592,16 +586,20 @@ class WP_User_Query {
* Execute the query, with the current variables. * Execute the query, with the current variables.
* *
* @since 3.1.0 * @since 3.1.0
*
* @global wpdb $wpdb WordPress database abstraction object.
*/ */
public function query() { public function query() {
global $wpdb;
$qv =& $this->query_vars; $qv =& $this->query_vars;
$this->request = "SELECT $this->query_fields $this->query_from $this->query_where $this->query_orderby $this->query_limit"; $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'] ) { if ( is_array( $qv['fields'] ) || 'all' == $qv['fields'] ) {
$this->results = $this->db->get_results( $this->request ); $this->results = $wpdb->get_results( $this->request );
} else { } 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 * @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. * @param string $sql The SELECT FOUND_ROWS() query for the current WP_User_Query.
*/ */
if ( isset( $qv['count_total'] ) && $qv['count_total'] ) { if ( isset( $qv['count_total'] ) && $qv['count_total'] )
$this->total_users = $this->db->get_var( apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()' ) ); $this->total_users = $wpdb->get_var( apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()' ) );
}
if ( ! $this->results ) { if ( !$this->results )
return; return;
}
if ( 'all_with_meta' == $qv['fields'] ) { if ( 'all_with_meta' == $qv['fields'] ) {
cache_users( $this->results ); cache_users( $this->results );
@ -669,6 +667,8 @@ class WP_User_Query {
* @access protected * @access protected
* @since 3.1.0 * @since 3.1.0
* *
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $string * @param string $string
* @param array $cols * @param array $cols
* @param bool $wild Whether to allow wildcard searches. Default is false for Network Admin, true for single site. * @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 * @return string
*/ */
protected function get_search_sql( $string, $cols, $wild = false ) { protected function get_search_sql( $string, $cols, $wild = false ) {
global $wpdb;
$searches = array(); $searches = array();
$leading_wild = ( 'leading' == $wild || 'both' == $wild ) ? '%' : ''; $leading_wild = ( 'leading' == $wild || 'both' == $wild ) ? '%' : '';
$trailing_wild = ( 'trailing' == $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 ) { foreach ( $cols as $col ) {
if ( 'ID' == $col ) { if ( 'ID' == $col ) {
$searches[] = $this->db->prepare( "$col = %s", $string ); $searches[] = $wpdb->prepare( "$col = %s", $string );
} else { } 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 * @since 4.2.0
* @access protected * @access protected
* *
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $orderby Alias for the field to order by. * @param string $orderby Alias for the field to order by.
* @return string Value to used in the ORDER clause, if `$orderby` is valid. * @return string Value to used in the ORDER clause, if `$orderby` is valid.
*/ */
protected function parse_orderby( $orderby ) { protected function parse_orderby( $orderby ) {
global $wpdb;
$meta_query_clauses = $this->meta_query->get_clauses(); $meta_query_clauses = $this->meta_query->get_clauses();
$_orderby = ''; $_orderby = '';
@ -740,22 +746,22 @@ class WP_User_Query {
$where = get_posts_by_author_sql( 'post' ); $where = get_posts_by_author_sql( 'post' );
$this->query_from .= " LEFT OUTER JOIN ( $this->query_from .= " LEFT OUTER JOIN (
SELECT post_author, COUNT(*) as post_count SELECT post_author, COUNT(*) as post_count
FROM {$this->db->posts} FROM $wpdb->posts
$where $where
GROUP BY post_author GROUP BY post_author
) p ON ({$this->db->users}.ID = p.post_author) ) p ON ({$wpdb->users}.ID = p.post_author)
"; ";
$_orderby = 'post_count'; $_orderby = 'post_count';
} elseif ( 'ID' == $orderby || 'id' == $orderby ) { } elseif ( 'ID' == $orderby || 'id' == $orderby ) {
$_orderby = 'ID'; $_orderby = 'ID';
} elseif ( 'meta_value' == $orderby || $this->get( 'meta_key' ) == $orderby ) { } 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 ) { } 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'] ) ) { } elseif ( 'include' === $orderby && ! empty( $this->query_vars['include'] ) ) {
$include = wp_parse_id_list( $this->query_vars['include'] ); $include = wp_parse_id_list( $this->query_vars['include'] );
$include_sql = implode( ',', $include ); $include_sql = implode( ',', $include );
$_orderby = "FIELD( {$this->db->users}.ID, $include_sql )"; $_orderby = "FIELD( $wpdb->users.ID, $include_sql )";
} elseif ( 'nicename__in' === $orderby ) { } elseif ( 'nicename__in' === $orderby ) {
$sanitized_nicename__in = array_map( 'esc_sql', $this->query_vars['nicename__in'] ); $sanitized_nicename__in = array_map( 'esc_sql', $this->query_vars['nicename__in'] );
$nicename__in = implode( "','", $sanitized_nicename__in ); $nicename__in = implode( "','", $sanitized_nicename__in );

View File

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

View File

@ -53,13 +53,6 @@ class wp_xmlrpc_server extends IXR_Server {
*/ */
protected $auth_failed = false; protected $auth_failed = false;
/**
* @since 4.7.0
* @access protected
* @var wpdb
*/
protected $db;
/** /**
* Registers all of the XMLRPC methods that XMLRPC server understands. * 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 * @since 1.5.0
*/ */
public function __construct() { public function __construct() {
$this->db = $GLOBALS['wpdb'];
$this->methods = array( $this->methods = array(
// WordPress API // WordPress API
'wp.getUsersBlogs' => 'this:wp_getUsersBlogs', 'wp.getUsersBlogs' => 'this:wp_getUsersBlogs',
@ -2894,6 +2885,8 @@ class wp_xmlrpc_server extends IXR_Server {
* *
* @since 2.2.0 * @since 2.2.0
* *
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param array $args { * @param array $args {
* Method arguments. Note: arguments must be ordered as documented. * Method arguments. Note: arguments must be ordered as documented.
* *
@ -2904,6 +2897,8 @@ class wp_xmlrpc_server extends IXR_Server {
* @return array|IXR_Error * @return array|IXR_Error
*/ */
public function wp_getPageList( $args ) { public function wp_getPageList( $args ) {
global $wpdb;
$this->escape( $args ); $this->escape( $args );
$username = $args[1]; $username = $args[1];
@ -2919,14 +2914,14 @@ class wp_xmlrpc_server extends IXR_Server {
do_action( 'xmlrpc_call', 'wp.getPageList' ); do_action( 'xmlrpc_call', 'wp.getPageList' );
// Get list of pages ids and titles // Get list of pages ids and titles
$page_list = $this->db->get_results(" $page_list = $wpdb->get_results("
SELECT ID page_id, SELECT ID page_id,
post_title page_title, post_title page_title,
post_parent page_parent_id, post_parent page_parent_id,
post_date_gmt, post_date_gmt,
post_date, post_date,
post_status post_status
FROM {$this->db->posts} FROM {$wpdb->posts}
WHERE post_type = 'page' WHERE post_type = 'page'
ORDER BY ID ORDER BY ID
"); ");
@ -5143,17 +5138,20 @@ class wp_xmlrpc_server extends IXR_Server {
* *
* @since 2.1.0 * @since 2.1.0
* *
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param int $post_ID Post ID. * @param int $post_ID Post ID.
* @param string $post_content Post Content for attachment. * @param string $post_content Post Content for attachment.
*/ */
public function attach_uploads( $post_ID, $post_content ) { public function attach_uploads( $post_ID, $post_content ) {
global $wpdb;
// find any unattached files // 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 ) ) { if ( is_array( $attachments ) ) {
foreach ( $attachments as $file ) { foreach ( $attachments as $file ) {
if ( ! empty( $file->guid ) && strpos( $post_content, $file->guid ) !== false ) { 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 ) ); $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 * @since 1.5.0
* *
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param array $args { * @param array $args {
* Method arguments. Note: arguments must be ordered as documented. * Method arguments. Note: arguments must be ordered as documented.
* *
@ -5784,6 +5784,8 @@ class wp_xmlrpc_server extends IXR_Server {
* @return array|IXR_Error * @return array|IXR_Error
*/ */
public function mw_newMediaObject( $args ) { public function mw_newMediaObject( $args ) {
global $wpdb;
$username = $this->escape( $args[1] ); $username = $this->escape( $args[1] );
$password = $this->escape( $args[2] ); $password = $this->escape( $args[2] );
$data = $args[3]; $data = $args[3];
@ -6109,10 +6111,14 @@ class wp_xmlrpc_server extends IXR_Server {
* *
* @since 1.5.0 * @since 1.5.0
* *
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param int $post_ID * @param int $post_ID
* @return array|IXR_Error * @return array|IXR_Error
*/ */
public function mt_getTrackbackPings( $post_ID ) { public function mt_getTrackbackPings( $post_ID ) {
global $wpdb;
/** This action is documented in wp-includes/class-wp-xmlrpc-server.php */ /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
do_action( 'xmlrpc_call', 'mt.getTrackbackPings' ); do_action( 'xmlrpc_call', 'mt.getTrackbackPings' );
@ -6121,7 +6127,7 @@ class wp_xmlrpc_server extends IXR_Server {
if ( !$actual_post ) if ( !$actual_post )
return new IXR_Error(404, __('Sorry, no such 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 ) if ( !$comments )
return array(); return array();
@ -6256,8 +6262,8 @@ class wp_xmlrpc_server extends IXR_Server {
} elseif ( is_string($urltest['fragment']) ) { } elseif ( is_string($urltest['fragment']) ) {
// ...or a string #title, a little more complicated // ...or a string #title, a little more complicated
$title = preg_replace('/[^a-z0-9]/i', '.', $urltest['fragment']); $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 ); $sql = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title RLIKE %s", $title );
if (! ($post_ID = $this->db->get_var($sql)) ) { if (! ($post_ID = $wpdb->get_var($sql)) ) {
// returning unknown error '0' is better than die()ing // returning unknown error '0' is better than die()ing
return $this->pingback_error( 0, '' ); 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.' ) ); 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 // 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.' ) ); return $this->pingback_error( 48, __( 'The pingback has already been registered.' ) );
// very stupid, but gives time to the 'from' server to publish ! // 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 * @since 1.5.0
* *
* @global wpdb $wpdb WordPress database abstraction object.
*
* @param string $url * @param string $url
* @return array|IXR_Error * @return array|IXR_Error
*/ */
public function pingback_extensions_getPingbacks( $url ) { public function pingback_extensions_getPingbacks( $url ) {
global $wpdb;
/** This action is documented in wp-includes/class-wp-xmlrpc-server.php */ /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
do_action( 'xmlrpc_call', 'pingback.extensions.getPingbacks' ); 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.' ) ); 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 ) if ( !$comments )
return array(); 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' ); 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. * Constructor.
* *
@ -158,8 +151,6 @@ class WP_Date_Query {
* 'comment_date', 'comment_date_gmt'. * 'comment_date', 'comment_date_gmt'.
*/ */
public function __construct( $date_query, $default_column = 'post_date' ) { public function __construct( $date_query, $default_column = 'post_date' ) {
$this->db = $GLOBALS['wpdb'];
if ( isset( $date_query['relation'] ) && 'OR' === strtoupper( $date_query['relation'] ) ) { if ( isset( $date_query['relation'] ) && 'OR' === strtoupper( $date_query['relation'] ) ) {
$this->relation = 'OR'; $this->relation = 'OR';
} else { } else {
@ -494,6 +485,8 @@ class WP_Date_Query {
* @return string A validated column name value. * @return string A validated column name value.
*/ */
public function validate_column( $column ) { public function validate_column( $column ) {
global $wpdb;
$valid_columns = array( $valid_columns = array(
'post_date', 'post_date_gmt', 'post_modified', 'post_date', 'post_date_gmt', 'post_modified',
'post_modified_gmt', 'comment_date', 'comment_date_gmt', 'post_modified_gmt', 'comment_date', 'comment_date_gmt',
@ -518,20 +511,20 @@ class WP_Date_Query {
} }
$known_columns = array( $known_columns = array(
$this->db->posts => array( $wpdb->posts => array(
'post_date', 'post_date',
'post_date_gmt', 'post_date_gmt',
'post_modified', 'post_modified',
'post_modified_gmt', 'post_modified_gmt',
), ),
$this->db->comments => array( $wpdb->comments => array(
'comment_date', 'comment_date',
'comment_date_gmt', 'comment_date_gmt',
), ),
$this->db->users => array( $wpdb->users => array(
'user_registered', 'user_registered',
), ),
$this->db->blogs => array( $wpdb->blogs => array(
'registered', 'registered',
'last_updated', 'last_updated',
), ),
@ -723,6 +716,8 @@ class WP_Date_Query {
* } * }
*/ */
protected function get_sql_for_clause( $query, $parent_query ) { protected function get_sql_for_clause( $query, $parent_query ) {
global $wpdb;
// The sub-parts of a $where part. // The sub-parts of a $where part.
$where_parts = array(); $where_parts = array();
@ -745,10 +740,10 @@ class WP_Date_Query {
// Range queries. // Range queries.
if ( ! empty( $query['after'] ) ) { 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'] ) ) { 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. // Specific value queries.
@ -962,6 +957,8 @@ class WP_Date_Query {
* @return string|false A query part or false on failure. * @return string|false A query part or false on failure.
*/ */
public function build_time_query( $column, $compare, $hour = null, $minute = null, $second = null ) { public function build_time_query( $column, $compare, $hour = null, $minute = null, $second = null ) {
global $wpdb;
// Have to have at least one // Have to have at least one
if ( ! isset( $hour ) && ! isset( $minute ) && ! isset( $second ) ) if ( ! isset( $hour ) && ! isset( $minute ) && ! isset( $second ) )
return false; return false;
@ -1015,6 +1012,6 @@ class WP_Date_Query {
$time .= sprintf( '%02d', $second ); $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

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.7-alpha-38767'; $wp_version = '4.7-alpha-38768';
/** /**
* Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema. * Holds the WordPress DB revision, increments when changes are made to the WordPress DB schema.