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