PluginProbe
ActivityPub / 5.3.2
ActivityPub v5.3.2
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 5.3.2, at includes/handler/class-delete.php

197 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 /* @var \Activitypub\Model\Follower $follower Follower object. */
118 $follower = Followers::get_follower_by_actor( $activity['actor'] );
119
120 // Verify that Actor is deleted.
121 if ( $follower && Http::is_tombstone( $activity['actor'] ) ) {
122 $follower->delete();
123 self::maybe_delete_interactions( $activity );
124 }
125 }
126
127 /**
128 * Delete Reactions if Actor-URL is a Tombstone.
129 *
130 * @param array $activity The delete activity.
131 */
132 public static function maybe_delete_interactions( $activity ) {
133 // Verify that Actor is deleted.
134 if ( Http::is_tombstone( $activity['actor'] ) ) {
135 \wp_schedule_single_event(
136 \time(),
137 'activitypub_delete_actor_interactions',
138 array( $activity['actor'] )
139 );
140 }
141 }
142
143 /**
144 * Delete comments from an Actor.
145 *
146 * @param string $actor The URL of the actor whose comments to delete.
147 */
148 public static function delete_interactions( $actor ) {
149 $comments = Interactions::get_interactions_by_actor( $actor );
150
151 if ( is_array( $comments ) ) {
152 foreach ( $comments as $comment ) {
153 wp_delete_comment( $comment->comment_ID, true );
154 }
155 }
156 }
157
158 /**
159 * Delete a Reaction if URL is a Tombstone.
160 *
161 * @param array $activity The delete activity.
162 */
163 public static function maybe_delete_interaction( $activity ) {
164 if ( is_array( $activity['object'] ) ) {
165 $id = $activity['object']['id'];
166 } else {
167 $id = $activity['object'];
168 }
169
170 $comments = Interactions::get_interaction_by_id( $id );
171
172 if ( $comments && Http::is_tombstone( $id ) ) {
173 foreach ( $comments as $comment ) {
174 wp_delete_comment( $comment->comment_ID, true );
175 }
176 }
177 }
178
179 /**
180 * Defer signature verification for `Delete` requests.
181 *
182 * @param bool $defer Whether to defer signature verification.
183 * @param WP_REST_Request $request The request object.
184 *
185 * @return bool Whether to defer signature verification.
186 */
187 public static function defer_signature_verification( $defer, $request ) {
188 $json = $request->get_json_params();
189
190 if ( isset( $json['type'] ) && 'Delete' === $json['type'] ) {
191 return true;
192 }
193
194 return false;
195 }
196 }
197