PluginProbe
ActivityPub / 4.5.1
ActivityPub v4.5.1
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 4.5.1, at includes/handler/class-delete.php

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