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