Make WordPress Core

Changeset 34542


Ignore:
Timestamp:
09/25/2015 02:08:50 PM (11 years ago)
Author:
boonebgorges
Message:

Store SQL clauses in WP_Comment_Query property.

This small syntax change paves the way for some improvements related to #8071.

File:
1 edited

Legend:

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

    r34411 r34542  
    4343         */
    4444        protected $meta_query_clauses;
     45
     46        /**
     47         * SQL query clauses.
     48         *
     49         * @since 4.4.0
     50         * @access protected
     51         * @var array
     52         */
     53        protected $sql_clauses = array(
     54                'select'  => '',
     55                'from'    => '',
     56                'where'   => array(),
     57                'groupby' => '',
     58                'orderby' => '',
     59                'limits'  => '',
     60        );
    4561
    4662        /**
     
    358374                global $wpdb;
    359375
    360                 $groupby = '';
    361                 $where = array();
    362 
    363376                // Assemble clauses related to 'comment_approved'.
    364377                $approved_clauses = array();
     
    424437                if ( ! empty( $approved_clauses ) ) {
    425438                        if ( 1 === count( $approved_clauses ) ) {
    426                                 $where[] = $approved_clauses[0];
     439                                $this->sql_clauses['where']['approved'] = $approved_clauses[0];
    427440                        } else {
    428                                 $where[] = '( ' . implode( ' OR ', $approved_clauses ) . ' )';
     441                                $this->sql_clauses['where']['approved'] = '( ' . implode( ' OR ', $approved_clauses ) . ' )';
    429442                        }
    430443                }
     
    525538                                $limits = 'LIMIT ' . $number;
    526539                        }
    527                 } else {
    528                         $limits = '';
    529540                }
    530541
     
    535546                }
    536547
    537                 $join = '';
    538 
    539548                $post_id = absint( $this->query_vars['post_id'] );
    540549                if ( ! empty( $post_id ) ) {
    541                         $where[] = $wpdb->prepare( 'comment_post_ID = %d', $post_id );
     550                        $this->sql_clauses['where']['post_id'] = $wpdb->prepare( 'comment_post_ID = %d', $post_id );
    542551                }
    543552
    544553                // Parse comment IDs for an IN clause.
    545554                if ( ! empty( $this->query_vars['comment__in'] ) ) {
    546                         $where[] = "$wpdb->comments.comment_ID IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__in'] ) ) . ' )';
     555                        $this->sql_clauses['where']['comment__in'] = "$wpdb->comments.comment_ID IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__in'] ) ) . ' )';
    547556                }
    548557
    549558                // Parse comment IDs for a NOT IN clause.
    550559                if ( ! empty( $this->query_vars['comment__not_in'] ) ) {
    551                         $where[] = "$wpdb->comments.comment_ID NOT IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__not_in'] ) ) . ' )';
     560                        $this->sql_clauses['where']['comment__not_in'] = "$wpdb->comments.comment_ID NOT IN ( " . implode( ',', wp_parse_id_list( $this->query_vars['comment__not_in'] ) ) . ' )';
    552561                }
    553562
    554563                // Parse comment parent IDs for an IN clause.
    555564                if ( ! empty( $this->query_vars['parent__in'] ) ) {
    556                         $where[] = 'comment_parent IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['parent__in'] ) ) . ' )';
     565                        $this->sql_clauses['where']['parent__in'] = 'comment_parent IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['parent__in'] ) ) . ' )';
    557566                }
    558567
    559568                // Parse comment parent IDs for a NOT IN clause.
    560569                if ( ! empty( $this->query_vars['parent__not_in'] ) ) {
    561                         $where[] = 'comment_parent NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['parent__not_in'] ) ) . ' )';
     570                        $this->sql_clauses['where']['parent__not_in'] = 'comment_parent NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['parent__not_in'] ) ) . ' )';
    562571                }
    563572
    564573                // Parse comment post IDs for an IN clause.
    565574                if ( ! empty( $this->query_vars['post__in'] ) ) {
    566                         $where[] = 'comment_post_ID IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__in'] ) ) . ' )';
     575                        $this->sql_clauses['where']['post__in'] = 'comment_post_ID IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__in'] ) ) . ' )';
    567576                }
    568577
    569578                // Parse comment post IDs for a NOT IN clause.
    570579                if ( ! empty( $this->query_vars['post__not_in'] ) ) {
    571                         $where[] = 'comment_post_ID NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__not_in'] ) ) . ' )';
     580                        $this->sql_clauses['where']['post__not_in'] = 'comment_post_ID NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__not_in'] ) ) . ' )';
    572581                }
    573582
    574583                if ( '' !== $this->query_vars['author_email'] ) {
    575                         $where[] = $wpdb->prepare( 'comment_author_email = %s', $this->query_vars['author_email'] );
     584                        $this->sql_clauses['where']['author_email'] = $wpdb->prepare( 'comment_author_email = %s', $this->query_vars['author_email'] );
    576585                }
    577586
    578587                if ( '' !== $this->query_vars['karma'] ) {
    579                         $where[] = $wpdb->prepare( 'comment_karma = %d', $this->query_vars['karma'] );
     588                        $this->sql_clauses['where']['karma'] = $wpdb->prepare( 'comment_karma = %d', $this->query_vars['karma'] );
    580589                }
    581590
     
    615624                        if ( ! empty( $comment_types[ $operator ] ) ) {
    616625                                $types_sql = implode( ', ', $comment_types[ $operator ] );
    617                                 $where[] = "comment_type $operator ($types_sql)";
     626                                $this->sql_clauses['where']['comment_type__' . strtolower( str_replace( ' ', '_', $operator ) ) ] = "comment_type $operator ($types_sql)";
    618627                        }
    619628                }
    620629
    621630                if ( '' !== $this->query_vars['parent'] ) {
    622                         $where[] = $wpdb->prepare( 'comment_parent = %d', $this->query_vars['parent'] );
     631                        $this->sql_clauses['where']['parent'] = $wpdb->prepare( 'comment_parent = %d', $this->query_vars['parent'] );
    623632                }
    624633
    625634                if ( is_array( $this->query_vars['user_id'] ) ) {
    626                         $where[] = 'user_id IN (' . implode( ',', array_map( 'absint', $this->query_vars['user_id'] ) ) . ')';
     635                        $this->sql_clauses['where']['user_id'] = 'user_id IN (' . implode( ',', array_map( 'absint', $this->query_vars['user_id'] ) ) . ')';
    627636                } elseif ( '' !== $this->query_vars['user_id'] ) {
    628                         $where[] = $wpdb->prepare( 'user_id = %d', $this->query_vars['user_id'] );
     637                        $this->sql_clauses['where']['user_id'] = $wpdb->prepare( 'user_id = %d', $this->query_vars['user_id'] );
    629638                }
    630639
     
    636645
    637646                        // Strip leading 'AND'.
    638                         $where[] = preg_replace( '/^\s*AND\s*/', '', $search_sql );
     647                        $this->sql_clauses['where']['search'] = preg_replace( '/^\s*AND\s*/', '', $search_sql );
    639648                }
    640649
     
    649658                                // $field_value may be an array.
    650659                                $esses = array_fill( 0, count( (array) $field_value ), '%s' );
    651                                 $where[] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value );
     660                                $this->sql_clauses['where']['post_fields'] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value );
    652661                        }
    653662                }
     
    655664                // Comment author IDs for an IN clause.
    656665                if ( ! empty( $this->query_vars['author__in'] ) ) {
    657                         $where[] = 'user_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__in'] ) ) . ' )';
     666                        $this->sql_clauses['where']['author__in'] = 'user_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__in'] ) ) . ' )';
    658667                }
    659668
    660669                // Comment author IDs for a NOT IN clause.
    661670                if ( ! empty( $this->query_vars['author__not_in'] ) ) {
    662                         $where[] = 'user_id NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__not_in'] ) ) . ' )';
     671                        $this->sql_clauses['where']['author__not_in'] = 'user_id NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__not_in'] ) ) . ' )';
    663672                }
    664673
     
    666675                if ( ! empty( $this->query_vars['post_author__in'] ) ) {
    667676                        $join_posts_table = true;
    668                         $where[] = 'post_author IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__in'] ) ) . ' )';
     677                        $this->sql_clauses['where']['post_author__in'] = 'post_author IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__in'] ) ) . ' )';
    669678                }
    670679
     
    672681                if ( ! empty( $this->query_vars['post_author__not_in'] ) ) {
    673682                        $join_posts_table = true;
    674                         $where[] = 'post_author NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__not_in'] ) ) . ' )';
    675                 }
     683                        $this->sql_clauses['where']['post_author__not_in'] = 'post_author NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__not_in'] ) ) . ' )';
     684                }
     685
     686                $join = '';
    676687
    677688                if ( $join_posts_table ) {
    678                         $join = "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
     689                        $join .= "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID";
    679690                }
    680691
     
    683694
    684695                        // Strip leading 'AND'.
    685                         $where[] = preg_replace( '/^\s*AND\s*/', '', $this->meta_query_clauses['where'] );
     696                        $this->sql_clauses['where']['meta_query'] = preg_replace( '/^\s*AND\s*/', '', $this->meta_query_clauses['where'] );
    686697
    687698                        if ( ! $this->query_vars['count'] ) {
     
    693704                if ( ! empty( $date_query ) && is_array( $date_query ) ) {
    694705                        $date_query_object = new WP_Date_Query( $date_query, 'comment_date' );
    695                         $where[] = preg_replace( '/^\s*AND\s*/', '', $date_query_object->get_sql() );
    696                 }
    697 
    698                 $where = implode( ' AND ', $where );
     706                        $this->sql_clauses['where']['date_query'] = preg_replace( '/^\s*AND\s*/', '', $date_query_object->get_sql() );
     707                }
     708
     709                $where = implode( ' AND ', $this->sql_clauses['where'] );
    699710
    700711                $pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' );
     
    728739                }
    729740
    730                 $this->request = "SELECT $fields FROM $wpdb->comments $join $where $groupby $orderby $limits";
     741                $this->sql_clauses['select']  = "SELECT $fields";
     742                $this->sql_clauses['from']    = "FROM $wpdb->comments $join";
     743                $this->sql_clauses['groupby'] = $groupby;
     744                $this->sql_clauses['orderby'] = $orderby;
     745                $this->sql_clauses['limits']  = $limits;
     746
     747                $this->request = "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} {$this->sql_clauses['orderby']} {$this->sql_clauses['limits']}";
    731748
    732749                if ( $this->query_vars['count'] ) {
Note: See TracChangeset for help on using the changeset viewer.