| 1 |
<?php |
| 2 |
namespace Activitypub\Handler; |
| 3 |
|
| 4 |
use Activitypub\Collection\Users; |
| 5 |
use Activitypub\Collection\Followers; |
| 6 |
use Activitypub\Comment; |
| 7 |
|
| 8 |
use function Activitypub\object_to_uri; |
| 9 |
|
| 10 |
/** |
| 11 |
* Handle Undo requests |
| 12 |
*/ |
| 13 |
class Undo { |
| 14 |
/** |
| 15 |
* Initialize the class, registering WordPress hooks |
| 16 |
*/ |
| 17 |
public static function init() { |
| 18 |
\add_action( |
| 19 |
'activitypub_inbox_undo', |
| 20 |
array( self::class, 'handle_undo' ) |
| 21 |
); |
| 22 |
} |
| 23 |
|
| 24 |
/** |
| 25 |
* Handle "Unfollow" requests |
| 26 |
* |
| 27 |
* @param array $activity The JSON "Undo" Activity |
| 28 |
* @param int $user_id The ID of the ID of the WordPress User |
| 29 |
*/ |
| 30 |
public static function handle_undo( $activity ) { |
| 31 |
if ( |
| 32 |
! isset( $activity['object']['type'] ) || |
| 33 |
! isset( $activity['object']['object'] ) |
| 34 |
) { |
| 35 |
return; |
| 36 |
} |
| 37 |
|
| 38 |
$type = $activity['object']['type']; |
| 39 |
|
| 40 |
// Handle "Unfollow" requests |
| 41 |
if ( 'Follow' === $type ) { |
| 42 |
$user_id = object_to_uri( $activity['object']['object'] ); |
| 43 |
$user = Users::get_by_resource( $user_id ); |
| 44 |
|
| 45 |
if ( ! $user || is_wp_error( $user ) ) { |
| 46 |
// If we can not find a user, |
| 47 |
// we can not initiate a follow process |
| 48 |
return; |
| 49 |
} |
| 50 |
|
| 51 |
$user_id = $user->get__id(); |
| 52 |
$actor = object_to_uri( $activity['actor'] ); |
| 53 |
|
| 54 |
Followers::remove_follower( $user_id, $actor ); |
| 55 |
} |
| 56 |
|
| 57 |
// Handle "Undo" requests for "Like" and "Create" activities |
| 58 |
if ( in_array( $type, array( 'Like', 'Create', 'Announce' ), true ) ) { |
| 59 |
if ( ACTIVITYPUB_DISABLE_INCOMING_INTERACTIONS ) { |
| 60 |
return; |
| 61 |
} |
| 62 |
|
| 63 |
$object_id = object_to_uri( $activity['object'] ); |
| 64 |
$comment = Comment::object_id_to_comment( esc_url_raw( $object_id ) ); |
| 65 |
|
| 66 |
if ( empty( $comment ) ) { |
| 67 |
return; |
| 68 |
} |
| 69 |
|
| 70 |
$state = wp_trash_comment( $comment ); |
| 71 |
|
| 72 |
do_action( 'activitypub_handled_undo', $activity, $user_id, isset( $state ) ? $state : null, null ); |
| 73 |
} |
| 74 |
} |
| 75 |
} |
| 76 |
|