Skip to content

Commit dac5fa8

Browse files
Editor: Notes should not appear in the context of comments.
Prevent notes from inadvertently showing up in the context of comments - including on the Dashboard recent comments widget and the “Mine” count on the Comments page. Notes are stored as a custom ‘note’ comment type and this change ensures the note type is only returned when explicitly requested, or when ‘all’ types are requested. The query for note children is modified to return all child notes. This fixes an issue where children were no longer being returned for the ‘note’ type. Also fixes WordPress/gutenberg#72548. Props adamsilverstein, timothyblynjacobs, shailu25, peterwilsoncc, westonruter, mamaduka, kadamwhite. Fixes #64145. Fixes #64152. git-svn-id: https://develop.svn.wordpress.org/trunk@61105 602fd350-edb4-49c9-b593-d223f7449a82
1 parent c53d6ba commit dac5fa8

File tree

7 files changed

+247
-2
lines changed

7 files changed

+247
-2
lines changed

src/wp-admin/includes/class-wp-comments-list-table.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -151,7 +151,6 @@ public function prepare_items() {
151151
'order' => $order,
152152
'post_type' => $post_type,
153153
'update_comment_post_cache' => true,
154-
'type__not_in' => array( 'note' ),
155154
);
156155

157156
/**

src/wp-admin/includes/comment.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -138,6 +138,7 @@ function get_comment_to_edit( $id ) {
138138
* Gets the number of pending comments on a post or posts.
139139
*
140140
* @since 2.3.0
141+
* @since 6.9.0 Exclude the 'note' comment type from the count.
141142
*
142143
* @global wpdb $wpdb WordPress database abstraction object.
143144
*

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

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -536,6 +536,7 @@ public function get_comments() {
536536
* Used internally to get a list of comment IDs matching the query vars.
537537
*
538538
* @since 4.4.0
539+
* @since 6.9.0 Excludes the 'note' comment type, unless 'all' or the 'note' types are requested.
539540
*
540541
* @global wpdb $wpdb WordPress database abstraction object.
541542
*
@@ -770,6 +771,15 @@ protected function get_comment_ids() {
770771
'NOT IN' => (array) $this->query_vars['type__not_in'],
771772
);
772773

774+
// Exclude the 'note' comment type, unless 'all' types or the 'note' type explicitly are requested.
775+
if (
776+
! in_array( 'all', $raw_types['IN'], true ) &&
777+
! in_array( 'note', $raw_types['IN'], true ) &&
778+
! in_array( 'note', $raw_types['NOT IN'], true )
779+
) {
780+
$raw_types['NOT IN'][] = 'note';
781+
}
782+
773783
$comment_types = array();
774784
foreach ( $raw_types as $operator => $_raw_types ) {
775785
$_raw_types = array_unique( $_raw_types );

src/wp-includes/comment.php

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -417,7 +417,6 @@ function get_comment_count( $post_id = 0 ) {
417417
'count' => true,
418418
'update_comment_meta_cache' => false,
419419
'orderby' => 'none',
420-
'type__not_in' => array( 'note' ),
421420
);
422421
if ( $post_id > 0 ) {
423422
$args['post_id'] = $post_id;

src/wp-includes/rest-api/endpoints/class-wp-rest-comments-controller.php

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1254,6 +1254,7 @@ protected function prepare_links( $comment ) {
12541254
array(
12551255
'count' => true,
12561256
'orderby' => 'none',
1257+
'type' => 'all',
12571258
)
12581259
);
12591260

@@ -1270,6 +1271,22 @@ protected function prepare_links( $comment ) {
12701271
);
12711272
}
12721273

1274+
// Embedding children for notes requires `type` and `status` inheritance.
1275+
if ( isset( $links['children'] ) && 'note' === $comment->comment_type ) {
1276+
$args = array(
1277+
'parent' => $comment->comment_ID,
1278+
'type' => $comment->comment_type,
1279+
'status' => 'all',
1280+
);
1281+
1282+
$rest_url = add_query_arg( $args, rest_url( $this->namespace . '/' . $this->rest_base ) );
1283+
1284+
$links['children'] = array(
1285+
'href' => $rest_url,
1286+
'embeddable' => true,
1287+
);
1288+
}
1289+
12731290
return $links;
12741291
}
12751292

tests/phpunit/tests/comment/query.php

Lines changed: 162 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5372,4 +5372,166 @@ public function test_query_does_not_have_leading_whitespace() {
53725372

53735373
$this->assertSame( ltrim( $q->request ), $q->request, 'The query has leading whitespace' );
53745374
}
5375+
5376+
/**
5377+
* Helper method to create standard test comments for note type exclusion tests.
5378+
*
5379+
* @since 6.9.0
5380+
*
5381+
* @return array<'comment'|'pingback'|'note', int> Array of comments created.
5382+
*/
5383+
protected function create_note_type_test_comments(): array {
5384+
return array(
5385+
'comment' => self::factory()->comment->create(
5386+
array(
5387+
'comment_post_ID' => self::$post_id,
5388+
'comment_approved' => '1',
5389+
)
5390+
),
5391+
'pingback' => self::factory()->comment->create(
5392+
array(
5393+
'comment_post_ID' => self::$post_id,
5394+
'comment_approved' => '1',
5395+
'comment_type' => 'pingback',
5396+
)
5397+
),
5398+
'note' => self::factory()->comment->create(
5399+
array(
5400+
'comment_post_ID' => self::$post_id,
5401+
'comment_approved' => '1',
5402+
'comment_type' => 'note',
5403+
)
5404+
),
5405+
);
5406+
}
5407+
5408+
/**
5409+
* @ticket 64145
5410+
* @covers WP_Comment_Query::get_comment_ids
5411+
* @dataProvider data_note_type_exclusion
5412+
*
5413+
* @param array<string, string|array> $query_args Query arguments for WP_Comment_Query.
5414+
* @param string[] $expected_types Expected comment types.
5415+
*/
5416+
public function test_note_type_exclusion( array $query_args, array $expected_types ) {
5417+
$this->create_note_type_test_comments();
5418+
5419+
$query = new WP_Comment_Query();
5420+
$found = $query->query( array_merge( $query_args, array( 'fields' => 'ids' ) ) );
5421+
5422+
$actual_types = array_map(
5423+
static function ( int $comment_id ): string {
5424+
return get_comment( $comment_id )->comment_type;
5425+
},
5426+
$found
5427+
);
5428+
5429+
$this->assertSameSets( $expected_types, $actual_types, 'Expected comment query to return comments of the these types.' );
5430+
}
5431+
5432+
/**
5433+
* Data provider for note type exclusion tests.
5434+
*
5435+
* @since 6.9.0
5436+
*
5437+
* @return array<string, array{ query_args: array<string, string|array>, expected_types: string[] }>
5438+
*/
5439+
public function data_note_type_exclusion(): array {
5440+
return array(
5441+
'default query excludes note' => array(
5442+
'query_args' => array(),
5443+
'expected_types' => array( 'comment', 'pingback' ),
5444+
),
5445+
'empty type parameter excludes note' => array(
5446+
'query_args' => array( 'type' => '' ),
5447+
'expected_types' => array( 'comment', 'pingback' ),
5448+
),
5449+
'type all includes note' => array(
5450+
'query_args' => array( 'type' => 'all' ),
5451+
'expected_types' => array( 'comment', 'pingback', 'note' ),
5452+
),
5453+
'explicit note type' => array(
5454+
'query_args' => array( 'type' => 'note' ),
5455+
'expected_types' => array( 'note' ),
5456+
),
5457+
'type__in with note' => array(
5458+
'query_args' => array( 'type__in' => array( 'note' ) ),
5459+
'expected_types' => array( 'note' ),
5460+
),
5461+
'type__in with note and pingback' => array(
5462+
'query_args' => array( 'type__in' => array( 'note', 'pingback' ) ),
5463+
'expected_types' => array( 'note', 'pingback' ),
5464+
),
5465+
'type pings excludes note' => array(
5466+
'query_args' => array( 'type' => 'pings' ),
5467+
'expected_types' => array( 'pingback' ),
5468+
),
5469+
'type__not_in with note' => array(
5470+
'query_args' => array( 'type__not_in' => array( 'note' ) ),
5471+
'expected_types' => array( 'comment', 'pingback' ),
5472+
),
5473+
);
5474+
}
5475+
5476+
/**
5477+
* @ticket 64145
5478+
* @covers WP_Comment_Query::get_comment_ids
5479+
*/
5480+
public function test_note_type_not_duplicated_in_type__not_in() {
5481+
global $wpdb;
5482+
5483+
$comments = $this->create_note_type_test_comments();
5484+
5485+
$query = new WP_Comment_Query();
5486+
$found = $query->query(
5487+
array(
5488+
'type__not_in' => array( 'note' ),
5489+
'fields' => 'ids',
5490+
)
5491+
);
5492+
5493+
$this->assertSameSets( array( $comments['comment'], $comments['pingback'] ), $found );
5494+
$this->assertNotContains( $comments['note'], $found );
5495+
$note_count = substr_count( $wpdb->last_query, "'note'" );
5496+
$this->assertSame( 1, $note_count, 'The note type should only appear once in the query' );
5497+
}
5498+
5499+
/**
5500+
* @ticket 64145
5501+
* @covers ::get_comment_count
5502+
*/
5503+
public function test_get_comment_count_excludes_note_type() {
5504+
$post_id = self::factory()->post->create();
5505+
5506+
self::factory()->comment->create(
5507+
array(
5508+
'comment_post_ID' => $post_id,
5509+
'comment_approved' => '1',
5510+
)
5511+
);
5512+
self::factory()->comment->create(
5513+
array(
5514+
'comment_post_ID' => $post_id,
5515+
'comment_approved' => '1',
5516+
'comment_type' => 'note',
5517+
)
5518+
);
5519+
self::factory()->comment->create(
5520+
array(
5521+
'comment_post_ID' => $post_id,
5522+
'comment_approved' => '0',
5523+
'comment_type' => 'note',
5524+
)
5525+
);
5526+
5527+
$counts = get_comment_count( $post_id );
5528+
5529+
$this->assertSame( 1, $counts['approved'] );
5530+
$this->assertSame( 0, $counts['awaiting_moderation'] );
5531+
$this->assertSame( 0, $counts['spam'] );
5532+
$this->assertSame( 0, $counts['trash'] );
5533+
$this->assertSame( 0, $counts['post-trashed'] );
5534+
$this->assertSame( 1, $counts['all'] );
5535+
$this->assertSame( 1, $counts['total_comments'] );
5536+
}
53755537
}

tests/phpunit/tests/rest-api/rest-comments-controller.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4076,4 +4076,61 @@ public function data_note_status_provider() {
40764076
'reopen' => array( 'reopen' ),
40774077
);
40784078
}
4079+
4080+
/**
4081+
* Test children link for note comment type. Based on test_get_comment_with_children_link.
4082+
*
4083+
* @ticket 64152
4084+
*/
4085+
public function test_get_note_with_children_link() {
4086+
$parent_comment_id = self::factory()->comment->create(
4087+
array(
4088+
'comment_approved' => 1,
4089+
'comment_post_ID' => self::$post_id,
4090+
'user_id' => self::$admin_id,
4091+
'comment_type' => 'note',
4092+
'comment_content' => 'Parent note comment',
4093+
)
4094+
);
4095+
4096+
self::factory()->comment->create(
4097+
array(
4098+
'comment_approved' => 1,
4099+
'comment_parent' => $parent_comment_id,
4100+
'comment_post_ID' => self::$post_id,
4101+
'user_id' => self::$admin_id,
4102+
'comment_type' => 'note',
4103+
'comment_content' => 'First child note comment',
4104+
)
4105+
);
4106+
4107+
wp_set_current_user( self::$admin_id );
4108+
$request = new WP_REST_Request( 'GET', sprintf( '/wp/v2/comments/%s', $parent_comment_id ) );
4109+
$request->set_param( 'type', 'note' );
4110+
$request->set_param( 'context', 'edit' );
4111+
$response = rest_get_server()->dispatch( $request );
4112+
$this->assertSame( 200, $response->get_status() );
4113+
4114+
$this->assertArrayHasKey( 'children', $response->get_links() );
4115+
4116+
$request = new WP_REST_Request( 'GET', '/wp/v2/comments' );
4117+
$request->set_param( 'post', self::$post_id );
4118+
$request->set_param( 'type', 'note' );
4119+
$request->set_param( 'context', 'edit' );
4120+
$request->set_param( 'parent', 0 );
4121+
4122+
$response = rest_get_server()->dispatch( $request );
4123+
$this->assertSame( 200, $response->get_status() );
4124+
4125+
$data = $response->get_data();
4126+
4127+
$this->assertArrayHasKey( '_links', $data[0] );
4128+
$this->assertArrayHasKey( 'children', $data[0]['_links'] );
4129+
4130+
$children = $data[0]['_links']['children'];
4131+
4132+
// Verify the href attribute contains the expected status and type parameters.
4133+
$this->assertStringContainsString( 'status=all', $children[0]['href'] );
4134+
$this->assertStringContainsString( 'type=note', $children[0]['href'] );
4135+
}
40794136
}

0 commit comments

Comments
 (0)