Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions .github/changelog/add-get-non-public-post-types
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Significance: minor
Type: added

Hide comments from non-public post types in the WordPress admin comments list.
35 changes: 24 additions & 11 deletions includes/class-comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
namespace Activitypub;

use Activitypub\Collection\Actors;
use Activitypub\Collection\Posts;

/**
* ActivityPub Comment Class.
Expand Down Expand Up @@ -704,9 +703,7 @@ public static function comment_query( $query ) {

// Do only exclude interactions of `ap_post` post type.
if ( \is_admin() ) {
if ( \get_option( 'activitypub_create_posts', false ) ) {
$query->query_vars['post_type'] = array_diff( \get_post_types_by_support( 'comments' ), array( Posts::POST_TYPE ) );
}
$query->query_vars['post_type'] = array_diff( \get_post_types_by_support( 'comments' ), self::get_post_types_to_hide_comments_for() );
return;
}

Expand Down Expand Up @@ -767,14 +764,11 @@ public static function pre_comment_approved( $approved, $comment_data ) {
return 1;
}

// Auto approve reactions to an `ap_post`.
if ( \get_option( 'activitypub_create_posts', false ) ) {
$post_id = $comment_data['comment_post_ID'];
$post = \get_post( $post_id );
$post_id = $comment_data['comment_post_ID'];
$post = \get_post( $post_id );

if ( $post && Posts::POST_TYPE === $post->post_type ) {
return 1;
}
if ( $post && in_array( $post->post_type, self::get_post_types_to_hide_comments_for(), true ) ) {
return 1;
}

return $approved;
Expand Down Expand Up @@ -827,4 +821,23 @@ public static function pre_wp_update_comment_count_now( $new_count, $old_count,
public static function is_comment_type_enabled( $comment_type ) {
return '1' === get_option( "activitypub_allow_{$comment_type}s", '1' );
}

/**
* Get post types to hide comments for in admin.
*
* These are non-public post types whose comments should not appear
* in the main comments list in the WordPress admin.
*
* @return string[] Array of post type names to hide comments for.
*/
public static function get_post_types_to_hide_comments_for() {
$post_types = \get_post_types( array( 'public' => false ), 'names' );

/**
* Filters the list of post types to hide comments for.
*
* @param string[] $post_types Array of post type names to hide comments for.
*/
return \apply_filters( 'activitypub_post_types_to_hide_comments_for', $post_types );
}
}
15 changes: 15 additions & 0 deletions tests/phpunit/tests/includes/class-test-comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@

namespace Activitypub\Tests;

use Activitypub\Collection\Posts;
use Activitypub\Comment;

/**
Expand Down Expand Up @@ -992,6 +993,20 @@ public function test_auto_approve_different_comment_types_on_ap_post() {
\add_action( 'check_comment_flood', 'check_comment_flood_db', 10, 4 );
}

/**
* Test get_post_types_to_hide_comments_for.
*
* @covers ::get_post_types_to_hide_comments_for
*/
public function test_get_post_types_to_hide_comments_for() {
$post_types = Comment::get_post_types_to_hide_comments_for();

$this->assertIsArray( $post_types );
$this->assertContains( Posts::POST_TYPE, $post_types, 'ap_post should be in the list of post types to hide comments for' );
$this->assertNotContains( 'post', $post_types, 'post should not be in the list of post types to hide comments for' );
$this->assertNotContains( 'page', $post_types, 'page should not be in the list of post types to hide comments for' );
}

/**
* Test that multiple ap_post comments are excluded while regular comments remain.
*
Expand Down
Loading