| @@ -1,47 +1,90 @@ | ||
| 1 | 1 | <?php |
| 2 | +/** | |
| 3 | + * Undo handler file. | |
| 4 | + * | |
| 5 | + * @package Activitypub | |
| 6 | + */ | |
| 7 | + | |
| 2 | 8 | namespace Activitypub\Handler; |
| 3 | 9 | |
| 4 | -use Activitypub\Collection\Users; | |
| 10 | +use Activitypub\Collection\Actors; | |
| 5 | 11 | use Activitypub\Collection\Followers; |
| 12 | +use Activitypub\Comment; | |
| 6 | 13 | |
| 14 | +use function Activitypub\object_to_uri; | |
| 15 | + | |
| 7 | 16 | /** |
| 8 | - * Handle Undo requests | |
| 17 | + * Handle Undo requests. | |
| 9 | 18 | */ |
| 10 | 19 | class Undo { |
| 11 | 20 | /** |
| 12 | - * Initialize the class, registering WordPress hooks | |
| 21 | + * Initialize the class, registering WordPress hooks. | |
| 13 | 22 | */ |
| 14 | 23 | public static function init() { |
| 15 | 24 | \add_action( |
| 16 | 25 | 'activitypub_inbox_undo', |
| 17 | - array( self::class, 'handle_undo' ) | |
| 26 | + array( self::class, 'handle_undo' ), | |
| 27 | + 10, | |
| 28 | + 2 | |
| 18 | 29 | ); |
| 19 | 30 | } |
| 20 | 31 | |
| 21 | 32 | /** |
| 22 | - * Handle "Unfollow" requests | |
| 33 | + * Handle "Unfollow" requests. | |
| 23 | 34 | * |
| 24 | - * @param array $activity The JSON "Undo" Activity | |
| 25 | - * @param int $user_id The ID of the ID of the WordPress User | |
| 35 | + * @param array $activity The JSON "Undo" Activity. | |
| 36 | + * @param int|null $user_id The ID of the user who initiated the "Undo" activity. | |
| 26 | 37 | */ |
| 27 | - public static function handle_undo( $activity ) { | |
| 38 | + public static function handle_undo( $activity, $user_id ) { | |
| 28 | 39 | if ( |
| 29 | - isset( $activity['object']['type'] ) && | |
| 30 | - 'Follow' === $activity['object']['type'] && | |
| 31 | - isset( $activity['object']['object'] ) && | |
| 32 | - filter_var( $activity['object']['object'], FILTER_VALIDATE_URL ) | |
| 40 | + ! isset( $activity['object']['type'] ) || | |
| 41 | + ! isset( $activity['object']['object'] ) | |
| 33 | 42 | ) { |
| 34 | - $user = Users::get_by_resource( $activity['object']['object'] ); | |
| 43 | + return; | |
| 44 | + } | |
| 35 | 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 = Actors::get_by_resource( $id ); | |
| 53 | + | |
| 36 | 54 | if ( ! $user || is_wp_error( $user ) ) { |
| 37 | - // If we can not find a user, | |
| 38 | - // we can not initiate a follow process | |
| 55 | + // If we can not find a user, we can not initiate a follow process. | |
| 39 | 56 | return; |
| 40 | 57 | } |
| 41 | 58 | |
| 42 | 59 | $user_id = $user->get__id(); |
| 60 | + $actor = object_to_uri( $activity['actor'] ); | |
| 43 | 61 | |
| 44 | - Followers::remove_follower( $user_id, $activity['actor'] ); | |
| 62 | + $state = Followers::remove_follower( $user_id, $actor ); | |
| 45 | 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 ); | |
| 46 | 89 | } |
| 47 | 90 | } |