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-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}
Note: See TracChangeset for help on using the changeset viewer.