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
activitypub / includes / transformer / class-post.php

class-post.php in ActivityPub 8.0.2, at includes/transformer/class-post.php

1,208 lines 31.7 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * WordPress Post Transformer Class file.
4 *
5 * @package Activitypub
6 */
7
8 namespace Activitypub\Transformer;
9
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;
16 use Activitypub\Shortcodes;
17
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;
24 use function Activitypub\is_single_user;
25 use function Activitypub\site_supports_blocks;
26
27 /**
28 * WordPress Post Transformer.
29 *
30 * The Post Transformer is responsible for transforming a WP_Post object into different other
31 * Object-Types.
32 *
33 * Currently supported are:
34 *
35 * - Activitypub\Activity\Base_Object
36 */
37 class Post extends Base {
38 /**
39 * The User as Actor Object.
40 *
41 * @var \Activitypub\Activity\Actor
42 */
43 private $actor_object = null;
44
45 /**
46 * The content.
47 *
48 * @var string|false False indicates not yet computed.
49 */
50 private $content = false;
51
52 /**
53 * The summary.
54 *
55 * @var string|null|false False indicates not yet computed.
56 */
57 private $summary = false;
58
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 /**
88 * Transforms the WP_Post object to an ActivityPub Object
89 *
90 * @return \Activitypub\Activity\Base_Object The ActivityPub Object
91 */
92 public function to_object() {
93 $post = $this->item;
94 $object = parent::to_object();
95
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 }
103
104 return $object;
105 }
106
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() );
119
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 ) );
123 }
124
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(),
154 );
155 }
156
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 }
168
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;
184 }
185
186 /**
187 * Returns the ID of the Post.
188 *
189 * @return string The Posts ID.
190 */
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
200 return $this->get_url();
201 }
202
203 /**
204 * Returns the URL of the Post.
205 *
206 * @return string The Posts URL.
207 */
208 public function get_url() {
209 $post = $this->item;
210
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;
226 }
227
228 return \esc_url( $permalink );
229 }
230
231 /**
232 * Returns the User-URL of the Author of the Post.
233 *
234 * If `single_user` mode is enabled, the URL of the Blog-User is returned.
235 *
236 * @return string The User-URL.
237 */
238 protected function get_attributed_to() {
239 return $this->get_actor_object()->get_id();
240 }
241
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;
256 }
257
258 $id = \get_post_thumbnail_id( $post_id );
259 $image_size = 'large';
260
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;
277 }
278
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;
293 }
294
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 /**
352 * Generates all Media Attachments for a Post.
353 *
354 * @return array The Attachments.
355 */
356 protected function get_attachment() {
357 if ( false !== $this->attachment ) {
358 return $this->attachment;
359 }
360
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;
369 }
370
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;
443 }
444
445 /**
446 * Returns the ActivityStreams 2.0 Object-Type for a Post based on the
447 * settings and the Post-Type.
448 *
449 * @see https://www.w3.org/TR/activitystreams-vocabulary/#activity-types
450 *
451 * @return string The Object-Type.
452 */
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 );
458 }
459
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 }
464
465 // Default to Note.
466 $object_type = 'Note';
467 $post_type = \get_post_type( $this->item );
468
469 if ( 'page' === $post_type ) {
470 $object_type = 'Page';
471 } elseif ( ! \get_post_format( $this->item ) ) {
472 $object_type = 'Article';
473 }
474
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();
489 }
490
491 return null;
492 }
493
494 /**
495 * Returns a list of Tags, used in the Post.
496 *
497 * This includes Hash-Tags and Mentions.
498 *
499 * @return array The list of Tags.
500 */
501 protected function get_tag() {
502 if ( false !== $this->tags ) {
503 return $this->tags;
504 }
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 );
521 }
522 }
523
524 $this->tags = \array_unique( $tags, SORT_REGULAR );
525
526 return $this->tags;
527 }
528
529 /**
530 * Returns the summary for the ActivityPub Item.
531 *
532 * The summary will be generated based on the user settings and only if the
533 * object type is not set to `note`.
534 *
535 * @return string|null The summary or null if the object type is `note`.
536 */
537 protected function get_summary() {
538 if ( 'Note' === $this->get_type() ) {
539 return null;
540 }
541
542 if ( false !== $this->summary ) {
543 return $this->summary;
544 }
545
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' );
549
550 return $this->summary;
551 }
552
553 $this->summary = generate_post_summary( $this->item );
554
555 return $this->summary;
556 }
557
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 }
570
571 $title = \get_the_title( $this->item->ID );
572
573 if ( ! $title ) {
574 return null;
575 }
576
577 return \wp_strip_all_tags(
578 \html_entity_decode(
579 $title
580 )
581 );
582 }
583
584 /**
585 * Returns the content for the ActivityPub Item.
586 *
587 * The content will be generated based on the user settings.
588 *
589 * @return string The content.
590 */
591 protected function get_content() {
592 if ( false !== $this->content ) {
593 return $this->content;
594 }
595
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' );
599
600 return $this->content;
601 }
602
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';
621 }
622
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();
629
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;
642 }
643
644 /**
645 * Generate HTML @ link for reply block.
646 *
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.
653 */
654 public function generate_reply_link( $block_content, $block ) {
655 _deprecated_function( __METHOD__, '7.4.0', 'Activitypub\Blocks::generate_reply_link' );
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
680 foreach ( $blocks as $block ) {
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 }
687 }
688 }
689
690 if ( empty( $reply_urls ) ) {
691 $this->in_reply_to = null;
692
693 return $this->in_reply_to;
694 }
695
696 if ( 1 === count( $reply_urls ) ) {
697 $this->in_reply_to = \current( $reply_urls );
698
699 return $this->in_reply_to;
700 }
701
702 $this->in_reply_to = \array_values( \array_unique( $reply_urls ) );
703
704 return $this->in_reply_to;
705 }
706
707 /**
708 * Returns the published date of the post.
709 *
710 * @return string The published date of the post.
711 */
712 protected function get_published() {
713 $published = \strtotime( $this->item->post_date_gmt );
714
715 return \gmdate( ACTIVITYPUB_DATE_TIME_RFC3339, $published );
716 }
717
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 );
726
727 if ( $updated > $published ) {
728 return \gmdate( ACTIVITYPUB_DATE_TIME_RFC3339, $updated );
729 }
730
731 return null;
732 }
733
734 /**
735 * Returns the location of the post as a Place object.
736 *
737 * Uses WordPress Geodata post meta fields to build the location.
738 *
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.
743 */
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
773 /**
774 * Filter the location Place object for a post.
775 *
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.
781 */
782 return \apply_filters( 'activitypub_post_location', $place, $this->item, $post_id );
783 }
784
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 }
794
795 /**
796 * Filter the mentions in the post content.
797 *
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.
803 */
804 $this->mentions = apply_filters(
805 'activitypub_extract_mentions',
806 array(),
807 $this->item->post_content . ' ' . $this->item->post_excerpt,
808 $this->item
809 );
810
811 return $this->mentions;
812 }
813
814 /**
815 * Transform Embed blocks to block level link.
816 *
817 * Remote servers will simply drop iframe elements, rendering incomplete content.
818 *
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
828 */
829 public function revert_embed_links( $block_content, $block ) {
830 _deprecated_function( __METHOD__, '7.4.0', 'Activitypub\Blocks::revert_embed_links' );
831
832 return Blocks::revert_embed_links( $block_content, $block );
833 }
834
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;
842 }
843
844 /**
845 * Get enclosures for a post.
846 *
847 * @param array $media The media array grouped by type.
848 *
849 * @return array The media array extended with enclosures.
850 */
851 protected function get_enclosures( $media ) {
852 $enclosures = get_enclosures( $this->item->ID );
853
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 );
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 }
883 }
884
885 return $media;
886 }
887
888 /**
889 * Get media attachments from blocks. They will be formatted as ActivityPub attachments, not as WP attachments.
890 *
891 * @param array $media The media array grouped by type.
892 * @param int $max_media The maximum number of attachments to return.
893 *
894 * @return array The attachments.
895 */
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 }
901
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 );
920 }
921
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;
993 }
994 }
995
996 return $media;
997 }
998
999 /**
1000 * Filter media IDs by object type.
1001 *
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.
1005 *
1006 * @return array The filtered media IDs.
1007 */
1008 protected function filter_media_by_object_type( $media, $type, $item ) {
1009 /**
1010 * Filter the object type for media attachments.
1011 *
1012 * @param string $type The object type.
1013 * @param \WP_Post $item The post object.
1014 *
1015 * @return string The filtered object type.
1016 */
1017 $type = \apply_filters( 'filter_media_by_object_type', \strtolower( $type ), $item );
1018
1019 if ( ! empty( $media[ $type ] ) ) {
1020 return $media[ $type ];
1021 }
1022
1023 return array_filter( array_merge( ...array_values( $media ) ) );
1024 }
1025
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()' );
1037
1038 return parent::transform_attachment( $media );
1039 }
1040
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 ) );
1050 }
1051
1052 /**
1053 * Gets the template to use to generate the content of the activitypub item.
1054 *
1055 * @return string The Template.
1056 */
1057 protected function get_post_content_template() {
1058 $content = \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT );
1059 $template = $content ?: ACTIVITYPUB_CUSTOM_POST_CONTENT;
1060
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
1170 default:
1171 return $this->get_public_interaction_policy();
1172 }
1173 }
1174
1175 /**
1176 * Get the public interaction policy.
1177 *
1178 * @return array The public interaction policy.
1179 */
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 );
1185 }
1186
1187 /**
1188 * Get the actor ID(s) for the `me` audience for use in interaction policies.
1189 *
1190 * @return string|array The actor ID(s).
1191 */
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();
1196
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 }
1206 }
1207 }
1208