| 1 |
<?php |
| 2 |
/** |
| 3 |
* Comment REST-Class file. |
| 4 |
* |
| 5 |
* @package Activitypub |
| 6 |
*/ |
| 7 |
|
| 8 |
namespace Activitypub\Rest; |
| 9 |
|
| 10 |
use WP_Error; |
| 11 |
use WP_REST_Server; |
| 12 |
use WP_REST_Request; |
| 13 |
use WP_REST_Response; |
| 14 |
use Activitypub\Comment as Comment_Utils; |
| 15 |
use Activitypub\Webfinger as Webfinger_Utils; |
| 16 |
|
| 17 |
/** |
| 18 |
* ActivityPub Followers REST-Class. |
| 19 |
* |
| 20 |
* @author Matthias Pfefferle |
| 21 |
* |
| 22 |
* @see https://www.w3.org/TR/activitypub/#followers |
| 23 |
*/ |
| 24 |
class Comment { |
| 25 |
/** |
| 26 |
* Initialize the class, registering WordPress hooks. |
| 27 |
*/ |
| 28 |
public static function init() { |
| 29 |
self::register_routes(); |
| 30 |
} |
| 31 |
|
| 32 |
/** |
| 33 |
* Register routes. |
| 34 |
*/ |
| 35 |
public static function register_routes() { |
| 36 |
\register_rest_route( |
| 37 |
ACTIVITYPUB_REST_NAMESPACE, |
| 38 |
'/comments/(?P<comment_id>\d+)/remote-reply', |
| 39 |
array( |
| 40 |
array( |
| 41 |
'methods' => WP_REST_Server::READABLE, |
| 42 |
'callback' => array( self::class, 'remote_reply_get' ), |
| 43 |
'permission_callback' => '__return_true', |
| 44 |
'args' => array( |
| 45 |
'resource' => array( |
| 46 |
'required' => true, |
| 47 |
'sanitize_callback' => 'sanitize_text_field', |
| 48 |
), |
| 49 |
), |
| 50 |
), |
| 51 |
) |
| 52 |
); |
| 53 |
} |
| 54 |
|
| 55 |
/** |
| 56 |
* Endpoint for remote follow UI/Block. |
| 57 |
* |
| 58 |
* @param WP_REST_Request $request The request object. |
| 59 |
* |
| 60 |
* @return array|string|WP_Error|WP_REST_Response The URL to the remote follow page or an error. |
| 61 |
*/ |
| 62 |
public static function remote_reply_get( WP_REST_Request $request ) { |
| 63 |
$resource = $request->get_param( 'resource' ); |
| 64 |
$comment_id = $request->get_param( 'comment_id' ); |
| 65 |
|
| 66 |
$comment = get_comment( $comment_id ); |
| 67 |
|
| 68 |
if ( ! $comment ) { |
| 69 |
return new WP_Error( 'activitypub_comment_not_found', __( 'Comment not found', 'activitypub' ), array( 'status' => 404 ) ); |
| 70 |
} |
| 71 |
|
| 72 |
$is_local = Comment_Utils::is_local( $comment ); |
| 73 |
|
| 74 |
if ( $is_local ) { |
| 75 |
return new WP_Error( 'activitypub_local_only_comment', __( 'Comment is local only', 'activitypub' ), array( 'status' => 403 ) ); |
| 76 |
} |
| 77 |
|
| 78 |
$template = Webfinger_Utils::get_remote_follow_endpoint( $resource ); |
| 79 |
|
| 80 |
if ( is_wp_error( $template ) ) { |
| 81 |
return $template; |
| 82 |
} |
| 83 |
|
| 84 |
$resource = Comment_Utils::get_source_id( $comment_id ); |
| 85 |
|
| 86 |
if ( ! $resource ) { |
| 87 |
$resource = Comment_Utils::generate_id( $comment ); |
| 88 |
} |
| 89 |
|
| 90 |
$url = str_replace( '{uri}', $resource, $template ); |
| 91 |
|
| 92 |
return new WP_REST_Response( |
| 93 |
array( |
| 94 |
'url' => $url, |
| 95 |
'template' => $template, |
| 96 |
), |
| 97 |
200 |
| 98 |
); |
| 99 |
} |
| 100 |
} |
| 101 |
|