PluginProbe
ActivityPub / 8.1.1
ActivityPub v8.1.1
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
activitypub / includes / handler / outbox / class-undo.php

class-undo.php in ActivityPub 8.1.1, at includes/handler/outbox/class-undo.php

89 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Outbox Undo handler file.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub\Handler\Outbox;
9
10 use Activitypub\Collection\Outbox as Outbox_Collection;
11 use Activitypub\Moderation;
12
13 use function Activitypub\object_to_uri;
14 use function Activitypub\unfollow;
15
16 /**
17 * Handle outgoing Undo activities.
18 */
19 class Undo {
20 /**
21 * Initialize the class, registering WordPress hooks.
22 */
23 public static function init() {
24 \add_filter( 'activitypub_outbox_undo', array( self::class, 'handle_undo' ), 10, 2 );
25 }
26
27 /**
28 * Handle outgoing "Undo" activities from local actors.
29 *
30 * Resolves the referenced activity from the outbox and delegates
31 * to the appropriate collection method to reverse its side effects
32 * and create the Undo activity.
33 *
34 * @param array $data The activity data array.
35 * @param int $user_id The user ID.
36 *
37 * @return int|\WP_Error The undo outbox item ID, or WP_Error on failure.
38 */
39 public static function handle_undo( $data, $user_id = null ) {
40 $id = object_to_uri( $data['object'] ?? '' );
41
42 if ( empty( $id ) ) {
43 return $data;
44 }
45
46 $outbox_item = Outbox_Collection::get_by_guid( $id );
47
48 if ( \is_wp_error( $outbox_item ) ) {
49 return $data;
50 }
51
52 // Verify the user owns this outbox item (blog actor user_id === 0 can undo any).
53 if ( $user_id > 0 && (int) $outbox_item->post_author !== $user_id ) {
54 return new \WP_Error(
55 'activitypub_forbidden',
56 \__( 'You can only undo your own activities.', 'activitypub' ),
57 array( 'status' => 403 )
58 );
59 }
60
61 $activity_type = \get_post_meta( $outbox_item->ID, '_activitypub_activity_type', true );
62
63 switch ( $activity_type ) {
64 case 'Follow':
65 $stored = \json_decode( $outbox_item->post_content, true );
66 $target = object_to_uri( $stored['object'] ?? '' );
67
68 if ( $target ) {
69 return unfollow( $target, $user_id );
70 }
71
72 return $data;
73
74 case 'Block':
75 $stored = \json_decode( $outbox_item->post_content, true );
76 $actor_uri = \is_array( $stored ) ? object_to_uri( $stored['object'] ?? '' ) : '';
77
78 if ( $actor_uri ) {
79 Moderation::remove_user_block( $user_id, Moderation::TYPE_ACTOR, $actor_uri );
80 }
81
82 return Outbox_Collection::undo( $outbox_item );
83
84 default:
85 return Outbox_Collection::undo( $outbox_item );
86 }
87 }
88 }
89