wordpress-activitypub/includes/rest/class-webfinger.php

109 lines
2.2 KiB
PHP
Raw Normal View History

2018-12-07 23:02:18 +00:00
<?php
namespace Activitypub\Rest;
2018-12-07 23:02:18 +00:00
2023-04-20 13:22:11 +00:00
use WP_Error;
use WP_REST_Response;
2019-02-24 12:01:28 +00:00
/**
* ActivityPub WebFinger REST-Class
*
* @author Matthias Pfefferle
*
* @see https://webfinger.net/
*/
class Webfinger {
2019-02-24 12:01:28 +00:00
/**
2023-07-11 12:26:07 +00:00
* Initialize the class, registering WordPress hooks.
*
* @return void
2019-02-24 12:01:28 +00:00
*/
public static function init() {
self::register_routes();
}
2019-02-28 18:31:55 +00:00
/**
2023-07-11 12:26:07 +00:00
* Register routes.
*
* @return void
*/
public static function register_routes() {
2019-09-27 08:12:59 +00:00
\register_rest_route(
ACTIVITYPUB_REST_NAMESPACE,
2022-01-27 12:09:11 +00:00
'/webfinger',
array(
array(
2020-09-18 14:36:09 +00:00
'methods' => \WP_REST_Server::READABLE,
2023-04-20 13:22:11 +00:00
'callback' => array( self::class, 'webfinger' ),
2020-09-18 14:36:09 +00:00
'args' => self::request_parameters(),
'permission_callback' => '__return_true',
),
)
);
}
/**
2023-07-11 12:26:07 +00:00
* WebFinger endpoint.
*
* @param WP_REST_Request $request The request object.
*
2023-07-11 12:26:07 +00:00
* @return WP_REST_Response The response object.
*/
public static function webfinger( $request ) {
2023-07-25 08:47:59 +00:00
/*
* Action triggerd prior to the ActivityPub profile being created and sent to the client
*/
\do_action( 'activitypub_rest_webfinger_pre' );
2024-06-09 12:48:31 +00:00
$code = 200;
$resource = $request->get_param( 'resource' );
2023-07-05 13:32:49 +00:00
$response = self::get_profile( $resource );
2024-06-09 12:48:31 +00:00
if ( \is_wp_error( $response ) ) {
$code = 400;
$error_data = $response->get_error_data();
if ( isset( $error_data['status'] ) ) {
$code = $error_data['status'];
}
}
2024-06-07 17:04:53 +00:00
2024-06-09 12:48:31 +00:00
return new WP_REST_Response(
$response,
$code,
array(
'Access-Control-Allow-Origin' => '*',
'Content-Type' => 'application/jrd+json; charset=' . get_option( 'blog_charset' ),
2024-06-09 12:48:31 +00:00
)
);
}
/**
* The supported parameters
*
* @return array list of parameters
*/
public static function request_parameters() {
$params = array();
$params['resource'] = array(
'required' => true,
'type' => 'string',
'pattern' => '^(acct:)|^(https?://)(.+)$',
);
return $params;
}
2023-07-11 12:26:07 +00:00
/**
* Get the WebFinger profile.
*
* @param string $resource the WebFinger resource.
*
* @return array the WebFinger profile.
*/
2024-08-02 08:31:42 +00:00
public static function get_profile( $resource ) { // phpcs:ignore
return apply_filters( 'webfinger_data', array(), $resource );
2023-07-05 13:32:49 +00:00
}
2018-12-07 23:02:18 +00:00
}