diff --git src/wp-includes/class-wp-comment-query.php src/wp-includes/class-wp-comment-query.php
index 787e769..28b17d7 100644
|
|
class WP_Comment_Query { |
35 | 35 | public $meta_query = false; |
36 | 36 | |
37 | 37 | /** |
| 38 | * Metadata query clauses. |
| 39 | * |
| 40 | * @since 4.4.0 |
| 41 | * @access protected |
| 42 | * @var array |
| 43 | */ |
| 44 | protected $meta_query_clauses; |
| 45 | |
| 46 | /** |
38 | 47 | * Date query container |
39 | 48 | * |
40 | 49 | * @since 3.7.0 |
… |
… |
class WP_Comment_Query { |
261 | 270 | public function get_comments() { |
262 | 271 | global $wpdb; |
263 | 272 | |
264 | | $groupby = ''; |
265 | | |
266 | 273 | $this->parse_query(); |
267 | 274 | |
268 | 275 | // Parse meta query |
… |
… |
class WP_Comment_Query { |
281 | 288 | // Reparse query vars, in case they were modified in a 'pre_get_comments' callback. |
282 | 289 | $this->meta_query->parse_query_vars( $this->query_vars ); |
283 | 290 | if ( ! empty( $this->meta_query->queries ) ) { |
284 | | $meta_query_clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this ); |
| 291 | $this->meta_query_clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this ); |
285 | 292 | } |
286 | 293 | |
287 | 294 | // $args can include anything. Only use the args defined in the query_var_defaults to compute the key. |
… |
… |
class WP_Comment_Query { |
291 | 298 | $last_changed = microtime(); |
292 | 299 | wp_cache_set( 'last_changed', $last_changed, 'comment' ); |
293 | 300 | } |
294 | | $cache_key = "get_comments:$key:$last_changed"; |
| 301 | $cache_key = "get_comment_ids:$key:$last_changed"; |
| 302 | |
| 303 | $comment_ids = wp_cache_get( $cache_key, 'comment' ); |
| 304 | if ( false === $comment_ids ) { |
| 305 | $comment_ids = $this->get_comment_ids(); |
| 306 | wp_cache_add( $cache_key, $comment_ids, 'comment' ); |
| 307 | } |
| 308 | |
| 309 | // If querying for a count only, there's nothing more to do. |
| 310 | if ( $this->query_vars['count'] ) { |
| 311 | return $comment_ids; |
| 312 | } |
| 313 | |
| 314 | $comment_ids = array_map( 'intval', $comment_ids ); |
295 | 315 | |
296 | | if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) { |
297 | | $this->comments = $cache; |
| 316 | if ( 'ids' == $this->query_vars['fields'] ) { |
| 317 | $this->comments = $comment_ids; |
298 | 318 | return $this->comments; |
299 | 319 | } |
300 | 320 | |
| 321 | _prime_comment_caches( $comment_ids, $this->query_vars['update_comment_meta_cache'] ); |
| 322 | |
| 323 | // Fetch full comment objects from the primed cache. |
| 324 | $_comments = array(); |
| 325 | foreach ( $comment_ids as $comment_id ) { |
| 326 | if ( $_comment = wp_cache_get( $comment_id, 'comment' ) ) { |
| 327 | $_comments[] = $_comment; |
| 328 | } |
| 329 | } |
| 330 | |
| 331 | /** |
| 332 | * Filter the comment query results. |
| 333 | * |
| 334 | * @since 3.1.0 |
| 335 | * |
| 336 | * @param array $results An array of comments. |
| 337 | * @param WP_Comment_Query &$this Current instance of WP_Comment_Query, passed by reference. |
| 338 | */ |
| 339 | $_comments = apply_filters_ref_array( 'the_comments', array( $_comments, &$this ) ); |
| 340 | |
| 341 | // Convert to WP_Comment instances |
| 342 | $comments = array_map( 'get_comment', $_comments ); |
| 343 | |
| 344 | $this->comments = $comments; |
| 345 | return $this->comments; |
| 346 | } |
| 347 | |
| 348 | /** |
| 349 | * Used internally to get a list of comment IDs matching the query vars. |
| 350 | * |
| 351 | * @since 4.4.0 |
| 352 | * @access protected |
| 353 | * |
| 354 | * @global wpdb $wpdb |
| 355 | */ |
| 356 | protected function get_comment_ids() { |
| 357 | global $wpdb; |
| 358 | |
| 359 | $groupby = ''; |
301 | 360 | $where = array(); |
302 | 361 | |
303 | 362 | // Assemble clauses related to 'comment_approved'. |
… |
… |
class WP_Comment_Query { |
455 | 514 | $orderby = "$wpdb->comments.comment_date_gmt $order"; |
456 | 515 | } |
457 | 516 | |
458 | | $number = absint( $this->query_vars['number'] ); |
459 | | $offset = absint( $this->query_vars['offset'] ); |
460 | | |
461 | | if ( ! empty( $number ) ) { |
462 | | if ( $offset ) { |
463 | | $limits = 'LIMIT ' . $offset . ',' . $number; |
464 | | } else { |
465 | | $limits = 'LIMIT ' . $number; |
466 | | } |
467 | | } else { |
468 | | $limits = ''; |
469 | | } |
470 | | |
471 | 517 | if ( $this->query_vars['count'] ) { |
472 | 518 | $fields = 'COUNT(*)'; |
473 | 519 | } else { |
474 | | switch ( strtolower( $this->query_vars['fields'] ) ) { |
475 | | case 'ids': |
476 | | $fields = "$wpdb->comments.comment_ID"; |
477 | | break; |
478 | | default: |
479 | | $fields = "*"; |
480 | | break; |
481 | | } |
| 520 | $fields = "$wpdb->comments.comment_ID"; |
482 | 521 | } |
483 | 522 | |
484 | 523 | $join = ''; |
… |
… |
class WP_Comment_Query { |
625 | 664 | $join = "JOIN $wpdb->posts ON $wpdb->posts.ID = $wpdb->comments.comment_post_ID"; |
626 | 665 | } |
627 | 666 | |
628 | | if ( ! empty( $meta_query_clauses ) ) { |
629 | | $join .= $meta_query_clauses['join']; |
| 667 | if ( ! empty( $this->meta_query_clauses ) ) { |
| 668 | $join .= $this->meta_query_clauses['join']; |
630 | 669 | |
631 | 670 | // Strip leading 'AND'. |
632 | | $where[] = preg_replace( '/^\s*AND\s*/', '', $meta_query_clauses['where'] ); |
| 671 | $where[] = preg_replace( '/^\s*AND\s*/', '', $this->meta_query_clauses['where'] ); |
633 | 672 | |
634 | 673 | if ( ! $this->query_vars['count'] ) { |
635 | 674 | $groupby = "{$wpdb->comments}.comment_ID"; |
… |
… |
class WP_Comment_Query { |
677 | 716 | $this->request = "SELECT $fields FROM $wpdb->comments $join $where $groupby $orderby $limits"; |
678 | 717 | |
679 | 718 | if ( $this->query_vars['count'] ) { |
680 | | return $wpdb->get_var( $this->request ); |
681 | | } |
682 | | |
683 | | if ( 'ids' == $this->query_vars['fields'] ) { |
684 | | $this->comments = $wpdb->get_col( $this->request ); |
685 | | return array_map( 'intval', $this->comments ); |
686 | | } |
687 | | |
688 | | $results = $wpdb->get_results( $this->request ); |
689 | | /** |
690 | | * Filter the comment query results. |
691 | | * |
692 | | * @since 3.1.0 |
693 | | * |
694 | | * @param array $results An array of comments. |
695 | | * @param WP_Comment_Query &$this Current instance of WP_Comment_Query, passed by reference. |
696 | | */ |
697 | | $_comments = apply_filters_ref_array( 'the_comments', array( $results, &$this ) ); |
698 | | |
699 | | // Convert to WP_Comment instances |
700 | | $comments = array_map( 'get_comment', $_comments ); |
701 | | |
702 | | wp_cache_add( $cache_key, $comments, 'comment' ); |
703 | | if ( '*' === $fields ) { |
704 | | update_comment_cache( $comments, $this->query_vars['update_comment_meta_cache'] ); |
| 719 | return intval( $wpdb->get_var( $this->request ) ); |
| 720 | } else { |
| 721 | $comment_ids = $wpdb->get_col( $this->request ); |
| 722 | return array_map( 'intval', $comment_ids ); |
705 | 723 | } |
706 | | |
707 | | $this->comments = $comments; |
708 | | return $this->comments; |
709 | 724 | } |
710 | 725 | |
711 | 726 | /** |
diff --git src/wp-includes/comment-functions.php src/wp-includes/comment-functions.php
index f0807b1..d10dd26 100644
|
|
function update_comment_cache( $comments, $update_meta_cache = true ) { |
2377 | 2377 | } |
2378 | 2378 | |
2379 | 2379 | /** |
| 2380 | * Adds any comments from the given ids to the cache that do not already exist in cache. |
| 2381 | * |
| 2382 | * @since 4.4.0 |
| 2383 | * @access private |
| 2384 | * |
| 2385 | * @see update_comment_cache() |
| 2386 | * |
| 2387 | * @global wpdb $wpdb |
| 2388 | * |
| 2389 | * @param array $comment_ids ID list |
| 2390 | * @param bool $update_meta_cache Optional. Whether to update the meta cache. Default true. |
| 2391 | */ |
| 2392 | function _prime_comment_caches( $comment_ids, $update_meta_cache = true ) { |
| 2393 | global $wpdb; |
| 2394 | |
| 2395 | $non_cached_ids = _get_non_cached_ids( $comment_ids, 'comment' ); |
| 2396 | if ( !empty( $non_cached_ids ) ) { |
| 2397 | $fresh_comments = $wpdb->get_results( sprintf( "SELECT $wpdb->comments.* FROM $wpdb->comments WHERE comment_ID IN (%s)", join( ",", array_map( 'intval', $non_cached_ids ) ) ) ); |
| 2398 | |
| 2399 | update_comment_cache( $fresh_comments, $update_meta_cache ); |
| 2400 | } |
| 2401 | } |
| 2402 | |
| 2403 | /** |
2380 | 2404 | * Lazy load comment meta when inside of a `WP_Query` loop. |
2381 | 2405 | * |
2382 | 2406 | * @since 4.4.0 |
diff --git tests/phpunit/tests/comment/metaCache.php tests/phpunit/tests/comment/metaCache.php
index ff7de36..edec8b8 100644
|
|
class Tests_Comment_Meta_Cache extends WP_UnitTestCase { |
17 | 17 | update_comment_meta( $cid, 'foo', 'bar' ); |
18 | 18 | } |
19 | 19 | |
| 20 | // Clear comment cache, just in case. |
| 21 | clean_comment_cache( $comment_ids ); |
| 22 | |
20 | 23 | $q = new WP_Comment_Query( array( |
21 | 24 | 'post_ID' => $p, |
22 | 25 | ) ); |
… |
… |
class Tests_Comment_Meta_Cache extends WP_UnitTestCase { |
42 | 45 | update_comment_meta( $cid, 'foo', 'bar' ); |
43 | 46 | } |
44 | 47 | |
| 48 | // Clear comment cache, just in case. |
| 49 | clean_comment_cache( $comment_ids ); |
| 50 | |
45 | 51 | $q = new WP_Comment_Query( array( |
46 | 52 | 'post_ID' => $p, |
47 | 53 | 'update_comment_meta_cache' => true, |
diff --git tests/phpunit/tests/comment/query.php tests/phpunit/tests/comment/query.php
index cecf92c..73ab4aa 100644
|
|
class Tests_Comment_Query extends WP_UnitTestCase { |
1667 | 1667 | $q1 = new WP_Comment_Query(); |
1668 | 1668 | $q1->query( array( |
1669 | 1669 | 'post_id' => $p, |
| 1670 | 'fields' => 'ids', |
1670 | 1671 | ) ); |
1671 | 1672 | |
1672 | 1673 | $num_queries = $wpdb->num_queries; |
… |
… |
class Tests_Comment_Query extends WP_UnitTestCase { |
1674 | 1675 | $q2 = new WP_Comment_Query(); |
1675 | 1676 | $q2->query( array( |
1676 | 1677 | 'post_id' => $p, |
| 1678 | 'fields' => 'ids', |
1677 | 1679 | 'foo' => 'bar', |
1678 | 1680 | ) ); |
1679 | 1681 | |