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