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 +583 -853 8.2.1 → 3.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\Blocks;
12 -use Activitypub\Collection\Actors;
13 -use Activitypub\Collection\Interactions;
14 -use Activitypub\Collection\Replies;
4 +use WP_Post;
5 +use Activitypub\Shortcodes;
15 6 use Activitypub\Model\Blog;
16 -use Activitypub\Shortcodes;
7 +use Activitypub\Collection\Users;
8 +use Activitypub\Transformer\Base;
17 9
18 10 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;
11 +use function Activitypub\is_single_user;
22 12 use function Activitypub\get_enclosures;
13 +use function Activitypub\site_supports_blocks;
23 14 use function Activitypub\get_rest_url_by_path;
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,137 +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 - $post = $this->item;
64 + $post = $this->wp_object;
94 65 $object = parent::to_object();
95 66
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 - }
67 + $published = \strtotime( $post->post_date_gmt );
103 68
104 - return $object;
105 - }
69 + $object->set_published( \gmdate( 'Y-m-d\TH:i:s\Z', $published ) );
106 70
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() );
71 + $updated = \strtotime( $post->post_modified_gmt );
119 72
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 ) );
73 + if ( $updated > $published ) {
74 + $object->set_updated( \gmdate( 'Y-m-d\TH:i:s\Z', $updated ) );
123 75 }
124 76
125 - return $object;
126 - }
77 + $object->set_content_map(
78 + array(
79 + $this->get_locale() => $this->get_content(),
80 + )
81 + );
127 82
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 - }
83 + $object->set_to(
84 + array(
85 + 'https://www.w3.org/ns/activitystreams#Public',
86 + $this->get_actor_object()->get_followers(),
87 + )
88 + );
137 89
138 - return $this->content_visibility;
90 + return $object;
139 91 }
140 92
141 93 /**
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 94 * Returns the User-Object of the Author of the Post.
159 95 *
160 96 * If `single_user` mode is enabled, the Blog-User is returned.
161 97 *
162 - * @return \Activitypub\Activity\Actor The User-Object.
98 + * @return Activitypub\Activity\Actor The User-Object.
163 99 */
164 - public function get_actor_object() {
100 + protected function get_actor_object() {
165 101 if ( $this->actor_object ) {
166 102 return $this->actor_object;
167 103 }
168 104
169 - $blog_user = new Blog();
105 + $blog_user = new Blog();
170 106 $this->actor_object = $blog_user;
171 107
172 108 if ( is_single_user() ) {
173 109 return $blog_user;
@@ -172,9 +108,9 @@
172 108 if ( is_single_user() ) {
173 109 return $blog_user;
174 110 }
175 111
176 - $user = Actors::get_by_id( $this->item->post_author );
112 + $user = Users::get_by_id( $this->wp_object->post_author );
177 113
178 114 if ( $user && ! is_wp_error( $user ) ) {
179 115 $this->actor_object = $user;
180 116 return $user;
@@ -188,16 +124,8 @@
188 124 *
189 125 * @return string The Posts ID.
190 126 */
191 127 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 128 return $this->get_url();
201 129 }
202 130
203 131 /**
@@ -205,16 +133,16 @@
205 133 *
206 134 * @return string The Posts URL.
207 135 */
208 136 public function get_url() {
209 - $post = $this->item;
137 + $post = $this->wp_object;
210 138
211 139 switch ( \get_post_status( $post ) ) {
212 140 case 'trash':
213 - $permalink = \get_post_meta( $post->ID, '_activitypub_canonical_url', true );
141 + $permalink = \get_post_meta( $post->ID, 'activitypub_canonical_url', true );
214 142 break;
215 143 case 'draft':
216 - // Get_sample_permalink is in wp-admin, not always loaded.
144 + // get_sample_permalink is in wp-admin, not always loaded
217 145 if ( ! \function_exists( '\get_sample_permalink' ) ) {
218 146 require_once ABSPATH . 'wp-admin/includes/post.php';
219 147 }
220 148 $sample = \get_sample_permalink( $post->ID );
@@ -235,827 +163,680 @@
235 163 *
236 164 * @return string The User-URL.
237 165 */
238 166 protected function get_attributed_to() {
239 - return $this->get_actor_object()->get_id();
167 + return $this->get_actor_object()->get_url();
240 168 }
241 169
242 170 /**
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 171 * Generates all Media Attachments for a Post.
353 172 *
354 173 * @return array The Attachments.
355 174 */
356 175 protected function get_attachment() {
357 - if ( false !== $this->attachment ) {
358 - return $this->attachment;
176 + // Remove attachments from drafts.
177 + if ( 'draft' === \get_post_status( $this->wp_object ) ) {
178 + return array();
359 179 }
360 180
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();
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 + );
367 189
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 190 $media = array(
395 - 'image' => array(),
396 191 'audio' => array(),
397 192 'video' => array(),
193 + 'image' => array(),
398 194 );
399 - $id = $this->item->ID;
195 + $id = $this->wp_object->ID;
400 196
401 - // List post thumbnail first if this post has one.
402 - 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 ) ) {
403 199 $media['image'][] = array( 'id' => \get_post_thumbnail_id( $id ) );
404 200 }
405 201
406 202 $media = $this->get_enclosures( $media );
407 203
408 - if ( site_supports_blocks() && \has_blocks( $this->item->post_content ) ) {
204 + if ( site_supports_blocks() && \has_blocks( $this->wp_object->post_content ) ) {
409 205 $media = $this->get_block_attachments( $media, $max_media );
410 206 } else {
411 - $media = $this->parse_html_images( $media, $max_media, $this->item->post_content );
207 + $media = $this->get_classic_editor_images( $media, $max_media );
412 208 }
413 209
414 - $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 );
415 214
416 215 /**
417 216 * Filter the attachment IDs for a post.
418 217 *
419 - * @param array $media The media array grouped by type.
420 - * @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.
421 220 *
422 221 * @return array The filtered attachment IDs.
423 222 */
424 - $media = \apply_filters( 'activitypub_attachment_ids', $media, $this->item );
223 + $media = \apply_filters( 'activitypub_attachment_ids', $media, $this->wp_object );
425 224
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 );
225 + $attchments = \array_filter( \array_map( array( self::class, 'wp_attachment_to_activity_attachment' ), $media ) );
429 226
430 - $attachments = \array_filter( \array_map( array( $this, 'transform_attachment' ), $media ) );
431 -
432 227 /**
433 228 * Filter the attachments for a post.
434 229 *
435 - * @param array $attachments The attachments.
436 - * @param \WP_Post $item The post object.
230 + * @param array $attchments The attachments.
231 + * @param WP_Post $this->wp_object The post object.
437 232 *
438 233 * @return array The filtered attachments.
439 234 */
440 - $this->attachment = \apply_filters( 'activitypub_attachments', $attachments, $this->item );
441 -
442 - return $this->attachment;
235 + return \apply_filters( 'activitypub_attachments', $attchments, $this->wp_object );
443 236 }
444 237
445 238 /**
446 - * Returns the ActivityStreams 2.0 Object-Type for a Post based on the
447 - * settings and the Post-Type.
239 + * Get enclosures for a post.
448 240 *
449 - * @see https://www.w3.org/TR/activitystreams-vocabulary/#activity-types
241 + * @param array $media The media array grouped by type.
450 242 *
451 - * @return string The Object-Type.
243 + * @return array The media array extended with enclosures.
452 244 */
453 - protected function get_type() {
454 - $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 );
455 247
456 - if ( 'wordpress-post-format' !== $post_format_setting ) {
457 - $object_type = \ucfirst( $post_format_setting );
458 - } elseif ( ! \post_type_supports( $this->item->post_type, 'title' ) || ! $this->item->post_title ) {
459 - $object_type = 'Note';
460 - } elseif ( 'page' === \get_post_type( $this->item ) ) {
461 - $object_type = 'Page';
462 - } elseif ( ! \get_post_format( $this->item ) ) {
463 - $object_type = 'Article';
464 - } else {
465 - $object_type = 'Note';
248 + if ( ! $enclosures ) {
249 + return $media;
466 250 }
467 251
468 - /**
469 - * Filters the ActivityPub object type for a post.
470 - *
471 - * Allows downstream consumers to override the discriminator that
472 - * decides whether a post federates as Note, Article, or Page.
473 - * The filtered value propagates to all internal callers of
474 - * get_type(), including former_type/tombstone handling,
475 - * summary and title decisions, the content template, and the
476 - * preview guard, not only the wire-format type property.
477 - *
478 - * @since 8.1.1
479 - *
480 - * @param string $object_type The computed ActivityPub object type.
481 - * @param \WP_Post $post The WordPress post being transformed.
482 - */
483 - return \apply_filters( 'activitypub_post_object_type', $object_type, $this->item );
484 - }
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 + }
485 260
486 - /**
487 - * Returns the Audience for the Post.
488 - *
489 - * @return string|null The audience.
490 - */
491 - public function get_audience() {
492 - $actor_mode = \get_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_MODE );
261 + $mime_type = $enclosure['mediaType'];
262 + $mime_type_parts = \explode( '/', $mime_type );
493 263
494 - if ( ACTIVITYPUB_ACTOR_AND_BLOG_MODE === $actor_mode ) {
495 - $blog = new Blog();
496 - return $blog->get_id();
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 + }
497 275 }
498 276
499 - return null;
277 + return $media;
500 278 }
501 279
502 280 /**
503 - * Returns a list of Tags, used in the Post.
281 + * Get media attachments from blocks. They will be formatted as ActivityPub attachments, not as WP attachments.
504 282 *
505 - * This includes Hash-Tags and Mentions.
283 + * @param array $media The media array grouped by type.
284 + * @param int $max_media The maximum number of attachments to return.
506 285 *
507 - * @return array The list of Tags.
286 + * @return array The attachments.
508 287 */
509 - protected function get_tag() {
510 - if ( false !== $this->tags ) {
511 - return $this->tags;
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();
512 292 }
513 293
514 - $tags = parent::get_tag();
294 + $blocks = \parse_blocks( $this->wp_object->post_content );
295 + $media = self::get_media_from_blocks( $blocks, $media );
515 296
516 - $post_tags = \get_the_tags( $this->item->ID );
517 - if ( $post_tags ) {
518 - foreach ( $post_tags as $post_tag ) {
519 - // Tag can be empty.
520 - if ( ! $post_tag ) {
521 - continue;
522 - }
523 -
524 - $tags[] = array(
525 - 'type' => 'Hashtag',
526 - 'href' => \esc_url( \get_tag_link( $post_tag->term_id ) ),
527 - 'name' => esc_hashtag( $post_tag->name ),
528 - );
529 - }
530 - }
531 -
532 - $this->tags = \array_unique( $tags, SORT_REGULAR );
533 -
534 - return $this->tags;
297 + return $media;
535 298 }
536 299
537 300 /**
538 - * Returns the summary for the ActivityPub Item.
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.
539 305 *
540 - * The summary will be generated based on the user settings and only if the
541 - * object type is not set to `note`.
542 - *
543 - * @return string|null The summary or null if the object type is `note`.
306 + * @return array The image IDs.
544 307 */
545 - protected function get_summary() {
546 - if ( 'Note' === $this->get_type() ) {
547 - return null;
548 - }
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 + }
549 314
550 - if ( false !== $this->summary ) {
551 - return $this->summary;
552 - }
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 );
553 321
554 - // Remove Teaser from unpublished posts.
555 - if ( ! $this->is_preview() && 'publish' !== \get_post_status( $this->item ) ) {
556 - $this->summary = \__( '(This post is being modified)', 'activitypub' );
322 + if ( $check ) {
323 + $alt = $match[2];
324 + }
557 325
558 - return $this->summary;
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;
365 + }
559 366 }
560 367
561 - $this->summary = generate_post_summary( $this->item );
562 -
563 - return $this->summary;
368 + return $media;
564 369 }
565 370
566 371 /**
567 - * Returns the title 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 title 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 title or null if the object type is `note`.
378 + * @return array The attachments.
573 379 */
574 - protected function get_name() {
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 - $title = \get_the_title( $this->item->ID );
580 -
581 - if ( ! $title ) {
582 - return null;
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 + }
583 392 }
584 393
585 - return \wp_strip_all_tags(
586 - \html_entity_decode(
587 - $title
588 - )
589 - );
394 + return $media;
590 395 }
591 396
592 397 /**
593 - * Returns the content for the ActivityPub Item.
398 + * Get image embeds from the classic editor by parsing HTML.
594 399 *
595 - * The content will be generated based on the user settings.
400 + * @param int $max_images The maximum number of images to return.
596 401 *
597 - * @return string The content.
402 + * @return array The attachments.
598 403 */
599 - protected function get_content() {
600 - if ( false !== $this->content ) {
601 - return $this->content;
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();
602 408 }
603 409
604 - // Remove Content from unpublished posts.
605 - if ( ! $this->is_preview() && 'publish' !== \get_post_status( $this->item ) ) {
606 - $this->content = \__( '(This post is being modified)', 'activitypub' );
410 + // max images can't be negative or zero
411 + if ( $max_images <= 0 ) {
412 + return array();
413 + }
607 414
608 - return $this->content;
609 - }
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 );
610 419
611 - global $post;
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' );
612 425
613 - // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
614 - $post = $this->item;
615 - $content = $this->get_post_content_template();
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 );
616 436
617 - /**
618 - * Provides an action hook so plugins can add their own hooks/filters before AP content is generated.
619 - *
620 - * 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.
621 - *
622 - * @param \WP_Post $post The post object.
623 - */
624 - \do_action( 'activitypub_before_get_content', $post );
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 + }
625 444
626 - // It seems that shortcodes are only applied to published posts.
627 - if ( is_preview() ) {
628 - $post->post_status = 'publish';
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 + }
629 457 }
630 458
631 - // Register our shortcodes just in time.
632 - Shortcodes::register();
633 - // Fill in the shortcodes.
634 - \setup_postdata( $post );
635 - $content = \do_shortcode( $content );
636 - \wp_reset_postdata();
459 + return $images;
460 + }
637 461
638 - // Don't need these anymore, should never appear in a post.
639 - Shortcodes::unregister();
462 + /**
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.
466 + *
467 + * @param int $max_images The maximum number of images to return.
468 + *
469 + * @return array The attachment IDs.
470 + */
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();
475 + }
640 476
641 - /**
642 - * Filters the post content after it was transformed for ActivityPub.
643 - *
644 - * @param string $content The transformed post content.
645 - * @param \WP_Post $post The post object being transformed.
646 - */
647 - $this->content = \apply_filters( 'activitypub_the_content', $content, $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 + );
648 489
649 - return $this->content;
490 + foreach ( $query->get_posts() as $attachment ) {
491 + if ( ! \in_array( $attachment->ID, $images, true ) ) {
492 + $images[] = array( 'id' => $attachment->ID );
493 + }
494 + }
495 +
496 + return $images;
650 497 }
651 498
652 499 /**
653 - * Generate HTML @ link for reply block.
500 + * Filter media IDs by object type.
654 501 *
655 - * @deprecated 7.4.0 Use {@see Blocks::generate_reply_link()}.
502 + * @param array $media The media array grouped by type.
503 + * @param string $type The object type.
656 504 *
657 - * @param string $block_content The block content.
658 - * @param array $block The block data.
659 - *
660 - * @return string The HTML @ link.
505 + * @return array The filtered media IDs.
661 506 */
662 - public function generate_reply_link( $block_content, $block ) {
663 - _deprecated_function( __METHOD__, '7.4.0', 'Activitypub\Blocks::generate_reply_link' );
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 );
664 509
665 - return Blocks::generate_reply_link( $block_content, $block );
510 + if ( ! empty( $media[ $type ] ) ) {
511 + return $media[ $type ];
512 + }
513 +
514 + return array_filter( array_merge( ...array_values( $media ) ) );
666 515 }
667 516
668 517 /**
669 - * Returns the in-reply-to URL of the post.
518 + * Converts a WordPress Attachment to an ActivityPub Attachment.
670 519 *
671 - * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-inreplyto
520 + * @param array $media The Attachment array.
672 521 *
673 - * @return string|array|null The in-reply-to URL of the post.
522 + * @return array The ActivityPub Attachment.
674 523 */
675 - protected function get_in_reply_to() {
676 - if ( false !== $this->in_reply_to ) {
677 - return $this->in_reply_to;
524 + public static function wp_attachment_to_activity_attachment( $media ) {
525 + if ( ! isset( $media['id'] ) ) {
526 + return $media;
678 527 }
679 528
680 - if ( ! site_supports_blocks() ) {
681 - $this->in_reply_to = null;
682 - return $this->in_reply_to;
683 - }
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';
684 537
685 - $reply_urls = array();
686 - $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 + );
687 551
688 - foreach ( $blocks as $block ) {
689 - 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 + );
690 558
691 - // Check if the URL has been validated as ActivityPub. Default to true for backwards compatibility.
692 - if ( $block['attrs']['isValidActivityPub'] ?? true ) {
693 - $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;
694 569 }
695 - }
696 - }
570 + break;
697 571
698 - if ( empty( $reply_urls ) ) {
699 - $this->in_reply_to = null;
700 -
701 - 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;
702 588 }
703 589
704 - if ( 1 === count( $reply_urls ) ) {
705 - $this->in_reply_to = \current( $reply_urls );
706 -
707 - return $this->in_reply_to;
708 - }
709 -
710 - $this->in_reply_to = \array_values( \array_unique( $reply_urls ) );
711 -
712 - return $this->in_reply_to;
590 + return \apply_filters( 'activitypub_attachment', $attachment, $id );
713 591 }
714 592
715 593 /**
716 - * Returns the published date of the post.
594 + * Return details about an image attachment.
717 595 *
718 - * @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.
719 600 */
720 - protected function get_published() {
721 - $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 );
722 609
723 - return \gmdate( ACTIVITYPUB_DATE_TIME_RFC3339, $published );
724 - }
610 + $image = \wp_get_attachment_image_src( $id, $image_size );
725 611
726 - /**
727 - * Returns the updated date of the post.
728 - *
729 - * @return string|null The updated date of the post.
730 - */
731 - protected function get_updated() {
732 - $published = \strtotime( $this->item->post_date_gmt );
733 - $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 );
734 619
735 - if ( $updated > $published ) {
736 - return \gmdate( ACTIVITYPUB_DATE_TIME_RFC3339, $updated );
737 - }
738 -
739 - return null;
620 + return $image;
740 621 }
741 622
742 623 /**
743 - * 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.
744 626 *
745 - * Uses WordPress Geodata post meta fields to build the location.
627 + * @see https://www.w3.org/TR/activitystreams-vocabulary/#activity-types
746 628 *
747 - * @see https://codex.wordpress.org/Geodata
748 - * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-location
749 - *
750 - * @return array|null The Place object or null if no public geodata is available.
629 + * @return string The Object-Type.
751 630 */
752 - protected function get_location() {
753 - $post_id = $this->item->ID;
754 - $meta = \get_post_meta( $post_id );
631 + protected function get_type() {
632 + $post_format_setting = \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE );
755 633
756 - // If geo_public exists and is explicitly set to 0, don't share location.
757 - if ( isset( $meta['geo_public'] ) && '0' === $meta['geo_public'][0] ) {
758 - return null;
634 + if ( 'wordpress-post-format' !== $post_format_setting ) {
635 + return \ucfirst( $post_format_setting );
759 636 }
760 637
761 - // Both latitude and longitude are required for a valid location.
762 - // Use is_numeric() instead of empty() since 0 is a valid coordinate (Equator/Prime Meridian).
763 - $has_latitude = isset( $meta['geo_latitude'][0] ) && is_numeric( $meta['geo_latitude'][0] );
764 - $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' );
765 639
766 - if ( ! $has_latitude || ! $has_longitude ) {
767 - return null;
640 + if ( ! $has_title ) {
641 + return 'Note';
768 642 }
769 643
770 - $place = array(
771 - 'type' => 'Place',
772 - 'latitude' => (float) $meta['geo_latitude'][0],
773 - 'longitude' => (float) $meta['geo_longitude'][0],
774 - );
644 + // Default to Article.
645 + $object_type = 'Article';
646 + $post_format = 'standard';
775 647
776 - // Add the address/name if available.
777 - if ( ! empty( $meta['geo_address'][0] ) ) {
778 - $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 );
779 650 }
780 651
781 - /**
782 - * Filter the location Place object for a post.
783 - *
784 - * @param array $place The Place object.
785 - * @param \WP_Post $post The post object.
786 - * @param int $post_id The post ID.
787 - *
788 - * @return array|null The filtered Place object or null to disable location.
789 - */
790 - return \apply_filters( 'activitypub_post_location', $place, $this->item, $post_id );
791 - }
792 -
793 - /**
794 - * Helper function to extract the @-Mentions from the post content.
795 - *
796 - * @return array The list of @-Mentions.
797 - */
798 - protected function get_mentions() {
799 - if ( false !== $this->mentions ) {
800 - return $this->mentions;
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;
801 671 }
802 672
803 - /**
804 - * Filter the mentions in the post content.
805 - *
806 - * @param array $mentions The mentions.
807 - * @param string $content The post content.
808 - * @param \WP_Post $post The post object.
809 - *
810 - * @return array The filtered mentions.
811 - */
812 - $this->mentions = apply_filters(
813 - 'activitypub_extract_mentions',
814 - array(),
815 - $this->item->post_content . ' ' . $this->item->post_excerpt,
816 - $this->item
817 - );
818 -
819 - return $this->mentions;
673 + return $object_type;
820 674 }
821 675
822 676 /**
823 - * Transform Embed blocks to block level link.
677 + * Returns a list of Mentions, used in the Post.
824 678 *
825 - * Remote servers will simply drop iframe elements, rendering incomplete content.
679 + * @see https://docs.joinmastodon.org/spec/activitypub/#Mention
826 680 *
827 - * @deprecated 7.4.0 Use {@see Blocks::revert_embed_links()}.
828 - *
829 - * @see https://www.w3.org/TR/activitypub/#security-sanitizing-content
830 - * @see https://www.w3.org/wiki/ActivityPub/Primer/HTML
831 - *
832 - * @param string $block_content The block content (html).
833 - * @param object $block The block object.
834 - *
835 - * @return string A block level link
681 + * @return array The list of Mentions.
836 682 */
837 - public function revert_embed_links( $block_content, $block ) {
838 - _deprecated_function( __METHOD__, '7.4.0', 'Activitypub\Blocks::revert_embed_links' );
683 + protected function get_cc() {
684 + $cc = array();
839 685
840 - return Blocks::revert_embed_links( $block_content, $block );
686 + $mentions = $this->get_mentions();
687 + if ( $mentions ) {
688 + foreach ( $mentions as $url ) {
689 + $cc[] = $url;
690 + }
691 + }
692 +
693 + return $cc;
841 694 }
842 695
843 - /**
844 - * Check if the post is a preview.
845 - *
846 - * @return boolean True if the post is a preview, false otherwise.
847 - */
848 - private function is_preview() {
849 - return defined( 'ACTIVITYPUB_PREVIEW' ) && ACTIVITYPUB_PREVIEW;
696 +
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/audio':
958 - if ( ! empty( $block['attrs']['id'] ) ) {
959 - $media['audio'][] = array( 'id' => $block['attrs']['id'] );
960 - }
961 - break;
962 - case 'core/video':
963 - case 'videopress/video':
964 - if ( ! empty( $block['attrs']['id'] ) ) {
965 - $video = array( 'id' => $block['attrs']['id'] );
966 -
967 - // The poster is stored as an HTML attribute on the <video> tag, not in block attrs.
968 - $processor = new \WP_HTML_Tag_Processor( $block['innerHTML'] );
969 - if ( $processor->next_tag( array( 'tag_name' => 'video' ) ) ) {
970 - $poster = $processor->get_attribute( 'poster' );
971 - if ( ! empty( $poster ) ) {
972 - $video['icon'] = \esc_url_raw( $poster );
973 - }
974 - }
975 -
976 - $media['video'][] = $video;
977 - }
978 - break;
979 - case 'jetpack/slideshow':
980 - case 'jetpack/tiled-gallery':
981 - if ( ! empty( $block['attrs']['ids'] ) ) {
982 - $media['image'] = array_merge(
983 - $media['image'],
984 - array_map(
985 - static function ( $id ) {
986 - return array( 'id' => $id );
987 - },
988 - $block['attrs']['ids']
989 - )
990 - );
991 - }
992 - break;
993 - case 'jetpack/image-compare':
994 - if ( ! empty( $block['attrs']['beforeImageId'] ) ) {
995 - $media['image'][] = array( 'id' => $block['attrs']['beforeImageId'] );
996 - }
997 - if ( ! empty( $block['attrs']['afterImageId'] ) ) {
998 - $media['image'][] = array( 'id' => $block['attrs']['afterImageId'] );
999 - }
1000 - break;
1001 - }
779 + if ( $title ) {
780 + return \wp_strip_all_tags(
781 + \html_entity_decode(
782 + $title
783 + )
784 + );
1002 785 }
1003 786
1004 - return $media;
787 + return null;
1005 788 }
1006 789
1007 790 /**
1008 - * Filter media IDs by object type.
791 + * Returns the content for the ActivityPub Item.
1009 792 *
1010 - * @param array $media The media array grouped by type.
1011 - * @param string $type The object type.
1012 - * @param \WP_Post $item The post object.
793 + * The content will be generated based on the user settings.
1013 794 *
1014 - * @return array The filtered media IDs.
795 + * @return string The content.
1015 796 */
1016 - 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 +
1017 807 /**
1018 - * 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.
1019 809 *
1020 - * @param string $type The object type.
1021 - * @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.
1022 811 *
1023 - * @return string The filtered object type.
812 + * @param WP_Post $post The post object.
1024 813 */
1025 - $type = \apply_filters( 'filter_media_by_object_type', \strtolower( $type ), $item );
814 + do_action( 'activitypub_before_get_content', $post );
1026 815
1027 - if ( ! empty( $media[ $type ] ) ) {
1028 - return $media[ $type ];
1029 - }
816 + add_filter( 'render_block_core/embed', array( self::class, 'revert_embed_links' ), 10, 2 );
1030 817
1031 - return array_filter( array_merge( ...array_values( $media ) ) );
1032 - }
818 + // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
819 + $post = $this->wp_object;
820 + $content = $this->get_post_content_template();
1033 821
1034 - /**
1035 - * Converts a WordPress Attachment to an ActivityPub Attachment.
1036 - *
1037 - * @deprecated 7.2.0 Use {@see Base::transform_attachment()} instead.
1038 - *
1039 - * @param array $media The Attachment array.
1040 - *
1041 - * @return array The ActivityPub Attachment.
1042 - */
1043 - public function wp_attachment_to_activity_attachment( $media ) {
1044 - _deprecated_function( __METHOD__, '7.2.0', '\Activitypub\Transformer\Base::transform_attachment()' );
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();
1045 828
1046 - return parent::transform_attachment( $media );
1047 - }
829 + $content = \wpautop( $content );
830 + $content = \preg_replace( '/[\n\r\t]/', '', $content );
831 + $content = \trim( $content );
1048 832
1049 - /**
1050 - * Get the context of the post.
1051 - *
1052 - * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-context
1053 - *
1054 - * @return string The context of the post.
1055 - */
1056 - protected function get_context() {
1057 - return get_rest_url_by_path( sprintf( 'posts/%d/context', $this->item->ID ) );
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;
1058 839 }
1059 840
1060 841 /**
1061 842 * Gets the template to use to generate the content of the activitypub item.
@@ -1062,154 +843,103 @@
1062 843 *
1063 844 * @return string The Template.
1064 845 */
1065 846 protected function get_post_content_template() {
1066 - $content = \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT );
1067 - $template = $content ?: ACTIVITYPUB_CUSTOM_POST_CONTENT;
847 + $type = \get_option( 'activitypub_post_content_type', 'content' );
1068 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 +
1069 865 $post_format_setting = \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE );
1070 - $type = $this->get_type();
1071 866
1072 867 if ( 'wordpress-post-format' === $post_format_setting ) {
1073 - $template = '';
1074 -
1075 - /*
1076 - * If the post is a note, not a reply, and does not have mentions
1077 - * force the inclusion of the post title.
1078 - */
1079 - if (
1080 - 'Note' === $type
1081 - && empty( $this->get_in_reply_to() )
1082 - && empty( $this->get_mentions() )
1083 - ) {
1084 - $template .= '[ap_title type="html"]';
1085 - }
1086 -
1087 - $template .= '[ap_content]';
868 + $template = '[ap_content]';
1088 869 }
1089 870
1090 - /**
1091 - * Filters the template used to generate ActivityPub object content.
1092 - *
1093 - * This filter allows developers to modify the template that determines how post
1094 - * content is formatted in ActivityPub objects. The template can include special
1095 - * shortcodes like [ap_title] and [ap_content] that are processed during content
1096 - * generation.
1097 - *
1098 - * @since 7.6.0 Added the $type parameter.
1099 - *
1100 - * @param string $template The template string containing shortcodes.
1101 - * @param \WP_Post $item The WordPress post object being transformed.
1102 - * @param string $type ActivityStreams 2.0 Object-Type for the post.
1103 - */
1104 - return apply_filters( 'activitypub_object_content_template', $template, $this->item, $type );
871 + return apply_filters( 'activitypub_object_content_template', $template, $this->wp_object );
1105 872 }
1106 873
1107 874 /**
1108 - * Get the replies Collection.
875 + * Helper function to get the @-Mentions from the post content.
1109 876 *
1110 - * @return array|null The replies collection on success or null on failure.
877 + * @return array The list of @-Mentions.
1111 878 */
1112 - public function get_replies() {
1113 - 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 + );
1114 886 }
1115 887
1116 888 /**
1117 - * Get the likes Collection.
889 + * Returns the locale of the post.
1118 890 *
1119 - * @return array The likes collection.
891 + * @return string The locale of the post.
1120 892 */
1121 - public function get_likes() {
1122 - return array(
1123 - 'id' => get_rest_url_by_path( sprintf( 'posts/%d/likes', $this->item->ID ) ),
1124 - 'type' => 'Collection',
1125 - 'totalItems' => Interactions::count_by_type( $this->item->ID, 'like' ),
1126 - );
1127 - }
893 + public function get_locale() {
894 + $post_id = $this->wp_object->ID;
895 + $lang = \strtolower( \strtok( \get_locale(), '_-' ) );
1128 896
1129 - /**
1130 - * Get the shares Collection.
1131 - *
1132 - * @return array The Shares collection.
1133 - */
1134 - public function get_shares() {
1135 - return array(
1136 - 'id' => get_rest_url_by_path( sprintf( 'posts/%d/shares', $this->item->ID ) ),
1137 - 'type' => 'Collection',
1138 - 'totalItems' => Interactions::count_by_type( $this->item->ID, 'repost' ) + Interactions::count_by_type( $this->item->ID, 'quote' ),
1139 - );
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 );
1140 907 }
1141 908
1142 909 /**
1143 - * Get the preview of the post.
910 + * Returns the in-reply-to URL of the post.
1144 911 *
1145 - * @return array|null The preview of the post or null if the post is not an Article.
1146 - */
1147 - public function get_preview() {
1148 - if ( 'Article' !== $this->get_type() ) {
1149 - return null;
1150 - }
1151 -
1152 - return array(
1153 - 'type' => 'Note',
1154 - 'content' => $this->get_summary(),
1155 - );
1156 - }
1157 -
1158 - /**
1159 - * Get the quote policy.
912 + * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-inreplyto
1160 913 *
1161 - * @return array The quote policy.
914 + * @return string|null The in-reply-to URL of the post.
1162 915 */
1163 - private function get_quote_policy() {
1164 - $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 );
1165 918
1166 - // Fall back to global default if not set.
1167 - if ( ! $policy ) {
1168 - $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 + }
1169 924 }
1170 925
1171 - switch ( $policy ) {
1172 - case ACTIVITYPUB_INTERACTION_POLICY_FOLLOWERS:
1173 - return array( 'automaticApproval' => get_rest_url_by_path( sprintf( 'actors/%d/followers', $this->item->post_author ) ) );
1174 -
1175 - case ACTIVITYPUB_INTERACTION_POLICY_ME:
1176 - return array( 'automaticApproval' => $this->get_self_interaction_policy() );
1177 -
1178 - default:
1179 - return $this->get_public_interaction_policy();
1180 - }
926 + return null;
1181 927 }
1182 928
1183 929 /**
1184 - * Get the public interaction policy.
930 + * Transform Embed blocks to block level link.
1185 931 *
1186 - * @return array The public interaction policy.
1187 - */
1188 - private function get_public_interaction_policy() {
1189 - return array(
1190 - 'automaticApproval' => 'https://www.w3.org/ns/activitystreams#Public',
1191 - 'always' => 'https://www.w3.org/ns/activitystreams#Public',
1192 - );
1193 - }
1194 -
1195 - /**
1196 - * 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.
1197 933 *
1198 - * @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
1199 941 */
1200 - private function get_self_interaction_policy() {
1201 - switch ( \get_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_MODE ) ) {
1202 - case ACTIVITYPUB_BLOG_MODE:
1203 - return ( new Blog() )->get_id();
1204 -
1205 - case ACTIVITYPUB_ACTOR_AND_BLOG_MODE:
1206 - return array(
1207 - $this->get_actor_object()->get_id(),
1208 - ( new Blog() )->get_id(),
1209 - );
1210 -
1211 - default:
1212 - return $this->get_actor_object()->get_id();
1213 - }
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>';
1214 944 }
1215 945 }