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 / class-reject.php

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

108 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 * Reject handler file.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub\Handler;
9
10 use Activitypub\Collection\Following;
11 use Activitypub\Collection\Outbox;
12 use Activitypub\Collection\Remote_Actors;
13
14 use function Activitypub\object_to_uri;
15
16 /**
17 * Handle "Reject" requests.
18 */
19 class Reject {
20 /**
21 * Initialize the class, registering WordPress hooks.
22 */
23 public static function init() {
24 \add_action( 'activitypub_inbox_reject', array( self::class, 'handle_reject' ), 10, 2 );
25 \add_filter( 'activitypub_validate_object', array( self::class, 'validate_object' ), 10, 3 );
26 }
27
28 /**
29 * Handles "Reject" requests.
30 *
31 * @param array $reject The activity-object.
32 * @param int|int[] $user_ids The user ID(s).
33 */
34 public static function handle_reject( $reject, $user_ids ) {
35 // Validate that there is a preceding Activity.
36 $outbox_post = Outbox::get_by_guid( $reject['object']['id'] );
37
38 if ( \is_wp_error( $outbox_post ) ) {
39 return;
40 }
41
42 // We currently only support reject for Follow activities. But we will support more in the future.
43 switch ( \get_post_meta( $outbox_post->ID, '_activitypub_activity_type', true ) ) {
44 case 'Follow':
45 self::reject_follow( $reject, $user_ids );
46 break;
47 default:
48 break;
49 }
50 }
51
52 /**
53 * Reject a "Follow" request.
54 *
55 * @param array $reject The activity-object.
56 * @param int|int[] $user_ids The user ID(s).
57 */
58 private static function reject_follow( $reject, $user_ids ) {
59 $actor_uri = $reject['object']['actor'] ?? '';
60 $actor_post = Remote_Actors::get_by_uri( object_to_uri( $actor_uri ) );
61
62 if ( \is_wp_error( $actor_post ) ) {
63 return;
64 }
65
66 $user_id = is_array( $user_ids ) ? reset( $user_ids ) : $user_ids;
67 $result = Following::reject( $actor_post, $user_id );
68 $success = ! \is_wp_error( $result );
69
70 /**
71 * Fires after an ActivityPub Reject activity has been handled.
72 *
73 * @param array $reject The ActivityPub activity data.
74 * @param int[] $user_ids The local user IDs.
75 * @param bool $success True on success, false otherwise.
76 * @param \WP_Post|\WP_Error $result Actor post on success, WP_Error on failure.
77 */
78 \do_action( 'activitypub_handled_reject', $reject, (array) $user_ids, $success, $result );
79 }
80
81 /**
82 * Validate the object.
83 *
84 * @param bool $valid The validation state.
85 * @param string $param The object parameter.
86 * @param \WP_REST_Request $request The request object.
87 *
88 * @return bool The validation state: true if valid, false if not.
89 */
90 public static function validate_object( $valid, $param, $request ) {
91 $activity = $request->get_json_params();
92
93 if ( empty( $activity['type'] ) ) {
94 return false;
95 }
96
97 if ( 'Reject' !== $activity['type'] ) {
98 return $valid;
99 }
100
101 if ( ! isset( $activity['actor'], $activity['object'] ) ) {
102 return false;
103 }
104
105 return $valid;
106 }
107 }
108