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