Ticket #43298: 43298.patch
File 43298.patch, 6.6 KB (added by , 7 years ago) |
---|
-
src/wp-includes/class-wp-comment-query.php
diff --git src/wp-includes/class-wp-comment-query.php src/wp-includes/class-wp-comment-query.php index 34255953f5..d94ddde252 100644
class WP_Comment_Query { 379 379 $this->meta_query_clauses = $this->meta_query->get_sql( 'comment', $wpdb->comments, 'comment_ID', $this ); 380 380 } 381 381 382 /* 383 * Add the blacklist filter to the query vars, as it can impact query 384 * results and should trigger cache refreshes when modified. 385 */ 386 $this->query_vars['type__not_in'] = array_unique( array_merge( 387 (array) $this->query_vars['type__not_in'], 388 /** This filter is documented in wp-includes/class-wp-comment-query */ 389 (array) apply_filters( 'wp_hidden_comment_types', array() ) 390 ) ); 391 382 392 /* 383 393 * Only use the args defined in the query_var_defaults to compute the key, 384 394 * but ignore 'fields', which does not affect query results. … … class WP_Comment_Query { 465 475 return $this->comments; 466 476 } 467 477 478 /** 479 * Get an SQL clause to exclude hidden comment types. 480 * 481 * @since 5.0.0 482 * 483 * @param string $conjunction Optional. Conjunction word to use. Defaults to 484 * 'WHERE'. 485 * 486 * @return string SQL clause that excludes hidden comment types. 487 */ 488 public static function get_hidden_comment_types_clause( $conjunction = 'WHERE' ) { 489 global $wpdb; 490 491 /** 492 * Filters the list of hidden comment types. 493 * 494 * Hidden comment types are excluded from the default comment queries 495 * and comment counts. 496 * 497 * @since 5.0.0 498 * 499 * @param array Array of comment types to hide by default. 500 */ 501 $hidden_types = apply_filters( 'wp_hidden_comment_types', array() ); 502 $hidden_types = array_filter( (array) $hidden_types ); 503 504 $type_clauses = array(); 505 foreach ( $hidden_types as $hidden_type ) { 506 $type_clauses[] = $wpdb->prepare( 507 'comment_type NOT IN (%s)', 508 $hidden_type 509 ); 510 } 511 512 return ! empty( $type_clauses ) 513 ? sprintf( 514 '%s %s', 515 $conjunction, 516 implode( ' AND ', $type_clauses ) 517 ) 518 : ''; 519 } 520 468 521 /** 469 522 * Used internally to get a list of comment IDs matching the query vars. 470 523 * … … class WP_Comment_Query { 699 752 700 753 // Filtering by comment_type: 'type', 'type__in', 'type__not_in'. 701 754 $raw_types = array( 702 'IN' => array_merge( (array) $this->query_vars['type'], (array) $this->query_vars['type__in'] ), 755 'IN' => array_unique( array_merge( 756 (array) $this->query_vars['type'], 757 (array) $this->query_vars['type__in'] 758 ) ), 703 759 'NOT IN' => (array) $this->query_vars['type__not_in'], 704 760 ); 705 761 762 /* 763 * In case a specific comment type was requested, ensure it was not 764 * already blacklisted at the same time. 765 * This means that explicit type inclusion trumps type exclusion. 766 */ 767 $raw_types['NOT IN'] = array_diff( $raw_types['NOT IN'], $raw_types['IN'] ); 768 706 769 $comment_types = array(); 707 770 foreach ( $raw_types as $operator => $_raw_types ) { 708 $_raw_types = array_unique( $_raw_types );709 710 771 foreach ( $_raw_types as $type ) { 711 772 switch ( $type ) { 712 773 // An empty translates to 'all', for backward compatibility -
src/wp-includes/comment.php
diff --git src/wp-includes/comment.php src/wp-includes/comment.php index 0d35f091f9..d4851854db 100644
function get_comment_count( $post_id = 0 ) { 371 371 $post_id = (int) $post_id; 372 372 373 373 $where = ''; 374 374 375 if ( $post_id > 0 ) { 375 376 $where = $wpdb->prepare( 'WHERE comment_post_ID = %d', $post_id ); 376 377 } 377 378 379 $where .= WP_Comment_Query::get_hidden_comment_types_clause( 380 empty( $where ) ? 'WHERE' : 'AND' 381 ); 382 378 383 $totals = (array) $wpdb->get_results( 379 384 " 380 385 SELECT comment_approved, COUNT( * ) AS total … … function wp_update_comment_count_now( $post_id ) { 2439 2444 $new = apply_filters( 'pre_wp_update_comment_count_now', null, $old, $post_id ); 2440 2445 2441 2446 if ( is_null( $new ) ) { 2442 $new = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id ) ); 2447 $sql = $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1'", $post_id ); 2448 2449 $sql .= WP_Comment_Query::get_hidden_comment_types_clause( 'AND' ); 2450 2451 $new = (int) $wpdb->get_var( $sql ); 2443 2452 } else { 2444 2453 $new = (int) $new; 2445 2454 } -
new file tests/phpunit/tests/comment/commentTypes.php
diff --git tests/phpunit/tests/comment/commentTypes.php tests/phpunit/tests/comment/commentTypes.php new file mode 100644 index 0000000000..9cd4995ba3
- + 1 <?php 2 3 /** 4 * @group comment 5 */ 6 class Tests_Comment_Types extends WP_UnitTestCase { 7 8 public function filter_comment_type_blacklist( $blacklist ) { 9 $blacklist[] = 'test_comment_type'; 10 return $blacklist; 11 } 12 13 /** 14 * @ticket 43298 15 */ 16 public function test_get_comments_is_filtered_on_type() { 17 $comments = get_comments(); 18 $this->assertEquals( 0, count( $comments ) ); 19 20 self::factory()->comment->create( 21 array( 'comment_type' => 'test_comment_type' ) 22 ); 23 24 $comments = get_comments(); 25 $this->assertEquals( 1, count( $comments ) ); 26 27 add_filter( 28 'wp_hidden_comment_types', 29 array( $this, 'filter_comment_type_blacklist' ) 30 ); 31 32 $comments = get_comments(); 33 $this->assertEquals( 0, count( $comments ) ); 34 } 35 36 /** 37 * @ticket 43298 38 */ 39 public function test_get_comment_count_is_filtered_on_type() { 40 $count = get_comment_count(); 41 $this->assertEquals( 0, $count['all'] ); 42 43 self::factory()->comment->create( 44 array( 'comment_type' => 'test_comment_type' ) 45 ); 46 47 $count = get_comment_count(); 48 $this->assertEquals( 1, $count['all'] ); 49 50 add_filter( 51 'wp_hidden_comment_types', 52 array( $this, 'filter_comment_type_blacklist' ) 53 ); 54 55 $count = get_comment_count(); 56 $this->assertEquals( 0, $count['all'] ); 57 } 58 59 /** 60 * @ticket 43298 61 */ 62 public function test_wp_update_comment_count_is_filtered_on_type() { 63 $post_id = self::factory()->post->create( 64 array( 'post_author' => 1 ) 65 ); 66 67 wp_update_comment_count( $post_id ); 68 $post = get_post( $post_id ); 69 $this->assertEquals( 0, $post->comment_count ); 70 71 self::factory()->comment->create( 72 array( 73 'comment_post_ID' => $post_id, 74 'comment_type' => 'test_comment_type', 75 ) 76 ); 77 78 wp_update_comment_count( $post_id ); 79 $post = get_post( $post_id ); 80 $this->assertEquals( 1, $post->comment_count ); 81 82 add_filter( 83 'wp_hidden_comment_types', 84 array( $this, 'filter_comment_type_blacklist' ) 85 ); 86 87 wp_update_comment_count( $post_id ); 88 $post = get_post( $post_id ); 89 $this->assertEquals( 0, $post->comment_count ); 90 } 91 }