2023-05-24 16:32:00 +02:00
|
|
|
<?php
|
|
|
|
namespace Activitypub\Rest;
|
|
|
|
|
|
|
|
use WP_Error;
|
|
|
|
use WP_REST_Server;
|
|
|
|
use WP_REST_Response;
|
2023-08-29 12:50:17 +02:00
|
|
|
use Activitypub\Validator\Query;
|
2023-07-03 19:52:54 +02:00
|
|
|
use Activitypub\Activity\Activity;
|
2023-07-03 17:59:42 +02:00
|
|
|
use Activitypub\Collection\Users as User_Collection;
|
2023-05-24 16:32:00 +02:00
|
|
|
|
2023-05-30 10:22:01 +02:00
|
|
|
use function Activitypub\is_activitypub_request;
|
|
|
|
|
2023-05-24 16:32:00 +02:00
|
|
|
/**
|
|
|
|
* ActivityPub Followers REST-Class
|
|
|
|
*
|
|
|
|
* @author Matthias Pfefferle
|
|
|
|
*
|
|
|
|
* @see https://www.w3.org/TR/activitypub/#followers
|
|
|
|
*/
|
2023-05-24 17:27:46 +02:00
|
|
|
class Users {
|
2023-05-24 16:32:00 +02:00
|
|
|
/**
|
|
|
|
* Initialize the class, registering WordPress hooks
|
|
|
|
*/
|
|
|
|
public static function init() {
|
|
|
|
\add_action( 'rest_api_init', array( self::class, 'register_routes' ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register routes
|
|
|
|
*/
|
|
|
|
public static function register_routes() {
|
|
|
|
\register_rest_route(
|
|
|
|
ACTIVITYPUB_REST_NAMESPACE,
|
2023-05-26 16:08:08 +02:00
|
|
|
'/users/(?P<user_id>[\w\-\.]+)',
|
2023-05-24 16:32:00 +02:00
|
|
|
array(
|
|
|
|
array(
|
|
|
|
'methods' => WP_REST_Server::READABLE,
|
|
|
|
'callback' => array( self::class, 'get' ),
|
2023-08-29 12:50:17 +02:00
|
|
|
'args' => Query::get_default_args(),
|
2023-05-24 16:32:00 +02:00
|
|
|
'permission_callback' => '__return_true',
|
|
|
|
),
|
|
|
|
)
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Handle GET request
|
|
|
|
*
|
|
|
|
* @param WP_REST_Request $request
|
|
|
|
*
|
|
|
|
* @return WP_REST_Response
|
|
|
|
*/
|
|
|
|
public static function get( $request ) {
|
|
|
|
$user_id = $request->get_param( 'user_id' );
|
2023-07-03 11:20:44 +02:00
|
|
|
$user = User_Collection::get_by_various( $user_id );
|
2023-05-24 16:32:00 +02:00
|
|
|
|
|
|
|
if ( is_wp_error( $user ) ) {
|
|
|
|
return $user;
|
|
|
|
}
|
|
|
|
|
2023-05-30 10:22:01 +02:00
|
|
|
// redirect to canonical URL if it is not an ActivityPub request
|
|
|
|
if ( ! is_activitypub_request() ) {
|
2023-05-30 10:29:23 +02:00
|
|
|
header( 'Location: ' . $user->get_canonical_url(), true, 301 );
|
2023-05-30 10:22:01 +02:00
|
|
|
exit;
|
|
|
|
}
|
|
|
|
|
2023-05-24 16:32:00 +02:00
|
|
|
/*
|
|
|
|
* Action triggerd prior to the ActivityPub profile being created and sent to the client
|
|
|
|
*/
|
2023-07-25 10:47:59 +02:00
|
|
|
\do_action( 'activitypub_rest_users_pre' );
|
2023-05-24 16:32:00 +02:00
|
|
|
|
2023-06-28 18:02:14 +02:00
|
|
|
$user->set_context(
|
2023-07-03 11:20:44 +02:00
|
|
|
Activity::CONTEXT
|
2023-06-28 18:02:14 +02:00
|
|
|
);
|
|
|
|
|
2023-05-24 16:32:00 +02:00
|
|
|
$json = $user->to_array();
|
|
|
|
|
|
|
|
$response = new WP_REST_Response( $json, 200 );
|
|
|
|
$response->header( 'Content-Type', 'application/activity+json' );
|
|
|
|
|
|
|
|
return $response;
|
|
|
|
}
|
|
|
|
}
|