Skip to content

Commit 37a94b4

Browse files
committed
Add get_non_public_post_types method to Comment class.
This method returns all post types that are not public, which is useful for filtering comment queries and auto-approving comments on non-public post types like ap_post.
1 parent 7c51303 commit 37a94b4

File tree

3 files changed

+38
-11
lines changed

3 files changed

+38
-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+
Add `get_non_public_post_types` method to Comment class.

includes/class-comment.php

Lines changed: 21 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_non_public_post_types() );
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_non_public_post_types(), true ) ) {
771+
return 1;
778772
}
779773

780774
return $approved;
@@ -827,4 +821,20 @@ 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 all post types that are not public.
827+
*
828+
* @return string[] Array of non-public post type names.
829+
*/
830+
public static function get_non_public_post_types() {
831+
$post_types = \get_post_types( array( 'public' => false ), 'names' );
832+
833+
/**
834+
* Filters the list of non-public post types.
835+
*
836+
* @param string[] $post_types Array of non-public post type names.
837+
*/
838+
return \apply_filters( 'activitypub_non_public_post_types', $post_types );
839+
}
830840
}

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

Lines changed: 13 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,18 @@ 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_non_public_post_types.
998+
*
999+
* @covers ::get_non_public_post_types
1000+
*/
1001+
public function test_get_non_public_post_types() {
1002+
$post_types = Comment::get_non_public_post_types();
1003+
1004+
$this->assertIsArray( $post_types );
1005+
$this->assertContains( Posts::POST_TYPE, $post_types, 'ap_post should be a non-public post type' );
1006+
}
1007+
9951008
/**
9961009
* Test that multiple ap_post comments are excluded while regular comments remain.
9971010
*

0 commit comments

Comments
 (0)