Skip to content

Commit 06e44b8

Browse files
authored
Refactor notification system to use action hooks instead (#2223)
1 parent d84d147 commit 06e44b8

27 files changed

+2473
-601
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: changed
3+
4+
Standardized notification handling with new hooks for better extensibility and consistency.

includes/class-notification.php

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@
99

1010
/**
1111
* Notification class.
12+
*
13+
* @deprecated unreleased Use action hooks like 'activitypub_handled_{type}' instead.
1214
*/
1315
class Notification {
1416
/**
@@ -48,6 +50,8 @@ class Notification {
4850
* @param int $target The WordPress User-Id.
4951
*/
5052
public function __construct( $type, $actor, $activity, $target ) {
53+
\_deprecated_class( __CLASS__, 'unreleased', 'Use action hooks like "activitypub_handled_{type}" instead.' );
54+
5155
$this->type = $type;
5256
$this->actor = $actor;
5357
$this->object = $activity;
@@ -63,15 +67,19 @@ public function send() {
6367
/**
6468
* Action to send ActivityPub notifications.
6569
*
70+
* @deprecated unreleased Use "activitypub_handled_{$type}" instead.
71+
*
6672
* @param Notification $instance The notification object.
6773
*/
68-
do_action( 'activitypub_notification', $this );
74+
\do_action_deprecated( 'activitypub_notification', array( $this ), 'unreleased', "activitypub_handled_{$type}" );
6975

7076
/**
7177
* Type-specific action to send ActivityPub notifications.
7278
*
79+
* @deprecated unreleased Use "activitypub_handled_{$type}" instead.
80+
*
7381
* @param Notification $instance The notification object.
7482
*/
75-
do_action( "activitypub_notification_{$type}", $this );
83+
\do_action_deprecated( "activitypub_notification_{$type}", array( $this ), 'unreleased', "activitypub_handled_{$type}" );
7684
}
7785
}

includes/collection/class-followers.php

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,13 +69,13 @@ public static function add_follower( $user_id, $actor ) {
6969
/**
7070
* Remove a Follower.
7171
*
72-
* @param int $post_id The ID of the remote Actor.
73-
* @param int $user_id The ID of the WordPress User.
72+
* @param \WP_Post|int $post_or_id The ID of the remote Actor.
73+
* @param int $user_id The ID of the WordPress User.
7474
*
7575
* @return bool True on success, false on failure.
7676
*/
77-
public static function remove( $post_id, $user_id ) {
78-
$post = \get_post( $post_id );
77+
public static function remove( $post_or_id, $user_id ) {
78+
$post = \get_post( $post_or_id );
7979

8080
if ( ! $post ) {
8181
return false;
@@ -93,7 +93,7 @@ public static function remove( $post_id, $user_id ) {
9393
*/
9494
\do_action( 'activitypub_followers_pre_remove_follower', $post, $user_id, Remote_Actors::get_actor( $post ) );
9595

96-
return \delete_post_meta( $post_id, self::FOLLOWER_META_KEY, $user_id );
96+
return \delete_post_meta( $post->ID, self::FOLLOWER_META_KEY, $user_id );
9797
}
9898

9999
/**

includes/collection/class-interactions.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,7 @@ public static function update_comment( $activity ) {
8989
*
9090
* @param array $activity Activity array.
9191
*
92-
* @return array|false Comment data or `false` on failure.
92+
* @return array|string|int|\WP_Error|false Comment data or `false` on failure.
9393
*/
9494
public static function add_reaction( $activity ) {
9595
$url = object_to_uri( $activity['object'] );

includes/handler/class-accept.php

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@
1010
use Activitypub\Collection\Following;
1111
use Activitypub\Collection\Outbox;
1212
use Activitypub\Collection\Remote_Actors;
13-
use Activitypub\Notification;
1413

1514
use function Activitypub\object_to_uri;
1615

@@ -22,19 +21,8 @@ class Accept {
2221
* Initialize the class, registering WordPress hooks.
2322
*/
2423
public static function init() {
25-
\add_action(
26-
'activitypub_inbox_accept',
27-
array( self::class, 'handle_accept' ),
28-
10,
29-
2
30-
);
31-
32-
\add_filter(
33-
'activitypub_validate_object',
34-
array( self::class, 'validate_object' ),
35-
10,
36-
3
37-
);
24+
\add_action( 'activitypub_inbox_accept', array( self::class, 'handle_accept' ), 10, 2 );
25+
\add_filter( 'activitypub_validate_object', array( self::class, 'validate_object' ), 10, 3 );
3826
}
3927

4028
/**
@@ -60,16 +48,18 @@ public static function handle_accept( $accept, $user_id ) {
6048
return;
6149
}
6250

63-
Following::accept( $actor_post, $user_id );
64-
65-
// Send notification.
66-
$notification = new Notification(
67-
'accept',
68-
$actor_post->guid,
69-
$accept,
70-
$user_id
71-
);
72-
$notification->send();
51+
$result = Following::accept( $actor_post, $user_id );
52+
$success = ! \is_wp_error( $result );
53+
54+
/**
55+
* Fires after an ActivityPub Accept activity has been handled.
56+
*
57+
* @param array $accept The ActivityPub activity data.
58+
* @param int $user_id The local user ID.
59+
* @param bool $success True on success, false otherwise.
60+
* @param \WP_Post|\WP_Error $result The remote actor post or error.
61+
*/
62+
\do_action( 'activitypub_handled_accept', $accept, $user_id, $success, $result );
7363
}
7464

7565
/**

includes/handler/class-announce.php

Lines changed: 12 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -23,12 +23,7 @@ class Announce {
2323
* Initialize the class, registering WordPress hooks.
2424
*/
2525
public static function init() {
26-
\add_action(
27-
'activitypub_inbox_announce',
28-
array( self::class, 'handle_announce' ),
29-
10,
30-
3
31-
);
26+
\add_action( 'activitypub_inbox_announce', array( self::class, 'handle_announce' ), 10, 3 );
3227
}
3328

3429
/**
@@ -106,21 +101,22 @@ public static function maybe_save_announce( $activity, $user_id ) {
106101
return;
107102
}
108103

109-
$state = Interactions::add_reaction( $activity );
110-
$reaction = null;
104+
$success = false;
105+
$result = Interactions::add_reaction( $activity );
111106

112-
if ( $state && ! is_wp_error( $state ) ) {
113-
$reaction = get_comment( $state );
107+
if ( $result && ! is_wp_error( $result ) ) {
108+
$success = true;
109+
$result = get_comment( $result );
114110
}
115111

116112
/**
117-
* Fires after an Announce has been saved.
113+
* Fires after an ActivityPub Announce activity has been handled.
118114
*
119-
* @param array $activity The activity-object.
120-
* @param int $user_id The id of the local blog-user.
121-
* @param mixed $state The state of the reaction.
122-
* @param mixed $reaction The reaction.
115+
* @param array $activity The ActivityPub activity data.
116+
* @param int $user_id The local user ID.
117+
* @param bool $success True on success, false otherwise.
118+
* @param array|string|int|\WP_Error|false $result The WP_Comment object of the created announce/repost comment, or null if creation failed.
123119
*/
124-
do_action( 'activitypub_handled_announce', $activity, $user_id, $state, $reaction );
120+
\do_action( 'activitypub_handled_announce', $activity, $user_id, $success, $result );
125121
}
126122
}

includes/handler/class-create.php

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -61,22 +61,23 @@ public static function handle_create( $activity, $user_id, $activity_object = nu
6161
return;
6262
}
6363

64-
$state = Interactions::add_comment( $activity );
65-
$reaction = null;
64+
$success = false;
65+
$result = Interactions::add_comment( $activity );
6666

67-
if ( $state && ! \is_wp_error( $state ) ) {
68-
$reaction = \get_comment( $state );
67+
if ( $result && ! \is_wp_error( $result ) ) {
68+
$success = true;
69+
$result = \get_comment( $result );
6970
}
7071

7172
/**
72-
* Fires after a Create activity has been handled.
73+
* Fires after an ActivityPub Create activity has been handled.
7374
*
74-
* @param array $activity The activity-object.
75-
* @param int $user_id The id of the local blog-user.
76-
* @param \WP_Comment|\WP_Error $state The comment object or WP_Error.
77-
* @param \WP_Comment|\WP_Error|null $reaction The reaction object or WP_Error.
75+
* @param array $activity The ActivityPub activity data.
76+
* @param int $user_id The local user ID.
77+
* @param bool $success True on success, false otherwise.
78+
* @param array|string|int|\WP_Error|false $result The WP_Comment object of the created comment, or null if creation failed.
7879
*/
79-
\do_action( 'activitypub_handled_create', $activity, $user_id, $state, $reaction );
80+
\do_action( 'activitypub_handled_create', $activity, $user_id, $success, $result );
8081
}
8182

8283
/**

includes/handler/class-delete.php

Lines changed: 47 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ class Delete {
2121
* Initialize the class, registering WordPress hooks.
2222
*/
2323
public static function init() {
24-
\add_action( 'activitypub_inbox_delete', array( self::class, 'handle_delete' ) );
24+
\add_action( 'activitypub_inbox_delete', array( self::class, 'handle_delete' ), 10, 2 );
2525
\add_filter( 'activitypub_defer_signature_verification', array( self::class, 'defer_signature_verification' ), 10, 2 );
2626
\add_action( 'activitypub_delete_actor_interactions', array( self::class, 'delete_interactions' ) );
2727

@@ -33,9 +33,12 @@ public static function init() {
3333
* Handles "Delete" requests.
3434
*
3535
* @param array $activity The delete activity.
36+
* @param int $user_id The local user ID.
3637
*/
37-
public static function handle_delete( $activity ) {
38+
public static function handle_delete( $activity, $user_id ) {
3839
$object_type = $activity['object']['type'] ?? '';
40+
$success = false;
41+
$result = null;
3942

4043
switch ( $object_type ) {
4144
/*
@@ -48,7 +51,7 @@ public static function handle_delete( $activity ) {
4851
case 'Organization':
4952
case 'Service':
5053
case 'Application':
51-
self::maybe_delete_follower( $activity );
54+
$result = self::maybe_delete_follower( $activity );
5255
break;
5356

5457
/*
@@ -63,7 +66,7 @@ public static function handle_delete( $activity ) {
6366
case 'Video':
6467
case 'Event':
6568
case 'Document':
66-
self::maybe_delete_interaction( $activity );
69+
$result = self::maybe_delete_interaction( $activity );
6770
break;
6871

6972
/*
@@ -72,7 +75,7 @@ public static function handle_delete( $activity ) {
7275
* @see: https://www.w3.org/TR/activitystreams-vocabulary/#dfn-tombstone
7376
*/
7477
case 'Tombstone':
75-
self::maybe_delete_interaction( $activity );
78+
$result = self::maybe_delete_interaction( $activity );
7679
break;
7780

7881
/*
@@ -88,34 +91,52 @@ public static function handle_delete( $activity ) {
8891

8992
// Check if Object is an Actor.
9093
if ( $activity['actor'] === $activity['object'] ) {
91-
self::maybe_delete_follower( $activity );
94+
$result = self::maybe_delete_follower( $activity );
9295
} else { // Assume an interaction otherwise.
93-
self::maybe_delete_interaction( $activity );
96+
$result = self::maybe_delete_interaction( $activity );
9497
}
9598
// Maybe handle Delete Activity for other Object Types.
9699
break;
97100
}
101+
102+
$success = (bool) $result;
103+
104+
/**
105+
* Fires after an ActivityPub Delete activity has been handled.
106+
*
107+
* @param array $activity The ActivityPub activity data.
108+
* @param int $user_id The local user ID.
109+
* @param bool $success True on success, false otherwise.
110+
* @param mixed|null $result The result of the delete operation (e.g., WP_Comment object or deletion status).
111+
*/
112+
\do_action( 'activitypub_handled_delete', $activity, $user_id, $success, $result );
98113
}
99114

100115
/**
101116
* Delete a Follower if Actor-URL is a Tombstone.
102117
*
103118
* @param array $activity The delete activity.
119+
*
120+
* @return bool True on success, false otherwise.
104121
*/
105122
public static function maybe_delete_follower( $activity ) {
106123
$follower = Remote_Actors::get_by_uri( $activity['actor'] );
107124

108125
// Verify that Actor is deleted.
109126
if ( ! is_wp_error( $follower ) && Tombstone::exists( $activity['actor'] ) ) {
110-
Remote_Actors::delete( $follower->ID );
127+
$state = Remote_Actors::delete( $follower->ID );
111128
self::maybe_delete_interactions( $activity );
112129
}
130+
131+
return $state ?? false;
113132
}
114133

115134
/**
116135
* Delete Reactions if Actor-URL is a Tombstone.
117136
*
118137
* @param array $activity The delete activity.
138+
*
139+
* @return bool True on success, false otherwise.
119140
*/
120141
public static function maybe_delete_interactions( $activity ) {
121142
// Verify that Actor is deleted.
@@ -125,26 +146,40 @@ public static function maybe_delete_interactions( $activity ) {
125146
'activitypub_delete_actor_interactions',
126147
array( $activity['actor'] )
127148
);
149+
150+
return true;
128151
}
152+
153+
return false;
129154
}
130155

131156
/**
132157
* Delete comments from an Actor.
133158
*
134159
* @param string $actor The URL of the actor whose comments to delete.
160+
*
161+
* @return bool True on success, false otherwise.
135162
*/
136163
public static function delete_interactions( $actor ) {
137164
$comments = Interactions::get_interactions_by_actor( $actor );
138165

139166
foreach ( $comments as $comment ) {
140167
wp_delete_comment( $comment, true );
141168
}
169+
170+
if ( $comments ) {
171+
return true;
172+
} else {
173+
return false;
174+
}
142175
}
143176

144177
/**
145178
* Delete a Reaction if URL is a Tombstone.
146179
*
147180
* @param array $activity The delete activity.
181+
*
182+
* @return bool True on success, false otherwise.
148183
*/
149184
public static function maybe_delete_interaction( $activity ) {
150185
if ( is_array( $activity['object'] ) ) {
@@ -159,7 +194,11 @@ public static function maybe_delete_interaction( $activity ) {
159194
foreach ( $comments as $comment ) {
160195
wp_delete_comment( $comment->comment_ID, true );
161196
}
197+
198+
return true;
162199
}
200+
201+
return false;
163202
}
164203

165204
/**

0 commit comments

Comments
 (0)