PluginProbe
ActivityPub / 1.3.0
ActivityPub v1.3.0
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
activitypub / includes / handler / class-delete.php

class-delete.php in ActivityPub 1.3.0, at includes/handler/class-delete.php

166 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Activitypub\Handler;
3
4 use WP_Error;
5 use WP_REST_Request;
6 use Activitypub\Http;
7 use Activitypub\Collection\Followers;
8 use Activitypub\Collection\Interactions;
9
10 /**
11 * Handles Delete requests.
12 */
13 class Delete {
14 /**
15 * Initialize the class, registering WordPress hooks
16 */
17 public static function init() {
18 \add_action( 'activitypub_inbox_delete', array( self::class, 'handle_delete' ), 10, 2 );
19 // defer signature verification for `Delete` requests.
20 \add_filter( 'activitypub_defer_signature_verification', array( self::class, 'defer_signature_verification' ), 10, 2 );
21 // side effect
22 \add_action( 'activitypub_delete_actor_interactions', array( self::class, 'delete_interactions' ), 10, 1 );
23 }
24
25 /**
26 * Handles "Delete" requests.
27 *
28 * @param array $activity The delete activity.
29 * @param int $user_id The ID of the user performing the delete activity.
30 */
31 public static function handle_delete( $activity, $user_id ) {
32 $object_type = isset( $activity['object']['type'] ) ? $activity['object']['type'] : '';
33
34 switch ( $object_type ) {
35 // Actor Types
36 // @see https://www.w3.org/TR/activitystreams-vocabulary/#actor-types
37 case 'Person':
38 case 'Group':
39 case 'Organization':
40 case 'Service':
41 case 'Application':
42 self::maybe_delete_follower( $user_id, $activity );
43 break;
44 // Object and Link Types
45 // @see https://www.w3.org/TR/activitystreams-vocabulary/#object-types
46 case 'Note':
47 case 'Article':
48 case 'Image':
49 case 'Audio':
50 case 'Video':
51 case 'Event':
52 case 'Document':
53 self::maybe_delete_interaction( $activity );
54 break;
55 // Tombstone Type
56 // @see: https://www.w3.org/TR/activitystreams-vocabulary/#dfn-tombstone
57 case 'Tombstone':
58 self::maybe_delete_interaction( $activity );
59 break;
60 // Minimal Activity
61 // @see https://www.w3.org/TR/activitystreams-core/#example-1
62 default:
63 // ignore non Minimal Activities.
64 if ( ! is_string( $activity['object'] ) ) {
65 return;
66 }
67
68 // check if Object is an Actor.
69 if ( $activity['actor'] === $activity['object'] ) {
70 self::maybe_delete_follower( $activity );
71 self::maybe_delete_interactions( $activity );
72 } else { // assume a interaction otherwise.
73 self::maybe_delete_interaction( $activity );
74 }
75 // maybe handle Delete Activity for other Object Types.
76 break;
77 }
78 }
79
80 /**
81 * Delete a Follower if Actor-URL is a Tombstone.
82 *
83 * @param array $activity The delete activity.
84 */
85 public static function maybe_delete_follower( $activity ) {
86 $follower = Followers::get_follower_by_actor( $activity['actor'] );
87
88 // verify if Actor is deleted.
89 if ( $follower && Http::is_tombstone( $activity['actor'] ) ) {
90 $follower->delete();
91 }
92 }
93
94 /**
95 * Delete Reactions if Actor-URL is a Tombstone.
96 *
97 * @param array $activity The delete activity.
98 */
99 public static function maybe_delete_interactions( $activity ) {
100 // verify if Actor is deleted.
101 if ( Http::is_tombstone( $activity['actor'] ) ) {
102 \wp_schedule_single_event(
103 \time(),
104 'activitypub_delete_actor_interactions',
105 array( $activity['actor'] )
106 );
107 }
108 }
109
110 /**
111 * Delete comments from an Actor.
112 *
113 * @param array $comments The comments to delete.
114 */
115 public static function delete_interactions( $actor ) {
116 $comments = Interactions::get_interactions_by_actor( $actor );
117
118 if ( is_array( $comments ) ) {
119 foreach ( $comments as $comment ) {
120 wp_delete_comment( $comment->comment_ID );
121 }
122 }
123 }
124
125 /**
126 * Delete a Reaction if URL is a Tombstone.
127 *
128 * @param array $activity The delete activity.
129 *
130 * @return void
131 */
132 public static function maybe_delete_interaction( $activity ) {
133 if ( is_array( $activity['object'] ) ) {
134 $id = $activity['object']['id'];
135 } else {
136 $id = $activity['object'];
137 }
138
139 $comments = Interactions::get_interaction_by_id( $id );
140
141 if ( $comments && Http::is_tombstone( $id ) ) {
142 foreach ( $comments as $comment ) {
143 wp_delete_comment( $comment->comment_ID, true );
144 }
145 }
146 }
147
148 /**
149 * Defer signature verification for `Delete` requests.
150 *
151 * @param bool $defer Whether to defer signature verification.
152 * @param WP_REST_Request $request The request object.
153 *
154 * @return bool Whether to defer signature verification.
155 */
156 public static function defer_signature_verification( $defer, $request ) {
157 $json = $request->get_json_params();
158
159 if ( isset( $json['type'] ) && 'Delete' === $json['type'] ) {
160 return true;
161 }
162
163 return false;
164 }
165 }
166