Skip to content

Commit 8239677

Browse files
authored
Enhance keyword blocking to support content maps (#2093)
1 parent fd037a6 commit 8239677

File tree

4 files changed

+48
-3
lines changed

4 files changed

+48
-3
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+
Moderation now checks blocked keywords across all language variants of the content, summary and name fields.

includes/activity/class-generic-object.php

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,20 @@
2525
* @method array[]|null get_attachment() Gets the attachment property of the object.
2626
* @method string[]|null get_cc() Gets the secondary recipients of the object.
2727
* @method string|null get_content() Gets the content property of the object.
28+
* @method string[]|null get_content_map() Gets the content map property of the object.
2829
* @method string[]|null get_endpoints() Gets the endpoint property of the object.
2930
* @method string[]|null get_icon() Gets the icon property of the object.
3031
* @method string|null get_id() Gets the object's unique global identifier.
3132
* @method string[]|null get_image() Gets the image property of the object.
3233
* @method string[]|string|null get_in_reply_to() Gets the objects this object is in reply to.
3334
* @method string|null get_inbox() Gets the inbox property of the object.
3435
* @method string|null get_name() Gets the natural language name of the object.
36+
* @method string[]|null get_name_map() Gets the name map property of the object.
3537
* @method Base_Object|string|null get_object() Gets the direct object of the activity.
3638
* @method string|null get_preferred_username() Gets the preferred username of the object.
3739
* @method string|null get_published() Gets the date and time the object was published in ISO 8601 format.
3840
* @method string|null get_summary() Gets the natural language summary of the object.
41+
* @method string[]|null get_summary_map() Gets the summary map property of the object.
3942
* @method array[]|null get_tag() Gets the tag property of the object.
4043
* @method string[]|string|null get_to() Gets the primary recipients of the object.
4144
* @method string get_type() Gets the type of the object.

includes/class-moderation.php

Lines changed: 23 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -242,11 +242,31 @@ private static function check_activity_against_blocks( $activity, $blocked_actor
242242

243243
// Check blocked keywords in activity content.
244244
if ( $has_object ) {
245-
$content = $activity->get_object()->get_content() . ' ' . $activity->get_object()->get_summary() . ' ' . $activity->get_object()->get_name();
246-
if ( is_actor( $activity->get_object() ) ) {
247-
$content .= ' ' . $activity->get_object()->get_preferred_username();
245+
$object = $activity->get_object();
246+
$content_map = array();
247+
$content_map[] = $object->get_content();
248+
$content_map[] = $object->get_summary();
249+
$content_map[] = $object->get_name();
250+
251+
if ( is_actor( $object ) ) {
252+
$content_map[] = $object->get_preferred_username();
248253
}
249254

255+
if ( \is_array( $object->get_content_map() ) ) {
256+
$content_map = \array_merge( $content_map, \array_values( $object->get_content_map() ) );
257+
}
258+
259+
if ( \is_array( $object->get_summary_map() ) ) {
260+
$content_map = \array_merge( $content_map, \array_values( $object->get_summary_map() ) );
261+
}
262+
263+
if ( \is_array( $object->get_name_map() ) ) {
264+
$content_map = \array_merge( $content_map, \array_values( $object->get_name_map() ) );
265+
}
266+
267+
$content_map = \array_filter( $content_map );
268+
$content = \implode( ' ', $content_map );
269+
250270
foreach ( $blocked_keywords as $keyword ) {
251271
if ( \stripos( $content, $keyword ) !== false ) {
252272
return true;

tests/includes/class-test-moderation.php

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -288,6 +288,24 @@ public function test_activity_is_blocked_site_wide() {
288288
);
289289
$this->assertTrue( Moderation::activity_is_blocked( $activity ) );
290290

291+
// Test keyword blocking.
292+
/* @var Activity $activity Activity. */
293+
$activity = Activity::init_from_array(
294+
array(
295+
'type' => 'Create',
296+
'actor' => 'https://good.example.com/@user',
297+
'object' => array(
298+
'id' => 'https://example.com/note/1',
299+
'type' => 'Note',
300+
'content_map' => array(
301+
'en' => 'Check out this product, buy now!',
302+
'de' => 'Überprüfe dieses Produkt, kaufe jetzt!',
303+
),
304+
),
305+
)
306+
);
307+
$this->assertTrue( Moderation::activity_is_blocked( $activity ) );
308+
291309
// Test non-blocked activity.
292310
/* @var Activity $activity Activity. */
293311
$activity = Activity::init_from_array(

0 commit comments

Comments
 (0)