/**
* SQL WHERE clause.
*
- * Stored after the 'comments_clauses' filter is run on the compiled WHERE sub-clauses.
+ * Stored after the {@see 'comments_clauses'} filter is run on the compiled WHERE sub-clauses.
*
* @since 4.4.2
* @access protected
public $max_num_pages = 0;
/**
- * Make private/protected methods readable for backwards compatibility.
+ * Make private/protected methods readable for backward compatibility.
*
* @since 4.0.0
* @access public
* @since 4.4.0 Order by `comment__in` was added. `$update_comment_meta_cache`, `$no_found_rows`,
* `$hierarchical`, and `$update_comment_post_cache` were added.
* @since 4.5.0 Introduced the `$author_url` argument.
+ * @since 4.6.0 Introduced the `$cache_domain` argument.
* @access public
*
* @param string|array $query {
* @type array $meta_query Meta query clauses to limit retrieved comments by.
* See WP_Meta_Query. Default empty.
* @type int $number Maximum number of comments to retrieve.
- * Default null (no limit).
+ * Default empty (no limit).
* @type int $offset Number of comments to offset the query. Used to build
* LIMIT clause. Default 0.
* @type bool $no_found_rows Whether to disable the `SQL_CALC_FOUND_ROWS` query.
* The parameter is ignored (forced to `false`) when
* `$fields` is 'ids' or 'counts'. Accepts 'threaded',
* 'flat', or false. Default: false.
+ * @type string $cache_domain Unique cache key to be produced when this query is stored in
+ * an object cache. Default is 'core'.
* @type bool $update_comment_meta_cache Whether to prime the metadata cache for found comments.
* Default true.
* @type bool $update_comment_post_cache Whether to prime the cache for comment posts.
'meta_query' => '',
'date_query' => null, // See WP_Date_Query
'hierarchical' => false,
+ 'cache_domain' => 'core',
'update_comment_meta_cache' => true,
'update_comment_post_cache' => false,
);
/**
* Parse arguments passed to the comment query with default query parameters.
*
- * @since 4.2.0 Extracted from WP_Comment_Query::query().
+ * @since 4.2.0 Extracted from WP_Comment_Query::query().
*
* @access public
*
}
$this->query_vars = wp_parse_args( $query, $this->query_var_defaults );
+
+ /**
+ * Fires after the comment query vars have been parsed.
+ *
+ * @since 4.2.0
+ *
+ * @param WP_Comment_Query &$this The WP_Comment_Query instance (passed by reference).
+ */
do_action_ref_array( 'parse_comment_query', array( &$this ) );
}
*
* @global wpdb $wpdb WordPress database abstraction object.
*
- * @return int|array The list of comments.
+ * @return int|array List of comments or number of found comments if `$count` argument is true.
*/
public function get_comments() {
global $wpdb;
// $args can include anything. Only use the args defined in the query_var_defaults to compute the key.
$key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) );
- $last_changed = wp_cache_get( 'last_changed', 'comment' );
- if ( ! $last_changed ) {
- $last_changed = microtime();
- wp_cache_set( 'last_changed', $last_changed, 'comment' );
- }
- $cache_key = "get_comment_ids:$key:$last_changed";
+ $last_changed = wp_cache_get_last_changed( 'comment' );
- $comment_ids = wp_cache_get( $cache_key, 'comment' );
- if ( false === $comment_ids ) {
+
+ $cache_key = "get_comments:$key:$last_changed";
+ $cache_value = wp_cache_get( $cache_key, 'comment' );
+ if ( false === $cache_value ) {
$comment_ids = $this->get_comment_ids();
- wp_cache_add( $cache_key, $comment_ids, 'comment' );
+ if ( $comment_ids ) {
+ $this->set_found_comments();
+ }
+
+ $cache_value = array(
+ 'comment_ids' => $comment_ids,
+ 'found_comments' => $this->found_comments,
+ );
+ wp_cache_add( $cache_key, $cache_value, 'comment' );
+ } else {
+ $comment_ids = $cache_value['comment_ids'];
+ $this->found_comments = $cache_value['found_comments'];
+ }
+
+ if ( $this->found_comments && $this->query_vars['number'] ) {
+ $this->max_num_pages = ceil( $this->found_comments / $this->query_vars['number'] );
}
// If querying for a count only, there's nothing more to do.
$comment_ids = array_map( 'intval', $comment_ids );
- $this->comment_count = count( $this->comments );
-
- if ( $comment_ids && $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
- /**
- * Filter the query used to retrieve found comment count.
- *
- * @since 4.4.0
- *
- * @param string $found_comments_query SQL query. Default 'SELECT FOUND_ROWS()'.
- * @param WP_Comment_Query $comment_query The `WP_Comment_Query` instance.
- */
- $found_comments_query = apply_filters( 'found_comments_query', 'SELECT FOUND_ROWS()', $this );
- $this->found_comments = (int) $wpdb->get_var( $found_comments_query );
-
- $this->max_num_pages = ceil( $this->found_comments / $this->query_vars['number'] );
- }
-
if ( 'ids' == $this->query_vars['fields'] ) {
$this->comments = $comment_ids;
return $this->comments;
}
/**
- * Filter the comment query results.
+ * Filters the comment query results.
*
* @since 3.1.0
*
}
}
- if ( $this->query_vars['hierarchical'] && ! $this->query_vars['parent'] ) {
- $this->query_vars['parent'] = 0;
+ $parent = $this->query_vars['parent'];
+ if ( $this->query_vars['hierarchical'] && ! $parent ) {
+ $parent = 0;
}
- if ( '' !== $this->query_vars['parent'] ) {
- $this->sql_clauses['where']['parent'] = $wpdb->prepare( 'comment_parent = %d', $this->query_vars['parent'] );
+ if ( '' !== $parent ) {
+ $this->sql_clauses['where']['parent'] = $wpdb->prepare( 'comment_parent = %d', $parent );
}
if ( is_array( $this->query_vars['user_id'] ) ) {
}
}
- $date_query = $this->query_vars['date_query'];
- if ( ! empty( $date_query ) && is_array( $date_query ) ) {
- $date_query_object = new WP_Date_Query( $date_query, 'comment_date' );
- $this->sql_clauses['where']['date_query'] = preg_replace( '/^\s*AND\s*/', '', $date_query_object->get_sql() );
+ if ( ! empty( $this->query_vars['date_query'] ) && is_array( $this->query_vars['date_query'] ) ) {
+ $this->date_query = new WP_Date_Query( $this->query_vars['date_query'], 'comment_date' );
+ $this->sql_clauses['where']['date_query'] = preg_replace( '/^\s*AND\s*/', '', $this->date_query->get_sql() );
}
$where = implode( ' AND ', $this->sql_clauses['where'] );
$pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' );
/**
- * Filter the comment query clauses.
+ * Filters the comment query clauses.
*
* @since 3.1.0
*
}
}
+ /**
+ * Populates found_comments and max_num_pages properties for the current
+ * query if the limit clause was used.
+ *
+ * @since 4.6.0
+ * @access private
+ *
+ * @global wpdb $wpdb WordPress database abstraction object.
+ */
+ private function set_found_comments() {
+ global $wpdb;
+
+ if ( $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
+ /**
+ * Filters the query used to retrieve found comment count.
+ *
+ * @since 4.4.0
+ *
+ * @param string $found_comments_query SQL query. Default 'SELECT FOUND_ROWS()'.
+ * @param WP_Comment_Query $comment_query The `WP_Comment_Query` instance.
+ */
+ $found_comments_query = apply_filters( 'found_comments_query', 'SELECT FOUND_ROWS()', $this );
+
+ $this->found_comments = (int) $wpdb->get_var( $found_comments_query );
+ }
+ }
+
/**
* Fetch descendants for located comments.
*
}
}
+ $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) );
+ $last_changed = wp_cache_get_last_changed( 'comment' );
+
// Fetch an entire level of the descendant tree at a time.
$level = 0;
do {
- $parent_ids = $levels[ $level ];
- if ( ! $parent_ids ) {
- break;
+ // Parent-child relationships may be cached. Only query for those that are not.
+ $child_ids = $uncached_parent_ids = array();
+ $_parent_ids = $levels[ $level ];
+ foreach ( $_parent_ids as $parent_id ) {
+ $cache_key = "get_comment_child_ids:$parent_id:$key:$last_changed";
+ $parent_child_ids = wp_cache_get( $cache_key, 'comment' );
+ if ( false !== $parent_child_ids ) {
+ $child_ids = array_merge( $child_ids, $parent_child_ids );
+ } else {
+ $uncached_parent_ids[] = $parent_id;
+ }
}
- $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" );
+ if ( $uncached_parent_ids ) {
+ // Fetch this level of comments.
+ $parent_query_args = $this->query_vars;
+ foreach ( $exclude_keys as $exclude_key ) {
+ $parent_query_args[ $exclude_key ] = '';
+ }
+ $parent_query_args['parent__in'] = $uncached_parent_ids;
+ $parent_query_args['no_found_rows'] = true;
+ $parent_query_args['hierarchical'] = false;
+ $parent_query_args['offset'] = 0;
+ $parent_query_args['number'] = 0;
+
+ $level_comments = get_comments( $parent_query_args );
+
+ // Cache parent-child relationships.
+ $parent_map = array_fill_keys( $uncached_parent_ids, array() );
+ foreach ( $level_comments as $level_comment ) {
+ $parent_map[ $level_comment->comment_parent ][] = $level_comment->comment_ID;
+ $child_ids[] = $level_comment->comment_ID;
+ }
+
+ foreach ( $parent_map as $parent_id => $children ) {
+ $cache_key = "get_comment_child_ids:$parent_id:$key:$last_changed";
+ wp_cache_set( $cache_key, $children, 'comment' );
+ }
+ }
$level++;
- $levels[ $level ] = $comment_ids;
- } while ( $comment_ids );
+ $levels[ $level ] = $child_ids;
+ } while ( $child_ids );
// Prime comment caches for non-top-level comments.
$descendant_ids = array();
- for ( $i = 1; $i < count( $levels ); $i++ ) {
+ for ( $i = 1, $c = count( $levels ); $i < $c; $i++ ) {
$descendant_ids = array_merge( $descendant_ids, $levels[ $i ] );
}