Ticket #24826: 24826.2.diff
File 24826.2.diff, 6.0 KB (added by , 10 years ago) |
---|
-
src/wp-includes/comment.php
diff --git src/wp-includes/comment.php src/wp-includes/comment.php index ada21ec..84fef4a 100644
function get_comment(&$comment, $output = OBJECT) { 220 220 * 221 221 * @global wpdb $wpdb WordPress database abstraction object. 222 222 * 223 * @param string|array $args Optional. Array or string of arguments. See {@see WP_Comment_Query:: query()}223 * @param string|array $args Optional. Array or string of arguments. See {@see WP_Comment_Query::parse_query()} 224 224 * for information on accepted arguments. Default empty. 225 225 * @return int|array List of comments or number of found comments if `$count` argument is true. 226 226 */ … … 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 { 292 300 } 293 301 294 302 /** 295 * Execute the query303 * Constructor. 296 304 * 297 * @since 3.1.0 298 * @since 4.1.0 Introduced 'comment__in', 'comment__not_in', 'post_author__in', 299 * 'post_author__not_in', 'author__in', 'author__not_in', 'post__in', 300 * 'post__not_in', 'include_unapproved', 'type__in', and 'type__not_in' 301 * arguments to $query_vars. 305 * Sets up the comment query, if parameter is not empty. 306 * 307 * @since 4.2.0 308 * @access public 302 309 * 303 * @param string|array $query_vars { 310 * @param string $query URL query string. 311 * @return WP_Comment_Query 312 */ 313 function __construct( $query = '' ) { 314 if ( ! empty( $query ) ) { 315 $this->query( $query ); 316 } 317 } 318 319 /** 320 * Parse a query string and set query type booleans. 321 * 322 * @since 4.2.0 Extracted from {@link WP_Comment_Query::query()}. 323 * @access public 324 * 325 * @param string|array $query { 304 326 * Optional. Array or query string of comment query parameters. 305 327 * 306 328 * @type string $author_email Comment author email address. Default empty. … … class WP_Comment_Query { 363 385 * @type array $type__not_in Exclude comments from a given array of comment types. Default empty. 364 386 * @type int $user_id Include comments for a specific user ID. Default empty. 365 387 * } 366 * @return int|array Array of comments or number of found comments if `$count` is set to true.367 388 */ 368 public function query( $query_vars ) { 369 global $wpdb; 370 389 function parse_query( $query = '' ) { 390 if ( empty( $query ) ) { 391 $query = $this->query_vars; 392 } 371 393 $defaults = array( 372 394 'author_email' => '', 373 395 'author__in' => '', … … class WP_Comment_Query { 407 429 'date_query' => null, // See WP_Date_Query 408 430 ); 409 431 432 $this->query_vars = wp_parse_args( $query, $defaults ); 433 do_action_ref_array( 'parse_comment_query', array( &$this ) ); 434 } 435 436 /** 437 * Sets up the WordPress query for retrieving comments. 438 * 439 * @since 3.1.0 440 * @since 4.1.0 Introduced 'comment__in', 'comment__not_in', 'post_author__in', 441 * 'post_author__not_in', 'author__in', 'author__not_in', 'post__in', 442 * 'post__not_in', 'include_unapproved', 'type__in', and 'type__not_in' 443 * arguments to $query_vars. 444 * @since 4.2.0 Moved parsing to {@link WP_Comment_Query::parse_query()}. 445 446 * @access public 447 * 448 * @param string $query_vars URL query string. 449 * @return array List of comments. 450 */ 451 function query( $query_vars ) { 452 $this->query_vars = wp_parse_args( $query_vars ); 453 return $this->get_comments(); 454 } 455 456 /** 457 * Get a list of comments. 458 * 459 * @since 4.2.0 460 * 461 * @return array The list of comments. 462 */ 463 function get_comments() { 464 global $wpdb; 465 410 466 $groupby = ''; 411 467 412 $this-> query_vars = wp_parse_args( $query_vars, $defaults);468 $this->parse_query(); 413 469 414 470 // Parse meta query 415 471 $this->meta_query = new WP_Meta_Query(); … … class WP_Comment_Query { 428 484 */ 429 485 do_action_ref_array( 'pre_get_comments', array( &$this ) ); 430 486 431 // $args can be whatever, only use the args defined in defaults to compute the key432 $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $defaults ) )) );487 // $args can be whatever, only use the args defined in the query_vars to compute the key 488 $key = md5( serialize( $this->query_vars) ); 433 489 $last_changed = wp_cache_get( 'last_changed', 'comment' ); 434 490 if ( ! $last_changed ) { 435 491 $last_changed = microtime(); … … class WP_Comment_Query { 438 494 $cache_key = "get_comments:$key:$last_changed"; 439 495 440 496 if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) { 497 $this->comments = $cache; 441 498 return $cache; 442 499 } 443 500 … … class WP_Comment_Query { 825 882 $comments = apply_filters_ref_array( 'the_comments', array( $results, &$this ) ); 826 883 827 884 wp_cache_add( $cache_key, $comments, 'comment' ); 885 $this->comments = $comments; 828 886 829 887 return $comments; 830 888 } -
tests/phpunit/tests/comment/query.php
diff --git tests/phpunit/tests/comment/query.php tests/phpunit/tests/comment/query.php index 55e7d73..6fd1780 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 }