PluginProbe
ActivityPub / 3.2.4
ActivityPub v3.2.4
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
← All changes | includes/handler/class-undo.php +45 -72 9.1.03.2.4 View file →
@@ -1,102 +1,75 @@
1 1 <?php
2 -/**
3 - * Undo handler file.
4 - *
5 - * @package Activitypub
6 - */
7 -
8 2 namespace Activitypub\Handler;
9 3
10 -use Activitypub\Collection\Inbox as Inbox_Collection;
4 +use Activitypub\Collection\Users;
5 +use Activitypub\Collection\Followers;
6 +use Activitypub\Comment;
11 7
12 8 use function Activitypub\object_to_uri;
13 9
14 10 /**
15 - * Handle Undo requests.
11 + * Handle Undo requests
16 12 */
17 13 class Undo {
18 14 /**
19 - * Initialize the class, registering WordPress hooks.
15 + * Initialize the class, registering WordPress hooks
20 16 */
21 17 public static function init() {
22 - \add_action( 'activitypub_inbox_undo', array( self::class, 'handle_undo' ), 10, 2 );
23 - \add_action( 'activitypub_validate_object', array( self::class, 'validate_object' ), 10, 3 );
18 + \add_action(
19 + 'activitypub_inbox_undo',
20 + array( self::class, 'handle_undo' )
21 + );
24 22 }
25 23
26 24 /**
27 - * Handle "Unfollow" requests.
25 + * Handle "Unfollow" requests
28 26 *
29 - * @param array $activity The JSON "Undo" Activity.
30 - * @param int|int[]|null $user_ids The user ID(s).
27 + * @param array $activity The JSON "Undo" Activity
28 + * @param int $user_id The ID of the ID of the WordPress User
31 29 */
32 - public static function handle_undo( $activity, $user_ids ) {
33 - $success = false;
30 + public static function handle_undo( $activity ) {
31 + if (
32 + ! isset( $activity['object']['type'] ) ||
33 + ! isset( $activity['object']['object'] )
34 + ) {
35 + return;
36 + }
34 37
35 - /*
36 - * Resolve the sender so Inbox::undo() can verify ownership. A genuinely absent actor
37 - * maps to null (no ownership check, for programmatic callers), but an actor that is
38 - * present yet unparseable must be rejected rather than skipping the check — passing
39 - * null there would re-open the undo-by-id attack.
40 - */
41 - $actor = isset( $activity['actor'] ) ? object_to_uri( $activity['actor'] ) : null;
38 + $type = $activity['object']['type'];
42 39
43 - if ( isset( $activity['actor'] ) && empty( $actor ) ) {
44 - $result = new \WP_Error(
45 - 'activitypub_undo_invalid_actor',
46 - \__( 'The Undo activity has an invalid actor.', 'activitypub' ),
47 - array( 'status' => 400 )
48 - );
49 - } else {
50 - $result = Inbox_Collection::undo( object_to_uri( $activity['object'] ), $actor );
51 - }
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 );
52 44
53 - if ( $result && ! \is_wp_error( $result ) ) {
54 - $success = true;
55 - }
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 + }
56 50
57 - /**
58 - * Fires after an ActivityPub Undo activity has been handled.
59 - *
60 - * @param array $activity The ActivityPub activity data.
61 - * @param int[] $user_ids The local user IDs.
62 - * @param bool $success True on success, false on failure.
63 - * @param \WP_Comment|string $result The target, based on the activity that is being undone.
64 - */
65 - \do_action( 'activitypub_handled_undo', $activity, (array) $user_ids, $success, $result );
66 - }
51 + $user_id = $user->get__id();
52 + $actor = object_to_uri( $activity['actor'] );
67 53
68 - /**
69 - * Validate the object.
70 - *
71 - * @param bool $valid The validation state.
72 - * @param string $param The object parameter.
73 - * @param \WP_REST_Request $request The request object.
74 - *
75 - * @return bool The validation state: true if valid, false if not.
76 - */
77 - public static function validate_object( $valid, $param, $request ) {
78 - $activity = $request->get_json_params();
54 + Followers::remove_follower( $user_id, $actor );
55 + }
79 56
80 - if ( empty( $activity['type'] ) ) {
81 - return false;
82 - }
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 + }
83 62
84 - if ( 'Undo' !== $activity['type'] ) {
85 - return $valid;
86 - }
63 + $object_id = object_to_uri( $activity['object'] );
64 + $comment = Comment::object_id_to_comment( esc_url_raw( $object_id ) );
87 65
88 - if ( ! isset( $activity['actor'], $activity['object'] ) ) {
89 - return false;
90 - }
66 + if ( empty( $comment ) ) {
67 + return;
68 + }
91 69
92 - if ( ! \is_array( $activity['object'] ) && ! \is_string( $activity['object'] ) ) {
93 - return false;
94 - }
70 + $state = wp_trash_comment( $comment );
95 71
96 - if ( \is_array( $activity['object'] ) && ! isset( $activity['object']['id'] ) ) {
97 - return false;
72 + do_action( 'activitypub_handled_undo', $activity, $user_id, isset( $state ) ? $state : null, null );
98 73 }
99 -
100 - return $valid;
101 74 }
102 75 }