PluginProbe
ActivityPub / 7.0.0
ActivityPub v7.0.0
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.0.0, at includes/handler/class-follow.php

121 lines 2.9 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\Notification;
11 use Activitypub\Activity\Activity;
12 use Activitypub\Collection\Actors;
13 use Activitypub\Collection\Followers;
14
15 use function Activitypub\add_to_outbox;
16
17 /**
18 * Handle Follow requests.
19 */
20 class Follow {
21 /**
22 * Initialize the class, registering WordPress hooks.
23 */
24 public static function init() {
25 \add_action(
26 'activitypub_inbox_follow',
27 array( self::class, 'handle_follow' )
28 );
29
30 \add_action(
31 'activitypub_followers_post_follow',
32 array( self::class, 'queue_accept' ),
33 10,
34 4
35 );
36 }
37
38 /**
39 * Handle "Follow" requests.
40 *
41 * @param array $activity The activity object.
42 */
43 public static function handle_follow( $activity ) {
44 $user = Actors::get_by_resource( $activity['object'] );
45
46 if ( ! $user || is_wp_error( $user ) ) {
47 // If we can not find a user, we can not initiate a follow process.
48 return;
49 }
50
51 $user_id = $user->get__id();
52
53 // Save follower.
54 $remote_actor = Followers::add_follower(
55 $user_id,
56 $activity['actor']
57 );
58
59 if ( \is_wp_error( $remote_actor ) ) {
60 return $remote_actor;
61 }
62
63 $remote_actor = \get_post( $remote_actor );
64
65 /**
66 * Fires after a new follower has been added.
67 *
68 * @param string $actor The URL of the actor (follower) who initiated the follow.
69 * @param array $activity The complete activity data of the follow request.
70 * @param int $user_id The ID of the WordPress user being followed.
71 * @param \WP_Post|\WP_Error $remote_actor The Actor object containing the new follower's data.
72 */
73 do_action( 'activitypub_followers_post_follow', $activity['actor'], $activity, $user_id, $remote_actor );
74
75 // Send notification.
76 $notification = new Notification(
77 'follow',
78 $remote_actor->guid,
79 $activity,
80 $user_id
81 );
82 $notification->send();
83 }
84
85 /**
86 * Send Accept response.
87 *
88 * @param string $actor The Actor URL.
89 * @param array $activity_object The Activity object.
90 * @param int $user_id The ID of the WordPress User.
91 * @param \WP_Post|\WP_Error $remote_actor The Actor object.
92 */
93 public static function queue_accept( $actor, $activity_object, $user_id, $remote_actor ) {
94 if ( \is_wp_error( $remote_actor ) ) {
95 // Impossible to send a "Reject" because we can not get the Remote-Inbox.
96 return;
97 }
98
99 // Only send minimal data.
100 $activity_object = array_intersect_key(
101 $activity_object,
102 array_flip(
103 array(
104 'id',
105 'type',
106 'actor',
107 'object',
108 )
109 )
110 );
111
112 $activity = new Activity();
113 $activity->set_type( 'Accept' );
114 $activity->set_actor( Actors::get_by_id( $user_id )->get_id() );
115 $activity->set_object( $activity_object );
116 $activity->set_to( array( $actor ) );
117
118 add_to_outbox( $activity, null, $user_id, ACTIVITYPUB_CONTENT_VISIBILITY_PRIVATE );
119 }
120 }
121