PluginProbe
ActivityPub / 5.7.0
ActivityPub v5.7.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 5.7.0, at includes/handler/class-follow.php

115 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 $follower = Followers::add_follower(
55 $user_id,
56 $activity['actor']
57 );
58
59 /**
60 * Fires after a new follower has been added.
61 *
62 * @param string $actor The URL of the actor (follower) who initiated the follow.
63 * @param array $activity The complete activity data of the follow request.
64 * @param int $user_id The ID of the WordPress user being followed.
65 * @param \Activitypub\Model\Follower|\WP_Error $follower The Follower object containing the new follower's data.
66 */
67 do_action( 'activitypub_followers_post_follow', $activity['actor'], $activity, $user_id, $follower );
68
69 // Send notification.
70 $notification = new Notification(
71 'follow',
72 $activity['actor'],
73 $activity,
74 $user_id
75 );
76 $notification->send();
77 }
78
79 /**
80 * Send Accept response.
81 *
82 * @param string $actor The Actor URL.
83 * @param array $activity_object The Activity object.
84 * @param int $user_id The ID of the WordPress User.
85 * @param \Activitypub\Model\Follower|\WP_Error $follower The Follower object.
86 */
87 public static function queue_accept( $actor, $activity_object, $user_id, $follower ) {
88 if ( \is_wp_error( $follower ) ) {
89 // Impossible to send a "Reject" because we can not get the Remote-Inbox.
90 return;
91 }
92
93 // Only send minimal data.
94 $activity_object = array_intersect_key(
95 $activity_object,
96 array_flip(
97 array(
98 'id',
99 'type',
100 'actor',
101 'object',
102 )
103 )
104 );
105
106 $activity = new Activity();
107 $activity->set_type( 'Accept' );
108 $activity->set_actor( Actors::get_by_id( $user_id )->get_id() );
109 $activity->set_object( $activity_object );
110 $activity->set_to( array( $actor ) );
111
112 add_to_outbox( $activity, null, $user_id, ACTIVITYPUB_CONTENT_VISIBILITY_PRIVATE );
113 }
114 }
115