Skip to content

Commit 4483c30

Browse files
authored
Improve Interactions moderation (#1065)
1 parent 163346b commit 4483c30

File tree

6 files changed

+179
-0
lines changed

6 files changed

+179
-0
lines changed

CHANGELOG.md

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,13 @@ All notable changes to this project will be documented in this file.
55
The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
66
and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
77

8+
## [Unreleased]
9+
10+
### Improved
11+
12+
* Improve Interactions moderation
13+
* Compatibility with Akismet
14+
815
## [4.4.0] - 2024-12-09
916

1017
### Added

includes/class-admin.php

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,7 @@ public static function init() {
3636
\add_filter( 'comment_row_actions', array( self::class, 'comment_row_actions' ), 10, 2 );
3737
\add_filter( 'manage_edit-comments_columns', array( static::class, 'manage_comment_columns' ) );
3838
\add_action( 'manage_comments_custom_column', array( static::class, 'manage_comments_custom_column' ), 9, 2 );
39+
\add_action( 'admin_comment_types_dropdown', array( static::class, 'comment_types_dropdown' ) );
3940

4041
\add_filter( 'manage_posts_columns', array( static::class, 'manage_post_columns' ), 10, 2 );
4142
\add_action( 'manage_posts_custom_column', array( self::class, 'manage_posts_custom_column' ), 10, 2 );
@@ -599,6 +600,10 @@ public static function comment_row_actions( $actions, $comment ) {
599600
unset( $actions['quickedit'] );
600601
}
601602

603+
if ( in_array( get_comment_type( $comment ), Comment::get_comment_type_names(), true ) ) {
604+
unset( $actions['reply'] );
605+
}
606+
602607
return $actions;
603608
}
604609

@@ -668,6 +673,21 @@ public static function manage_comments_custom_column( $column, $comment_id ) {
668673
}
669674
}
670675

676+
/**
677+
* Add the new ActivityPub comment types to the comment types dropdown.
678+
*
679+
* @param array $types The existing comment types.
680+
*
681+
* @return array The extended comment types.
682+
*/
683+
public static function comment_types_dropdown( $types ) {
684+
foreach ( Comment::get_comment_types() as $comment_type ) {
685+
$types[ $comment_type['type'] ] = esc_html( $comment_type['label'] );
686+
}
687+
688+
return $types;
689+
}
690+
671691
/**
672692
* Return the results for the activitypub column.
673693
*

integration/class-akismet.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
/**
3+
* Akismet integration.
4+
*
5+
* @package Activitypub
6+
*/
7+
8+
namespace Activitypub\Integration;
9+
10+
use function Activitypub\was_comment_received;
11+
12+
/**
13+
* Compatibility with the Akismet plugin.
14+
*
15+
* @see https://wordpress.org/plugins/akismet/
16+
*/
17+
class Akismet {
18+
/**
19+
* Initialize the class, registering WordPress hooks.
20+
*/
21+
public static function init() {
22+
\add_filter( 'comment_row_actions', array( self::class, 'comment_row_actions' ), 10, 2 );
23+
}
24+
25+
/**
26+
* Remove the "history" action from the comment row actions.
27+
*
28+
* @param array $actions The existing actions.
29+
* @param int|\WP_Comment $comment The comment object or ID.
30+
*
31+
* @return array The modified actions.
32+
*/
33+
public static function comment_row_actions( $actions, $comment ) {
34+
if ( was_comment_received( $comment ) ) {
35+
unset( $actions['history'] );
36+
}
37+
38+
return $actions;
39+
}
40+
}

integration/load.php

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -66,6 +66,17 @@ function plugin_init() {
6666
Jetpack::init();
6767
}
6868

69+
/**
70+
* Adds Akismet support.
71+
*
72+
* This class handles the compatibility with the Akismet plugin.
73+
*
74+
* @see https://wordpress.org/plugins/akismet/
75+
*/
76+
if ( \defined( 'AKISMET_VERSION' ) ) {
77+
Akismet::init();
78+
}
79+
6980
/**
7081
* Adds Seriously Simple Podcasting support.
7182
*

readme.txt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ For reasons of data protection, it is not possible to see the followers of other
132132

133133
== Changelog ==
134134

135+
= Unreleased =
136+
137+
* Improved: Interactions moderation
138+
* Improved: Compatibility with Akismet
139+
135140
= 4.4.0 =
136141

137142
* Added: Setting to enable/disable Authorized-Fetch
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
<?php
2+
/**
3+
* Test Akismet Integration.
4+
*
5+
* @package ActivityPub
6+
*/
7+
8+
namespace Activitypub\Tests\Integration;
9+
10+
use Activitypub\Integration\Akismet;
11+
use WP_UnitTestCase;
12+
13+
/**
14+
* Test Akismet Integration class.
15+
*
16+
* @coversDefaultClass \Activitypub\Integration\Akismet
17+
*/
18+
class Test_Akismet extends WP_UnitTestCase {
19+
/**
20+
* A test post.
21+
*
22+
* @var int
23+
*/
24+
protected static $post_id;
25+
26+
/**
27+
* Create fake data before tests run.
28+
*
29+
* @param WP_UnitTest_Factory $factory Helper that creates fake data.
30+
*/
31+
public static function wpSetUpBeforeClass( $factory ) {
32+
self::$post_id = $factory->post->create();
33+
}
34+
35+
/**
36+
* Clean up after tests.
37+
*/
38+
public static function wpTearDownAfterClass() {
39+
wp_delete_post( self::$post_id, true );
40+
}
41+
42+
/**
43+
* Test comment_row_actions method.
44+
*
45+
* @covers ::comment_row_actions
46+
*/
47+
public function test_comment_row_actions() {
48+
// Create a normal comment.
49+
$normal_comment_id = $this->factory->comment->create(
50+
array(
51+
'comment_post_ID' => self::$post_id,
52+
'comment_content' => 'Normal comment',
53+
)
54+
);
55+
56+
// Create an ActivityPub comment.
57+
$ap_comment_id = $this->factory->comment->create(
58+
array(
59+
'comment_post_ID' => self::$post_id,
60+
'comment_content' => 'ActivityPub comment',
61+
)
62+
);
63+
add_comment_meta( $ap_comment_id, 'protocol', 'activitypub' );
64+
65+
// Test actions for normal comment.
66+
$actions = array(
67+
'approve' => 'Approve',
68+
'spam' => 'Spam',
69+
'history' => 'History',
70+
);
71+
72+
$filtered_actions = Akismet::comment_row_actions( $actions, get_comment( $normal_comment_id ) );
73+
$this->assertArrayHasKey( 'history', $filtered_actions, 'History action should remain for normal comments' );
74+
75+
// Test actions for ActivityPub comment.
76+
$filtered_actions = Akismet::comment_row_actions( $actions, get_comment( $ap_comment_id ) );
77+
$this->assertArrayNotHasKey( 'history', $filtered_actions, 'History action should be removed for ActivityPub comments' );
78+
$this->assertArrayHasKey( 'approve', $filtered_actions, 'Other actions should remain untouched' );
79+
$this->assertArrayHasKey( 'spam', $filtered_actions, 'Other actions should remain untouched' );
80+
81+
// Clean up.
82+
wp_delete_comment( $normal_comment_id, true );
83+
wp_delete_comment( $ap_comment_id, true );
84+
}
85+
86+
/**
87+
* Test init method.
88+
*
89+
* @covers ::init
90+
*/
91+
public function test_init() {
92+
Akismet::init();
93+
94+
$this->assertEquals( 10, has_filter( 'comment_row_actions', array( Akismet::class, 'comment_row_actions' ) ) );
95+
}
96+
}

0 commit comments

Comments
 (0)