2023-12-08 23:23:11 +00:00
|
|
|
<?php
|
|
|
|
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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle Undo requests
|
|
|
|
*/
|
|
|
|
class Undo {
|
|
|
|
/**
|
|
|
|
* Initialize the class, registering WordPress hooks
|
|
|
|
*/
|
|
|
|
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
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle "Unfollow" requests
|
|
|
|
*
|
|
|
|
* @param array $activity The JSON "Undo" Activity
|
|
|
|
* @param int $user_id The ID of the ID of the WordPress User
|
|
|
|
*/
|
2024-02-08 12:31:25 +00:00
|
|
|
public static function handle_undo( $activity ) {
|
2023-12-08 23:23:11 +00:00
|
|
|
if (
|
|
|
|
isset( $activity['object']['type'] ) &&
|
2024-02-08 12:31:25 +00:00
|
|
|
'Follow' === $activity['object']['type'] &&
|
|
|
|
isset( $activity['object']['object'] ) &&
|
|
|
|
filter_var( $activity['object']['object'], FILTER_VALIDATE_URL )
|
2023-12-08 23:23:11 +00:00
|
|
|
) {
|
2024-02-08 12:31:25 +00:00
|
|
|
$user = Users::get_by_resource( $activity['object']['object'] );
|
|
|
|
|
|
|
|
if ( ! $user || is_wp_error( $user ) ) {
|
|
|
|
// If we can not find a user,
|
|
|
|
// we can not initiate a follow process
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
$user_id = $user->get__id();
|
|
|
|
|
2023-12-08 23:23:11 +00:00
|
|
|
Followers::remove_follower( $user_id, $activity['actor'] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|