PluginProbe
ActivityPub / 4.0.2
ActivityPub v4.0.2
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-actors.php

class-actors.php in ActivityPub 4.0.2, at includes/rest/class-actors.php

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