/**
* 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 `$parent__in` and `$parent__not_in` were added.
* @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 {
* Optional. Array or query string of comment query parameters. Default empty.
*
* @type string $author_email Comment author email address. Default empty.
+ * @type string $author_url Comment author URL. Default empty.
* @type array $author__in Array of author IDs to include comments for. Default empty.
* @type array $author__not_in Array of author IDs to exclude comments for. Default empty.
* @type array $comment__in Array of comment IDs to include. Default empty.
* Default empty.
* @type array $post__not_in Array of post IDs to exclude affiliated comments for.
* Default empty.
- * @type int $post_author Comment author ID to limit results by. Default empty.
- * @type string $post_status Post status to retrieve affiliated comments for.
- * Default empty.
- * @type string $post_type Post type to retrieve affiliated comments for.
+ * @type int $post_author Post author ID to limit results by. Default empty.
+ * @type string|array $post_status Post status or array of post statuses to retrieve
+ * affiliated comments for. Pass 'any' to match any value.
* Default empty.
+ * @type string $post_type Post type or array of post types to retrieve affiliated
+ * comments for. Pass 'any' to match any value. Default empty.
* @type string $post_name Post name to retrieve affiliated comments for.
* Default empty.
* @type int $post_parent Post parent ID to retrieve affiliated comments for.
* 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.
public function __construct( $query = '' ) {
$this->query_var_defaults = array(
'author_email' => '',
+ 'author_url' => '',
'author__in' => '',
'author__not_in' => '',
'include_unapproved' => '',
'orderby' => '',
'order' => 'DESC',
'parent' => '',
+ 'parent__in' => '',
+ 'parent__not_in' => '',
'post_author__in' => '',
'post_author__not_in' => '',
'post_ID' => '',
'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;
$last_changed = microtime();
wp_cache_set( 'last_changed', $last_changed, 'comment' );
}
- $cache_key = "get_comment_ids:$key:$last_changed";
- $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
*
$this->sql_clauses['where']['author_email'] = $wpdb->prepare( 'comment_author_email = %s', $this->query_vars['author_email'] );
}
+ if ( '' !== $this->query_vars['author_url'] ) {
+ $this->sql_clauses['where']['author_url'] = $wpdb->prepare( 'comment_author_url = %s', $this->query_vars['author_url'] );
+ }
+
if ( '' !== $this->query_vars['karma'] ) {
$this->sql_clauses['where']['karma'] = $wpdb->prepare( 'comment_karma = %d', $this->query_vars['karma'] );
}
}
}
- 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'] ) ) {
$this->sql_clauses['where']['user_id'] = $wpdb->prepare( 'user_id = %d', $this->query_vars['user_id'] );
}
- if ( '' !== $this->query_vars['search'] ) {
+ // Falsy search strings are ignored.
+ if ( strlen( $this->query_vars['search'] ) ) {
$search_sql = $this->get_search_sql(
$this->query_vars['search'],
array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content' )
// If any post-related query vars are passed, join the posts table.
$join_posts_table = false;
- $plucked = wp_array_slice_assoc( $this->query_vars, array( 'post_author', 'post_name', 'post_parent', 'post_status', 'post_type' ) );
+ $plucked = wp_array_slice_assoc( $this->query_vars, array( 'post_author', 'post_name', 'post_parent' ) );
$post_fields = array_filter( $plucked );
if ( ! empty( $post_fields ) ) {
}
}
+ // 'post_status' and 'post_type' are handled separately, due to the specialized behavior of 'any'.
+ foreach ( array( 'post_status', 'post_type' ) as $field_name ) {
+ $q_values = array();
+ if ( ! empty( $this->query_vars[ $field_name ] ) ) {
+ $q_values = $this->query_vars[ $field_name ];
+ if ( ! is_array( $q_values ) ) {
+ $q_values = explode( ',', $q_values );
+ }
+
+ // 'any' will cause the query var to be ignored.
+ if ( in_array( 'any', $q_values, true ) || empty( $q_values ) ) {
+ continue;
+ }
+
+ $join_posts_table = true;
+
+ $esses = array_fill( 0, count( $q_values ), '%s' );
+ $this->sql_clauses['where'][ $field_name ] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} IN (" . implode( ',', $esses ) . ")", $q_values );
+ }
+ }
+
// Comment author IDs for an IN clause.
if ( ! empty( $this->query_vars['author__in'] ) ) {
$this->sql_clauses['where']['author__in'] = 'user_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__in'] ) ) . ' )';
}
}
- $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' );
+ if ( ! $last_changed ) {
+ $last_changed = microtime();
+ wp_cache_set( 'last_changed', $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 ) {
+ $where = 'WHERE ' . $_where . ' AND comment_parent IN (' . implode( ',', array_map( 'intval', $uncached_parent_ids ) ) . ')';
+ $level_comments = $wpdb->get_results( "SELECT $wpdb->comments.comment_ID, $wpdb->comments.comment_parent {$this->sql_clauses['from']} {$where} {$this->sql_clauses['groupby']} ORDER BY comment_date_gmt ASC, comment_ID ASC" );
+
+ // 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();