X-Git-Url: https://scripts.mit.edu/gitweb/autoinstalls/wordpress.git/blobdiff_plain/03f2fa83c13c1b532284205fa7efcab9b8b2c41f..0459461f9ea42e0b090759ff6fe5f48360bef750:/wp-includes/class-wp-comment-query.php diff --git a/wp-includes/class-wp-comment-query.php b/wp-includes/class-wp-comment-query.php index e30094dd..9f6aaa53 100644 --- a/wp-includes/class-wp-comment-query.php +++ b/wp-includes/class-wp-comment-query.php @@ -59,6 +59,17 @@ class WP_Comment_Query { 'limits' => '', ); + /** + * SQL WHERE clause. + * + * Stored after the 'comments_clauses' filter is run on the compiled WHERE sub-clauses. + * + * @since 4.4.2 + * @access protected + * @var string + */ + protected $filtered_where_clause; + /** * Date query container * @@ -747,7 +758,7 @@ class WP_Comment_Query { foreach ( $post_fields as $field_name => $field_value ) { // $field_value may be an array. $esses = array_fill( 0, count( (array) $field_value ), '%s' ); - $this->sql_clauses['where']['post_fields'] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value ); + $this->sql_clauses['where'][ $field_name ] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ')', $field_value ); } } @@ -816,6 +827,8 @@ class WP_Comment_Query { $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : ''; $groupby = isset( $clauses[ 'groupby' ] ) ? $clauses[ 'groupby' ] : ''; + $this->filtered_where_clause = $where; + if ( $where ) { $where = 'WHERE ' . $where; } @@ -867,12 +880,27 @@ class WP_Comment_Query { 0 => wp_list_pluck( $comments, 'comment_ID' ), ); - $where_clauses = $this->sql_clauses['where']; - unset( - $where_clauses['parent'], - $where_clauses['parent__in'], - $where_clauses['parent__not_in'] - ); + /* + * The WHERE clause for the descendant query is the same as for the top-level + * query, minus the `parent`, `parent__in`, and `parent__not_in` sub-clauses. + */ + $_where = $this->filtered_where_clause; + $exclude_keys = array( 'parent', 'parent__in', 'parent__not_in' ); + foreach ( $exclude_keys as $exclude_key ) { + if ( isset( $this->sql_clauses['where'][ $exclude_key ] ) ) { + $clause = $this->sql_clauses['where'][ $exclude_key ]; + + // Strip the clause as well as any adjacent ANDs. + $pattern = '|(?:AND)?\s*' . $clause . '\s*(?:AND)?|'; + $_where_parts = preg_split( $pattern, $_where ); + + // Remove empties. + $_where_parts = array_filter( array_map( 'trim', $_where_parts ) ); + + // Reassemble with an AND. + $_where = implode( ' AND ', $_where_parts ); + } + } // Fetch an entire level of the descendant tree at a time. $level = 0; @@ -882,7 +910,7 @@ class WP_Comment_Query { break; } - $where = 'WHERE ' . implode( ' AND ', $where_clauses ) . ' AND comment_parent IN (' . implode( ',', array_map( 'intval', $parent_ids ) ) . ')'; + $where = 'WHERE ' . $_where . ' AND comment_parent IN (' . implode( ',', array_map( 'intval', $parent_ids ) ) . ')'; $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" ); $level++;