2023-12-08 23:23:11 +00:00
|
|
|
<?php
|
2024-10-09 12:44:17 +00:00
|
|
|
/**
|
|
|
|
* Undo handler file.
|
|
|
|
*
|
|
|
|
* @package Activitypub
|
|
|
|
*/
|
|
|
|
|
2023-12-08 23:23:11 +00:00
|
|
|
namespace Activitypub\Handler;
|
|
|
|
|
2024-02-08 12:31:25 +00:00
|
|
|
use Activitypub\Collection\Users;
|
2023-12-08 23:23:11 +00:00
|
|
|
use Activitypub\Collection\Followers;
|
2024-10-09 12:44:17 +00:00
|
|
|
use Activitypub\Comment;
|
2023-12-08 23:23:11 +00:00
|
|
|
|
2024-05-09 15:26:55 +00:00
|
|
|
use function Activitypub\object_to_uri;
|
|
|
|
|
2023-12-08 23:23:11 +00:00
|
|
|
/**
|
2024-10-09 12:44:17 +00:00
|
|
|
* Handle Undo requests.
|
2023-12-08 23:23:11 +00:00
|
|
|
*/
|
|
|
|
class Undo {
|
|
|
|
/**
|
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_undo',
|
|
|
|
array( self::class, 'handle_undo' )
|
|
|
|
);
|
2023-12-08 23:23:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2024-10-09 12:44:17 +00:00
|
|
|
* Handle "Unfollow" requests.
|
2023-12-08 23:23:11 +00:00
|
|
|
*
|
2024-10-09 12:44:17 +00:00
|
|
|
* @param array $activity The JSON "Undo" Activity.
|
2023-12-08 23:23:11 +00:00
|
|
|
*/
|
2024-02-08 12:31:25 +00:00
|
|
|
public static function handle_undo( $activity ) {
|
2023-12-08 23:23:11 +00:00
|
|
|
if (
|
2024-10-09 12:44:17 +00:00
|
|
|
! isset( $activity['object']['type'] ) ||
|
|
|
|
! isset( $activity['object']['object'] )
|
2023-12-08 23:23:11 +00:00
|
|
|
) {
|
2024-10-09 12:44:17 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$type = $activity['object']['type'];
|
|
|
|
|
|
|
|
// Handle "Unfollow" requests.
|
|
|
|
if ( 'Follow' === $type ) {
|
2024-05-09 15:26:55 +00:00
|
|
|
$user_id = object_to_uri( $activity['object']['object'] );
|
2024-10-09 12:44:17 +00:00
|
|
|
$user = Users::get_by_resource( $user_id );
|
2024-02-08 12:31:25 +00:00
|
|
|
|
|
|
|
if ( ! $user || is_wp_error( $user ) ) {
|
2024-10-09 12:44:17 +00:00
|
|
|
// If we can not find a user, we can not initiate a follow process.
|
2024-02-08 12:31:25 +00:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$user_id = $user->get__id();
|
2024-05-09 15:26:55 +00:00
|
|
|
$actor = object_to_uri( $activity['actor'] );
|
2024-02-08 12:31:25 +00:00
|
|
|
|
2024-05-09 15:26:55 +00:00
|
|
|
Followers::remove_follower( $user_id, $actor );
|
2023-12-08 23:23:11 +00:00
|
|
|
}
|
2024-10-09 12:44:17 +00:00
|
|
|
|
|
|
|
// Handle "Undo" requests for "Like" and "Create" activities.
|
|
|
|
if ( in_array( $type, array( 'Like', 'Create', 'Announce' ), true ) ) {
|
|
|
|
if ( ACTIVITYPUB_DISABLE_INCOMING_INTERACTIONS ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$object_id = object_to_uri( $activity['object'] );
|
|
|
|
$comment = Comment::object_id_to_comment( esc_url_raw( $object_id ) );
|
|
|
|
|
|
|
|
if ( empty( $comment ) ) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$state = wp_trash_comment( $comment );
|
|
|
|
|
|
|
|
do_action( 'activitypub_handled_undo', $activity, $user_id, isset( $state ) ? $state : null, null );
|
|
|
|
}
|
2023-12-08 23:23:11 +00:00
|
|
|
}
|
|
|
|
}
|