151
151
* @since 4.4.0 Order by `comment__in` was added. `$update_comment_meta_cache`, `$no_found_rows`,
152
152
* `$hierarchical`, and `$update_comment_post_cache` were added.
153
153
* @since 4.5.0 Introduced the `$author_url` argument.
154
* @since 4.6.0 Introduced the `$cache_domain` argument.
156
157
* @param string|array $query {
250
251
* The parameter is ignored (forced to `false`) when
251
252
* `$fields` is 'ids' or 'counts'. Accepts 'threaded',
252
253
* 'flat', or false. Default: false.
254
* @type string $cache_domain Unique cache key to be produced when this query is stored in
255
* an object cache. Default is 'core'.
253
256
* @type bool $update_comment_meta_cache Whether to prime the metadata cache for found comments.
255
258
* @type bool $update_comment_post_cache Whether to prime the cache for comment posts.
299
302
'meta_query' => '',
300
303
'date_query' => null, // See WP_Date_Query
301
304
'hierarchical' => false,
305
'cache_domain' => 'core',
302
306
'update_comment_meta_cache' => true,
303
307
'update_comment_post_cache' => false,
386
398
$last_changed = microtime();
387
399
wp_cache_set( 'last_changed', $last_changed, 'comment' );
389
$cache_key = "get_comment_ids:$key:$last_changed";
391
$comment_ids = wp_cache_get( $cache_key, 'comment' );
392
if ( false === $comment_ids ) {
402
$cache_key = "get_comments:$key:$last_changed";
403
$cache_value = wp_cache_get( $cache_key, 'comment' );
404
if ( false === $cache_value ) {
393
405
$comment_ids = $this->get_comment_ids();
394
wp_cache_add( $cache_key, $comment_ids, 'comment' );
406
if ( $comment_ids ) {
407
$this->set_found_comments();
410
$cache_value = array(
411
'comment_ids' => $comment_ids,
412
'found_comments' => $this->found_comments,
414
wp_cache_add( $cache_key, $cache_value, 'comment' );
416
$comment_ids = $cache_value['comment_ids'];
417
$this->found_comments = $cache_value['found_comments'];
420
if ( $this->found_comments && $this->query_vars['number'] ) {
421
$this->max_num_pages = ceil( $this->found_comments / $this->query_vars['number'] );
397
424
// If querying for a count only, there's nothing more to do.
403
430
$comment_ids = array_map( 'intval', $comment_ids );
405
$this->comment_count = count( $this->comments );
407
if ( $comment_ids && $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
409
* Filter the query used to retrieve found comment count.
413
* @param string $found_comments_query SQL query. Default 'SELECT FOUND_ROWS()'.
414
* @param WP_Comment_Query $comment_query The `WP_Comment_Query` instance.
416
$found_comments_query = apply_filters( 'found_comments_query', 'SELECT FOUND_ROWS()', $this );
417
$this->found_comments = (int) $wpdb->get_var( $found_comments_query );
419
$this->max_num_pages = ceil( $this->found_comments / $this->query_vars['number'] );
422
432
if ( 'ids' == $this->query_vars['fields'] ) {
423
433
$this->comments = $comment_ids;
424
434
return $this->comments;
737
if ( $this->query_vars['hierarchical'] && ! $this->query_vars['parent'] ) {
738
$this->query_vars['parent'] = 0;
747
$parent = $this->query_vars['parent'];
748
if ( $this->query_vars['hierarchical'] && ! $parent ) {
741
if ( '' !== $this->query_vars['parent'] ) {
742
$this->sql_clauses['where']['parent'] = $wpdb->prepare( 'comment_parent = %d', $this->query_vars['parent'] );
752
if ( '' !== $parent ) {
753
$this->sql_clauses['where']['parent'] = $wpdb->prepare( 'comment_parent = %d', $parent );
745
756
if ( is_array( $this->query_vars['user_id'] ) ) {
836
$date_query = $this->query_vars['date_query'];
837
if ( ! empty( $date_query ) && is_array( $date_query ) ) {
838
$date_query_object = new WP_Date_Query( $date_query, 'comment_date' );
839
$this->sql_clauses['where']['date_query'] = preg_replace( '/^\s*AND\s*/', '', $date_query_object->get_sql() );
847
if ( ! empty( $this->query_vars['date_query'] ) && is_array( $this->query_vars['date_query'] ) ) {
848
$this->date_query = new WP_Date_Query( $this->query_vars['date_query'], 'comment_date' );
849
$this->sql_clauses['where']['date_query'] = preg_replace( '/^\s*AND\s*/', '', $this->date_query->get_sql() );
842
852
$where = implode( ' AND ', $this->sql_clauses['where'] );
844
854
$pieces = array( 'fields', 'join', 'where', 'orderby', 'limits', 'groupby' );
846
* Filter the comment query clauses.
856
* Filters the comment query clauses.
908
* Populates found_comments and max_num_pages properties for the current
909
* query if the limit clause was used.
914
* @global wpdb $wpdb WordPress database abstraction object.
916
private function set_found_comments() {
919
if ( $this->query_vars['number'] && ! $this->query_vars['no_found_rows'] ) {
921
* Filters the query used to retrieve found comment count.
925
* @param string $found_comments_query SQL query. Default 'SELECT FOUND_ROWS()'.
926
* @param WP_Comment_Query $comment_query The `WP_Comment_Query` instance.
928
$found_comments_query = apply_filters( 'found_comments_query', 'SELECT FOUND_ROWS()', $this );
930
$this->found_comments = (int) $wpdb->get_var( $found_comments_query );
898
935
* Fetch descendants for located comments.
900
937
* Instead of calling `get_children()` separately on each child comment, we do a single set of queries to fetch
974
$key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) );
975
$last_changed = wp_cache_get( 'last_changed', 'comment' );
976
if ( ! $last_changed ) {
977
$last_changed = microtime();
978
wp_cache_set( 'last_changed', $last_changed, 'comment' );
937
981
// Fetch an entire level of the descendant tree at a time.
940
$parent_ids = $levels[ $level ];
941
if ( ! $parent_ids ) {
945
$where = 'WHERE ' . $_where . ' AND comment_parent IN (' . implode( ',', array_map( 'intval', $parent_ids ) ) . ')';
946
$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" );
984
// Parent-child relationships may be cached. Only query for those that are not.
985
$child_ids = $uncached_parent_ids = array();
986
$_parent_ids = $levels[ $level ];
987
foreach ( $_parent_ids as $parent_id ) {
988
$cache_key = "get_comment_child_ids:$parent_id:$key:$last_changed";
989
$parent_child_ids = wp_cache_get( $cache_key, 'comment' );
990
if ( false !== $parent_child_ids ) {
991
$child_ids = array_merge( $child_ids, $parent_child_ids );
993
$uncached_parent_ids[] = $parent_id;
997
if ( $uncached_parent_ids ) {
998
$where = 'WHERE ' . $_where . ' AND comment_parent IN (' . implode( ',', array_map( 'intval', $uncached_parent_ids ) ) . ')';
999
$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" );
1001
// Cache parent-child relationships.
1002
$parent_map = array_fill_keys( $uncached_parent_ids, array() );
1003
foreach ( $level_comments as $level_comment ) {
1004
$parent_map[ $level_comment->comment_parent ][] = $level_comment->comment_ID;
1005
$child_ids[] = $level_comment->comment_ID;
1008
foreach ( $parent_map as $parent_id => $children ) {
1009
$cache_key = "get_comment_child_ids:$parent_id:$key:$last_changed";
1010
wp_cache_set( $cache_key, $children, 'comment' );
949
$levels[ $level ] = $comment_ids;
950
} while ( $comment_ids );
1015
$levels[ $level ] = $child_ids;
1016
} while ( $child_ids );
952
1018
// Prime comment caches for non-top-level comments.
953
1019
$descendant_ids = array();