2023-11-20 18:15:54 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Inspired by the PHP ActivityPub Library by @Landrok
|
|
|
|
*
|
|
|
|
* @link https://github.com/landrok/activitypub
|
|
|
|
*/
|
2023-11-22 13:59:39 +01:00
|
|
|
namespace Activitypub\Transformer;
|
2023-11-20 18:15:54 +01:00
|
|
|
|
|
|
|
use WP_Post;
|
|
|
|
use Activitypub\Collection\Users;
|
|
|
|
use Activitypub\Model\Blog_User;
|
|
|
|
use Activitypub\Activity\Base_Object;
|
|
|
|
use Activitypub\Shortcodes;
|
|
|
|
|
|
|
|
use function Activitypub\esc_hashtag;
|
|
|
|
use function Activitypub\is_single_user;
|
|
|
|
use function Activitypub\get_rest_url_by_path;
|
|
|
|
use function Activitypub\site_supports_blocks;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Base class to implement WordPress to ActivityPub transformers.
|
2023-12-04 19:42:38 +01:00
|
|
|
*
|
|
|
|
* All have to be extended from this one. It provides some WordPress typical
|
|
|
|
* getter functions, that you might want to use:
|
|
|
|
*
|
|
|
|
*
|
2023-11-20 18:15:54 +01:00
|
|
|
*/
|
2023-12-11 19:43:50 +01:00
|
|
|
abstract class Base { // todo renmae to Base_tranformer
|
2023-11-20 18:15:54 +01:00
|
|
|
/**
|
|
|
|
* The WP_Post object.
|
|
|
|
*
|
|
|
|
* @var WP_Post
|
|
|
|
*/
|
|
|
|
protected $wp_post;
|
|
|
|
|
2023-12-09 02:01:13 +01:00
|
|
|
/**
|
|
|
|
* Time format string for date or gmdate
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
|
|
|
protected $timeformat;
|
|
|
|
|
2023-12-04 19:42:38 +01:00
|
|
|
/**
|
|
|
|
* Get the name used for registering the transformer with the ActivityPub plugin.
|
|
|
|
*
|
|
|
|
* @since version_number_transformer_management_placeholder
|
|
|
|
* @return string name
|
|
|
|
*/
|
|
|
|
abstract public function get_name();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the display name for the ActivityPub transformer.
|
|
|
|
*
|
|
|
|
* @since version_number_transformer_management_placeholder
|
|
|
|
* @return string display name
|
|
|
|
*/
|
|
|
|
abstract public function get_label();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the ActivityStreams 2.0 Object-Type for a Post.
|
|
|
|
*
|
|
|
|
* @see https://www.w3.org/TR/activitystreams-vocabulary/#activity-types
|
|
|
|
*
|
|
|
|
* @return string The Object-Type.
|
|
|
|
*/
|
|
|
|
abstract protected function get_object_type();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Transforms the WP_Post object to an ActivityPub Object
|
|
|
|
*
|
|
|
|
* @see \Activitypub\Activity\Base_Object
|
|
|
|
* @param WP_Post $wp_post The WordPress Post
|
|
|
|
*
|
|
|
|
* @return \Activitypub\Activity\Base_Object The ActivityPub Object
|
|
|
|
*/
|
|
|
|
abstract public function transform();
|
|
|
|
|
2023-12-09 02:01:13 +01:00
|
|
|
/**
|
|
|
|
* Retrieves a post meta field for the configured post.
|
|
|
|
*
|
2023-12-11 23:21:47 +01:00
|
|
|
* @param string $key The meta key to retrieve. By default,
|
|
|
|
* returns data for all keys.
|
|
|
|
* @param bool $single Optional. Whether to return a single value.
|
|
|
|
* This parameter has no effect if `$key` is not specified.
|
|
|
|
* Default false.
|
|
|
|
* @param mixed $default_value Optional. if the key doesn't exists, it returns the default value.
|
|
|
|
* Default null.
|
2023-12-09 02:01:13 +01:00
|
|
|
* @return mixed An array of values if `$single` is false.
|
|
|
|
* The value of the meta field if `$single` is true.
|
|
|
|
* False for an invalid `$post_id` (non-numeric, zero, or negative value).
|
|
|
|
* An empty string if a valid but non-existing post ID is passed.
|
|
|
|
*/
|
2023-12-11 23:21:47 +01:00
|
|
|
public function get_post_meta( $key, $single = false, $default_value = null ) {
|
|
|
|
if ( \metadata_exists( 'post', $this->wp_post->ID, $key ) ) {
|
|
|
|
return \get_post_meta( $this->wp_post->ID, $key, $single );
|
|
|
|
}
|
|
|
|
return $default_value;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves a post property field for the configured post.
|
|
|
|
*
|
|
|
|
* @param string $key The property key to retrieve. By default,
|
|
|
|
* returns data for all keys.
|
|
|
|
* @param mixed $default_value Optional. if the key doesn't exists, it returns the default value.
|
|
|
|
* Default null.
|
|
|
|
* @return mixed The value of the property.
|
|
|
|
*/
|
|
|
|
public function get_post_property( $key, $default_value = null ) {
|
|
|
|
if ( isset( $this->wp_post->$key ) ) {
|
|
|
|
return $this->wp_post->$key;
|
|
|
|
}
|
|
|
|
return $default_value;
|
2023-12-09 02:01:13 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves a post meta field for the configured post.
|
|
|
|
*
|
|
|
|
* @param string $key The meta key of the time value to retrieve.
|
|
|
|
* @return string formatted time
|
|
|
|
*/
|
|
|
|
public function get_post_meta_time( string $key ) {
|
|
|
|
$time = $this->get_post_meta( $key, true ); // todo what if not exists
|
2023-12-11 23:22:07 +01:00
|
|
|
if ( null === $time ) {
|
|
|
|
return null;
|
|
|
|
}
|
2023-12-09 02:01:13 +01:00
|
|
|
$time = \strtotime( $time );
|
|
|
|
return \gmdate( $this->timeformat, $time );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Retrieves if comments are enabled of the configured post.
|
|
|
|
*
|
|
|
|
* @return bool True if the comments are open.
|
|
|
|
*/
|
|
|
|
public function get_comments_open() {
|
|
|
|
return comments_open( $this->wp_post->ID );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
2023-11-30 08:46:15 +01:00
|
|
|
/**
|
|
|
|
* Getter function for this wp_post.
|
|
|
|
*
|
|
|
|
* @return WP_post This WordPress Post object.
|
|
|
|
*/
|
|
|
|
protected function get_wp_post() {
|
|
|
|
return $this->wp_post;
|
|
|
|
}
|
|
|
|
|
2023-11-20 18:15:54 +01:00
|
|
|
/**
|
|
|
|
* Assign WP_Post Object to a specific transformer instance.
|
|
|
|
*
|
|
|
|
* This helps to chain the output of the Transformer.
|
|
|
|
*
|
2023-11-22 13:59:39 +01:00
|
|
|
* @param WP_Post $wp_post The WP_Post object.
|
2023-11-20 18:15:54 +01:00
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function set_wp_post( WP_Post $wp_post ) {
|
2023-11-22 14:24:25 +01:00
|
|
|
$post_type = get_post_type( $wp_post );
|
|
|
|
if ( ! $this->supports_post_type( $post_type ) ) {
|
|
|
|
_doing_it_wrong(
|
|
|
|
__METHOD__,
|
|
|
|
/* translators: %s: Block name. */
|
|
|
|
sprintf( 'The Transformer "%s" does not support the post type "%s".', esc_html( $this->get_label() ), esc_html( $post_type ) ),
|
|
|
|
'version_number_transformer_management_placeholder'
|
|
|
|
);
|
2023-11-22 13:59:39 +01:00
|
|
|
}
|
2023-11-22 14:24:25 +01:00
|
|
|
$this->wp_post = $wp_post;
|
2023-11-20 18:15:54 +01:00
|
|
|
}
|
|
|
|
|
2023-12-09 02:01:13 +01:00
|
|
|
/**
|
|
|
|
* Setter for $timeformat.
|
|
|
|
*
|
|
|
|
* This configures a timeformat string for converting timestaps into dates
|
|
|
|
*
|
|
|
|
* @param string $timeformat The time format string.
|
|
|
|
* @return void
|
|
|
|
*/
|
|
|
|
public function set_timeformat( string $timeformat ) {
|
|
|
|
$this->timeformat = $timeformat;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get published time in configured format
|
|
|
|
*
|
|
|
|
* This queries the published time from the wp_object and returns it in the configured format
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_published() {
|
|
|
|
$published = \strtotime( $this->wp_post->post_date_gmt );
|
|
|
|
return \gmdate( 'Y-m-d\TH:i:s\Z', $published );
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get updated time in configured format
|
|
|
|
*
|
|
|
|
* This queries the update time from the wp_object and returns it in the configured format
|
|
|
|
*
|
|
|
|
* @return string
|
|
|
|
*/
|
|
|
|
public function get_updated() {
|
|
|
|
$updated = \strtotime( $this->wp_post->post_modified_gmt );
|
|
|
|
return \gmdate( 'Y-m-d\TH:i:s\Z', $updated );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* TODO review and write description
|
|
|
|
*
|
|
|
|
* @return array
|
|
|
|
*/
|
|
|
|
public function get_followers_stream() {
|
|
|
|
$path = sprintf( 'users/%d/followers', intval( $this->wp_post->post_author ) );
|
|
|
|
return array(
|
|
|
|
'https://www.w3.org/ns/activitystreams#Public',
|
|
|
|
get_rest_url_by_path( $path ),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2023-11-22 13:59:39 +01:00
|
|
|
/**
|
2023-11-22 14:24:25 +01:00
|
|
|
* Get the supported WP post types that the transformer can use as an input.
|
2023-11-20 18:15:54 +01:00
|
|
|
*
|
2023-11-22 13:59:39 +01:00
|
|
|
* By default all post types are supported.
|
2023-11-20 18:15:54 +01:00
|
|
|
* You may very likely wish to override this function.
|
2023-11-22 13:59:39 +01:00
|
|
|
*
|
2023-11-20 18:15:54 +01:00
|
|
|
* @since version_number_transformer_management_placeholder
|
2023-11-22 13:59:39 +01:00
|
|
|
* @return string[] An array containing all the supported post types.
|
2023-11-20 18:15:54 +01:00
|
|
|
*/
|
2023-12-04 19:42:38 +01:00
|
|
|
public static function get_supported_post_types() {
|
2023-11-22 13:59:39 +01:00
|
|
|
return \get_post_types( array( 'public' => true ), 'names' );
|
|
|
|
}
|
2023-11-20 18:15:54 +01:00
|
|
|
|
2023-11-22 13:59:39 +01:00
|
|
|
/**
|
2023-11-20 18:15:54 +01:00
|
|
|
* Get the name of the plugin that registered the transformer.
|
2023-11-22 13:59:39 +01:00
|
|
|
*
|
|
|
|
* @see Forked from the WordPress elementor plugin.
|
2023-11-20 18:15:54 +01:00
|
|
|
* @since version_number_transformer_management_placeholder
|
2023-11-22 13:59:39 +01:00
|
|
|
* @return string Plugin name
|
2023-11-20 18:15:54 +01:00
|
|
|
*/
|
2023-12-04 19:42:38 +01:00
|
|
|
final private function get_plugin_name_from_transformer_instance( $transformer ) {
|
2023-11-20 18:15:54 +01:00
|
|
|
$class_reflection = new \ReflectionClass( $transformer );
|
|
|
|
|
|
|
|
$plugin_basename = plugin_basename( $class_reflection->getFileName() );
|
|
|
|
|
|
|
|
$plugin_directory = strtok( $plugin_basename, '/' );
|
|
|
|
|
|
|
|
$plugins_data = get_plugins( '/' . $plugin_directory );
|
2023-11-22 13:59:39 +01:00
|
|
|
$plugin_data = array_shift( $plugins_data );
|
2023-11-20 18:15:54 +01:00
|
|
|
|
2023-11-22 14:24:25 +01:00
|
|
|
if ( isset( $plugin_data['Name'] ) ) {
|
|
|
|
return $plugin_data['Name'];
|
|
|
|
} else {
|
|
|
|
return esc_html__( 'Unknown', 'activitypub' );
|
|
|
|
}
|
2023-11-20 18:15:54 +01:00
|
|
|
}
|
|
|
|
|
2023-11-22 13:59:39 +01:00
|
|
|
/**
|
2023-11-20 18:15:54 +01:00
|
|
|
* Return whether the transformer supports a post type.
|
|
|
|
*
|
|
|
|
* @since version_number_transformer_management_placeholder
|
2023-11-22 13:59:39 +01:00
|
|
|
* @return string post_type Post type name.
|
2023-11-20 18:15:54 +01:00
|
|
|
*/
|
2023-11-22 13:59:39 +01:00
|
|
|
final public function supports_post_type( $post_type ) {
|
2023-11-25 10:37:54 +01:00
|
|
|
return in_array( $post_type, $this->get_supported_post_types(), true );
|
2023-11-20 18:15:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 );
|
|
|
|
|
|
|
|
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
|
|
|
|
$post = $this->wp_post;
|
|
|
|
$content = $this->get_post_content_template();
|
|
|
|
|
|
|
|
// Register our shortcodes just in time.
|
|
|
|
Shortcodes::register();
|
|
|
|
// Fill in the shortcodes.
|
|
|
|
setup_postdata( $post );
|
|
|
|
$content = do_shortcode( $content );
|
|
|
|
wp_reset_postdata();
|
|
|
|
|
|
|
|
$content = \wpautop( $content );
|
|
|
|
$content = \preg_replace( '/[\n\r\t]/', '', $content );
|
|
|
|
$content = \trim( $content );
|
|
|
|
|
|
|
|
$content = \apply_filters( 'activitypub_the_content', $content, $post );
|
|
|
|
|
|
|
|
// Don't need these any more, should never appear in a post.
|
|
|
|
Shortcodes::unregister();
|
|
|
|
|
|
|
|
return $content;
|
|
|
|
}
|
|
|
|
|
2023-11-25 10:37:54 +01:00
|
|
|
/**
|
|
|
|
* Gets the template to use to generate the content of the activitypub item.
|
|
|
|
*
|
|
|
|
* @return string The Template.
|
|
|
|
*/
|
|
|
|
protected function get_post_content_template() {
|
2023-12-12 17:10:52 +01:00
|
|
|
$type = \get_option( 'activitypub_post_content_type', 'content' );
|
|
|
|
|
|
|
|
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-11-25 10:37:54 +01:00
|
|
|
}
|
|
|
|
|
2023-12-12 17:10:52 +01:00
|
|
|
return apply_filters( 'activitypub_object_content_template', $template, $this->wp_post );
|
2023-11-25 10:37:54 +01:00
|
|
|
}
|
2023-11-20 18:15:54 +01:00
|
|
|
|
2023-11-22 13:59:39 +01:00
|
|
|
/**
|
2023-11-20 18:15:54 +01: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() {
|
|
|
|
$post = $this->wp_post;
|
|
|
|
|
|
|
|
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 );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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() {
|
2023-12-11 19:43:50 +01:00
|
|
|
// todo is this descriptive enough? what if for some people the event should be attributed to the venue?
|
|
|
|
// i would maybe prefer something like "get post author??"
|
2023-12-09 02:01:13 +01:00
|
|
|
if ( is_single_user() ) { // todo what is if this setting gets changed
|
2023-11-20 18:15:54 +01:00
|
|
|
$user = new Blog_User();
|
|
|
|
return $user->get_url();
|
|
|
|
}
|
|
|
|
|
|
|
|
return Users::get_by_id( $this->wp_post->post_author )->get_url();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Generates all Media Attachments for a Post.
|
|
|
|
*
|
|
|
|
* @return array The Attachments.
|
|
|
|
*/
|
|
|
|
protected function get_attachments() {
|
|
|
|
// 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 ) ) );
|
|
|
|
|
|
|
|
if ( site_supports_blocks() && \has_blocks( $this->wp_post->post_content ) ) {
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
protected function get_block_attachments( $max_media ) {
|
|
|
|
// max media can't be negative or zero
|
|
|
|
if ( $max_media <= 0 ) {
|
|
|
|
return array();
|
|
|
|
}
|
|
|
|
|
|
|
|
$id = $this->wp_post->ID;
|
|
|
|
|
|
|
|
$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-11-22 13:59:39 +01:00
|
|
|
$blocks = \parse_blocks( $this->wp_post->post_content );
|
2023-11-20 18:15:54 +01: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 ) );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get image attachments from the classic editor.
|
|
|
|
* Note that audio/video attachments are only supported in the block editor.
|
|
|
|
*
|
|
|
|
* @param int $max_images The maximum number of images to return.
|
|
|
|
*
|
|
|
|
* @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();
|
|
|
|
}
|
|
|
|
|
|
|
|
$id = $this->wp_post->ID;
|
|
|
|
|
|
|
|
$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(
|
2023-11-22 13:59:39 +01:00
|
|
|
'post_parent' => $id,
|
|
|
|
'post_status' => 'inherit',
|
|
|
|
'post_type' => 'attachment',
|
2023-11-20 18:15:54 +01:00
|
|
|
'post_mime_type' => 'image',
|
2023-11-22 13:59:39 +01:00
|
|
|
'order' => 'ASC',
|
|
|
|
'orderby' => 'menu_order ID',
|
2023-11-20 18:15:54 +01:00
|
|
|
'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.
|
2023-11-22 13:59:39 +01:00
|
|
|
*
|
2023-11-20 18:15:54 +01:00
|
|
|
* @param array $blocks The blocks to search for media IDs
|
|
|
|
* @param array $media_ids The media IDs to append new IDs to
|
2023-11-22 13:59:39 +01:00
|
|
|
* @param int $max_media The maximum number of media to return.
|
2023-11-20 18:15:54 +01:00
|
|
|
*
|
|
|
|
* @return array The image IDs.
|
|
|
|
*/
|
|
|
|
protected static function get_media_ids_from_blocks( $blocks, $media_ids, $max_media ) {
|
|
|
|
|
|
|
|
foreach ( $blocks as $block ) {
|
|
|
|
// recurse into inner blocks
|
|
|
|
if ( ! empty( $block['innerBlocks'] ) ) {
|
|
|
|
$media_ids = self::get_media_ids_from_blocks( $block['innerBlocks'], $media_ids, $max_media );
|
|
|
|
}
|
|
|
|
|
|
|
|
switch ( $block['blockName'] ) {
|
|
|
|
case 'core/image':
|
|
|
|
case 'core/cover':
|
|
|
|
case 'core/audio':
|
|
|
|
case 'core/video':
|
|
|
|
case 'videopress/video':
|
|
|
|
if ( ! empty( $block['attrs']['id'] ) ) {
|
|
|
|
$media_ids[] = $block['attrs']['id'];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'jetpack/slideshow':
|
|
|
|
case 'jetpack/tiled-gallery':
|
|
|
|
if ( ! empty( $block['attrs']['ids'] ) ) {
|
|
|
|
$media_ids = array_merge( $media_ids, $block['attrs']['ids'] );
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case 'jetpack/image-compare':
|
|
|
|
if ( ! empty( $block['attrs']['beforeImageId'] ) ) {
|
|
|
|
$media_ids[] = $block['attrs']['beforeImageId'];
|
|
|
|
}
|
|
|
|
if ( ! empty( $block['attrs']['afterImageId'] ) ) {
|
|
|
|
$media_ids[] = $block['attrs']['afterImageId'];
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2023-11-21 13:50:57 +01:00
|
|
|
// depupe
|
|
|
|
$media_ids = \array_unique( $media_ids );
|
|
|
|
|
2023-11-20 18:15:54 +01:00
|
|
|
// stop doing unneeded work
|
|
|
|
if ( count( $media_ids ) >= $max_media ) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// still need to slice it because one gallery could knock us over the limit
|
|
|
|
return array_slice( $media_ids, 0, $max_media );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Converts a WordPress Attachment to an ActivityPub Attachment.
|
|
|
|
*
|
|
|
|
* @param int $id The Attachment ID.
|
|
|
|
*
|
|
|
|
* @return array The ActivityPub Attachment.
|
|
|
|
*/
|
|
|
|
public static function wp_attachment_to_activity_attachment( $id ) {
|
2023-11-22 13:59:39 +01:00
|
|
|
$attachment = array();
|
|
|
|
$mime_type = \get_post_mime_type( $id );
|
2023-11-20 18:15:54 +01:00
|
|
|
$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',
|
|
|
|
self::get_image( $id, $image_size ),
|
|
|
|
$id,
|
|
|
|
$image_size
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( $thumbnail ) {
|
|
|
|
$alt = \get_post_meta( $id, '_wp_attachment_image_alt', true );
|
|
|
|
$image = array(
|
|
|
|
'type' => 'Image',
|
|
|
|
'url' => $thumbnail[0],
|
|
|
|
'mediaType' => $mime_type,
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( $alt ) {
|
|
|
|
$image['name'] = $alt;
|
|
|
|
}
|
|
|
|
$attachment = $image;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
|
|
|
case 'audio':
|
|
|
|
case 'video':
|
|
|
|
$attachment = array(
|
|
|
|
'type' => 'Document',
|
|
|
|
'mediaType' => $mime_type,
|
|
|
|
'url' => \wp_get_attachment_url( $id ),
|
|
|
|
'name' => \get_the_title( $id ),
|
|
|
|
);
|
2023-11-22 13:59:39 +01:00
|
|
|
$meta = wp_get_attachment_metadata( $id );
|
2023-11-20 18:15:54 +01:00
|
|
|
// height and width for videos
|
|
|
|
if ( isset( $meta['width'] ) && isset( $meta['height'] ) ) {
|
2023-11-22 13:59:39 +01:00
|
|
|
$attachment['width'] = $meta['width'];
|
2023-11-20 18:15:54 +01:00
|
|
|
$attachment['height'] = $meta['height'];
|
|
|
|
}
|
|
|
|
// @todo: add `icon` support for audio/video attachments. Maybe use post thumbnail?
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
return \apply_filters( 'activitypub_attachment', $attachment, $id );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Return details about an image attachment.
|
|
|
|
*
|
|
|
|
* @param int $id The attachment ID.
|
|
|
|
* @param string $image_size The image size to retrieve. Set to 'full' by default.
|
|
|
|
*
|
|
|
|
* @return array|false Array of image data, or boolean false if no image is available.
|
|
|
|
*/
|
|
|
|
protected static function get_image( $id, $image_size = 'full' ) {
|
|
|
|
/**
|
|
|
|
* Hook into the image retrieval process. Before image retrieval.
|
|
|
|
*
|
|
|
|
* @param int $id The attachment ID.
|
|
|
|
* @param string $image_size The image size to retrieve. Set to 'full' by default.
|
|
|
|
*/
|
|
|
|
do_action( 'activitypub_get_image_pre', $id, $image_size );
|
|
|
|
|
|
|
|
$image = \wp_get_attachment_image_src( $id, $image_size );
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Hook into the image retrieval process. After image retrieval.
|
|
|
|
*
|
|
|
|
* @param int $id The attachment ID.
|
|
|
|
* @param string $image_size The image size to retrieve. Set to 'full' by default.
|
|
|
|
*/
|
|
|
|
do_action( 'activitypub_get_image_post', $id, $image_size );
|
|
|
|
|
|
|
|
return $image;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Helper function to get the @-Mentions from the post content.
|
|
|
|
*
|
|
|
|
* @return array The list of @-Mentions.
|
|
|
|
*/
|
|
|
|
protected function get_mentions() {
|
|
|
|
return apply_filters( 'activitypub_extract_mentions', array(), $this->wp_post->post_content, $this->wp_post );
|
|
|
|
}
|
|
|
|
|
2023-12-04 19:42:38 +01:00
|
|
|
/**
|
|
|
|
* Returns a list of Mentions, used in the Post.
|
|
|
|
*
|
|
|
|
* @see https://docs.joinmastodon.org/spec/activitypub/#Mention
|
|
|
|
*
|
|
|
|
* @return array The list of Mentions.
|
|
|
|
*/
|
2023-11-20 18:15:54 +01:00
|
|
|
protected function get_cc() {
|
|
|
|
$cc = array();
|
|
|
|
|
|
|
|
$mentions = $this->get_mentions();
|
|
|
|
if ( $mentions ) {
|
|
|
|
foreach ( $mentions as $url ) {
|
|
|
|
$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.
|
|
|
|
*/
|
|
|
|
protected function get_tags() {
|
|
|
|
$tags = array();
|
|
|
|
|
|
|
|
$post_tags = \get_the_tags( $this->wp_post->ID );
|
|
|
|
if ( $post_tags ) {
|
|
|
|
foreach ( $post_tags as $post_tag ) {
|
2023-11-22 13:59:39 +01:00
|
|
|
$tag = array(
|
2023-11-20 18:15:54 +01:00
|
|
|
'type' => 'Hashtag',
|
|
|
|
'href' => \esc_url( \get_tag_link( $post_tag->term_id ) ),
|
|
|
|
'name' => esc_hashtag( $post_tag->name ),
|
|
|
|
);
|
|
|
|
$tags[] = $tag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
$mentions = $this->get_mentions();
|
|
|
|
if ( $mentions ) {
|
|
|
|
foreach ( $mentions as $mention => $url ) {
|
2023-11-22 13:59:39 +01:00
|
|
|
$tag = array(
|
2023-11-20 18:15:54 +01:00
|
|
|
'type' => 'Mention',
|
|
|
|
'href' => \esc_url( $url ),
|
|
|
|
'name' => \esc_html( $mention ),
|
|
|
|
);
|
|
|
|
$tags[] = $tag;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return $tags;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Returns the locale of the post.
|
|
|
|
*
|
|
|
|
* @return string The locale of the post.
|
|
|
|
*/
|
|
|
|
public function get_locale() {
|
|
|
|
$post_id = $this->wp_post->ID;
|
|
|
|
$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.
|
|
|
|
*/
|
|
|
|
return apply_filters( 'activitypub_post_locale', $lang, $post_id, $this->wp_post );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Gets the contentMap
|
2023-11-22 13:59:39 +01:00
|
|
|
*
|
2023-11-20 18:15:54 +01:00
|
|
|
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-contentmap
|
2023-11-22 13:59:39 +01:00
|
|
|
*
|
2023-11-20 18:15:54 +01:00
|
|
|
* @return array the contenmap
|
|
|
|
*/
|
2023-12-09 02:01:13 +01:00
|
|
|
protected function get_basic_content_map() {
|
2023-11-20 18:15:54 +01:00
|
|
|
return array(
|
2023-12-11 19:43:50 +01:00
|
|
|
$this->get_locale() => $this->get_content(), // todo is this a valid assumption?
|
2023-11-20 18:15:54 +01:00
|
|
|
);
|
|
|
|
}
|
2023-11-22 13:59:39 +01:00
|
|
|
}
|