wordpress-activitypub/includes/rest/class-comment.php
André Menrath 7370e97b5a
Add replies collection (#876)
* typo in phpdoc

* add first draft for adding replies collections to posts and comments

* refactoring

* Fix php CodeSniffer violations

* fix typo in php comment

* add draft for testing replies

* replies: test with own comment

* fix basic test for replies collection

* Restrict 'type' parameter for replies to 'post' or 'comment' in REST API

* some cleanups

* prefer ID over URL

* rename to `reply_id` to make clear that it is not the WordPress comment_id

* modularize retrieving of comment link via comment meta

* fix phpcs

* I think we should be more precise with this

and maybe there are other fallbacks coming

---------

Co-authored-by: Matthias Pfefferle <pfefferle@users.noreply.github.com>
2024-09-25 13:24:35 +02:00

91 lines
2.2 KiB
PHP

<?php
namespace Activitypub\Rest;
use WP_Error;
use WP_REST_Server;
use WP_REST_Request;
use WP_REST_Response;
use Activitypub\Comment as Comment_Utils;
use Activitypub\Webfinger as Webfinger_Utils;
/**
* ActivityPub Followers REST-Class
*
* @author Matthias Pfefferle
*
* @see https://www.w3.org/TR/activitypub/#followers
*/
class Comment {
/**
* Initialize the class, registering WordPress hooks
*/
public static function init() {
self::register_routes();
}
/**
* Register routes
*/
public static function register_routes() {
\register_rest_route(
ACTIVITYPUB_REST_NAMESPACE,
'/comments/(?P<comment_id>\d+)/remote-reply',
array(
array(
'methods' => WP_REST_Server::READABLE,
'callback' => array( self::class, 'remote_reply_get' ),
'permission_callback' => '__return_true',
'args' => array(
'resource' => array(
'required' => true,
'sanitize_callback' => 'sanitize_text_field',
),
),
),
)
);
}
/**
* Endpoint for remote follow UI/Block
*
* @param WP_REST_Request $request The request object.
*
* @return void|string The URL to the remote follow page
*/
public static function remote_reply_get( WP_REST_Request $request ) {
$resource = $request->get_param( 'resource' );
$comment_id = $request->get_param( 'comment_id' );
$comment = get_comment( $comment_id );
if ( ! $comment ) {
return new WP_Error( 'activitypub_comment_not_found', __( 'Comment not found', 'activitypub' ), array( 'status' => 404 ) );
}
$is_local = Comment_Utils::is_local( $comment );
if ( $is_local ) {
return new WP_Error( 'activitypub_local_only_comment', __( 'Comment is local only', 'activitypub' ), array( 'status' => 403 ) );
}
$template = Webfinger_Utils::get_remote_follow_endpoint( $resource );
if ( is_wp_error( $template ) ) {
return $template;
}
$resource = Comment_Utils::get_source_id( $comment_id );
if ( ! $resource ) {
$resource = Comment_Utils::generate_id( $comment );
}
$url = str_replace( '{uri}', $resource, $template );
return new WP_REST_Response(
array( 'url' => $url, 'template' => $template ),
200
);
}
}