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