2023-12-08 23:23:11 +00:00
|
|
|
<?php
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Delete handler file.
|
|
|
|
*
|
|
|
|
* @package Activitypub
|
|
|
|
*/
|
|
|
|
|
2023-12-08 23:23:11 +00:00
|
|
|
namespace Activitypub\Handler;
|
|
|
|
|
|
|
|
use WP_REST_Request;
|
|
|
|
use Activitypub\Http;
|
|
|
|
use Activitypub\Collection\Followers;
|
|
|
|
use Activitypub\Collection\Interactions;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles Delete requests.
|
|
|
|
*/
|
|
|
|
class Delete {
|
|
|
|
/**
|
2024-10-09 12:44:17 +00:00
|
|
|
* Initialize the class, registering WordPress hooks.
|
2023-12-08 23:23:11 +00:00
|
|
|
*/
|
|
|
|
public static function init() {
|
2024-02-08 12:31:25 +00:00
|
|
|
\add_action(
|
|
|
|
'activitypub_inbox_delete',
|
|
|
|
array( self::class, 'handle_delete' )
|
|
|
|
);
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
// Defer signature verification for `Delete` requests.
|
2024-02-08 12:31:25 +00:00
|
|
|
\add_filter(
|
|
|
|
'activitypub_defer_signature_verification',
|
|
|
|
array( self::class, 'defer_signature_verification' ),
|
|
|
|
10,
|
|
|
|
2
|
|
|
|
);
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
// Side effect.
|
2024-02-08 12:31:25 +00:00
|
|
|
\add_action(
|
|
|
|
'activitypub_delete_actor_interactions',
|
|
|
|
array( self::class, 'delete_interactions' )
|
|
|
|
);
|
2023-12-08 23:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handles "Delete" requests.
|
|
|
|
*
|
|
|
|
* @param array $activity The delete activity.
|
|
|
|
*/
|
2024-02-08 12:31:25 +00:00
|
|
|
public static function handle_delete( $activity ) {
|
2023-12-08 23:23:11 +00:00
|
|
|
$object_type = isset( $activity['object']['type'] ) ? $activity['object']['type'] : '';
|
|
|
|
|
|
|
|
switch ( $object_type ) {
|
2024-10-09 12:44:17 +00:00
|
|
|
/*
|
|
|
|
* Actor Types.
|
|
|
|
*
|
|
|
|
* @see https://www.w3.org/TR/activitystreams-vocabulary/#actor-types
|
|
|
|
*/
|
2023-12-08 23:23:11 +00:00
|
|
|
case 'Person':
|
|
|
|
case 'Group':
|
|
|
|
case 'Organization':
|
|
|
|
case 'Service':
|
|
|
|
case 'Application':
|
2024-02-08 12:31:25 +00:00
|
|
|
self::maybe_delete_follower( $activity );
|
2023-12-08 23:23:11 +00:00
|
|
|
break;
|
2024-10-09 12:44:17 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Object and Link Types.
|
|
|
|
*
|
|
|
|
* @see https://www.w3.org/TR/activitystreams-vocabulary/#object-types
|
|
|
|
*/
|
2023-12-08 23:23:11 +00:00
|
|
|
case 'Note':
|
|
|
|
case 'Article':
|
|
|
|
case 'Image':
|
|
|
|
case 'Audio':
|
|
|
|
case 'Video':
|
|
|
|
case 'Event':
|
|
|
|
case 'Document':
|
|
|
|
self::maybe_delete_interaction( $activity );
|
|
|
|
break;
|
2024-10-09 12:44:17 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Tombstone Type.
|
|
|
|
*
|
|
|
|
* @see: https://www.w3.org/TR/activitystreams-vocabulary/#dfn-tombstone
|
|
|
|
*/
|
2023-12-08 23:23:11 +00:00
|
|
|
case 'Tombstone':
|
|
|
|
self::maybe_delete_interaction( $activity );
|
|
|
|
break;
|
2024-10-09 12:44:17 +00:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Minimal Activity.
|
|
|
|
*
|
|
|
|
* @see https://www.w3.org/TR/activitystreams-core/#example-1
|
|
|
|
*/
|
2023-12-08 23:23:11 +00:00
|
|
|
default:
|
2024-10-09 12:44:17 +00:00
|
|
|
// Ignore non Minimal Activities.
|
2023-12-08 23:23:11 +00:00
|
|
|
if ( ! is_string( $activity['object'] ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
// Check if Object is an Actor.
|
2023-12-08 23:23:11 +00:00
|
|
|
if ( $activity['actor'] === $activity['object'] ) {
|
|
|
|
self::maybe_delete_follower( $activity );
|
2024-10-09 12:44:17 +00:00
|
|
|
} else { // Assume an interaction otherwise.
|
2023-12-08 23:23:11 +00:00
|
|
|
self::maybe_delete_interaction( $activity );
|
|
|
|
}
|
2024-10-09 12:44:17 +00:00
|
|
|
// Maybe handle Delete Activity for other Object Types.
|
2023-12-08 23:23:11 +00:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a Follower if Actor-URL is a Tombstone.
|
|
|
|
*
|
|
|
|
* @param array $activity The delete activity.
|
|
|
|
*/
|
|
|
|
public static function maybe_delete_follower( $activity ) {
|
|
|
|
$follower = Followers::get_follower_by_actor( $activity['actor'] );
|
|
|
|
|
2024-10-09 12:44:17 +00:00
|
|
|
// Verify that Actor is deleted.
|
2023-12-08 23:23:11 +00:00
|
|
|
if ( $follower && Http::is_tombstone( $activity['actor'] ) ) {
|
|
|
|
$follower->delete();
|
2024-03-28 09:39:50 +00:00
|
|
|
self::maybe_delete_interactions( $activity );
|
2023-12-08 23:23:11 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete Reactions if Actor-URL is a Tombstone.
|
|
|
|
*
|
|
|
|
* @param array $activity The delete activity.
|
|
|
|
*/
|
|
|
|
public static function maybe_delete_interactions( $activity ) {
|
2024-10-09 12:44:17 +00:00
|
|
|
// Verify that Actor is deleted.
|
2023-12-08 23:23:11 +00:00
|
|
|
if ( Http::is_tombstone( $activity['actor'] ) ) {
|
|
|
|
\wp_schedule_single_event(
|
|
|
|
\time(),
|
|
|
|
'activitypub_delete_actor_interactions',
|
|
|
|
array( $activity['actor'] )
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete comments from an Actor.
|
|
|
|
*
|
2024-10-09 12:44:17 +00:00
|
|
|
* @param array $actor The actor whose comments to delete.
|
2023-12-08 23:23:11 +00:00
|
|
|
*/
|
|
|
|
public static function delete_interactions( $actor ) {
|
|
|
|
$comments = Interactions::get_interactions_by_actor( $actor );
|
|
|
|
|
|
|
|
if ( is_array( $comments ) ) {
|
|
|
|
foreach ( $comments as $comment ) {
|
|
|
|
wp_delete_comment( $comment->comment_ID );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Delete a Reaction if URL is a Tombstone.
|
|
|
|
*
|
|
|
|
* @param array $activity The delete activity.
|
|
|
|
*/
|
|
|
|
public static function maybe_delete_interaction( $activity ) {
|
|
|
|
if ( is_array( $activity['object'] ) ) {
|
|
|
|
$id = $activity['object']['id'];
|
|
|
|
} else {
|
|
|
|
$id = $activity['object'];
|
|
|
|
}
|
|
|
|
|
|
|
|
$comments = Interactions::get_interaction_by_id( $id );
|
|
|
|
|
|
|
|
if ( $comments && Http::is_tombstone( $id ) ) {
|
|
|
|
foreach ( $comments as $comment ) {
|
|
|
|
wp_delete_comment( $comment->comment_ID, true );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Defer signature verification for `Delete` requests.
|
|
|
|
*
|
|
|
|
* @param bool $defer Whether to defer signature verification.
|
|
|
|
* @param WP_REST_Request $request The request object.
|
|
|
|
*
|
|
|
|
* @return bool Whether to defer signature verification.
|
|
|
|
*/
|
|
|
|
public static function defer_signature_verification( $defer, $request ) {
|
|
|
|
$json = $request->get_json_params();
|
|
|
|
|
|
|
|
if ( isset( $json['type'] ) && 'Delete' === $json['type'] ) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|