PluginProbe
ActivityPub / 7.6.1
ActivityPub v7.6.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 / class-follow.php

class-follow.php in ActivityPub 7.6.1, at includes/handler/class-follow.php

144 lines 4.4 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Follow handler file.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub\Handler;
9
10 use Activitypub\Activity\Activity;
11 use Activitypub\Collection\Actors;
12 use Activitypub\Collection\Followers;
13
14 use function Activitypub\add_to_outbox;
15
16 /**
17 * Handle Follow requests.
18 */
19 class Follow {
20 /**
21 * Initialize the class, registering WordPress hooks.
22 */
23 public static function init() {
24 \add_action( 'activitypub_inbox_follow', array( self::class, 'handle_follow' ), 10, 2 );
25 \add_action( 'activitypub_handled_follow', array( self::class, 'queue_accept' ), 10, 4 );
26 }
27
28 /**
29 * Handle "Follow" requests.
30 *
31 * @param array $activity The activity object.
32 * @param int|int[] $user_ids The user ID(s).
33 */
34 public static function handle_follow( $activity, $user_ids ) {
35 // Extract the user ID (follow requests are always for a single user).
36 $user_id = \is_array( $user_ids ) ? \reset( $user_ids ) : $user_ids;
37
38 if ( Actors::APPLICATION_USER_ID === $user_id ) {
39 self::queue_reject( $activity, $user_id );
40 return;
41 }
42
43 // Save follower.
44 $remote_actor = Followers::add(
45 $user_id,
46 $activity['actor']
47 );
48
49 $success = ! \is_wp_error( $remote_actor );
50
51 if ( ! \is_wp_error( $remote_actor ) ) {
52 $remote_actor = \get_post( $remote_actor );
53 }
54
55 /**
56 * Fires after a new follower has been added.
57 *
58 * @deprecated 7.5.0 Use "activitypub_handled_follow" instead.
59 *
60 * @param string $actor The URL of the actor (follower) who initiated the follow.
61 * @param array $activity The complete activity data of the follow request.
62 * @param int $user_id The ID of the WordPress user being followed.
63 * @param \WP_Post|\WP_Error $remote_actor The Actor object containing the new follower's data.
64 */
65 \do_action_deprecated( 'activitypub_followers_post_follow', array( $activity['actor'], $activity, $user_id, $remote_actor ), '7.5.0', 'activitypub_handled_follow' );
66
67 /**
68 * Fires after a Follow activity has been handled.
69 *
70 * @param array $activity The ActivityPub activity data.
71 * @param int[] $user_ids The local user IDs.
72 * @param bool $success True on success, false otherwise.
73 * @param \WP_Post|\WP_Error $remote_actor The remote actor/follower, or WP_Error if failed.
74 */
75 \do_action( 'activitypub_handled_follow', $activity, (array) $user_ids, $success, $remote_actor );
76 }
77
78 /**
79 * Send Accept response.
80 *
81 * @param array $activity_object The ActivityPub activity data.
82 * @param int|int[] $user_ids The local user IDs.
83 * @param bool $success True on success, false otherwise.
84 * @param \WP_Post|\WP_Error $remote_actor The remote actor/follower, or WP_Error if failed.
85 */
86 public static function queue_accept( $activity_object, $user_ids, $success, $remote_actor ) {
87 if ( \is_wp_error( $remote_actor ) ) {
88 // Impossible to send a "Reject" because we can not get the Remote-Inbox.
89 return;
90 }
91
92 // Extract the user ID from the array (follow requests are always for a single user).
93 $user_id = \is_array( $user_ids ) ? \reset( $user_ids ) : $user_ids;
94
95 $actor = $activity_object['actor'];
96
97 // Only send minimal data.
98 $activity_object = array_intersect_key(
99 $activity_object,
100 array(
101 'id' => 1,
102 'type' => 1,
103 'actor' => 1,
104 'object' => 1,
105 )
106 );
107
108 $activity = new Activity();
109 $activity->set_type( 'Accept' );
110 $activity->set_actor( Actors::get_by_id( $user_id )->get_id() );
111 $activity->set_object( $activity_object );
112 $activity->set_to( array( $actor ) );
113
114 add_to_outbox( $activity, null, $user_id, ACTIVITYPUB_CONTENT_VISIBILITY_PRIVATE );
115 }
116
117 /**
118 * Send Reject response.
119 *
120 * @param array $activity The Activity array.
121 * @param int $user_id The ID of the WordPress User.
122 */
123 public static function queue_reject( $activity, $user_id ) {
124 // Only send minimal data.
125 $origin_activity = array_intersect_key(
126 $activity,
127 array(
128 'id' => 1,
129 'type' => 1,
130 'actor' => 1,
131 'object' => 1,
132 )
133 );
134
135 $activity = new Activity();
136 $activity->set_type( 'Reject' );
137 $activity->set_actor( Actors::get_by_id( $user_id )->get_id() );
138 $activity->set_object( $origin_activity );
139 $activity->set_to( array( $origin_activity['actor'] ) );
140
141 add_to_outbox( $activity, null, $user_id, ACTIVITYPUB_CONTENT_VISIBILITY_PRIVATE );
142 }
143 }
144