diff --git src/wp-includes/comment.php src/wp-includes/comment.php
index ada21ec..1f8b352 100644
|
|
class WP_Comment_Query { |
265 | 265 | public $date_query = false; |
266 | 266 | |
267 | 267 | /** |
| 268 | * Query vars set by the user |
| 269 | * |
| 270 | * @since 3.1.0 |
| 271 | * @access public |
268 | 272 | * @var array |
269 | 273 | */ |
270 | 274 | public $query_vars; |
271 | 275 | |
272 | 276 | /** |
| 277 | * List of comments. |
| 278 | * |
| 279 | * @since 4.0.0 |
| 280 | * @access public |
273 | 281 | * @var array |
274 | 282 | */ |
275 | 283 | public $comments; |
… |
… |
class WP_Comment_Query { |
365 | 373 | * } |
366 | 374 | * @return int|array Array of comments or number of found comments if `$count` is set to true. |
367 | 375 | */ |
368 | | public function query( $query_vars ) { |
369 | | global $wpdb; |
370 | 376 | |
| 377 | /** |
| 378 | * Constructor. |
| 379 | * |
| 380 | * Sets up the comment query, if parameter is not empty. |
| 381 | * |
| 382 | * @since 4.0.0 |
| 383 | * @access public |
| 384 | * |
| 385 | * @param string $query URL query string. |
| 386 | * @return WP_Comment_Query |
| 387 | */ |
| 388 | function __construct( $query = '' ) { |
| 389 | if ( ! empty( $query ) ) { |
| 390 | $this->query( $query ); |
| 391 | } |
| 392 | } |
| 393 | |
| 394 | /** |
| 395 | * Parse a query string and set query type booleans. |
| 396 | * |
| 397 | * @since 1.5.0 |
| 398 | * @access public |
| 399 | * |
| 400 | * @param string|array $query Optional query. |
| 401 | */ |
| 402 | function parse_query( $query = '' ) { |
| 403 | if ( empty( $query ) ) { |
| 404 | $query = $this->query_vars; |
| 405 | } |
371 | 406 | $defaults = array( |
372 | 407 | 'author_email' => '', |
373 | 408 | 'author__in' => '', |
… |
… |
class WP_Comment_Query { |
407 | 442 | 'date_query' => null, // See WP_Date_Query |
408 | 443 | ); |
409 | 444 | |
| 445 | $this->query_vars = wp_parse_args( $query, $defaults ); |
| 446 | do_action_ref_array('parse_comment_query', array(&$this)); |
| 447 | } |
| 448 | |
| 449 | /** |
| 450 | * Sets up the WordPress query by parsing query string. |
| 451 | * |
| 452 | * @since 1.5.0 |
| 453 | * @access public |
| 454 | * |
| 455 | * @param string $query_vars URL query string. |
| 456 | * @return array List of posts. |
| 457 | */ |
| 458 | function query( $query_vars ) { |
| 459 | $this->query_vars = wp_parse_args( $query_vars ); |
| 460 | return $this->get_comments(); |
| 461 | } |
| 462 | |
| 463 | /** |
| 464 | * Execute the query |
| 465 | * |
| 466 | * @since 4.0.0 |
| 467 | * |
| 468 | * @return int|array |
| 469 | */ |
| 470 | function get_comments() { |
| 471 | global $wpdb; |
| 472 | |
410 | 473 | $groupby = ''; |
411 | 474 | |
412 | | $this->query_vars = wp_parse_args( $query_vars, $defaults ); |
| 475 | $this->parse_query(); |
413 | 476 | |
414 | 477 | // Parse meta query |
415 | 478 | $this->meta_query = new WP_Meta_Query(); |
… |
… |
class WP_Comment_Query { |
428 | 491 | */ |
429 | 492 | do_action_ref_array( 'pre_get_comments', array( &$this ) ); |
430 | 493 | |
431 | | // $args can be whatever, only use the args defined in defaults to compute the key |
432 | | $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $defaults ) ) ) ); |
| 494 | // $args can be whatever, only use the args defined in the query_vars to compute the key |
| 495 | $key = md5( serialize( $this->query_vars) ); |
433 | 496 | $last_changed = wp_cache_get( 'last_changed', 'comment' ); |
434 | 497 | if ( ! $last_changed ) { |
435 | 498 | $last_changed = microtime(); |
… |
… |
class WP_Comment_Query { |
438 | 501 | $cache_key = "get_comments:$key:$last_changed"; |
439 | 502 | |
440 | 503 | if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) { |
| 504 | $this->comments = $cache; |
441 | 505 | return $cache; |
442 | 506 | } |
443 | 507 | |
… |
… |
class WP_Comment_Query { |
825 | 889 | $comments = apply_filters_ref_array( 'the_comments', array( $results, &$this ) ); |
826 | 890 | |
827 | 891 | wp_cache_add( $cache_key, $comments, 'comment' ); |
| 892 | $this->comments = $comments; |
828 | 893 | |
829 | 894 | return $comments; |
830 | 895 | } |
diff --git tests/phpunit/tests/comment/query.php tests/phpunit/tests/comment/query.php
index 55e7d73..4f16a1d 100644
|
|
class Tests_Comment_Query extends WP_UnitTestCase { |
1587 | 1587 | |
1588 | 1588 | $this->assertEqualSets( array_merge( $c1, $c3 ), $found ); |
1589 | 1589 | } |
| 1590 | |
| 1591 | /** |
| 1592 | * Ticket 24826 |
| 1593 | */ |
| 1594 | function test_comment_query_object() { |
| 1595 | $comment_id = $this->factory->comment->create(); |
| 1596 | |
| 1597 | $query1 = new WP_Comment_Query(); |
| 1598 | $this->assertNull( $query1->query_vars ); |
| 1599 | $this->assertEmpty( $query1->comments ); |
| 1600 | $comments = $query1->query( array( 'status' => 'all' ) ); |
| 1601 | $this->assertInternalType( 'array', $query1->query_vars ); |
| 1602 | $this->assertNotEmpty( $query1->comments ); |
| 1603 | $this->assertInternalType( 'array', $query1->comments ); |
| 1604 | |
| 1605 | $query2 = new WP_Comment_Query( array( 'status' => 'all' ) ); |
| 1606 | $this->assertNotEmpty( $query2->query_vars ); |
| 1607 | $this->assertNotEmpty( $query2->comments ); |
| 1608 | $this->assertEquals( $query2->comments, $query1->get_comments() ); |
| 1609 | } |
1590 | 1610 | } |