PluginProbe
ActivityPub / 4.1.1
ActivityPub v4.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-webfinger.php

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

117 lines 2.5 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 use WP_REST_Response;
11
12 /**
13 * ActivityPub WebFinger REST-Class.
14 *
15 * @author Matthias Pfefferle
16 *
17 * @see https://webfinger.net/
18 */
19 class Webfinger {
20 /**
21 * Initialize the class, registering WordPress hooks.
22 */
23 public static function init() {
24 self::register_routes();
25 }
26
27 /**
28 * Register routes.
29 */
30 public static function register_routes() {
31 \register_rest_route(
32 ACTIVITYPUB_REST_NAMESPACE,
33 '/webfinger',
34 array(
35 array(
36 'methods' => \WP_REST_Server::READABLE,
37 'callback' => array( self::class, 'webfinger' ),
38 'args' => self::request_parameters(),
39 'permission_callback' => '__return_true',
40 ),
41 )
42 );
43 }
44
45 /**
46 * WebFinger endpoint.
47 *
48 * @param \WP_REST_Request $request The request object.
49 *
50 * @return WP_REST_Response The response object.
51 */
52 public static function webfinger( $request ) {
53 /**
54 * Action triggered prior to the ActivityPub profile being created and sent to the client.
55 */
56 \do_action( 'activitypub_rest_webfinger_pre' );
57
58 $code = 200;
59
60 $resource = $request->get_param( 'resource' );
61 $response = self::get_profile( $resource );
62
63 if ( \is_wp_error( $response ) ) {
64 $code = 400;
65 $error_data = $response->get_error_data();
66
67 if ( isset( $error_data['status'] ) ) {
68 $code = $error_data['status'];
69 }
70 }
71
72 return new WP_REST_Response(
73 $response,
74 $code,
75 array(
76 'Access-Control-Allow-Origin' => '*',
77 'Content-Type' => 'application/jrd+json; charset=' . get_option( 'blog_charset' ),
78 )
79 );
80 }
81
82 /**
83 * The supported parameters.
84 *
85 * @return array list of parameters
86 */
87 public static function request_parameters() {
88 $params = array();
89
90 $params['resource'] = array(
91 'required' => true,
92 'type' => 'string',
93 'pattern' => '^(acct:)|^(https?://)(.+)$',
94 'sanitize_callback' => 'sanitize_text_field',
95 );
96
97 return $params;
98 }
99
100 /**
101 * Get the WebFinger profile.
102 *
103 * @param string $webfinger the WebFinger resource.
104 *
105 * @return array|\WP_Error The WebFinger profile or WP_Error if not found.
106 */
107 public static function get_profile( $webfinger ) {
108 /**
109 * Filter the WebFinger data.
110 *
111 * @param array $data The WebFinger data.
112 * @param string $webfinger The WebFinger resource.
113 */
114 return apply_filters( 'webfinger_data', array(), $webfinger );
115 }
116 }
117