Query: add a `protected` field, `$db`, (composition, as it were) to `WP_*_Query` classes to hold the value for the database abstraction, instead of importing the `global $wpdb` into every method that uses it. Reduces the number of global imports by 32.

See #37699.

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


git-svn-id: http://core.svn.wordpress.org/trunk@38216 1a063a9b-81f0-0310-95a4-ce76da25c4cd
This commit is contained in:
Scott Taylor 2016-08-18 18:21:31 +00:00
parent 1dd3ed4bd5
commit e7ffad7fac
10 changed files with 252 additions and 293 deletions

View File

@ -141,6 +141,13 @@ class WP_Comment_Query {
return false; return false;
} }
/**
* @since 4.7.0
* @access protected
* @var wpdb
*/
protected $db;
/** /**
* Constructor. * Constructor.
* *
@ -260,6 +267,8 @@ 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' => '',
@ -363,13 +372,9 @@ 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
@ -388,7 +393,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', $wpdb->comments, 'comment_ID', $this ); $this->meta_query_clauses = $this->meta_query->get_sql( 'comment', $this->db->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.
@ -480,12 +485,8 @@ 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();
@ -514,7 +515,7 @@ class WP_Comment_Query {
break; break;
default : default :
$status_clauses[] = $wpdb->prepare( "comment_approved = %s", $status ); $status_clauses[] = $this->db->prepare( "comment_approved = %s", $status );
break; break;
} }
} }
@ -537,11 +538,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[] = $wpdb->prepare( "( user_id = %d AND comment_approved = '0' )", $unapproved_identifier ); $approved_clauses[] = $this->db->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[] = $wpdb->prepare( "( comment_author_email = %s AND comment_approved = '0' )", $unapproved_identifier ); $approved_clauses[] = $this->db->prepare( "( comment_author_email = %s AND comment_approved = '0' )", $unapproved_identifier );
} }
} }
} }
@ -600,7 +601,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[] = "$wpdb->comments.comment_date_gmt $order"; $orderby_array[] = "{$this->db->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.
@ -633,12 +634,12 @@ class WP_Comment_Query {
$comment_ID_order = 'DESC'; $comment_ID_order = 'DESC';
} }
$orderby_array[] = "$wpdb->comments.comment_ID $comment_ID_order"; $orderby_array[] = "{$this->db->comments}.comment_ID $comment_ID_order";
} }
$orderby = implode( ', ', $orderby_array ); $orderby = implode( ', ', $orderby_array );
} else { } else {
$orderby = "$wpdb->comments.comment_date_gmt $order"; $orderby = "{$this->db->comments}.comment_date_gmt $order";
} }
$number = absint( $this->query_vars['number'] ); $number = absint( $this->query_vars['number'] );
@ -655,22 +656,22 @@ class WP_Comment_Query {
if ( $this->query_vars['count'] ) { if ( $this->query_vars['count'] ) {
$fields = 'COUNT(*)'; $fields = 'COUNT(*)';
} else { } else {
$fields = "$wpdb->comments.comment_ID"; $fields = "{$this->db->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'] = $wpdb->prepare( 'comment_post_ID = %d', $post_id ); $this->sql_clauses['where']['post_id'] = $this->db->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'] = "$wpdb->comments.comment_ID IN ( " . implode( ',', wp_parse_id_list( $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'] ) ) . ' )';
} }
// 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'] = "$wpdb->comments.comment_ID NOT IN ( " . implode( ',', wp_parse_id_list( $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'] ) ) . ' )';
} }
// Parse comment parent IDs for an IN clause. // Parse comment parent IDs for an IN clause.
@ -694,15 +695,15 @@ class WP_Comment_Query {
} }
if ( '' !== $this->query_vars['author_email'] ) { if ( '' !== $this->query_vars['author_email'] ) {
$this->sql_clauses['where']['author_email'] = $wpdb->prepare( 'comment_author_email = %s', $this->query_vars['author_email'] ); $this->sql_clauses['where']['author_email'] = $this->db->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'] = $wpdb->prepare( 'comment_author_url = %s', $this->query_vars['author_url'] ); $this->sql_clauses['where']['author_url'] = $this->db->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'] = $wpdb->prepare( 'comment_karma = %d', $this->query_vars['karma'] ); $this->sql_clauses['where']['karma'] = $this->db->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'.
@ -733,7 +734,7 @@ class WP_Comment_Query {
break; break;
default: default:
$comment_types[ $operator ][] = $wpdb->prepare( '%s', $type ); $comment_types[ $operator ][] = $this->db->prepare( '%s', $type );
break; break;
} }
} }
@ -750,13 +751,13 @@ class WP_Comment_Query {
} }
if ( '' !== $parent ) { if ( '' !== $parent ) {
$this->sql_clauses['where']['parent'] = $wpdb->prepare( 'comment_parent = %d', $parent ); $this->sql_clauses['where']['parent'] = $this->db->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'] = $wpdb->prepare( 'user_id = %d', $this->query_vars['user_id'] ); $this->sql_clauses['where']['user_id'] = $this->db->prepare( 'user_id = %d', $this->query_vars['user_id'] );
} }
// Falsy search strings are ignored. // Falsy search strings are ignored.
@ -780,7 +781,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 ] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value ); $this->sql_clauses['where'][ $field_name ] = $this->db->prepare( " {$this->db->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value );
} }
} }
@ -801,7 +802,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 ] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ")", $q_values ); $this->sql_clauses['where'][ $field_name ] = $this->db->prepare( " {$this->db->posts}.{$field_name} IN (" . implode( ',', $esses ) . ")", $q_values );
} }
} }
@ -830,7 +831,7 @@ class WP_Comment_Query {
$join = ''; $join = '';
if ( $join_posts_table ) { if ( $join_posts_table ) {
$join .= "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID"; $join .= "JOIN {$this->db->posts} ON {$this->db->posts}.ID = {$this->db->comments}.comment_post_ID";
} }
if ( ! empty( $this->meta_query_clauses ) ) { if ( ! empty( $this->meta_query_clauses ) ) {
@ -840,7 +841,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 = "{$wpdb->comments}.comment_ID"; $groupby = "{$this->db->comments}.comment_ID";
} }
} }
@ -889,7 +890,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 $wpdb->comments $join"; $this->sql_clauses['from'] = "FROM {$this->db->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;
@ -897,9 +898,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( $wpdb->get_var( $this->request ) ); return intval( $this->db->get_var( $this->request ) );
} else { } else {
$comment_ids = $wpdb->get_col( $this->request ); $comment_ids = $this->db->get_col( $this->request );
return array_map( 'intval', $comment_ids ); return array_map( 'intval', $comment_ids );
} }
} }
@ -910,12 +911,8 @@ 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.
@ -927,7 +924,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) $wpdb->get_var( $found_comments_query ); $this->found_comments = (int) $this->db->get_var( $found_comments_query );
} }
} }
@ -943,8 +940,6 @@ 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' ),
); );
@ -996,7 +991,7 @@ class WP_Comment_Query {
if ( $uncached_parent_ids ) { if ( $uncached_parent_ids ) {
$where = 'WHERE ' . $_where . ' AND comment_parent IN (' . implode( ',', array_map( 'intval', $uncached_parent_ids ) ) . ')'; $where = 'WHERE ' . $_where . ' AND comment_parent IN (' . implode( ',', array_map( 'intval', $uncached_parent_ids ) ) . ')';
$level_comments = $wpdb->get_results( "SELECT $wpdb->comments.comment_ID, $wpdb->comments.comment_parent {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} ORDER BY comment_date_gmt ASC, comment_ID ASC" ); $level_comments = $this->db->get_results( "SELECT {$this->db->comments}.comment_ID, {$this->db->comments}.comment_parent {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} ORDER BY comment_date_gmt ASC, comment_ID ASC" );
// Cache parent-child relationships. // Cache parent-child relationships.
$parent_map = array_fill_keys( $uncached_parent_ids, array() ); $parent_map = array_fill_keys( $uncached_parent_ids, array() );
@ -1067,20 +1062,16 @@ 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 ) {
global $wpdb; $like = '%' . $this->db->esc_like( $string ) . '%';
$like = '%' . $wpdb->esc_like( $string ) . '%';
$searches = array(); $searches = array();
foreach ( $cols as $col ) { foreach ( $cols as $col ) {
$searches[] = $wpdb->prepare( "$col LIKE %s", $like ); $searches[] = $this->db->prepare( "$col LIKE %s", $like );
} }
return ' AND (' . implode(' OR ', $searches) . ')'; return ' AND (' . implode(' OR ', $searches) . ')';
@ -1092,14 +1083,10 @@ 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',
@ -1131,19 +1118,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 = "$wpdb->commentmeta.meta_value"; $parsed = "{$this->db->commentmeta}.meta_value";
} elseif ( $orderby == 'meta_value_num' ) { } elseif ( $orderby == 'meta_value_num' ) {
$parsed = "$wpdb->commentmeta.meta_value+0"; $parsed = "{$this->db->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( {$wpdb->comments}.comment_ID, $comment__in )"; $parsed = "FIELD( {$this->db->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 = "$wpdb->comments.$orderby"; $parsed = "{$this->db->comments}.$orderby";
} }
} }

View File

@ -105,6 +105,13 @@ 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.
* *
@ -137,8 +144,11 @@ class WP_Meta_Query {
* } * }
*/ */
public function __construct( $meta_query = false ) { public function __construct( $meta_query = false ) {
if ( !$meta_query ) $this->db = $GLOBALS['wpdb'];
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';
@ -484,8 +494,6 @@ 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`
@ -498,8 +506,6 @@ 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(),
@ -537,7 +543,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 .= $wpdb->prepare( " ON ($this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column AND $alias.meta_key = %s )", $clause['key'] ); $join .= $this->db->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 {
@ -581,7 +587,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'][] = $wpdb->prepare( "$alias.meta_key = %s", trim( $clause['key'] ) ); $sql_chunks['where'][] = $this->db->prepare( "$alias.meta_key = %s", trim( $clause['key'] ) );
} }
} }
@ -601,25 +607,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 = $wpdb->prepare( $meta_compare_string, $meta_value ); $where = $this->db->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 = $wpdb->prepare( '%s AND %s', $meta_value ); $where = $this->db->prepare( '%s AND %s', $meta_value );
break; break;
case 'LIKE' : case 'LIKE' :
case 'NOT LIKE' : case 'NOT LIKE' :
$meta_value = '%' . $wpdb->esc_like( $meta_value ) . '%'; $meta_value = '%' . $this->db->esc_like( $meta_value ) . '%';
$where = $wpdb->prepare( '%s', $meta_value ); $where = $this->db->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 = $wpdb->prepare( '%s', $meta_value ); $where = $this->db->prepare( '%s', $meta_value );
break; break;
// 'value' is ignored for NOT EXISTS. // 'value' is ignored for NOT EXISTS.
@ -628,7 +634,7 @@ class WP_Meta_Query {
break; break;
default : default :
$where = $wpdb->prepare( '%s', $meta_value ); $where = $this->db->prepare( '%s', $meta_value );
break; break;
} }

View File

@ -86,6 +86,13 @@ 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.
* *
@ -124,6 +131,8 @@ 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' => '',
@ -290,8 +299,6 @@ 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.
@ -332,7 +339,7 @@ class WP_Network_Query {
$orderby = implode( ', ', $orderby_array ); $orderby = implode( ', ', $orderby_array );
} else { } else {
$orderby = "$wpdb->site.id $order"; $orderby = "{$this->db->site}.id $order";
} }
$number = absint( $this->query_vars['number'] ); $number = absint( $this->query_vars['number'] );
@ -349,52 +356,52 @@ class WP_Network_Query {
if ( $this->query_vars['count'] ) { if ( $this->query_vars['count'] ) {
$fields = 'COUNT(*)'; $fields = 'COUNT(*)';
} else { } else {
$fields = "$wpdb->site.id"; $fields = "{$this->db->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'] = "$wpdb->site.id IN ( " . implode( ',', wp_parse_id_list( $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'] ) ) . ' )';
} }
// 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'] = "$wpdb->site.id NOT IN ( " . implode( ',', wp_parse_id_list( $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'] ) ) . ' )';
} }
if ( ! empty( $this->query_vars['domain'] ) ) { if ( ! empty( $this->query_vars['domain'] ) ) {
$this->sql_clauses['where']['domain'] = $wpdb->prepare( "$wpdb->site.domain = %s", $this->query_vars['domain'] ); $this->sql_clauses['where']['domain'] = $this->db->prepare( "{$this->db->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'] = "$wpdb->site.domain IN ( '" . implode( "', '", $wpdb->_escape( $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'] ) ) . "' )";
} }
// 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'] = "$wpdb->site.domain NOT IN ( '" . implode( "', '", $wpdb->_escape( $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'] ) ) . "' )";
} }
if ( ! empty( $this->query_vars['path'] ) ) { if ( ! empty( $this->query_vars['path'] ) ) {
$this->sql_clauses['where']['path'] = $wpdb->prepare( "$wpdb->site.path = %s", $this->query_vars['path'] ); $this->sql_clauses['where']['path'] = $this->db->prepare( "{$this->db->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'] = "$wpdb->site.path IN ( '" . implode( "', '", $wpdb->_escape( $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'] ) ) . "' )";
} }
// 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'] = "$wpdb->site.path NOT IN ( '" . implode( "', '", $wpdb->_escape( $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'] ) ) . "' )";
} }
// 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( "$wpdb->site.domain", "$wpdb->site.path" ) array( "{$this->db->site}.domain", "{$this->db->site}.path" )
); );
} }
@ -439,7 +446,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 $wpdb->site $join"; $this->sql_clauses['from'] = "FROM {$this->db->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;
@ -447,10 +454,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( $wpdb->get_var( $this->request ) ); return intval( $this->db->get_var( $this->request ) );
} }
$network_ids = $wpdb->get_col( $this->request ); $network_ids = $this->db->get_col( $this->request );
return array_map( 'intval', $network_ids ); return array_map( 'intval', $network_ids );
} }
@ -461,12 +468,8 @@ 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.
@ -478,7 +481,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) $wpdb->get_var( $found_networks_query ); $this->found_networks = (int) $this->db->get_var( $found_networks_query );
} }
} }
@ -488,21 +491,17 @@ 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 ) {
global $wpdb; $like = '%' . $this->db->esc_like( $string ) . '%';
$like = '%' . $wpdb->esc_like( $string ) . '%';
$searches = array(); $searches = array();
foreach ( $columns as $column ) { foreach ( $columns as $column ) {
$searches[] = $wpdb->prepare( "$column LIKE %s", $like ); $searches[] = $this->db->prepare( "$column LIKE %s", $like );
} }
return '(' . implode( ' OR ', $searches ) . ')'; return '(' . implode( ' OR ', $searches ) . ')';
@ -514,14 +513,10 @@ 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',
@ -531,12 +526,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( {$wpdb->site}.id, $network__in )"; $parsed = "FIELD( {$this->db->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($wpdb->site.$field)"; $parsed = "CHAR_LENGTH({$this->db->site}.$field)";
} elseif ( in_array( $orderby, $allowed_keys ) ) { } elseif ( in_array( $orderby, $allowed_keys ) ) {
$parsed = "$wpdb->site.$orderby"; $parsed = "{$this->db->site}.$orderby";
} }
return $parsed; return $parsed;

View File

@ -95,6 +95,13 @@ 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.
* *
@ -147,6 +154,8 @@ 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' => '',
@ -325,13 +334,9 @@ 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.
@ -395,7 +400,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'] = $wpdb->prepare( 'blog_id = %d', $site_id ); $this->sql_clauses['where']['ID'] = $this->db->prepare( 'blog_id = %d', $site_id );
} }
// Parse site IDs for an IN clause. // Parse site IDs for an IN clause.
@ -411,7 +416,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'] = $wpdb->prepare( 'site_id = %d', $network_id ); $this->sql_clauses['where']['network_id'] = $this->db->prepare( 'site_id = %d', $network_id );
} }
// Parse site network IDs for an IN clause. // Parse site network IDs for an IN clause.
@ -425,56 +430,56 @@ class WP_Site_Query {
} }
if ( ! empty( $this->query_vars['domain'] ) ) { if ( ! empty( $this->query_vars['domain'] ) ) {
$this->sql_clauses['where']['domain'] = $wpdb->prepare( 'domain = %s', $this->query_vars['domain'] ); $this->sql_clauses['where']['domain'] = $this->db->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( "', '", $wpdb->_escape( $this->query_vars['domain__in'] ) ) . "' )"; $this->sql_clauses['where']['domain__in'] = "domain IN ( '" . implode( "', '", $this->db->_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( "', '", $wpdb->_escape( $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'] ) ) . "' )";
} }
if ( ! empty( $this->query_vars['path'] ) ) { if ( ! empty( $this->query_vars['path'] ) ) {
$this->sql_clauses['where']['path'] = $wpdb->prepare( 'path = %s', $this->query_vars['path'] ); $this->sql_clauses['where']['path'] = $this->db->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( "', '", $wpdb->_escape( $this->query_vars['path__in'] ) ) . "' )"; $this->sql_clauses['where']['path__in'] = "path IN ( '" . implode( "', '", $this->db->_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( "', '", $wpdb->_escape( $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'] ) ) . "' )";
} }
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'] = $wpdb->prepare( "archived = %d ", $archived ); $this->sql_clauses['where']['archived'] = $this->db->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'] = $wpdb->prepare( "mature = %d ", $mature ); $this->sql_clauses['where']['mature'] = $this->db->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'] = $wpdb->prepare( "spam = %d ", $spam ); $this->sql_clauses['where']['spam'] = $this->db->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'] = $wpdb->prepare( "deleted = %d ", $deleted ); $this->sql_clauses['where']['deleted'] = $this->db->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'] = $wpdb->prepare( "public = %d ", $public ); $this->sql_clauses['where']['public'] = $this->db->prepare( "public = %d ", $public );
} }
// Falsey search strings are ignored. // Falsey search strings are ignored.
@ -550,7 +555,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 $wpdb->blogs $join"; $this->sql_clauses['from'] = "FROM {$this->db->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;
@ -558,10 +563,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( $wpdb->get_var( $this->request ) ); return intval( $this->db->get_var( $this->request ) );
} }
$site_ids = $wpdb->get_col( $this->request ); $site_ids = $this->db->get_col( $this->request );
return array_map( 'intval', $site_ids ); return array_map( 'intval', $site_ids );
} }
@ -572,12 +577,8 @@ 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.
@ -589,7 +590,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) $wpdb->get_var( $found_sites_query ); $this->found_sites = (int) $this->db->get_var( $found_sites_query );
} }
} }
@ -599,24 +600,20 @@ 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( $wpdb, 'esc_like' ), explode( '*', $string ) ) ) . '%'; $like = '%' . implode( '%', array_map( array( $this->db, 'esc_like' ), explode( '*', $string ) ) ) . '%';
} else { } else {
$like = '%' . $wpdb->esc_like( $string ) . '%'; $like = '%' . $this->db->esc_like( $string ) . '%';
} }
$searches = array(); $searches = array();
foreach ( $columns as $column ) { foreach ( $columns as $column ) {
$searches[] = $wpdb->prepare( "$column LIKE %s", $like ); $searches[] = $this->db->prepare( "$column LIKE %s", $like );
} }
return '(' . implode( ' OR ', $searches ) . ')'; return '(' . implode( ' OR ', $searches ) . ')';
@ -628,24 +625,20 @@ 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( {$wpdb->blogs}.blog_id, $site__in )"; $parsed = "FIELD( {$this->db->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( {$wpdb->blogs}.site_id, $network__in )"; $parsed = "FIELD( {$this->db->blogs}.site_id, $network__in )";
break; break;
case 'domain': case 'domain':
case 'last_updated': case 'last_updated':

View File

@ -91,6 +91,13 @@ class WP_Tax_Query {
*/ */
public $primary_id_column; public $primary_id_column;
/**
* @since 4.7.0
* @access protected
* @var wpdb
*/
protected $db;
/** /**
* Constructor. * Constructor.
* *
@ -119,6 +126,8 @@ 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 {
@ -387,8 +396,6 @@ 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 {
@ -399,8 +406,6 @@ 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(),
@ -432,7 +437,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 : $wpdb->term_relationships; $alias = $i ? 'tt' . $i : $this->db->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;
@ -440,7 +445,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 $wpdb->term_relationships"; $join .= " LEFT JOIN {$this->db->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)";
} }
@ -458,7 +463,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 $wpdb->term_relationships FROM {$this->db->term_relationships}
WHERE term_taxonomy_id IN ($terms) WHERE term_taxonomy_id IN ($terms)
)"; )";
@ -474,20 +479,20 @@ class WP_Tax_Query {
$where = "( $where = "(
SELECT COUNT(1) SELECT COUNT(1)
FROM $wpdb->term_relationships FROM {$this->db->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 = $wpdb->prepare( "$operator ( $where = $this->db->prepare( "$operator (
SELECT 1 SELECT 1
FROM $wpdb->term_relationships FROM {$this->db->term_relationships}
INNER JOIN $wpdb->term_taxonomy INNER JOIN {$this->db->term_taxonomy}
ON $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id ON {$this->db->term_taxonomy}.term_taxonomy_id = {$this->db->term_relationships}.term_taxonomy_id
WHERE $wpdb->term_taxonomy.taxonomy = %s WHERE {$this->db->term_taxonomy}.taxonomy = %s
AND $wpdb->term_relationships.object_id = $this->primary_table.$this->primary_id_column AND {$this->db->term_relationships}.object_id = $this->primary_table.$this->primary_id_column
)", $clause['taxonomy'] ); )", $clause['taxonomy'] );
} }
@ -597,15 +602,11 @@ 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;
@ -628,27 +629,27 @@ class WP_Tax_Query {
$terms = implode( ",", $query['terms'] ); $terms = implode( ",", $query['terms'] );
$terms = $wpdb->get_col( " $terms = $this->db->get_col( "
SELECT $wpdb->term_taxonomy.$resulting_field SELECT {$this->db->term_taxonomy}.$resulting_field
FROM $wpdb->term_taxonomy FROM {$this->db->term_taxonomy}
INNER JOIN $wpdb->terms USING (term_id) INNER JOIN {$this->db->terms} USING (term_id)
WHERE taxonomy = '{$query['taxonomy']}' WHERE taxonomy = '{$query['taxonomy']}'
AND $wpdb->terms.{$query['field']} IN ($terms) AND {$this->db->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 = $wpdb->get_col( " $terms = $this->db->get_col( "
SELECT $resulting_field SELECT $resulting_field
FROM $wpdb->term_taxonomy FROM {$this->db->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 = $wpdb->get_col( " $terms = $this->db->get_col( "
SELECT $resulting_field SELECT $resulting_field
FROM $wpdb->term_taxonomy FROM {$this->db->term_taxonomy}
WHERE taxonomy = '{$query['taxonomy']}' WHERE taxonomy = '{$query['taxonomy']}'
AND term_id IN ($terms) AND term_id IN ($terms)
" ); " );

View File

@ -86,6 +86,13 @@ class WP_Term_Query {
*/ */
public $terms; public $terms;
/**
* @since 4.7.0
* @access protected
* @var wpdb
*/
protected $db;
/** /**
* Constructor. * Constructor.
* *
@ -172,6 +179,8 @@ 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,
'orderby' => 'name', 'orderby' => 'name',
@ -293,13 +302,9 @@ 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;
@ -486,16 +491,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'] = $wpdb->prepare( "tt.term_taxonomy_id = %d", $args['term_taxonomy_id'] ); $this->sql_clauses['where']['term_taxonomy_id'] = $this->db->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'] = $wpdb->prepare( "t.name LIKE %s", '%' . $wpdb->esc_like( $args['name__like'] ) . '%' ); $this->sql_clauses['where']['name__like'] = $this->db->prepare( "t.name LIKE %s", '%' . $this->db->esc_like( $args['name__like'] ) . '%' );
} }
if ( ! empty( $args['description__like'] ) ) { if ( ! empty( $args['description__like'] ) ) {
$this->sql_clauses['where']['description__like'] = $wpdb->prepare( "tt.description LIKE %s", '%' . $wpdb->esc_like( $args['description__like'] ) . '%' ); $this->sql_clauses['where']['description__like'] = $this->db->prepare( "tt.description LIKE %s", '%' . $this->db->esc_like( $args['description__like'] ) . '%' );
} }
if ( '' !== $parent ) { if ( '' !== $parent ) {
@ -591,7 +596,7 @@ 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 $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id"; $join .= " INNER JOIN {$this->db->term_taxonomy} AS tt ON t.term_id = tt.term_id";
$where = implode( ' AND ', $this->sql_clauses['where'] ); $where = implode( ' AND ', $this->sql_clauses['where'] );
@ -621,7 +626,7 @@ class WP_Term_Query {
} }
$this->sql_clauses['select'] = "SELECT $distinct $fields"; $this->sql_clauses['select'] = "SELECT $distinct $fields";
$this->sql_clauses['from'] = "FROM $wpdb->terms AS t $join"; $this->sql_clauses['from'] = "FROM {$this->db->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;
@ -646,10 +651,10 @@ class WP_Term_Query {
} }
if ( 'count' == $_fields ) { if ( 'count' == $_fields ) {
return $wpdb->get_var( $this->request ); return $this->db->get_var( $this->request );
} }
$terms = $wpdb->get_results( $this->request ); $terms = $this->db->get_results( $this->request );
if ( 'all' == $_fields ) { if ( 'all' == $_fields ) {
update_term_cache( $terms ); update_term_cache( $terms );
} }
@ -753,8 +758,6 @@ 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.
*/ */
@ -894,16 +897,12 @@ 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 ) {
global $wpdb; $like = '%' . $this->db->esc_like( $string ) . '%';
$like = '%' . $wpdb->esc_like( $string ) . '%'; return $this->db->prepare( '((t.name LIKE %s) OR (t.slug LIKE %s))', $like, $like );
return $wpdb->prepare( '((t.name LIKE %s) OR (t.slug LIKE %s))', $like, $like );
} }
} }

View File

@ -70,6 +70,13 @@ 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.
* *
@ -78,6 +85,8 @@ 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();
@ -134,7 +143,6 @@ 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 {
@ -198,8 +206,6 @@ 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 );
@ -228,19 +234,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[] = "$wpdb->users.$field"; $this->query_fields[] = "{$this->db->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 = "$wpdb->users.*"; $this->query_fields = "{$this->db->users}.*";
} else { } else {
$this->query_fields = "$wpdb->users.ID"; $this->query_fields = "{$this->db->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 $wpdb->users"; $this->query_from = "FROM {$this->db->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.
@ -263,11 +269,11 @@ class WP_User_Query {
} }
foreach ( $post_types as &$post_type ) { foreach ( $post_types as &$post_type ) {
$post_type = $wpdb->prepare( '%s', $post_type ); $post_type = $this->db->prepare( '%s', $post_type );
} }
$posts_table = $wpdb->get_blog_prefix( $blog_id ) . 'posts'; $posts_table = $this->db->get_blog_prefix( $blog_id ) . 'posts';
$this->query_where .= " AND $wpdb->users.ID IN ( SELECT DISTINCT $posts_table.post_author FROM $posts_table WHERE $posts_table.post_status = 'publish' AND $posts_table.post_type IN ( " . join( ", ", $post_types ) . " ) )"; $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 ) . " ) )";
} }
// Meta query. // Meta query.
@ -276,7 +282,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' => $wpdb->get_blog_prefix( $blog_id ) . 'user_level', 'key' => $this->db->get_blog_prefix( $blog_id ) . 'user_level',
'value' => 0, 'value' => 0,
'compare' => '!=', 'compare' => '!=',
); );
@ -323,7 +329,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' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities', 'key' => $this->db->get_blog_prefix( $blog_id ) . 'capabilities',
'value' => '"' . $role . '"', 'value' => '"' . $role . '"',
'compare' => 'LIKE', 'compare' => 'LIKE',
); );
@ -336,7 +342,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' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities', 'key' => $this->db->get_blog_prefix( $blog_id ) . 'capabilities',
'value' => '"' . $role . '"', 'value' => '"' . $role . '"',
'compare' => 'LIKE', 'compare' => 'LIKE',
); );
@ -349,7 +355,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' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities', 'key' => $this->db->get_blog_prefix( $blog_id ) . 'capabilities',
'value' => '"' . $role . '"', 'value' => '"' . $role . '"',
'compare' => 'NOT LIKE', 'compare' => 'NOT LIKE',
); );
@ -361,7 +367,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' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities', 'key' => $this->db->get_blog_prefix( $blog_id ) . 'capabilities',
'compare' => 'EXISTS', 'compare' => 'EXISTS',
); );
} }
@ -383,7 +389,7 @@ class WP_User_Query {
} }
if ( ! empty( $this->meta_query->queries ) ) { if ( ! empty( $this->meta_query->queries ) ) {
$clauses = $this->meta_query->get_sql( 'user', $wpdb->users, 'ID', $this ); $clauses = $this->meta_query->get_sql( 'user', $this->db->users, 'ID', $this );
$this->query_from .= $clauses['join']; $this->query_from .= $clauses['join'];
$this->query_where .= $clauses['where']; $this->query_where .= $clauses['where'];
@ -441,9 +447,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 = $wpdb->prepare("LIMIT %d, %d", $qv['offset'], $qv['number']); $this->query_limit = $this->db->prepare("LIMIT %d, %d", $qv['offset'], $qv['number']);
} else { } else {
$this->query_limit = $wpdb->prepare( "LIMIT %d, %d", $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] ); $this->query_limit = $this->db->prepare( "LIMIT %d, %d", $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] );
} }
} }
@ -499,10 +505,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 $wpdb->users.ID IN ($ids)"; $this->query_where .= " AND {$this->db->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 $wpdb->users.ID NOT IN ($ids)"; $this->query_where .= " AND {$this->db->users}.ID NOT IN ($ids)";
} }
// Date queries are allowed for the user_registered field. // Date queries are allowed for the user_registered field.
@ -530,20 +536,16 @@ 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 = $wpdb->get_results( $this->request ); $this->results = $this->db->get_results( $this->request );
} else { } else {
$this->results = $wpdb->get_col( $this->request ); $this->results = $this->db->get_col( $this->request );
} }
/** /**
@ -551,15 +553,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 = $wpdb->get_var( apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()' ) ); $this->total_users = $this->db->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 );
@ -611,8 +613,6 @@ 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.
@ -620,18 +620,16 @@ 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 . $wpdb->esc_like( $string ) . $trailing_wild; $like = $leading_wild . $this->db->esc_like( $string ) . $trailing_wild;
foreach ( $cols as $col ) { foreach ( $cols as $col ) {
if ( 'ID' == $col ) { if ( 'ID' == $col ) {
$searches[] = $wpdb->prepare( "$col = %s", $string ); $searches[] = $this->db->prepare( "$col = %s", $string );
} else { } else {
$searches[] = $wpdb->prepare( "$col LIKE %s", $like ); $searches[] = $this->db->prepare( "$col LIKE %s", $like );
} }
} }
@ -668,14 +666,10 @@ 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 = '';
@ -690,22 +684,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 $wpdb->posts FROM {$this->db->posts}
$where $where
GROUP BY post_author GROUP BY post_author
) p ON ({$wpdb->users}.ID = p.post_author) ) p ON ({$this->db->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 = "$wpdb->usermeta.meta_value"; $_orderby = "{$this->db->usermeta}.meta_value";
} elseif ( 'meta_value_num' == $orderby ) { } elseif ( 'meta_value_num' == $orderby ) {
$_orderby = "$wpdb->usermeta.meta_value+0"; $_orderby = "{$this->db->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( $wpdb->users.ID, $include_sql )"; $_orderby = "FIELD( {$this->db->users}.ID, $include_sql )";
} elseif ( isset( $meta_query_clauses[ $orderby ] ) ) { } elseif ( isset( $meta_query_clauses[ $orderby ] ) ) {
$meta_clause = $meta_query_clauses[ $orderby ]; $meta_clause = $meta_query_clauses[ $orderby ];
$_orderby = sprintf( "CAST(%s.meta_value AS %s)", esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) ); $_orderby = sprintf( "CAST(%s.meta_value AS %s)", esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) );

View File

@ -101,6 +101,13 @@ 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.
* *
@ -109,15 +116,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 = $GLOBALS['wpdb']->prefix; $prefix = $this->db->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',
@ -232,10 +239,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;
@ -442,18 +449,14 @@ 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 = '' ) {
global $wpdb; if ( empty( $cap_key ) ) {
$this->cap_key = $this->db->get_blog_prefix() . 'capabilities';
if ( empty($cap_key) ) } else {
$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 ) )
@ -631,13 +634,10 @@ 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, $wpdb->get_blog_prefix() . 'user_level', $this->user_level ); update_user_meta( $this->ID, $this->db->get_blog_prefix() . 'user_level', $this->user_level );
} }
/** /**
@ -679,14 +679,11 @@ 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, $wpdb->get_blog_prefix() . 'user_level' ); delete_user_meta( $this->ID, $this->db->get_blog_prefix() . 'user_level' );
$this->get_role_caps(); $this->get_role_caps();
} }
@ -772,16 +769,14 @@ 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 = '' ) {
global $wpdb; if ( ! empty( $blog_id ) ) {
if ( ! empty( $blog_id ) ) $cap_key = $this->db->get_blog_prefix( $blog_id ) . 'capabilities';
$cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities'; } else {
else
$cap_key = ''; $cap_key = '';
}
$this->_init_caps( $cap_key ); $this->_init_caps( $cap_key );
} }
} }

View File

@ -53,6 +53,13 @@ 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.
* *
@ -63,6 +70,8 @@ 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',
@ -2882,8 +2891,6 @@ 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.
* *
@ -2894,8 +2901,6 @@ 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];
@ -2911,14 +2916,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 = $wpdb->get_results(" $page_list = $this->db->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 {$wpdb->posts} FROM {$this->db->posts}
WHERE post_type = 'page' WHERE post_type = 'page'
ORDER BY ID ORDER BY ID
"); ");
@ -5130,20 +5135,17 @@ 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 = $wpdb->get_results( "SELECT ID, guid FROM {$wpdb->posts} WHERE post_parent = '0' AND post_type = 'attachment'" ); $attachments = $this->db->get_results( "SELECT ID, guid FROM {$this->db->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 ) {
$wpdb->update($wpdb->posts, array('post_parent' => $post_ID), array('ID' => $file->ID) ); $this->db->update( $this->db->posts, array( 'post_parent' => $post_ID ), array( 'ID' => $file->ID ) );
}
} }
} }
} }
@ -5763,8 +5765,6 @@ 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.
* *
@ -5776,8 +5776,6 @@ 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];
@ -6102,14 +6100,10 @@ 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' );
@ -6118,7 +6112,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 = $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) ); $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) );
if ( !$comments ) if ( !$comments )
return array(); return array();
@ -6192,7 +6186,6 @@ class wp_xmlrpc_server extends IXR_Server {
* *
* @since 1.5.0 * @since 1.5.0
* *
* @global wpdb $wpdb WordPress database abstraction object.
* @global string $wp_version * @global string $wp_version
* *
* @param array $args { * @param array $args {
@ -6204,7 +6197,7 @@ class wp_xmlrpc_server extends IXR_Server {
* @return string|IXR_Error * @return string|IXR_Error
*/ */
public function pingback_ping( $args ) { public function pingback_ping( $args ) {
global $wpdb, $wp_version; global $wp_version;
/** 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.ping' ); do_action( 'xmlrpc_call', 'pingback.ping' );
@ -6258,8 +6251,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 = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title RLIKE %s", $title ); $sql = $this->db->prepare("SELECT ID FROM {$this->db->posts} WHERE post_title RLIKE %s", $title );
if (! ($post_ID = $wpdb->get_var($sql)) ) { if (! ($post_ID = $this->db->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, '' );
} }
@ -6283,7 +6276,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’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’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 ( $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_author_url = %s", $post_ID, $pagelinkedfrom) ) ) 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) ) )
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,14 +6401,10 @@ 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' );
@ -6434,7 +6423,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 = $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) ); $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) );
if ( !$comments ) if ( !$comments )
return array(); return array();

View File

@ -4,7 +4,7 @@
* *
* @global string $wp_version * @global string $wp_version
*/ */
$wp_version = '4.7-alpha-38274'; $wp_version = '4.7-alpha-38275';
/** /**
* 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.