WordPress.org

Make WordPress Core

Changeset 38275


Ignore:
Timestamp:
08/18/2016 06:20:55 PM (3 years ago)
Author:
wonderboymusic
Message:

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.

Location:
trunk/src/wp-includes
Files:
9 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-comment-query.php

    r38121 r38275  
    141141        return false;
    142142    }
     143
     144    /**
     145     * @since 4.7.0
     146     * @access protected
     147     * @var wpdb
     148     */
     149    protected $db;
    143150
    144151    /**
     
    261268     */
    262269    public function __construct( $query = '' ) {
     270        $this->db = $GLOBALS['wpdb'];
     271
    263272        $this->query_var_defaults = array(
    264273            'author_email' => '',
     
    364373     * @access public
    365374     *
    366      * @global wpdb $wpdb WordPress database abstraction object.
    367      *
    368375     * @return int|array List of comments or number of found comments if `$count` argument is true.
    369376     */
    370377    public function get_comments() {
    371         global $wpdb;
    372 
    373378        $this->parse_query();
    374379
     
    389394        $this->meta_query->parse_query_vars( $this->query_vars );
    390395        if ( ! empty( $this->meta_query->queries ) ) {
    391             $this->meta_query_clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this );
     396            $this->meta_query_clauses = $this->meta_query->get_sql( 'comment', $this->db->comments, 'comment_ID', $this );
    392397        }
    393398
     
    481486     * @since 4.4.0
    482487     * @access protected
    483      *
    484      * @global wpdb $wpdb WordPress database abstraction object.
    485488     */
    486489    protected function get_comment_ids() {
    487         global $wpdb;
    488 
    489490        // Assemble clauses related to 'comment_approved'.
    490491        $approved_clauses = array();
     
    515516
    516517                    default :
    517                         $status_clauses[] = $wpdb->prepare( "comment_approved = %s", $status );
     518                        $status_clauses[] = $this->db->prepare( "comment_approved = %s", $status );
    518519                        break;
    519520                }
     
    538539                // Numeric values are assumed to be user ids.
    539540                if ( is_numeric( $unapproved_identifier ) ) {
    540                     $approved_clauses[] = $wpdb->prepare( "( user_id = %d AND comment_approved = '0' )", $unapproved_identifier );
     541                    $approved_clauses[] = $this->db->prepare( "( user_id = %d AND comment_approved = '0' )", $unapproved_identifier );
    541542
    542543                // Otherwise we match against email addresses.
    543544                } else {
    544                     $approved_clauses[] = $wpdb->prepare( "( comment_author_email = %s AND comment_approved = '0' )", $unapproved_identifier );
     545                    $approved_clauses[] = $this->db->prepare( "( comment_author_email = %s AND comment_approved = '0' )", $unapproved_identifier );
    545546                }
    546547            }
     
    601602            // If no valid clauses were found, order by comment_date_gmt.
    602603            if ( empty( $orderby_array ) ) {
    603                 $orderby_array[] = "$wpdb->comments.comment_date_gmt $order";
     604                $orderby_array[] = "{$this->db->comments}.comment_date_gmt $order";
    604605            }
    605606
     
    634635                }
    635636
    636                 $orderby_array[] = "$wpdb->comments.comment_ID $comment_ID_order";
     637                $orderby_array[] = "{$this->db->comments}.comment_ID $comment_ID_order";
    637638            }
    638639
    639640            $orderby = implode( ', ', $orderby_array );
    640641        } else {
    641             $orderby = "$wpdb->comments.comment_date_gmt $order";
     642            $orderby = "{$this->db->comments}.comment_date_gmt $order";
    642643        }
    643644
     
    656657            $fields = 'COUNT(*)';
    657658        } else {
    658             $fields = "$wpdb->comments.comment_ID";
     659            $fields = "{$this->db->comments}.comment_ID";
    659660        }
    660661
    661662        $post_id = absint( $this->query_vars['post_id'] );
    662663        if ( ! empty( $post_id ) ) {
    663             $this->sql_clauses['where']['post_id'] = $wpdb->prepare( 'comment_post_ID = %d', $post_id );
     664            $this->sql_clauses['where']['post_id'] = $this->db->prepare( 'comment_post_ID = %d', $post_id );
    664665        }
    665666
    666667        // Parse comment IDs for an IN clause.
    667668        if ( ! empty( $this->query_vars['comment__in'] ) ) {
    668             $this->sql_clauses['where']['comment__in'] = "$wpdb->comments.comment_ID IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__in'] ) ) . ' )';
     669            $this->sql_clauses['where']['comment__in'] = "{$this->db->comments}.comment_ID IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__in'] ) ) . ' )';
    669670        }
    670671
    671672        // Parse comment IDs for a NOT IN clause.
    672673        if ( ! empty( $this->query_vars['comment__not_in'] ) ) {
    673             $this->sql_clauses['where']['comment__not_in'] = "$wpdb->comments.comment_ID NOT IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__not_in'] ) ) . ' )';
     674            $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'] ) ) . ' )';
    674675        }
    675676
     
    695696
    696697        if ( '' !== $this->query_vars['author_email'] ) {
    697             $this->sql_clauses['where']['author_email'] = $wpdb->prepare( 'comment_author_email = %s', $this->query_vars['author_email'] );
     698            $this->sql_clauses['where']['author_email'] = $this->db->prepare( 'comment_author_email = %s', $this->query_vars['author_email'] );
    698699        }
    699700
    700701        if ( '' !== $this->query_vars['author_url'] ) {
    701             $this->sql_clauses['where']['author_url'] = $wpdb->prepare( 'comment_author_url = %s', $this->query_vars['author_url'] );
     702            $this->sql_clauses['where']['author_url'] = $this->db->prepare( 'comment_author_url = %s', $this->query_vars['author_url'] );
    702703        }
    703704
    704705        if ( '' !== $this->query_vars['karma'] ) {
    705             $this->sql_clauses['where']['karma'] = $wpdb->prepare( 'comment_karma = %d', $this->query_vars['karma'] );
     706            $this->sql_clauses['where']['karma'] = $this->db->prepare( 'comment_karma = %d', $this->query_vars['karma'] );
    706707        }
    707708
     
    734735
    735736                    default:
    736                         $comment_types[ $operator ][] = $wpdb->prepare( '%s', $type );
     737                        $comment_types[ $operator ][] = $this->db->prepare( '%s', $type );
    737738                        break;
    738739                }
     
    751752
    752753        if ( '' !== $parent ) {
    753             $this->sql_clauses['where']['parent'] = $wpdb->prepare( 'comment_parent = %d', $parent );
     754            $this->sql_clauses['where']['parent'] = $this->db->prepare( 'comment_parent = %d', $parent );
    754755        }
    755756
     
    757758            $this->sql_clauses['where']['user_id'] = 'user_id IN (' . implode( ',', array_map( 'absint', $this->query_vars['user_id'] ) ) . ')';
    758759        } elseif ( '' !== $this->query_vars['user_id'] ) {
    759             $this->sql_clauses['where']['user_id'] = $wpdb->prepare( 'user_id = %d', $this->query_vars['user_id'] );
     760            $this->sql_clauses['where']['user_id'] = $this->db->prepare( 'user_id = %d', $this->query_vars['user_id'] );
    760761        }
    761762
     
    781782                // $field_value may be an array.
    782783                $esses = array_fill( 0, count( (array) $field_value ), '%s' );
    783                 $this->sql_clauses['where'][ $field_name ] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value );
     784                $this->sql_clauses['where'][ $field_name ] = $this->db->prepare( " {$this->db->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value );
    784785            }
    785786        }
     
    802803
    803804                $esses = array_fill( 0, count( $q_values ), '%s' );
    804                 $this->sql_clauses['where'][ $field_name ] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ")", $q_values );
     805                $this->sql_clauses['where'][ $field_name ] = $this->db->prepare( " {$this->db->posts}.{$field_name} IN (" . implode( ',', $esses ) . ")", $q_values );
    805806            }
    806807        }
     
    831832
    832833        if ( $join_posts_table ) {
    833             $join .= "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
     834            $join .= "JOIN {$this->db->posts} ON {$this->db->posts}.ID = {$this->db->comments}.comment_post_ID";
    834835        }
    835836
     
    841842
    842843            if ( ! $this->query_vars['count'] ) {
    843                 $groupby = "{$wpdb->comments}.comment_ID";
     844                $groupby = "{$this->db->comments}.comment_ID";
    844845            }
    845846        }
     
    890891
    891892        $this->sql_clauses['select']  = "SELECT $found_rows $fields";
    892         $this->sql_clauses['from']    = "FROM $wpdb->comments $join";
     893        $this->sql_clauses['from']    = "FROM {$this->db->comments} $join";
    893894        $this->sql_clauses['groupby'] = $groupby;
    894895        $this->sql_clauses['orderby'] = $orderby;
     
    898899
    899900        if ( $this->query_vars['count'] ) {
    900             return intval( $wpdb->get_var( $this->request ) );
     901            return intval( $this->db->get_var( $this->request ) );
    901902        } else {
    902             $comment_ids = $wpdb->get_col( $this->request );
     903            $comment_ids = $this->db->get_col( $this->request );
    903904            return array_map( 'intval', $comment_ids );
    904905        }
     
    911912     * @since 4.6.0
    912913     * @access private
    913      *
    914      * @global wpdb $wpdb WordPress database abstraction object.
    915914     */
    916915    private function set_found_comments() {
    917         global $wpdb;
    918 
    919916        if ( $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
    920917            /**
     
    928925            $found_comments_query = apply_filters( 'found_comments_query', 'SELECT FOUND_ROWS()', $this );
    929926
    930             $this->found_comments = (int) $wpdb->get_var( $found_comments_query );
     927            $this->found_comments = (int) $this->db->get_var( $found_comments_query );
    931928        }
    932929    }
     
    944941     */
    945942    protected function fill_descendants( $comments ) {
    946         global $wpdb;
    947 
    948943        $levels = array(
    949944            0 => wp_list_pluck( $comments, 'comment_ID' ),
     
    997992            if ( $uncached_parent_ids ) {
    998993                $where = 'WHERE ' . $_where . ' AND comment_parent IN (' . implode( ',', array_map( 'intval', $uncached_parent_ids ) ) . ')';
    999                 $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" );
     994                $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" );
    1000995
    1001996                // Cache parent-child relationships.
     
    10681063     * @access protected
    10691064     *
    1070      * @global wpdb $wpdb WordPress database abstraction object.
    1071      *
    10721065     * @param string $string
    10731066     * @param array $cols
     
    10751068     */
    10761069    protected function get_search_sql( $string, $cols ) {
    1077         global $wpdb;
    1078 
    1079         $like = '%' . $wpdb->esc_like( $string ) . '%';
     1070        $like = '%' . $this->db->esc_like( $string ) . '%';
    10801071
    10811072        $searches = array();
    10821073        foreach ( $cols as $col ) {
    1083             $searches[] = $wpdb->prepare( "$col LIKE %s", $like );
     1074            $searches[] = $this->db->prepare( "$col LIKE %s", $like );
    10841075        }
    10851076
     
    10931084     * @access protected
    10941085     *
    1095      * @global wpdb $wpdb WordPress database abstraction object.
    1096      *
    10971086     * @param string $orderby Alias for the field to order by.
    10981087     * @return string|false Value to used in the ORDER clause. False otherwise.
    10991088     */
    11001089    protected function parse_orderby( $orderby ) {
    1101         global $wpdb;
    1102 
    11031090        $allowed_keys = array(
    11041091            'comment_agent',
     
    11321119        $parsed = false;
    11331120        if ( $orderby == $this->query_vars['meta_key'] || $orderby == 'meta_value' ) {
    1134             $parsed = "$wpdb->commentmeta.meta_value";
     1121            $parsed = "{$this->db->commentmeta}.meta_value";
    11351122        } elseif ( $orderby == 'meta_value_num' ) {
    1136             $parsed = "$wpdb->commentmeta.meta_value+0";
     1123            $parsed = "{$this->db->commentmeta}.meta_value+0";
    11371124        } elseif ( $orderby == 'comment__in' ) {
    11381125            $comment__in = implode( ',', array_map( 'absint', $this->query_vars['comment__in'] ) );
    1139             $parsed = "FIELD( {$wpdb->comments}.comment_ID, $comment__in )";
     1126            $parsed = "FIELD( {$this->db->comments}.comment_ID, $comment__in )";
    11401127        } elseif ( in_array( $orderby, $allowed_keys ) ) {
    11411128
     
    11441131                $parsed = sprintf( "CAST(%s.meta_value AS %s)", esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) );
    11451132            } else {
    1146                 $parsed = "$wpdb->comments.$orderby";
     1133                $parsed = "{$this->db->comments}.$orderby";
    11471134            }
    11481135        }
  • trunk/src/wp-includes/class-wp-meta-query.php

    r37860 r38275  
    107107
    108108    /**
     109     * @since 4.7.0
     110     * @access protected
     111     * @var wpdb
     112     */
     113    protected $db;
     114
     115    /**
    109116     * Constructor.
    110117     *
     
    138145     */
    139146    public function __construct( $meta_query = false ) {
    140         if ( !$meta_query )
     147        $this->db = $GLOBALS['wpdb'];
     148
     149        if ( ! $meta_query ) {
    141150            return;
     151        }
    142152
    143153        if ( isset( $meta_query['relation'] ) && strtoupper( $meta_query['relation'] ) == 'OR' ) {
     
    485495     * @access public
    486496     *
    487      * @global wpdb $wpdb WordPress database abstraction object.
    488      *
    489497     * @param array  $clause       Query clause, passed by reference.
    490498     * @param array  $parent_query Parent query array.
     
    499507     */
    500508    public function get_sql_for_clause( &$clause, $parent_query, $clause_key = '' ) {
    501         global $wpdb;
    502 
    503509        $sql_chunks = array(
    504510            'where' => array(),
     
    538544                $join .= " LEFT JOIN $this->meta_table";
    539545                $join .= $i ? " AS $alias" : '';
    540                 $join .= $wpdb->prepare( " ON ($this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column AND $alias.meta_key = %s )", $clause['key'] );
     546                $join .= $this->db->prepare( " ON ($this->primary_table.$this->primary_id_column = $alias.$this->meta_id_column AND $alias.meta_key = %s )", $clause['key'] );
    541547
    542548            // All other JOIN clauses.
     
    582588                $sql_chunks['where'][] = $alias . '.' . $this->meta_id_column . ' IS NULL';
    583589            } else {
    584                 $sql_chunks['where'][] = $wpdb->prepare( "$alias.meta_key = %s", trim( $clause['key'] ) );
     590                $sql_chunks['where'][] = $this->db->prepare( "$alias.meta_key = %s", trim( $clause['key'] ) );
    585591            }
    586592        }
     
    602608                case 'NOT IN' :
    603609                    $meta_compare_string = '(' . substr( str_repeat( ',%s', count( $meta_value ) ), 1 ) . ')';
    604                     $where = $wpdb->prepare( $meta_compare_string, $meta_value );
     610                    $where = $this->db->prepare( $meta_compare_string, $meta_value );
    605611                    break;
    606612
     
    608614                case 'NOT BETWEEN' :
    609615                    $meta_value = array_slice( $meta_value, 0, 2 );
    610                     $where = $wpdb->prepare( '%s AND %s', $meta_value );
     616                    $where = $this->db->prepare( '%s AND %s', $meta_value );
    611617                    break;
    612618
    613619                case 'LIKE' :
    614620                case 'NOT LIKE' :
    615                     $meta_value = '%' . $wpdb->esc_like( $meta_value ) . '%';
    616                     $where = $wpdb->prepare( '%s', $meta_value );
     621                    $meta_value = '%' . $this->db->esc_like( $meta_value ) . '%';
     622                    $where = $this->db->prepare( '%s', $meta_value );
    617623                    break;
    618624
     
    620626                case 'EXISTS' :
    621627                    $meta_compare = '=';
    622                     $where = $wpdb->prepare( '%s', $meta_value );
     628                    $where = $this->db->prepare( '%s', $meta_value );
    623629                    break;
    624630
     
    629635
    630636                default :
    631                     $where = $wpdb->prepare( '%s', $meta_value );
     637                    $where = $this->db->prepare( '%s', $meta_value );
    632638                    break;
    633639
  • trunk/src/wp-includes/class-wp-network-query.php

    r38104 r38275  
    8686     */
    8787    public $max_num_pages = 0;
     88
     89    /**
     90     * @since 4.7.0
     91     * @access protected
     92     * @var wpdb
     93     */
     94    protected $db;
    8895
    8996    /**
     
    125132     */
    126133    public function __construct( $query = '' ) {
     134        $this->db = $GLOBALS['wpdb'];
     135
    127136        $this->query_var_defaults = array(
    128137            'network__in'          => '',
     
    291300     */
    292301    protected function get_network_ids() {
    293         global $wpdb;
    294 
    295302        $order = $this->parse_order( $this->query_vars['order'] );
    296303
     
    333340            $orderby = implode( ', ', $orderby_array );
    334341        } else {
    335             $orderby = "$wpdb->site.id $order";
     342            $orderby = "{$this->db->site}.id $order";
    336343        }
    337344
     
    350357            $fields = 'COUNT(*)';
    351358        } else {
    352             $fields = "$wpdb->site.id";
     359            $fields = "{$this->db->site}.id";
    353360        }
    354361
    355362        // Parse network IDs for an IN clause.
    356363        if ( ! empty( $this->query_vars['network__in'] ) ) {
    357             $this->sql_clauses['where']['network__in'] = "$wpdb->site.id IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['network__in'] ) ) . ' )';
     364            $this->sql_clauses['where']['network__in'] = "{$this->db->site}.id IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['network__in'] ) ) . ' )';
    358365        }
    359366
    360367        // Parse network IDs for a NOT IN clause.
    361368        if ( ! empty( $this->query_vars['network__not_in'] ) ) {
    362             $this->sql_clauses['where']['network__not_in'] = "$wpdb->site.id NOT IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['network__not_in'] ) ) . ' )';
     369            $this->sql_clauses['where']['network__not_in'] = "{$this->db->site}.id NOT IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['network__not_in'] ) ) . ' )';
    363370        }
    364371
    365372        if ( ! empty( $this->query_vars['domain'] ) ) {
    366             $this->sql_clauses['where']['domain'] = $wpdb->prepare( "$wpdb->site.domain = %s", $this->query_vars['domain'] );
     373            $this->sql_clauses['where']['domain'] = $this->db->prepare( "{$this->db->site}.domain = %s", $this->query_vars['domain'] );
    367374        }
    368375
    369376        // Parse network domain for an IN clause.
    370377        if ( is_array( $this->query_vars['domain__in'] ) ) {
    371             $this->sql_clauses['where']['domain__in'] = "$wpdb->site.domain IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['domain__in'] ) ) . "' )";
     378            $this->sql_clauses['where']['domain__in'] = "{$this->db->site}.domain IN ( '" . implode( "', '", $this->db->_escape( $this->query_vars['domain__in'] ) ) . "' )";
    372379        }
    373380
    374381        // Parse network domain for a NOT IN clause.
    375382        if ( is_array( $this->query_vars['domain__not_in'] ) ) {
    376             $this->sql_clauses['where']['domain__not_in'] = "$wpdb->site.domain NOT IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['domain__not_in'] ) ) . "' )";
     383            $this->sql_clauses['where']['domain__not_in'] = "{$this->db->site}.domain NOT IN ( '" . implode( "', '", $this->db->_escape( $this->query_vars['domain__not_in'] ) ) . "' )";
    377384        }
    378385
    379386        if ( ! empty( $this->query_vars['path'] ) ) {
    380             $this->sql_clauses['where']['path'] = $wpdb->prepare( "$wpdb->site.path = %s", $this->query_vars['path'] );
     387            $this->sql_clauses['where']['path'] = $this->db->prepare( "{$this->db->site}.path = %s", $this->query_vars['path'] );
    381388        }
    382389
    383390        // Parse network path for an IN clause.
    384391        if ( is_array( $this->query_vars['path__in'] ) ) {
    385             $this->sql_clauses['where']['path__in'] = "$wpdb->site.path IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['path__in'] ) ) . "' )";
     392            $this->sql_clauses['where']['path__in'] = "{$this->db->site}.path IN ( '" . implode( "', '", $this->db->_escape( $this->query_vars['path__in'] ) ) . "' )";
    386393        }
    387394
    388395        // Parse network path for a NOT IN clause.
    389396        if ( is_array( $this->query_vars['path__not_in'] ) ) {
    390             $this->sql_clauses['where']['path__not_in'] = "$wpdb->site.path NOT IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['path__not_in'] ) ) . "' )";
     397            $this->sql_clauses['where']['path__not_in'] = "{$this->db->site}.path NOT IN ( '" . implode( "', '", $this->db->_escape( $this->query_vars['path__not_in'] ) ) . "' )";
    391398        }
    392399
     
    395402            $this->sql_clauses['where']['search'] = $this->get_search_sql(
    396403                $this->query_vars['search'],
    397                 array( "$wpdb->site.domain", "$wpdb->site.path" )
     404                array( "{$this->db->site}.domain", "{$this->db->site}.path" )
    398405            );
    399406        }
     
    440447
    441448        $this->sql_clauses['select']  = "SELECT $found_rows $fields";
    442         $this->sql_clauses['from']    = "FROM $wpdb->site $join";
     449        $this->sql_clauses['from']    = "FROM {$this->db->site} $join";
    443450        $this->sql_clauses['groupby'] = $groupby;
    444451        $this->sql_clauses['orderby'] = $orderby;
     
    448455
    449456        if ( $this->query_vars['count'] ) {
    450             return intval( $wpdb->get_var( $this->request ) );
    451         }
    452 
    453         $network_ids = $wpdb->get_col( $this->request );
     457            return intval( $this->db->get_var( $this->request ) );
     458        }
     459
     460        $network_ids = $this->db->get_col( $this->request );
    454461
    455462        return array_map( 'intval', $network_ids );
     
    462469     * @since 4.6.0
    463470     * @access private
    464      *
    465      * @global wpdb $wpdb WordPress database abstraction object.
    466471     */
    467472    private function set_found_networks() {
    468         global $wpdb;
    469 
    470473        if ( $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
    471474            /**
     
    479482            $found_networks_query = apply_filters( 'found_networks_query', 'SELECT FOUND_ROWS()', $this );
    480483
    481             $this->found_networks = (int) $wpdb->get_var( $found_networks_query );
     484            $this->found_networks = (int) $this->db->get_var( $found_networks_query );
    482485        }
    483486    }
     
    488491     * @since 4.6.0
    489492     * @access protected
    490      *
    491      * @global wpdb  $wpdb WordPress database abstraction object.
    492493     *
    493494     * @param string $string  Search string.
     
    497498     */
    498499    protected function get_search_sql( $string, $columns ) {
    499         global $wpdb;
    500 
    501         $like = '%' . $wpdb->esc_like( $string ) . '%';
     500        $like = '%' . $this->db->esc_like( $string ) . '%';
    502501
    503502        $searches = array();
    504503        foreach ( $columns as $column ) {
    505             $searches[] = $wpdb->prepare( "$column LIKE %s", $like );
     504            $searches[] = $this->db->prepare( "$column LIKE %s", $like );
    506505        }
    507506
     
    514513     * @since 4.6.0
    515514     * @access protected
    516      *
    517      * @global wpdb $wpdb WordPress database abstraction object.
    518515     *
    519516     * @param string $orderby Alias for the field to order by.
     
    521518     */
    522519    protected function parse_orderby( $orderby ) {
    523         global $wpdb;
    524 
    525520        $allowed_keys = array(
    526521            'id',
     
    532527        if ( $orderby == 'network__in' ) {
    533528            $network__in = implode( ',', array_map( 'absint', $this->query_vars['network__in'] ) );
    534             $parsed = "FIELD( {$wpdb->site}.id, $network__in )";
     529            $parsed = "FIELD( {$this->db->site}.id, $network__in )";
    535530        } elseif ( $orderby == 'domain_length' || $orderby == 'path_length' ) {
    536531            $field = substr( $orderby, 0, -7 );
    537             $parsed = "CHAR_LENGTH($wpdb->site.$field)";
     532            $parsed = "CHAR_LENGTH({$this->db->site}.$field)";
    538533        } elseif ( in_array( $orderby, $allowed_keys ) ) {
    539             $parsed = "$wpdb->site.$orderby";
     534            $parsed = "{$this->db->site}.$orderby";
    540535        }
    541536
  • trunk/src/wp-includes/class-wp-site-query.php

    r38103 r38275  
    9595     */
    9696    public $max_num_pages = 0;
     97
     98    /**
     99     * @since 4.7.0
     100     * @access protected
     101     * @var wpdb
     102     */
     103    protected $db;
    97104
    98105    /**
     
    148155     */
    149156    public function __construct( $query = '' ) {
     157        $this->db = $GLOBALS['wpdb'];
     158
    150159        $this->query_var_defaults = array(
    151160            'fields'            => '',
     
    326335     * @access protected
    327336     *
    328      * @global wpdb $wpdb WordPress database abstraction object.
    329      *
    330337     * @return int|array A single count of site IDs if a count query. An array of site IDs if a full query.
    331338     */
    332339    protected function get_site_ids() {
    333         global $wpdb;
    334 
    335340        $order = $this->parse_order( $this->query_vars['order'] );
    336341
     
    396401        $site_id = absint( $this->query_vars['ID'] );
    397402        if ( ! empty( $site_id ) ) {
    398             $this->sql_clauses['where']['ID'] = $wpdb->prepare( 'blog_id = %d', $site_id );
     403            $this->sql_clauses['where']['ID'] = $this->db->prepare( 'blog_id = %d', $site_id );
    399404        }
    400405
     
    412417
    413418        if ( ! empty( $network_id ) ) {
    414             $this->sql_clauses['where']['network_id'] = $wpdb->prepare( 'site_id = %d', $network_id );
     419            $this->sql_clauses['where']['network_id'] = $this->db->prepare( 'site_id = %d', $network_id );
    415420        }
    416421
     
    426431
    427432        if ( ! empty( $this->query_vars['domain'] ) ) {
    428             $this->sql_clauses['where']['domain'] = $wpdb->prepare( 'domain = %s', $this->query_vars['domain'] );
     433            $this->sql_clauses['where']['domain'] = $this->db->prepare( 'domain = %s', $this->query_vars['domain'] );
    429434        }
    430435
    431436        // Parse site domain for an IN clause.
    432437        if ( is_array( $this->query_vars['domain__in'] ) ) {
    433             $this->sql_clauses['where']['domain__in'] = "domain IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['domain__in'] ) ) . "' )";
     438            $this->sql_clauses['where']['domain__in'] = "domain IN ( '" . implode( "', '", $this->db->_escape( $this->query_vars['domain__in'] ) ) . "' )";
    434439        }
    435440
    436441        // Parse site domain for a NOT IN clause.
    437442        if ( is_array( $this->query_vars['domain__not_in'] ) ) {
    438             $this->sql_clauses['where']['domain__not_in'] = "domain NOT IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['domain__not_in'] ) ) . "' )";
     443            $this->sql_clauses['where']['domain__not_in'] = "domain NOT IN ( '" . implode( "', '", $this->db->_escape( $this->query_vars['domain__not_in'] ) ) . "' )";
    439444        }
    440445
    441446        if ( ! empty( $this->query_vars['path'] ) ) {
    442             $this->sql_clauses['where']['path'] = $wpdb->prepare( 'path = %s', $this->query_vars['path'] );
     447            $this->sql_clauses['where']['path'] = $this->db->prepare( 'path = %s', $this->query_vars['path'] );
    443448        }
    444449
    445450        // Parse site path for an IN clause.
    446451        if ( is_array( $this->query_vars['path__in'] ) ) {
    447             $this->sql_clauses['where']['path__in'] = "path IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['path__in'] ) ) . "' )";
     452            $this->sql_clauses['where']['path__in'] = "path IN ( '" . implode( "', '", $this->db->_escape( $this->query_vars['path__in'] ) ) . "' )";
    448453        }
    449454
    450455        // Parse site path for a NOT IN clause.
    451456        if ( is_array( $this->query_vars['path__not_in'] ) ) {
    452             $this->sql_clauses['where']['path__not_in'] = "path NOT IN ( '" . implode( "', '", $wpdb->_escape( $this->query_vars['path__not_in'] ) ) . "' )";
     457            $this->sql_clauses['where']['path__not_in'] = "path NOT IN ( '" . implode( "', '", $this->db->_escape( $this->query_vars['path__not_in'] ) ) . "' )";
    453458        }
    454459
    455460        if ( is_numeric( $this->query_vars['archived'] ) ) {
    456461            $archived = absint( $this->query_vars['archived'] );
    457             $this->sql_clauses['where']['archived'] = $wpdb->prepare( "archived = %d ", $archived );
     462            $this->sql_clauses['where']['archived'] = $this->db->prepare( "archived = %d ", $archived );
    458463        }
    459464
    460465        if ( is_numeric( $this->query_vars['mature'] ) ) {
    461466            $mature = absint( $this->query_vars['mature'] );
    462             $this->sql_clauses['where']['mature'] = $wpdb->prepare( "mature = %d ", $mature );
     467            $this->sql_clauses['where']['mature'] = $this->db->prepare( "mature = %d ", $mature );
    463468        }
    464469
    465470        if ( is_numeric( $this->query_vars['spam'] ) ) {
    466471            $spam = absint( $this->query_vars['spam'] );
    467             $this->sql_clauses['where']['spam'] = $wpdb->prepare( "spam = %d ", $spam );
     472            $this->sql_clauses['where']['spam'] = $this->db->prepare( "spam = %d ", $spam );
    468473        }
    469474
    470475        if ( is_numeric( $this->query_vars['deleted'] ) ) {
    471476            $deleted = absint( $this->query_vars['deleted'] );
    472             $this->sql_clauses['where']['deleted'] = $wpdb->prepare( "deleted = %d ", $deleted );
     477            $this->sql_clauses['where']['deleted'] = $this->db->prepare( "deleted = %d ", $deleted );
    473478        }
    474479
    475480        if ( is_numeric( $this->query_vars['public'] ) ) {
    476481            $public = absint( $this->query_vars['public'] );
    477             $this->sql_clauses['where']['public'] = $wpdb->prepare( "public = %d ", $public );
     482            $this->sql_clauses['where']['public'] = $this->db->prepare( "public = %d ", $public );
    478483        }
    479484
     
    551556
    552557        $this->sql_clauses['select']  = "SELECT $found_rows $fields";
    553         $this->sql_clauses['from']    = "FROM $wpdb->blogs $join";
     558        $this->sql_clauses['from']    = "FROM {$this->db->blogs} $join";
    554559        $this->sql_clauses['groupby'] = $groupby;
    555560        $this->sql_clauses['orderby'] = $orderby;
     
    559564
    560565        if ( $this->query_vars['count'] ) {
    561             return intval( $wpdb->get_var( $this->request ) );
    562         }
    563 
    564         $site_ids = $wpdb->get_col( $this->request );
     566            return intval( $this->db->get_var( $this->request ) );
     567        }
     568
     569        $site_ids = $this->db->get_col( $this->request );
    565570
    566571        return array_map( 'intval', $site_ids );
     
    573578     * @since 4.6.0
    574579     * @access private
    575      *
    576      * @global wpdb $wpdb WordPress database abstraction object.
    577580     */
    578581    private function set_found_sites() {
    579         global $wpdb;
    580 
    581582        if ( $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
    582583            /**
     
    590591            $found_sites_query = apply_filters( 'found_sites_query', 'SELECT FOUND_ROWS()', $this );
    591592
    592             $this->found_sites = (int) $wpdb->get_var( $found_sites_query );
     593            $this->found_sites = (int) $this->db->get_var( $found_sites_query );
    593594        }
    594595    }
     
    599600     * @since 4.6.0
    600601     * @access protected
    601      *
    602      * @global wpdb  $wpdb WordPress database abstraction object.
    603602     *
    604603     * @param string $string  Search string.
     
    607606     */
    608607    protected function get_search_sql( $string, $columns ) {
    609         global $wpdb;
    610 
    611608        if ( false !== strpos( $string, '*' ) ) {
    612             $like = '%' . implode( '%', array_map( array( $wpdb, 'esc_like' ), explode( '*', $string ) ) ) . '%';
     609            $like = '%' . implode( '%', array_map( array( $this->db, 'esc_like' ), explode( '*', $string ) ) ) . '%';
    613610        } else {
    614             $like = '%' . $wpdb->esc_like( $string ) . '%';
     611            $like = '%' . $this->db->esc_like( $string ) . '%';
    615612        }
    616613
    617614        $searches = array();
    618615        foreach ( $columns as $column ) {
    619             $searches[] = $wpdb->prepare( "$column LIKE %s", $like );
     616            $searches[] = $this->db->prepare( "$column LIKE %s", $like );
    620617        }
    621618
     
    628625     * @since 4.6.0
    629626     * @access protected
    630      *
    631      * @global wpdb $wpdb WordPress database abstraction object.
    632627     *
    633628     * @param string $orderby Alias for the field to order by.
     
    635630     */
    636631    protected function parse_orderby( $orderby ) {
    637         global $wpdb;
    638 
    639632        $parsed = false;
    640633
     
    642635            case 'site__in':
    643636                $site__in = implode( ',', array_map( 'absint', $this->query_vars['site__in'] ) );
    644                 $parsed = "FIELD( {$wpdb->blogs}.blog_id, $site__in )";
     637                $parsed = "FIELD( {$this->db->blogs}.blog_id, $site__in )";
    645638                break;
    646639            case 'network__in':
    647640                $network__in = implode( ',', array_map( 'absint', $this->query_vars['network__in'] ) );
    648                 $parsed = "FIELD( {$wpdb->blogs}.site_id, $network__in )";
     641                $parsed = "FIELD( {$this->db->blogs}.site_id, $network__in )";
    649642                break;
    650643            case 'domain':
  • trunk/src/wp-includes/class-wp-tax-query.php

    r38079 r38275  
    9393
    9494    /**
     95     * @since 4.7.0
     96     * @access protected
     97     * @var wpdb
     98     */
     99    protected $db;
     100
     101    /**
    95102     * Constructor.
    96103     *
     
    120127     */
    121128    public function __construct( $tax_query ) {
     129        $this->db = $GLOBALS['wpdb'];
     130
    122131        if ( isset( $tax_query['relation'] ) ) {
    123132            $this->relation = $this->sanitize_relation( $tax_query['relation'] );
     
    388397     * @access public
    389398     *
    390      * @global wpdb $wpdb The WordPress database abstraction object.
    391      *
    392399     * @param array $clause       Query clause, passed by reference.
    393400     * @param array $parent_query Parent query array.
     
    400407     */
    401408    public function get_sql_for_clause( &$clause, $parent_query ) {
    402         global $wpdb;
    403 
    404409        $sql = array(
    405410            'where' => array(),
     
    433438            if ( false === $alias ) {
    434439                $i = count( $this->table_aliases );
    435                 $alias = $i ? 'tt' . $i : $wpdb->term_relationships;
     440                $alias = $i ? 'tt' . $i : $this->db->term_relationships;
    436441
    437442                // Store the alias as part of a flat array to build future iterators.
     
    441446                $clause['alias'] = $alias;
    442447
    443                 $join .= " LEFT JOIN $wpdb->term_relationships";
     448                $join .= " LEFT JOIN {$this->db->term_relationships}";
    444449                $join .= $i ? " AS $alias" : '';
    445450                $join .= " ON ($this->primary_table.$this->primary_id_column = $alias.object_id)";
     
    459464            $where = "$this->primary_table.$this->primary_id_column NOT IN (
    460465                SELECT object_id
    461                 FROM $wpdb->term_relationships
     466                FROM {$this->db->term_relationships}
    462467                WHERE term_taxonomy_id IN ($terms)
    463468            )";
     
    475480            $where = "(
    476481                SELECT COUNT(1)
    477                 FROM $wpdb->term_relationships
     482                FROM {$this->db->term_relationships}
    478483                WHERE term_taxonomy_id IN ($terms)
    479484                AND object_id = $this->primary_table.$this->primary_id_column
     
    482487        } elseif ( 'NOT EXISTS' === $operator || 'EXISTS' === $operator ) {
    483488
    484             $where = $wpdb->prepare( "$operator (
     489            $where = $this->db->prepare( "$operator (
    485490                SELECT 1
    486                 FROM $wpdb->term_relationships
    487                 INNER JOIN $wpdb->term_taxonomy
    488                 ON $wpdb->term_taxonomy.term_taxonomy_id = $wpdb->term_relationships.term_taxonomy_id
    489                 WHERE $wpdb->term_taxonomy.taxonomy = %s
    490                 AND $wpdb->term_relationships.object_id = $this->primary_table.$this->primary_id_column
     491                FROM {$this->db->term_relationships}
     492                INNER JOIN {$this->db->term_taxonomy}
     493                ON {$this->db->term_taxonomy}.term_taxonomy_id = {$this->db->term_relationships}.term_taxonomy_id
     494                WHERE {$this->db->term_taxonomy}.taxonomy = %s
     495                AND {$this->db->term_relationships}.object_id = $this->primary_table.$this->primary_id_column
    491496            )", $clause['taxonomy'] );
    492497
     
    598603     * @since 3.2.0
    599604     *
    600      * @global wpdb $wpdb The WordPress database abstraction object.
    601      *
    602605     * @param array  $query           The single query. Passed by reference.
    603606     * @param string $resulting_field The resulting field. Accepts 'slug', 'name', 'term_taxonomy_id',
     
    605608     */
    606609    public function transform_query( &$query, $resulting_field ) {
    607         global $wpdb;
    608 
    609610        if ( empty( $query['terms'] ) )
    610611            return;
     
    629630                $terms = implode( ",", $query['terms'] );
    630631
    631                 $terms = $wpdb->get_col( "
    632                     SELECT $wpdb->term_taxonomy.$resulting_field
    633                     FROM $wpdb->term_taxonomy
    634                     INNER JOIN $wpdb->terms USING (term_id)
     632                $terms = $this->db->get_col( "
     633                    SELECT {$this->db->term_taxonomy}.$resulting_field
     634                    FROM {$this->db->term_taxonomy}
     635                    INNER JOIN {$this->db->terms} USING (term_id)
    635636                    WHERE taxonomy = '{$query['taxonomy']}'
    636                     AND $wpdb->terms.{$query['field']} IN ($terms)
     637                    AND {$this->db->terms}.{$query['field']} IN ($terms)
    637638                " );
    638639                break;
    639640            case 'term_taxonomy_id':
    640641                $terms = implode( ',', array_map( 'intval', $query['terms'] ) );
    641                 $terms = $wpdb->get_col( "
     642                $terms = $this->db->get_col( "
    642643                    SELECT $resulting_field
    643                     FROM $wpdb->term_taxonomy
     644                    FROM {$this->db->term_taxonomy}
    644645                    WHERE term_taxonomy_id IN ($terms)
    645646                " );
     
    647648            default:
    648649                $terms = implode( ',', array_map( 'intval', $query['terms'] ) );
    649                 $terms = $wpdb->get_col( "
     650                $terms = $this->db->get_col( "
    650651                    SELECT $resulting_field
    651                     FROM $wpdb->term_taxonomy
     652                    FROM {$this->db->term_taxonomy}
    652653                    WHERE taxonomy = '{$query['taxonomy']}'
    653654                    AND term_id IN ($terms)
  • trunk/src/wp-includes/class-wp-term-query.php

    r38212 r38275  
    8686     */
    8787    public $terms;
     88
     89    /**
     90     * @since 4.7.0
     91     * @access protected
     92     * @var wpdb
     93     */
     94    protected $db;
    8895
    8996    /**
     
    173180     */
    174181    public function __construct( $query = '' ) {
     182        $this->db = $GLOBALS['wpdb'];
     183
    175184        $this->query_var_defaults = array(
    176185            'taxonomy'               => null,
     
    294303     * @access public
    295304     *
    296      * @global wpdb $wpdb WordPress database abstraction object.
    297      *
    298305     * @return array
    299306     */
    300307    public function get_terms() {
    301         global $wpdb;
    302 
    303308        $this->parse_query( $this->query_vars );
    304309        $args = $this->query_vars;
     
    487492                $this->sql_clauses['where']['term_taxonomy_id'] = "tt.term_taxonomy_id IN ({$tt_ids})";
    488493            } else {
    489                 $this->sql_clauses['where']['term_taxonomy_id'] = $wpdb->prepare( "tt.term_taxonomy_id = %d", $args['term_taxonomy_id'] );
     494                $this->sql_clauses['where']['term_taxonomy_id'] = $this->db->prepare( "tt.term_taxonomy_id = %d", $args['term_taxonomy_id'] );
    490495            }
    491496        }
    492497
    493498        if ( ! empty( $args['name__like'] ) ) {
    494             $this->sql_clauses['where']['name__like'] = $wpdb->prepare( "t.name LIKE %s", '%' . $wpdb->esc_like( $args['name__like'] ) . '%' );
     499            $this->sql_clauses['where']['name__like'] = $this->db->prepare( "t.name LIKE %s", '%' . $this->db->esc_like( $args['name__like'] ) . '%' );
    495500        }
    496501
    497502        if ( ! empty( $args['description__like'] ) ) {
    498             $this->sql_clauses['where']['description__like'] = $wpdb->prepare( "tt.description LIKE %s", '%' . $wpdb->esc_like( $args['description__like'] ) . '%' );
     503            $this->sql_clauses['where']['description__like'] = $this->db->prepare( "tt.description LIKE %s", '%' . $this->db->esc_like( $args['description__like'] ) . '%' );
    499504        }
    500505
     
    592597        $fields = implode( ', ', apply_filters( 'get_terms_fields', $selects, $args, $taxonomies ) );
    593598
    594         $join .= " INNER JOIN $wpdb->term_taxonomy AS tt ON t.term_id = tt.term_id";
     599        $join .= " INNER JOIN {$this->db->term_taxonomy} AS tt ON t.term_id = tt.term_id";
    595600
    596601        $where = implode( ' AND ', $this->sql_clauses['where'] );
     
    622627
    623628        $this->sql_clauses['select']  = "SELECT $distinct $fields";
    624         $this->sql_clauses['from']    = "FROM $wpdb->terms AS t $join";
     629        $this->sql_clauses['from']    = "FROM {$this->db->terms} AS t $join";
    625630        $this->sql_clauses['orderby'] = $orderby ? "$orderby $order" : '';
    626631        $this->sql_clauses['limits']  = $limits;
     
    647652
    648653        if ( 'count' == $_fields ) {
    649             return $wpdb->get_var( $this->request );
    650         }
    651 
    652         $terms = $wpdb->get_results( $this->request );
     654            return $this->db->get_var( $this->request );
     655        }
     656
     657        $terms = $this->db->get_results( $this->request );
    653658        if ( 'all' == $_fields ) {
    654659            update_term_cache( $terms );
     
    753758     * @since 4.6.0
    754759     * @access protected
    755      *
    756      * @global wpdb $wpdb WordPress database abstraction object.
    757760     *
    758761     * @param string $orderby_raw Alias for the field to order by.
     
    895898     * @access protected
    896899     *
    897      * @global wpdb $wpdb WordPress database abstraction object.
    898      *
    899900     * @param string $string
    900901     * @return string
    901902     */
    902903    protected function get_search_sql( $string ) {
    903         global $wpdb;
    904 
    905         $like = '%' . $wpdb->esc_like( $string ) . '%';
    906 
    907         return $wpdb->prepare( '((t.name LIKE %s) OR (t.slug LIKE %s))', $like, $like );
     904        $like = '%' . $this->db->esc_like( $string ) . '%';
     905
     906        return $this->db->prepare( '((t.name LIKE %s) OR (t.slug LIKE %s))', $like, $like );
    908907    }
    909908}
  • trunk/src/wp-includes/class-wp-user-query.php

    r37492 r38275  
    7272
    7373    /**
     74     * @since 4.7.0
     75     * @access protected
     76     * @var wpdb
     77     */
     78    protected $db;
     79
     80    /**
    7481     * PHP5 constructor.
    7582     *
     
    7986     */
    8087    public function __construct( $query = null ) {
     88        $this->db = $GLOBALS['wpdb'];
     89
    8190        if ( ! empty( $query ) ) {
    8291            $this->prepare_query( $query );
     
    135144     * @access public
    136145     *
    137      * @global wpdb $wpdb WordPress database abstraction object.
    138146     * @global int  $blog_id
    139147     *
     
    199207     */
    200208    public function prepare_query( $query = array() ) {
    201         global $wpdb;
    202 
    203209        if ( empty( $this->query_vars ) || ! empty( $query ) ) {
    204210            $this->query_limit = null;
     
    229235            foreach ( $qv['fields'] as $field ) {
    230236                $field = 'ID' === $field ? 'ID' : sanitize_key( $field );
    231                 $this->query_fields[] = "$wpdb->users.$field";
     237                $this->query_fields[] = "{$this->db->users}.$field";
    232238            }
    233239            $this->query_fields = implode( ',', $this->query_fields );
    234240        } elseif ( 'all' == $qv['fields'] ) {
    235             $this->query_fields = "$wpdb->users.*";
     241            $this->query_fields = "{$this->db->users}.*";
    236242        } else {
    237             $this->query_fields = "$wpdb->users.ID";
     243            $this->query_fields = "{$this->db->users}.ID";
    238244        }
    239245
     
    241247            $this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields;
    242248
    243         $this->query_from = "FROM $wpdb->users";
     249        $this->query_from = "FROM {$this->db->users}";
    244250        $this->query_where = "WHERE 1=1";
    245251
     
    264270
    265271            foreach ( $post_types as &$post_type ) {
    266                 $post_type = $wpdb->prepare( '%s', $post_type );
    267             }
    268 
    269             $posts_table = $wpdb->get_blog_prefix( $blog_id ) . 'posts';
    270             $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 ) . " ) )";
     272                $post_type = $this->db->prepare( '%s', $post_type );
     273            }
     274
     275            $posts_table = $this->db->get_blog_prefix( $blog_id ) . 'posts';
     276            $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 ) . " ) )";
    271277        }
    272278
     
    277283        if ( isset( $qv['who'] ) && 'authors' == $qv['who'] && $blog_id ) {
    278284            $who_query = array(
    279                 'key' => $wpdb->get_blog_prefix( $blog_id ) . 'user_level',
     285                'key' => $this->db->get_blog_prefix( $blog_id ) . 'user_level',
    280286                'value' => 0,
    281287                'compare' => '!=',
     
    324330                foreach ( $roles as $role ) {
    325331                    $roles_clauses[] = array(
    326                         'key'     => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
     332                        'key'     => $this->db->get_blog_prefix( $blog_id ) . 'capabilities',
    327333                        'value'   => '"' . $role . '"',
    328334                        'compare' => 'LIKE',
     
    337343                foreach ( $role__in as $role ) {
    338344                    $role__in_clauses[] = array(
    339                         'key'     => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
     345                        'key'     => $this->db->get_blog_prefix( $blog_id ) . 'capabilities',
    340346                        'value'   => '"' . $role . '"',
    341347                        'compare' => 'LIKE',
     
    350356                foreach ( $role__not_in as $role ) {
    351357                    $role__not_in_clauses[] = array(
    352                         'key'     => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
     358                        'key'     => $this->db->get_blog_prefix( $blog_id ) . 'capabilities',
    353359                        'value'   => '"' . $role . '"',
    354360                        'compare' => 'NOT LIKE',
     
    362368            if ( empty( $role_queries ) ) {
    363369                $role_queries[] = array(
    364                     'key' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
     370                    'key' => $this->db->get_blog_prefix( $blog_id ) . 'capabilities',
    365371                    'compare' => 'EXISTS',
    366372                );
     
    384390
    385391        if ( ! empty( $this->meta_query->queries ) ) {
    386             $clauses = $this->meta_query->get_sql( 'user', $wpdb->users, 'ID', $this );
     392            $clauses = $this->meta_query->get_sql( 'user', $this->db->users, 'ID', $this );
    387393            $this->query_from .= $clauses['join'];
    388394            $this->query_where .= $clauses['where'];
     
    442448        if ( isset( $qv['number'] ) && $qv['number'] > 0 ) {
    443449            if ( $qv['offset'] ) {
    444                 $this->query_limit = $wpdb->prepare("LIMIT %d, %d", $qv['offset'], $qv['number']);
     450                $this->query_limit = $this->db->prepare("LIMIT %d, %d", $qv['offset'], $qv['number']);
    445451            } else {
    446                 $this->query_limit = $wpdb->prepare( "LIMIT %d, %d", $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] );
     452                $this->query_limit = $this->db->prepare( "LIMIT %d, %d", $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] );
    447453            }
    448454        }
     
    500506            // Sanitized earlier.
    501507            $ids = implode( ',', $include );
    502             $this->query_where .= " AND $wpdb->users.ID IN ($ids)";
     508            $this->query_where .= " AND {$this->db->users}.ID IN ($ids)";
    503509        } elseif ( ! empty( $qv['exclude'] ) ) {
    504510            $ids = implode( ',', wp_parse_id_list( $qv['exclude'] ) );
    505             $this->query_where .= " AND $wpdb->users.ID NOT IN ($ids)";
     511            $this->query_where .= " AND {$this->db->users}.ID NOT IN ($ids)";
    506512        }
    507513
     
    531537     *
    532538     * @since 3.1.0
    533      *
    534      * @global wpdb $wpdb WordPress database abstraction object.
    535539     */
    536540    public function query() {
    537         global $wpdb;
    538 
    539541        $qv =& $this->query_vars;
    540542
     
    542544
    543545        if ( is_array( $qv['fields'] ) || 'all' == $qv['fields'] ) {
    544             $this->results = $wpdb->get_results( $this->request );
     546            $this->results = $this->db->get_results( $this->request );
    545547        } else {
    546             $this->results = $wpdb->get_col( $this->request );
     548            $this->results = $this->db->get_col( $this->request );
    547549        }
    548550
     
    552554         * @since 3.2.0
    553555         *
    554          * @global wpdb $wpdb WordPress database abstraction object.
    555          *
    556556         * @param string $sql The SELECT FOUND_ROWS() query for the current WP_User_Query.
    557557         */
    558         if ( isset( $qv['count_total'] ) && $qv['count_total'] )
    559             $this->total_users = $wpdb->get_var( apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()' ) );
    560 
    561         if ( !$this->results )
     558        if ( isset( $qv['count_total'] ) && $qv['count_total'] ) {
     559            $this->total_users = $this->db->get_var( apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()' ) );
     560        }
     561
     562        if ( ! $this->results ) {
    562563            return;
     564        }
    563565
    564566        if ( 'all_with_meta' == $qv['fields'] ) {
     
    611613     * @access protected
    612614     * @since 3.1.0
    613      *
    614      * @global wpdb $wpdb WordPress database abstraction object.
    615615     *
    616616     * @param string $string
     
    621621     */
    622622    protected function get_search_sql( $string, $cols, $wild = false ) {
    623         global $wpdb;
    624 
    625623        $searches = array();
    626624        $leading_wild = ( 'leading' == $wild || 'both' == $wild ) ? '%' : '';
    627625        $trailing_wild = ( 'trailing' == $wild || 'both' == $wild ) ? '%' : '';
    628         $like = $leading_wild . $wpdb->esc_like( $string ) . $trailing_wild;
     626        $like = $leading_wild . $this->db->esc_like( $string ) . $trailing_wild;
    629627
    630628        foreach ( $cols as $col ) {
    631629            if ( 'ID' == $col ) {
    632                 $searches[] = $wpdb->prepare( "$col = %s", $string );
     630                $searches[] = $this->db->prepare( "$col = %s", $string );
    633631            } else {
    634                 $searches[] = $wpdb->prepare( "$col LIKE %s", $like );
     632                $searches[] = $this->db->prepare( "$col LIKE %s", $like );
    635633            }
    636634        }
     
    669667     * @access protected
    670668     *
    671      * @global wpdb $wpdb WordPress database abstraction object.
    672      *
    673669     * @param string $orderby Alias for the field to order by.
    674670     * @return string Value to used in the ORDER clause, if `$orderby` is valid.
    675671     */
    676672    protected function parse_orderby( $orderby ) {
    677         global $wpdb;
    678 
    679673        $meta_query_clauses = $this->meta_query->get_clauses();
    680674
     
    691685            $this->query_from .= " LEFT OUTER JOIN (
    692686                SELECT post_author, COUNT(*) as post_count
    693                 FROM $wpdb->posts
     687                FROM {$this->db->posts}
    694688                $where
    695689                GROUP BY post_author
    696             ) p ON ({$wpdb->users}.ID = p.post_author)
     690            ) p ON ({$this->db->users}.ID = p.post_author)
    697691            ";
    698692            $_orderby = 'post_count';
     
    700694            $_orderby = 'ID';
    701695        } elseif ( 'meta_value' == $orderby || $this->get( 'meta_key' ) == $orderby ) {
    702             $_orderby = "$wpdb->usermeta.meta_value";
     696            $_orderby = "{$this->db->usermeta}.meta_value";
    703697        } elseif ( 'meta_value_num' == $orderby ) {
    704             $_orderby = "$wpdb->usermeta.meta_value+0";
     698            $_orderby = "{$this->db->usermeta}.meta_value+0";
    705699        } elseif ( 'include' === $orderby && ! empty( $this->query_vars['include'] ) ) {
    706700            $include = wp_parse_id_list( $this->query_vars['include'] );
    707701            $include_sql = implode( ',', $include );
    708             $_orderby = "FIELD( $wpdb->users.ID, $include_sql )";
     702            $_orderby = "FIELD( {$this->db->users}.ID, $include_sql )";
    709703        } elseif ( isset( $meta_query_clauses[ $orderby ] ) ) {
    710704            $meta_clause = $meta_query_clauses[ $orderby ];
  • trunk/src/wp-includes/class-wp-user.php

    r37985 r38275  
    103103
    104104    /**
     105     * @since 4.7.0
     106     * @access protected
     107     * @var wpdb
     108     */
     109    protected $db;
     110
     111    /**
    105112     * Constructor.
    106113     *
     
    109116     * @since 2.0.0
    110117     * @access public
    111      *
    112      * @global wpdb $wpdb WordPress database abstraction object.
    113118     *
    114119     * @param int|string|stdClass|WP_User $id User's ID, a WP_User object, or a user object from the DB.
     
    117122     */
    118123    public function __construct( $id = 0, $name = '', $blog_id = '' ) {
     124        $this->db = $GLOBALS['wpdb'];
     125
    119126        if ( ! isset( self::$back_compat_keys ) ) {
    120             $prefix = $GLOBALS['wpdb']->prefix;
     127            $prefix = $this->db->prefix;
    121128            self::$back_compat_keys = array(
    122129                'user_firstname' => 'first_name',
     
    233240
    234241        if ( !$user = $wpdb->get_row( $wpdb->prepare(
    235             "SELECT * FROM $wpdb->users WHERE $db_field = %s", $value
    236         ) ) )
     242            "SELECT * FROM {$wpdb->users} WHERE $db_field = %s", $value
     243        ) ) ) {
    237244            return false;
    238 
     245        }
    239246        update_user_caches( $user );
    240247
     
    443450     * @since 2.1.0
    444451     *
    445      * @global wpdb $wpdb WordPress database abstraction object.
    446      *
    447452     * @param string $cap_key Optional capability key
    448453     */
    449454    protected function _init_caps( $cap_key = '' ) {
    450         global $wpdb;
    451 
    452         if ( empty($cap_key) )
    453             $this->cap_key = $wpdb->get_blog_prefix() . 'capabilities';
    454         else
     455        if ( empty( $cap_key ) ) {
     456            $this->cap_key = $this->db->get_blog_prefix() . 'capabilities';
     457        } else {
    455458            $this->cap_key = $cap_key;
    456 
     459        }
    457460        $this->caps = get_user_meta( $this->ID, $this->cap_key, true );
    458461
     
    632635     * @since 2.0.0
    633636     * @access public
    634      *
    635      * @global wpdb $wpdb WordPress database abstraction object.
    636637     */
    637638    public function update_user_level_from_caps() {
    638         global $wpdb;
    639639        $this->user_level = array_reduce( array_keys( $this->allcaps ), array( $this, 'level_reduction' ), 0 );
    640         update_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level', $this->user_level );
     640        update_user_meta( $this->ID, $this->db->get_blog_prefix() . 'user_level', $this->user_level );
    641641    }
    642642
     
    680680     * @since 2.1.0
    681681     * @access public
    682      *
    683      * @global wpdb $wpdb WordPress database abstraction object.
    684682     */
    685683    public function remove_all_caps() {
    686         global $wpdb;
    687684        $this->caps = array();
    688685        delete_user_meta( $this->ID, $this->cap_key );
    689         delete_user_meta( $this->ID, $wpdb->get_blog_prefix() . 'user_level' );
     686        delete_user_meta( $this->ID, $this->db->get_blog_prefix() . 'user_level' );
    690687        $this->get_role_caps();
    691688    }
     
    773770     * @since 3.0.0
    774771     *
    775      * @global wpdb $wpdb WordPress database abstraction object.
    776      *
    777772     * @param int $blog_id Optional. Site ID, defaults to current site.
    778773     */
    779774    public function for_blog( $blog_id = '' ) {
    780         global $wpdb;
    781         if ( ! empty( $blog_id ) )
    782             $cap_key = $wpdb->get_blog_prefix( $blog_id ) . 'capabilities';
    783         else
     775        if ( ! empty( $blog_id ) ) {
     776            $cap_key = $this->db->get_blog_prefix( $blog_id ) . 'capabilities';
     777        } else {
    784778            $cap_key = '';
     779        }
    785780        $this->_init_caps( $cap_key );
    786781    }
  • trunk/src/wp-includes/class-wp-xmlrpc-server.php

    r38077 r38275  
    5555
    5656    /**
     57     * @since 4.7.0
     58     * @access protected
     59     * @var wpdb
     60     */
     61    protected $db;
     62
     63    /**
    5764     * Registers all of the XMLRPC methods that XMLRPC server understands.
    5865     *
     
    6471     */
    6572    public function __construct() {
     73        $this->db = $GLOBALS['wpdb'];
     74
    6675        $this->methods = array(
    6776            // WordPress API
     
    28832892     * @since 2.2.0
    28842893     *
    2885      * @global wpdb $wpdb WordPress database abstraction object.
    2886      *
    28872894     * @param array  $args {
    28882895     *     Method arguments. Note: arguments must be ordered as documented.
     
    28952902     */
    28962903    public function wp_getPageList( $args ) {
    2897         global $wpdb;
    2898 
    28992904        $this->escape( $args );
    29002905
     
    29122917
    29132918        // Get list of pages ids and titles
    2914         $page_list = $wpdb->get_results("
     2919        $page_list = $this->db->get_results("
    29152920            SELECT ID page_id,
    29162921                post_title page_title,
     
    29192924                post_date,
    29202925                post_status
    2921             FROM {$wpdb->posts}
     2926            FROM {$this->db->posts}
    29222927            WHERE post_type = 'page'
    29232928            ORDER BY ID
     
    51315136     * @since 2.1.0
    51325137     *
    5133      * @global wpdb $wpdb WordPress database abstraction object.
    5134      *
    51355138     * @param int $post_ID Post ID.
    51365139     * @param string $post_content Post Content for attachment.
    51375140     */
    51385141    public function attach_uploads( $post_ID, $post_content ) {
    5139         global $wpdb;
    5140 
    51415142        // find any unattached files
    5142         $attachments = $wpdb->get_results( "SELECT ID, guid FROM {$wpdb->posts} WHERE post_parent = '0' AND post_type = 'attachment'" );
     5143        $attachments = $this->db->get_results( "SELECT ID, guid FROM {$this->db->posts} WHERE post_parent = '0' AND post_type = 'attachment'" );
    51435144        if ( is_array( $attachments ) ) {
    51445145            foreach ( $attachments as $file ) {
    5145                 if ( ! empty( $file->guid ) && strpos( $post_content, $file->guid ) !== false )
    5146                     $wpdb->update($wpdb->posts, array('post_parent' => $post_ID), array('ID' => $file->ID) );
     5146                if ( ! empty( $file->guid ) && strpos( $post_content, $file->guid ) !== false ) {
     5147                    $this->db->update( $this->db->posts, array( 'post_parent' => $post_ID ), array( 'ID' => $file->ID ) );
     5148                }
    51475149            }
    51485150        }
     
    57645766     * @since 1.5.0
    57655767     *
    5766      * @global wpdb $wpdb WordPress database abstraction object.
    5767      *
    57685768     * @param array  $args {
    57695769     *     Method arguments. Note: arguments must be ordered as documented.
     
    57775777     */
    57785778    public function mw_newMediaObject( $args ) {
    5779         global $wpdb;
    5780 
    57815779        $username = $this->escape( $args[1] );
    57825780        $password = $this->escape( $args[2] );
     
    61036101     * @since 1.5.0
    61046102     *
    6105      * @global wpdb $wpdb WordPress database abstraction object.
    6106      *
    61076103     * @param int $post_ID
    61086104     * @return array|IXR_Error
    61096105     */
    61106106    public function mt_getTrackbackPings( $post_ID ) {
    6111         global $wpdb;
    6112 
    61136107        /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
    61146108        do_action( 'xmlrpc_call', 'mt.getTrackbackPings' );
     
    61196113            return new IXR_Error(404, __('Sorry, no such post.'));
    61206114
    6121         $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) );
     6115        $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) );
    61226116
    61236117        if ( !$comments )
     
    61936187     * @since 1.5.0
    61946188     *
    6195      * @global wpdb $wpdb WordPress database abstraction object.
    61966189     * @global string $wp_version
    61976190     *
     
    62056198     */
    62066199    public function pingback_ping( $args ) {
    6207         global $wpdb, $wp_version;
     6200        global $wp_version;
    62086201
    62096202        /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
     
    62596252                // ...or a string #title, a little more complicated
    62606253                $title = preg_replace('/[^a-z0-9]/i', '.', $urltest['fragment']);
    6261                 $sql = $wpdb->prepare("SELECT ID FROM $wpdb->posts WHERE post_title RLIKE %s", $title );
    6262                 if (! ($post_ID = $wpdb->get_var($sql)) ) {
     6254                $sql = $this->db->prepare("SELECT ID FROM {$this->db->posts} WHERE post_title RLIKE %s", $title );
     6255                if (! ($post_ID = $this->db->get_var($sql)) ) {
    62636256                    // returning unknown error '0' is better than die()ing
    62646257                    return $this->pingback_error( 0, '' );
     
    62846277
    62856278        // Let's check that the remote site didn't already pingback this entry
    6286         if ( $wpdb->get_results( $wpdb->prepare("SELECT * FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_author_url = %s", $post_ID, $pagelinkedfrom) ) )
     6279        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) ) )
    62876280            return $this->pingback_error( 48, __( 'The pingback has already been registered.' ) );
    62886281
     
    64096402     * @since 1.5.0
    64106403     *
    6411      * @global wpdb $wpdb WordPress database abstraction object.
    6412      *
    64136404     * @param string $url
    64146405     * @return array|IXR_Error
    64156406     */
    64166407    public function pingback_extensions_getPingbacks( $url ) {
    6417         global $wpdb;
    6418 
    64196408        /** This action is documented in wp-includes/class-wp-xmlrpc-server.php */
    64206409        do_action( 'xmlrpc_call', 'pingback.extensions.getPingbacks' );
     
    64356424        }
    64366425
    6437         $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) );
     6426        $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) );
    64386427
    64396428        if ( !$comments )
Note: See TracChangeset for help on using the changeset viewer.