Skip to content

Commit ea19200

Browse files
Notes: Centralize internal comment types via gutenberg_get_internal_comment_types().
Mirrors the change being proposed for core in WordPress/wordpress-develop#10930. Introduces a single helper that returns the list of internal comment types ('note', 'reaction'), filterable so future additions only need to update one place. Apply it across all the existing 'note'/'reaction' guards in the 7.1 compat layer: * gutenberg_update_get_avatar_comment_type_7_1() — avatar-eligible types. * gutenberg_exclude_block_comments_from_admin_7_1() — admin query exclude. * gutenberg_filter_comment_count_query_exclude_block_comments_7_1() — pending-count SQL guard. * gutenberg_hide_note_from_comment_list_table_7_1() — list table args. * gutenberg_exclude_notes_from_comment_count_7_1() — approved-count SQL. * Gutenberg_REST_Comment_Controller_7_1::is_note_or_reaction() — REST controller's per-request type check (used by permissions, prepare, validation, and links). Props westonruter for the suggestion to centralize this list.
1 parent a99bc46 commit ea19200

2 files changed

Lines changed: 53 additions & 13 deletions

File tree

lib/compat/wordpress-7.1/block-comments.php

Lines changed: 48 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,34 @@
2424
*/
2525

2626
/**
27-
* Updates the comment type for avatars to include reactions.
27+
* Returns the list of internal comment types used by core features.
28+
*
29+
* Internal comment types (currently 'note' and 'reaction') back editor
30+
* functionality such as block notes and emoji reactions, and should be
31+
* excluded from front-end comment listings, counts, and similar contexts
32+
* that target user discussion. Centralizing the list keeps every guard
33+
* in sync when new internal types are added.
34+
*
35+
* Mirrors the planned `wp_get_internal_comment_types()` core helper
36+
* (see https://github.com/WordPress/wordpress-develop/pull/10930).
37+
*
38+
* @since 7.1.0
39+
*
40+
* @return string[] List of internal comment type slugs.
41+
*/
42+
function gutenberg_get_internal_comment_types() {
43+
/**
44+
* Filters the list of internal comment types.
45+
*
46+
* @since 7.1.0
47+
*
48+
* @param string[] $types List of internal comment type slugs.
49+
*/
50+
return apply_filters( 'gutenberg_internal_comment_types', array( 'note', 'reaction' ) );
51+
}
52+
53+
/**
54+
* Updates the comment type for avatars to include internal comment types.
2855
*
2956
* Replaces the 6.9 implementation to also add the 'reaction' type
3057
* to the list of comment types for which avatars should be retrieved.
@@ -33,9 +60,7 @@
3360
* @return array The updated array of comment types.
3461
*/
3562
function gutenberg_update_get_avatar_comment_type_7_1( $comment_type ) {
36-
$comment_type[] = 'note';
37-
$comment_type[] = 'reaction';
38-
return $comment_type;
63+
return array_values( array_unique( array_merge( $comment_type, gutenberg_get_internal_comment_types() ) ) );
3964
}
4065
remove_filter( 'get_avatar_comment_types', 'update_get_avatar_comment_type' );
4166
add_filter( 'get_avatar_comment_types', 'gutenberg_update_get_avatar_comment_type_7_1' );
@@ -57,7 +82,10 @@ function gutenberg_exclude_block_comments_from_admin_7_1( $clauses, $query ) {
5782
$query->set( 'type', '' );
5883

5984
global $wpdb;
60-
$clauses['where'] .= " AND {$wpdb->comments}.comment_type != 'note' AND {$wpdb->comments}.comment_type != 'reaction'";
85+
$internal_types = gutenberg_get_internal_comment_types();
86+
$type_placeholders = implode( ', ', array_fill( 0, count( $internal_types ), '%s' ) );
87+
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared, WordPress.DB.PreparedSQLPlaceholders.UnfinishedPrepare
88+
$clauses['where'] .= ' AND ' . $wpdb->prepare( "{$wpdb->comments}.comment_type NOT IN ( $type_placeholders )", $internal_types );
6189
}
6290

6391
return $clauses;
@@ -76,7 +104,11 @@ function gutenberg_exclude_block_comments_from_admin_7_1( $clauses, $query ) {
76104
function gutenberg_filter_comment_count_query_exclude_block_comments_7_1( $query ) {
77105
if ( str_starts_with( $query, 'SELECT comment_post_ID, COUNT(comment_ID) as num_comments FROM' ) && str_contains( $query, 'comment_approved' ) ) {
78106
if ( ! str_contains( $query, "comment_type != 'note'" ) ) {
79-
$query = str_replace( 'comment_approved', "comment_type != 'note' AND comment_type != 'reaction' AND comment_approved", $query );
107+
$type_clauses = array();
108+
foreach ( gutenberg_get_internal_comment_types() as $internal_type ) {
109+
$type_clauses[] = "comment_type != '" . esc_sql( $internal_type ) . "'";
110+
}
111+
$query = str_replace( 'comment_approved', implode( ' AND ', $type_clauses ) . ' AND comment_approved', $query );
80112
}
81113
}
82114
return $query;
@@ -93,7 +125,7 @@ function gutenberg_filter_comment_count_query_exclude_block_comments_7_1( $query
93125
* @return array Possibly modified arguments for get_comments().
94126
*/
95127
function gutenberg_hide_note_from_comment_list_table_7_1( $args ) {
96-
if ( ! empty( $_REQUEST['comment_type'] ) && in_array( $_REQUEST['comment_type'], array( 'note', 'reaction' ), true ) ) {
128+
if ( ! empty( $_REQUEST['comment_type'] ) && in_array( $_REQUEST['comment_type'], gutenberg_get_internal_comment_types(), true ) ) {
97129
unset( $args['type'] );
98130
}
99131
return $args;
@@ -116,7 +148,15 @@ function gutenberg_exclude_notes_from_comment_count_7_1( $new_count, $old_count,
116148
if ( null !== $new_count ) {
117149
return $new_count;
118150
}
119-
$new_count = (int) $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' AND comment_type != 'note' AND comment_type != 'reaction'", $post_id ) );
151+
$internal_types = gutenberg_get_internal_comment_types();
152+
$type_placeholders = implode( ', ', array_fill( 0, count( $internal_types ), '%s' ) );
153+
$new_count = (int) $wpdb->get_var(
154+
$wpdb->prepare(
155+
// phpcs:ignore WordPress.DB.PreparedSQL.InterpolatedNotPrepared
156+
"SELECT COUNT(*) FROM $wpdb->comments WHERE comment_post_ID = %d AND comment_approved = '1' AND comment_type NOT IN ( $type_placeholders )",
157+
array_merge( array( $post_id ), $internal_types )
158+
)
159+
);
120160
return $new_count;
121161
}
122162
remove_filter( 'pre_wp_update_comment_count_now', 'gutenberg_exclude_notes_from_comment_count', 10 );

lib/compat/wordpress-7.1/class-gutenberg-rest-comment-controller-7-1.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -79,13 +79,13 @@ public function get_item_schema() {
7979
}
8080

8181
/**
82-
* Checks whether the request type is a note or reaction.
82+
* Checks whether the request type is an internal comment type (note or reaction).
8383
*
8484
* @param string $type The comment type from the request.
85-
* @return bool True if the type is 'note' or 'reaction'.
85+
* @return bool True if the type is one of the internal comment types.
8686
*/
8787
protected function is_note_or_reaction( $type ) {
88-
return in_array( $type, array( 'note', 'reaction' ), true );
88+
return in_array( $type, gutenberg_get_internal_comment_types(), true );
8989
}
9090

9191
public function get_items_permissions_check( $request ) {
@@ -349,8 +349,8 @@ public function create_item( $request ) {
349349
);
350350
}
351351

352-
// Allow 'comment', 'note', and 'reaction' types.
353-
if ( ! empty( $request['type'] ) && ! in_array( $request['type'], array( 'comment', 'note', 'reaction' ), true ) ) {
352+
// Allow 'comment' plus internal comment types (note, reaction).
353+
if ( ! empty( $request['type'] ) && ! in_array( $request['type'], array_merge( array( 'comment' ), gutenberg_get_internal_comment_types() ), true ) ) {
354354
return new WP_Error(
355355
'rest_invalid_comment_type',
356356
__( 'Cannot create a comment with that type.', 'gutenberg' ),

0 commit comments

Comments
 (0)