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