Skip to content

Commit 19f34f0

Browse files
committed
Add hide_for method to Comment class.
This method returns post types whose comments should be hidden in the WordPress admin comments list.
1 parent 0ccf3fe commit 19f34f0

File tree

3 files changed

+196
-20
lines changed

3 files changed

+196
-20
lines changed
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
11
Significance: minor
22
Type: added
33

4-
Hide comments from non-public post types in the WordPress admin comments list.
4+
Hide comments from specific post types in the WordPress admin comments list.

includes/class-comment.php

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
namespace Activitypub;
99

1010
use Activitypub\Collection\Actors;
11+
use Activitypub\Collection\Posts;
1112

1213
/**
1314
* ActivityPub Comment Class.
@@ -703,7 +704,7 @@ public static function comment_query( $query ) {
703704

704705
// Do only exclude interactions of `ap_post` post type.
705706
if ( \is_admin() ) {
706-
$query->query_vars['post_type'] = array_diff( \get_post_types_by_support( 'comments' ), self::get_post_types_to_hide_comments_for() );
707+
$query->query_vars['post_type'] = array_diff( \get_post_types_by_support( 'comments' ), self::hide_for() );
707708
return;
708709
}
709710

@@ -767,7 +768,7 @@ public static function pre_comment_approved( $approved, $comment_data ) {
767768
$post_id = $comment_data['comment_post_ID'];
768769
$post = \get_post( $post_id );
769770

770-
if ( $post && in_array( $post->post_type, self::get_post_types_to_hide_comments_for(), true ) ) {
771+
if ( $post && in_array( $post->post_type, self::hide_for(), true ) ) {
771772
return 1;
772773
}
773774

@@ -830,14 +831,14 @@ public static function is_comment_type_enabled( $comment_type ) {
830831
*
831832
* @return string[] Array of post type names to hide comments for.
832833
*/
833-
public static function get_post_types_to_hide_comments_for() {
834-
$post_types = \get_post_types( array( 'public' => false ), 'names' );
834+
public static function hide_for() {
835+
$post_types = array( Posts::POST_TYPE );
835836

836837
/**
837838
* Filters the list of post types to hide comments for.
838839
*
839840
* @param string[] $post_types Array of post type names to hide comments for.
840841
*/
841-
return \apply_filters( 'activitypub_post_types_to_hide_comments_for', $post_types );
842+
return \apply_filters( 'activitypub_hide_comments_for', $post_types );
842843
}
843844
}

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

Lines changed: 189 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -773,11 +773,11 @@ public function test_ap_post_comments_shown_on_frontend() {
773773
}
774774

775775
/**
776-
* 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.
777777
*
778778
* @covers ::comment_query
779779
*/
780-
public function test_ap_post_comments_shown_when_querying_specific_post() {
780+
public function test_ap_post_comments_hidden_when_querying_specific_post() {
781781
// Create an ap_post.
782782
$ap_post_id = wp_insert_post(
783783
array(
@@ -800,7 +800,7 @@ public function test_ap_post_comments_shown_when_querying_specific_post() {
800800
// Simulate admin context.
801801
\set_current_screen( 'edit-comments' );
802802

803-
// Query comments for specific post - should include ap_post comments.
803+
// Query comments for specific post - should NOT include ap_post comments.
804804
$query = new \WP_Comment_Query();
805805
$comments = $query->query(
806806
array(
@@ -809,7 +809,7 @@ public function test_ap_post_comments_shown_when_querying_specific_post() {
809809
);
810810

811811
$comment_ids = wp_list_pluck( $comments, 'comment_ID' );
812-
$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' );
813813

814814
// Clean up.
815815
\set_current_screen( 'front' );
@@ -860,11 +860,11 @@ public function test_auto_approve_comments_on_ap_post_when_enabled() {
860860
}
861861

862862
/**
863-
* Test not auto-approving comments on ap_post when option is disabled.
863+
* Test auto-approving comments on ap_post regardless of option.
864864
*
865865
* @covers ::pre_comment_approved
866866
*/
867-
public function test_no_auto_approve_comments_on_ap_post_when_disabled() {
867+
public function test_auto_approve_comments_on_ap_post_always() {
868868
// Disable flood control.
869869
\remove_action( 'check_comment_flood', 'check_comment_flood_db', 10 );
870870

@@ -894,9 +894,9 @@ public function test_no_auto_approve_comments_on_ap_post_when_disabled() {
894894
)
895895
);
896896

897-
// The comment should NOT be auto-approved.
897+
// The comment should be auto-approved on ap_post regardless of option.
898898
$comment = \get_comment( $comment_id );
899-
$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' );
900900

901901
// Clean up.
902902
\add_action( 'check_comment_flood', 'check_comment_flood_db', 10, 4 );
@@ -994,17 +994,192 @@ public function test_auto_approve_different_comment_types_on_ap_post() {
994994
}
995995

996996
/**
997-
* Test get_post_types_to_hide_comments_for.
997+
* Test hide_for returns ap_post by default.
998998
*
999-
* @covers ::get_post_types_to_hide_comments_for
999+
* @covers ::hide_for
10001000
*/
1001-
public function test_get_post_types_to_hide_comments_for() {
1002-
$post_types = Comment::get_post_types_to_hide_comments_for();
1001+
public function test_hide_for() {
1002+
$post_types = Comment::hide_for();
10031003

10041004
$this->assertIsArray( $post_types );
10051005
$this->assertContains( Posts::POST_TYPE, $post_types, 'ap_post should be in the list of post types to hide comments for' );
1006-
$this->assertNotContains( 'post', $post_types, 'post should not be in the list of post types to hide comments for' );
1007-
$this->assertNotContains( 'page', $post_types, 'page should not 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 );
10081183
}
10091184

10101185
/**

0 commit comments

Comments
 (0)