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