Skip to content

Commit 9cabe83

Browse files
Comments: Exclude notes from comment feed queries.
Add `comment_type != 'note'` to the three raw comment feed queries, matching the exclusion already used by `WP_Comment_Query` and `wp_count_comments()`. [61105] excluded the `note` type in `WP_Comment_Query`, but the comment feed queries are built with raw SQL that bypasses `WP_Comment_Query` entirely, so the exclusion never reached feeds. Follow-up to [61105]. Props westonruter, wildworks, mukesh27, odkdn1, khokansardar. Fixes #65613. git-svn-id: https://develop.svn.wordpress.org/trunk@62804 602fd350-edb4-49c9-b593-d223f7449a82
1 parent d792cbd commit 9cabe83

2 files changed

Lines changed: 99 additions & 5 deletions

File tree

src/wp-includes/class-wp-query.php

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2825,11 +2825,11 @@ public function get_posts() {
28252825
if ( $this->is_comment_feed && ! $this->is_singular ) {
28262826
if ( $this->is_archive || $this->is_search ) {
28272827
$cjoin = "JOIN {$wpdb->posts} ON ( {$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID ) $join ";
2828-
$cwhere = "WHERE comment_approved = '1' $where";
2828+
$cwhere = "WHERE comment_approved = '1' AND {$wpdb->comments}.comment_type != 'note' $where";
28292829
$cgroupby = "{$wpdb->comments}.comment_id";
28302830
} else { // Other non-singular, e.g. front.
28312831
$cjoin = "JOIN {$wpdb->posts} ON ( {$wpdb->comments}.comment_post_ID = {$wpdb->posts}.ID )";
2832-
$cwhere = "WHERE ( post_status = 'publish' OR ( post_status = 'inherit' AND post_type = 'attachment' ) ) AND comment_approved = '1'";
2832+
$cwhere = "WHERE ( post_status = 'publish' OR ( post_status = 'inherit' AND post_type = 'attachment' ) ) AND comment_approved = '1' AND {$wpdb->comments}.comment_type != 'note'";
28332833
$cgroupby = '';
28342834
}
28352835

@@ -3487,7 +3487,7 @@ public function get_posts() {
34873487
$cjoin = apply_filters_ref_array( 'comment_feed_join', array( '', &$this ) );
34883488

34893489
/** This filter is documented in wp-includes/class-wp-query.php */
3490-
$cwhere = apply_filters_ref_array( 'comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1'", &$this ) );
3490+
$cwhere = apply_filters_ref_array( 'comment_feed_where', array( "WHERE comment_post_ID = '{$this->posts[0]->ID}' AND comment_approved = '1' AND {$wpdb->comments}.comment_type != 'note'", &$this ) );
34913491

34923492
/** This filter is documented in wp-includes/class-wp-query.php */
34933493
$cgroupby = apply_filters_ref_array( 'comment_feed_groupby', array( '', &$this ) );

tests/phpunit/tests/query/commentFeed.php

Lines changed: 96 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,12 @@
66
* @group feeds
77
*/
88
class Tests_Query_CommentFeed extends WP_UnitTestCase {
9-
public static $post_type = 'post';
10-
protected static $post_ids = array();
9+
public static string $post_type = 'post';
10+
11+
/**
12+
* @var int[]
13+
*/
14+
protected static array $post_ids = array();
1115

1216
public static function wpSetUpBeforeClass( WP_UnitTest_Factory $factory ) {
1317
self::$post_ids = $factory->post->create_many(
@@ -82,6 +86,96 @@ public function test_archive_comment_feed_invalid_cache() {
8286
$this->assertSame( 20, $comment_count );
8387
}
8488

89+
/**
90+
* @ticket 65613
91+
*/
92+
public function test_main_comment_feed_should_exclude_notes(): void {
93+
$note_id = self::factory()->comment->create(
94+
array(
95+
'comment_post_ID' => self::$post_ids[0],
96+
'comment_type' => 'note',
97+
'comment_approved' => '1',
98+
)
99+
);
100+
101+
$q = new WP_Query();
102+
$q->query(
103+
array(
104+
'withcomments' => 1,
105+
'feed' => 'comments-rss',
106+
)
107+
);
108+
109+
$this->assertTrue( $q->is_comment_feed() );
110+
$this->assertFalse( $q->is_singular() );
111+
112+
$comment_ids = array_map( 'intval', wp_list_pluck( $q->comments, 'comment_ID' ) );
113+
$this->assertNotContains( $note_id, $comment_ids, 'Comments feed should not include notes.' );
114+
$this->assertSame( 15, $q->comment_count, 'Comments feed should include all regular comments.' );
115+
}
116+
117+
/**
118+
* @ticket 65613
119+
*/
120+
public function test_archive_comment_feed_should_exclude_notes(): void {
121+
$note_id = self::factory()->comment->create(
122+
array(
123+
'comment_post_ID' => self::$post_ids[0],
124+
'comment_type' => 'note',
125+
'comment_approved' => '1',
126+
)
127+
);
128+
129+
$q = new WP_Query();
130+
$q->query(
131+
array(
132+
'withcomments' => 1,
133+
'feed' => 'comments-rss',
134+
'year' => (int) get_the_date( 'Y', self::$post_ids[0] ),
135+
)
136+
);
137+
138+
$this->assertTrue( $q->is_comment_feed() );
139+
$this->assertTrue( $q->is_archive() );
140+
141+
$comment_ids = array_map( 'intval', wp_list_pluck( $q->comments, 'comment_ID' ) );
142+
$this->assertNotContains( $note_id, $comment_ids, 'Archive comments feed should not include notes.' );
143+
$this->assertSame( 15, $q->comment_count, 'Archive comments feed should include all regular comments.' );
144+
}
145+
146+
/**
147+
* @ticket 65613
148+
*/
149+
public function test_single_comment_feed_should_exclude_notes(): void {
150+
$post = get_post( self::$post_ids[0] );
151+
$this->assertInstanceOf( WP_Post::class, $post );
152+
153+
$note_id = self::factory()->comment->create(
154+
array(
155+
'comment_post_ID' => $post->ID,
156+
'comment_type' => 'note',
157+
'comment_approved' => '1',
158+
)
159+
);
160+
161+
$q = new WP_Query();
162+
$q->query(
163+
array(
164+
'withcomments' => 1,
165+
'feed' => 'comments-rss',
166+
'post_type' => $post->post_type,
167+
'name' => $post->post_name,
168+
)
169+
);
170+
171+
$this->assertTrue( $q->is_comment_feed() );
172+
$this->assertTrue( $q->is_singular() );
173+
174+
$comment_ids = array_map( 'intval', wp_list_pluck( $q->comments, 'comment_ID' ) );
175+
$this->assertNotContains( $note_id, $comment_ids, 'Singular comments feed should not include notes.' );
176+
$this->assertSame( 5, $q->comment_count, 'Singular comments feed should include all regular comments.' );
177+
}
178+
85179
/**
86180
* @ticket 36904
87181
*/

0 commit comments

Comments
 (0)