WordPress.org

Make WordPress Core


Ignore:
Timestamp:
08/18/2016 06:20:55 PM (5 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-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
Note: See TracChangeset for help on using the changeset viewer.