PluginProbe
ActivityPub / 3.2.4
ActivityPub v3.2.4
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 +456 -197 2.0.13.2.4 View file →
@@ -2,17 +2,19 @@
2 2 namespace Activitypub\Transformer;
3 3
4 4 use WP_Post;
5 5 use Activitypub\Shortcodes;
6 -use Activitypub\Model\Blog_User;
6 +use Activitypub\Model\Blog;
7 +use Activitypub\Collection\Users;
7 8 use Activitypub\Transformer\Base;
8 -use Activitypub\Collection\Users;
9 -use Activitypub\Activity\Base_Object;
10 9
11 10 use function Activitypub\esc_hashtag;
12 11 use function Activitypub\is_single_user;
12 +use function Activitypub\get_enclosures;
13 +use function Activitypub\site_supports_blocks;
13 14 use function Activitypub\get_rest_url_by_path;
14 -use function Activitypub\site_supports_blocks;
15 +use function Activitypub\is_user_type_disabled;
16 +use function Activitypub\generate_post_summary;
15 17
16 18 /**
17 19 * WordPress Post Transformer
18 20 *
@@ -24,8 +26,15 @@
24 26 * - Activitypub\Activity\Base_Object
25 27 */
26 28 class Post extends Base {
27 29 /**
30 + * The User as Actor Object.
31 + *
32 + * @var Activitypub\Activity\Actor
33 + */
34 + private $actor_object = null;
35 +
36 + /**
28 37 * Returns the ID of the WordPress Post.
29 38 *
30 39 * @return int The ID of the WordPress Post
31 40 */
@@ -69,14 +78,13 @@
69 78 array(
70 79 $this->get_locale() => $this->get_content(),
71 80 )
72 81 );
73 - $path = sprintf( 'users/%d/followers', intval( $post->post_author ) );
74 82
75 83 $object->set_to(
76 84 array(
77 85 'https://www.w3.org/ns/activitystreams#Public',
78 - get_rest_url_by_path( $path ),
86 + $this->get_actor_object()->get_followers(),
79 87 )
80 88 );
81 89
82 90 return $object;
@@ -82,8 +90,37 @@
82 90 return $object;
83 91 }
84 92
85 93 /**
94 + * Returns the User-Object of the Author of the Post.
95 + *
96 + * If `single_user` mode is enabled, the Blog-User is returned.
97 + *
98 + * @return Activitypub\Activity\Actor The User-Object.
99 + */
100 + protected function get_actor_object() {
101 + if ( $this->actor_object ) {
102 + return $this->actor_object;
103 + }
104 +
105 + $blog_user = new Blog();
106 + $this->actor_object = $blog_user;
107 +
108 + if ( is_single_user() ) {
109 + return $blog_user;
110 + }
111 +
112 + $user = Users::get_by_id( $this->wp_object->post_author );
113 +
114 + if ( $user && ! is_wp_error( $user ) ) {
115 + $this->actor_object = $user;
116 + return $user;
117 + }
118 +
119 + return $blog_user;
120 + }
121 +
122 + /**
86 123 * Returns the ID of the Post.
87 124 *
88 125 * @return string The Posts ID.
89 126 */
@@ -98,12 +135,23 @@
98 135 */
99 136 public function get_url() {
100 137 $post = $this->wp_object;
101 138
102 - if ( 'trash' === get_post_status( $post ) ) {
103 - $permalink = \get_post_meta( $post->ID, 'activitypub_canonical_url', true );
104 - } else {
105 - $permalink = \get_permalink( $post );
139 + switch ( \get_post_status( $post ) ) {
140 + case 'trash':
141 + $permalink = \get_post_meta( $post->ID, 'activitypub_canonical_url', true );
142 + break;
143 + case 'draft':
144 + // get_sample_permalink is in wp-admin, not always loaded
145 + if ( ! \function_exists( '\get_sample_permalink' ) ) {
146 + require_once ABSPATH . 'wp-admin/includes/post.php';
147 + }
148 + $sample = \get_sample_permalink( $post->ID );
149 + $permalink = \str_replace( array( '%pagename%', '%postname%' ), $sample[1], $sample[0] );
150 + break;
151 + default:
152 + $permalink = \get_permalink( $post );
153 + break;
106 154 }
107 155
108 156 return \esc_url( $permalink );
109 157 }
@@ -115,102 +163,236 @@
115 163 *
116 164 * @return string The User-URL.
117 165 */
118 166 protected function get_attributed_to() {
119 - $blog_user = new Blog_User();
167 + return $this->get_actor_object()->get_url();
168 + }
120 169
121 - if ( is_single_user() ) {
122 - return $blog_user->get_url();
170 + /**
171 + * Generates all Media Attachments for a Post.
172 + *
173 + * @return array The Attachments.
174 + */
175 + protected function get_attachment() {
176 + // Remove attachments from drafts.
177 + if ( 'draft' === \get_post_status( $this->wp_object ) ) {
178 + return array();
123 179 }
124 180
125 - $user = Users::get_by_id( $this->wp_object->post_author );
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 + );
126 189
127 - if ( $user && ! is_wp_error( $user ) ) {
128 - return $user->get_url();
190 + $media = array(
191 + 'audio' => array(),
192 + 'video' => array(),
193 + 'image' => array(),
194 + );
195 + $id = $this->wp_object->ID;
196 +
197 + // list post thumbnail first if this post has one
198 + if ( \function_exists( 'has_post_thumbnail' ) && \has_post_thumbnail( $id ) ) {
199 + $media['image'][] = array( 'id' => \get_post_thumbnail_id( $id ) );
129 200 }
130 201
131 - return $blog_user->get_url();
202 + $media = $this->get_enclosures( $media );
203 +
204 + if ( site_supports_blocks() && \has_blocks( $this->wp_object->post_content ) ) {
205 + $media = $this->get_block_attachments( $media, $max_media );
206 + } else {
207 + $media = $this->get_classic_editor_images( $media, $max_media );
208 + }
209 +
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 );
214 +
215 + /**
216 + * Filter the attachment IDs for a post.
217 + *
218 + * @param array $media The media array grouped by type.
219 + * @param WP_Post $this->wp_object The post object.
220 + *
221 + * @return array The filtered attachment IDs.
222 + */
223 + $media = \apply_filters( 'activitypub_attachment_ids', $media, $this->wp_object );
224 +
225 + $attchments = \array_filter( \array_map( array( self::class, 'wp_attachment_to_activity_attachment' ), $media ) );
226 +
227 + /**
228 + * Filter the attachments for a post.
229 + *
230 + * @param array $attchments The attachments.
231 + * @param WP_Post $this->wp_object The post object.
232 + *
233 + * @return array The filtered attachments.
234 + */
235 + return \apply_filters( 'activitypub_attachments', $attchments, $this->wp_object );
132 236 }
133 237
134 238 /**
135 - * Generates all Media Attachments for a Post.
239 + * Get enclosures for a post.
136 240 *
137 - * @return array The Attachments.
241 + * @param array $media The media array grouped by type.
242 + *
243 + * @return array The media array extended with enclosures.
138 244 */
139 - protected function get_attachment() {
140 - // Once upon a time we only supported images, but we now support audio/video as well.
141 - // We maintain the image-centric naming for backwards compatibility.
142 - $max_media = intval( \apply_filters( 'activitypub_max_image_attachments', \get_option( 'activitypub_max_image_attachments', ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS ) ) );
245 + public function get_enclosures( $media ) {
246 + $enclosures = get_enclosures( $this->wp_object->ID );
143 247
144 - if ( site_supports_blocks() && \has_blocks( $this->wp_object->post_content ) ) {
145 - return $this->get_block_attachments( $max_media );
248 + if ( ! $enclosures ) {
249 + return $media;
146 250 }
147 251
148 - return $this->get_classic_editor_images( $max_media );
252 + foreach ( $enclosures as $enclosure ) {
253 + // check if URL is an attachment
254 + $attachment_id = \attachment_url_to_postid( $enclosure['url'] );
255 + if ( $attachment_id ) {
256 + $enclosure['id'] = $attachment_id;
257 + $enclosure['url'] = \wp_get_attachment_url( $attachment_id );
258 + $enclosure['mediaType'] = \get_post_mime_type( $attachment_id );
259 + }
260 +
261 + $mime_type = $enclosure['mediaType'];
262 + $mime_type_parts = \explode( '/', $mime_type );
263 +
264 + switch ( $mime_type_parts[0] ) {
265 + case 'image':
266 + $media['image'][] = $enclosure;
267 + break;
268 + case 'audio':
269 + $media['audio'][] = $enclosure;
270 + break;
271 + case 'video':
272 + $media['video'][] = $enclosure;
273 + break;
274 + }
275 + }
276 +
277 + return $media;
149 278 }
150 279
151 280 /**
152 281 * Get media attachments from blocks. They will be formatted as ActivityPub attachments, not as WP attachments.
153 282 *
154 - * @param int $max_media The maximum number of attachments to return.
283 + * @param array $media The media array grouped by type.
284 + * @param int $max_media The maximum number of attachments to return.
155 285 *
156 286 * @return array The attachments.
157 287 */
158 - protected function get_block_attachments( $max_media ) {
288 + protected function get_block_attachments( $media, $max_media ) {
159 289 // max media can't be negative or zero
160 290 if ( $max_media <= 0 ) {
161 291 return array();
162 292 }
163 293
164 - $id = $this->wp_object->ID;
294 + $blocks = \parse_blocks( $this->wp_object->post_content );
295 + $media = self::get_media_from_blocks( $blocks, $media );
165 296
166 - $media_ids = array();
297 + return $media;
298 + }
167 299
168 - // list post thumbnail first if this post has one
169 - if ( \function_exists( 'has_post_thumbnail' ) && \has_post_thumbnail( $id ) ) {
170 - $media_ids[] = \get_post_thumbnail_id( $id );
171 - }
300 + /**
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.
305 + *
306 + * @return array The image IDs.
307 + */
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 + }
172 314
173 - if ( $max_media > 0 ) {
174 - $blocks = \parse_blocks( $this->wp_object->post_content );
175 - $media_ids = self::get_media_ids_from_blocks( $blocks, $media_ids, $max_media );
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 );
321 +
322 + if ( $check ) {
323 + $alt = $match[2];
324 + }
325 +
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 + }
176 366 }
177 367
178 - return \array_filter( \array_map( array( self::class, 'wp_attachment_to_activity_attachment' ), $media_ids ) );
368 + return $media;
179 369 }
180 370
181 371 /**
182 - * Get image attachments from the classic editor.
183 - * This is imperfect as the contained images aren't necessarily the
184 - * same as the attachments.
372 + * Get post images from the classic editor.
373 + * Note that audio/video attachments are only supported in the block editor.
185 374 *
186 - * @param int $max_images The maximum number of images to return.
375 + * @param array $media The media array grouped by type.
376 + * @param int $max_images The maximum number of images to return.
187 377 *
188 - * @return array The attachment IDs.
378 + * @return array The attachments.
189 379 */
190 - protected function get_classic_editor_image_attachments( $max_images ) {
380 + protected function get_classic_editor_images( $media, $max_images ) {
191 381 // max images can't be negative or zero
192 382 if ( $max_images <= 0 ) {
193 383 return array();
194 384 }
195 - $image_ids = array();
196 - $query = new \WP_Query(
197 - array(
198 - 'post_parent' => $this->wp_object->ID,
199 - 'post_status' => 'inherit',
200 - 'post_type' => 'attachment',
201 - 'post_mime_type' => 'image',
202 - 'order' => 'ASC',
203 - 'orderby' => 'menu_order ID',
204 - 'posts_per_page' => $max_images,
205 - )
206 - );
207 - foreach ( $query->get_posts() as $attachment ) {
208 - if ( ! \in_array( $attachment->ID, $image_ids, true ) ) {
209 - $image_ids[] = $attachment->ID;
385 +
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 ) );
210 391 }
211 392 }
212 - return $image_ids;
393 +
394 + return $media;
213 395 }
214 396
215 397 /**
216 398 * Get image embeds from the classic editor by parsing HTML.
@@ -216,9 +398,9 @@
216 398 * Get image embeds from the classic editor by parsing HTML.
217 399 *
218 400 * @param int $max_images The maximum number of images to return.
219 401 *
220 - * @return array The attachment IDs.
402 + * @return array The attachments.
221 403 */
222 404 protected function get_classic_editor_image_embeds( $max_images ) {
223 405 // if someone calls that function directly, bail
224 406 if ( ! \class_exists( '\WP_HTML_Tag_Processor' ) ) {
@@ -229,17 +411,17 @@
229 411 if ( $max_images <= 0 ) {
230 412 return array();
231 413 }
232 414
233 - $image_ids = array();
234 - $base = \wp_get_upload_dir()['baseurl'];
235 - $content = \get_post_field( 'post_content', $this->wp_object );
236 - $tags = new \WP_HTML_Tag_Processor( $content );
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 );
237 419
238 420 // This linter warning is a false positive - we have to
239 - // re-count each time here as we modify $image_ids.
421 + // re-count each time here as we modify $images.
240 422 // phpcs:ignore Squiz.PHP.DisallowSizeFunctionsInLoops.Found
241 - while ( $tags->next_tag( 'img' ) && ( \count( $image_ids ) < $max_images ) ) {
423 + while ( $tags->next_tag( 'img' ) && ( \count( $images ) <= $max_images ) ) {
242 424 $src = $tags->get_attribute( 'src' );
243 425
244 426 // If the img source is in our uploads dir, get the
245 427 // associated ID. Note: if there's a -500x500
@@ -265,130 +447,101 @@
265 447 $img_id = \attachment_url_to_postid( $src );
266 448 }
267 449
268 450 if ( 0 !== $img_id ) {
269 - if ( ! \in_array( $img_id, $image_ids, true ) ) {
270 - $image_ids[] = $img_id;
271 - }
451 + $images[] = array(
452 + 'id' => $img_id,
453 + 'alt' => $tags->get_attribute( 'alt' ),
454 + );
272 455 }
273 456 }
274 457 }
275 - return $image_ids;
458 +
459 + return $images;
276 460 }
277 461
278 462 /**
279 - * Get post images from the classic editor.
280 - * Note that audio/video attachments are only supported in the block editor.
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.
281 466 *
282 467 * @param int $max_images The maximum number of images to return.
283 468 *
284 - * @return array The attachments.
469 + * @return array The attachment IDs.
285 470 */
286 - protected function get_classic_editor_images( $max_images ) {
471 + protected function get_classic_editor_image_attachments( $max_images ) {
287 472 // max images can't be negative or zero
288 473 if ( $max_images <= 0 ) {
289 474 return array();
290 475 }
291 476
292 - $id = $this->wp_object->ID;
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 + );
293 489
294 - $image_ids = array();
295 -
296 - // list post thumbnail first if this post has one
297 - if ( \function_exists( 'has_post_thumbnail' ) && \has_post_thumbnail( $id ) ) {
298 - $image_ids[] = \get_post_thumbnail_id( $id );
299 - }
300 -
301 - if ( \count( $image_ids ) < $max_images ) {
302 - if ( \class_exists( '\WP_HTML_Tag_Processor' ) ) {
303 - $image_ids = \array_merge( $image_ids, $this->get_classic_editor_image_embeds( $max_images ) );
304 - } else {
305 - $image_ids = \array_merge( $image_ids, $this->get_classic_editor_image_attachments( $max_images ) );
490 + foreach ( $query->get_posts() as $attachment ) {
491 + if ( ! \in_array( $attachment->ID, $images, true ) ) {
492 + $images[] = array( 'id' => $attachment->ID );
306 493 }
307 494 }
308 - // unique then slice as the thumbnail may duplicate another image
309 - $image_ids = \array_slice( \array_unique( $image_ids ), 0, $max_images );
310 495
311 - return \array_filter( \array_map( array( self::class, 'wp_attachment_to_activity_attachment' ), $image_ids ) );
496 + return $images;
312 497 }
313 498
314 499 /**
315 - * Recursively get media IDs from blocks.
316 - * @param array $blocks The blocks to search for media IDs
317 - * @param array $media_ids The media IDs to append new IDs to
318 - * @param int $max_media The maximum number of media to return.
500 + * Filter media IDs by object type.
319 501 *
320 - * @return array The image IDs.
502 + * @param array $media The media array grouped by type.
503 + * @param string $type The object type.
504 + *
505 + * @return array The filtered media IDs.
321 506 */
322 - protected static function get_media_ids_from_blocks( $blocks, $media_ids, $max_media ) {
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 );
323 509
324 - foreach ( $blocks as $block ) {
325 - // recurse into inner blocks
326 - if ( ! empty( $block['innerBlocks'] ) ) {
327 - $media_ids = self::get_media_ids_from_blocks( $block['innerBlocks'], $media_ids, $max_media );
328 - }
329 -
330 - switch ( $block['blockName'] ) {
331 - case 'core/image':
332 - case 'core/cover':
333 - case 'core/audio':
334 - case 'core/video':
335 - case 'videopress/video':
336 - if ( ! empty( $block['attrs']['id'] ) ) {
337 - $media_ids[] = $block['attrs']['id'];
338 - }
339 - break;
340 - case 'jetpack/slideshow':
341 - case 'jetpack/tiled-gallery':
342 - if ( ! empty( $block['attrs']['ids'] ) ) {
343 - $media_ids = array_merge( $media_ids, $block['attrs']['ids'] );
344 - }
345 - break;
346 - case 'jetpack/image-compare':
347 - if ( ! empty( $block['attrs']['beforeImageId'] ) ) {
348 - $media_ids[] = $block['attrs']['beforeImageId'];
349 - }
350 - if ( ! empty( $block['attrs']['afterImageId'] ) ) {
351 - $media_ids[] = $block['attrs']['afterImageId'];
352 - }
353 - break;
354 - }
355 -
356 - // depupe
357 - $media_ids = \array_unique( $media_ids );
358 -
359 - // stop doing unneeded work
360 - if ( count( $media_ids ) >= $max_media ) {
361 - break;
362 - }
510 + if ( ! empty( $media[ $type ] ) ) {
511 + return $media[ $type ];
363 512 }
364 513
365 - // still need to slice it because one gallery could knock us over the limit
366 - return array_slice( $media_ids, 0, $max_media );
514 + return array_filter( array_merge( ...array_values( $media ) ) );
367 515 }
368 516
369 517 /**
370 518 * Converts a WordPress Attachment to an ActivityPub Attachment.
371 519 *
372 - * @param int $id The Attachment ID.
520 + * @param array $media The Attachment array.
373 521 *
374 522 * @return array The ActivityPub Attachment.
375 523 */
376 - public static function wp_attachment_to_activity_attachment( $id ) {
377 - $attachment = array();
378 - $mime_type = \get_post_mime_type( $id );
524 + public static function wp_attachment_to_activity_attachment( $media ) {
525 + if ( ! isset( $media['id'] ) ) {
526 + return $media;
527 + }
528 +
529 + $id = $media['id'];
530 + $attachment = array();
531 + $mime_type = \get_post_mime_type( $id );
379 532 $mime_type_parts = \explode( '/', $mime_type );
380 533 // switching on image/audio/video
381 534 switch ( $mime_type_parts[0] ) {
382 535 case 'image':
383 - $image_size = 'full';
536 + $image_size = 'large';
384 537
385 538 /**
386 539 * Filter the image URL returned for each post.
387 540 *
388 - * @param array|false $thumbnail The image URL, or false if no image is available.
389 - * @param int $id The attachment ID.
390 - * @param string $image_size The image size to retrieve. Set to 'full' by default.
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.
391 544 */
392 545 $thumbnail = apply_filters(
393 546 'activitypub_get_image',
394 547 self::get_wordpress_attachment( $id, $image_size ),
@@ -396,18 +549,23 @@
396 549 $image_size
397 550 );
398 551
399 552 if ( $thumbnail ) {
400 - $alt = \get_post_meta( $id, '_wp_attachment_image_alt', true );
401 553 $image = array(
402 554 'type' => 'Image',
403 - 'url' => $thumbnail[0],
404 - 'mediaType' => $mime_type,
555 + 'url' => \esc_url( $thumbnail[0] ),
556 + 'mediaType' => \esc_attr( $mime_type ),
405 557 );
406 558
407 - if ( $alt ) {
408 - $image['name'] = $alt;
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 + }
409 566 }
567 +
410 568 $attachment = $image;
411 569 }
412 570 break;
413 571
@@ -414,17 +572,17 @@
414 572 case 'audio':
415 573 case 'video':
416 574 $attachment = array(
417 575 'type' => 'Document',
418 - 'mediaType' => $mime_type,
419 - 'url' => \wp_get_attachment_url( $id ),
420 - 'name' => \get_the_title( $id ),
576 + 'mediaType' => \esc_attr( $mime_type ),
577 + 'url' => \esc_url( \wp_get_attachment_url( $id ) ),
578 + 'name' => \esc_attr( \get_the_title( $id ) ),
421 579 );
422 580 $meta = wp_get_attachment_metadata( $id );
423 581 // height and width for videos
424 582 if ( isset( $meta['width'] ) && isset( $meta['height'] ) ) {
425 - $attachment['width'] = $meta['width'];
426 - $attachment['height'] = $meta['height'];
583 + $attachment['width'] = \esc_attr( $meta['width'] );
584 + $attachment['height'] = \esc_attr( $meta['height'] );
427 585 }
428 586 // @todo: add `icon` support for audio/video attachments. Maybe use post thumbnail?
429 587 break;
430 588 }
@@ -435,18 +593,18 @@
435 593 /**
436 594 * Return details about an image attachment.
437 595 *
438 596 * @param int $id The attachment ID.
439 - * @param string $image_size The image size to retrieve. Set to 'full' by default.
597 + * @param string $image_size The image size to retrieve. Set to 'large' by default.
440 598 *
441 599 * @return array|false Array of image data, or boolean false if no image is available.
442 600 */
443 - protected static function get_wordpress_attachment( $id, $image_size = 'full' ) {
601 + protected static function get_wordpress_attachment( $id, $image_size = 'large' ) {
444 602 /**
445 603 * Hook into the image retrieval process. Before image retrieval.
446 604 *
447 605 * @param int $id The attachment ID.
448 - * @param string $image_size The image size to retrieve. Set to 'full' by default.
606 + * @param string $image_size The image size to retrieve. Set to 'large' by default.
449 607 */
450 608 do_action( 'activitypub_get_image_pre', $id, $image_size );
451 609
452 610 $image = \wp_get_attachment_image_src( $id, $image_size );
@@ -454,9 +612,9 @@
454 612 /**
455 613 * Hook into the image retrieval process. After image retrieval.
456 614 *
457 615 * @param int $id The attachment ID.
458 - * @param string $image_size The image size to retrieve. Set to 'full' by default.
616 + * @param string $image_size The image size to retrieve. Set to 'large' by default.
459 617 */
460 618 do_action( 'activitypub_get_image_post', $id, $image_size );
461 619
462 620 return $image;
@@ -470,37 +628,38 @@
470 628 *
471 629 * @return string The Object-Type.
472 630 */
473 631 protected function get_type() {
474 - if ( 'wordpress-post-format' !== \get_option( 'activitypub_object_type', 'note' ) ) {
475 - return \ucfirst( \get_option( 'activitypub_object_type', 'note' ) );
632 + $post_format_setting = \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE );
633 +
634 + if ( 'wordpress-post-format' !== $post_format_setting ) {
635 + return \ucfirst( $post_format_setting );
476 636 }
477 637
638 + $has_title = post_type_supports( $this->wp_object->post_type, 'title' );
639 +
640 + if ( ! $has_title ) {
641 + return 'Note';
642 + }
643 +
478 644 // Default to Article.
479 645 $object_type = 'Article';
646 + $post_format = 'standard';
647 +
648 + if ( \get_theme_support( 'post-formats' ) ) {
649 + $post_format = \get_post_format( $this->wp_object );
650 + }
651 +
480 652 $post_type = \get_post_type( $this->wp_object );
481 653 switch ( $post_type ) {
482 654 case 'post':
483 - $post_format = \get_post_format( $this->wp_object );
484 655 switch ( $post_format ) {
485 - case 'aside':
486 - case 'status':
487 - case 'quote':
488 - case 'note':
489 - $object_type = 'Note';
656 + case 'standard':
657 + case '':
658 + $object_type = 'Article';
490 659 break;
491 - case 'gallery':
492 - case 'image':
493 - $object_type = 'Image';
494 - break;
495 - case 'video':
496 - $object_type = 'Video';
497 - break;
498 - case 'audio':
499 - $object_type = 'Audio';
500 - break;
501 660 default:
502 - $object_type = 'Article';
661 + $object_type = 'Note';
503 662 break;
504 663 }
505 664 break;
506 665 case 'page':
@@ -505,23 +664,8 @@
505 664 break;
506 665 case 'page':
507 666 $object_type = 'Page';
508 667 break;
509 - case 'attachment':
510 - $mime_type = \get_post_mime_type();
511 - $media_type = \preg_replace( '/(\/[a-zA-Z]+)/i', '', $mime_type );
512 - switch ( $media_type ) {
513 - case 'audio':
514 - $object_type = 'Audio';
515 - break;
516 - case 'video':
517 - $object_type = 'Video';
518 - break;
519 - case 'image':
520 - $object_type = 'Image';
521 - break;
522 - }
523 - break;
524 668 default:
525 669 $object_type = 'Article';
526 670 break;
527 671 }
@@ -548,8 +692,18 @@
548 692
549 693 return $cc;
550 694 }
551 695
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 + }
704 + }
705 +
552 706 /**
553 707 * Returns a list of Tags, used in the Post.
554 708 *
555 709 * This includes Hash-Tags and Mentions.
@@ -586,8 +740,55 @@
586 740 return $tags;
587 741 }
588 742
589 743 /**
744 + * Returns the summary for the ActivityPub Item.
745 + *
746 + * The summary will be generated based on the user settings and only if the
747 + * object type is not set to `note`.
748 + *
749 + * @return string|null The summary or null if the object type is `note`.
750 + */
751 + protected function get_summary() {
752 + if ( 'Note' === $this->get_type() ) {
753 + return null;
754 + }
755 +
756 + // Remove Teaser from drafts.
757 + if ( 'draft' === \get_post_status( $this->wp_object ) ) {
758 + return \__( '(This post is being modified)', 'activitypub' );
759 + }
760 +
761 + return generate_post_summary( $this->wp_object );
762 + }
763 +
764 + /**
765 + * Returns the title for the ActivityPub Item.
766 + *
767 + * The title will be generated based on the user settings and only if the
768 + * object type is not set to `note`.
769 + *
770 + * @return string|null The title or null if the object type is `note`.
771 + */
772 + protected function get_name() {
773 + if ( 'Note' === $this->get_type() ) {
774 + return null;
775 + }
776 +
777 + $title = \get_the_title( $this->wp_object->ID );
778 +
779 + if ( $title ) {
780 + return \wp_strip_all_tags(
781 + \html_entity_decode(
782 + $title
783 + )
784 + );
785 + }
786 +
787 + return null;
788 + }
789 +
790 + /**
590 791 * Returns the content for the ActivityPub Item.
591 792 *
592 793 * The content will be generated based on the user settings.
593 794 *
@@ -593,8 +794,15 @@
593 794 *
594 795 * @return string The content.
595 796 */
596 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 +
597 805 global $post;
598 806
599 807 /**
600 808 * Provides an action hook so plugins can add their own hooks/filters before AP content is generated.
@@ -604,8 +812,10 @@
604 812 * @param WP_Post $post The post object.
605 813 */
606 814 do_action( 'activitypub_before_get_content', $post );
607 815
816 + add_filter( 'render_block_core/embed', array( self::class, 'revert_embed_links' ), 10, 2 );
817 +
608 818 // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
609 819 $post = $this->wp_object;
610 820 $content = $this->get_post_content_template();
611 821
@@ -640,18 +850,25 @@
640 850 case 'excerpt':
641 851 $template = "[ap_excerpt]\n\n[ap_permalink type=\"html\"]";
642 852 break;
643 853 case 'title':
644 - $template = "[ap_title]\n\n[ap_permalink type=\"html\"]";
854 + $template = "<h2>[ap_title]</h2>\n\n[ap_permalink type=\"html\"]";
645 855 break;
646 856 case 'content':
647 857 $template = "[ap_content]\n\n[ap_permalink type=\"html\"]\n\n[ap_hashtags]";
648 858 break;
649 859 default:
650 - $template = \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT );
860 + // phpcs:ignore Universal.Operators.DisallowShortTernary.Found
861 + $template = \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT ) ?: ACTIVITYPUB_CUSTOM_POST_CONTENT;
651 862 break;
652 863 }
653 864
865 + $post_format_setting = \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE );
866 +
867 + if ( 'wordpress-post-format' === $post_format_setting ) {
868 + $template = '[ap_content]';
869 + }
870 +
654 871 return apply_filters( 'activitypub_object_content_template', $template, $this->wp_object );
655 872 }
656 873
657 874 /**
@@ -659,9 +876,14 @@
659 876 *
660 877 * @return array The list of @-Mentions.
661 878 */
662 879 protected function get_mentions() {
663 - return apply_filters( 'activitypub_extract_mentions', array(), $this->wp_object->post_content, $this->wp_object );
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 + );
664 886 }
665 887
666 888 /**
667 889 * Returns the locale of the post.
@@ -681,6 +903,43 @@
681 903 *
682 904 * @return string The filtered locale of the post.
683 905 */
684 906 return apply_filters( 'activitypub_post_locale', $lang, $post_id, $this->wp_object );
907 + }
908 +
909 + /**
910 + * Returns the in-reply-to URL of the post.
911 + *
912 + * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-inreplyto
913 + *
914 + * @return string|null The in-reply-to URL of the post.
915 + */
916 + public function get_in_reply_to() {
917 + $blocks = \parse_blocks( $this->wp_object->post_content );
918 +
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 + }
924 + }
925 +
926 + return null;
927 + }
928 +
929 + /**
930 + * Transform Embed blocks to block level link.
931 + *
932 + * Remote servers will simply drop iframe elements, rendering incomplete content.
933 + *
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
941 + */
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>';
685 944 }
686 945 }