Changeset 31793
- Timestamp:
- 03/16/2015 02:23:33 PM (10 years ago)
- Location:
- trunk
- Files:
-
- 2 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/src/wp-includes/comment.php
r31615 r31793 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. … … 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 */ … … 271 275 272 276 /** 277 * Default values for query vars. 278 * 279 * @since 4.2.0 280 * @access public 281 * @var array 282 */ 283 public $query_var_defaults; 284 285 /** 286 * List of comments located by the query. 287 * 288 * @since 4.0.0 289 * @access public 273 290 * @var array 274 291 */ … … 293 310 294 311 /** 295 * Execute the query 296 * 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. 302 * 303 * @param string|array $query_vars { 312 * Constructor. 313 * 314 * Sets up the comment query, based on the query vars passed. 315 * 316 * @since 4.2.0 317 * @access public 318 * 319 * @param string $query URL query string. 320 * @return WP_Comment_Query 321 */ 322 public function __construct( $query = '' ) { 323 $this->query_var_defaults = array( 324 'author_email' => '', 325 'author__in' => '', 326 'author__not_in' => '', 327 'include_unapproved' => '', 328 'fields' => '', 329 'ID' => '', 330 'comment__in' => '', 331 'comment__not_in' => '', 332 'karma' => '', 333 'number' => '', 334 'offset' => '', 335 'orderby' => '', 336 'order' => 'DESC', 337 'parent' => '', 338 'post_author__in' => '', 339 'post_author__not_in' => '', 340 'post_ID' => '', 341 'post_id' => 0, 342 'post__in' => '', 343 'post__not_in' => '', 344 'post_author' => '', 345 'post_name' => '', 346 'post_parent' => '', 347 'post_status' => '', 348 'post_type' => '', 349 'status' => 'all', 350 'type' => '', 351 'type__in' => '', 352 'type__not_in' => '', 353 'user_id' => '', 354 'search' => '', 355 'count' => false, 356 'meta_key' => '', 357 'meta_value' => '', 358 'meta_query' => '', 359 'date_query' => null, // See WP_Date_Query 360 ); 361 362 if ( ! empty( $query ) ) { 363 $this->query( $query ); 364 } 365 } 366 367 /** 368 * Parse arguments passed to the comment query with default query parameters. 369 * 370 * @since 4.2.0 Extracted from {@link WP_Comment_Query::query()}. 371 * @access public 372 * 373 * @param string|array $query { 304 374 * Optional. Array or query string of comment query parameters. 305 375 * … … 364 434 * @type int $user_id Include comments for a specific user ID. Default empty. 365 435 * } 366 * @return int|array Array of comments or number of found comments if `$count` is set to true. 367 */ 368 public function query( $query_vars ) { 436 */ 437 public function parse_query( $query = '' ) { 438 if ( empty( $query ) ) { 439 $query = $this->query_vars; 440 } 441 442 $this->query_vars = wp_parse_args( $query, $this->query_var_defaults ); 443 do_action_ref_array( 'parse_comment_query', array( &$this ) ); 444 } 445 446 /** 447 * Sets up the WordPress query for retrieving comments. 448 * 449 * @since 3.1.0 450 * @since 4.1.0 Introduced 'comment__in', 'comment__not_in', 'post_author__in', 451 * 'post_author__not_in', 'author__in', 'author__not_in', 'post__in', 452 * 'post__not_in', 'include_unapproved', 'type__in', and 'type__not_in' 453 * arguments to $query_vars. 454 * @since 4.2.0 Moved parsing to {@link WP_Comment_Query::parse_query()}. 455 * @access public 456 * 457 * @param string|array $query Array or URL query string of parameters. 458 * @return array List of comments. 459 */ 460 public function query( $query ) { 461 $this->query_vars = wp_parse_args( $query ); 462 return $this->get_comments(); 463 } 464 465 /** 466 * Get a list of comments matching the query vars. 467 * 468 * @since 4.2.0 469 * @access public 470 * 471 * @return array The list of comments. 472 */ 473 public function get_comments() { 369 474 global $wpdb; 370 475 371 $defaults = array(372 'author_email' => '',373 'author__in' => '',374 'author__not_in' => '',375 'include_unapproved' => '',376 'fields' => '',377 'ID' => '',378 'comment__in' => '',379 'comment__not_in' => '',380 'karma' => '',381 'number' => '',382 'offset' => '',383 'orderby' => '',384 'order' => 'DESC',385 'parent' => '',386 'post_author__in' => '',387 'post_author__not_in' => '',388 'post_ID' => '',389 'post_id' => 0,390 'post__in' => '',391 'post__not_in' => '',392 'post_author' => '',393 'post_name' => '',394 'post_parent' => '',395 'post_status' => '',396 'post_type' => '',397 'status' => 'all',398 'type' => '',399 'type__in' => '',400 'type__not_in' => '',401 'user_id' => '',402 'search' => '',403 'count' => false,404 'meta_key' => '',405 'meta_value' => '',406 'meta_query' => '',407 'date_query' => null, // See WP_Date_Query408 );409 410 476 $groupby = ''; 411 477 412 $this-> query_vars = wp_parse_args( $query_vars, $defaults);478 $this->parse_query(); 413 479 414 480 // Parse meta query … … 429 495 do_action_ref_array( 'pre_get_comments', array( &$this ) ); 430 496 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 ) ) ));497 // $args can include anything. Only use the args defined in the query_var_defaults to compute the key. 498 $key = md5( serialize( wp_array_slice_assoc( $this->query_vars, array_keys( $this->query_var_defaults ) ) ) ); 433 499 $last_changed = wp_cache_get( 'last_changed', 'comment' ); 434 500 if ( ! $last_changed ) { … … 439 505 440 506 if ( $cache = wp_cache_get( $cache_key, 'comment' ) ) { 441 return $cache; 507 $this->comments = $cache; 508 return $this->comments; 442 509 } 443 510 … … 827 894 wp_cache_add( $cache_key, $comments, 'comment' ); 828 895 829 return $comments; 896 $this->comments = $comments; 897 return $this->comments; 830 898 } 831 899 -
trunk/tests/phpunit/tests/comment/query.php
r31666 r31793 1588 1588 $this->assertEqualSets( array_merge( $c1, $c3 ), $found ); 1589 1589 } 1590 1591 /** 1592 * @ticket 24826 1593 */ 1594 public 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 } 1610 1611 /** 1612 * @ticket 22400 1613 */ 1614 public function test_comment_cache_key_should_ignore_custom_params() { 1615 global $wpdb; 1616 1617 $p = $this->factory->post->create(); 1618 $c = $this->factory->comment->create( array( 'comment_post_ID' => $p ) ); 1619 1620 $q1 = new WP_Comment_Query(); 1621 $q1->query( array( 1622 'post_id' => $p, 1623 ) ); 1624 1625 $num_queries = $wpdb->num_queries; 1626 1627 $q2 = new WP_Comment_Query(); 1628 $q2->query( array( 1629 'post_id' => $p, 1630 'foo' => 'bar', 1631 ) ); 1632 1633 $this->assertSame( $num_queries, $wpdb->num_queries ); 1634 } 1590 1635 }
Note: See TracChangeset
for help on using the changeset viewer.