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