Make WordPress Core


Ignore:
Timestamp:
08/18/2016 06:20:55 PM (10 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.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • 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':
Note: See TracChangeset for help on using the changeset viewer.