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

    r38121 r38275  
    141141                return false;
    142142        }
     143
     144        /**
     145         * @since 4.7.0
     146         * @access protected
     147         * @var wpdb
     148         */
     149        protected $db;
    143150
    144151        /**
     
    261268         */
    262269        public function __construct( $query = '' ) {
     270                $this->db = $GLOBALS['wpdb'];
     271
    263272                $this->query_var_defaults = array(
    264273                        'author_email' => '',
     
    364373         * @access public
    365374         *
    366          * @global wpdb $wpdb WordPress database abstraction object.
    367          *
    368375         * @return int|array List of comments or number of found comments if `$count` argument is true.
    369376         */
    370377        public function get_comments() {
    371                 global $wpdb;
    372 
    373378                $this->parse_query();
    374379
     
    389394                $this->meta_query->parse_query_vars( $this->query_vars );
    390395                if ( ! empty( $this->meta_query->queries ) ) {
    391                         $this->meta_query_clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this );
     396                        $this->meta_query_clauses = $this->meta_query->get_sql( 'comment', $this->db->comments, 'comment_ID', $this );
    392397                }
    393398
     
    481486         * @since 4.4.0
    482487         * @access protected
    483          *
    484          * @global wpdb $wpdb WordPress database abstraction object.
    485488         */
    486489        protected function get_comment_ids() {
    487                 global $wpdb;
    488 
    489490                // Assemble clauses related to 'comment_approved'.
    490491                $approved_clauses = array();
     
    515516
    516517                                        default :
    517                                                 $status_clauses[] = $wpdb->prepare( "comment_approved = %s", $status );
     518                                                $status_clauses[] = $this->db->prepare( "comment_approved = %s", $status );
    518519                                                break;
    519520                                }
     
    538539                                // Numeric values are assumed to be user ids.
    539540                                if ( is_numeric( $unapproved_identifier ) ) {
    540                                         $approved_clauses[] = $wpdb->prepare( "( user_id = %d AND comment_approved = '0' )", $unapproved_identifier );
     541                                        $approved_clauses[] = $this->db->prepare( "( user_id = %d AND comment_approved = '0' )", $unapproved_identifier );
    541542
    542543                                // Otherwise we match against email addresses.
    543544                                } else {
    544                                         $approved_clauses[] = $wpdb->prepare( "( comment_author_email = %s AND comment_approved = '0' )", $unapproved_identifier );
     545                                        $approved_clauses[] = $this->db->prepare( "( comment_author_email = %s AND comment_approved = '0' )", $unapproved_identifier );
    545546                                }
    546547                        }
     
    601602                        // If no valid clauses were found, order by comment_date_gmt.
    602603                        if ( empty( $orderby_array ) ) {
    603                                 $orderby_array[] = "$wpdb->comments.comment_date_gmt $order";
     604                                $orderby_array[] = "{$this->db->comments}.comment_date_gmt $order";
    604605                        }
    605606
     
    634635                                }
    635636
    636                                 $orderby_array[] = "$wpdb->comments.comment_ID $comment_ID_order";
     637                                $orderby_array[] = "{$this->db->comments}.comment_ID $comment_ID_order";
    637638                        }
    638639
    639640                        $orderby = implode( ', ', $orderby_array );
    640641                } else {
    641                         $orderby = "$wpdb->comments.comment_date_gmt $order";
     642                        $orderby = "{$this->db->comments}.comment_date_gmt $order";
    642643                }
    643644
     
    656657                        $fields = 'COUNT(*)';
    657658                } else {
    658                         $fields = "$wpdb->comments.comment_ID";
     659                        $fields = "{$this->db->comments}.comment_ID";
    659660                }
    660661
    661662                $post_id = absint( $this->query_vars['post_id'] );
    662663                if ( ! empty( $post_id ) ) {
    663                         $this->sql_clauses['where']['post_id'] = $wpdb->prepare( 'comment_post_ID = %d', $post_id );
     664                        $this->sql_clauses['where']['post_id'] = $this->db->prepare( 'comment_post_ID = %d', $post_id );
    664665                }
    665666
    666667                // Parse comment IDs for an IN clause.
    667668                if ( ! empty( $this->query_vars['comment__in'] ) ) {
    668                         $this->sql_clauses['where']['comment__in'] = "$wpdb->comments.comment_ID IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__in'] ) ) . ' )';
     669                        $this->sql_clauses['where']['comment__in'] = "{$this->db->comments}.comment_ID IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__in'] ) ) . ' )';
    669670                }
    670671
    671672                // Parse comment IDs for a NOT IN clause.
    672673                if ( ! empty( $this->query_vars['comment__not_in'] ) ) {
    673                         $this->sql_clauses['where']['comment__not_in'] = "$wpdb->comments.comment_ID NOT IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__not_in'] ) ) . ' )';
     674                        $this->sql_clauses['where']['comment__not_in'] = "{$this->db->comments}.comment_ID NOT IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__not_in'] ) ) . ' )';
    674675                }
    675676
     
    695696
    696697                if ( '' !== $this->query_vars['author_email'] ) {
    697                         $this->sql_clauses['where']['author_email'] = $wpdb->prepare( 'comment_author_email = %s', $this->query_vars['author_email'] );
     698                        $this->sql_clauses['where']['author_email'] = $this->db->prepare( 'comment_author_email = %s', $this->query_vars['author_email'] );
    698699                }
    699700
    700701                if ( '' !== $this->query_vars['author_url'] ) {
    701                         $this->sql_clauses['where']['author_url'] = $wpdb->prepare( 'comment_author_url = %s', $this->query_vars['author_url'] );
     702                        $this->sql_clauses['where']['author_url'] = $this->db->prepare( 'comment_author_url = %s', $this->query_vars['author_url'] );
    702703                }
    703704
    704705                if ( '' !== $this->query_vars['karma'] ) {
    705                         $this->sql_clauses['where']['karma'] = $wpdb->prepare( 'comment_karma = %d', $this->query_vars['karma'] );
     706                        $this->sql_clauses['where']['karma'] = $this->db->prepare( 'comment_karma = %d', $this->query_vars['karma'] );
    706707                }
    707708
     
    734735
    735736                                        default:
    736                                                 $comment_types[ $operator ][] = $wpdb->prepare( '%s', $type );
     737                                                $comment_types[ $operator ][] = $this->db->prepare( '%s', $type );
    737738                                                break;
    738739                                }
     
    751752
    752753                if ( '' !== $parent ) {
    753                         $this->sql_clauses['where']['parent'] = $wpdb->prepare( 'comment_parent = %d', $parent );
     754                        $this->sql_clauses['where']['parent'] = $this->db->prepare( 'comment_parent = %d', $parent );
    754755                }
    755756
     
    757758                        $this->sql_clauses['where']['user_id'] = 'user_id IN (' . implode( ',', array_map( 'absint', $this->query_vars['user_id'] ) ) . ')';
    758759                } elseif ( '' !== $this->query_vars['user_id'] ) {
    759                         $this->sql_clauses['where']['user_id'] = $wpdb->prepare( 'user_id = %d', $this->query_vars['user_id'] );
     760                        $this->sql_clauses['where']['user_id'] = $this->db->prepare( 'user_id = %d', $this->query_vars['user_id'] );
    760761                }
    761762
     
    781782                                // $field_value may be an array.
    782783                                $esses = array_fill( 0, count( (array) $field_value ), '%s' );
    783                                 $this->sql_clauses['where'][ $field_name ] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value );
     784                                $this->sql_clauses['where'][ $field_name ] = $this->db->prepare( " {$this->db->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value );
    784785                        }
    785786                }
     
    802803
    803804                                $esses = array_fill( 0, count( $q_values ), '%s' );
    804                                 $this->sql_clauses['where'][ $field_name ] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ")", $q_values );
     805                                $this->sql_clauses['where'][ $field_name ] = $this->db->prepare( " {$this->db->posts}.{$field_name} IN (" . implode( ',', $esses ) . ")", $q_values );
    805806                        }
    806807                }
     
    831832
    832833                if ( $join_posts_table ) {
    833                         $join .= "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
     834                        $join .= "JOIN {$this->db->posts} ON {$this->db->posts}.ID = {$this->db->comments}.comment_post_ID";
    834835                }
    835836
     
    841842
    842843                        if ( ! $this->query_vars['count'] ) {
    843                                 $groupby = "{$wpdb->comments}.comment_ID";
     844                                $groupby = "{$this->db->comments}.comment_ID";
    844845                        }
    845846                }
     
    890891
    891892                $this->sql_clauses['select']  = "SELECT $found_rows $fields";
    892                 $this->sql_clauses['from']    = "FROM $wpdb->comments $join";
     893                $this->sql_clauses['from']    = "FROM {$this->db->comments} $join";
    893894                $this->sql_clauses['groupby'] = $groupby;
    894895                $this->sql_clauses['orderby'] = $orderby;
     
    898899
    899900                if ( $this->query_vars['count'] ) {
    900                         return intval( $wpdb->get_var( $this->request ) );
     901                        return intval( $this->db->get_var( $this->request ) );
    901902                } else {
    902                         $comment_ids = $wpdb->get_col( $this->request );
     903                        $comment_ids = $this->db->get_col( $this->request );
    903904                        return array_map( 'intval', $comment_ids );
    904905                }
     
    911912         * @since 4.6.0
    912913         * @access private
    913          *
    914          * @global wpdb $wpdb WordPress database abstraction object.
    915914         */
    916915        private function set_found_comments() {
    917                 global $wpdb;
    918 
    919916                if ( $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
    920917                        /**
     
    928925                        $found_comments_query = apply_filters( 'found_comments_query', 'SELECT FOUND_ROWS()', $this );
    929926
    930                         $this->found_comments = (int) $wpdb->get_var( $found_comments_query );
     927                        $this->found_comments = (int) $this->db->get_var( $found_comments_query );
    931928                }
    932929        }
     
    944941         */
    945942        protected function fill_descendants( $comments ) {
    946                 global $wpdb;
    947 
    948943                $levels = array(
    949944                        0 => wp_list_pluck( $comments, 'comment_ID' ),
     
    997992                        if ( $uncached_parent_ids ) {
    998993                                $where = 'WHERE ' . $_where . ' AND comment_parent IN (' . implode( ',', array_map( 'intval', $uncached_parent_ids ) ) . ')';
    999                                 $level_comments = $wpdb->get_results( "SELECT $wpdb->comments.comment_ID, $wpdb->comments.comment_parent {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} ORDER BY comment_date_gmt ASC, comment_ID ASC" );
     994                                $level_comments = $this->db->get_results( "SELECT {$this->db->comments}.comment_ID, {$this->db->comments}.comment_parent {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} ORDER BY comment_date_gmt ASC, comment_ID ASC" );
    1000995
    1001996                                // Cache parent-child relationships.
     
    10681063         * @access protected
    10691064         *
    1070          * @global wpdb $wpdb WordPress database abstraction object.
    1071          *
    10721065         * @param string $string
    10731066         * @param array $cols
     
    10751068         */
    10761069        protected function get_search_sql( $string, $cols ) {
    1077                 global $wpdb;
    1078 
    1079                 $like = '%' . $wpdb->esc_like( $string ) . '%';
     1070                $like = '%' . $this->db->esc_like( $string ) . '%';
    10801071
    10811072                $searches = array();
    10821073                foreach ( $cols as $col ) {
    1083                         $searches[] = $wpdb->prepare( "$col LIKE %s", $like );
     1074                        $searches[] = $this->db->prepare( "$col LIKE %s", $like );
    10841075                }
    10851076
     
    10931084         * @access protected
    10941085         *
    1095          * @global wpdb $wpdb WordPress database abstraction object.
    1096          *
    10971086         * @param string $orderby Alias for the field to order by.
    10981087         * @return string|false Value to used in the ORDER clause. False otherwise.
    10991088         */
    11001089        protected function parse_orderby( $orderby ) {
    1101                 global $wpdb;
    1102 
    11031090                $allowed_keys = array(
    11041091                        'comment_agent',
     
    11321119                $parsed = false;
    11331120                if ( $orderby == $this->query_vars['meta_key'] || $orderby == 'meta_value' ) {
    1134                         $parsed = "$wpdb->commentmeta.meta_value";
     1121                        $parsed = "{$this->db->commentmeta}.meta_value";
    11351122                } elseif ( $orderby == 'meta_value_num' ) {
    1136                         $parsed = "$wpdb->commentmeta.meta_value+0";
     1123                        $parsed = "{$this->db->commentmeta}.meta_value+0";
    11371124                } elseif ( $orderby == 'comment__in' ) {
    11381125                        $comment__in = implode( ',', array_map( 'absint', $this->query_vars['comment__in'] ) );
    1139                         $parsed = "FIELD( {$wpdb->comments}.comment_ID, $comment__in )";
     1126                        $parsed = "FIELD( {$this->db->comments}.comment_ID, $comment__in )";
    11401127                } elseif ( in_array( $orderby, $allowed_keys ) ) {
    11411128
     
    11441131                                $parsed = sprintf( "CAST(%s.meta_value AS %s)", esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) );
    11451132                        } else {
    1146                                 $parsed = "$wpdb->comments.$orderby";
     1133                                $parsed = "{$this->db->comments}.$orderby";
    11471134                        }
    11481135                }
Note: See TracChangeset for help on using the changeset viewer.