2023-11-30 11:43:48 +01:00
|
|
|
<?php
|
|
|
|
namespace Activitypub\Handler;
|
|
|
|
|
2023-12-22 17:45:27 +01:00
|
|
|
use Activitypub\Collection\Users;
|
2023-11-30 11:43:48 +01:00
|
|
|
use Activitypub\Collection\Followers;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle Undo requests
|
|
|
|
*/
|
|
|
|
class Undo {
|
|
|
|
/**
|
|
|
|
* Initialize the class, registering WordPress hooks
|
|
|
|
*/
|
|
|
|
public static function init() {
|
2023-12-22 17:45:27 +01:00
|
|
|
\add_action(
|
|
|
|
'activitypub_inbox_undo',
|
|
|
|
array( self::class, 'handle_undo' )
|
|
|
|
);
|
2023-11-30 11:43:48 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle "Unfollow" requests
|
|
|
|
*
|
|
|
|
* @param array $activity The JSON "Undo" Activity
|
|
|
|
* @param int $user_id The ID of the ID of the WordPress User
|
|
|
|
*/
|
2023-12-22 17:45:27 +01:00
|
|
|
public static function handle_undo( $activity ) {
|
2023-11-30 11:43:48 +01:00
|
|
|
if (
|
|
|
|
isset( $activity['object']['type'] ) &&
|
2023-12-22 17:45:27 +01:00
|
|
|
'Follow' === $activity['object']['type'] &&
|
|
|
|
isset( $activity['object']['object'] ) &&
|
|
|
|
filter_var( $activity['object']['object'], FILTER_VALIDATE_URL )
|
2023-11-30 11:43:48 +01:00
|
|
|
) {
|
2023-12-22 17:45:27 +01: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-11-30 11:43:48 +01:00
|
|
|
Followers::remove_follower( $user_id, $activity['actor'] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|