| @@ -1,9 +1,13 @@ | ||
| 1 | 1 | <?php |
| 2 | 2 | namespace Activitypub\Handler; |
| 3 | 3 | |
| 4 | +use Activitypub\Collection\Users; | |
| 4 | 5 | use Activitypub\Collection\Followers; |
| 6 | +use Activitypub\Comment; | |
| 5 | 7 | |
| 8 | +use function Activitypub\object_to_uri; | |
| 9 | + | |
| 6 | 10 | /** |
| 7 | 11 | * Handle Undo requests |
| 8 | 12 | */ |
| 9 | 13 | class Undo { |
| @@ -10,9 +14,12 @@ | ||
| 10 | 14 | /** |
| 11 | 15 | * Initialize the class, registering WordPress hooks |
| 12 | 16 | */ |
| 13 | 17 | public static function init() { |
| 14 | - \add_action( 'activitypub_inbox_undo', array( self::class, 'handle_undo' ), 10, 2 ); | |
| 18 | + \add_action( | |
| 19 | + 'activitypub_inbox_undo', | |
| 20 | + array( self::class, 'handle_undo' ) | |
| 21 | + ); | |
| 15 | 22 | } |
| 16 | 23 | |
| 17 | 24 | /** |
| 18 | 25 | * Handle "Unfollow" requests |
| @@ -19,13 +26,50 @@ | ||
| 19 | 26 | * |
| 20 | 27 | * @param array $activity The JSON "Undo" Activity |
| 21 | 28 | * @param int $user_id The ID of the ID of the WordPress User |
| 22 | 29 | */ |
| 23 | - public static function handle_undo( $activity, $user_id ) { | |
| 30 | + public static function handle_undo( $activity ) { | |
| 24 | 31 | if ( |
| 25 | - isset( $activity['object']['type'] ) && | |
| 26 | - 'Follow' === $activity['object']['type'] | |
| 32 | + ! isset( $activity['object']['type'] ) || | |
| 33 | + ! isset( $activity['object']['object'] ) | |
| 27 | 34 | ) { |
| 28 | - Followers::remove_follower( $user_id, $activity['actor'] ); | |
| 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 ); | |
| 29 | 73 | } |
| 30 | 74 | } |
| 31 | 75 | } |