Ticket #29612: 29612.patch
| File 29612.patch, 12.6 KB (added by , 11 years ago) |
|---|
-
src/wp-includes/comment.php
diff --git src/wp-includes/comment.php src/wp-includes/comment.php index f374675..5d4076e 100644
class WP_Comment_Query { 293 293 'post_parent' => '', 294 294 'post_status' => '', 295 295 'post_type' => '', 296 'status' => ' ',296 'status' => 'all', 297 297 'type' => '', 298 298 'user_id' => '', 299 299 'search' => '', … … class WP_Comment_Query { 334 334 return $cache; 335 335 } 336 336 337 $where = array(); 338 337 339 // Assemble clauses related to 'comment_approved'. 338 340 $approved_clauses = array(); 339 $status = $this->query_vars['status']; 340 if ( 'hold' == $status ) { 341 $approved_clauses[] = "comment_approved = '0'"; 342 } elseif ( 'approve' == $status ) { 343 $approved_clauses[] = "comment_approved = '1'"; 344 } elseif ( ! empty( $status ) && 'all' != $status ) { 345 $approved_clauses[] = $wpdb->prepare( "comment_approved = %s", $status ); 346 } else { 347 $approved_clauses[] = "( comment_approved = '0' OR comment_approved = '1' )"; 341 342 // 'status' accepts an array or a comma-separated string. 343 $status_clauses = array(); 344 $statuses = $this->query_vars['status']; 345 if ( ! is_array( $statuses ) ) { 346 $statuses = preg_split( '/[\s,]+/', $statuses ); 347 } 348 349 // Remove empty statuses. 350 $statuses = array_filter( $statuses ); 351 352 // 'any' overrides other statuses. 353 if ( ! in_array( 'any', $statuses ) ) { 354 foreach ( $statuses as $status ) { 355 switch ( $status ) { 356 case 'hold' : 357 $status_clauses[] = "comment_approved = '0'"; 358 break; 359 360 case 'approve' : 361 $status_clauses[] = "comment_approved = '1'"; 362 break; 363 364 case 'all' : 365 $status_clauses[] = "( comment_approved = '0' OR comment_approved = '1' )"; 366 break; 367 368 default : 369 $status_clauses[] = $wpdb->prepare( "comment_approved = %s", $status ); 370 break; 371 } 372 } 373 374 if ( ! empty( $status_clauses ) ) { 375 $approved_clauses[] = '( ' . implode( ' OR ', $status_clauses ) . ' )'; 376 } 348 377 } 349 378 350 379 // User IDs or emails whose unapproved comments are included, regardless of $status. … … class WP_Comment_Query { 370 399 } 371 400 372 401 // Collapse comment_approved clauses into a single OR-separated clause. 373 if ( 1 === count( $approved_clauses ) ) { 374 $approved = $approved_clauses[0]; 375 } else { 376 $approved = '( ' . implode( ' OR ', $approved_clauses ) . ' )'; 402 if ( ! empty( $approved_clauses ) ) { 403 if ( 1 === count( $approved_clauses ) ) { 404 $where[] = $approved_clauses[0]; 405 } else { 406 $where[] = '( ' . implode( ' OR ', $approved_clauses ) . ' )'; 407 } 377 408 } 378 409 379 410 $order = ( 'ASC' == strtoupper( $this->query_vars['order'] ) ) ? 'ASC' : 'DESC'; … … class WP_Comment_Query { 445 476 } 446 477 447 478 $join = ''; 448 $where = $approved;449 479 450 480 $post_id = absint( $this->query_vars['post_id'] ); 451 481 if ( ! empty( $post_id ) ) { 452 $where .= $wpdb->prepare( ' ANDcomment_post_ID = %d', $post_id );482 $where[] = $wpdb->prepare( 'comment_post_ID = %d', $post_id ); 453 483 } 454 484 455 485 // Parse comment IDs for an IN clause. 456 486 if ( ! empty( $this->query_vars['comment__in'] ) ) { 457 $where .= ' ANDcomment_ID IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['comment__in'] ) ) . ' )';487 $where[] = 'comment_ID IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['comment__in'] ) ) . ' )'; 458 488 } 459 489 460 490 // Parse comment IDs for a NOT IN clause. 461 491 if ( ! empty( $this->query_vars['comment__not_in'] ) ) { 462 $where .= ' ANDcomment_ID NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['comment__not_in'] ) ) . ' )';492 $where[] = 'comment_ID NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['comment__not_in'] ) ) . ' )'; 463 493 } 464 494 465 495 // Parse comment post IDs for an IN clause. 466 496 if ( ! empty( $this->query_vars['post__in'] ) ) { 467 $where .= ' ANDcomment_post_ID IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__in'] ) ) . ' )';497 $where[] = 'comment_post_ID IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__in'] ) ) . ' )'; 468 498 } 469 499 470 500 // Parse comment post IDs for a NOT IN clause. 471 501 if ( ! empty( $this->query_vars['post__not_in'] ) ) { 472 $where .= ' ANDcomment_post_ID NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__not_in'] ) ) . ' )';502 $where[] = 'comment_post_ID NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post__not_in'] ) ) . ' )'; 473 503 } 474 504 475 505 if ( '' !== $this->query_vars['author_email'] ) { 476 $where .= $wpdb->prepare( ' ANDcomment_author_email = %s', $this->query_vars['author_email'] );506 $where[] = $wpdb->prepare( 'comment_author_email = %s', $this->query_vars['author_email'] ); 477 507 } 478 508 479 509 if ( '' !== $this->query_vars['karma'] ) { 480 $where .= $wpdb->prepare( ' ANDcomment_karma = %d', $this->query_vars['karma'] );510 $where[] = $wpdb->prepare( 'comment_karma = %d', $this->query_vars['karma'] ); 481 511 } 482 512 483 513 if ( 'comment' == $this->query_vars['type'] ) { 484 $where .= " ANDcomment_type = ''";514 $where[] = "comment_type = ''"; 485 515 } elseif( 'pings' == $this->query_vars['type'] ) { 486 $where .= ' ANDcomment_type IN ("pingback", "trackback")';516 $where[] = 'comment_type IN ("pingback", "trackback")'; 487 517 } elseif ( ! empty( $this->query_vars['type'] ) ) { 488 $where .= $wpdb->prepare( ' ANDcomment_type = %s', $this->query_vars['type'] );518 $where[] = $wpdb->prepare( 'comment_type = %s', $this->query_vars['type'] ); 489 519 } 490 520 491 521 if ( '' !== $this->query_vars['parent'] ) { 492 $where .= $wpdb->prepare( ' ANDcomment_parent = %d', $this->query_vars['parent'] );522 $where[] = $wpdb->prepare( 'comment_parent = %d', $this->query_vars['parent'] ); 493 523 } 494 524 495 525 if ( is_array( $this->query_vars['user_id'] ) ) { 496 $where .= ' ANDuser_id IN (' . implode( ',', array_map( 'absint', $this->query_vars['user_id'] ) ) . ')';526 $where[] = 'user_id IN (' . implode( ',', array_map( 'absint', $this->query_vars['user_id'] ) ) . ')'; 497 527 } elseif ( '' !== $this->query_vars['user_id'] ) { 498 $where .= $wpdb->prepare( ' ANDuser_id = %d', $this->query_vars['user_id'] );528 $where[] = $wpdb->prepare( 'user_id = %d', $this->query_vars['user_id'] ); 499 529 } 500 530 501 531 if ( '' !== $this->query_vars['search'] ) { 502 $ where .= $this->get_search_sql(532 $search_sql = $this->get_search_sql( 503 533 $this->query_vars['search'], 504 534 array( 'comment_author', 'comment_author_email', 'comment_author_url', 'comment_author_IP', 'comment_content' ) 505 535 ); 536 537 // Strip leading 'AND'. 538 $where[] = preg_replace( '/^\s*AND\s*/', '', $search_sql ); 506 539 } 507 540 508 541 // If any post-related query vars are passed, join the posts table. … … class WP_Comment_Query { 513 546 if ( ! empty( $post_fields ) ) { 514 547 $join_posts_table = true; 515 548 foreach ( $post_fields as $field_name => $field_value ) { 516 $where .= $wpdb->prepare( " AND{$wpdb->posts}.{$field_name} = %s", $field_value );549 $where[] = $wpdb->prepare( " {$wpdb->posts}.{$field_name} = %s", $field_value ); 517 550 } 518 551 } 519 552 520 553 // Comment author IDs for an IN clause. 521 554 if ( ! empty( $this->query_vars['author__in'] ) ) { 522 $where .= ' ANDuser_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__in'] ) ) . ' )';555 $where[] = 'user_id IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__in'] ) ) . ' )'; 523 556 } 524 557 525 558 // Comment author IDs for a NOT IN clause. 526 559 if ( ! empty( $this->query_vars['author__not_in'] ) ) { 527 $where .= ' ANDuser_id NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__not_in'] ) ) . ' )';560 $where[] = 'user_id NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['author__not_in'] ) ) . ' )'; 528 561 } 529 562 530 563 // Post author IDs for an IN clause. 531 564 if ( ! empty( $this->query_vars['post_author__in'] ) ) { 532 565 $join_posts_table = true; 533 $where .= ' ANDpost_author IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__in'] ) ) . ' )';566 $where[] = 'post_author IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__in'] ) ) . ' )'; 534 567 } 535 568 536 569 // Post author IDs for a NOT IN clause. 537 570 if ( ! empty( $this->query_vars['post_author__not_in'] ) ) { 538 571 $join_posts_table = true; 539 $where .= ' ANDpost_author NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__not_in'] ) ) . ' )';572 $where[] = 'post_author NOT IN ( ' . implode( ',', wp_parse_id_list( $this->query_vars['post_author__not_in'] ) ) . ' )'; 540 573 } 541 574 542 575 if ( $join_posts_table ) { … … class WP_Comment_Query { 546 579 if ( ! empty( $this->meta_query->queries ) ) { 547 580 $clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this ); 548 581 $join .= $clauses['join']; 549 $where .= $clauses['where']; 582 583 // Strip leading 'AND'. 584 $where[] = preg_replace( '/^\s*AND\s*/', '', $clauses['where'] ); 585 550 586 $groupby = "{$wpdb->comments}.comment_ID"; 551 587 } 552 588 553 589 $date_query = $this->query_vars['date_query']; 554 590 if ( ! empty( $date_query ) && is_array( $date_query ) ) { 555 591 $date_query_object = new WP_Date_Query( $date_query, 'comment_date' ); 556 $where .= $date_query_object->get_sql(); 592 // Strip leading 'AND'. 593 $where[] = preg_replace( '/^\s*AND\s*/', '', $date_query_object->get_sql() ); 557 594 } 558 595 596 $where = implode( ' AND ', $where ); 597 559 598 $pieces = array( 'fields', 'join', 'where', 'orderby', 'order', 'limits', 'groupby' ); 560 599 /** 561 600 * Filter the comment query clauses. … … class WP_Comment_Query { 575 614 $limits = isset( $clauses[ 'limits' ] ) ? $clauses[ 'limits' ] : ''; 576 615 $groupby = isset( $clauses[ 'groupby' ] ) ? $clauses[ 'groupby' ] : ''; 577 616 617 if ( $where ) { 618 $where = 'WHERE ' . $where; 619 } 620 578 621 if ( $groupby ) { 579 622 $groupby = 'GROUP BY ' . $groupby; 580 623 } 581 $query = "SELECT $fields FROM $wpdb->comments $join WHERE $where $groupby ORDER BY $orderby $order $limits"; 624 625 $query = "SELECT $fields FROM $wpdb->comments $join $where $groupby ORDER BY $orderby $order $limits"; 582 626 583 627 if ( $this->query_vars['count'] ) { 584 628 return $wpdb->get_var( $query ); -
tests/phpunit/tests/comment/query.php
diff --git tests/phpunit/tests/comment/query.php tests/phpunit/tests/comment/query.php index 61f39ac..be9136b 100644
class Tests_Comment_Query extends WP_UnitTestCase { 75 75 $this->assertEqualSets( array( $c1, $c3 ), $found ); 76 76 } 77 77 78 public function test_status_default_to_all() { 79 $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) ); 80 $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => 'foo' ) ); 81 $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '0' ) ); 82 83 $q = new WP_Comment_Query(); 84 $found = $q->query( array( 85 'fields' => 'ids', 86 ) ); 87 88 $this->assertEqualSets( array( $c1, $c3 ), $found ); 89 } 90 91 /** 92 * @ticket 29612 93 */ 94 public function test_status_comma_any() { 95 $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) ); 96 $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => 'foo' ) ); 97 $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '0' ) ); 98 99 $q = new WP_Comment_Query(); 100 $found = $q->query( array( 101 'status' => 'any', 102 'fields' => 'ids', 103 ) ); 104 105 $this->assertEqualSets( array( $c1, $c2, $c3 ), $found ); 106 } 107 108 /** 109 * @ticket 29612 110 */ 111 public function test_status_comma_separated() { 112 $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) ); 113 $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => 'foo' ) ); 114 $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '0' ) ); 115 116 $q = new WP_Comment_Query(); 117 $found = $q->query( array( 118 'status' => 'approve,foo,bar', 119 'fields' => 'ids', 120 ) ); 121 122 $this->assertEqualSets( array( $c1, $c2 ), $found ); 123 } 124 125 /** 126 * @ticket 29612 127 */ 128 public function test_status_array() { 129 $c1 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '1' ) ); 130 $c2 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => 'foo' ) ); 131 $c3 = $this->factory->comment->create( array( 'comment_post_ID' => $this->post_id, 'comment_approved' => '0' ) ); 132 133 $q = new WP_Comment_Query(); 134 $found = $q->query( array( 135 'status' => array( 'approve', 'foo', 'bar', ), 136 'fields' => 'ids', 137 ) ); 138 139 $this->assertEqualSets( array( $c1, $c2 ), $found ); 140 } 141 78 142 function test_get_comments_for_post() { 79 143 $post_id = $this->factory->post->create(); 80 144 $this->factory->comment->create_post_comments( $post_id, 10 );