Make WordPress Core


Ignore:
Timestamp:
10/10/2016 06:37:02 AM (9 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-comment-query.php

    r38639 r38768  
    141141        return false;
    142142    }
    143 
    144     /**
    145      * @since 4.7.0
    146      * @access protected
    147      * @var wpdb
    148      */
    149     protected $db;
    150143
    151144    /**
     
    268261     */
    269262    public function __construct( $query = '' ) {
    270         $this->db = $GLOBALS['wpdb'];
    271 
    272263        $this->query_var_defaults = array(
    273264            'author_email' => '',
     
    373364     * @access public
    374365     *
     366     * @global wpdb $wpdb WordPress database abstraction object.
     367     *
    375368     * @return int|array List of comments or number of found comments if `$count` argument is true.
    376369     */
    377370    public function get_comments() {
     371        global $wpdb;
     372
    378373        $this->parse_query();
    379374
     
    394389        $this->meta_query->parse_query_vars( $this->query_vars );
    395390        if ( ! empty( $this->meta_query->queries ) ) {
    396             $this->meta_query_clauses = $this->meta_query->get_sql( 'comment', $this->db->comments, 'comment_ID', $this );
     391            $this->meta_query_clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this );
    397392        }
    398393
     
    486481     * @since 4.4.0
    487482     * @access protected
     483     *
     484     * @global wpdb $wpdb WordPress database abstraction object.
    488485     */
    489486    protected function get_comment_ids() {
     487        global $wpdb;
     488
    490489        // Assemble clauses related to 'comment_approved'.
    491490        $approved_clauses = array();
     
    516515
    517516                    default :
    518                         $status_clauses[] = $this->db->prepare( "comment_approved = %s", $status );
     517                        $status_clauses[] = $wpdb->prepare( "comment_approved = %s", $status );
    519518                        break;
    520519                }
     
    539538                // Numeric values are assumed to be user ids.
    540539                if ( is_numeric( $unapproved_identifier ) ) {
    541                     $approved_clauses[] = $this->db->prepare( "( user_id = %d AND comment_approved = '0' )", $unapproved_identifier );
     540                    $approved_clauses[] = $wpdb->prepare( "( user_id = %d AND comment_approved = '0' )", $unapproved_identifier );
    542541
    543542                // Otherwise we match against email addresses.
    544543                } else {
    545                     $approved_clauses[] = $this->db->prepare( "( comment_author_email = %s AND comment_approved = '0' )", $unapproved_identifier );
     544                    $approved_clauses[] = $wpdb->prepare( "( comment_author_email = %s AND comment_approved = '0' )", $unapproved_identifier );
    546545                }
    547546            }
     
    602601            // If no valid clauses were found, order by comment_date_gmt.
    603602            if ( empty( $orderby_array ) ) {
    604                 $orderby_array[] = "{$this->db->comments}.comment_date_gmt $order";
     603                $orderby_array[] = "$wpdb->comments.comment_date_gmt $order";
    605604            }
    606605
     
    635634                }
    636635
    637                 $orderby_array[] = "{$this->db->comments}.comment_ID $comment_ID_order";
     636                $orderby_array[] = "$wpdb->comments.comment_ID $comment_ID_order";
    638637            }
    639638
    640639            $orderby = implode( ', ', $orderby_array );
    641640        } else {
    642             $orderby = "{$this->db->comments}.comment_date_gmt $order";
     641            $orderby = "$wpdb->comments.comment_date_gmt $order";
    643642        }
    644643
     
    657656            $fields = 'COUNT(*)';
    658657        } else {
    659             $fields = "{$this->db->comments}.comment_ID";
     658            $fields = "$wpdb->comments.comment_ID";
    660659        }
    661660
    662661        $post_id = absint( $this->query_vars['post_id'] );
    663662        if ( ! empty( $post_id ) ) {
    664             $this->sql_clauses['where']['post_id'] = $this->db->prepare( 'comment_post_ID = %d', $post_id );
     663            $this->sql_clauses['where']['post_id'] = $wpdb->prepare( 'comment_post_ID = %d', $post_id );
    665664        }
    666665
    667666        // Parse comment IDs for an IN clause.
    668667        if ( ! empty( $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'] ) ) . ' )';
     668            $this->sql_clauses['where']['comment__in'] = "$wpdb->comments.comment_ID IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__in'] ) ) . ' )';
    670669        }
    671670
    672671        // Parse comment IDs for a NOT IN clause.
    673672        if ( ! empty( $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'] ) ) . ' )';
     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'] ) ) . ' )';
    675674        }
    676675
     
    696695
    697696        if ( '' !== $this->query_vars['author_email'] ) {
    698             $this->sql_clauses['where']['author_email'] = $this->db->prepare( 'comment_author_email = %s', $this->query_vars['author_email'] );
     697            $this->sql_clauses['where']['author_email'] = $wpdb->prepare( 'comment_author_email = %s', $this->query_vars['author_email'] );
    699698        }
    700699
    701700        if ( '' !== $this->query_vars['author_url'] ) {
    702             $this->sql_clauses['where']['author_url'] = $this->db->prepare( 'comment_author_url = %s', $this->query_vars['author_url'] );
     701            $this->sql_clauses['where']['author_url'] = $wpdb->prepare( 'comment_author_url = %s', $this->query_vars['author_url'] );
    703702        }
    704703
    705704        if ( '' !== $this->query_vars['karma'] ) {
    706             $this->sql_clauses['where']['karma'] = $this->db->prepare( 'comment_karma = %d', $this->query_vars['karma'] );
     705            $this->sql_clauses['where']['karma'] = $wpdb->prepare( 'comment_karma = %d', $this->query_vars['karma'] );
    707706        }
    708707
     
    735734
    736735                    default:
    737                         $comment_types[ $operator ][] = $this->db->prepare( '%s', $type );
     736                        $comment_types[ $operator ][] = $wpdb->prepare( '%s', $type );
    738737                        break;
    739738                }
     
    752751
    753752        if ( '' !== $parent ) {
    754             $this->sql_clauses['where']['parent'] = $this->db->prepare( 'comment_parent = %d', $parent );
     753            $this->sql_clauses['where']['parent'] = $wpdb->prepare( 'comment_parent = %d', $parent );
    755754        }
    756755
     
    758757            $this->sql_clauses['where']['user_id'] = 'user_id IN (' . implode( ',', array_map( 'absint', $this->query_vars['user_id'] ) ) . ')';
    759758        } elseif ( '' !== $this->query_vars['user_id'] ) {
    760             $this->sql_clauses['where']['user_id'] = $this->db->prepare( 'user_id = %d', $this->query_vars['user_id'] );
     759            $this->sql_clauses['where']['user_id'] = $wpdb->prepare( 'user_id = %d', $this->query_vars['user_id'] );
    761760        }
    762761
     
    782781                // $field_value may be an array.
    783782                $esses = array_fill( 0, count( (array) $field_value ), '%s' );
    784                 $this->sql_clauses['where'][ $field_name ] = $this->db->prepare( " {$this->db->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value );
     783                $this->sql_clauses['where'][ $field_name ] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value );
    785784            }
    786785        }
     
    803802
    804803                $esses = array_fill( 0, count( $q_values ), '%s' );
    805                 $this->sql_clauses['where'][ $field_name ] = $this->db->prepare( " {$this->db->posts}.{$field_name} IN (" . implode( ',', $esses ) . ")", $q_values );
     804                $this->sql_clauses['where'][ $field_name ] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ")", $q_values );
    806805            }
    807806        }
     
    832831
    833832        if ( $join_posts_table ) {
    834             $join .= "JOIN {$this->db->posts} ON {$this->db->posts}.ID = {$this->db->comments}.comment_post_ID";
     833            $join .= "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
    835834        }
    836835
     
    842841
    843842            if ( ! $this->query_vars['count'] ) {
    844                 $groupby = "{$this->db->comments}.comment_ID";
     843                $groupby = "{$wpdb->comments}.comment_ID";
    845844            }
    846845        }
     
    891890
    892891        $this->sql_clauses['select']  = "SELECT $found_rows $fields";
    893         $this->sql_clauses['from']    = "FROM {$this->db->comments} $join";
     892        $this->sql_clauses['from']    = "FROM $wpdb->comments $join";
    894893        $this->sql_clauses['groupby'] = $groupby;
    895894        $this->sql_clauses['orderby'] = $orderby;
     
    899898
    900899        if ( $this->query_vars['count'] ) {
    901             return intval( $this->db->get_var( $this->request ) );
     900            return intval( $wpdb->get_var( $this->request ) );
    902901        } else {
    903             $comment_ids = $this->db->get_col( $this->request );
     902            $comment_ids = $wpdb->get_col( $this->request );
    904903            return array_map( 'intval', $comment_ids );
    905904        }
     
    912911     * @since 4.6.0
    913912     * @access private
     913     *
     914     * @global wpdb $wpdb WordPress database abstraction object.
    914915     */
    915916    private function set_found_comments() {
     917        global $wpdb;
     918
    916919        if ( $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
    917920            /**
     
    925928            $found_comments_query = apply_filters( 'found_comments_query', 'SELECT FOUND_ROWS()', $this );
    926929
    927             $this->found_comments = (int) $this->db->get_var( $found_comments_query );
     930            $this->found_comments = (int) $wpdb->get_var( $found_comments_query );
    928931        }
    929932    }
     
    941944     */
    942945    protected function fill_descendants( $comments ) {
     946        global $wpdb;
     947
    943948        $levels = array(
    944949            0 => wp_list_pluck( $comments, 'comment_ID' ),
     
    10711076     * @access protected
    10721077     *
     1078     * @global wpdb $wpdb WordPress database abstraction object.
     1079     *
    10731080     * @param string $string
    10741081     * @param array $cols
     
    10761083     */
    10771084    protected function get_search_sql( $string, $cols ) {
    1078         $like = '%' . $this->db->esc_like( $string ) . '%';
     1085        global $wpdb;
     1086
     1087        $like = '%' . $wpdb->esc_like( $string ) . '%';
    10791088
    10801089        $searches = array();
    10811090        foreach ( $cols as $col ) {
    1082             $searches[] = $this->db->prepare( "$col LIKE %s", $like );
     1091            $searches[] = $wpdb->prepare( "$col LIKE %s", $like );
    10831092        }
    10841093
     
    10921101     * @access protected
    10931102     *
     1103     * @global wpdb $wpdb WordPress database abstraction object.
     1104     *
    10941105     * @param string $orderby Alias for the field to order by.
    10951106     * @return string|false Value to used in the ORDER clause. False otherwise.
    10961107     */
    10971108    protected function parse_orderby( $orderby ) {
     1109        global $wpdb;
     1110
    10981111        $allowed_keys = array(
    10991112            'comment_agent',
     
    11271140        $parsed = false;
    11281141        if ( $orderby == $this->query_vars['meta_key'] || $orderby == 'meta_value' ) {
    1129             $parsed = "{$this->db->commentmeta}.meta_value";
     1142            $parsed = "$wpdb->commentmeta.meta_value";
    11301143        } elseif ( $orderby == 'meta_value_num' ) {
    1131             $parsed = "{$this->db->commentmeta}.meta_value+0";
     1144            $parsed = "$wpdb->commentmeta.meta_value+0";
    11321145        } elseif ( $orderby == 'comment__in' ) {
    11331146            $comment__in = implode( ',', array_map( 'absint', $this->query_vars['comment__in'] ) );
    1134             $parsed = "FIELD( {$this->db->comments}.comment_ID, $comment__in )";
     1147            $parsed = "FIELD( {$wpdb->comments}.comment_ID, $comment__in )";
    11351148        } elseif ( in_array( $orderby, $allowed_keys ) ) {
    11361149
     
    11391152                $parsed = sprintf( "CAST(%s.meta_value AS %s)", esc_sql( $meta_clause['alias'] ), esc_sql( $meta_clause['cast'] ) );
    11401153            } else {
    1141                 $parsed = "{$this->db->comments}.$orderby";
     1154                $parsed = "$wpdb->comments.$orderby";
    11421155            }
    11431156        }
Note: See TracChangeset for help on using the changeset viewer.