Skip to content

Commit bfc5210

Browse files
authored
Add hide_for method to Comment class (#2662)
1 parent 007f23c commit bfc5210

File tree

3 files changed

+226
-18
lines changed

3 files changed

+226
-18
lines changed
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
Significance: minor
2+
Type: added
3+
4+
Hide comments from specific post types in the WordPress admin comments list.

includes/class-comment.php

Lines changed: 24 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -704,9 +704,7 @@ public static function comment_query( $query ) {
704704

705705
// Do only exclude interactions of `ap_post` post type.
706706
if ( \is_admin() ) {
707-
if ( \get_option( 'activitypub_create_posts', false ) ) {
708-
$query->query_vars['post_type'] = array_diff( \get_post_types_by_support( 'comments' ), array( Posts::POST_TYPE ) );
709-
}
707+
$query->query_vars['post_type'] = array_diff( \get_post_types_by_support( 'comments' ), self::hide_for() );
710708
return;
711709
}
712710

@@ -767,14 +765,11 @@ public static function pre_comment_approved( $approved, $comment_data ) {
767765
return 1;
768766
}
769767

770-
// Auto approve reactions to an `ap_post`.
771-
if ( \get_option( 'activitypub_create_posts', false ) ) {
772-
$post_id = $comment_data['comment_post_ID'];
773-
$post = \get_post( $post_id );
768+
$post_id = $comment_data['comment_post_ID'];
769+
$post = \get_post( $post_id );
774770

775-
if ( $post && Posts::POST_TYPE === $post->post_type ) {
776-
return 1;
777-
}
771+
if ( $post && in_array( $post->post_type, self::hide_for(), true ) ) {
772+
return 1;
778773
}
779774

780775
return $approved;
@@ -827,4 +822,23 @@ public static function pre_wp_update_comment_count_now( $new_count, $old_count,
827822
public static function is_comment_type_enabled( $comment_type ) {
828823
return '1' === get_option( "activitypub_allow_{$comment_type}s", '1' );
829824
}
825+
826+
/**
827+
* Get post types to hide comments for in admin.
828+
*
829+
* These are non-public post types whose comments should not appear
830+
* in the main comments list in the WordPress admin.
831+
*
832+
* @return string[] Array of post type names to hide comments for.
833+
*/
834+
public static function hide_for() {
835+
$post_types = array( Posts::POST_TYPE );
836+
837+
/**
838+
* Filters the list of post types to hide comments for.
839+
*
840+
* @param string[] $post_types Array of post type names to hide comments for.
841+
*/
842+
return \apply_filters( 'activitypub_hide_comments_for', $post_types );
843+
}
830844
}

tests/phpunit/tests/includes/class-test-comment.php

Lines changed: 198 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77

88
namespace Activitypub\Tests;
99

10+
use Activitypub\Collection\Posts;
1011
use Activitypub\Comment;
1112

1213
/**
@@ -772,11 +773,11 @@ public function test_ap_post_comments_shown_on_frontend() {
772773
}
773774

774775
/**
775-
* Test that ap_post comments are shown when querying for specific post.
776+
* Test that ap_post comments are hidden even when querying for specific post.
776777
*
777778
* @covers ::comment_query
778779
*/
779-
public function test_ap_post_comments_shown_when_querying_specific_post() {
780+
public function test_ap_post_comments_hidden_when_querying_specific_post() {
780781
// Create an ap_post.
781782
$ap_post_id = wp_insert_post(
782783
array(
@@ -799,7 +800,7 @@ public function test_ap_post_comments_shown_when_querying_specific_post() {
799800
// Simulate admin context.
800801
\set_current_screen( 'edit-comments' );
801802

802-
// Query comments for specific post - should include ap_post comments.
803+
// Query comments for specific post - should NOT include ap_post comments.
803804
$query = new \WP_Comment_Query();
804805
$comments = $query->query(
805806
array(
@@ -808,7 +809,7 @@ public function test_ap_post_comments_shown_when_querying_specific_post() {
808809
);
809810

810811
$comment_ids = wp_list_pluck( $comments, 'comment_ID' );
811-
$this->assertContains( (string) $ap_comment_id, $comment_ids, 'AP post comment should be shown when querying specific post' );
812+
$this->assertNotContains( (string) $ap_comment_id, $comment_ids, 'AP post comment should be hidden even when querying specific post' );
812813

813814
// Clean up.
814815
\set_current_screen( 'front' );
@@ -859,11 +860,11 @@ public function test_auto_approve_comments_on_ap_post_when_enabled() {
859860
}
860861

861862
/**
862-
* Test not auto-approving comments on ap_post when option is disabled.
863+
* Test auto-approving comments on ap_post regardless of option.
863864
*
864865
* @covers ::pre_comment_approved
865866
*/
866-
public function test_no_auto_approve_comments_on_ap_post_when_disabled() {
867+
public function test_auto_approve_comments_on_ap_post_always() {
867868
// Disable flood control.
868869
\remove_action( 'check_comment_flood', 'check_comment_flood_db', 10 );
869870

@@ -893,9 +894,9 @@ public function test_no_auto_approve_comments_on_ap_post_when_disabled() {
893894
)
894895
);
895896

896-
// The comment should NOT be auto-approved.
897+
// The comment should be auto-approved on ap_post regardless of option.
897898
$comment = \get_comment( $comment_id );
898-
$this->assertEquals( '0', $comment->comment_approved, 'Comment on ap_post should not be auto-approved when option is disabled' );
899+
$this->assertEquals( '1', $comment->comment_approved, 'Comment on ap_post should be auto-approved regardless of option' );
899900

900901
// Clean up.
901902
\add_action( 'check_comment_flood', 'check_comment_flood_db', 10, 4 );
@@ -992,6 +993,195 @@ public function test_auto_approve_different_comment_types_on_ap_post() {
992993
\add_action( 'check_comment_flood', 'check_comment_flood_db', 10, 4 );
993994
}
994995

996+
/**
997+
* Test hide_for returns ap_post by default.
998+
*
999+
* @covers ::hide_for
1000+
*/
1001+
public function test_hide_for() {
1002+
$post_types = Comment::hide_for();
1003+
1004+
$this->assertIsArray( $post_types );
1005+
$this->assertContains( Posts::POST_TYPE, $post_types, 'ap_post should be in the list of post types to hide comments for' );
1006+
$this->assertCount( 1, $post_types, 'Only ap_post should be in the default list' );
1007+
}
1008+
1009+
/**
1010+
* Test hide_for filter can add post types.
1011+
*
1012+
* @covers ::hide_for
1013+
*/
1014+
public function test_hide_for_filter_can_add_post_types() {
1015+
$filter = function ( $post_types ) {
1016+
$post_types[] = 'custom_post_type';
1017+
return $post_types;
1018+
};
1019+
1020+
\add_filter( 'activitypub_hide_comments_for', $filter );
1021+
1022+
$post_types = Comment::hide_for();
1023+
1024+
$this->assertContains( 'custom_post_type', $post_types, 'Filter should be able to add custom post types' );
1025+
$this->assertContains( Posts::POST_TYPE, $post_types, 'ap_post should still be in the list' );
1026+
1027+
\remove_filter( 'activitypub_hide_comments_for', $filter );
1028+
}
1029+
1030+
/**
1031+
* Test hide_for filter can remove post types.
1032+
*
1033+
* @covers ::hide_for
1034+
*/
1035+
public function test_hide_for_filter_can_remove_post_types() {
1036+
$filter = function ( $post_types ) {
1037+
return array_diff( $post_types, array( Posts::POST_TYPE ) );
1038+
};
1039+
1040+
\add_filter( 'activitypub_hide_comments_for', $filter );
1041+
1042+
$post_types = Comment::hide_for();
1043+
1044+
$this->assertNotContains( Posts::POST_TYPE, $post_types, 'Filter should be able to remove ap_post from the list' );
1045+
1046+
\remove_filter( 'activitypub_hide_comments_for', $filter );
1047+
}
1048+
1049+
/**
1050+
* Test hide_for filter affects comment_query behavior.
1051+
*
1052+
* @covers ::hide_for
1053+
* @covers ::comment_query
1054+
*/
1055+
public function test_hide_for_filter_affects_comment_query() {
1056+
// Register a custom post type for testing.
1057+
\register_post_type(
1058+
'custom_hidden',
1059+
array(
1060+
'public' => true,
1061+
'supports' => array( 'comments' ),
1062+
)
1063+
);
1064+
1065+
// Create a custom post.
1066+
$custom_post_id = wp_insert_post(
1067+
array(
1068+
'post_type' => 'custom_hidden',
1069+
'post_title' => 'Custom Post',
1070+
'post_status' => 'publish',
1071+
)
1072+
);
1073+
1074+
// Create a comment on the custom post.
1075+
$custom_comment_id = wp_insert_comment(
1076+
array(
1077+
'comment_post_ID' => $custom_post_id,
1078+
'comment_content' => 'Comment on custom post',
1079+
'comment_author' => 'Test User',
1080+
)
1081+
);
1082+
1083+
// Simulate admin context.
1084+
\set_current_screen( 'edit-comments' );
1085+
1086+
// Without filter, comment should be visible.
1087+
$query = new \WP_Comment_Query();
1088+
$comments = $query->query( array() );
1089+
$comment_ids = wp_list_pluck( $comments, 'comment_ID' );
1090+
$this->assertContains( (string) $custom_comment_id, $comment_ids, 'Custom post comment should be visible without filter' );
1091+
1092+
// Add filter to hide custom_hidden post type.
1093+
$filter = function ( $post_types ) {
1094+
$post_types[] = 'custom_hidden';
1095+
return $post_types;
1096+
};
1097+
\add_filter( 'activitypub_hide_comments_for', $filter );
1098+
1099+
// With filter, comment should be hidden.
1100+
$query = new \WP_Comment_Query();
1101+
$comments = $query->query( array() );
1102+
$comment_ids = wp_list_pluck( $comments, 'comment_ID' );
1103+
$this->assertNotContains( (string) $custom_comment_id, $comment_ids, 'Custom post comment should be hidden with filter' );
1104+
1105+
// Clean up.
1106+
\remove_filter( 'activitypub_hide_comments_for', $filter );
1107+
\set_current_screen( 'front' );
1108+
\unregister_post_type( 'custom_hidden' );
1109+
}
1110+
1111+
/**
1112+
* Test hide_for filter affects pre_comment_approved behavior.
1113+
*
1114+
* @covers ::hide_for
1115+
* @covers ::pre_comment_approved
1116+
*/
1117+
public function test_hide_for_filter_affects_auto_approval() {
1118+
// Disable flood control.
1119+
\remove_action( 'check_comment_flood', 'check_comment_flood_db', 10 );
1120+
1121+
// Register a custom post type for testing.
1122+
\register_post_type(
1123+
'custom_hidden',
1124+
array(
1125+
'public' => true,
1126+
'supports' => array( 'comments' ),
1127+
)
1128+
);
1129+
1130+
// Create a custom post.
1131+
$custom_post_id = self::factory()->post->create(
1132+
array(
1133+
'post_type' => 'custom_hidden',
1134+
'post_status' => 'publish',
1135+
)
1136+
);
1137+
1138+
// Without filter, comment should NOT be auto-approved.
1139+
$comment_id = \wp_new_comment(
1140+
array(
1141+
'comment_type' => 'comment',
1142+
'comment_content' => 'Comment without filter.',
1143+
'comment_author' => 'Test User',
1144+
'comment_author_url' => 'https://example.com/@testuser',
1145+
'comment_post_ID' => $custom_post_id,
1146+
'comment_author_email' => '',
1147+
'comment_meta' => array(
1148+
'protocol' => 'activitypub',
1149+
),
1150+
)
1151+
);
1152+
$comment = \get_comment( $comment_id );
1153+
$this->assertEquals( '0', $comment->comment_approved, 'Comment should not be auto-approved without filter' );
1154+
1155+
// Add filter to include custom_hidden in hide_for list.
1156+
$filter = function ( $post_types ) {
1157+
$post_types[] = 'custom_hidden';
1158+
return $post_types;
1159+
};
1160+
\add_filter( 'activitypub_hide_comments_for', $filter );
1161+
1162+
// With filter, comment should be auto-approved.
1163+
$comment_id_2 = \wp_new_comment(
1164+
array(
1165+
'comment_type' => 'comment',
1166+
'comment_content' => 'Comment with filter.',
1167+
'comment_author' => 'Test User 2',
1168+
'comment_author_url' => 'https://example.com/@testuser2',
1169+
'comment_post_ID' => $custom_post_id,
1170+
'comment_author_email' => '',
1171+
'comment_meta' => array(
1172+
'protocol' => 'activitypub',
1173+
),
1174+
)
1175+
);
1176+
$comment_2 = \get_comment( $comment_id_2 );
1177+
$this->assertEquals( '1', $comment_2->comment_approved, 'Comment should be auto-approved with filter' );
1178+
1179+
// Clean up.
1180+
\remove_filter( 'activitypub_hide_comments_for', $filter );
1181+
\unregister_post_type( 'custom_hidden' );
1182+
\add_action( 'check_comment_flood', 'check_comment_flood_db', 10, 4 );
1183+
}
1184+
9951185
/**
9961186
* Test that multiple ap_post comments are excluded while regular comments remain.
9971187
*

0 commit comments

Comments
 (0)