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-user-query.php

    r37492 r38275  
    7272
    7373        /**
     74         * @since 4.7.0
     75         * @access protected
     76         * @var wpdb
     77         */
     78        protected $db;
     79
     80        /**
    7481         * PHP5 constructor.
    7582         *
     
    7986         */
    8087        public function __construct( $query = null ) {
     88                $this->db = $GLOBALS['wpdb'];
     89
    8190                if ( ! empty( $query ) ) {
    8291                        $this->prepare_query( $query );
     
    135144         * @access public
    136145         *
    137          * @global wpdb $wpdb WordPress database abstraction object.
    138146         * @global int  $blog_id
    139147         *
     
    199207         */
    200208        public function prepare_query( $query = array() ) {
    201                 global $wpdb;
    202 
    203209                if ( empty( $this->query_vars ) || ! empty( $query ) ) {
    204210                        $this->query_limit = null;
     
    229235                        foreach ( $qv['fields'] as $field ) {
    230236                                $field = 'ID' === $field ? 'ID' : sanitize_key( $field );
    231                                 $this->query_fields[] = "$wpdb->users.$field";
     237                                $this->query_fields[] = "{$this->db->users}.$field";
    232238                        }
    233239                        $this->query_fields = implode( ',', $this->query_fields );
    234240                } elseif ( 'all' == $qv['fields'] ) {
    235                         $this->query_fields = "$wpdb->users.*";
     241                        $this->query_fields = "{$this->db->users}.*";
    236242                } else {
    237                         $this->query_fields = "$wpdb->users.ID";
     243                        $this->query_fields = "{$this->db->users}.ID";
    238244                }
    239245
     
    241247                        $this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields;
    242248
    243                 $this->query_from = "FROM $wpdb->users";
     249                $this->query_from = "FROM {$this->db->users}";
    244250                $this->query_where = "WHERE 1=1";
    245251
     
    264270
    265271                        foreach ( $post_types as &$post_type ) {
    266                                 $post_type = $wpdb->prepare( '%s', $post_type );
    267                         }
    268 
    269                         $posts_table = $wpdb->get_blog_prefix( $blog_id ) . 'posts';
    270                         $this->query_where .= " AND $wpdb->users.ID IN ( SELECT DISTINCT $posts_table.post_author FROM $posts_table WHERE $posts_table.post_status = 'publish' AND $posts_table.post_type IN ( " . join( ", ", $post_types ) . " ) )";
     272                                $post_type = $this->db->prepare( '%s', $post_type );
     273                        }
     274
     275                        $posts_table = $this->db->get_blog_prefix( $blog_id ) . 'posts';
     276                        $this->query_where .= " AND {$this->db->users}.ID IN ( SELECT DISTINCT $posts_table.post_author FROM $posts_table WHERE $posts_table.post_status = 'publish' AND $posts_table.post_type IN ( " . join( ", ", $post_types ) . " ) )";
    271277                }
    272278
     
    277283                if ( isset( $qv['who'] ) && 'authors' == $qv['who'] && $blog_id ) {
    278284                        $who_query = array(
    279                                 'key' => $wpdb->get_blog_prefix( $blog_id ) . 'user_level',
     285                                'key' => $this->db->get_blog_prefix( $blog_id ) . 'user_level',
    280286                                'value' => 0,
    281287                                'compare' => '!=',
     
    324330                                foreach ( $roles as $role ) {
    325331                                        $roles_clauses[] = array(
    326                                                 'key'     => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
     332                                                'key'     => $this->db->get_blog_prefix( $blog_id ) . 'capabilities',
    327333                                                'value'   => '"' . $role . '"',
    328334                                                'compare' => 'LIKE',
     
    337343                                foreach ( $role__in as $role ) {
    338344                                        $role__in_clauses[] = array(
    339                                                 'key'     => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
     345                                                'key'     => $this->db->get_blog_prefix( $blog_id ) . 'capabilities',
    340346                                                'value'   => '"' . $role . '"',
    341347                                                'compare' => 'LIKE',
     
    350356                                foreach ( $role__not_in as $role ) {
    351357                                        $role__not_in_clauses[] = array(
    352                                                 'key'     => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
     358                                                'key'     => $this->db->get_blog_prefix( $blog_id ) . 'capabilities',
    353359                                                'value'   => '"' . $role . '"',
    354360                                                'compare' => 'NOT LIKE',
     
    362368                        if ( empty( $role_queries ) ) {
    363369                                $role_queries[] = array(
    364                                         'key' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
     370                                        'key' => $this->db->get_blog_prefix( $blog_id ) . 'capabilities',
    365371                                        'compare' => 'EXISTS',
    366372                                );
     
    384390
    385391                if ( ! empty( $this->meta_query->queries ) ) {
    386                         $clauses = $this->meta_query->get_sql( 'user', $wpdb->users, 'ID', $this );
     392                        $clauses = $this->meta_query->get_sql( 'user', $this->db->users, 'ID', $this );
    387393                        $this->query_from .= $clauses['join'];
    388394                        $this->query_where .= $clauses['where'];
     
    442448                if ( isset( $qv['number'] ) && $qv['number'] > 0 ) {
    443449                        if ( $qv['offset'] ) {
    444                                 $this->query_limit = $wpdb->prepare("LIMIT %d, %d", $qv['offset'], $qv['number']);
     450                                $this->query_limit = $this->db->prepare("LIMIT %d, %d", $qv['offset'], $qv['number']);
    445451                        } else {
    446                                 $this->query_limit = $wpdb->prepare( "LIMIT %d, %d", $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] );
     452                                $this->query_limit = $this->db->prepare( "LIMIT %d, %d", $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] );
    447453                        }
    448454                }
     
    500506                        // Sanitized earlier.
    501507                        $ids = implode( ',', $include );
    502                         $this->query_where .= " AND $wpdb->users.ID IN ($ids)";
     508                        $this->query_where .= " AND {$this->db->users}.ID IN ($ids)";
    503509                } elseif ( ! empty( $qv['exclude'] ) ) {
    504510                        $ids = implode( ',', wp_parse_id_list( $qv['exclude'] ) );
    505                         $this->query_where .= " AND $wpdb->users.ID NOT IN ($ids)";
     511                        $this->query_where .= " AND {$this->db->users}.ID NOT IN ($ids)";
    506512                }
    507513
     
    531537         *
    532538         * @since 3.1.0
    533          *
    534          * @global wpdb $wpdb WordPress database abstraction object.
    535539         */
    536540        public function query() {
    537                 global $wpdb;
    538 
    539541                $qv =& $this->query_vars;
    540542
     
    542544
    543545                if ( is_array( $qv['fields'] ) || 'all' == $qv['fields'] ) {
    544                         $this->results = $wpdb->get_results( $this->request );
     546                        $this->results = $this->db->get_results( $this->request );
    545547                } else {
    546                         $this->results = $wpdb->get_col( $this->request );
     548                        $this->results = $this->db->get_col( $this->request );
    547549                }
    548550
     
    552554                 * @since 3.2.0
    553555                 *
    554                  * @global wpdb $wpdb WordPress database abstraction object.
    555                  *
    556556                 * @param string $sql The SELECT FOUND_ROWS() query for the current WP_User_Query.
    557557                 */
    558                 if ( isset( $qv['count_total'] ) && $qv['count_total'] )
    559                         $this->total_users = $wpdb->get_var( apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()' ) );
    560 
    561                 if ( !$this->results )
     558                if ( isset( $qv['count_total'] ) && $qv['count_total'] ) {
     559                        $this->total_users = $this->db->get_var( apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()' ) );
     560                }
     561
     562                if ( ! $this->results ) {
    562563                        return;
     564                }
    563565
    564566                if ( 'all_with_meta' == $qv['fields'] ) {
     
    611613         * @access protected
    612614         * @since 3.1.0
    613          *
    614          * @global wpdb $wpdb WordPress database abstraction object.
    615615         *
    616616         * @param string $string
     
    621621         */
    622622        protected function get_search_sql( $string, $cols, $wild = false ) {
    623                 global $wpdb;
    624 
    625623                $searches = array();
    626624                $leading_wild = ( 'leading' == $wild || 'both' == $wild ) ? '%' : '';
    627625                $trailing_wild = ( 'trailing' == $wild || 'both' == $wild ) ? '%' : '';
    628                 $like = $leading_wild . $wpdb->esc_like( $string ) . $trailing_wild;
     626                $like = $leading_wild . $this->db->esc_like( $string ) . $trailing_wild;
    629627
    630628                foreach ( $cols as $col ) {
    631629                        if ( 'ID' == $col ) {
    632                                 $searches[] = $wpdb->prepare( "$col = %s", $string );
     630                                $searches[] = $this->db->prepare( "$col = %s", $string );
    633631                        } else {
    634                                 $searches[] = $wpdb->prepare( "$col LIKE %s", $like );
     632                                $searches[] = $this->db->prepare( "$col LIKE %s", $like );
    635633                        }
    636634                }
     
    669667         * @access protected
    670668         *
    671          * @global wpdb $wpdb WordPress database abstraction object.
    672          *
    673669         * @param string $orderby Alias for the field to order by.
    674670         * @return string Value to used in the ORDER clause, if `$orderby` is valid.
    675671         */
    676672        protected function parse_orderby( $orderby ) {
    677                 global $wpdb;
    678 
    679673                $meta_query_clauses = $this->meta_query->get_clauses();
    680674
     
    691685                        $this->query_from .= " LEFT OUTER JOIN (
    692686                                SELECT post_author, COUNT(*) as post_count
    693                                 FROM $wpdb->posts
     687                                FROM {$this->db->posts}
    694688                                $where
    695689                                GROUP BY post_author
    696                         ) p ON ({$wpdb->users}.ID = p.post_author)
     690                        ) p ON ({$this->db->users}.ID = p.post_author)
    697691                        ";
    698692                        $_orderby = 'post_count';
     
    700694                        $_orderby = 'ID';
    701695                } elseif ( 'meta_value' == $orderby || $this->get( 'meta_key' ) == $orderby ) {
    702                         $_orderby = "$wpdb->usermeta.meta_value";
     696                        $_orderby = "{$this->db->usermeta}.meta_value";
    703697                } elseif ( 'meta_value_num' == $orderby ) {
    704                         $_orderby = "$wpdb->usermeta.meta_value+0";
     698                        $_orderby = "{$this->db->usermeta}.meta_value+0";
    705699                } elseif ( 'include' === $orderby && ! empty( $this->query_vars['include'] ) ) {
    706700                        $include = wp_parse_id_list( $this->query_vars['include'] );
    707701                        $include_sql = implode( ',', $include );
    708                         $_orderby = "FIELD( $wpdb->users.ID, $include_sql )";
     702                        $_orderby = "FIELD( {$this->db->users}.ID, $include_sql )";
    709703                } elseif ( isset( $meta_query_clauses[ $orderby ] ) ) {
    710704                        $meta_clause = $meta_query_clauses[ $orderby ];
Note: See TracChangeset for help on using the changeset viewer.