PluginProbe
ActivityPub / 7.0.0
ActivityPub v7.0.0
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
activitypub / includes / rest / class-comments-controller.php

class-comments-controller.php in ActivityPub 7.0.0, at includes/rest/class-comments-controller.php

157 lines 3.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Comments_Controller file.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub\Rest;
9
10 use Activitypub\Comment;
11 use Activitypub\Webfinger;
12
13 /**
14 * Comments_Controller class.
15 *
16 * @author Matthias Pfefferle
17 *
18 * @see https://www.w3.org/TR/activitypub/#followers
19 */
20 class Comments_Controller extends \WP_REST_Controller {
21 /**
22 * The namespace of this controller's route.
23 *
24 * @var string
25 */
26 protected $namespace = ACTIVITYPUB_REST_NAMESPACE;
27
28 /**
29 * The base of this controller's route.
30 *
31 * @var string
32 */
33 protected $rest_base = 'comments/(?P<comment_id>\d+)';
34
35 /**
36 * Register routes.
37 */
38 public function register_routes() {
39 \register_rest_route(
40 $this->namespace,
41 '/' . $this->rest_base . '/remote-reply',
42 array(
43 'args' => array(
44 'comment_id' => array(
45 'description' => 'The ID of the comment.',
46 'type' => 'integer',
47 'required' => true,
48 'validate_callback' => array( $this, 'validate_comment' ),
49 ),
50 ),
51 array(
52 'methods' => \WP_REST_Server::READABLE,
53 'callback' => array( $this, 'get_item' ),
54 'permission_callback' => '__return_true',
55 'args' => array(
56 'resource' => array(
57 'description' => 'The resource to reply to.',
58 'type' => 'string',
59 'required' => true,
60 ),
61 ),
62 ),
63 'schema' => array( $this, 'get_item_schema' ),
64 )
65 );
66 }
67
68 /**
69 * Validates if a comment can be replied to remotely.
70 *
71 * @param mixed $param The parameter to validate.
72 *
73 * @return true|\WP_Error True if the comment can be replied to, WP_Error otherwise.
74 */
75 public function validate_comment( $param ) {
76 $comment = \get_comment( $param );
77
78 if ( ! $comment ) {
79 return new \WP_Error( 'activitypub_comment_not_found', \__( 'Comment not found', 'activitypub' ), array( 'status' => 404 ) );
80 }
81
82 $is_local = Comment::is_local( $comment );
83
84 if ( $is_local ) {
85 return new \WP_Error( 'activitypub_local_only_comment', \__( 'Comment is local only', 'activitypub' ), array( 'status' => 403 ) );
86 }
87
88 return true;
89 }
90
91 /**
92 * Retrieves the remote reply URL for a comment.
93 *
94 * @param \WP_REST_Request $request The request object.
95 *
96 * @return \WP_REST_Response|\WP_Error Response object or WP_Error object.
97 */
98 public function get_item( $request ) {
99 $resource = $request->get_param( 'resource' );
100 $comment_id = $request->get_param( 'comment_id' );
101
102 $template = Webfinger::get_remote_follow_endpoint( $resource );
103
104 if ( \is_wp_error( $template ) ) {
105 return $template;
106 }
107
108 $resource = Comment::get_source_id( $comment_id );
109
110 if ( ! $resource ) {
111 $resource = Comment::generate_id( \get_comment( $comment_id ) );
112 }
113
114 $url = \str_replace( '{uri}', $resource, $template );
115
116 return \rest_ensure_response(
117 array(
118 'url' => $url,
119 'template' => $template,
120 )
121 );
122 }
123
124 /**
125 * Retrieves the schema for the remote reply endpoint.
126 *
127 * @return array Schema data.
128 */
129 public function get_item_schema() {
130 if ( $this->schema ) {
131 return $this->add_additional_fields_schema( $this->schema );
132 }
133
134 $this->schema = array(
135 '$schema' => 'http://json-schema.org/draft-04/schema#',
136 'title' => 'remote-reply',
137 'type' => 'object',
138 'properties' => array(
139 'url' => array(
140 'description' => 'The URL to the remote reply page.',
141 'type' => 'string',
142 'format' => 'uri',
143 'required' => true,
144 ),
145 'template' => array(
146 'description' => 'The template URL for remote replies.',
147 'type' => 'string',
148 'format' => 'uri',
149 'required' => true,
150 ),
151 ),
152 );
153
154 return $this->add_additional_fields_schema( $this->schema );
155 }
156 }
157