Make WordPress Core


Ignore:
Timestamp:
10/10/2016 06:37:02 AM (10 years ago)
Author:
pento
Message:

General: Restore usage of $wpdb, instead of $this->db.

Hiding the $wpdb global behind a property decreases the readability of the code, as well as causing irrelevant output when dumping an object.

Reverts [38275], [38278], [38279], [38280], [38387].
See #37699.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • trunk/src/wp-includes/class-wp-user-query.php

    r38715 r38768  
    7272
    7373        /**
    74          * @since 4.7.0
    75          * @access protected
    76          * @var wpdb
    77          */
    78         protected $db;
    79 
    80         /**
    8174         * PHP5 constructor.
    8275         *
     
    8679         */
    8780        public function __construct( $query = null ) {
    88                 $this->db = $GLOBALS['wpdb'];
    89 
    9081                if ( ! empty( $query ) ) {
    9182                        $this->prepare_query( $query );
     
    152143         * @access public
    153144         *
     145         * @global wpdb $wpdb WordPress database abstraction object.
    154146         * @global int  $blog_id
    155147         *
     
    225217         */
    226218        public function prepare_query( $query = array() ) {
     219                global $wpdb;
     220
    227221                if ( empty( $this->query_vars ) || ! empty( $query ) ) {
    228222                        $this->query_limit = null;
     
    253247                        foreach ( $qv['fields'] as $field ) {
    254248                                $field = 'ID' === $field ? 'ID' : sanitize_key( $field );
    255                                 $this->query_fields[] = "{$this->db->users}.$field";
     249                                $this->query_fields[] = "$wpdb->users.$field";
    256250                        }
    257251                        $this->query_fields = implode( ',', $this->query_fields );
    258252                } elseif ( 'all' == $qv['fields'] ) {
    259                         $this->query_fields = "{$this->db->users}.*";
     253                        $this->query_fields = "$wpdb->users.*";
    260254                } else {
    261                         $this->query_fields = "{$this->db->users}.ID";
     255                        $this->query_fields = "$wpdb->users.ID";
    262256                }
    263257
     
    265259                        $this->query_fields = 'SQL_CALC_FOUND_ROWS ' . $this->query_fields;
    266260
    267                 $this->query_from = "FROM {$this->db->users}";
     261                $this->query_from = "FROM $wpdb->users";
    268262                $this->query_where = "WHERE 1=1";
    269263
     
    288282
    289283                        foreach ( $post_types as &$post_type ) {
    290                                 $post_type = $this->db->prepare( '%s', $post_type );
    291                         }
    292 
    293                         $posts_table = $this->db->get_blog_prefix( $blog_id ) . 'posts';
    294                         $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 ) . " ) )";
     284                                $post_type = $wpdb->prepare( '%s', $post_type );
     285                        }
     286
     287                        $posts_table = $wpdb->get_blog_prefix( $blog_id ) . 'posts';
     288                        $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 ) . " ) )";
    295289                }
    296290
    297291                // nicename
    298292                if ( '' !== $qv['nicename']) {
    299                         $this->query_where .= $this->db->prepare( ' AND user_nicename = %s', $qv['nicename'] );
     293                        $this->query_where .= $wpdb->prepare( ' AND user_nicename = %s', $qv['nicename'] );
    300294                }
    301295
     
    314308                // login
    315309                if ( '' !== $qv['login']) {
    316                         $this->query_where .= $this->db->prepare( ' AND user_login = %s', $qv['login'] );
     310                        $this->query_where .= $wpdb->prepare( ' AND user_login = %s', $qv['login'] );
    317311                }
    318312
     
    335329                if ( isset( $qv['who'] ) && 'authors' == $qv['who'] && $blog_id ) {
    336330                        $who_query = array(
    337                                 'key' => $this->db->get_blog_prefix( $blog_id ) . 'user_level',
     331                                'key' => $wpdb->get_blog_prefix( $blog_id ) . 'user_level',
    338332                                'value' => 0,
    339333                                'compare' => '!=',
     
    382376                                foreach ( $roles as $role ) {
    383377                                        $roles_clauses[] = array(
    384                                                 'key'     => $this->db->get_blog_prefix( $blog_id ) . 'capabilities',
     378                                                'key'     => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
    385379                                                'value'   => '"' . $role . '"',
    386380                                                'compare' => 'LIKE',
     
    395389                                foreach ( $role__in as $role ) {
    396390                                        $role__in_clauses[] = array(
    397                                                 'key'     => $this->db->get_blog_prefix( $blog_id ) . 'capabilities',
     391                                                'key'     => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
    398392                                                'value'   => '"' . $role . '"',
    399393                                                'compare' => 'LIKE',
     
    408402                                foreach ( $role__not_in as $role ) {
    409403                                        $role__not_in_clauses[] = array(
    410                                                 'key'     => $this->db->get_blog_prefix( $blog_id ) . 'capabilities',
     404                                                'key'     => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
    411405                                                'value'   => '"' . $role . '"',
    412406                                                'compare' => 'NOT LIKE',
     
    420414                        if ( empty( $role_queries ) ) {
    421415                                $role_queries[] = array(
    422                                         'key' => $this->db->get_blog_prefix( $blog_id ) . 'capabilities',
     416                                        'key' => $wpdb->get_blog_prefix( $blog_id ) . 'capabilities',
    423417                                        'compare' => 'EXISTS',
    424418                                );
     
    442436
    443437                if ( ! empty( $this->meta_query->queries ) ) {
    444                         $clauses = $this->meta_query->get_sql( 'user', $this->db->users, 'ID', $this );
     438                        $clauses = $this->meta_query->get_sql( 'user', $wpdb->users, 'ID', $this );
    445439                        $this->query_from .= $clauses['join'];
    446440                        $this->query_where .= $clauses['where'];
     
    504498                if ( isset( $qv['number'] ) && $qv['number'] > 0 ) {
    505499                        if ( $qv['offset'] ) {
    506                                 $this->query_limit = $this->db->prepare("LIMIT %d, %d", $qv['offset'], $qv['number']);
     500                                $this->query_limit = $wpdb->prepare("LIMIT %d, %d", $qv['offset'], $qv['number']);
    507501                        } else {
    508                                 $this->query_limit = $this->db->prepare( "LIMIT %d, %d", $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] );
     502                                $this->query_limit = $wpdb->prepare( "LIMIT %d, %d", $qv['number'] * ( $qv['paged'] - 1 ), $qv['number'] );
    509503                        }
    510504                }
     
    562556                        // Sanitized earlier.
    563557                        $ids = implode( ',', $include );
    564                         $this->query_where .= " AND {$this->db->users}.ID IN ($ids)";
     558                        $this->query_where .= " AND $wpdb->users.ID IN ($ids)";
    565559                } elseif ( ! empty( $qv['exclude'] ) ) {
    566560                        $ids = implode( ',', wp_parse_id_list( $qv['exclude'] ) );
    567                         $this->query_where .= " AND {$this->db->users}.ID NOT IN ($ids)";
     561                        $this->query_where .= " AND $wpdb->users.ID NOT IN ($ids)";
    568562                }
    569563
     
    593587         *
    594588         * @since 3.1.0
     589         *
     590         * @global wpdb $wpdb WordPress database abstraction object.
    595591         */
    596592        public function query() {
     593                global $wpdb;
     594
    597595                $qv =& $this->query_vars;
    598596
     
    600598
    601599                if ( is_array( $qv['fields'] ) || 'all' == $qv['fields'] ) {
    602                         $this->results = $this->db->get_results( $this->request );
     600                        $this->results = $wpdb->get_results( $this->request );
    603601                } else {
    604                         $this->results = $this->db->get_col( $this->request );
     602                        $this->results = $wpdb->get_col( $this->request );
    605603                }
    606604
     
    610608                 * @since 3.2.0
    611609                 *
     610                 * @global wpdb $wpdb WordPress database abstraction object.
     611                 *
    612612                 * @param string $sql The SELECT FOUND_ROWS() query for the current WP_User_Query.
    613613                 */
    614                 if ( isset( $qv['count_total'] ) && $qv['count_total'] ) {
    615                         $this->total_users = $this->db->get_var( apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()' ) );
    616                 }
    617 
    618                 if ( ! $this->results ) {
     614                if ( isset( $qv['count_total'] ) && $qv['count_total'] )
     615                        $this->total_users = $wpdb->get_var( apply_filters( 'found_users_query', 'SELECT FOUND_ROWS()' ) );
     616
     617                if ( !$this->results )
    619618                        return;
    620                 }
    621619
    622620                if ( 'all_with_meta' == $qv['fields'] ) {
     
    669667         * @access protected
    670668         * @since 3.1.0
     669         *
     670         * @global wpdb $wpdb WordPress database abstraction object.
    671671         *
    672672         * @param string $string
     
    677677         */
    678678        protected function get_search_sql( $string, $cols, $wild = false ) {
     679                global $wpdb;
     680
    679681                $searches = array();
    680682                $leading_wild = ( 'leading' == $wild || 'both' == $wild ) ? '%' : '';
    681683                $trailing_wild = ( 'trailing' == $wild || 'both' == $wild ) ? '%' : '';
    682                 $like = $leading_wild . $this->db->esc_like( $string ) . $trailing_wild;
     684                $like = $leading_wild . $wpdb->esc_like( $string ) . $trailing_wild;
    683685
    684686                foreach ( $cols as $col ) {
    685687                        if ( 'ID' == $col ) {
    686                                 $searches[] = $this->db->prepare( "$col = %s", $string );
     688                                $searches[] = $wpdb->prepare( "$col = %s", $string );
    687689                        } else {
    688                                 $searches[] = $this->db->prepare( "$col LIKE %s", $like );
     690                                $searches[] = $wpdb->prepare( "$col LIKE %s", $like );
    689691                        }
    690692                }
     
    723725         * @access protected
    724726         *
     727         * @global wpdb $wpdb WordPress database abstraction object.
     728         *
    725729         * @param string $orderby Alias for the field to order by.
    726730         * @return string Value to used in the ORDER clause, if `$orderby` is valid.
    727731         */
    728732        protected function parse_orderby( $orderby ) {
     733                global $wpdb;
     734
    729735                $meta_query_clauses = $this->meta_query->get_clauses();
    730736
     
    741747                        $this->query_from .= " LEFT OUTER JOIN (
    742748                                SELECT post_author, COUNT(*) as post_count
    743                                 FROM {$this->db->posts}
     749                                FROM $wpdb->posts
    744750                                $where
    745751                                GROUP BY post_author
    746                         ) p ON ({$this->db->users}.ID = p.post_author)
     752                        ) p ON ({$wpdb->users}.ID = p.post_author)
    747753                        ";
    748754                        $_orderby = 'post_count';
     
    750756                        $_orderby = 'ID';
    751757                } elseif ( 'meta_value' == $orderby || $this->get( 'meta_key' ) == $orderby ) {
    752                         $_orderby = "{$this->db->usermeta}.meta_value";
     758                        $_orderby = "$wpdb->usermeta.meta_value";
    753759                } elseif ( 'meta_value_num' == $orderby ) {
    754                         $_orderby = "{$this->db->usermeta}.meta_value+0";
     760                        $_orderby = "$wpdb->usermeta.meta_value+0";
    755761                } elseif ( 'include' === $orderby && ! empty( $this->query_vars['include'] ) ) {
    756762                        $include = wp_parse_id_list( $this->query_vars['include'] );
    757763                        $include_sql = implode( ',', $include );
    758                         $_orderby = "FIELD( {$this->db->users}.ID, $include_sql )";
     764                        $_orderby = "FIELD( $wpdb->users.ID, $include_sql )";
    759765                } elseif ( 'nicename__in' === $orderby ) {
    760766                        $sanitized_nicename__in = array_map( 'esc_sql', $this->query_vars['nicename__in'] );
Note: See TracChangeset for help on using the changeset viewer.