PluginProbe
ActivityPub / 2.1.1
ActivityPub v2.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 / rest / class-users.php

class-users.php in ActivityPub 2.1.1, at includes/rest/class-users.php

151 lines 3.3 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 namespace Activitypub\Rest;
3
4 use WP_Error;
5 use WP_REST_Server;
6 use WP_REST_Request;
7 use WP_REST_Response;
8 use Activitypub\Webfinger;
9 use Activitypub\Activity\Activity;
10 use Activitypub\Collection\Users as User_Collection;
11
12 use function Activitypub\is_activitypub_request;
13
14 /**
15 * ActivityPub Followers REST-Class
16 *
17 * @author Matthias Pfefferle
18 *
19 * @see https://www.w3.org/TR/activitypub/#followers
20 */
21 class Users {
22 /**
23 * Initialize the class, registering WordPress hooks
24 */
25 public static function init() {
26 self::register_routes();
27 }
28
29 /**
30 * Register routes
31 */
32 public static function register_routes() {
33 \register_rest_route(
34 ACTIVITYPUB_REST_NAMESPACE,
35 '/users/(?P<user_id>[\w\-\.]+)',
36 array(
37 array(
38 'methods' => WP_REST_Server::READABLE,
39 'callback' => array( self::class, 'get' ),
40 'args' => self::request_parameters(),
41 'permission_callback' => '__return_true',
42 ),
43 )
44 );
45
46 \register_rest_route(
47 ACTIVITYPUB_REST_NAMESPACE,
48 '/users/(?P<user_id>[\w\-\.]+)/remote-follow',
49 array(
50 array(
51 'methods' => WP_REST_Server::READABLE,
52 'callback' => array( self::class, 'remote_follow_get' ),
53 'permission_callback' => '__return_true',
54 'args' => array(
55 'resource' => array(
56 'required' => true,
57 'sanitize_callback' => 'sanitize_text_field',
58 ),
59 ),
60 ),
61 )
62 );
63 }
64
65 /**
66 * Handle GET request
67 *
68 * @param WP_REST_Request $request
69 *
70 * @return WP_REST_Response
71 */
72 public static function get( $request ) {
73 $user_id = $request->get_param( 'user_id' );
74 $user = User_Collection::get_by_various( $user_id );
75
76 if ( is_wp_error( $user ) ) {
77 return $user;
78 }
79
80 // redirect to canonical URL if it is not an ActivityPub request
81 if ( ! is_activitypub_request() ) {
82 header( 'Location: ' . $user->get_canonical_url(), true, 301 );
83 exit;
84 }
85
86 /*
87 * Action triggerd prior to the ActivityPub profile being created and sent to the client
88 */
89 \do_action( 'activitypub_rest_users_pre' );
90
91 $json = $user->to_array();
92
93 $rest_response = new WP_REST_Response( $json, 200 );
94 $rest_response->header( 'Content-Type', 'application/activity+json; charset=' . get_option( 'blog_charset' ) );
95
96 return $rest_response;
97 }
98
99
100 /**
101 * Endpoint for remote follow UI/Block
102 *
103 * @param WP_REST_Request $request The request object.
104 *
105 * @return void|string The URL to the remote follow page
106 */
107 public static function remote_follow_get( WP_REST_Request $request ) {
108 $resource = $request->get_param( 'resource' );
109 $user_id = $request->get_param( 'user_id' );
110 $user = User_Collection::get_by_various( $user_id );
111
112 if ( is_wp_error( $user ) ) {
113 return $user;
114 }
115
116 $template = Webfinger::get_remote_follow_endpoint( $resource );
117
118 if ( is_wp_error( $template ) ) {
119 return $template;
120 }
121
122 $resource = $user->get_webfinger();
123 $url = str_replace( '{uri}', $resource, $template );
124
125 return new WP_REST_Response(
126 array( 'url' => $url ),
127 200
128 );
129 }
130
131 /**
132 * The supported parameters
133 *
134 * @return array list of parameters
135 */
136 public static function request_parameters() {
137 $params = array();
138
139 $params['page'] = array(
140 'type' => 'string',
141 );
142
143 $params['user_id'] = array(
144 'required' => true,
145 'type' => 'string',
146 );
147
148 return $params;
149 }
150 }
151