item; $object = parent::to_object(); $content_warning = get_content_warning( $post ); if ( ! empty( $content_warning ) ) { $object->set_sensitive( true ); $object->set_summary( $content_warning ); $object->set_summary_map( null ); $object->set_dcterms( array( 'subject' => $content_warning ) ); } return $object; } /** * Returns a Tombstone object for the post. * * @return Base_Object The Tombstone object. */ public function to_tombstone() { $object = new Base_Object(); $object->set_type( 'Tombstone' ); $object->set_id( $this->get_id() ); $object->set_former_type( $this->get_type() ); $object->set_published( $this->get_published() ); $object->set_updated( $this->get_updated() ); $deleted_at = \get_post_meta( $this->item->ID, 'activitypub_deleted_at', true ); if ( $deleted_at ) { $object->set_deleted( \gmdate( ACTIVITYPUB_DATE_TIME_RFC3339, $deleted_at ) ); } return $object; } /** * Get the content visibility. * * @return string The content visibility. */ public function get_content_visibility() { if ( ! $this->content_visibility ) { return get_content_visibility( $this->item ); } return $this->content_visibility; } /** * Get the Interaction Policy. * * @see https://docs.gotosocial.org/en/latest/federation/interaction_policy/ * * @return array The interaction policy. */ public function get_interaction_policy() { return array( 'canAnnounce' => $this->get_public_interaction_policy(), 'canLike' => $this->get_public_interaction_policy(), 'canQuote' => $this->get_quote_policy(), 'canReply' => $this->get_public_interaction_policy(), ); } /** * Returns the User-Object of the Author of the Post. * * If `single_user` mode is enabled, the Blog-User is returned. * * @return \Activitypub\Activity\Actor The User-Object. */ public function get_actor_object() { if ( $this->actor_object ) { return $this->actor_object; } $blog_user = new Blog(); $this->actor_object = $blog_user; if ( is_single_user() ) { return $blog_user; } $user = Actors::get_by_id( $this->item->post_author ); if ( $user && ! is_wp_error( $user ) ) { $this->actor_object = $user; return $user; } return $blog_user; } /** * Returns the ID of the Post. * * @return string The Posts ID. */ public function get_id() { $last_legacy_id = (int) \get_option( 'activitypub_last_post_with_permalink_as_id', 0 ); $post_id = (int) $this->item->ID; if ( $post_id > $last_legacy_id ) { // Generate URI based on post ID. return \add_query_arg( 'p', $post_id, \home_url( '/' ) ); } return $this->get_url(); } /** * Returns the URL of the Post. * * @return string The Posts URL. */ public function get_url() { $post = $this->item; switch ( \get_post_status( $post ) ) { case 'trash': $permalink = \get_post_meta( $post->ID, '_activitypub_canonical_url', true ); break; case 'draft': // Get_sample_permalink is in wp-admin, not always loaded. if ( ! \function_exists( '\get_sample_permalink' ) ) { require_once ABSPATH . 'wp-admin/includes/post.php'; } $sample = \get_sample_permalink( $post->ID ); $permalink = \str_replace( array( '%pagename%', '%postname%' ), $sample[1], $sample[0] ); break; default: $permalink = \get_permalink( $post ); break; } return \esc_url( $permalink ); } /** * Returns the User-URL of the Author of the Post. * * If `single_user` mode is enabled, the URL of the Blog-User is returned. * * @return string The User-URL. */ protected function get_attributed_to() { return $this->get_actor_object()->get_id(); } /** * Returns the featured image as `Image`. * * @return array|null The Image or null if no image is available. */ protected function get_image() { $post_id = $this->item->ID; // List post thumbnail first if this post has one. if ( ! \function_exists( 'has_post_thumbnail' ) || ! \has_post_thumbnail( $post_id ) ) { return null; } $id = \get_post_thumbnail_id( $post_id ); $image_size = 'large'; /** * Filter the image URL returned for each post. * * @param array|false $thumbnail The image URL, or false if no image is available. * @param int $id The attachment ID. * @param string $image_size The image size to retrieve. Set to 'large' by default. */ $thumbnail = apply_filters( 'activitypub_get_image', $this->get_attachment_image_src( $id, $image_size ), $id, $image_size ); if ( ! $thumbnail ) { return null; } $mime_type = \get_post_mime_type( $id ); $image = array( 'type' => 'Image', 'url' => \esc_url( $thumbnail[0] ), 'mediaType' => \esc_attr( $mime_type ), ); $alt = \get_post_meta( $id, '_wp_attachment_image_alt', true ); if ( $alt ) { $image['name'] = \html_entity_decode( \wp_strip_all_tags( $alt ), ENT_QUOTES, 'UTF-8' ); } return $image; } /** * Returns an Icon, based on the Featured Image with a fallback to the site-icon. * * @return array|null The Icon or null if no icon is available. */ protected function get_icon() { $post_id = $this->item->ID; // List post thumbnail first if this post has one. if ( \has_post_thumbnail( $post_id ) ) { $id = \get_post_thumbnail_id( $post_id ); } else { // Try site_logo, falling back to site_icon, first. $id = get_option( 'site_icon' ); } if ( ! $id ) { return null; } $image_size = 'thumbnail'; /** * Filter the image URL returned for each post. * * @param array|false $thumbnail The image URL, or false if no image is available. * @param int $id The attachment ID. * @param string $image_size The image size to retrieve. Set to 'large' by default. */ $thumbnail = apply_filters( 'activitypub_get_image', $this->get_attachment_image_src( $id, $image_size ), $id, $image_size ); if ( ! $thumbnail ) { return null; } $mime_type = \get_post_mime_type( $id ); $image = array( 'type' => 'Image', 'url' => \esc_url( $thumbnail[0] ), 'mediaType' => \esc_attr( $mime_type ), ); $alt = \get_post_meta( $id, '_wp_attachment_image_alt', true ); if ( $alt ) { $image['name'] = \html_entity_decode( \wp_strip_all_tags( $alt ), ENT_QUOTES, 'UTF-8' ); } return $image; } /** * Generates all Media Attachments for a Post. * * @return array The Attachments. */ protected function get_attachment() { if ( false !== $this->attachment ) { return $this->attachment; } /* * Remove attachments from the Fediverse if a post was federated and then unpublished. * Except in preview mode, where we want to show attachments. */ if ( ! $this->is_preview() && 'publish' !== \get_post_status( $this->item ) ) { $this->attachment = array(); return $this->attachment; } $max_media = \get_post_meta( $this->item->ID, 'activitypub_max_image_attachments', true ); if ( ! is_numeric( $max_media ) ) { $max_media = \get_option( 'activitypub_max_image_attachments', ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS ); } /** * Filters the maximum number of media attachments allowed in a post. * * Despite the name suggesting only images, this filter controls the maximum number * of all media attachments (images, audio, and video) that can be included in an * ActivityPub post. The name is maintained for backwards compatibility. * * @param int $max_media Maximum number of media attachments. Default ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS. */ $max_media = (int) \apply_filters( 'activitypub_max_image_attachments', $max_media ); if ( 0 === $max_media ) { $this->attachment = array(); return $this->attachment; } $media = array( 'image' => array(), 'audio' => array(), 'video' => array(), ); $id = $this->item->ID; // List post thumbnail first if this post has one. if ( \has_post_thumbnail( $id ) ) { $media['image'][] = array( 'id' => \get_post_thumbnail_id( $id ) ); } $media = $this->get_enclosures( $media ); if ( site_supports_blocks() && \has_blocks( $this->item->post_content ) ) { $media = $this->get_block_attachments( $media, $max_media ); } else { $media = $this->parse_html_images( $media, $max_media, $this->item->post_content ); } $media = $this->filter_media_by_object_type( $media, \get_post_format( $this->item ), $this->item ); /** * Filter the attachment IDs for a post. * * @param array $media The media array grouped by type. * @param \WP_Post $item The post object. * * @return array The filtered attachment IDs. */ $media = \apply_filters( 'activitypub_attachment_ids', $media, $this->item ); // Deduplicate and limit after filter to ensure plugins adding attachments don't cause duplicates. $media = $this->filter_unique_attachments( $media ); $media = \array_slice( $media, 0, $max_media ); $attachments = \array_filter( \array_map( array( $this, 'transform_attachment' ), $media ) ); /** * Filter the attachments for a post. * * @param array $attachments The attachments. * @param \WP_Post $item The post object. * * @return array The filtered attachments. */ $this->attachment = \apply_filters( 'activitypub_attachments', $attachments, $this->item ); return $this->attachment; } /** * Returns the ActivityStreams 2.0 Object-Type for a Post based on the * settings and the Post-Type. * * @see https://www.w3.org/TR/activitystreams-vocabulary/#activity-types * * @return string The Object-Type. */ protected function get_type() { $post_format_setting = \get_option( 'activitypub_object_type', ACTIVITYPUB_DEFAULT_OBJECT_TYPE ); if ( 'wordpress-post-format' !== $post_format_setting ) { $object_type = \ucfirst( $post_format_setting ); } elseif ( ! \post_type_supports( $this->item->post_type, 'title' ) || ! $this->item->post_title ) { $object_type = 'Note'; } elseif ( 'page' === \get_post_type( $this->item ) ) { $object_type = 'Page'; } elseif ( ! \get_post_format( $this->item ) ) { $object_type = 'Article'; } else { $object_type = 'Note'; } /** * Filters the ActivityPub object type for a post. * * Allows downstream consumers to override the discriminator that * decides whether a post federates as Note, Article, or Page. * The filtered value propagates to all internal callers of * get_type(), including former_type/tombstone handling, * summary and title decisions, the content template, and the * preview guard, not only the wire-format type property. * * @since 8.1.1 * * @param string $object_type The computed ActivityPub object type. * @param \WP_Post $post The WordPress post being transformed. */ return \apply_filters( 'activitypub_post_object_type', $object_type, $this->item ); } /** * Returns the Audience for the Post. * * @return string|null The audience. */ public function get_audience() { $actor_mode = \get_option( 'activitypub_actor_mode', ACTIVITYPUB_ACTOR_MODE ); if ( ACTIVITYPUB_ACTOR_AND_BLOG_MODE === $actor_mode ) { $blog = new Blog(); return $blog->get_id(); } return null; } /** * Returns a list of Tags, used in the Post. * * This includes Hash-Tags and Mentions. * * @return array The list of Tags. */ protected function get_tag() { if ( false !== $this->tags ) { return $this->tags; } $tags = parent::get_tag(); $post_tags = \get_the_tags( $this->item->ID ); if ( $post_tags ) { foreach ( $post_tags as $post_tag ) { // Tag can be empty. if ( ! $post_tag ) { continue; } $tags[] = array( 'type' => 'Hashtag', 'href' => \esc_url( \get_tag_link( $post_tag->term_id ) ), 'name' => esc_hashtag( $post_tag->name ), ); } } $this->tags = \array_unique( $tags, SORT_REGULAR ); return $this->tags; } /** * Returns the summary for the ActivityPub Item. * * The summary will be generated based on the user settings and only if the * object type is not set to `note`. * * @return string|null The summary or null if the object type is `note`. */ protected function get_summary() { if ( 'Note' === $this->get_type() ) { return null; } if ( false !== $this->summary ) { return $this->summary; } // Remove Teaser from unpublished posts. if ( ! $this->is_preview() && 'publish' !== \get_post_status( $this->item ) ) { $this->summary = \__( '(This post is being modified)', 'activitypub' ); return $this->summary; } $this->summary = generate_post_summary( $this->item ); return $this->summary; } /** * Returns the title for the ActivityPub Item. * * The title will be generated based on the user settings and only if the * object type is not set to `note`. * * @return string|null The title or null if the object type is `note`. */ protected function get_name() { if ( 'Note' === $this->get_type() ) { return null; } $title = \get_the_title( $this->item->ID ); if ( ! $title ) { return null; } return \wp_strip_all_tags( \html_entity_decode( $title ) ); } /** * Returns the content for the ActivityPub Item. * * The content will be generated based on the user settings. * * @return string The content. */ protected function get_content() { if ( false !== $this->content ) { return $this->content; } // Remove Content from unpublished posts. if ( ! $this->is_preview() && 'publish' !== \get_post_status( $this->item ) ) { $this->content = \__( '(This post is being modified)', 'activitypub' ); return $this->content; } global $post; // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited $post = $this->item; $content = $this->get_post_content_template(); /** * Provides an action hook so plugins can add their own hooks/filters before AP content is generated. * * 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. * * @param \WP_Post $post The post object. */ \do_action( 'activitypub_before_get_content', $post ); // It seems that shortcodes are only applied to published posts. if ( is_preview() ) { $post->post_status = 'publish'; } // Register our shortcodes just in time. Shortcodes::register(); // Fill in the shortcodes. \setup_postdata( $post ); $content = \do_shortcode( $content ); \wp_reset_postdata(); // Don't need these anymore, should never appear in a post. Shortcodes::unregister(); /** * Filters the post content after it was transformed for ActivityPub. * * @param string $content The transformed post content. * @param \WP_Post $post The post object being transformed. */ $this->content = \apply_filters( 'activitypub_the_content', $content, $post ); return $this->content; } /** * Generate HTML @ link for reply block. * * @deprecated 7.4.0 Use {@see Blocks::generate_reply_link()}. * * @param string $block_content The block content. * @param array $block The block data. * * @return string The HTML @ link. */ public function generate_reply_link( $block_content, $block ) { _deprecated_function( __METHOD__, '7.4.0', 'Activitypub\Blocks::generate_reply_link' ); return Blocks::generate_reply_link( $block_content, $block ); } /** * Returns the in-reply-to URL of the post. * * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-inreplyto * * @return string|array|null The in-reply-to URL of the post. */ protected function get_in_reply_to() { if ( false !== $this->in_reply_to ) { return $this->in_reply_to; } if ( ! site_supports_blocks() ) { $this->in_reply_to = null; return $this->in_reply_to; } $reply_urls = array(); $blocks = \parse_blocks( $this->item->post_content ); foreach ( $blocks as $block ) { if ( 'activitypub/reply' === $block['blockName'] && isset( $block['attrs']['url'] ) ) { // Check if the URL has been validated as ActivityPub. Default to true for backwards compatibility. if ( $block['attrs']['isValidActivityPub'] ?? true ) { $reply_urls[] = $block['attrs']['url']; } } } if ( empty( $reply_urls ) ) { $this->in_reply_to = null; return $this->in_reply_to; } if ( 1 === count( $reply_urls ) ) { $this->in_reply_to = \current( $reply_urls ); return $this->in_reply_to; } $this->in_reply_to = \array_values( \array_unique( $reply_urls ) ); return $this->in_reply_to; } /** * Returns the published date of the post. * * @return string The published date of the post. */ protected function get_published() { $published = \strtotime( $this->item->post_date_gmt ); return \gmdate( ACTIVITYPUB_DATE_TIME_RFC3339, $published ); } /** * Returns the updated date of the post. * * @return string|null The updated date of the post. */ protected function get_updated() { $published = \strtotime( $this->item->post_date_gmt ); $updated = \strtotime( $this->item->post_modified_gmt ); if ( $updated > $published ) { return \gmdate( ACTIVITYPUB_DATE_TIME_RFC3339, $updated ); } return null; } /** * Returns the location of the post as a Place object. * * Uses WordPress Geodata post meta fields to build the location. * * @see https://codex.wordpress.org/Geodata * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-location * * @return array|null The Place object or null if no public geodata is available. */ protected function get_location() { $post_id = $this->item->ID; $meta = \get_post_meta( $post_id ); // If geo_public exists and is explicitly set to 0, don't share location. if ( isset( $meta['geo_public'] ) && '0' === $meta['geo_public'][0] ) { return null; } // Both latitude and longitude are required for a valid location. // Use is_numeric() instead of empty() since 0 is a valid coordinate (Equator/Prime Meridian). $has_latitude = isset( $meta['geo_latitude'][0] ) && is_numeric( $meta['geo_latitude'][0] ); $has_longitude = isset( $meta['geo_longitude'][0] ) && is_numeric( $meta['geo_longitude'][0] ); if ( ! $has_latitude || ! $has_longitude ) { return null; } $place = array( 'type' => 'Place', 'latitude' => (float) $meta['geo_latitude'][0], 'longitude' => (float) $meta['geo_longitude'][0], ); // Add the address/name if available. if ( ! empty( $meta['geo_address'][0] ) ) { $place['name'] = \sanitize_text_field( $meta['geo_address'][0] ); } /** * Filter the location Place object for a post. * * @param array $place The Place object. * @param \WP_Post $post The post object. * @param int $post_id The post ID. * * @return array|null The filtered Place object or null to disable location. */ return \apply_filters( 'activitypub_post_location', $place, $this->item, $post_id ); } /** * Helper function to extract the @-Mentions from the post content. * * @return array The list of @-Mentions. */ protected function get_mentions() { if ( false !== $this->mentions ) { return $this->mentions; } /** * Filter the mentions in the post content. * * @param array $mentions The mentions. * @param string $content The post content. * @param \WP_Post $post The post object. * * @return array The filtered mentions. */ $this->mentions = apply_filters( 'activitypub_extract_mentions', array(), $this->item->post_content . ' ' . $this->item->post_excerpt, $this->item ); return $this->mentions; } /** * Transform Embed blocks to block level link. * * Remote servers will simply drop iframe elements, rendering incomplete content. * * @deprecated 7.4.0 Use {@see Blocks::revert_embed_links()}. * * @see https://www.w3.org/TR/activitypub/#security-sanitizing-content * @see https://www.w3.org/wiki/ActivityPub/Primer/HTML * * @param string $block_content The block content (html). * @param object $block The block object. * * @return string A block level link */ public function revert_embed_links( $block_content, $block ) { _deprecated_function( __METHOD__, '7.4.0', 'Activitypub\Blocks::revert_embed_links' ); return Blocks::revert_embed_links( $block_content, $block ); } /** * Check if the post is a preview. * * @return boolean True if the post is a preview, false otherwise. */ private function is_preview() { return defined( 'ACTIVITYPUB_PREVIEW' ) && ACTIVITYPUB_PREVIEW; } /** * Get enclosures for a post. * * @param array $media The media array grouped by type. * * @return array The media array extended with enclosures. */ protected function get_enclosures( $media ) { $enclosures = get_enclosures( $this->item->ID ); if ( ! $enclosures ) { return $media; } foreach ( $enclosures as $enclosure ) { // Check if URL is an attachment. $attachment_id = \attachment_url_to_postid( $enclosure['url'] ); if ( $attachment_id ) { $enclosure['id'] = $attachment_id; $enclosure['url'] = \wp_get_attachment_url( $attachment_id ); $enclosure['mediaType'] = \get_post_mime_type( $attachment_id ); } $mime_type = $enclosure['mediaType']; $media_type = \strtok( $mime_type, '/' ); $enclosure['type'] = \ucfirst( $media_type ); switch ( $media_type ) { case 'image': $media['image'][] = $enclosure; break; case 'audio': $media['audio'][] = $enclosure; break; case 'video': $media['video'][] = $enclosure; break; } } return $media; } /** * Get media attachments from blocks. They will be formatted as ActivityPub attachments, not as WP attachments. * * @param array $media The media array grouped by type. * @param int $max_media The maximum number of attachments to return. * * @return array The attachments. */ protected function get_block_attachments( $media, $max_media ) { // Max media can't be negative or zero. if ( $max_media <= 0 ) { return array(); } $blocks = \parse_blocks( $this->item->post_content ); return $this->get_media_from_blocks( $blocks, $media ); } /** * Recursively get media IDs from blocks. * * @param array $blocks The blocks to search for media IDs. * @param array $media The media IDs to append new IDs to. * * @return array The image IDs. */ protected function get_media_from_blocks( $blocks, $media ) { foreach ( $blocks as $block ) { // Recurse into inner blocks. if ( ! empty( $block['innerBlocks'] ) ) { $media = $this->get_media_from_blocks( $block['innerBlocks'], $media ); } switch ( $block['blockName'] ) { case 'core/image': case 'core/cover': if ( ! empty( $block['attrs']['id'] ) ) { $alt = ''; $processor = new \WP_HTML_Tag_Processor( $block['innerHTML'] ); if ( $processor->next_tag( array( 'tag_name' => 'img' ) ) ) { $alt = $processor->get_attribute( 'alt' ) ?? ''; } $found = false; foreach ( $media['image'] as $i => $image ) { if ( isset( $image['id'] ) && $image['id'] === $block['attrs']['id'] ) { $media['image'][ $i ]['alt'] = $alt; $found = true; break; } } if ( ! $found ) { $media['image'][] = array( 'id' => $block['attrs']['id'], 'alt' => $alt, ); } } break; case 'core/audio': if ( ! empty( $block['attrs']['id'] ) ) { $media['audio'][] = array( 'id' => $block['attrs']['id'] ); } break; case 'core/video': case 'videopress/video': if ( ! empty( $block['attrs']['id'] ) ) { $video = array( 'id' => $block['attrs']['id'] ); // The poster is stored as an HTML attribute on the