| @@ -1,32 +1,21 @@ | ||
| 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\Collection\Actors; | |
| 12 | -use Activitypub\Collection\Interactions; | |
| 13 | -use Activitypub\Collection\Replies; | |
| 4 | +use WP_Post; | |
| 5 | +use Activitypub\Shortcodes; | |
| 14 | 6 | use Activitypub\Model\Blog; |
| 15 | -use Activitypub\Shortcodes; | |
| 7 | +use Activitypub\Transformer\Base; | |
| 8 | +use Activitypub\Collection\Users; | |
| 16 | 9 | |
| 17 | 10 | 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; | |
| 11 | +use function Activitypub\is_single_user; | |
| 21 | 12 | use function Activitypub\get_enclosures; |
| 22 | 13 | use function Activitypub\get_rest_url_by_path; |
| 23 | -use function Activitypub\is_post_publicly_queryable; | |
| 24 | -use function Activitypub\is_single_user; | |
| 25 | 14 | use function Activitypub\site_supports_blocks; |
| 26 | 15 | |
| 27 | 16 | /** |
| 28 | - * WordPress Post Transformer. | |
| 17 | + * WordPress Post Transformer | |
| 29 | 18 | * |
| 30 | 19 | * The Post Transformer is responsible for transforming a WP_Post object into different other |
| 31 | 20 | * Object-Types. |
| 32 | 21 | * |
| @@ -35,208 +24,71 @@ | ||
| 35 | 24 | * - Activitypub\Activity\Base_Object |
| 36 | 25 | */ |
| 37 | 26 | class Post extends Base { |
| 38 | 27 | /** |
| 39 | - * The User as Actor Object. | |
| 28 | + * Returns the ID of the WordPress Post. | |
| 40 | 29 | * |
| 41 | - * @var \Activitypub\Activity\Actor | |
| 30 | + * @return int The ID of the WordPress Post | |
| 42 | 31 | */ |
| 43 | - private $actor_object = null; | |
| 32 | + public function get_wp_user_id() { | |
| 33 | + return $this->wp_object->post_author; | |
| 34 | + } | |
| 44 | 35 | |
| 45 | 36 | /** |
| 46 | - * The content. | |
| 37 | + * Change the User-ID of the WordPress Post. | |
| 47 | 38 | * |
| 48 | - * @var string|false False indicates not yet computed. | |
| 39 | + * @return int The User-ID of the WordPress Post | |
| 49 | 40 | */ |
| 50 | - private $content = false; | |
| 41 | + public function change_wp_user_id( $user_id ) { | |
| 42 | + $this->wp_object->post_author = $user_id; | |
| 51 | 43 | |
| 52 | - /** | |
| 53 | - * The summary. | |
| 54 | - * | |
| 55 | - * @var string|null|false False indicates not yet computed. | |
| 56 | - */ | |
| 57 | - private $summary = false; | |
| 44 | + return $this; | |
| 45 | + } | |
| 58 | 46 | |
| 59 | 47 | /** |
| 60 | - * The tags. | |
| 48 | + * Transforms the WP_Post object to an ActivityPub Object | |
| 61 | 49 | * |
| 62 | - * @var array|false False indicates not yet computed. | |
| 63 | - */ | |
| 64 | - private $tags = false; | |
| 65 | - | |
| 66 | - /** | |
| 67 | - * The attachment. | |
| 50 | + * @see \Activitypub\Activity\Base_Object | |
| 68 | 51 | * |
| 69 | - * @var array|false False indicates not yet computed. | |
| 70 | - */ | |
| 71 | - private $attachment = false; | |
| 72 | - | |
| 73 | - /** | |
| 74 | - * The mentions. | |
| 75 | - * | |
| 76 | - * @var array|false False indicates not yet computed. | |
| 77 | - */ | |
| 78 | - private $mentions = false; | |
| 79 | - | |
| 80 | - /** | |
| 81 | - * The in_reply_to. | |
| 82 | - * | |
| 83 | - * @var string|array|null|false False indicates not yet computed. | |
| 84 | - */ | |
| 85 | - private $in_reply_to = false; | |
| 86 | - | |
| 87 | - /** | |
| 88 | - * Transforms the WP_Post object to an ActivityPub Object | |
| 89 | - * | |
| 90 | 52 | * @return \Activitypub\Activity\Base_Object The ActivityPub Object |
| 91 | 53 | */ |
| 92 | 54 | 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' ) ); | |
| 111 | - | |
| 112 | - return $tombstone; | |
| 113 | - } | |
| 114 | - | |
| 115 | - $post = $this->item; | |
| 55 | + $post = $this->wp_object; | |
| 116 | 56 | $object = parent::to_object(); |
| 117 | 57 | |
| 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 | - } | |
| 58 | + $published = \strtotime( $post->post_date_gmt ); | |
| 125 | 59 | |
| 126 | - return $object; | |
| 127 | - } | |
| 60 | + $object->set_published( \gmdate( 'Y-m-d\TH:i:s\Z', $published ) ); | |
| 128 | 61 | |
| 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() ); | |
| 62 | + $updated = \strtotime( $post->post_modified_gmt ); | |
| 144 | 63 | |
| 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 ) ); | |
| 64 | + if ( $updated > $published ) { | |
| 65 | + $object->set_updated( \gmdate( 'Y-m-d\TH:i:s\Z', $updated ) ); | |
| 148 | 66 | } |
| 149 | 67 | |
| 150 | - return $object; | |
| 151 | - } | |
| 68 | + $object->set_content_map( | |
| 69 | + array( | |
| 70 | + $this->get_locale() => $this->get_content(), | |
| 71 | + ) | |
| 72 | + ); | |
| 73 | + $path = sprintf( 'actors/%d/followers', intval( $post->post_author ) ); | |
| 152 | 74 | |
| 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(), | |
| 75 | + $object->set_to( | |
| 76 | + array( | |
| 77 | + 'https://www.w3.org/ns/activitystreams#Public', | |
| 78 | + get_rest_url_by_path( $path ), | |
| 79 | + ) | |
| 179 | 80 | ); |
| 180 | - } | |
| 181 | 81 | |
| 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; | |
| 82 | + return $object; | |
| 209 | 83 | } |
| 210 | 84 | |
| 211 | 85 | /** |
| 212 | 86 | * Returns the ID of the Post. |
| 213 | 87 | * |
| 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 | 88 | * @return string The Posts ID. |
| 229 | 89 | */ |
| 230 | 90 | 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 | 91 | return $this->get_url(); |
| 240 | 92 | } |
| 241 | 93 | |
| 242 | 94 | /** |
| @@ -244,28 +96,20 @@ | ||
| 244 | 96 | * |
| 245 | 97 | * @return string The Posts URL. |
| 246 | 98 | */ |
| 247 | 99 | public function get_url() { |
| 248 | - $post = $this->item; | |
| 100 | + $post = $this->wp_object; | |
| 249 | 101 | |
| 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; | |
| 102 | + if ( 'trash' === get_post_status( $post ) ) { | |
| 103 | + $permalink = \get_post_meta( $post->ID, 'activitypub_canonical_url', true ); | |
| 104 | + } elseif ( 'draft' === get_post_status( $post ) && get_sample_permalink( $post->ID ) ) { | |
| 105 | + $sample = get_sample_permalink( $post->ID ); | |
| 106 | + $permalink = str_replace( array( '%pagename%', '%postname%' ), $sample[1], $sample[0] ); | |
| 107 | + } else { | |
| 108 | + $permalink = \get_permalink( $post ); | |
| 265 | 109 | } |
| 266 | 110 | |
| 267 | - return \esc_url_raw( $permalink ); | |
| 111 | + return \esc_url( $permalink ); | |
| 268 | 112 | } |
| 269 | 113 | |
| 270 | 114 | /** |
| 271 | 115 | * Returns the User-URL of the Author of the Post. |
| @@ -274,583 +118,220 @@ | ||
| 274 | 118 | * |
| 275 | 119 | * @return string The User-URL. |
| 276 | 120 | */ |
| 277 | 121 | protected function get_attributed_to() { |
| 278 | - return $this->get_actor_object()->get_id(); | |
| 279 | - } | |
| 122 | + $blog_user = new Blog(); | |
| 280 | 123 | |
| 281 | - /** | |
| 282 | - * Returns the featured image as `Image`. | |
| 283 | - * | |
| 284 | - * @return array|null The Image or null if no image is available. | |
| 285 | - */ | |
| 286 | - protected function get_image() { | |
| 287 | - $post_id = $this->item->ID; | |
| 288 | - | |
| 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; | |
| 124 | + if ( is_single_user() ) { | |
| 125 | + return $blog_user->get_url(); | |
| 295 | 126 | } |
| 296 | 127 | |
| 297 | - $id = \get_post_thumbnail_id( $post_id ); | |
| 298 | - $image_size = 'large'; | |
| 128 | + $user = Users::get_by_id( $this->wp_object->post_author ); | |
| 299 | 129 | |
| 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; | |
| 130 | + if ( $user && ! is_wp_error( $user ) ) { | |
| 131 | + return $user->get_url(); | |
| 316 | 132 | } |
| 317 | 133 | |
| 318 | - $mime_type = \get_post_mime_type( $id ); | |
| 319 | - | |
| 320 | - $image = array( | |
| 321 | - 'type' => 'Image', | |
| 322 | - 'url' => \esc_url_raw( $thumbnail[0] ), | |
| 323 | - 'mediaType' => \esc_attr( $mime_type ), | |
| 324 | - ); | |
| 325 | - | |
| 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' ); | |
| 329 | - } | |
| 330 | - | |
| 331 | - return $image; | |
| 134 | + return $blog_user->get_url(); | |
| 332 | 135 | } |
| 333 | 136 | |
| 334 | 137 | /** |
| 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' ); | |
| 348 | - } | |
| 349 | - | |
| 350 | - if ( ! $id ) { | |
| 351 | - return null; | |
| 352 | - } | |
| 353 | - | |
| 354 | - $image_size = 'thumbnail'; | |
| 355 | - | |
| 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 | - ); | |
| 369 | - | |
| 370 | - if ( ! $thumbnail ) { | |
| 371 | - return null; | |
| 372 | - } | |
| 373 | - | |
| 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' ); | |
| 385 | - } | |
| 386 | - | |
| 387 | - return $image; | |
| 388 | - } | |
| 389 | - | |
| 390 | - /** | |
| 391 | 138 | * Generates all Media Attachments for a Post. |
| 392 | 139 | * |
| 393 | 140 | * @return array The Attachments. |
| 394 | 141 | */ |
| 395 | 142 | protected function get_attachment() { |
| 396 | - if ( false !== $this->attachment ) { | |
| 397 | - return $this->attachment; | |
| 143 | + // Remove attachments from drafts. | |
| 144 | + if ( 'draft' === \get_post_status( $this->wp_object ) ) { | |
| 145 | + return array(); | |
| 398 | 146 | } |
| 399 | 147 | |
| 400 | - $max_media = \get_post_meta( $this->item->ID, 'activitypub_max_image_attachments', true ); | |
| 148 | + // Once upon a time we only supported images, but we now support audio/video as well. | |
| 149 | + // We maintain the image-centric naming for backwards compatibility. | |
| 150 | + $max_media = \intval( | |
| 151 | + \apply_filters( | |
| 152 | + 'activitypub_max_image_attachments', | |
| 153 | + \get_option( 'activitypub_max_image_attachments', ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS ) | |
| 154 | + ) | |
| 155 | + ); | |
| 401 | 156 | |
| 402 | - if ( ! \is_numeric( $max_media ) ) { | |
| 403 | - $max_media = \get_option( 'activitypub_max_image_attachments', ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS ); | |
| 404 | - } | |
| 405 | - | |
| 406 | - /** | |
| 407 | - * Filters the maximum number of media attachments allowed in a post. | |
| 408 | - * | |
| 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. | |
| 414 | - */ | |
| 415 | - $max_media = (int) \apply_filters( 'activitypub_max_image_attachments', $max_media ); | |
| 416 | - | |
| 417 | - if ( 0 === $max_media ) { | |
| 418 | - $this->attachment = array(); | |
| 419 | - | |
| 420 | - return $this->attachment; | |
| 421 | - } | |
| 422 | - | |
| 423 | 157 | $media = array( |
| 424 | - 'image' => array(), | |
| 425 | 158 | 'audio' => array(), |
| 426 | 159 | 'video' => array(), |
| 160 | + 'image' => array(), | |
| 427 | 161 | ); |
| 428 | - $id = $this->item->ID; | |
| 162 | + $id = $this->wp_object->ID; | |
| 429 | 163 | |
| 430 | - // List post thumbnail first if this post has one. | |
| 431 | - if ( \has_post_thumbnail( $id ) ) { | |
| 164 | + // list post thumbnail first if this post has one | |
| 165 | + if ( \function_exists( 'has_post_thumbnail' ) && \has_post_thumbnail( $id ) ) { | |
| 432 | 166 | $media['image'][] = array( 'id' => \get_post_thumbnail_id( $id ) ); |
| 433 | 167 | } |
| 434 | 168 | |
| 435 | 169 | $media = $this->get_enclosures( $media ); |
| 436 | 170 | |
| 437 | - if ( site_supports_blocks() && \has_blocks( $this->item->post_content ) ) { | |
| 171 | + if ( site_supports_blocks() && \has_blocks( $this->wp_object->post_content ) ) { | |
| 438 | 172 | $media = $this->get_block_attachments( $media, $max_media ); |
| 439 | 173 | } else { |
| 440 | - $media = $this->parse_html_images( $media, $max_media, $this->item->post_content ); | |
| 174 | + $media = $this->get_classic_editor_images( $media, $max_media ); | |
| 441 | 175 | } |
| 442 | 176 | |
| 443 | - $media = $this->filter_media_by_object_type( $media, \get_post_format( $this->item ), $this->item ); | |
| 177 | + $media = self::filter_media_by_object_type( $media, \get_post_format( $this->wp_object ), $this->wp_object ); | |
| 178 | + $unique_ids = \array_unique( \array_column( $media, 'id' ) ); | |
| 179 | + $media = \array_intersect_key( $media, $unique_ids ); | |
| 180 | + $media = \array_slice( $media, 0, $max_media ); | |
| 444 | 181 | |
| 445 | - /** | |
| 446 | - * Filter the attachment IDs for a post. | |
| 447 | - * | |
| 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. | |
| 452 | - */ | |
| 453 | - $media = \apply_filters( 'activitypub_attachment_ids', $media, $this->item ); | |
| 454 | - | |
| 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; | |
| 182 | + return \array_filter( \array_map( array( self::class, 'wp_attachment_to_activity_attachment' ), $media ) ); | |
| 472 | 183 | } |
| 473 | 184 | |
| 474 | 185 | /** |
| 475 | - * Returns the ActivityStreams 2.0 Object-Type for a Post based on the | |
| 476 | - * settings and the Post-Type. | |
| 186 | + * Get media attachments from blocks. They will be formatted as ActivityPub attachments, not as WP attachments. | |
| 477 | 187 | * |
| 478 | - * @see https://www.w3.org/TR/activitystreams-vocabulary/#activity-types | |
| 188 | + * @param array $media The media array grouped by type. | |
| 189 | + * @param int $max_media The maximum number of attachments to return. | |
| 479 | 190 | * |
| 480 | - * @return string The Object-Type. | |
| 191 | + * @return array The attachments. | |
| 481 | 192 | */ |
| 482 | - protected function get_type() { | |
| 483 | - $post_format_setting = \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE ); | |
| 484 | - | |
| 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'; | |
| 193 | + protected function get_block_attachments( $media, $max_media ) { | |
| 194 | + // max media can't be negative or zero | |
| 195 | + if ( $max_media <= 0 ) { | |
| 196 | + return array(); | |
| 495 | 197 | } |
| 496 | 198 | |
| 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 ); | |
| 513 | - } | |
| 199 | + $blocks = \parse_blocks( $this->wp_object->post_content ); | |
| 200 | + $media = self::get_media_from_blocks( $blocks, $media ); | |
| 514 | 201 | |
| 515 | - /** | |
| 516 | - * Returns the Audience for the Post. | |
| 517 | - * | |
| 518 | - * @return string|null The audience. | |
| 519 | - */ | |
| 520 | - public function get_audience() { | |
| 521 | - $actor_mode = \get_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_MODE ); | |
| 522 | - | |
| 523 | - if ( ACTIVITYPUB_ACTOR_AND_BLOG_MODE === $actor_mode ) { | |
| 524 | - $blog = new Blog(); | |
| 525 | - return $blog->get_id(); | |
| 526 | - } | |
| 527 | - | |
| 528 | - return null; | |
| 202 | + return $media; | |
| 529 | 203 | } |
| 530 | 204 | |
| 531 | 205 | /** |
| 532 | - * Returns a list of Tags, used in the Post. | |
| 206 | + * Get image attachments from the classic editor. | |
| 207 | + * This is imperfect as the contained images aren't necessarily the | |
| 208 | + * same as the attachments. | |
| 533 | 209 | * |
| 534 | - * This includes Hash-Tags and Mentions. | |
| 210 | + * @param int $max_images The maximum number of images to return. | |
| 535 | 211 | * |
| 536 | - * @return array The list of Tags. | |
| 212 | + * @return array The attachment IDs. | |
| 537 | 213 | */ |
| 538 | - protected function get_tag() { | |
| 539 | - if ( false !== $this->tags ) { | |
| 540 | - return $this->tags; | |
| 214 | + protected function get_classic_editor_image_attachments( $max_images ) { | |
| 215 | + // max images can't be negative or zero | |
| 216 | + if ( $max_images <= 0 ) { | |
| 217 | + return array(); | |
| 541 | 218 | } |
| 542 | 219 | |
| 543 | - $tags = parent::get_tag(); | |
| 220 | + $images = array(); | |
| 221 | + $query = new \WP_Query( | |
| 222 | + array( | |
| 223 | + 'post_parent' => $this->wp_object->ID, | |
| 224 | + 'post_status' => 'inherit', | |
| 225 | + 'post_type' => 'attachment', | |
| 226 | + 'post_mime_type' => 'image', | |
| 227 | + 'order' => 'ASC', | |
| 228 | + 'orderby' => 'menu_order ID', | |
| 229 | + 'posts_per_page' => $max_images, | |
| 230 | + ) | |
| 231 | + ); | |
| 544 | 232 | |
| 545 | - $post_tags = \get_the_tags( $this->item->ID ); | |
| 546 | - if ( $post_tags ) { | |
| 547 | - foreach ( $post_tags as $post_tag ) { | |
| 548 | - // Tag can be empty. | |
| 549 | - if ( ! $post_tag ) { | |
| 550 | - continue; | |
| 551 | - } | |
| 552 | - | |
| 553 | - $tags[] = array( | |
| 554 | - 'type' => 'Hashtag', | |
| 555 | - 'href' => \esc_url_raw( \get_tag_link( $post_tag->term_id ) ), | |
| 556 | - 'name' => esc_hashtag( $post_tag->name ), | |
| 557 | - ); | |
| 233 | + foreach ( $query->get_posts() as $attachment ) { | |
| 234 | + if ( ! \in_array( $attachment->ID, $images, true ) ) { | |
| 235 | + $images[] = array( 'id' => $attachment->ID ); | |
| 558 | 236 | } |
| 559 | 237 | } |
| 560 | 238 | |
| 561 | - $this->tags = \array_unique( $tags, SORT_REGULAR ); | |
| 562 | - | |
| 563 | - return $this->tags; | |
| 239 | + return $images; | |
| 564 | 240 | } |
| 565 | 241 | |
| 566 | 242 | /** |
| 567 | - * Returns the summary for the ActivityPub Item. | |
| 243 | + * Get image embeds from the classic editor by parsing HTML. | |
| 568 | 244 | * |
| 569 | - * The summary will be generated based on the user settings and only if the | |
| 570 | - * object type is not set to `note`. | |
| 245 | + * @param int $max_images The maximum number of images to return. | |
| 571 | 246 | * |
| 572 | - * @return string|null The summary or null if the object type is `note`. | |
| 247 | + * @return array The attachments. | |
| 573 | 248 | */ |
| 574 | - protected function get_summary() { | |
| 575 | - if ( 'Note' === $this->get_type() ) { | |
| 576 | - return null; | |
| 249 | + protected function get_classic_editor_image_embeds( $max_images ) { | |
| 250 | + // if someone calls that function directly, bail | |
| 251 | + if ( ! \class_exists( '\WP_HTML_Tag_Processor' ) ) { | |
| 252 | + return array(); | |
| 577 | 253 | } |
| 578 | 254 | |
| 579 | - if ( false !== $this->summary ) { | |
| 580 | - return $this->summary; | |
| 255 | + // max images can't be negative or zero | |
| 256 | + if ( $max_images <= 0 ) { | |
| 257 | + return array(); | |
| 581 | 258 | } |
| 582 | 259 | |
| 583 | - $this->summary = generate_post_summary( $this->item ); | |
| 260 | + $images = array(); | |
| 261 | + $base = \wp_get_upload_dir()['baseurl']; | |
| 262 | + $content = \get_post_field( 'post_content', $this->wp_object ); | |
| 263 | + $tags = new \WP_HTML_Tag_Processor( $content ); | |
| 584 | 264 | |
| 585 | - return $this->summary; | |
| 586 | - } | |
| 265 | + // This linter warning is a false positive - we have to | |
| 266 | + // re-count each time here as we modify $images. | |
| 267 | + // phpcs:ignore Squiz.PHP.DisallowSizeFunctionsInLoops.Found | |
| 268 | + while ( $tags->next_tag( 'img' ) && ( \count( $images ) <= $max_images ) ) { | |
| 269 | + $src = $tags->get_attribute( 'src' ); | |
| 587 | 270 | |
| 588 | - /** | |
| 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 | - } | |
| 271 | + // If the img source is in our uploads dir, get the | |
| 272 | + // associated ID. Note: if there's a -500x500 | |
| 273 | + // type suffix, we remove it, but we try the original | |
| 274 | + // first in case the original image is actually called | |
| 275 | + // that. Likewise, we try adding the -scaled suffix for | |
| 276 | + // the case that this is a small version of an image | |
| 277 | + // that was big enough to get scaled down on upload: | |
| 278 | + // https://make.wordpress.org/core/2019/10/09/introducing-handling-of-big-images-in-wordpress-5-3/ | |
| 279 | + if ( null !== $src && \str_starts_with( $src, $base ) ) { | |
| 280 | + $img_id = \attachment_url_to_postid( $src ); | |
| 600 | 281 | |
| 601 | - $title = \get_the_title( $this->item->ID ); | |
| 282 | + if ( 0 === $img_id ) { | |
| 283 | + $count = 0; | |
| 284 | + $src = preg_replace( '/-(?:\d+x\d+)(\.[a-zA-Z]+)$/', '$1', $src, 1, $count ); | |
| 285 | + if ( $count > 0 ) { | |
| 286 | + $img_id = \attachment_url_to_postid( $src ); | |
| 287 | + } | |
| 288 | + } | |
| 602 | 289 | |
| 603 | - if ( ! $title ) { | |
| 604 | - return null; | |
| 605 | - } | |
| 290 | + if ( 0 === $img_id ) { | |
| 291 | + $src = preg_replace( '/(\.[a-zA-Z]+)$/', '-scaled$1', $src ); | |
| 292 | + $img_id = \attachment_url_to_postid( $src ); | |
| 293 | + } | |
| 606 | 294 | |
| 607 | - return \wp_strip_all_tags( | |
| 608 | - \html_entity_decode( | |
| 609 | - $title | |
| 610 | - ) | |
| 611 | - ); | |
| 612 | - } | |
| 613 | - | |
| 614 | - /** | |
| 615 | - * Returns the content for the ActivityPub Item. | |
| 616 | - * | |
| 617 | - * The content will be generated based on the user settings. | |
| 618 | - * | |
| 619 | - * @return string The content. | |
| 620 | - */ | |
| 621 | - protected function get_content() { | |
| 622 | - if ( false !== $this->content ) { | |
| 623 | - return $this->content; | |
| 624 | - } | |
| 625 | - | |
| 626 | - global $post; | |
| 627 | - | |
| 628 | - // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited | |
| 629 | - $post = $this->item; | |
| 630 | - $content = $this->get_post_content_template(); | |
| 631 | - | |
| 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 | - // Fill in the shortcodes. | |
| 649 | - \setup_postdata( $post ); | |
| 650 | - $content = \do_shortcode( $content ); | |
| 651 | - \wp_reset_postdata(); | |
| 652 | - | |
| 653 | - // Don't need these anymore, should never appear in a post. | |
| 654 | - Shortcodes::unregister(); | |
| 655 | - | |
| 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 ); | |
| 663 | - | |
| 664 | - return $this->content; | |
| 665 | - } | |
| 666 | - | |
| 667 | - /** | |
| 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']; | |
| 295 | + if ( 0 !== $img_id ) { | |
| 296 | + $images[] = array( | |
| 297 | + 'id' => $img_id, | |
| 298 | + 'alt' => $tags->get_attribute( 'alt' ), | |
| 299 | + ); | |
| 693 | 300 | } |
| 694 | 301 | } |
| 695 | 302 | } |
| 696 | 303 | |
| 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; | |
| 304 | + return $images; | |
| 712 | 305 | } |
| 713 | 306 | |
| 714 | 307 | /** |
| 715 | - * Returns the published date of the post. | |
| 308 | + * Get post images from the classic editor. | |
| 309 | + * Note that audio/video attachments are only supported in the block editor. | |
| 716 | 310 | * |
| 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. | |
| 311 | + * @param array $media The media array grouped by type. | |
| 312 | + * @param int $max_images The maximum number of images to return. | |
| 727 | 313 | * |
| 728 | - * @return string|null The updated date of the post. | |
| 314 | + * @return array The attachments. | |
| 729 | 315 | */ |
| 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 ); | |
| 316 | + protected function get_classic_editor_images( $media, $max_images ) { | |
| 317 | + // max images can't be negative or zero | |
| 318 | + if ( $max_images <= 0 ) { | |
| 319 | + return array(); | |
| 736 | 320 | } |
| 737 | 321 | |
| 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; | |
| 322 | + if ( \count( $media['image'] ) <= $max_images ) { | |
| 323 | + if ( \class_exists( '\WP_HTML_Tag_Processor' ) ) { | |
| 324 | + $media['image'] = \array_merge( $media['image'], $this->get_classic_editor_image_embeds( $max_images ) ); | |
| 325 | + } else { | |
| 326 | + $media['image'] = \array_merge( $media['image'], $this->get_classic_editor_image_attachments( $max_images ) ); | |
| 327 | + } | |
| 758 | 328 | } |
| 759 | 329 | |
| 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 ); | |
| 330 | + return $media; | |
| 790 | 331 | } |
| 791 | 332 | |
| 792 | 333 | /** |
| 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 | 334 | * Get enclosures for a post. |
| 854 | 335 | * |
| 855 | 336 | * @param array $media The media array grouped by type. |
| 856 | 337 | * |
| @@ -855,10 +336,10 @@ | ||
| 855 | 336 | * @param array $media The media array grouped by type. |
| 856 | 337 | * |
| 857 | 338 | * @return array The media array extended with enclosures. |
| 858 | 339 | */ |
| 859 | - protected function get_enclosures( $media ) { | |
| 860 | - $enclosures = get_enclosures( $this->item->ID ); | |
| 340 | + public function get_enclosures( $media ) { | |
| 341 | + $enclosures = get_enclosures( $this->wp_object->ID ); | |
| 861 | 342 | |
| 862 | 343 | if ( ! $enclosures ) { |
| 863 | 344 | return $media; |
| 864 | 345 | } |
| @@ -863,22 +344,20 @@ | ||
| 863 | 344 | return $media; |
| 864 | 345 | } |
| 865 | 346 | |
| 866 | 347 | foreach ( $enclosures as $enclosure ) { |
| 867 | - // Check if URL is an attachment. | |
| 348 | + // check if URL is an attachment | |
| 868 | 349 | $attachment_id = \attachment_url_to_postid( $enclosure['url'] ); |
| 869 | - | |
| 870 | 350 | if ( $attachment_id ) { |
| 871 | - $enclosure['id'] = $attachment_id; | |
| 872 | - $enclosure['url'] = \wp_get_attachment_url( $attachment_id ); | |
| 351 | + $enclosure['id'] = $attachment_id; | |
| 352 | + $enclosure['url'] = \wp_get_attachment_url( $attachment_id ); | |
| 873 | 353 | $enclosure['mediaType'] = \get_post_mime_type( $attachment_id ); |
| 874 | 354 | } |
| 875 | 355 | |
| 876 | - $mime_type = $enclosure['mediaType']; | |
| 877 | - $media_type = \strtok( $mime_type, '/' ); | |
| 878 | - $enclosure['type'] = \ucfirst( $media_type ); | |
| 356 | + $mime_type = $enclosure['mediaType']; | |
| 357 | + $mime_type_parts = \explode( '/', $mime_type ); | |
| 879 | 358 | |
| 880 | - switch ( $media_type ) { | |
| 359 | + switch ( $mime_type_parts[0] ) { | |
| 881 | 360 | case 'image': |
| 882 | 361 | $media['image'][] = $enclosure; |
| 883 | 362 | break; |
| 884 | 363 | case 'audio': |
| @@ -893,39 +372,20 @@ | ||
| 893 | 372 | return $media; |
| 894 | 373 | } |
| 895 | 374 | |
| 896 | 375 | /** |
| 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 | 376 | * Recursively get media IDs from blocks. |
| 377 | + * @param array $blocks The blocks to search for media IDs | |
| 378 | + * @param array $media The media IDs to append new IDs to | |
| 379 | + * @param int $max_media The maximum number of media to return. | |
| 917 | 380 | * |
| 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 | 381 | * @return array The image IDs. |
| 922 | 382 | */ |
| 923 | - protected function get_media_from_blocks( $blocks, $media ) { | |
| 383 | + protected static function get_media_from_blocks( $blocks, $media ) { | |
| 924 | 384 | foreach ( $blocks as $block ) { |
| 925 | - // Recurse into inner blocks. | |
| 385 | + // recurse into inner blocks | |
| 926 | 386 | if ( ! empty( $block['innerBlocks'] ) ) { |
| 927 | - $media = $this->get_media_from_blocks( $block['innerBlocks'], $media ); | |
| 387 | + $media = self::get_media_from_blocks( $block['innerBlocks'], $media ); | |
| 928 | 388 | } |
| 929 | 389 | |
| 930 | 390 | switch ( $block['blockName'] ) { |
| 931 | 391 | case 'core/image': |
| @@ -930,76 +390,21 @@ | ||
| 930 | 390 | switch ( $block['blockName'] ) { |
| 931 | 391 | case 'core/image': |
| 932 | 392 | case 'core/cover': |
| 933 | 393 | 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 | - } | |
| 394 | + $alt = ''; | |
| 395 | + $check = preg_match( '/<img.*?alt\s*=\s*([\"\'])(.*?)\1.*>/i', $block['innerHTML'], $match ); | |
| 939 | 396 | |
| 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 | - } | |
| 397 | + if ( $check ) { | |
| 398 | + $alt = $match[2]; | |
| 947 | 399 | } |
| 948 | 400 | |
| 949 | - if ( ! $found ) { | |
| 950 | - $media['image'][] = array( | |
| 951 | - 'id' => $block['attrs']['id'], | |
| 952 | - 'alt' => $alt, | |
| 953 | - ); | |
| 954 | - } | |
| 401 | + $media['image'][] = array( | |
| 402 | + 'id' => $block['attrs']['id'], | |
| 403 | + 'alt' => $alt, | |
| 404 | + ); | |
| 955 | 405 | } |
| 956 | 406 | 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 | 407 | case 'core/audio': |
| 1003 | 408 | if ( ! empty( $block['attrs']['id'] ) ) { |
| 1004 | 409 | $media['audio'][] = array( 'id' => $block['attrs']['id'] ); |
| 1005 | 410 | } |
| @@ -1006,29 +411,18 @@ | ||
| 1006 | 411 | break; |
| 1007 | 412 | case 'core/video': |
| 1008 | 413 | case 'videopress/video': |
| 1009 | 414 | 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; | |
| 415 | + $media['video'][] = array( 'id' => $block['attrs']['id'] ); | |
| 1022 | 416 | } |
| 1023 | 417 | break; |
| 1024 | 418 | case 'jetpack/slideshow': |
| 1025 | 419 | case 'jetpack/tiled-gallery': |
| 1026 | 420 | if ( ! empty( $block['attrs']['ids'] ) ) { |
| 1027 | - $media['image'] = \array_merge( | |
| 421 | + $media['image'] = array_merge( | |
| 1028 | 422 | $media['image'], |
| 1029 | - \array_map( | |
| 1030 | - static function ( $id ) { | |
| 423 | + array_map( | |
| 424 | + function ( $id ) { | |
| 1031 | 425 | return array( 'id' => $id ); |
| 1032 | 426 | }, |
| 1033 | 427 | $block['attrs']['ids'] |
| 1034 | 428 | ) |
| @@ -1051,195 +445,437 @@ | ||
| 1051 | 445 | |
| 1052 | 446 | /** |
| 1053 | 447 | * Filter media IDs by object type. |
| 1054 | 448 | * |
| 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. | |
| 449 | + * @param array $media The media array grouped by type. | |
| 450 | + * @param string $type The object type. | |
| 1058 | 451 | * |
| 1059 | 452 | * @return array The filtered media IDs. |
| 1060 | 453 | */ |
| 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 ); | |
| 454 | + protected static function filter_media_by_object_type( $media, $type, $wp_object ) { | |
| 455 | + $type = \apply_filters( 'filter_media_by_object_type', \strtolower( $type ), $wp_object ); | |
| 1071 | 456 | |
| 1072 | 457 | if ( ! empty( $media[ $type ] ) ) { |
| 1073 | 458 | return $media[ $type ]; |
| 1074 | 459 | } |
| 1075 | 460 | |
| 1076 | - return \array_filter( \array_merge( ...\array_values( $media ) ) ); | |
| 461 | + return array_filter( array_merge( array(), ...array_values( $media ) ) ); | |
| 1077 | 462 | } |
| 1078 | 463 | |
| 1079 | 464 | /** |
| 1080 | - * Get the context of the post. | |
| 465 | + * Converts a WordPress Attachment to an ActivityPub Attachment. | |
| 1081 | 466 | * |
| 1082 | - * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-context | |
| 467 | + * @param array $media The Attachment array. | |
| 1083 | 468 | * |
| 1084 | - * @return string The context of the post. | |
| 469 | + * @return array The ActivityPub Attachment. | |
| 1085 | 470 | */ |
| 1086 | - protected function get_context() { | |
| 1087 | - return get_rest_url_by_path( \sprintf( 'posts/%d/context', $this->item->ID ) ); | |
| 471 | + public static function wp_attachment_to_activity_attachment( $media ) { | |
| 472 | + if ( ! isset( $media['id'] ) ) { | |
| 473 | + return $media; | |
| 474 | + } | |
| 475 | + | |
| 476 | + $id = $media['id']; | |
| 477 | + $attachment = array(); | |
| 478 | + $mime_type = \get_post_mime_type( $id ); | |
| 479 | + $mime_type_parts = \explode( '/', $mime_type ); | |
| 480 | + // switching on image/audio/video | |
| 481 | + switch ( $mime_type_parts[0] ) { | |
| 482 | + case 'image': | |
| 483 | + $image_size = 'large'; | |
| 484 | + | |
| 485 | + /** | |
| 486 | + * Filter the image URL returned for each post. | |
| 487 | + * | |
| 488 | + * @param array|false $thumbnail The image URL, or false if no image is available. | |
| 489 | + * @param int $id The attachment ID. | |
| 490 | + * @param string $image_size The image size to retrieve. Set to 'large' by default. | |
| 491 | + */ | |
| 492 | + $thumbnail = apply_filters( | |
| 493 | + 'activitypub_get_image', | |
| 494 | + self::get_wordpress_attachment( $id, $image_size ), | |
| 495 | + $id, | |
| 496 | + $image_size | |
| 497 | + ); | |
| 498 | + | |
| 499 | + if ( $thumbnail ) { | |
| 500 | + $image = array( | |
| 501 | + 'type' => 'Image', | |
| 502 | + 'url' => \esc_url( $thumbnail[0] ), | |
| 503 | + 'mediaType' => \esc_attr( $mime_type ), | |
| 504 | + ); | |
| 505 | + | |
| 506 | + if ( ! empty( $media['alt'] ) ) { | |
| 507 | + $image['name'] = \wp_strip_all_tags( \html_entity_decode( $media['alt'] ) ); | |
| 508 | + } else { | |
| 509 | + $alt = \get_post_meta( $id, '_wp_attachment_image_alt', true ); | |
| 510 | + if ( $alt ) { | |
| 511 | + $image['name'] = \wp_strip_all_tags( \html_entity_decode( $alt ) ); | |
| 512 | + } | |
| 513 | + } | |
| 514 | + | |
| 515 | + $attachment = $image; | |
| 516 | + } | |
| 517 | + break; | |
| 518 | + | |
| 519 | + case 'audio': | |
| 520 | + case 'video': | |
| 521 | + $attachment = array( | |
| 522 | + 'type' => 'Document', | |
| 523 | + 'mediaType' => \esc_attr( $mime_type ), | |
| 524 | + 'url' => \esc_url( \wp_get_attachment_url( $id ) ), | |
| 525 | + 'name' => \esc_attr( \get_the_title( $id ) ), | |
| 526 | + ); | |
| 527 | + $meta = wp_get_attachment_metadata( $id ); | |
| 528 | + // height and width for videos | |
| 529 | + if ( isset( $meta['width'] ) && isset( $meta['height'] ) ) { | |
| 530 | + $attachment['width'] = \esc_attr( $meta['width'] ); | |
| 531 | + $attachment['height'] = \esc_attr( $meta['height'] ); | |
| 532 | + } | |
| 533 | + // @todo: add `icon` support for audio/video attachments. Maybe use post thumbnail? | |
| 534 | + break; | |
| 535 | + } | |
| 536 | + | |
| 537 | + return \apply_filters( 'activitypub_attachment', $attachment, $id ); | |
| 1088 | 538 | } |
| 1089 | 539 | |
| 1090 | 540 | /** |
| 1091 | - * Gets the template to use to generate the content of the activitypub item. | |
| 541 | + * Return details about an image attachment. | |
| 1092 | 542 | * |
| 1093 | - * @return string The Template. | |
| 543 | + * @param int $id The attachment ID. | |
| 544 | + * @param string $image_size The image size to retrieve. Set to 'large' by default. | |
| 545 | + * | |
| 546 | + * @return array|false Array of image data, or boolean false if no image is available. | |
| 1094 | 547 | */ |
| 1095 | - 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; | |
| 548 | + protected static function get_wordpress_attachment( $id, $image_size = 'large' ) { | |
| 549 | + /** | |
| 550 | + * Hook into the image retrieval process. Before image retrieval. | |
| 551 | + * | |
| 552 | + * @param int $id The attachment ID. | |
| 553 | + * @param string $image_size The image size to retrieve. Set to 'large' by default. | |
| 554 | + */ | |
| 555 | + do_action( 'activitypub_get_image_pre', $id, $image_size ); | |
| 1098 | 556 | |
| 557 | + $image = \wp_get_attachment_image_src( $id, $image_size ); | |
| 558 | + | |
| 559 | + /** | |
| 560 | + * Hook into the image retrieval process. After image retrieval. | |
| 561 | + * | |
| 562 | + * @param int $id The attachment ID. | |
| 563 | + * @param string $image_size The image size to retrieve. Set to 'large' by default. | |
| 564 | + */ | |
| 565 | + do_action( 'activitypub_get_image_post', $id, $image_size ); | |
| 566 | + | |
| 567 | + return $image; | |
| 568 | + } | |
| 569 | + | |
| 570 | + /** | |
| 571 | + * Returns the ActivityStreams 2.0 Object-Type for a Post based on the | |
| 572 | + * settings and the Post-Type. | |
| 573 | + * | |
| 574 | + * @see https://www.w3.org/TR/activitystreams-vocabulary/#activity-types | |
| 575 | + * | |
| 576 | + * @return string The Object-Type. | |
| 577 | + */ | |
| 578 | + protected function get_type() { | |
| 1099 | 579 | $post_format_setting = \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE ); |
| 1100 | - $type = $this->get_type(); | |
| 1101 | 580 | |
| 1102 | - if ( 'wordpress-post-format' === $post_format_setting ) { | |
| 1103 | - $template = ''; | |
| 581 | + if ( 'wordpress-post-format' !== $post_format_setting ) { | |
| 582 | + return \ucfirst( $post_format_setting ); | |
| 583 | + } | |
| 1104 | 584 | |
| 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 | - } | |
| 585 | + $has_title = post_type_supports( $this->wp_object->post_type, 'title' ); | |
| 1116 | 586 | |
| 1117 | - $template .= '[ap_content]'; | |
| 587 | + if ( ! $has_title ) { | |
| 588 | + return 'Note'; | |
| 1118 | 589 | } |
| 1119 | 590 | |
| 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 ); | |
| 591 | + // Default to Article. | |
| 592 | + $object_type = 'Note'; | |
| 593 | + $post_format = 'standard'; | |
| 594 | + | |
| 595 | + if ( \get_theme_support( 'post-formats' ) ) { | |
| 596 | + $post_format = \get_post_format( $this->wp_object ); | |
| 597 | + } | |
| 598 | + | |
| 599 | + $post_type = \get_post_type( $this->wp_object ); | |
| 600 | + switch ( $post_type ) { | |
| 601 | + case 'post': | |
| 602 | + switch ( $post_format ) { | |
| 603 | + case 'standard': | |
| 604 | + case '': | |
| 605 | + $object_type = 'Article'; | |
| 606 | + break; | |
| 607 | + default: | |
| 608 | + $object_type = 'Note'; | |
| 609 | + break; | |
| 610 | + } | |
| 611 | + break; | |
| 612 | + case 'page': | |
| 613 | + $object_type = 'Page'; | |
| 614 | + break; | |
| 615 | + default: | |
| 616 | + $object_type = 'Note'; | |
| 617 | + break; | |
| 618 | + } | |
| 619 | + | |
| 620 | + return $object_type; | |
| 1135 | 621 | } |
| 1136 | 622 | |
| 1137 | 623 | /** |
| 1138 | - * Get the replies Collection. | |
| 624 | + * Returns a list of Mentions, used in the Post. | |
| 1139 | 625 | * |
| 1140 | - * @return array|null The replies collection on success or null on failure. | |
| 626 | + * @see https://docs.joinmastodon.org/spec/activitypub/#Mention | |
| 627 | + * | |
| 628 | + * @return array The list of Mentions. | |
| 1141 | 629 | */ |
| 1142 | - public function get_replies() { | |
| 1143 | - return Replies::get_collection( $this->item ); | |
| 630 | + protected function get_cc() { | |
| 631 | + $cc = array(); | |
| 632 | + | |
| 633 | + $mentions = $this->get_mentions(); | |
| 634 | + if ( $mentions ) { | |
| 635 | + foreach ( $mentions as $url ) { | |
| 636 | + $cc[] = $url; | |
| 637 | + } | |
| 638 | + } | |
| 639 | + | |
| 640 | + return $cc; | |
| 1144 | 641 | } |
| 1145 | 642 | |
| 643 | + | |
| 644 | + public function get_audience() { | |
| 645 | + if ( is_single_user() ) { | |
| 646 | + return null; | |
| 647 | + } else { | |
| 648 | + $blog = new Blog(); | |
| 649 | + return $blog->get_id(); | |
| 650 | + } | |
| 651 | + } | |
| 652 | + | |
| 1146 | 653 | /** |
| 1147 | - * Get the likes Collection. | |
| 654 | + * Returns a list of Tags, used in the Post. | |
| 1148 | 655 | * |
| 1149 | - * @return array The likes collection. | |
| 656 | + * This includes Hash-Tags and Mentions. | |
| 657 | + * | |
| 658 | + * @return array The list of Tags. | |
| 1150 | 659 | */ |
| 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 | - ); | |
| 660 | + protected function get_tag() { | |
| 661 | + $tags = array(); | |
| 662 | + | |
| 663 | + $post_tags = \get_the_tags( $this->wp_object->ID ); | |
| 664 | + if ( $post_tags ) { | |
| 665 | + foreach ( $post_tags as $post_tag ) { | |
| 666 | + $tag = array( | |
| 667 | + 'type' => 'Hashtag', | |
| 668 | + 'href' => \esc_url( \get_tag_link( $post_tag->term_id ) ), | |
| 669 | + 'name' => esc_hashtag( $post_tag->name ), | |
| 670 | + ); | |
| 671 | + $tags[] = $tag; | |
| 672 | + } | |
| 673 | + } | |
| 674 | + | |
| 675 | + $mentions = $this->get_mentions(); | |
| 676 | + if ( $mentions ) { | |
| 677 | + foreach ( $mentions as $mention => $url ) { | |
| 678 | + $tag = array( | |
| 679 | + 'type' => 'Mention', | |
| 680 | + 'href' => \esc_url( $url ), | |
| 681 | + 'name' => \esc_html( $mention ), | |
| 682 | + ); | |
| 683 | + $tags[] = $tag; | |
| 684 | + } | |
| 685 | + } | |
| 686 | + | |
| 687 | + return $tags; | |
| 1157 | 688 | } |
| 1158 | 689 | |
| 1159 | 690 | /** |
| 1160 | - * Get the shares Collection. | |
| 691 | + * Returns the summary for the ActivityPub Item. | |
| 1161 | 692 | * |
| 1162 | - * @return array The Shares collection. | |
| 693 | + * The summary will be generated based on the user settings and only if the | |
| 694 | + * object type is not set to `note`. | |
| 695 | + * | |
| 696 | + * @return string|null The summary or null if the object type is `note`. | |
| 1163 | 697 | */ |
| 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 | - ); | |
| 698 | + protected function get_summary() { | |
| 699 | + if ( 'Note' === $this->get_type() ) { | |
| 700 | + return null; | |
| 701 | + } | |
| 702 | + | |
| 703 | + // Remove Teaser from drafts. | |
| 704 | + if ( 'draft' === \get_post_status( $this->wp_object ) ) { | |
| 705 | + return \__( '(This post is being modified)', 'activitypub' ); | |
| 706 | + } | |
| 707 | + | |
| 708 | + $content = \get_post_field( 'post_content', $this->wp_object->ID ); | |
| 709 | + $content = \html_entity_decode( $content ); | |
| 710 | + $content = \wp_strip_all_tags( $content ); | |
| 711 | + $content = \trim( $content ); | |
| 712 | + $content = \preg_replace( '/\R+/m', "\n\n", $content ); | |
| 713 | + $content = \preg_replace( '/[\r\t]/', '', $content ); | |
| 714 | + | |
| 715 | + $excerpt_more = \apply_filters( 'activitypub_excerpt_more', '[...]' ); | |
| 716 | + $length = 500; | |
| 717 | + $length = $length - strlen( $excerpt_more ); | |
| 718 | + | |
| 719 | + if ( \strlen( $content ) > $length ) { | |
| 720 | + $content = \wordwrap( $content, $length, '</activitypub-summary>' ); | |
| 721 | + $content = \explode( '</activitypub-summary>', $content, 2 ); | |
| 722 | + $content = $content[0]; | |
| 723 | + } | |
| 724 | + | |
| 725 | + return $content . ' ' . $excerpt_more; | |
| 1170 | 726 | } |
| 1171 | 727 | |
| 1172 | 728 | /** |
| 1173 | - * Get the preview of the post. | |
| 729 | + * Returns the title for the ActivityPub Item. | |
| 1174 | 730 | * |
| 1175 | - * @return array|null The preview of the post or null if the post is not an Article. | |
| 731 | + * The title will be generated based on the user settings and only if the | |
| 732 | + * object type is not set to `note`. | |
| 733 | + * | |
| 734 | + * @return string|null The title or null if the object type is `note`. | |
| 1176 | 735 | */ |
| 1177 | - public function get_preview() { | |
| 1178 | - if ( 'Article' !== $this->get_type() ) { | |
| 736 | + protected function get_name() { | |
| 737 | + if ( 'Note' === $this->get_type() ) { | |
| 1179 | 738 | return null; |
| 1180 | 739 | } |
| 1181 | 740 | |
| 1182 | - return array( | |
| 1183 | - 'type' => 'Note', | |
| 1184 | - 'content' => $this->get_summary(), | |
| 1185 | - ); | |
| 741 | + $title = \get_the_title( $this->wp_object->ID ); | |
| 742 | + | |
| 743 | + if ( $title ) { | |
| 744 | + return \wp_strip_all_tags( | |
| 745 | + \html_entity_decode( | |
| 746 | + $title | |
| 747 | + ) | |
| 748 | + ); | |
| 749 | + } | |
| 750 | + | |
| 751 | + return null; | |
| 1186 | 752 | } |
| 1187 | 753 | |
| 1188 | 754 | /** |
| 1189 | - * Get the quote policy. | |
| 755 | + * Returns the content for the ActivityPub Item. | |
| 1190 | 756 | * |
| 1191 | - * @return array The quote policy. | |
| 757 | + * The content will be generated based on the user settings. | |
| 758 | + * | |
| 759 | + * @return string The content. | |
| 1192 | 760 | */ |
| 1193 | - private function get_quote_policy() { | |
| 1194 | - $policy = \get_post_meta( $this->item->ID, 'activitypub_interaction_policy_quote', true ); | |
| 761 | + protected function get_content() { | |
| 762 | + // Remove Content from drafts. | |
| 763 | + if ( 'draft' === \get_post_status( $this->wp_object ) ) { | |
| 764 | + return \__( '(This post is being modified)', 'activitypub' ); | |
| 765 | + } | |
| 1195 | 766 | |
| 1196 | - // Fall back to global default if not set. | |
| 1197 | - if ( ! $policy ) { | |
| 1198 | - $policy = \get_option( 'activitypub_default_quote_policy', ACTIVITYPUB_INTERACTION_POLICY_ANYONE ); | |
| 1199 | - } | |
| 767 | + global $post; | |
| 1200 | 768 | |
| 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 ) ) ); | |
| 769 | + /** | |
| 770 | + * Provides an action hook so plugins can add their own hooks/filters before AP content is generated. | |
| 771 | + * | |
| 772 | + * 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. | |
| 773 | + * | |
| 774 | + * @param WP_Post $post The post object. | |
| 775 | + */ | |
| 776 | + do_action( 'activitypub_before_get_content', $post ); | |
| 1204 | 777 | |
| 1205 | - case ACTIVITYPUB_INTERACTION_POLICY_ME: | |
| 1206 | - return array( 'automaticApproval' => $this->get_self_interaction_policy() ); | |
| 778 | + add_filter( 'render_block_core/embed', array( self::class, 'revert_embed_links' ), 10, 2 ); | |
| 1207 | 779 | |
| 780 | + // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited | |
| 781 | + $post = $this->wp_object; | |
| 782 | + $content = $this->get_post_content_template(); | |
| 783 | + | |
| 784 | + // Register our shortcodes just in time. | |
| 785 | + Shortcodes::register(); | |
| 786 | + // Fill in the shortcodes. | |
| 787 | + setup_postdata( $post ); | |
| 788 | + $content = do_shortcode( $content ); | |
| 789 | + wp_reset_postdata(); | |
| 790 | + | |
| 791 | + $content = \wpautop( $content ); | |
| 792 | + $content = \preg_replace( '/[\n\r\t]/', '', $content ); | |
| 793 | + $content = \trim( $content ); | |
| 794 | + | |
| 795 | + $content = \apply_filters( 'activitypub_the_content', $content, $post ); | |
| 796 | + | |
| 797 | + // Don't need these any more, should never appear in a post. | |
| 798 | + Shortcodes::unregister(); | |
| 799 | + | |
| 800 | + return $content; | |
| 801 | + } | |
| 802 | + | |
| 803 | + /** | |
| 804 | + * Gets the template to use to generate the content of the activitypub item. | |
| 805 | + * | |
| 806 | + * @return string The Template. | |
| 807 | + */ | |
| 808 | + protected function get_post_content_template() { | |
| 809 | + $type = \get_option( 'activitypub_post_content_type', 'content' ); | |
| 810 | + | |
| 811 | + switch ( $type ) { | |
| 812 | + case 'excerpt': | |
| 813 | + $template = "[ap_excerpt]\n\n[ap_permalink type=\"html\"]"; | |
| 814 | + break; | |
| 815 | + case 'title': | |
| 816 | + $template = "<h2>[ap_title]</h2>\n\n[ap_permalink type=\"html\"]"; | |
| 817 | + break; | |
| 818 | + case 'content': | |
| 819 | + $template = "[ap_content]\n\n[ap_permalink type=\"html\"]\n\n[ap_hashtags]"; | |
| 820 | + break; | |
| 1208 | 821 | default: |
| 1209 | - return $this->get_public_interaction_policy(); | |
| 822 | + $template = \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT ); | |
| 823 | + break; | |
| 1210 | 824 | } |
| 825 | + | |
| 826 | + $post_format_setting = \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE ); | |
| 827 | + | |
| 828 | + if ( 'wordpress-post-format' === $post_format_setting ) { | |
| 829 | + $template = '[ap_content]'; | |
| 830 | + } | |
| 831 | + | |
| 832 | + return apply_filters( 'activitypub_object_content_template', $template, $this->wp_object ); | |
| 1211 | 833 | } |
| 1212 | 834 | |
| 1213 | 835 | /** |
| 1214 | - * Get the public interaction policy. | |
| 836 | + * Helper function to get the @-Mentions from the post content. | |
| 1215 | 837 | * |
| 1216 | - * @return array The public interaction policy. | |
| 838 | + * @return array The list of @-Mentions. | |
| 1217 | 839 | */ |
| 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 | - ); | |
| 840 | + protected function get_mentions() { | |
| 841 | + return apply_filters( 'activitypub_extract_mentions', array(), $this->wp_object->post_content, $this->wp_object ); | |
| 1223 | 842 | } |
| 1224 | 843 | |
| 1225 | 844 | /** |
| 1226 | - * Get the actor ID(s) for the `me` audience for use in interaction policies. | |
| 845 | + * Returns the locale of the post. | |
| 1227 | 846 | * |
| 1228 | - * @return string|array The actor ID(s). | |
| 847 | + * @return string The locale of the post. | |
| 1229 | 848 | */ |
| 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(); | |
| 849 | + public function get_locale() { | |
| 850 | + $post_id = $this->wp_object->ID; | |
| 851 | + $lang = \strtolower( \strtok( \get_locale(), '_-' ) ); | |
| 1234 | 852 | |
| 1235 | - case ACTIVITYPUB_ACTOR_AND_BLOG_MODE: | |
| 1236 | - return array( | |
| 1237 | - $this->get_actor_object()->get_id(), | |
| 1238 | - ( new Blog() )->get_id(), | |
| 1239 | - ); | |
| 853 | + /** | |
| 854 | + * Filter the locale of the post. | |
| 855 | + * | |
| 856 | + * @param string $lang The locale of the post. | |
| 857 | + * @param int $post_id The post ID. | |
| 858 | + * @param WP_Post $post The post object. | |
| 859 | + * | |
| 860 | + * @return string The filtered locale of the post. | |
| 861 | + */ | |
| 862 | + return apply_filters( 'activitypub_post_locale', $lang, $post_id, $this->wp_object ); | |
| 863 | + } | |
| 1240 | 864 | |
| 1241 | - default: | |
| 1242 | - return $this->get_actor_object()->get_id(); | |
| 1243 | - } | |
| 865 | + /** | |
| 866 | + * Transform Embed blocks to block level link. | |
| 867 | + * | |
| 868 | + * Remote servers will simply drop iframe elements, rendering incomplete content. | |
| 869 | + * | |
| 870 | + * @see https://www.w3.org/TR/activitypub/#security-sanitizing-content | |
| 871 | + * @see https://www.w3.org/wiki/ActivityPub/Primer/HTML | |
| 872 | + * | |
| 873 | + * @param string $block_content The block content (html) | |
| 874 | + * @param object $block The block object | |
| 875 | + * | |
| 876 | + * @return string A block level link | |
| 877 | + */ | |
| 878 | + public static function revert_embed_links( $block_content, $block ) { | |
| 879 | + return '<p><a href="' . esc_url( $block['attrs']['url'] ) . '">' . $block['attrs']['url'] . '</a></p>'; | |
| 1244 | 880 | } |
| 1245 | 881 | } |