PluginProbe
ActivityPub / 7.0.1
ActivityPub v7.0.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 7.0.1, at includes/handler/class-delete.php

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