WordPress.org

Make WordPress Core


Ignore:
Timestamp:
01/20/2016 05:27:07 AM (4 years ago)
Author:
dd32
Message:

Comments: Use the post-filter WHERE clause when querying for comment descendants.

The descendant query in WP_Comment_Query::fill_descendants() uses the clauses
of the main get_comment_ids() query as a basis, discarding the parent,
parent__in, and parent__not_in clauses. As implemented in WP 4.4 [34546],
the WHERE clause was assembled in such a way that any modifications applied
using the comments_clauses filter were not inherited by fill_descendants().
This resulted in descendant queries that did not always properly filter
results, and sometimes contained syntax errors.

The current changeset fixes the problem by using the post-filter WHERE clause
as the basis for the fill_descendants() query. This change requires a new
approach for eliminating the unneeded parent-related clauses: instead of
eliminating values in an associative array, we must use regular expressions.

Merges [36277] to the 4.4 branch.
Props boonebgorges, firebird75.
Fixes #35192.

Location:
branches/4.4
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • branches/4.4

  • branches/4.4/src/wp-includes/class-wp-comment-query.php

    r35757 r36357  
    5959        'limits'  => '',
    6060    );
     61
     62    /**
     63     * SQL WHERE clause.
     64     *
     65     * Stored after the 'comments_clauses' filter is run on the compiled WHERE sub-clauses.
     66     *
     67     * @since 4.4.2
     68     * @access protected
     69     * @var string
     70     */
     71    protected $filtered_where_clause;
    6172
    6273    /**
     
    817828        $groupby = isset( $clauses[ 'groupby' ] ) ? $clauses[ 'groupby' ] : '';
    818829
     830        $this->filtered_where_clause = $where;
     831
    819832        if ( $where ) {
    820833            $where = 'WHERE ' . $where;
     
    868881        );
    869882
    870         $where_clauses = $this->sql_clauses['where'];
    871         unset(
    872             $where_clauses['parent'],
    873             $where_clauses['parent__in'],
    874             $where_clauses['parent__not_in']
    875         );
     883        /*
     884         * The WHERE clause for the descendant query is the same as for the top-level
     885         * query, minus the `parent`, `parent__in`, and `parent__not_in` sub-clauses.
     886         */
     887        $_where = $this->filtered_where_clause;
     888        $exclude_keys = array( 'parent', 'parent__in', 'parent__not_in' );
     889        foreach ( $exclude_keys as $exclude_key ) {
     890            if ( isset( $this->sql_clauses['where'][ $exclude_key ] ) ) {
     891                $clause = $this->sql_clauses['where'][ $exclude_key ];
     892
     893                // Strip the clause as well as any adjacent ANDs.
     894                $pattern = '|(?:AND)?\s*' . $clause . '\s*(?:AND)?|';
     895                $_where_parts = preg_split( $pattern, $_where );
     896
     897                // Remove empties.
     898                $_where_parts = array_filter( array_map( 'trim', $_where_parts ) );
     899
     900                // Reassemble with an AND.
     901                $_where = implode( ' AND ', $_where_parts );
     902            }
     903        }
    876904
    877905        // Fetch an entire level of the descendant tree at a time.
     
    883911            }
    884912
    885             $where = 'WHERE ' . implode( ' AND ', $where_clauses ) . ' AND comment_parent IN (' . implode( ',', array_map( 'intval', $parent_ids ) ) . ')';
     913            $where = 'WHERE ' . $_where . ' AND comment_parent IN (' . implode( ',', array_map( 'intval', $parent_ids ) ) . ')';
    886914            $comment_ids = $wpdb->get_col( "{$this->sql_clauses['select']} {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} ORDER BY comment_date_gmt ASC, comment_ID ASC" );
    887915
Note: See TracChangeset for help on using the changeset viewer.