PluginProbe
ActivityPub / 8.3.0
ActivityPub v8.3.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 / rest / class-webfinger-controller.php

class-webfinger-controller.php in ActivityPub 8.3.0, at includes/rest/class-webfinger-controller.php

173 lines 4.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WebFinger REST-Class file.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub\Rest;
9
10 /**
11 * ActivityPub WebFinger REST-Class.
12 *
13 * @author Matthias Pfefferle
14 *
15 * @see https://webfinger.net/
16 */
17 class Webfinger_Controller extends \WP_REST_Controller {
18 /**
19 * The namespace of this controller's route.
20 *
21 * @var string
22 */
23 protected $namespace = ACTIVITYPUB_REST_NAMESPACE;
24
25 /**
26 * The base of this controller's route.
27 *
28 * @var string
29 */
30 protected $rest_base = 'webfinger';
31
32 /**
33 * Register routes.
34 */
35 public function register_routes() {
36 \register_rest_route(
37 $this->namespace,
38 '/' . $this->rest_base,
39 array(
40 array(
41 'methods' => \WP_REST_Server::READABLE,
42 'callback' => array( $this, 'get_item' ),
43 'permission_callback' => '__return_true',
44 'args' => array(
45 'resource' => array(
46 'description' => 'The WebFinger resource.',
47 'type' => 'string',
48 'required' => true,
49 'pattern' => '^(acct:)|^(https?://)(.+)$',
50 ),
51 ),
52 ),
53 'schema' => array( $this, 'get_item_schema' ),
54 )
55 );
56 }
57
58 /**
59 * Retrieves the WebFinger profile.
60 *
61 * @param \WP_REST_Request $request The request object.
62 *
63 * @return \WP_REST_Response Response object.
64 */
65 public function get_item( $request ) {
66 /**
67 * Action triggered prior to the ActivityPub profile being created and sent to the client.
68 */
69 \do_action( 'activitypub_rest_webfinger_pre' );
70
71 $resource = $request->get_param( 'resource' );
72 $response = $this->get_profile( $resource );
73 $code = 200;
74
75 if ( \is_wp_error( $response ) ) {
76 $code = 400;
77 $error_data = $response->get_error_data();
78
79 if ( isset( $error_data['status'] ) ) {
80 $code = $error_data['status'];
81 }
82 }
83
84 return new \WP_REST_Response(
85 $response,
86 $code,
87 array(
88 'Content-Type' => 'application/jrd+json; charset=' . \get_option( 'blog_charset' ),
89 )
90 );
91 }
92
93 /**
94 * Get the WebFinger profile.
95 *
96 * @param string $webfinger The WebFinger resource.
97 *
98 * @return array|\WP_Error The WebFinger profile or WP_Error if not found.
99 */
100 public function get_profile( $webfinger ) {
101 /**
102 * Filter the WebFinger data.
103 *
104 * @param array $data The WebFinger data.
105 * @param string $webfinger The WebFinger resource.
106 */
107 return \apply_filters( 'webfinger_data', array(), $webfinger );
108 }
109
110 /**
111 * Retrieves the schema for the WebFinger endpoint.
112 *
113 * @return array Schema data.
114 */
115 public function get_item_schema() {
116 if ( $this->schema ) {
117 return $this->add_additional_fields_schema( $this->schema );
118 }
119
120 $this->schema = array(
121 '$schema' => 'http://json-schema.org/draft-04/schema#',
122 'title' => 'webfinger',
123 'type' => 'object',
124 'required' => array( 'subject', 'links' ),
125 'properties' => array(
126 'subject' => array(
127 'description' => 'The subject of this WebFinger record.',
128 'type' => 'string',
129 'format' => 'uri',
130 ),
131 'aliases' => array(
132 'description' => 'Alternative identifiers for the subject.',
133 'type' => 'array',
134 'items' => array(
135 'type' => 'string',
136 'format' => 'uri',
137 ),
138 ),
139 'links' => array(
140 'description' => 'Links associated with the subject.',
141 'type' => 'array',
142 'items' => array(
143 'type' => 'object',
144 'properties' => array(
145 'rel' => array(
146 'description' => 'The relation type of the link.',
147 'type' => 'string',
148 'required' => true,
149 ),
150 'type' => array(
151 'description' => 'The content type of the link.',
152 'type' => 'string',
153 ),
154 'href' => array(
155 'description' => 'The target URL of the link.',
156 'type' => 'string',
157 'format' => 'uri',
158 ),
159 'template' => array(
160 'description' => 'A URI template for the link.',
161 'type' => 'string',
162 'format' => 'uri',
163 ),
164 ),
165 ),
166 ),
167 ),
168 );
169
170 return $this->add_additional_fields_schema( $this->schema );
171 }
172 }
173