PluginProbe
ActivityPub / 7.7.0
ActivityPub v7.7.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
← All changes | includes/rest/class-post-controller.php +18 -113 9.3.07.7.0 View file →
@@ -9,13 +9,9 @@
9 9
10 10 use Activitypub\Activity\Base_Object;
11 11 use Activitypub\Collection\Replies;
12 12 use Activitypub\Comment;
13 -use Activitypub\Sanitize;
14 -use Activitypub\Webfinger;
15 13
16 -use function Activitypub\get_post_id;
17 -use function Activitypub\get_reaction_author_name;
18 14 use function Activitypub\get_rest_url_by_path;
19 15
20 16 /**
21 17 * Class Post_Controller
@@ -35,9 +31,9 @@
35 31 * The base of this controller's route.
36 32 *
37 33 * @var string
38 34 */
39 - protected $rest_base = 'posts/(?P<id>[\d]+)';
35 + protected $rest_base = 'posts/(?P<id>[-]?\d+)';
40 36
41 37 /**
42 38 * Register routes.
43 39 */
@@ -47,12 +43,10 @@
47 43 '/' . $this->rest_base . '/reactions',
48 44 array(
49 45 'args' => array(
50 46 'id' => array(
51 - 'required' => true,
52 - 'type' => 'integer',
53 - 'minimum' => 1,
54 - 'validate_callback' => 'Activitypub\is_post_publicly_queryable',
47 + 'required' => true,
48 + 'type' => 'integer',
55 49 ),
56 50 ),
57 51 array(
58 52 'methods' => \WP_REST_Server::READABLE,
@@ -67,12 +61,10 @@
67 61 '/' . $this->rest_base . '/context',
68 62 array(
69 63 'args' => array(
70 64 'id' => array(
71 - 'required' => true,
72 - 'type' => 'integer',
73 - 'minimum' => 1,
74 - 'validate_callback' => 'Activitypub\is_post_publicly_queryable',
65 + 'required' => true,
66 + 'type' => 'integer',
75 67 ),
76 68 ),
77 69 array(
78 70 'methods' => \WP_REST_Server::READABLE,
@@ -80,44 +72,8 @@
80 72 'permission_callback' => '__return_true',
81 73 ),
82 74 )
83 75 );
84 -
85 - \register_rest_route(
86 - $this->namespace,
87 - '/' . $this->rest_base . '/remote-intent',
88 - array(
89 - 'args' => array(
90 - 'id' => array(
91 - 'description' => 'Unique identifier for the post.',
92 - 'type' => 'integer',
93 - 'minimum' => 1,
94 - 'required' => true,
95 - 'validate_callback' => 'Activitypub\is_post_publicly_queryable',
96 - ),
97 - ),
98 - array(
99 - 'methods' => \WP_REST_Server::READABLE,
100 - 'callback' => array( $this, 'get_remote_intent_template' ),
101 - 'permission_callback' => '__return_true',
102 - 'args' => array(
103 - 'resource' => array(
104 - 'description' => 'The Fediverse profile handle or URL.',
105 - 'type' => 'string',
106 - 'required' => true,
107 - 'sanitize_callback' => array( Sanitize::class, 'webfinger' ),
108 - ),
109 - 'intent' => array(
110 - 'description' => 'The intent type.',
111 - 'type' => 'string',
112 - 'default' => 'like',
113 - 'enum' => array( 'like', 'announce', 'create' ),
114 - 'sanitize_callback' => 'sanitize_text_field',
115 - ),
116 - ),
117 - ),
118 - )
119 - );
120 76 }
121 77
122 78 /**
123 79 * Get reactions for a post.
@@ -127,9 +83,14 @@
127 83 * @return \WP_REST_Response|\WP_Error Response object on success, or WP_Error object on failure.
128 84 */
129 85 public function get_reactions( $request ) {
130 86 $post_id = $request->get_param( 'id' );
87 + $post = \get_post( $post_id );
131 88
89 + if ( ! $post ) {
90 + return new \WP_Error( 'activitypub_post_not_found', 'Post not found', array( 'status' => 404 ) );
91 + }
92 +
132 93 $reactions = array();
133 94
134 95 foreach ( Comment::get_comment_types() as $type_object ) {
135 96 $comments = \get_comments(
@@ -160,18 +121,13 @@
160 121
161 122 $reactions[ $type_object['collection'] ] = array(
162 123 'label' => $label,
163 124 'items' => \array_map(
164 - static function ( $comment ) {
165 - /*
166 - * `esc_url_raw()` rejects `javascript:` and other unsafe
167 - * schemes without HTML-encoding ampersands (this is JSON,
168 - * not markup).
169 - */
125 + function ( $comment ) {
170 126 return array(
171 - 'name' => get_reaction_author_name( $comment ),
172 - 'url' => \esc_url_raw( $comment->comment_author_url ),
173 - 'avatar' => \esc_url_raw( \get_avatar_url( $comment ) ),
127 + 'name' => html_entity_decode( $comment->comment_author ),
128 + 'url' => $comment->comment_author_url,
129 + 'avatar' => \get_avatar_url( $comment ),
174 130 );
175 131 },
176 132 $comments
177 133 ),
@@ -188,16 +144,17 @@
188 144 *
189 145 * @return \WP_REST_Response|\WP_Error Response object on success, or WP_Error object on failure.
190 146 */
191 147 public function get_context( $request ) {
192 - $post_id = $request->get_param( 'id' );
148 + $post_id = $request->get_param( 'id' );
149 +
193 150 $collection = Replies::get_context_collection( $post_id );
194 151
195 152 if ( false === $collection ) {
196 - return new \WP_Error( 'activitypub_post_not_found', \__( 'Post not found', 'activitypub' ), array( 'status' => 404 ) );
153 + return new \WP_Error( 'activitypub_post_not_found', 'Post not found', array( 'status' => 404 ) );
197 154 }
198 155
199 - $response = \array_merge(
156 + $response = array_merge(
200 157 array(
201 158 '@context' => Base_Object::JSON_LD_CONTEXT,
202 159 'id' => get_rest_url_by_path( \sprintf( 'posts/%d/context', $post_id ) ),
203 160 ),
@@ -207,58 +164,6 @@
207 164 $response = \rest_ensure_response( $response );
208 165 $response->header( 'Content-Type', 'application/activity+json; charset=' . \get_option( 'blog_charset' ) );
209 166
210 167 return $response;
211 - }
212 -
213 - /**
214 - * Get the remote intent template for a post.
215 - *
216 - * @since 8.0.0
217 - *
218 - * @param \WP_REST_Request $request The request.
219 - *
220 - * @return \WP_REST_Response|\WP_Error Response object on success, or WP_Error object on failure.
221 - */
222 - public function get_remote_intent_template( $request ) {
223 - $post_id = $request->get_param( 'id' );
224 - $resource = $request->get_param( 'resource' );
225 - $intent = $request->get_param( 'intent' );
226 - $post = \get_post( $post_id );
227 -
228 - $template = Webfinger::get_intent_endpoint( $resource, $intent, true );
229 -
230 - if ( \is_wp_error( $template ) ) {
231 - return $template;
232 - }
233 -
234 - $id = get_post_id( $post_id );
235 -
236 - $url = \str_replace(
237 - array(
238 - '{object}',
239 - '{uri}',
240 - '{inReplyTo}',
241 - '{name}',
242 - '{target}',
243 - ),
244 - array(
245 - \rawurlencode( $id ),
246 - \rawurlencode( $id ),
247 - \rawurlencode( $id ),
248 - \rawurlencode( $post->post_title ),
249 - \rawurlencode( $resource ),
250 - ),
251 - $template
252 - );
253 -
254 - // Remove any other GET-Params with placeholders to avoid confusion.
255 - $url = \preg_replace( '/([&?][^=]+=\{[^}]+\})/', '', $url );
256 -
257 - return \rest_ensure_response(
258 - array(
259 - 'url' => $url,
260 - 'template' => $template,
261 - )
262 - );
263 168 }
264 169 }