Skip to content

Commit 0ccf3fe

Browse files
committed
Add get_post_types_to_hide_comments_for method to Comment class.
This method returns post types whose comments should be hidden in the WordPress admin. Used for filtering comment queries and auto-approving comments on non-public post types like ap_post.
1 parent 7c51303 commit 0ccf3fe

File tree

3 files changed

+43
-11
lines changed

3 files changed

+43
-11
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 non-public post types in the WordPress admin comments list.

includes/class-comment.php

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

1010
use Activitypub\Collection\Actors;
11-
use Activitypub\Collection\Posts;
1211

1312
/**
1413
* ActivityPub Comment Class.
@@ -704,9 +703,7 @@ public static function comment_query( $query ) {
704703

705704
// Do only exclude interactions of `ap_post` post type.
706705
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-
}
706+
$query->query_vars['post_type'] = array_diff( \get_post_types_by_support( 'comments' ), self::get_post_types_to_hide_comments_for() );
710707
return;
711708
}
712709

@@ -767,14 +764,11 @@ public static function pre_comment_approved( $approved, $comment_data ) {
767764
return 1;
768765
}
769766

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 );
767+
$post_id = $comment_data['comment_post_ID'];
768+
$post = \get_post( $post_id );
774769

775-
if ( $post && Posts::POST_TYPE === $post->post_type ) {
776-
return 1;
777-
}
770+
if ( $post && in_array( $post->post_type, self::get_post_types_to_hide_comments_for(), true ) ) {
771+
return 1;
778772
}
779773

780774
return $approved;
@@ -827,4 +821,23 @@ public static function pre_wp_update_comment_count_now( $new_count, $old_count,
827821
public static function is_comment_type_enabled( $comment_type ) {
828822
return '1' === get_option( "activitypub_allow_{$comment_type}s", '1' );
829823
}
824+
825+
/**
826+
* Get post types to hide comments for in admin.
827+
*
828+
* These are non-public post types whose comments should not appear
829+
* in the main comments list in the WordPress admin.
830+
*
831+
* @return string[] Array of post type names to hide comments for.
832+
*/
833+
public static function get_post_types_to_hide_comments_for() {
834+
$post_types = \get_post_types( array( 'public' => false ), 'names' );
835+
836+
/**
837+
* Filters the list of post types to hide comments for.
838+
*
839+
* @param string[] $post_types Array of post type names to hide comments for.
840+
*/
841+
return \apply_filters( 'activitypub_post_types_to_hide_comments_for', $post_types );
842+
}
830843
}

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

Lines changed: 15 additions & 0 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
/**
@@ -992,6 +993,20 @@ 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 get_post_types_to_hide_comments_for.
998+
*
999+
* @covers ::get_post_types_to_hide_comments_for
1000+
*/
1001+
public function test_get_post_types_to_hide_comments_for() {
1002+
$post_types = Comment::get_post_types_to_hide_comments_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->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' );
1008+
}
1009+
9951010
/**
9961011
* Test that multiple ap_post comments are excluded while regular comments remain.
9971012
*

0 commit comments

Comments
 (0)