| 1 |
<?php |
| 2 |
namespace Activitypub\Handler; |
| 3 |
|
| 4 |
use Activitypub\Collection\Users; |
| 5 |
use Activitypub\Collection\Followers; |
| 6 |
|
| 7 |
use function Activitypub\object_to_uri; |
| 8 |
|
| 9 |
/** |
| 10 |
* Handle Undo requests |
| 11 |
*/ |
| 12 |
class Undo { |
| 13 |
/** |
| 14 |
* Initialize the class, registering WordPress hooks |
| 15 |
*/ |
| 16 |
public static function init() { |
| 17 |
\add_action( |
| 18 |
'activitypub_inbox_undo', |
| 19 |
array( self::class, 'handle_undo' ) |
| 20 |
); |
| 21 |
} |
| 22 |
|
| 23 |
/** |
| 24 |
* Handle "Unfollow" requests |
| 25 |
* |
| 26 |
* @param array $activity The JSON "Undo" Activity |
| 27 |
* @param int $user_id The ID of the ID of the WordPress User |
| 28 |
*/ |
| 29 |
public static function handle_undo( $activity ) { |
| 30 |
if ( |
| 31 |
isset( $activity['object']['type'] ) && |
| 32 |
'Follow' === $activity['object']['type'] && |
| 33 |
isset( $activity['object']['object'] ) |
| 34 |
) { |
| 35 |
$user_id = object_to_uri( $activity['object']['object'] ); |
| 36 |
$user = Users::get_by_resource( $user_id ); |
| 37 |
|
| 38 |
if ( ! $user || is_wp_error( $user ) ) { |
| 39 |
// If we can not find a user, |
| 40 |
// we can not initiate a follow process |
| 41 |
return; |
| 42 |
} |
| 43 |
|
| 44 |
$user_id = $user->get__id(); |
| 45 |
$actor = object_to_uri( $activity['actor'] ); |
| 46 |
|
| 47 |
Followers::remove_follower( $user_id, $actor ); |
| 48 |
} |
| 49 |
} |
| 50 |
} |
| 51 |
|