2018-09-30 22:51:22 +02:00
|
|
|
<?php
|
2023-07-03 17:59:42 +02:00
|
|
|
namespace Activitypub\Transformer;
|
|
|
|
|
|
|
|
use WP_Post;
|
2023-12-22 10:12:26 +01:00
|
|
|
use Activitypub\Shortcodes;
|
2023-07-10 15:14:37 +02:00
|
|
|
use Activitypub\Model\Blog_User;
|
2023-12-22 10:12:26 +01:00
|
|
|
use Activitypub\Transformer\Base;
|
|
|
|
use Activitypub\Collection\Users;
|
2023-07-03 19:25:49 +02:00
|
|
|
use Activitypub\Activity\Base_Object;
|
2019-02-24 12:07:41 +01:00
|
|
|
|
2023-08-09 13:07:30 +02:00
|
|
|
use function Activitypub\esc_hashtag;
|
2023-07-10 10:57:06 +02:00
|
|
|
use function Activitypub\is_single_user;
|
2023-05-12 22:31:53 +02:00
|
|
|
use function Activitypub\get_rest_url_by_path;
|
2023-09-22 09:21:49 +02:00
|
|
|
use function Activitypub\site_supports_blocks;
|
2023-05-12 22:31:53 +02:00
|
|
|
|
2018-09-30 22:51:22 +02:00
|
|
|
/**
|
2023-07-03 17:59:42 +02:00
|
|
|
* WordPress Post Transformer
|
|
|
|
*
|
2023-12-22 10:12:26 +01:00
|
|
|
* The Post Transformer is responsible for transforming a WP_Post object into different other
|
2023-07-03 17:59:42 +02:00
|
|
|
* Object-Types.
|
|
|
|
*
|
|
|
|
* Currently supported are:
|
2018-09-30 22:51:22 +02:00
|
|
|
*
|
2023-07-03 17:59:42 +02:00
|
|
|
* - Activitypub\Activity\Base_Object
|
2018-09-30 22:51:22 +02:00
|
|
|
*/
|
2023-12-22 10:12:26 +01:00
|
|
|
class Post extends Base {
|
2023-12-22 16:18:18 +01:00
|
|
|
/**
|
|
|
|
* Getter function for the name of the transformer.
|
|
|
|
*
|
|
|
|
* @return string name
|
|
|
|
*/
|
|
|
|
public function get_name() {
|
|
|
|
return 'activitypub/post';
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Getter function for the display name (label/title) of the transformer.
|
|
|
|
*
|
|
|
|
* @return string name
|
|
|
|
*/
|
|
|
|
public function get_label() {
|
|
|
|
return 'Built-In Transformer for WordPress Posts';
|
|
|
|
}
|
|
|
|
|
2023-02-02 07:24:27 +01:00
|
|
|
/**
|
2023-12-22 10:12:26 +01:00
|
|
|
* Returns the ID of the WordPress Post.
|
2023-01-27 10:21:51 +01:00
|
|
|
*
|
2023-12-22 10:12:26 +01:00
|
|
|
* @return int The ID of the WordPress Post
|
2023-01-27 10:21:51 +01:00
|
|
|
*/
|
2023-12-22 10:12:26 +01:00
|
|
|
public function get_wp_user_id() {
|
|
|
|
return $this->object->post_author;
|
2018-12-20 11:33:08 +01:00
|
|
|
}
|
2018-09-30 22:51:22 +02:00
|
|
|
|
2023-01-27 10:21:51 +01:00
|
|
|
/**
|
2023-12-22 10:12:26 +01:00
|
|
|
* Change the User-ID of the WordPress Post.
|
2023-01-27 10:21:51 +01:00
|
|
|
*
|
2023-12-22 10:12:26 +01:00
|
|
|
* @return int The User-ID of the WordPress Post
|
2023-01-27 10:21:51 +01:00
|
|
|
*/
|
2023-12-22 10:12:26 +01:00
|
|
|
public function change_wp_user_id( $user_id ) {
|
|
|
|
$this->object->post_author = $user_id;
|
|
|
|
|
|
|
|
return $this;
|
2018-12-20 11:33:08 +01:00
|
|
|
}
|
2018-09-30 22:51:22 +02:00
|
|
|
|
2023-06-21 15:45:35 +02:00
|
|
|
/**
|
2023-07-03 17:59:42 +02:00
|
|
|
* Transforms the WP_Post object to an ActivityPub Object
|
2023-06-21 15:45:35 +02:00
|
|
|
*
|
2023-07-03 17:59:42 +02:00
|
|
|
* @see \Activitypub\Activity\Base_Object
|
2023-01-27 10:21:51 +01:00
|
|
|
*
|
2023-07-03 17:59:42 +02:00
|
|
|
* @return \Activitypub\Activity\Base_Object The ActivityPub Object
|
2023-01-27 10:21:51 +01:00
|
|
|
*/
|
2023-07-03 17:59:42 +02:00
|
|
|
public function to_object() {
|
2023-12-22 10:12:26 +01:00
|
|
|
$post = $this->object;
|
|
|
|
$object = parent::to_object();
|
2023-07-11 14:48:49 +02:00
|
|
|
|
2023-12-22 10:12:26 +01:00
|
|
|
$published = \strtotime( $post->post_date_gmt );
|
2023-07-11 14:48:49 +02:00
|
|
|
|
|
|
|
$object->set_published( \gmdate( 'Y-m-d\TH:i:s\Z', $published ) );
|
|
|
|
|
2023-12-22 10:12:26 +01:00
|
|
|
$updated = \strtotime( $post->post_modified_gmt );
|
2023-07-11 14:48:49 +02:00
|
|
|
|
|
|
|
if ( $updated > $published ) {
|
|
|
|
$object->set_updated( \gmdate( 'Y-m-d\TH:i:s\Z', $updated ) );
|
|
|
|
}
|
|
|
|
|
2023-07-03 17:59:42 +02:00
|
|
|
$object->set_content_map(
|
|
|
|
array(
|
2023-11-07 08:49:48 +01:00
|
|
|
$this->get_locale() => $this->get_content(),
|
2023-07-03 17:59:42 +02:00
|
|
|
)
|
2018-09-30 22:51:22 +02:00
|
|
|
);
|
2023-12-22 10:12:26 +01:00
|
|
|
$path = sprintf( 'users/%d/followers', intval( $post->post_author ) );
|
2018-09-30 22:51:22 +02:00
|
|
|
|
2023-07-03 17:59:42 +02:00
|
|
|
$object->set_to(
|
|
|
|
array(
|
|
|
|
'https://www.w3.org/ns/activitystreams#Public',
|
|
|
|
get_rest_url_by_path( $path ),
|
|
|
|
)
|
|
|
|
);
|
2023-04-23 22:56:45 +02:00
|
|
|
|
2023-07-03 17:59:42 +02:00
|
|
|
return $object;
|
2023-04-23 22:56:45 +02:00
|
|
|
}
|
|
|
|
|
2023-09-27 11:05:11 +02:00
|
|
|
/**
|
|
|
|
* Returns the ID of the Post.
|
|
|
|
*
|
|
|
|
* @return string The Posts ID.
|
|
|
|
*/
|
|
|
|
public function get_id() {
|
|
|
|
return $this->get_url();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the URL of the Post.
|
|
|
|
*
|
|
|
|
* @return string The Posts URL.
|
|
|
|
*/
|
|
|
|
public function get_url() {
|
2023-12-22 10:12:26 +01:00
|
|
|
$post = $this->object;
|
2023-09-27 11:05:11 +02:00
|
|
|
|
|
|
|
if ( 'trash' === get_post_status( $post ) ) {
|
|
|
|
$permalink = \get_post_meta( $post->ID, 'activitypub_canonical_url', true );
|
|
|
|
} else {
|
|
|
|
$permalink = \get_permalink( $post );
|
|
|
|
}
|
|
|
|
|
|
|
|
return \esc_url( $permalink );
|
|
|
|
}
|
|
|
|
|
2023-07-10 10:57:06 +02:00
|
|
|
/**
|
|
|
|
* 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() {
|
|
|
|
if ( is_single_user() ) {
|
|
|
|
$user = new Blog_User();
|
|
|
|
return $user->get_url();
|
|
|
|
}
|
|
|
|
|
2023-12-22 10:12:26 +01:00
|
|
|
return Users::get_by_id( $this->object->post_author )->get_url();
|
2023-07-10 10:57:06 +02:00
|
|
|
}
|
|
|
|
|
2023-09-22 09:21:49 +02:00
|
|
|
/**
|
2023-10-27 22:55:44 +02:00
|
|
|
* Generates all Media Attachments for a Post.
|
2023-09-22 09:21:49 +02:00
|
|
|
*
|
2023-10-27 22:55:44 +02:00
|
|
|
* @return array The Attachments.
|
|
|
|
*/
|
2023-12-22 10:12:26 +01:00
|
|
|
protected function get_attachment() {
|
2023-10-27 22:55:44 +02:00
|
|
|
// Once upon a time we only supported images, but we now support audio/video as well.
|
|
|
|
// We maintain the image-centric naming for backwards compatibility.
|
|
|
|
$max_media = intval( \apply_filters( 'activitypub_max_image_attachments', \get_option( 'activitypub_max_image_attachments', ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS ) ) );
|
|
|
|
|
2023-12-22 10:12:26 +01:00
|
|
|
if ( site_supports_blocks() && \has_blocks( $this->object->post_content ) ) {
|
2023-10-27 22:55:44 +02:00
|
|
|
return $this->get_block_attachments( $max_media );
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->get_classic_editor_images( $max_media );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get media attachments from blocks. They will be formatted as ActivityPub attachments, not as WP attachments.
|
|
|
|
*
|
|
|
|
* @param int $max_media The maximum number of attachments to return.
|
|
|
|
*
|
|
|
|
* @return array The attachments.
|
2023-09-22 09:21:49 +02:00
|
|
|
*/
|
2023-10-27 22:55:44 +02:00
|
|
|
protected function get_block_attachments( $max_media ) {
|
|
|
|
// max media can't be negative or zero
|
|
|
|
if ( $max_media <= 0 ) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2023-12-22 10:12:26 +01:00
|
|
|
$id = $this->object->ID;
|
2023-10-27 22:55:44 +02:00
|
|
|
|
|
|
|
$media_ids = array();
|
|
|
|
|
|
|
|
// list post thumbnail first if this post has one
|
|
|
|
if ( \function_exists( 'has_post_thumbnail' ) && \has_post_thumbnail( $id ) ) {
|
|
|
|
$media_ids[] = \get_post_thumbnail_id( $id );
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $max_media > 0 ) {
|
2023-12-22 10:12:26 +01:00
|
|
|
$blocks = \parse_blocks( $this->object->post_content );
|
2023-10-27 22:55:44 +02:00
|
|
|
$media_ids = self::get_media_ids_from_blocks( $blocks, $media_ids, $max_media );
|
|
|
|
}
|
|
|
|
|
|
|
|
return \array_filter( \array_map( array( self::class, 'wp_attachment_to_activity_attachment' ), $media_ids ) );
|
2023-09-22 09:21:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-10-27 22:55:44 +02:00
|
|
|
* Get image attachments from the classic editor.
|
|
|
|
* Note that audio/video attachments are only supported in the block editor.
|
|
|
|
*
|
2023-09-22 09:21:49 +02:00
|
|
|
* @param int $max_images The maximum number of images to return.
|
|
|
|
*
|
2023-10-27 22:55:44 +02:00
|
|
|
* @return array The attachments.
|
|
|
|
*/
|
|
|
|
protected function get_classic_editor_images( $max_images ) {
|
|
|
|
// max images can't be negative or zero
|
|
|
|
if ( $max_images <= 0 ) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
2023-12-22 10:12:26 +01:00
|
|
|
$id = $this->object->ID;
|
2023-10-27 22:55:44 +02:00
|
|
|
|
|
|
|
$image_ids = array();
|
|
|
|
|
|
|
|
// list post thumbnail first if this post has one
|
|
|
|
if ( \function_exists( 'has_post_thumbnail' ) && \has_post_thumbnail( $id ) ) {
|
|
|
|
$image_ids[] = \get_post_thumbnail_id( $id );
|
|
|
|
--$max_images;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $max_images > 0 ) {
|
|
|
|
$query = new \WP_Query(
|
|
|
|
array(
|
|
|
|
'post_parent' => $id,
|
|
|
|
'post_status' => 'inherit',
|
|
|
|
'post_type' => 'attachment',
|
|
|
|
'post_mime_type' => 'image',
|
|
|
|
'order' => 'ASC',
|
|
|
|
'orderby' => 'menu_order ID',
|
|
|
|
'posts_per_page' => $max_images,
|
|
|
|
)
|
|
|
|
);
|
|
|
|
foreach ( $query->get_posts() as $attachment ) {
|
|
|
|
if ( ! \in_array( $attachment->ID, $image_ids, true ) ) {
|
|
|
|
$image_ids[] = $attachment->ID;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$image_ids = \array_unique( $image_ids );
|
|
|
|
|
|
|
|
return \array_filter( \array_map( array( self::class, 'wp_attachment_to_activity_attachment' ), $image_ids ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Recursively get media IDs from blocks.
|
|
|
|
* @param array $blocks The blocks to search for media IDs
|
|
|
|
* @param array $media_ids The media IDs to append new IDs to
|
|
|
|
* @param int $max_media The maximum number of media to return.
|
|
|
|
*
|
2023-09-22 09:21:49 +02:00
|
|
|
* @return array The image IDs.
|
|
|
|
*/
|
2023-10-27 22:55:44 +02:00
|
|
|
protected static function get_media_ids_from_blocks( $blocks, $media_ids, $max_media ) {
|
|
|
|
|
2023-09-22 09:21:49 +02:00
|
|
|
foreach ( $blocks as $block ) {
|
|
|
|
// recurse into inner blocks
|
|
|
|
if ( ! empty( $block['innerBlocks'] ) ) {
|
2023-10-27 22:55:44 +02:00
|
|
|
$media_ids = self::get_media_ids_from_blocks( $block['innerBlocks'], $media_ids, $max_media );
|
2023-09-22 09:21:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
switch ( $block['blockName'] ) {
|
|
|
|
case 'core/image':
|
|
|
|
case 'core/cover':
|
2023-10-27 22:55:44 +02:00
|
|
|
case 'core/audio':
|
|
|
|
case 'core/video':
|
|
|
|
case 'videopress/video':
|
2023-09-22 09:21:49 +02:00
|
|
|
if ( ! empty( $block['attrs']['id'] ) ) {
|
2023-10-27 22:55:44 +02:00
|
|
|
$media_ids[] = $block['attrs']['id'];
|
2023-09-22 09:21:49 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'jetpack/slideshow':
|
|
|
|
case 'jetpack/tiled-gallery':
|
|
|
|
if ( ! empty( $block['attrs']['ids'] ) ) {
|
2023-10-27 22:55:44 +02:00
|
|
|
$media_ids = array_merge( $media_ids, $block['attrs']['ids'] );
|
2023-09-22 09:21:49 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'jetpack/image-compare':
|
|
|
|
if ( ! empty( $block['attrs']['beforeImageId'] ) ) {
|
2023-10-27 22:55:44 +02:00
|
|
|
$media_ids[] = $block['attrs']['beforeImageId'];
|
2023-09-22 09:21:49 +02:00
|
|
|
}
|
|
|
|
if ( ! empty( $block['attrs']['afterImageId'] ) ) {
|
2023-10-27 22:55:44 +02:00
|
|
|
$media_ids[] = $block['attrs']['afterImageId'];
|
2023-09-22 09:21:49 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-11-17 18:44:59 +01:00
|
|
|
// depupe
|
|
|
|
$media_ids = \array_unique( $media_ids );
|
|
|
|
|
2023-10-27 22:55:44 +02:00
|
|
|
// stop doing unneeded work
|
|
|
|
if ( count( $media_ids ) >= $max_media ) {
|
2023-09-22 09:21:49 +02:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// still need to slice it because one gallery could knock us over the limit
|
2023-10-27 22:55:44 +02:00
|
|
|
return array_slice( $media_ids, 0, $max_media );
|
2023-09-22 09:21:49 +02:00
|
|
|
}
|
|
|
|
|
2023-01-27 10:21:51 +01:00
|
|
|
/**
|
2023-10-27 22:55:44 +02:00
|
|
|
* Converts a WordPress Attachment to an ActivityPub Attachment.
|
|
|
|
*
|
|
|
|
* @param int $id The Attachment ID.
|
2023-01-27 10:21:51 +01:00
|
|
|
*
|
2023-10-27 22:55:44 +02:00
|
|
|
* @return array The ActivityPub Attachment.
|
2023-01-27 10:21:51 +01:00
|
|
|
*/
|
2023-10-27 22:55:44 +02:00
|
|
|
public static function wp_attachment_to_activity_attachment( $id ) {
|
|
|
|
$attachment = array();
|
|
|
|
$mime_type = \get_post_mime_type( $id );
|
|
|
|
$mime_type_parts = \explode( '/', $mime_type );
|
|
|
|
// switching on image/audio/video
|
|
|
|
switch ( $mime_type_parts[0] ) {
|
|
|
|
case 'image':
|
|
|
|
$image_size = 'full';
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 'full' by default.
|
|
|
|
*/
|
|
|
|
$thumbnail = apply_filters(
|
|
|
|
'activitypub_get_image',
|
2023-12-22 10:12:26 +01:00
|
|
|
self::get_wordpress_attachment( $id, $image_size ),
|
2023-10-27 22:55:44 +02:00
|
|
|
$id,
|
|
|
|
$image_size
|
|
|
|
);
|
2023-01-12 22:21:48 +01:00
|
|
|
|
2023-10-27 22:55:44 +02:00
|
|
|
if ( $thumbnail ) {
|
|
|
|
$alt = \get_post_meta( $id, '_wp_attachment_image_alt', true );
|
|
|
|
$image = array(
|
|
|
|
'type' => 'Image',
|
|
|
|
'url' => $thumbnail[0],
|
|
|
|
'mediaType' => $mime_type,
|
|
|
|
);
|
2023-01-12 22:21:48 +01:00
|
|
|
|
2023-10-27 22:55:44 +02:00
|
|
|
if ( $alt ) {
|
|
|
|
$image['name'] = $alt;
|
2023-09-22 09:21:49 +02:00
|
|
|
}
|
2023-10-27 22:55:44 +02:00
|
|
|
$attachment = $image;
|
2023-01-12 22:21:48 +01:00
|
|
|
}
|
2023-10-27 22:55:44 +02:00
|
|
|
break;
|
2018-12-20 11:33:08 +01:00
|
|
|
|
2023-10-27 22:55:44 +02:00
|
|
|
case 'audio':
|
|
|
|
case 'video':
|
|
|
|
$attachment = array(
|
|
|
|
'type' => 'Document',
|
|
|
|
'mediaType' => $mime_type,
|
|
|
|
'url' => \wp_get_attachment_url( $id ),
|
|
|
|
'name' => \get_the_title( $id ),
|
2018-12-20 11:33:08 +01:00
|
|
|
);
|
2023-10-27 22:55:44 +02:00
|
|
|
$meta = wp_get_attachment_metadata( $id );
|
|
|
|
// height and width for videos
|
|
|
|
if ( isset( $meta['width'] ) && isset( $meta['height'] ) ) {
|
|
|
|
$attachment['width'] = $meta['width'];
|
|
|
|
$attachment['height'] = $meta['height'];
|
2020-01-18 19:36:46 +01:00
|
|
|
}
|
2023-10-27 22:55:44 +02:00
|
|
|
// @todo: add `icon` support for audio/video attachments. Maybe use post thumbnail?
|
|
|
|
break;
|
2018-12-20 11:33:08 +01:00
|
|
|
}
|
|
|
|
|
2023-10-27 22:55:44 +02:00
|
|
|
return \apply_filters( 'activitypub_attachment', $attachment, $id );
|
2018-12-20 11:33:08 +01:00
|
|
|
}
|
|
|
|
|
2023-04-25 10:54:21 +02:00
|
|
|
/**
|
|
|
|
* Return details about an image attachment.
|
|
|
|
*
|
2023-04-26 10:45:35 +02:00
|
|
|
* @param int $id The attachment ID.
|
|
|
|
* @param string $image_size The image size to retrieve. Set to 'full' by default.
|
2023-04-25 10:54:21 +02:00
|
|
|
*
|
|
|
|
* @return array|false Array of image data, or boolean false if no image is available.
|
|
|
|
*/
|
2023-12-22 10:12:26 +01:00
|
|
|
protected static function get_wordpress_attachment( $id, $image_size = 'full' ) {
|
2023-04-25 10:54:21 +02:00
|
|
|
/**
|
2023-04-26 10:45:35 +02:00
|
|
|
* Hook into the image retrieval process. Before image retrieval.
|
2023-04-25 10:54:21 +02:00
|
|
|
*
|
2023-04-26 10:45:35 +02:00
|
|
|
* @param int $id The attachment ID.
|
|
|
|
* @param string $image_size The image size to retrieve. Set to 'full' by default.
|
2023-04-25 10:54:21 +02:00
|
|
|
*/
|
2023-04-26 10:45:35 +02:00
|
|
|
do_action( 'activitypub_get_image_pre', $id, $image_size );
|
2023-04-25 10:54:21 +02:00
|
|
|
|
2023-10-27 22:55:44 +02:00
|
|
|
$image = \wp_get_attachment_image_src( $id, $image_size );
|
2023-04-25 10:54:21 +02:00
|
|
|
|
2023-04-26 10:45:35 +02:00
|
|
|
/**
|
|
|
|
* Hook into the image retrieval process. After image retrieval.
|
2023-04-25 10:54:21 +02:00
|
|
|
*
|
2023-04-26 10:45:35 +02:00
|
|
|
* @param int $id The attachment ID.
|
|
|
|
* @param string $image_size The image size to retrieve. Set to 'full' by default.
|
2023-04-25 10:54:21 +02:00
|
|
|
*/
|
2023-09-14 19:50:27 +02:00
|
|
|
do_action( 'activitypub_get_image_post', $id, $image_size );
|
2023-04-25 10:54:21 +02:00
|
|
|
|
2023-10-27 22:55:44 +02:00
|
|
|
return $image;
|
2023-04-25 10:54:21 +02:00
|
|
|
}
|
|
|
|
|
2023-01-27 10:21:51 +01:00
|
|
|
/**
|
2023-07-03 17:59:42 +02:00
|
|
|
* Returns the ActivityStreams 2.0 Object-Type for a Post based on the
|
|
|
|
* settings and the Post-Type.
|
2023-01-27 10:21:51 +01:00
|
|
|
*
|
2023-07-03 17:59:42 +02:00
|
|
|
* @see https://www.w3.org/TR/activitystreams-vocabulary/#activity-types
|
2018-09-30 22:51:22 +02:00
|
|
|
*
|
2023-07-03 17:59:42 +02:00
|
|
|
* @return string The Object-Type.
|
2018-09-30 22:51:22 +02:00
|
|
|
*/
|
2023-12-22 11:01:23 +01:00
|
|
|
protected function get_type() {
|
2019-09-27 10:12:59 +02:00
|
|
|
if ( 'wordpress-post-format' !== \get_option( 'activitypub_object_type', 'note' ) ) {
|
|
|
|
return \ucfirst( \get_option( 'activitypub_object_type', 'note' ) );
|
2018-12-28 22:40:57 +01:00
|
|
|
}
|
|
|
|
|
2023-10-25 08:44:04 +02:00
|
|
|
// Default to Article.
|
|
|
|
$object_type = 'Article';
|
2023-12-22 10:12:26 +01:00
|
|
|
$post_type = \get_post_type( $this->object );
|
2018-09-30 22:51:22 +02:00
|
|
|
switch ( $post_type ) {
|
|
|
|
case 'post':
|
2023-12-22 10:12:26 +01:00
|
|
|
$post_format = \get_post_format( $this->object );
|
2018-09-30 22:51:22 +02:00
|
|
|
switch ( $post_format ) {
|
|
|
|
case 'aside':
|
|
|
|
case 'status':
|
|
|
|
case 'quote':
|
|
|
|
case 'note':
|
|
|
|
$object_type = 'Note';
|
|
|
|
break;
|
|
|
|
case 'gallery':
|
|
|
|
case 'image':
|
|
|
|
$object_type = 'Image';
|
|
|
|
break;
|
|
|
|
case 'video':
|
|
|
|
$object_type = 'Video';
|
|
|
|
break;
|
|
|
|
case 'audio':
|
|
|
|
$object_type = 'Audio';
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$object_type = 'Article';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'page':
|
|
|
|
$object_type = 'Page';
|
|
|
|
break;
|
|
|
|
case 'attachment':
|
2019-09-27 10:12:59 +02:00
|
|
|
$mime_type = \get_post_mime_type();
|
|
|
|
$media_type = \preg_replace( '/(\/[a-zA-Z]+)/i', '', $mime_type );
|
2018-09-30 22:51:22 +02:00
|
|
|
switch ( $media_type ) {
|
|
|
|
case 'audio':
|
|
|
|
$object_type = 'Audio';
|
|
|
|
break;
|
|
|
|
case 'video':
|
|
|
|
$object_type = 'Video';
|
|
|
|
break;
|
|
|
|
case 'image':
|
|
|
|
$object_type = 'Image';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$object_type = 'Article';
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $object_type;
|
|
|
|
}
|
2019-01-04 19:57:33 +01:00
|
|
|
|
2023-01-16 16:27:27 +01:00
|
|
|
/**
|
2023-07-03 17:59:42 +02:00
|
|
|
* Returns a list of Mentions, used in the Post.
|
|
|
|
*
|
|
|
|
* @see https://docs.joinmastodon.org/spec/activitypub/#Mention
|
2023-01-16 16:27:27 +01:00
|
|
|
*
|
2023-07-03 17:59:42 +02:00
|
|
|
* @return array The list of Mentions.
|
2023-01-16 16:27:27 +01:00
|
|
|
*/
|
2023-07-03 17:59:42 +02:00
|
|
|
protected function get_cc() {
|
|
|
|
$cc = array();
|
2023-02-02 08:18:10 +01:00
|
|
|
|
2023-07-03 17:59:42 +02:00
|
|
|
$mentions = $this->get_mentions();
|
|
|
|
if ( $mentions ) {
|
2023-08-30 21:23:20 +02:00
|
|
|
foreach ( $mentions as $url ) {
|
2023-07-03 17:59:42 +02:00
|
|
|
$cc[] = $url;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $cc;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns a list of Tags, used in the Post.
|
|
|
|
*
|
|
|
|
* This includes Hash-Tags and Mentions.
|
|
|
|
*
|
|
|
|
* @return array The list of Tags.
|
|
|
|
*/
|
2023-12-22 10:12:26 +01:00
|
|
|
protected function get_tag() {
|
2023-07-03 17:59:42 +02:00
|
|
|
$tags = array();
|
|
|
|
|
2023-12-22 10:12:26 +01:00
|
|
|
$post_tags = \get_the_tags( $this->object->ID );
|
2023-07-03 17:59:42 +02:00
|
|
|
if ( $post_tags ) {
|
|
|
|
foreach ( $post_tags as $post_tag ) {
|
|
|
|
$tag = array(
|
|
|
|
'type' => 'Hashtag',
|
2023-08-09 13:07:30 +02:00
|
|
|
'href' => \esc_url( \get_tag_link( $post_tag->term_id ) ),
|
|
|
|
'name' => esc_hashtag( $post_tag->name ),
|
2023-07-03 17:59:42 +02:00
|
|
|
);
|
|
|
|
$tags[] = $tag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$mentions = $this->get_mentions();
|
|
|
|
if ( $mentions ) {
|
|
|
|
foreach ( $mentions as $mention => $url ) {
|
|
|
|
$tag = array(
|
|
|
|
'type' => 'Mention',
|
|
|
|
'href' => \esc_url( $url ),
|
|
|
|
'name' => \esc_html( $mention ),
|
|
|
|
);
|
|
|
|
$tags[] = $tag;
|
|
|
|
}
|
2023-01-27 10:21:51 +01:00
|
|
|
}
|
|
|
|
|
2023-07-03 17:59:42 +02:00
|
|
|
return $tags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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() {
|
|
|
|
global $post;
|
|
|
|
|
2023-10-05 08:14:32 +02:00
|
|
|
/**
|
|
|
|
* 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 );
|
|
|
|
|
2023-02-02 08:18:10 +01:00
|
|
|
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
|
2023-12-22 10:12:26 +01:00
|
|
|
$post = $this->object;
|
2020-07-21 09:23:35 +02:00
|
|
|
$content = $this->get_post_content_template();
|
2019-01-04 19:57:33 +01:00
|
|
|
|
2023-10-19 21:46:31 +02:00
|
|
|
// Register our shortcodes just in time.
|
|
|
|
Shortcodes::register();
|
2023-01-13 21:47:13 +01:00
|
|
|
// Fill in the shortcodes.
|
2023-01-23 17:59:13 +01:00
|
|
|
setup_postdata( $post );
|
2023-01-13 21:47:13 +01:00
|
|
|
$content = do_shortcode( $content );
|
2023-01-23 17:59:13 +01:00
|
|
|
wp_reset_postdata();
|
2020-07-21 09:23:35 +02:00
|
|
|
|
2023-05-23 11:10:05 +02:00
|
|
|
$content = \wpautop( $content );
|
|
|
|
$content = \preg_replace( '/[\n\r\t]/', '', $content );
|
|
|
|
$content = \trim( $content );
|
2020-07-21 09:23:35 +02:00
|
|
|
|
2023-02-20 18:17:02 +01:00
|
|
|
$content = \apply_filters( 'activitypub_the_content', $content, $post );
|
2023-10-19 21:46:31 +02:00
|
|
|
|
|
|
|
// Don't need these any more, should never appear in a post.
|
|
|
|
Shortcodes::unregister();
|
2020-07-21 09:23:35 +02:00
|
|
|
|
2023-01-27 10:21:51 +01:00
|
|
|
return $content;
|
2019-01-04 19:57:33 +01:00
|
|
|
}
|
|
|
|
|
2023-01-16 16:27:27 +01:00
|
|
|
/**
|
|
|
|
* Gets the template to use to generate the content of the activitypub item.
|
|
|
|
*
|
2023-07-03 17:59:42 +02:00
|
|
|
* @return string The Template.
|
2023-01-16 16:27:27 +01:00
|
|
|
*/
|
2023-07-03 17:59:42 +02:00
|
|
|
protected function get_post_content_template() {
|
2023-12-12 13:58:44 +01:00
|
|
|
$type = \get_option( 'activitypub_post_content_type', 'content' );
|
2019-02-02 23:56:05 +01:00
|
|
|
|
2023-12-12 13:58:44 +01:00
|
|
|
switch ( $type ) {
|
|
|
|
case 'excerpt':
|
|
|
|
$template = "[ap_excerpt]\n\n[ap_permalink type=\"html\"]";
|
|
|
|
break;
|
|
|
|
case 'title':
|
|
|
|
$template = "[ap_title]\n\n[ap_permalink type=\"html\"]";
|
|
|
|
break;
|
|
|
|
case 'content':
|
|
|
|
$template = "[ap_content]\n\n[ap_permalink type=\"html\"]\n\n[ap_hashtags]";
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
$template = \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT );
|
|
|
|
break;
|
2023-01-13 21:47:13 +01:00
|
|
|
}
|
|
|
|
|
2023-12-22 10:12:26 +01:00
|
|
|
return apply_filters( 'activitypub_object_content_template', $template, $this->object );
|
2019-02-02 23:56:05 +01:00
|
|
|
}
|
2023-07-03 17:59:42 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to get the @-Mentions from the post content.
|
|
|
|
*
|
|
|
|
* @return array The list of @-Mentions.
|
|
|
|
*/
|
|
|
|
protected function get_mentions() {
|
2023-12-22 10:12:26 +01:00
|
|
|
return apply_filters( 'activitypub_extract_mentions', array(), $this->object->post_content, $this->object );
|
2023-07-03 17:59:42 +02:00
|
|
|
}
|
2023-11-07 08:49:48 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the locale of the post.
|
|
|
|
*
|
|
|
|
* @return string The locale of the post.
|
|
|
|
*/
|
|
|
|
public function get_locale() {
|
2023-12-22 10:12:26 +01:00
|
|
|
$post_id = $this->object->ID;
|
2023-11-07 08:49:48 +01:00
|
|
|
$lang = \strtolower( \strtok( \get_locale(), '_-' ) );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Filter the locale of the post.
|
|
|
|
*
|
|
|
|
* @param string $lang The locale of the post.
|
|
|
|
* @param int $post_id The post ID.
|
|
|
|
* @param WP_Post $post The post object.
|
|
|
|
*
|
|
|
|
* @return string The filtered locale of the post.
|
|
|
|
*/
|
2023-12-22 10:12:26 +01:00
|
|
|
return apply_filters( 'activitypub_post_locale', $lang, $post_id, $this->object );
|
2023-11-07 08:49:48 +01:00
|
|
|
}
|
2018-09-30 22:51:22 +02:00
|
|
|
}
|