PluginProbe
ActivityPub / 3.2.5
ActivityPub v3.2.5
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/transformer/class-post.php +594 -894 9.2.03.2.5 View file →
@@ -1,32 +1,23 @@
1 1 <?php
2 -/**
3 - * WordPress Post Transformer Class file.
4 - *
5 - * @package Activitypub
6 - */
7 -
8 2 namespace Activitypub\Transformer;
9 3
10 -use Activitypub\Activity\Base_Object;
11 -use Activitypub\Collection\Actors;
12 -use Activitypub\Collection\Interactions;
13 -use Activitypub\Collection\Replies;
4 +use WP_Post;
5 +use Activitypub\Shortcodes;
14 6 use Activitypub\Model\Blog;
15 -use Activitypub\Shortcodes;
7 +use Activitypub\Collection\Users;
8 +use Activitypub\Transformer\Base;
16 9
17 10 use function Activitypub\esc_hashtag;
18 -use function Activitypub\generate_post_summary;
19 -use function Activitypub\get_content_visibility;
20 -use function Activitypub\get_content_warning;
11 +use function Activitypub\is_single_user;
21 12 use function Activitypub\get_enclosures;
13 +use function Activitypub\site_supports_blocks;
22 14 use function Activitypub\get_rest_url_by_path;
23 -use function Activitypub\is_post_publicly_queryable;
24 -use function Activitypub\is_single_user;
25 -use function Activitypub\site_supports_blocks;
15 +use function Activitypub\is_user_type_disabled;
16 +use function Activitypub\generate_post_summary;
26 17
27 18 /**
28 - * WordPress Post Transformer.
19 + * WordPress Post Transformer
29 20 *
30 21 * The Post Transformer is responsible for transforming a WP_Post object into different other
31 22 * Object-Types.
32 23 *
@@ -37,162 +28,82 @@
37 28 class Post extends Base {
38 29 /**
39 30 * The User as Actor Object.
40 31 *
41 - * @var \Activitypub\Activity\Actor
32 + * @var Activitypub\Activity\Actor
42 33 */
43 34 private $actor_object = null;
44 35
45 36 /**
46 - * The content.
37 + * Returns the ID of the WordPress Post.
47 38 *
48 - * @var string|false False indicates not yet computed.
39 + * @return int The ID of the WordPress Post
49 40 */
50 - private $content = false;
41 + public function get_wp_user_id() {
42 + return $this->wp_object->post_author;
43 + }
51 44
52 45 /**
53 - * The summary.
46 + * Change the User-ID of the WordPress Post.
54 47 *
55 - * @var string|null|false False indicates not yet computed.
48 + * @return int The User-ID of the WordPress Post
56 49 */
57 - private $summary = false;
50 + public function change_wp_user_id( $user_id ) {
51 + $this->wp_object->post_author = $user_id;
58 52
59 - /**
60 - * The tags.
61 - *
62 - * @var array|false False indicates not yet computed.
63 - */
64 - private $tags = false;
53 + return $this;
54 + }
65 55
66 56 /**
67 - * The attachment.
57 + * Transforms the WP_Post object to an ActivityPub Object
68 58 *
69 - * @var array|false False indicates not yet computed.
70 - */
71 - private $attachment = false;
72 -
73 - /**
74 - * The mentions.
59 + * @see \Activitypub\Activity\Base_Object
75 60 *
76 - * @var array|false False indicates not yet computed.
77 - */
78 - private $mentions = false;
79 -
80 - /**
81 - * The in_reply_to.
82 - *
83 - * @var string|array|null|false False indicates not yet computed.
84 - */
85 - private $in_reply_to = false;
86 -
87 - /**
88 - * Transforms the WP_Post object to an ActivityPub Object
89 - *
90 61 * @return \Activitypub\Activity\Base_Object The ActivityPub Object
91 62 */
92 63 public function to_object() {
93 - /*
94 - * A redacted (password-protected or non-public) post is, from the
95 - * Fediverse's perspective, gone — the soft-delete path that reaches here
96 - * emits a Delete. Represent it as a Tombstone: content-free by type, so
97 - * no body-derived field (content, summary, tags, @-mentions, location,
98 - * attachments…) can ever leak, even one added to the transformer later.
99 - *
100 - * Address the teardown to the public collection. A post only reaches the
101 - * soft-delete path after being federated, and only public / quiet-public
102 - * posts federate (private and local ones never do), so the original
103 - * audience was always public — broadcasting the Delete tears the copy
104 - * down everywhere it may exist. Private/direct activities are deleted via
105 - * their own outbox path and keep their original (non-public) audience, so
106 - * they are not affected by this.
107 - */
108 - if ( $this->is_redacted() ) {
109 - $tombstone = $this->to_tombstone();
110 - $tombstone->set_to( array( 'https://www.w3.org/ns/activitystreams#Public' ) );
111 -
112 - return $tombstone;
113 - }
114 -
115 - $post = $this->item;
64 + $post = $this->wp_object;
116 65 $object = parent::to_object();
117 66
118 - $content_warning = get_content_warning( $post );
119 - if ( ! empty( $content_warning ) ) {
120 - $object->set_sensitive( true );
121 - $object->set_summary( $content_warning );
122 - $object->set_summary_map( null );
123 - $object->set_dcterms( array( 'subject' => $content_warning ) );
124 - }
67 + $published = \strtotime( $post->post_date_gmt );
125 68
126 - return $object;
127 - }
69 + $object->set_published( \gmdate( 'Y-m-d\TH:i:s\Z', $published ) );
128 70
129 - /**
130 - * Returns a Tombstone object for the post.
131 - *
132 - * @return Base_Object The Tombstone object.
133 - */
134 - public function to_tombstone() {
135 - $object = new Base_Object();
136 - $object->set_type( 'Tombstone' );
137 - $object->set_id( $this->get_id() );
138 - // Preserve the permalink so the tombstone registry can resolve a request
139 - // to it, even on sites whose ActivityPub ID is the post-ID URL (?p=123).
140 - $object->set_url( $this->get_url() );
141 - $object->set_former_type( $this->get_type() );
142 - $object->set_published( $this->get_published() );
143 - $object->set_updated( $this->get_updated() );
71 + $updated = \strtotime( $post->post_modified_gmt );
144 72
145 - $deleted_at = \get_post_meta( $this->item->ID, 'activitypub_deleted_at', true );
146 - if ( $deleted_at ) {
147 - $object->set_deleted( \gmdate( ACTIVITYPUB_DATE_TIME_RFC3339, $deleted_at ) );
73 + if ( $updated > $published ) {
74 + $object->set_updated( \gmdate( 'Y-m-d\TH:i:s\Z', $updated ) );
148 75 }
149 76
150 - return $object;
151 - }
77 + $object->set_content_map(
78 + array(
79 + $this->get_locale() => $this->get_content(),
80 + )
81 + );
152 82
153 - /**
154 - * Get the content visibility.
155 - *
156 - * @return string The content visibility.
157 - */
158 - public function get_content_visibility() {
159 - if ( ! $this->content_visibility ) {
160 - return get_content_visibility( $this->item );
161 - }
83 + $object->set_to(
84 + array(
85 + 'https://www.w3.org/ns/activitystreams#Public',
86 + $this->get_actor_object()->get_followers(),
87 + )
88 + );
162 89
163 - return $this->content_visibility;
90 + return $object;
164 91 }
165 92
166 93 /**
167 - * Get the Interaction Policy.
168 - *
169 - * @see https://docs.gotosocial.org/en/latest/federation/interaction_policy/
170 - *
171 - * @return array The interaction policy.
172 - */
173 - public function get_interaction_policy() {
174 - return array(
175 - 'canAnnounce' => $this->get_public_interaction_policy(),
176 - 'canLike' => $this->get_public_interaction_policy(),
177 - 'canQuote' => $this->get_quote_policy(),
178 - 'canReply' => $this->get_public_interaction_policy(),
179 - );
180 - }
181 -
182 - /**
183 94 * Returns the User-Object of the Author of the Post.
184 95 *
185 96 * If `single_user` mode is enabled, the Blog-User is returned.
186 97 *
187 - * @return \Activitypub\Activity\Actor The User-Object.
98 + * @return Activitypub\Activity\Actor The User-Object.
188 99 */
189 - public function get_actor_object() {
100 + protected function get_actor_object() {
190 101 if ( $this->actor_object ) {
191 102 return $this->actor_object;
192 103 }
193 104
194 - $blog_user = new Blog();
105 + $blog_user = new Blog();
195 106 $this->actor_object = $blog_user;
196 107
197 108 if ( is_single_user() ) {
198 109 return $blog_user;
@@ -197,11 +108,11 @@
197 108 if ( is_single_user() ) {
198 109 return $blog_user;
199 110 }
200 111
201 - $user = Actors::get_by_id( $this->item->post_author );
112 + $user = Users::get_by_id( $this->wp_object->post_author );
202 113
203 - if ( $user && ! \is_wp_error( $user ) ) {
114 + if ( $user && ! is_wp_error( $user ) ) {
204 115 $this->actor_object = $user;
205 116 return $user;
206 117 }
207 118
@@ -210,33 +121,11 @@
210 121
211 122 /**
212 123 * Returns the ID of the Post.
213 124 *
214 - * Posts past `activitypub_last_post_with_permalink_as_id` use the post-ID URL
215 - * as their canonical ActivityPub ID — stable across slug changes. Posts at
216 - * or below the threshold are *legacy* and use their permalink as the ID,
217 - * which means a slug change effectively renames the federated object.
218 - *
219 - * Known limitation: a legacy post whose slug changes in the same save as
220 - * a soft-delete transition (e.g. publish → draft + new post_name) will
221 - * emit a Delete targeting the new permalink, while remote servers cached
222 - * the original. The trash case mitigates this via the `wp_trash_post`
223 - * hook caching the pre-transition URL in `_activitypub_canonical_url`,
224 - * but draft / pending / private / password-applied transitions do not.
225 - * If you maintain a site that pre-dates the ID migration, avoid editing
226 - * the slug in the same save as the visibility change.
227 - *
228 125 * @return string The Posts ID.
229 126 */
230 127 public function get_id() {
231 - $last_legacy_id = (int) \get_option( 'activitypub_last_post_with_permalink_as_id', 0 );
232 - $post_id = (int) $this->item->ID;
233 -
234 - if ( $post_id > $last_legacy_id ) {
235 - // Generate URI based on post ID.
236 - return \add_query_arg( 'p', $post_id, \home_url( '/' ) );
237 - }
238 -
239 128 return $this->get_url();
240 129 }
241 130
242 131 /**
@@ -244,16 +133,16 @@
244 133 *
245 134 * @return string The Posts URL.
246 135 */
247 136 public function get_url() {
248 - $post = $this->item;
137 + $post = $this->wp_object;
249 138
250 139 switch ( \get_post_status( $post ) ) {
251 140 case 'trash':
252 - $permalink = \get_post_meta( $post->ID, '_activitypub_canonical_url', true );
141 + $permalink = \get_post_meta( $post->ID, 'activitypub_canonical_url', true );
253 142 break;
254 143 case 'draft':
255 - // Get_sample_permalink is in wp-admin, not always loaded.
144 + // get_sample_permalink is in wp-admin, not always loaded
256 145 if ( ! \function_exists( '\get_sample_permalink' ) ) {
257 146 require_once ABSPATH . 'wp-admin/includes/post.php';
258 147 }
259 148 $sample = \get_sample_permalink( $post->ID );
@@ -263,9 +152,9 @@
263 152 $permalink = \get_permalink( $post );
264 153 break;
265 154 }
266 155
267 - return \esc_url_raw( $permalink );
156 + return \esc_url( $permalink );
268 157 }
269 158
270 159 /**
271 160 * Returns the User-URL of the Author of the Post.
@@ -274,818 +163,680 @@
274 163 *
275 164 * @return string The User-URL.
276 165 */
277 166 protected function get_attributed_to() {
278 - return $this->get_actor_object()->get_id();
167 + return $this->get_actor_object()->get_url();
279 168 }
280 169
281 170 /**
282 - * Returns the featured image as `Image`.
283 - *
284 - * @return array|null The Image or null if no image is available.
285 - */
286 - protected function get_image() {
287 - $post_id = $this->item->ID;
288 -
289 - // List post thumbnail first if this post has one.
290 - if (
291 - ! \function_exists( 'has_post_thumbnail' ) ||
292 - ! \has_post_thumbnail( $post_id )
293 - ) {
294 - return null;
295 - }
296 -
297 - $id = \get_post_thumbnail_id( $post_id );
298 - $image_size = 'large';
299 -
300 - /**
301 - * Filter the image URL returned for each post.
302 - *
303 - * @param array|false $thumbnail The image URL, or false if no image is available.
304 - * @param int $id The attachment ID.
305 - * @param string $image_size The image size to retrieve. Set to 'large' by default.
306 - */
307 - $thumbnail = \apply_filters(
308 - 'activitypub_get_image',
309 - $this->get_attachment_image_src( $id, $image_size ),
310 - $id,
311 - $image_size
312 - );
313 -
314 - if ( ! $thumbnail ) {
315 - return null;
316 - }
317 -
318 - $mime_type = \get_post_mime_type( $id );
319 -
320 - $image = array(
321 - 'type' => 'Image',
322 - 'url' => \esc_url_raw( $thumbnail[0] ),
323 - 'mediaType' => \esc_attr( $mime_type ),
324 - );
325 -
326 - $alt = \get_post_meta( $id, '_wp_attachment_image_alt', true );
327 - if ( $alt ) {
328 - $image['name'] = \html_entity_decode( \wp_strip_all_tags( $alt ), ENT_QUOTES, 'UTF-8' );
329 - }
330 -
331 - return $image;
332 - }
333 -
334 - /**
335 - * Returns an Icon, based on the Featured Image with a fallback to the site-icon.
336 - *
337 - * @return array|null The Icon or null if no icon is available.
338 - */
339 - protected function get_icon() {
340 - $post_id = $this->item->ID;
341 -
342 - // List post thumbnail first if this post has one.
343 - if ( \has_post_thumbnail( $post_id ) ) {
344 - $id = \get_post_thumbnail_id( $post_id );
345 - } else {
346 - // Try site_logo, falling back to site_icon, first.
347 - $id = \get_option( 'site_icon' );
348 - }
349 -
350 - if ( ! $id ) {
351 - return null;
352 - }
353 -
354 - $image_size = 'thumbnail';
355 -
356 - /**
357 - * Filter the image URL returned for each post.
358 - *
359 - * @param array|false $thumbnail The image URL, or false if no image is available.
360 - * @param int $id The attachment ID.
361 - * @param string $image_size The image size to retrieve. Set to 'large' by default.
362 - */
363 - $thumbnail = \apply_filters(
364 - 'activitypub_get_image',
365 - $this->get_attachment_image_src( $id, $image_size ),
366 - $id,
367 - $image_size
368 - );
369 -
370 - if ( ! $thumbnail ) {
371 - return null;
372 - }
373 -
374 - $mime_type = \get_post_mime_type( $id );
375 -
376 - $image = array(
377 - 'type' => 'Image',
378 - 'url' => \esc_url_raw( $thumbnail[0] ),
379 - 'mediaType' => \esc_attr( $mime_type ),
380 - );
381 -
382 - $alt = \get_post_meta( $id, '_wp_attachment_image_alt', true );
383 - if ( $alt ) {
384 - $image['name'] = \html_entity_decode( \wp_strip_all_tags( $alt ), ENT_QUOTES, 'UTF-8' );
385 - }
386 -
387 - return $image;
388 - }
389 -
390 - /**
391 171 * Generates all Media Attachments for a Post.
392 172 *
393 173 * @return array The Attachments.
394 174 */
395 175 protected function get_attachment() {
396 - if ( false !== $this->attachment ) {
397 - return $this->attachment;
176 + // Remove attachments from drafts.
177 + if ( 'draft' === \get_post_status( $this->wp_object ) ) {
178 + return array();
398 179 }
399 180
400 - $max_media = \get_post_meta( $this->item->ID, 'activitypub_max_image_attachments', true );
181 + // Once upon a time we only supported images, but we now support audio/video as well.
182 + // We maintain the image-centric naming for backwards compatibility.
183 + $max_media = \intval(
184 + \apply_filters(
185 + 'activitypub_max_image_attachments',
186 + \get_option( 'activitypub_max_image_attachments', ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS )
187 + )
188 + );
401 189
402 - if ( ! \is_numeric( $max_media ) ) {
403 - $max_media = \get_option( 'activitypub_max_image_attachments', ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS );
404 - }
405 -
406 - /**
407 - * Filters the maximum number of media attachments allowed in a post.
408 - *
409 - * Despite the name suggesting only images, this filter controls the maximum number
410 - * of all media attachments (images, audio, and video) that can be included in an
411 - * ActivityPub post. The name is maintained for backwards compatibility.
412 - *
413 - * @param int $max_media Maximum number of media attachments. Default ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS.
414 - */
415 - $max_media = (int) \apply_filters( 'activitypub_max_image_attachments', $max_media );
416 -
417 - if ( 0 === $max_media ) {
418 - $this->attachment = array();
419 -
420 - return $this->attachment;
421 - }
422 -
423 190 $media = array(
424 - 'image' => array(),
425 191 'audio' => array(),
426 192 'video' => array(),
193 + 'image' => array(),
427 194 );
428 - $id = $this->item->ID;
195 + $id = $this->wp_object->ID;
429 196
430 - // List post thumbnail first if this post has one.
431 - if ( \has_post_thumbnail( $id ) ) {
197 + // list post thumbnail first if this post has one
198 + if ( \function_exists( 'has_post_thumbnail' ) && \has_post_thumbnail( $id ) ) {
432 199 $media['image'][] = array( 'id' => \get_post_thumbnail_id( $id ) );
433 200 }
434 201
435 202 $media = $this->get_enclosures( $media );
436 203
437 - if ( site_supports_blocks() && \has_blocks( $this->item->post_content ) ) {
204 + if ( site_supports_blocks() && \has_blocks( $this->wp_object->post_content ) ) {
438 205 $media = $this->get_block_attachments( $media, $max_media );
439 206 } else {
440 - $media = $this->parse_html_images( $media, $max_media, $this->item->post_content );
207 + $media = $this->get_classic_editor_images( $media, $max_media );
441 208 }
442 209
443 - $media = $this->filter_media_by_object_type( $media, \get_post_format( $this->item ), $this->item );
210 + $media = self::filter_media_by_object_type( $media, \get_post_format( $this->wp_object ), $this->wp_object );
211 + $unique_ids = \array_unique( \array_column( $media, 'id' ) );
212 + $media = \array_intersect_key( $media, $unique_ids );
213 + $media = \array_slice( $media, 0, $max_media );
444 214
445 215 /**
446 216 * Filter the attachment IDs for a post.
447 217 *
448 - * @param array $media The media array grouped by type.
449 - * @param \WP_Post $item The post object.
218 + * @param array $media The media array grouped by type.
219 + * @param WP_Post $this->wp_object The post object.
450 220 *
451 221 * @return array The filtered attachment IDs.
452 222 */
453 - $media = \apply_filters( 'activitypub_attachment_ids', $media, $this->item );
223 + $media = \apply_filters( 'activitypub_attachment_ids', $media, $this->wp_object );
454 224
455 - // Deduplicate and limit after filter to ensure plugins adding attachments don't cause duplicates.
456 - $media = $this->filter_unique_attachments( $media );
457 - $media = \array_slice( $media, 0, $max_media );
225 + $attchments = \array_filter( \array_map( array( self::class, 'wp_attachment_to_activity_attachment' ), $media ) );
458 226
459 - $attachments = \array_filter( \array_map( array( $this, 'transform_attachment' ), $media ) );
460 -
461 227 /**
462 228 * Filter the attachments for a post.
463 229 *
464 - * @param array $attachments The attachments.
465 - * @param \WP_Post $item The post object.
230 + * @param array $attchments The attachments.
231 + * @param WP_Post $this->wp_object The post object.
466 232 *
467 233 * @return array The filtered attachments.
468 234 */
469 - $this->attachment = \apply_filters( 'activitypub_attachments', $attachments, $this->item );
470 -
471 - return $this->attachment;
235 + return \apply_filters( 'activitypub_attachments', $attchments, $this->wp_object );
472 236 }
473 237
474 238 /**
475 - * Returns the ActivityStreams 2.0 Object-Type for a Post based on the
476 - * settings and the Post-Type.
239 + * Get enclosures for a post.
477 240 *
478 - * @see https://www.w3.org/TR/activitystreams-vocabulary/#activity-types
241 + * @param array $media The media array grouped by type.
479 242 *
480 - * @return string The Object-Type.
243 + * @return array The media array extended with enclosures.
481 244 */
482 - protected function get_type() {
483 - $post_format_setting = \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE );
245 + public function get_enclosures( $media ) {
246 + $enclosures = get_enclosures( $this->wp_object->ID );
484 247
485 - if ( 'wordpress-post-format' !== $post_format_setting ) {
486 - $object_type = \ucfirst( $post_format_setting );
487 - } elseif ( ! \post_type_supports( $this->item->post_type, 'title' ) || ! $this->item->post_title ) {
488 - $object_type = 'Note';
489 - } elseif ( 'page' === \get_post_type( $this->item ) ) {
490 - $object_type = 'Page';
491 - } elseif ( ! \get_post_format( $this->item ) ) {
492 - $object_type = 'Article';
493 - } else {
494 - $object_type = 'Note';
248 + if ( ! $enclosures ) {
249 + return $media;
495 250 }
496 251
497 - /**
498 - * Filters the ActivityPub object type for a post.
499 - *
500 - * Allows downstream consumers to override the discriminator that
501 - * decides whether a post federates as Note, Article, or Page.
502 - * The filtered value propagates to all internal callers of
503 - * get_type(), including former_type/tombstone handling,
504 - * summary and title decisions, the content template, and the
505 - * preview guard, not only the wire-format type property.
506 - *
507 - * @since 8.1.1
508 - *
509 - * @param string $object_type The computed ActivityPub object type.
510 - * @param \WP_Post $post The WordPress post being transformed.
511 - */
512 - return \apply_filters( 'activitypub_post_object_type', $object_type, $this->item );
252 + foreach ( $enclosures as $enclosure ) {
253 + // check if URL is an attachment
254 + $attachment_id = \attachment_url_to_postid( $enclosure['url'] );
255 + if ( $attachment_id ) {
256 + $enclosure['id'] = $attachment_id;
257 + $enclosure['url'] = \wp_get_attachment_url( $attachment_id );
258 + $enclosure['mediaType'] = \get_post_mime_type( $attachment_id );
259 + }
260 +
261 + $mime_type = $enclosure['mediaType'];
262 + $mime_type_parts = \explode( '/', $mime_type );
263 +
264 + switch ( $mime_type_parts[0] ) {
265 + case 'image':
266 + $media['image'][] = $enclosure;
267 + break;
268 + case 'audio':
269 + $media['audio'][] = $enclosure;
270 + break;
271 + case 'video':
272 + $media['video'][] = $enclosure;
273 + break;
274 + }
275 + }
276 +
277 + return $media;
513 278 }
514 279
515 280 /**
516 - * Returns the Audience for the Post.
281 + * Get media attachments from blocks. They will be formatted as ActivityPub attachments, not as WP attachments.
517 282 *
518 - * @return string|null The audience.
283 + * @param array $media The media array grouped by type.
284 + * @param int $max_media The maximum number of attachments to return.
285 + *
286 + * @return array The attachments.
519 287 */
520 - public function get_audience() {
521 - $actor_mode = \get_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_MODE );
288 + protected function get_block_attachments( $media, $max_media ) {
289 + // max media can't be negative or zero
290 + if ( $max_media <= 0 ) {
291 + return array();
292 + }
522 293
523 - if ( ACTIVITYPUB_ACTOR_AND_BLOG_MODE === $actor_mode ) {
524 - $blog = new Blog();
525 - return $blog->get_id();
526 - }
294 + $blocks = \parse_blocks( $this->wp_object->post_content );
295 + $media = self::get_media_from_blocks( $blocks, $media );
527 296
528 - return null;
297 + return $media;
529 298 }
530 299
531 300 /**
532 - * Returns a list of Tags, used in the Post.
301 + * Recursively get media IDs from blocks.
302 + * @param array $blocks The blocks to search for media IDs
303 + * @param array $media The media IDs to append new IDs to
304 + * @param int $max_media The maximum number of media to return.
533 305 *
534 - * This includes Hash-Tags and Mentions.
535 - *
536 - * @return array The list of Tags.
306 + * @return array The image IDs.
537 307 */
538 - protected function get_tag() {
539 - if ( false !== $this->tags ) {
540 - return $this->tags;
541 - }
308 + protected static function get_media_from_blocks( $blocks, $media ) {
309 + foreach ( $blocks as $block ) {
310 + // recurse into inner blocks
311 + if ( ! empty( $block['innerBlocks'] ) ) {
312 + $media = self::get_media_from_blocks( $block['innerBlocks'], $media );
313 + }
542 314
543 - $tags = parent::get_tag();
315 + switch ( $block['blockName'] ) {
316 + case 'core/image':
317 + case 'core/cover':
318 + if ( ! empty( $block['attrs']['id'] ) ) {
319 + $alt = '';
320 + $check = preg_match( '/<img.*?alt\s*=\s*([\"\'])(.*?)\1.*>/i', $block['innerHTML'], $match );
544 321
545 - $post_tags = \get_the_tags( $this->item->ID );
546 - if ( $post_tags ) {
547 - foreach ( $post_tags as $post_tag ) {
548 - // Tag can be empty.
549 - if ( ! $post_tag ) {
550 - continue;
551 - }
322 + if ( $check ) {
323 + $alt = $match[2];
324 + }
552 325
553 - $tags[] = array(
554 - 'type' => 'Hashtag',
555 - 'href' => \esc_url_raw( \get_tag_link( $post_tag->term_id ) ),
556 - 'name' => esc_hashtag( $post_tag->name ),
557 - );
326 + $media['image'][] = array(
327 + 'id' => $block['attrs']['id'],
328 + 'alt' => $alt,
329 + );
330 + }
331 + break;
332 + case 'core/audio':
333 + if ( ! empty( $block['attrs']['id'] ) ) {
334 + $media['audio'][] = array( 'id' => $block['attrs']['id'] );
335 + }
336 + break;
337 + case 'core/video':
338 + case 'videopress/video':
339 + if ( ! empty( $block['attrs']['id'] ) ) {
340 + $media['video'][] = array( 'id' => $block['attrs']['id'] );
341 + }
342 + break;
343 + case 'jetpack/slideshow':
344 + case 'jetpack/tiled-gallery':
345 + if ( ! empty( $block['attrs']['ids'] ) ) {
346 + $media['image'] = array_merge(
347 + $media['image'],
348 + array_map(
349 + function ( $id ) {
350 + return array( 'id' => $id );
351 + },
352 + $block['attrs']['ids']
353 + )
354 + );
355 + }
356 + break;
357 + case 'jetpack/image-compare':
358 + if ( ! empty( $block['attrs']['beforeImageId'] ) ) {
359 + $media['image'][] = array( 'id' => $block['attrs']['beforeImageId'] );
360 + }
361 + if ( ! empty( $block['attrs']['afterImageId'] ) ) {
362 + $media['image'][] = array( 'id' => $block['attrs']['afterImageId'] );
363 + }
364 + break;
558 365 }
559 366 }
560 367
561 - $this->tags = \array_unique( $tags, SORT_REGULAR );
562 -
563 - return $this->tags;
368 + return $media;
564 369 }
565 370
566 371 /**
567 - * Returns the summary for the ActivityPub Item.
372 + * Get post images from the classic editor.
373 + * Note that audio/video attachments are only supported in the block editor.
568 374 *
569 - * The summary will be generated based on the user settings and only if the
570 - * object type is not set to `note`.
375 + * @param array $media The media array grouped by type.
376 + * @param int $max_images The maximum number of images to return.
571 377 *
572 - * @return string|null The summary or null if the object type is `note`.
378 + * @return array The attachments.
573 379 */
574 - protected function get_summary() {
575 - if ( 'Note' === $this->get_type() ) {
576 - return null;
380 + protected function get_classic_editor_images( $media, $max_images ) {
381 + // max images can't be negative or zero
382 + if ( $max_images <= 0 ) {
383 + return array();
577 384 }
578 385
579 - if ( false !== $this->summary ) {
580 - return $this->summary;
386 + if ( \count( $media['image'] ) <= $max_images ) {
387 + if ( \class_exists( '\WP_HTML_Tag_Processor' ) ) {
388 + $media['image'] = \array_merge( $media['image'], $this->get_classic_editor_image_embeds( $max_images ) );
389 + } else {
390 + $media['image'] = \array_merge( $media['image'], $this->get_classic_editor_image_attachments( $max_images ) );
391 + }
581 392 }
582 393
583 - $this->summary = generate_post_summary( $this->item );
584 -
585 - return $this->summary;
394 + return $media;
586 395 }
587 396
588 397 /**
589 - * Returns the title for the ActivityPub Item.
398 + * Get image embeds from the classic editor by parsing HTML.
590 399 *
591 - * The title will be generated based on the user settings and only if the
592 - * object type is not set to `note`.
400 + * @param int $max_images The maximum number of images to return.
593 401 *
594 - * @return string|null The title or null if the object type is `note`.
402 + * @return array The attachments.
595 403 */
596 - protected function get_name() {
597 - if ( 'Note' === $this->get_type() ) {
598 - return null;
404 + protected function get_classic_editor_image_embeds( $max_images ) {
405 + // if someone calls that function directly, bail
406 + if ( ! \class_exists( '\WP_HTML_Tag_Processor' ) ) {
407 + return array();
599 408 }
600 409
601 - $title = \get_the_title( $this->item->ID );
410 + // max images can't be negative or zero
411 + if ( $max_images <= 0 ) {
412 + return array();
413 + }
602 414
603 - if ( ! $title ) {
604 - return null;
415 + $images = array();
416 + $base = \wp_get_upload_dir()['baseurl'];
417 + $content = \get_post_field( 'post_content', $this->wp_object );
418 + $tags = new \WP_HTML_Tag_Processor( $content );
419 +
420 + // This linter warning is a false positive - we have to
421 + // re-count each time here as we modify $images.
422 + // phpcs:ignore Squiz.PHP.DisallowSizeFunctionsInLoops.Found
423 + while ( $tags->next_tag( 'img' ) && ( \count( $images ) <= $max_images ) ) {
424 + $src = $tags->get_attribute( 'src' );
425 +
426 + // If the img source is in our uploads dir, get the
427 + // associated ID. Note: if there's a -500x500
428 + // type suffix, we remove it, but we try the original
429 + // first in case the original image is actually called
430 + // that. Likewise, we try adding the -scaled suffix for
431 + // the case that this is a small version of an image
432 + // that was big enough to get scaled down on upload:
433 + // https://make.wordpress.org/core/2019/10/09/introducing-handling-of-big-images-in-wordpress-5-3/
434 + if ( null !== $src && \str_starts_with( $src, $base ) ) {
435 + $img_id = \attachment_url_to_postid( $src );
436 +
437 + if ( 0 === $img_id ) {
438 + $count = 0;
439 + $src = preg_replace( '/-(?:\d+x\d+)(\.[a-zA-Z]+)$/', '$1', $src, 1, $count );
440 + if ( $count > 0 ) {
441 + $img_id = \attachment_url_to_postid( $src );
442 + }
443 + }
444 +
445 + if ( 0 === $img_id ) {
446 + $src = preg_replace( '/(\.[a-zA-Z]+)$/', '-scaled$1', $src );
447 + $img_id = \attachment_url_to_postid( $src );
448 + }
449 +
450 + if ( 0 !== $img_id ) {
451 + $images[] = array(
452 + 'id' => $img_id,
453 + 'alt' => $tags->get_attribute( 'alt' ),
454 + );
455 + }
456 + }
605 457 }
606 458
607 - return \wp_strip_all_tags(
608 - \html_entity_decode(
609 - $title
610 - )
611 - );
459 + return $images;
612 460 }
613 461
614 462 /**
615 - * Returns the content for the ActivityPub Item.
463 + * Get image attachments from the classic editor.
464 + * This is imperfect as the contained images aren't necessarily the
465 + * same as the attachments.
616 466 *
617 - * The content will be generated based on the user settings.
467 + * @param int $max_images The maximum number of images to return.
618 468 *
619 - * @return string The content.
469 + * @return array The attachment IDs.
620 470 */
621 - protected function get_content() {
622 - if ( false !== $this->content ) {
623 - return $this->content;
471 + protected function get_classic_editor_image_attachments( $max_images ) {
472 + // max images can't be negative or zero
473 + if ( $max_images <= 0 ) {
474 + return array();
624 475 }
625 476
626 - global $post;
477 + $images = array();
478 + $query = new \WP_Query(
479 + array(
480 + 'post_parent' => $this->wp_object->ID,
481 + 'post_status' => 'inherit',
482 + 'post_type' => 'attachment',
483 + 'post_mime_type' => 'image',
484 + 'order' => 'ASC',
485 + 'orderby' => 'menu_order ID',
486 + 'posts_per_page' => $max_images,
487 + )
488 + );
627 489
628 - // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
629 - $post = $this->item;
630 - $content = $this->get_post_content_template();
631 -
632 - /**
633 - * Provides an action hook so plugins can add their own hooks/filters before AP content is generated.
634 - *
635 - * Example: if a plugin adds a filter to `the_content` to add a button to the end of posts, it can also remove that filter here.
636 - *
637 - * @param \WP_Post $post The post object.
638 - */
639 - \do_action( 'activitypub_before_get_content', $post );
640 -
641 - // It seems that shortcodes are only applied to published posts.
642 - if ( \is_preview() ) {
643 - $post->post_status = 'publish';
490 + foreach ( $query->get_posts() as $attachment ) {
491 + if ( ! \in_array( $attachment->ID, $images, true ) ) {
492 + $images[] = array( 'id' => $attachment->ID );
493 + }
644 494 }
645 495
646 - // Register our shortcodes just in time.
647 - Shortcodes::register();
648 - // Fill in the shortcodes.
649 - \setup_postdata( $post );
650 - $content = \do_shortcode( $content );
651 - \wp_reset_postdata();
496 + return $images;
497 + }
652 498
653 - // Don't need these anymore, should never appear in a post.
654 - Shortcodes::unregister();
499 + /**
500 + * Filter media IDs by object type.
501 + *
502 + * @param array $media The media array grouped by type.
503 + * @param string $type The object type.
504 + *
505 + * @return array The filtered media IDs.
506 + */
507 + protected static function filter_media_by_object_type( $media, $type, $wp_object ) {
508 + $type = \apply_filters( 'filter_media_by_object_type', \strtolower( $type ), $wp_object );
655 509
656 - /**
657 - * Filters the post content after it was transformed for ActivityPub.
658 - *
659 - * @param string $content The transformed post content.
660 - * @param \WP_Post $post The post object being transformed.
661 - */
662 - $this->content = \apply_filters( 'activitypub_the_content', $content, $post );
510 + if ( ! empty( $media[ $type ] ) ) {
511 + return $media[ $type ];
512 + }
663 513
664 - return $this->content;
514 + return array_filter( array_merge( ...array_values( $media ) ) );
665 515 }
666 516
667 517 /**
668 - * Returns the in-reply-to URL of the post.
518 + * Converts a WordPress Attachment to an ActivityPub Attachment.
669 519 *
670 - * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-inreplyto
520 + * @param array $media The Attachment array.
671 521 *
672 - * @return string|array|null The in-reply-to URL of the post.
522 + * @return array The ActivityPub Attachment.
673 523 */
674 - protected function get_in_reply_to() {
675 - if ( false !== $this->in_reply_to ) {
676 - return $this->in_reply_to;
524 + public static function wp_attachment_to_activity_attachment( $media ) {
525 + if ( ! isset( $media['id'] ) ) {
526 + return $media;
677 527 }
678 528
679 - if ( ! site_supports_blocks() ) {
680 - $this->in_reply_to = null;
681 - return $this->in_reply_to;
682 - }
529 + $id = $media['id'];
530 + $attachment = array();
531 + $mime_type = \get_post_mime_type( $id );
532 + $mime_type_parts = \explode( '/', $mime_type );
533 + // switching on image/audio/video
534 + switch ( $mime_type_parts[0] ) {
535 + case 'image':
536 + $image_size = 'large';
683 537
684 - $reply_urls = array();
685 - $blocks = \parse_blocks( $this->item->post_content );
538 + /**
539 + * Filter the image URL returned for each post.
540 + *
541 + * @param array|false $thumbnail The image URL, or false if no image is available.
542 + * @param int $id The attachment ID.
543 + * @param string $image_size The image size to retrieve. Set to 'large' by default.
544 + */
545 + $thumbnail = apply_filters(
546 + 'activitypub_get_image',
547 + self::get_wordpress_attachment( $id, $image_size ),
548 + $id,
549 + $image_size
550 + );
686 551
687 - foreach ( $blocks as $block ) {
688 - if ( 'activitypub/reply' === $block['blockName'] && isset( $block['attrs']['url'] ) ) {
552 + if ( $thumbnail ) {
553 + $image = array(
554 + 'type' => 'Image',
555 + 'url' => \esc_url( $thumbnail[0] ),
556 + 'mediaType' => \esc_attr( $mime_type ),
557 + );
689 558
690 - // Check if the URL has been validated as ActivityPub. Default to true for backwards compatibility.
691 - if ( $block['attrs']['isValidActivityPub'] ?? true ) {
692 - $reply_urls[] = $block['attrs']['url'];
559 + if ( ! empty( $media['alt'] ) ) {
560 + $image['name'] = \wp_strip_all_tags( \html_entity_decode( $media['alt'] ) );
561 + } else {
562 + $alt = \get_post_meta( $id, '_wp_attachment_image_alt', true );
563 + if ( $alt ) {
564 + $image['name'] = \wp_strip_all_tags( \html_entity_decode( $alt ) );
565 + }
566 + }
567 +
568 + $attachment = $image;
693 569 }
694 - }
695 - }
570 + break;
696 571
697 - if ( empty( $reply_urls ) ) {
698 - $this->in_reply_to = null;
699 -
700 - return $this->in_reply_to;
572 + case 'audio':
573 + case 'video':
574 + $attachment = array(
575 + 'type' => 'Document',
576 + 'mediaType' => \esc_attr( $mime_type ),
577 + 'url' => \esc_url( \wp_get_attachment_url( $id ) ),
578 + 'name' => \esc_attr( \get_the_title( $id ) ),
579 + );
580 + $meta = wp_get_attachment_metadata( $id );
581 + // height and width for videos
582 + if ( isset( $meta['width'] ) && isset( $meta['height'] ) ) {
583 + $attachment['width'] = \esc_attr( $meta['width'] );
584 + $attachment['height'] = \esc_attr( $meta['height'] );
585 + }
586 + // @todo: add `icon` support for audio/video attachments. Maybe use post thumbnail?
587 + break;
701 588 }
702 589
703 - if ( 1 === \count( $reply_urls ) ) {
704 - $this->in_reply_to = \current( $reply_urls );
705 -
706 - return $this->in_reply_to;
707 - }
708 -
709 - $this->in_reply_to = \array_values( \array_unique( $reply_urls ) );
710 -
711 - return $this->in_reply_to;
590 + return \apply_filters( 'activitypub_attachment', $attachment, $id );
712 591 }
713 592
714 593 /**
715 - * Returns the published date of the post.
594 + * Return details about an image attachment.
716 595 *
717 - * @return string The published date of the post.
596 + * @param int $id The attachment ID.
597 + * @param string $image_size The image size to retrieve. Set to 'large' by default.
598 + *
599 + * @return array|false Array of image data, or boolean false if no image is available.
718 600 */
719 - protected function get_published() {
720 - $published = \strtotime( $this->item->post_date_gmt );
601 + protected static function get_wordpress_attachment( $id, $image_size = 'large' ) {
602 + /**
603 + * Hook into the image retrieval process. Before image retrieval.
604 + *
605 + * @param int $id The attachment ID.
606 + * @param string $image_size The image size to retrieve. Set to 'large' by default.
607 + */
608 + do_action( 'activitypub_get_image_pre', $id, $image_size );
721 609
722 - return \gmdate( ACTIVITYPUB_DATE_TIME_RFC3339, $published );
723 - }
610 + $image = \wp_get_attachment_image_src( $id, $image_size );
724 611
725 - /**
726 - * Returns the updated date of the post.
727 - *
728 - * @return string|null The updated date of the post.
729 - */
730 - protected function get_updated() {
731 - $published = \strtotime( $this->item->post_date_gmt );
732 - $updated = \strtotime( $this->item->post_modified_gmt );
612 + /**
613 + * Hook into the image retrieval process. After image retrieval.
614 + *
615 + * @param int $id The attachment ID.
616 + * @param string $image_size The image size to retrieve. Set to 'large' by default.
617 + */
618 + do_action( 'activitypub_get_image_post', $id, $image_size );
733 619
734 - if ( $updated > $published ) {
735 - return \gmdate( ACTIVITYPUB_DATE_TIME_RFC3339, $updated );
736 - }
737 -
738 - return null;
620 + return $image;
739 621 }
740 622
741 623 /**
742 - * Returns the location of the post as a Place object.
624 + * Returns the ActivityStreams 2.0 Object-Type for a Post based on the
625 + * settings and the Post-Type.
743 626 *
744 - * Uses WordPress Geodata post meta fields to build the location.
627 + * @see https://www.w3.org/TR/activitystreams-vocabulary/#activity-types
745 628 *
746 - * @see https://codex.wordpress.org/Geodata
747 - * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-location
748 - *
749 - * @return array|null The Place object or null if no public geodata is available.
629 + * @return string The Object-Type.
750 630 */
751 - protected function get_location() {
752 - $post_id = $this->item->ID;
753 - $meta = \get_post_meta( $post_id );
631 + protected function get_type() {
632 + $post_format_setting = \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE );
754 633
755 - // If geo_public exists and is explicitly set to 0, don't share location.
756 - if ( isset( $meta['geo_public'] ) && '0' === $meta['geo_public'][0] ) {
757 - return null;
634 + if ( 'wordpress-post-format' !== $post_format_setting ) {
635 + return \ucfirst( $post_format_setting );
758 636 }
759 637
760 - // Both latitude and longitude are required for a valid location.
761 - // Use is_numeric() instead of empty() since 0 is a valid coordinate (Equator/Prime Meridian).
762 - $has_latitude = isset( $meta['geo_latitude'][0] ) && \is_numeric( $meta['geo_latitude'][0] );
763 - $has_longitude = isset( $meta['geo_longitude'][0] ) && \is_numeric( $meta['geo_longitude'][0] );
638 + $has_title = post_type_supports( $this->wp_object->post_type, 'title' );
764 639
765 - if ( ! $has_latitude || ! $has_longitude ) {
766 - return null;
640 + if ( ! $has_title ) {
641 + return 'Note';
767 642 }
768 643
769 - $place = array(
770 - 'type' => 'Place',
771 - 'latitude' => (float) $meta['geo_latitude'][0],
772 - 'longitude' => (float) $meta['geo_longitude'][0],
773 - );
644 + // Default to Article.
645 + $object_type = 'Article';
646 + $post_format = 'standard';
774 647
775 - // Add the address/name if available.
776 - if ( ! empty( $meta['geo_address'][0] ) ) {
777 - $place['name'] = \sanitize_text_field( $meta['geo_address'][0] );
648 + if ( \get_theme_support( 'post-formats' ) ) {
649 + $post_format = \get_post_format( $this->wp_object );
778 650 }
779 651
780 - /**
781 - * Filter the location Place object for a post.
782 - *
783 - * @param array $place The Place object.
784 - * @param \WP_Post $post The post object.
785 - * @param int $post_id The post ID.
786 - *
787 - * @return array|null The filtered Place object or null to disable location.
788 - */
789 - return \apply_filters( 'activitypub_post_location', $place, $this->item, $post_id );
652 + $post_type = \get_post_type( $this->wp_object );
653 + switch ( $post_type ) {
654 + case 'post':
655 + switch ( $post_format ) {
656 + case 'standard':
657 + case '':
658 + $object_type = 'Article';
659 + break;
660 + default:
661 + $object_type = 'Note';
662 + break;
663 + }
664 + break;
665 + case 'page':
666 + $object_type = 'Page';
667 + break;
668 + default:
669 + $object_type = 'Article';
670 + break;
671 + }
672 +
673 + return $object_type;
790 674 }
791 675
792 676 /**
793 - * Helper function to extract the @-Mentions from the post content.
677 + * Returns a list of Mentions, used in the Post.
794 678 *
795 - * @return array The list of @-Mentions.
679 + * @see https://docs.joinmastodon.org/spec/activitypub/#Mention
680 + *
681 + * @return array The list of Mentions.
796 682 */
797 - protected function get_mentions() {
798 - if ( false !== $this->mentions ) {
799 - return $this->mentions;
683 + protected function get_cc() {
684 + $cc = array();
685 +
686 + $mentions = $this->get_mentions();
687 + if ( $mentions ) {
688 + foreach ( $mentions as $url ) {
689 + $cc[] = $url;
690 + }
800 691 }
801 692
802 - /**
803 - * Filter the mentions in the post content.
804 - *
805 - * @param array $mentions The mentions.
806 - * @param string $content The post content.
807 - * @param \WP_Post $post The post object.
808 - *
809 - * @return array The filtered mentions.
810 - */
811 - $this->mentions = \apply_filters(
812 - 'activitypub_extract_mentions',
813 - array(),
814 - $this->item->post_content . ' ' . $this->item->post_excerpt,
815 - $this->item
816 - );
693 + return $cc;
694 + }
817 695
818 - return $this->mentions;
819 - }
820 696
821 - /**
822 - * Whether the post should be redacted from ActivityPub representations.
823 - *
824 - * Redaction is fail-closed at a single boundary: `to_object()` returns a
825 - * Tombstone instead of transforming the post, so no body-derived field
826 - * (content, summary, name, preview, attachments, image/icon, tags, mentions,
827 - * in-reply-to, location) is ever read — not even one added to the transformer
828 - * later. This is the only caller of this gate.
829 - *
830 - * A post is redacted exactly when it is not publicly queryable — the same
831 - * predicate the scheduler uses to decide a federated post should emit a
832 - * Delete (`is_post_publicly_queryable()`), so the two never disagree. That
833 - * covers non-public status, password protection, the `local`/`private`
834 - * content-visibility meta, and a post type that no longer supports
835 - * ActivityPub. The Fediverse Preview keeps working because
836 - * `is_post_publicly_queryable()` itself treats a draft/pending/scheduled
837 - * post as queryable during a `?preview=true` request from a user who can
838 - * edit it.
839 - *
840 - * Note: we deliberately rely on `is_post_publicly_queryable()` rather than
841 - * `post_password_required()`. Federation output is per-instance, never
842 - * per-request, and `post_password_required()` returns false when a valid
843 - * `wp-postpass` cookie is on the current request (e.g. an editor who unlocked
844 - * the post), which would leak the protected body into an outbox snapshot.
845 - *
846 - * @return boolean True if the post must be redacted, false otherwise.
847 - */
848 - protected function is_redacted() {
849 - return ! is_post_publicly_queryable( $this->item );
697 + public function get_audience() {
698 + if ( is_single_user() ) {
699 + return null;
700 + } else {
701 + $blog = new Blog();
702 + return $blog->get_id();
703 + }
850 704 }
851 705
852 706 /**
853 - * Get enclosures for a post.
707 + * Returns a list of Tags, used in the Post.
854 708 *
855 - * @param array $media The media array grouped by type.
709 + * This includes Hash-Tags and Mentions.
856 710 *
857 - * @return array The media array extended with enclosures.
711 + * @return array The list of Tags.
858 712 */
859 - protected function get_enclosures( $media ) {
860 - $enclosures = get_enclosures( $this->item->ID );
713 + protected function get_tag() {
714 + $tags = array();
861 715
862 - if ( ! $enclosures ) {
863 - return $media;
716 + $post_tags = \get_the_tags( $this->wp_object->ID );
717 + if ( $post_tags ) {
718 + foreach ( $post_tags as $post_tag ) {
719 + $tag = array(
720 + 'type' => 'Hashtag',
721 + 'href' => \esc_url( \get_tag_link( $post_tag->term_id ) ),
722 + 'name' => esc_hashtag( $post_tag->name ),
723 + );
724 + $tags[] = $tag;
725 + }
864 726 }
865 727
866 - foreach ( $enclosures as $enclosure ) {
867 - // Check if URL is an attachment.
868 - $attachment_id = \attachment_url_to_postid( $enclosure['url'] );
869 -
870 - if ( $attachment_id ) {
871 - $enclosure['id'] = $attachment_id;
872 - $enclosure['url'] = \wp_get_attachment_url( $attachment_id );
873 - $enclosure['mediaType'] = \get_post_mime_type( $attachment_id );
728 + $mentions = $this->get_mentions();
729 + if ( $mentions ) {
730 + foreach ( $mentions as $mention => $url ) {
731 + $tag = array(
732 + 'type' => 'Mention',
733 + 'href' => \esc_url( $url ),
734 + 'name' => \esc_html( $mention ),
735 + );
736 + $tags[] = $tag;
874 737 }
875 -
876 - $mime_type = $enclosure['mediaType'];
877 - $media_type = \strtok( $mime_type, '/' );
878 - $enclosure['type'] = \ucfirst( $media_type );
879 -
880 - switch ( $media_type ) {
881 - case 'image':
882 - $media['image'][] = $enclosure;
883 - break;
884 - case 'audio':
885 - $media['audio'][] = $enclosure;
886 - break;
887 - case 'video':
888 - $media['video'][] = $enclosure;
889 - break;
890 - }
891 738 }
892 739
893 - return $media;
740 + return $tags;
894 741 }
895 742
896 743 /**
897 - * Get media attachments from blocks. They will be formatted as ActivityPub attachments, not as WP attachments.
744 + * Returns the summary for the ActivityPub Item.
898 745 *
899 - * @param array $media The media array grouped by type.
900 - * @param int $max_media The maximum number of attachments to return.
746 + * The summary will be generated based on the user settings and only if the
747 + * object type is not set to `note`.
901 748 *
902 - * @return array The attachments.
749 + * @return string|null The summary or null if the object type is `note`.
903 750 */
904 - protected function get_block_attachments( $media, $max_media ) {
905 - // Max media can't be negative or zero.
906 - if ( $max_media <= 0 ) {
907 - return array();
751 + protected function get_summary() {
752 + if ( 'Note' === $this->get_type() ) {
753 + return null;
908 754 }
909 755
910 - $blocks = \parse_blocks( $this->item->post_content );
756 + // Remove Teaser from drafts.
757 + if ( 'draft' === \get_post_status( $this->wp_object ) ) {
758 + return \__( '(This post is being modified)', 'activitypub' );
759 + }
911 760
912 - return $this->get_media_from_blocks( $blocks, $media );
761 + return generate_post_summary( $this->wp_object );
913 762 }
914 763
915 764 /**
916 - * Recursively get media IDs from blocks.
765 + * Returns the title for the ActivityPub Item.
917 766 *
918 - * @param array $blocks The blocks to search for media IDs.
919 - * @param array $media The media IDs to append new IDs to.
767 + * The title will be generated based on the user settings and only if the
768 + * object type is not set to `note`.
920 769 *
921 - * @return array The image IDs.
770 + * @return string|null The title or null if the object type is `note`.
922 771 */
923 - protected function get_media_from_blocks( $blocks, $media ) {
924 - foreach ( $blocks as $block ) {
925 - // Recurse into inner blocks.
926 - if ( ! empty( $block['innerBlocks'] ) ) {
927 - $media = $this->get_media_from_blocks( $block['innerBlocks'], $media );
928 - }
772 + protected function get_name() {
773 + if ( 'Note' === $this->get_type() ) {
774 + return null;
775 + }
929 776
930 - switch ( $block['blockName'] ) {
931 - case 'core/image':
932 - case 'core/cover':
933 - if ( ! empty( $block['attrs']['id'] ) ) {
934 - $alt = '';
935 - $processor = new \WP_HTML_Tag_Processor( $block['innerHTML'] );
936 - if ( $processor->next_tag( array( 'tag_name' => 'img' ) ) ) {
937 - $alt = $processor->get_attribute( 'alt' ) ?? '';
938 - }
777 + $title = \get_the_title( $this->wp_object->ID );
939 778
940 - $found = false;
941 - foreach ( $media['image'] as $i => $image ) {
942 - if ( isset( $image['id'] ) && $image['id'] === $block['attrs']['id'] ) {
943 - $media['image'][ $i ]['alt'] = $alt;
944 - $found = true;
945 - break;
946 - }
947 - }
948 -
949 - if ( ! $found ) {
950 - $media['image'][] = array(
951 - 'id' => $block['attrs']['id'],
952 - 'alt' => $alt,
953 - );
954 - }
955 - }
956 - break;
957 - case 'core/media-text':
958 - if ( ! empty( $block['attrs']['mediaId'] ) ) {
959 - $media_id = $block['attrs']['mediaId'];
960 -
961 - // Media & Text holds either an image or a video; the default is image.
962 - if ( 'video' === ( $block['attrs']['mediaType'] ?? 'image' ) ) {
963 - $video = array( 'id' => $media_id );
964 -
965 - // The poster is stored as an HTML attribute on the <video> tag, not in block attrs.
966 - $processor = new \WP_HTML_Tag_Processor( $block['innerHTML'] );
967 - if ( $processor->next_tag( array( 'tag_name' => 'video' ) ) ) {
968 - $poster = $processor->get_attribute( 'poster' );
969 - if ( ! empty( $poster ) ) {
970 - $video['icon'] = \esc_url_raw( $poster );
971 - }
972 - }
973 -
974 - $media['video'][] = $video;
975 - } else {
976 - $alt = '';
977 - $processor = new \WP_HTML_Tag_Processor( $block['innerHTML'] );
978 - if ( $processor->next_tag( array( 'tag_name' => 'img' ) ) ) {
979 - $alt = $processor->get_attribute( 'alt' ) ?? '';
980 - }
981 -
982 - // Update alt in place if the image was already collected, so a
983 - // duplicate ID does not get dropped (and its alt lost) later.
984 - $found = false;
985 - foreach ( $media['image'] as $i => $image ) {
986 - if ( isset( $image['id'] ) && $image['id'] === $media_id ) {
987 - $media['image'][ $i ]['alt'] = $alt;
988 - $found = true;
989 - break;
990 - }
991 - }
992 -
993 - if ( ! $found ) {
994 - $media['image'][] = array(
995 - 'id' => $media_id,
996 - 'alt' => $alt,
997 - );
998 - }
999 - }
1000 - }
1001 - break;
1002 - case 'core/audio':
1003 - if ( ! empty( $block['attrs']['id'] ) ) {
1004 - $media['audio'][] = array( 'id' => $block['attrs']['id'] );
1005 - }
1006 - break;
1007 - case 'core/video':
1008 - case 'videopress/video':
1009 - if ( ! empty( $block['attrs']['id'] ) ) {
1010 - $video = array( 'id' => $block['attrs']['id'] );
1011 -
1012 - // The poster is stored as an HTML attribute on the <video> tag, not in block attrs.
1013 - $processor = new \WP_HTML_Tag_Processor( $block['innerHTML'] );
1014 - if ( $processor->next_tag( array( 'tag_name' => 'video' ) ) ) {
1015 - $poster = $processor->get_attribute( 'poster' );
1016 - if ( ! empty( $poster ) ) {
1017 - $video['icon'] = \esc_url_raw( $poster );
1018 - }
1019 - }
1020 -
1021 - $media['video'][] = $video;
1022 - }
1023 - break;
1024 - case 'jetpack/slideshow':
1025 - case 'jetpack/tiled-gallery':
1026 - if ( ! empty( $block['attrs']['ids'] ) ) {
1027 - $media['image'] = \array_merge(
1028 - $media['image'],
1029 - \array_map(
1030 - static function ( $id ) {
1031 - return array( 'id' => $id );
1032 - },
1033 - $block['attrs']['ids']
1034 - )
1035 - );
1036 - }
1037 - break;
1038 - case 'jetpack/image-compare':
1039 - if ( ! empty( $block['attrs']['beforeImageId'] ) ) {
1040 - $media['image'][] = array( 'id' => $block['attrs']['beforeImageId'] );
1041 - }
1042 - if ( ! empty( $block['attrs']['afterImageId'] ) ) {
1043 - $media['image'][] = array( 'id' => $block['attrs']['afterImageId'] );
1044 - }
1045 - break;
1046 - }
779 + if ( $title ) {
780 + return \wp_strip_all_tags(
781 + \html_entity_decode(
782 + $title
783 + )
784 + );
1047 785 }
1048 786
1049 - return $media;
787 + return null;
1050 788 }
1051 789
1052 790 /**
1053 - * Filter media IDs by object type.
791 + * Returns the content for the ActivityPub Item.
1054 792 *
1055 - * @param array $media The media array grouped by type.
1056 - * @param string $type The object type.
1057 - * @param \WP_Post $item The post object.
793 + * The content will be generated based on the user settings.
1058 794 *
1059 - * @return array The filtered media IDs.
795 + * @return string The content.
1060 796 */
1061 - protected function filter_media_by_object_type( $media, $type, $item ) {
797 + protected function get_content() {
798 + add_filter( 'activitypub_reply_block', '__return_empty_string' );
799 +
800 + // Remove Content from drafts.
801 + if ( 'draft' === \get_post_status( $this->wp_object ) ) {
802 + return \__( '(This post is being modified)', 'activitypub' );
803 + }
804 +
805 + global $post;
806 +
1062 807 /**
1063 - * Filter the object type for media attachments.
808 + * Provides an action hook so plugins can add their own hooks/filters before AP content is generated.
1064 809 *
1065 - * @param string $type The object type.
1066 - * @param \WP_Post $item The post object.
810 + * Example: if a plugin adds a filter to `the_content` to add a button to the end of posts, it can also remove that filter here.
1067 811 *
1068 - * @return string The filtered object type.
812 + * @param WP_Post $post The post object.
1069 813 */
1070 - $type = \apply_filters( 'filter_media_by_object_type', \strtolower( $type ), $item );
814 + do_action( 'activitypub_before_get_content', $post );
1071 815
1072 - if ( ! empty( $media[ $type ] ) ) {
1073 - return $media[ $type ];
1074 - }
816 + add_filter( 'render_block_core/embed', array( self::class, 'revert_embed_links' ), 10, 2 );
1075 817
1076 - return \array_filter( \array_merge( ...\array_values( $media ) ) );
1077 - }
818 + // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
819 + $post = $this->wp_object;
820 + $content = $this->get_post_content_template();
1078 821
1079 - /**
1080 - * Get the context of the post.
1081 - *
1082 - * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-context
1083 - *
1084 - * @return string The context of the post.
1085 - */
1086 - protected function get_context() {
1087 - return get_rest_url_by_path( \sprintf( 'posts/%d/context', $this->item->ID ) );
822 + // Register our shortcodes just in time.
823 + Shortcodes::register();
824 + // Fill in the shortcodes.
825 + setup_postdata( $post );
826 + $content = do_shortcode( $content );
827 + wp_reset_postdata();
828 +
829 + $content = \wpautop( $content );
830 + $content = \preg_replace( '/[\n\r\t]/', '', $content );
831 + $content = \trim( $content );
832 +
833 + $content = \apply_filters( 'activitypub_the_content', $content, $post );
834 +
835 + // Don't need these any more, should never appear in a post.
836 + Shortcodes::unregister();
837 +
838 + return $content;
1088 839 }
1089 840
1090 841 /**
1091 842 * Gets the template to use to generate the content of the activitypub item.
@@ -1092,154 +843,103 @@
1092 843 *
1093 844 * @return string The Template.
1094 845 */
1095 846 protected function get_post_content_template() {
1096 - $content = \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT );
1097 - $template = $content ?: ACTIVITYPUB_CUSTOM_POST_CONTENT;
847 + $type = \get_option( 'activitypub_post_content_type', 'content' );
1098 848
849 + switch ( $type ) {
850 + case 'excerpt':
851 + $template = "[ap_excerpt]\n\n[ap_permalink type=\"html\"]";
852 + break;
853 + case 'title':
854 + $template = "<h2>[ap_title]</h2>\n\n[ap_permalink type=\"html\"]";
855 + break;
856 + case 'content':
857 + $template = "[ap_content]\n\n[ap_permalink type=\"html\"]\n\n[ap_hashtags]";
858 + break;
859 + default:
860 + // phpcs:ignore Universal.Operators.DisallowShortTernary.Found
861 + $template = \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT ) ?: ACTIVITYPUB_CUSTOM_POST_CONTENT;
862 + break;
863 + }
864 +
1099 865 $post_format_setting = \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE );
1100 - $type = $this->get_type();
1101 866
1102 867 if ( 'wordpress-post-format' === $post_format_setting ) {
1103 - $template = '';
1104 -
1105 - /*
1106 - * If the post is a note, not a reply, and does not have mentions
1107 - * force the inclusion of the post title.
1108 - */
1109 - if (
1110 - 'Note' === $type
1111 - && empty( $this->get_in_reply_to() )
1112 - && empty( $this->get_mentions() )
1113 - ) {
1114 - $template .= '[ap_title type="html"]';
1115 - }
1116 -
1117 - $template .= '[ap_content]';
868 + $template = '[ap_content]';
1118 869 }
1119 870
1120 - /**
1121 - * Filters the template used to generate ActivityPub object content.
1122 - *
1123 - * This filter allows developers to modify the template that determines how post
1124 - * content is formatted in ActivityPub objects. The template can include special
1125 - * shortcodes like [ap_title] and [ap_content] that are processed during content
1126 - * generation.
1127 - *
1128 - * @since 7.6.0 Added the $type parameter.
1129 - *
1130 - * @param string $template The template string containing shortcodes.
1131 - * @param \WP_Post $item The WordPress post object being transformed.
1132 - * @param string $type ActivityStreams 2.0 Object-Type for the post.
1133 - */
1134 - return \apply_filters( 'activitypub_object_content_template', $template, $this->item, $type );
871 + return apply_filters( 'activitypub_object_content_template', $template, $this->wp_object );
1135 872 }
1136 873
1137 874 /**
1138 - * Get the replies Collection.
875 + * Helper function to get the @-Mentions from the post content.
1139 876 *
1140 - * @return array|null The replies collection on success or null on failure.
877 + * @return array The list of @-Mentions.
1141 878 */
1142 - public function get_replies() {
1143 - return Replies::get_collection( $this->item );
879 + protected function get_mentions() {
880 + return apply_filters(
881 + 'activitypub_extract_mentions',
882 + array(),
883 + $this->wp_object->post_content . ' ' . $this->wp_object->post_excerpt,
884 + $this->wp_object
885 + );
1144 886 }
1145 887
1146 888 /**
1147 - * Get the likes Collection.
889 + * Returns the locale of the post.
1148 890 *
1149 - * @return array The likes collection.
891 + * @return string The locale of the post.
1150 892 */
1151 - public function get_likes() {
1152 - return array(
1153 - 'id' => get_rest_url_by_path( \sprintf( 'posts/%d/likes', $this->item->ID ) ),
1154 - 'type' => 'Collection',
1155 - 'totalItems' => Interactions::count_by_type( $this->item->ID, 'like' ),
1156 - );
1157 - }
893 + public function get_locale() {
894 + $post_id = $this->wp_object->ID;
895 + $lang = \strtolower( \strtok( \get_locale(), '_-' ) );
1158 896
1159 - /**
1160 - * Get the shares Collection.
1161 - *
1162 - * @return array The Shares collection.
1163 - */
1164 - public function get_shares() {
1165 - return array(
1166 - 'id' => get_rest_url_by_path( \sprintf( 'posts/%d/shares', $this->item->ID ) ),
1167 - 'type' => 'Collection',
1168 - 'totalItems' => Interactions::count_by_type( $this->item->ID, 'repost' ) + Interactions::count_by_type( $this->item->ID, 'quote' ),
1169 - );
897 + /**
898 + * Filter the locale of the post.
899 + *
900 + * @param string $lang The locale of the post.
901 + * @param int $post_id The post ID.
902 + * @param WP_Post $post The post object.
903 + *
904 + * @return string The filtered locale of the post.
905 + */
906 + return apply_filters( 'activitypub_post_locale', $lang, $post_id, $this->wp_object );
1170 907 }
1171 908
1172 909 /**
1173 - * Get the preview of the post.
910 + * Returns the in-reply-to URL of the post.
1174 911 *
1175 - * @return array|null The preview of the post or null if the post is not an Article.
1176 - */
1177 - public function get_preview() {
1178 - if ( 'Article' !== $this->get_type() ) {
1179 - return null;
1180 - }
1181 -
1182 - return array(
1183 - 'type' => 'Note',
1184 - 'content' => $this->get_summary(),
1185 - );
1186 - }
1187 -
1188 - /**
1189 - * Get the quote policy.
912 + * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-inreplyto
1190 913 *
1191 - * @return array The quote policy.
914 + * @return string|null The in-reply-to URL of the post.
1192 915 */
1193 - private function get_quote_policy() {
1194 - $policy = \get_post_meta( $this->item->ID, 'activitypub_interaction_policy_quote', true );
916 + public function get_in_reply_to() {
917 + $blocks = \parse_blocks( $this->wp_object->post_content );
1195 918
1196 - // Fall back to global default if not set.
1197 - if ( ! $policy ) {
1198 - $policy = \get_option( 'activitypub_default_quote_policy', ACTIVITYPUB_INTERACTION_POLICY_ANYONE );
919 + foreach ( $blocks as $block ) {
920 + if ( 'activitypub/reply' === $block['blockName'] ) {
921 + // We only support one reply block per post for now.
922 + return $block['attrs']['url'];
923 + }
1199 924 }
1200 925
1201 - switch ( $policy ) {
1202 - case ACTIVITYPUB_INTERACTION_POLICY_FOLLOWERS:
1203 - return array( 'automaticApproval' => get_rest_url_by_path( \sprintf( 'actors/%d/followers', $this->item->post_author ) ) );
1204 -
1205 - case ACTIVITYPUB_INTERACTION_POLICY_ME:
1206 - return array( 'automaticApproval' => $this->get_self_interaction_policy() );
1207 -
1208 - default:
1209 - return $this->get_public_interaction_policy();
1210 - }
926 + return null;
1211 927 }
1212 928
1213 929 /**
1214 - * Get the public interaction policy.
930 + * Transform Embed blocks to block level link.
1215 931 *
1216 - * @return array The public interaction policy.
1217 - */
1218 - private function get_public_interaction_policy() {
1219 - return array(
1220 - 'automaticApproval' => 'https://www.w3.org/ns/activitystreams#Public',
1221 - 'always' => 'https://www.w3.org/ns/activitystreams#Public',
1222 - );
1223 - }
1224 -
1225 - /**
1226 - * Get the actor ID(s) for the `me` audience for use in interaction policies.
932 + * Remote servers will simply drop iframe elements, rendering incomplete content.
1227 933 *
1228 - * @return string|array The actor ID(s).
934 + * @see https://www.w3.org/TR/activitypub/#security-sanitizing-content
935 + * @see https://www.w3.org/wiki/ActivityPub/Primer/HTML
936 + *
937 + * @param string $block_content The block content (html)
938 + * @param object $block The block object
939 + *
940 + * @return string A block level link
1229 941 */
1230 - private function get_self_interaction_policy() {
1231 - switch ( \get_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_MODE ) ) {
1232 - case ACTIVITYPUB_BLOG_MODE:
1233 - return ( new Blog() )->get_id();
1234 -
1235 - case ACTIVITYPUB_ACTOR_AND_BLOG_MODE:
1236 - return array(
1237 - $this->get_actor_object()->get_id(),
1238 - ( new Blog() )->get_id(),
1239 - );
1240 -
1241 - default:
1242 - return $this->get_actor_object()->get_id();
1243 - }
942 + public static function revert_embed_links( $block_content, $block ) {
943 + return '<p><a href="' . esc_url( $block['attrs']['url'] ) . '">' . $block['attrs']['url'] . '</a></p>';
1244 944 }
1245 945 }