modified file upgrade-temp-backup
This commit is contained in:
@ -0,0 +1,157 @@
|
||||
<?php
|
||||
/**
|
||||
* Activity Object Transformer Class.
|
||||
*
|
||||
* @package Activitypub
|
||||
*/
|
||||
|
||||
namespace Activitypub\Transformer;
|
||||
|
||||
/**
|
||||
* Activity Object Transformer Class.
|
||||
*/
|
||||
class Activity_Object extends Base {
|
||||
/**
|
||||
* The Activity Object.
|
||||
*
|
||||
* @var \Activitypub\Activity\Activity
|
||||
*/
|
||||
protected $item;
|
||||
|
||||
/**
|
||||
* Transform the WordPress Object into an ActivityPub Object.
|
||||
*
|
||||
* @return \Activitypub\Activity\Base_Object|\WP_Error
|
||||
*/
|
||||
public function to_object() {
|
||||
$activity_object = $this->transform_object_properties( $this->item );
|
||||
|
||||
if ( \is_wp_error( $activity_object ) ) {
|
||||
return $activity_object;
|
||||
}
|
||||
|
||||
return $this->set_audience( $activity_object );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the ID of the object.
|
||||
*
|
||||
* @return string The ID of the object.
|
||||
*/
|
||||
public function get_id() {
|
||||
return $this->item->get_id();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the attributed to.
|
||||
*
|
||||
* @return string The attributed to.
|
||||
*/
|
||||
public function get_attributed_to() {
|
||||
return $this->item->get_attributed_to();
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to get the @-Mentions from the post content.
|
||||
*
|
||||
* @return array The list of @-Mentions.
|
||||
*/
|
||||
protected function get_mentions() {
|
||||
/**
|
||||
* Filter the mentions in the post content.
|
||||
*
|
||||
* @param array $mentions The mentions.
|
||||
* @param string $content The post content.
|
||||
* @param \Activitypub\Activity\Activity $item The Activity object.
|
||||
*
|
||||
* @return array The filtered mentions.
|
||||
*/
|
||||
return apply_filters(
|
||||
'activitypub_extract_mentions',
|
||||
array(),
|
||||
$this->item->get_content() . ' ' . $this->item->get_summary(),
|
||||
$this->item
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the content map for the post.
|
||||
*
|
||||
* @return array The content map for the post.
|
||||
*/
|
||||
protected function get_content_map() {
|
||||
$content = $this->item->get_content();
|
||||
|
||||
if ( ! $content ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return array(
|
||||
$this->get_locale() => $content,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name map for the post.
|
||||
*
|
||||
* @return array The name map for the post.
|
||||
*/
|
||||
protected function get_name_map() {
|
||||
$name = $this->item->get_name();
|
||||
|
||||
if ( ! $name ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return array(
|
||||
$this->get_locale() => $name,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the summary map for the post.
|
||||
*
|
||||
* @return array The summary map for the post.
|
||||
*/
|
||||
protected function get_summary_map() {
|
||||
$summary = $this->item->get_summary();
|
||||
|
||||
if ( ! $summary ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return array(
|
||||
$this->get_locale() => $summary,
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of Tags, used in the Comment.
|
||||
*
|
||||
* This includes Hash-Tags and Mentions.
|
||||
*
|
||||
* @return array The list of Tags.
|
||||
*/
|
||||
protected function get_tag() {
|
||||
$tags = $this->item->get_tag();
|
||||
|
||||
if ( ! $tags ) {
|
||||
$tags = array();
|
||||
}
|
||||
|
||||
$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;
|
||||
}
|
||||
}
|
||||
|
||||
return \array_unique( $tags, SORT_REGULAR );
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,68 @@
|
||||
<?php
|
||||
/**
|
||||
* Attachment Transformer Class file.
|
||||
*
|
||||
* @package Activitypub
|
||||
*/
|
||||
|
||||
namespace Activitypub\Transformer;
|
||||
|
||||
/**
|
||||
* WordPress Attachment Transformer.
|
||||
*
|
||||
* The Attachment Transformer is responsible for transforming a WP_Post object into different other
|
||||
* Object-Types.
|
||||
*
|
||||
* Currently supported are:
|
||||
*
|
||||
* - Activitypub\Activity\Base_Object
|
||||
*/
|
||||
class Attachment extends Post {
|
||||
/**
|
||||
* Generates all Media Attachments for a Post.
|
||||
*
|
||||
* @return array The Attachments.
|
||||
*/
|
||||
protected function get_attachment() {
|
||||
$mime_type = \get_post_mime_type( $this->item->ID );
|
||||
$mime_type_parts = \explode( '/', $mime_type );
|
||||
$type = '';
|
||||
|
||||
switch ( $mime_type_parts[0] ) {
|
||||
case 'audio':
|
||||
$type = 'Audio';
|
||||
break;
|
||||
case 'video':
|
||||
$type = 'Video';
|
||||
break;
|
||||
case 'image':
|
||||
$type = 'Image';
|
||||
break;
|
||||
}
|
||||
|
||||
$attachment = array(
|
||||
'type' => $type,
|
||||
'url' => wp_get_attachment_url( $this->item->ID ),
|
||||
'mediaType' => $mime_type,
|
||||
);
|
||||
|
||||
$alt = \get_post_meta( $this->item->ID, '_wp_attachment_image_alt', true );
|
||||
if ( $alt ) {
|
||||
$attachment['name'] = $alt;
|
||||
}
|
||||
|
||||
return $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() {
|
||||
return 'Note';
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,736 @@
|
||||
<?php
|
||||
/**
|
||||
* Base Transformer Class file.
|
||||
*
|
||||
* @package Activitypub
|
||||
*/
|
||||
|
||||
namespace Activitypub\Transformer;
|
||||
|
||||
use Activitypub\Activity\Activity;
|
||||
use Activitypub\Activity\Base_Object;
|
||||
use Activitypub\Collection\Actors;
|
||||
use Activitypub\Http;
|
||||
|
||||
use function Activitypub\get_upload_baseurl;
|
||||
use function Activitypub\object_to_uri;
|
||||
|
||||
/**
|
||||
* WordPress Base Transformer.
|
||||
*
|
||||
* Transformers are responsible for transforming WordPress objects into different ActivityPub
|
||||
* Object-Types or Activities.
|
||||
*
|
||||
* @method string|null get_content() Returns the content for the transformed item.
|
||||
* @method string|array|null get_icon() Returns an icon for the transformed item.
|
||||
* @method string|null get_id() Returns the ID for the transformed item.
|
||||
* @method string|null get_name() Returns the name for the transformed item.
|
||||
* @method string|null get_summary() Returns the summary for the transformed item.
|
||||
*/
|
||||
abstract class Base {
|
||||
/**
|
||||
* The WP_Post or WP_Comment object.
|
||||
*
|
||||
* This is the source object of the transformer.
|
||||
*
|
||||
* @var \WP_Post|\WP_Comment|Base_Object|string|array|\WP_Term
|
||||
*/
|
||||
protected $item;
|
||||
|
||||
/**
|
||||
* The WP_Post or WP_Comment object.
|
||||
*
|
||||
* @deprecated version 5.0.0
|
||||
*
|
||||
* @var \WP_Post|\WP_Comment
|
||||
*/
|
||||
protected $wp_object;
|
||||
|
||||
/**
|
||||
* The content visibility.
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
protected $content_visibility;
|
||||
|
||||
/**
|
||||
* Static function to Transform a WordPress Object.
|
||||
*
|
||||
* This helps to chain the output of the Transformer.
|
||||
*
|
||||
* @param \WP_Post|\WP_Comment|Base_Object|string|array|\WP_term $item The item that should be transformed.
|
||||
*
|
||||
* @return Base
|
||||
*/
|
||||
public static function transform( $item ) {
|
||||
return new static( $item );
|
||||
}
|
||||
|
||||
/**
|
||||
* Base constructor.
|
||||
*
|
||||
* @param \WP_Post|\WP_Comment|Base_Object|string|array|\WP_Term $item The item that should be transformed.
|
||||
*/
|
||||
public function __construct( $item ) {
|
||||
$this->item = $item;
|
||||
$this->wp_object = $item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform all properties with available get(ter) functions.
|
||||
*
|
||||
* @param Base_Object $activity_object The ActivityPub Object.
|
||||
*
|
||||
* @return Base_Object|\WP_Error The transformed ActivityPub Object or WP_Error on failure.
|
||||
*/
|
||||
protected function transform_object_properties( $activity_object ) {
|
||||
if ( ! $activity_object || \is_wp_error( $activity_object ) ) {
|
||||
return $activity_object;
|
||||
}
|
||||
|
||||
// Save activity in the context of an activitypub request.
|
||||
\add_filter( 'activitypub_is_activitypub_request', '__return_true' );
|
||||
|
||||
$vars = $activity_object->get_object_var_keys();
|
||||
|
||||
foreach ( $vars as $var ) {
|
||||
$getter = 'get_' . $var;
|
||||
|
||||
if ( \method_exists( $this, $getter ) ) {
|
||||
$value = \call_user_func( array( $this, $getter ) );
|
||||
|
||||
if ( null !== $value ) {
|
||||
$setter = 'set_' . $var;
|
||||
|
||||
/**
|
||||
* Filter the value before it is set to the Activity-Object `$activity_object`.
|
||||
*
|
||||
* @param mixed $value The value that should be set.
|
||||
* @param mixed $item The Object.
|
||||
*/
|
||||
$value = \apply_filters( "activitypub_transform_{$setter}", $value, $this->item );
|
||||
|
||||
/**
|
||||
* Filter the value before it is set to the Activity-Object `$activity_object`.
|
||||
*
|
||||
* @param mixed $value The value that should be set.
|
||||
* @param string $var The variable name.
|
||||
* @param mixed $item The Object.
|
||||
*/
|
||||
$value = \apply_filters( 'activitypub_transform_set', $value, $var, $this->item );
|
||||
|
||||
\call_user_func( array( $activity_object, $setter ), $value );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Remove activity in the context of an activitypub request.
|
||||
\remove_filter( 'activitypub_is_activitypub_request', '__return_true' );
|
||||
|
||||
return $activity_object;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform the item into an ActivityPub Object.
|
||||
*
|
||||
* @return Base_Object The Activity-Object.
|
||||
*/
|
||||
public function to_object() {
|
||||
$activity_object = new Base_Object();
|
||||
$activity_object = $this->transform_object_properties( $activity_object );
|
||||
|
||||
if ( \is_wp_error( $activity_object ) ) {
|
||||
return $activity_object;
|
||||
}
|
||||
|
||||
return $this->set_audience( $activity_object );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the content visibility.
|
||||
*
|
||||
* @return string The content visibility.
|
||||
*/
|
||||
public function get_content_visibility() {
|
||||
if ( ! $this->content_visibility ) {
|
||||
return ACTIVITYPUB_CONTENT_VISIBILITY_PUBLIC;
|
||||
}
|
||||
|
||||
return $this->content_visibility;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the content visibility.
|
||||
*
|
||||
* @param string $content_visibility The content visibility.
|
||||
*/
|
||||
public function set_content_visibility( $content_visibility ) {
|
||||
$this->content_visibility = $content_visibility;
|
||||
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the audience.
|
||||
*
|
||||
* @param Base_Object $activity_object The ActivityPub Object.
|
||||
*
|
||||
* @return Base_Object The ActivityPub Object.
|
||||
*/
|
||||
protected function set_audience( $activity_object ) {
|
||||
$public = 'https://www.w3.org/ns/activitystreams#Public';
|
||||
$followers = null;
|
||||
$replied_to = null;
|
||||
|
||||
$actor = Actors::get_by_resource( $this->get_attributed_to() );
|
||||
if ( ! \is_wp_error( $actor ) ) {
|
||||
$followers = $actor->get_followers();
|
||||
}
|
||||
|
||||
$mentions = array_values( $this->get_mentions() );
|
||||
|
||||
if ( $this->get_in_reply_to() ) {
|
||||
$object = Http::get_remote_object( $this->get_in_reply_to() );
|
||||
if ( $object && ! \is_wp_error( $object ) && isset( $object['attributedTo'] ) ) {
|
||||
$replied_to = array( object_to_uri( $object['attributedTo'] ) );
|
||||
}
|
||||
}
|
||||
|
||||
switch ( $this->get_content_visibility() ) {
|
||||
case ACTIVITYPUB_CONTENT_VISIBILITY_PUBLIC:
|
||||
$activity_object->add_to( $public );
|
||||
$activity_object->add_cc( $followers );
|
||||
$activity_object->add_cc( $mentions );
|
||||
$activity_object->add_cc( $replied_to );
|
||||
break;
|
||||
case ACTIVITYPUB_CONTENT_VISIBILITY_QUIET_PUBLIC:
|
||||
$activity_object->add_to( $followers );
|
||||
$activity_object->add_to( $mentions );
|
||||
$activity_object->add_to( $replied_to );
|
||||
$activity_object->add_cc( $public );
|
||||
break;
|
||||
case ACTIVITYPUB_CONTENT_VISIBILITY_PRIVATE:
|
||||
$activity_object->add_to( $mentions );
|
||||
$activity_object->add_to( $replied_to );
|
||||
}
|
||||
|
||||
return $activity_object;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform the item to an ActivityPub ID.
|
||||
*
|
||||
* @return string The ID of the WordPress Object.
|
||||
*/
|
||||
public function to_id() {
|
||||
/* @var Attachment|Comment|Json|Post|User $this Object transformer. */
|
||||
return $this->get_id();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a Tombstone object for the item.
|
||||
*
|
||||
* @return Base_Object The Tombstone object.
|
||||
*/
|
||||
public function to_tombstone() {
|
||||
$object = new Base_Object();
|
||||
$object->set_type( 'Tombstone' );
|
||||
$object->set_id( $this->to_id() );
|
||||
|
||||
return $object;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms the ActivityPub Object to an Activity
|
||||
*
|
||||
* @param string $type The Activity-Type.
|
||||
*
|
||||
* @return Activity The Activity.
|
||||
*/
|
||||
public function to_activity( $type ) {
|
||||
$object = $this->to_object();
|
||||
|
||||
$activity = new Activity();
|
||||
$activity->set_type( $type );
|
||||
|
||||
// Pre-fill the Activity with data (for example, cc and to).
|
||||
$activity->set_object( $object );
|
||||
|
||||
// Use simple Object (only ID-URI) for Like and Announce.
|
||||
if ( 'Like' === $type ) {
|
||||
$activity->set_object( $object->get_id() );
|
||||
}
|
||||
|
||||
return $activity;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a generic locale based on the Blog settings.
|
||||
*
|
||||
* @return string The locale of the blog.
|
||||
*/
|
||||
protected function get_locale() {
|
||||
$lang = \strtolower( \strtok( \get_locale(), '_-' ) );
|
||||
|
||||
/**
|
||||
* Filter the locale of the post.
|
||||
*
|
||||
* @param string $lang The locale of the post.
|
||||
* @param mixed $item The post object.
|
||||
*
|
||||
* @return string The filtered locale of the post.
|
||||
*/
|
||||
return apply_filters( 'activitypub_locale', $lang, $this->item );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the default media type for an Object.
|
||||
*
|
||||
* @return string The media type.
|
||||
*/
|
||||
public function get_media_type() {
|
||||
return 'text/html';
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the content map for the post.
|
||||
*
|
||||
* @return array|null The content map for the post or null if not set.
|
||||
*/
|
||||
protected function get_content_map() {
|
||||
if ( ! \method_exists( $this, 'get_content' ) || ! $this->get_content() ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return array(
|
||||
$this->get_locale() => $this->get_content(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name map for the post.
|
||||
*
|
||||
* @return array|null The name map for the post or null if not set.
|
||||
*/
|
||||
protected function get_name_map() {
|
||||
if ( ! \method_exists( $this, 'get_name' ) || ! $this->get_name() ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return array(
|
||||
$this->get_locale() => $this->get_name(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the summary map for the post.
|
||||
*
|
||||
* @return array|null The summary map for the post or null if not set.
|
||||
*/
|
||||
protected function get_summary_map() {
|
||||
if ( ! \method_exists( $this, 'get_summary' ) || ! $this->get_summary() ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return array(
|
||||
$this->get_locale() => $this->get_summary(),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the tags for the post.
|
||||
*
|
||||
* @return array The tags for the post.
|
||||
*/
|
||||
protected function get_tag() {
|
||||
$tags = array();
|
||||
$mentions = $this->get_mentions();
|
||||
|
||||
foreach ( $mentions as $mention => $url ) {
|
||||
$tags[] = array(
|
||||
'type' => 'Mention',
|
||||
'href' => \esc_url( $url ),
|
||||
'name' => \esc_html( $mention ),
|
||||
);
|
||||
}
|
||||
|
||||
return \array_unique( $tags, SORT_REGULAR );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the attributed to.
|
||||
*
|
||||
* @return string The attributed to.
|
||||
*/
|
||||
protected function get_attributed_to() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Extracts mentions from the content.
|
||||
*
|
||||
* @return array The mentions.
|
||||
*/
|
||||
protected function get_mentions() {
|
||||
$content = '';
|
||||
|
||||
if ( method_exists( $this, 'get_content' ) ) {
|
||||
$content = $content . ' ' . $this->get_content();
|
||||
}
|
||||
|
||||
if ( method_exists( $this, 'get_summary' ) ) {
|
||||
$content = $content . ' ' . $this->get_summary();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
return apply_filters(
|
||||
'activitypub_extract_mentions',
|
||||
array(),
|
||||
$content,
|
||||
$this->item
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the in reply to.
|
||||
*
|
||||
* @return string|array|null The in reply to.
|
||||
*/
|
||||
protected function get_in_reply_to() {
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse HTML content for image tags and extract attachment information.
|
||||
*
|
||||
* This method is used by both Post and Comment transformers to find images
|
||||
* embedded in HTML content and extract their attachment IDs and alt text.
|
||||
*
|
||||
* @param array $media The existing media array grouped by type.
|
||||
* @param int $max_images Maximum number of images to extract.
|
||||
* @param string $content The HTML content to parse.
|
||||
*
|
||||
* @return array The updated media array with found images.
|
||||
*/
|
||||
protected function parse_html_images( $media, $max_images, $content ) {
|
||||
// If someone calls that function directly, bail.
|
||||
if ( ! \class_exists( '\WP_HTML_Tag_Processor' ) ) {
|
||||
return $media;
|
||||
}
|
||||
|
||||
// Max images can't be negative or zero.
|
||||
if ( $max_images <= 0 ) {
|
||||
return $media;
|
||||
}
|
||||
|
||||
$images = array();
|
||||
$base = get_upload_baseurl();
|
||||
$tags = new \WP_HTML_Tag_Processor( $content );
|
||||
|
||||
// This linter warning is a false positive - we have to re-count each time here as we modify $images.
|
||||
// phpcs:ignore Squiz.PHP.DisallowSizeFunctionsInLoops.Found
|
||||
while ( $tags->next_tag( 'img' ) && ( \count( $images ) <= $max_images ) ) {
|
||||
/**
|
||||
* Filter the image source URL.
|
||||
*
|
||||
* This can be used to modify the image source URL before it is used to
|
||||
* determine the attachment ID.
|
||||
*
|
||||
* @param string $src The image source URL.
|
||||
*/
|
||||
$src = \apply_filters( 'activitypub_image_src', $tags->get_attribute( 'src' ) );
|
||||
|
||||
/*
|
||||
* If the img source is in our uploads dir, get the
|
||||
* associated ID. Note: if there's a -500x500
|
||||
* type suffix, we remove it, but we try the original
|
||||
* first in case the original image is actually called
|
||||
* that. Likewise, we try adding the -scaled suffix for
|
||||
* the case that this is a small version of an image
|
||||
* that was big enough to get scaled down on upload:
|
||||
* https://make.wordpress.org/core/2019/10/09/introducing-handling-of-big-images-in-wordpress-5-3/
|
||||
*/
|
||||
if ( null !== $src && \str_starts_with( $src, $base ) ) {
|
||||
$img_id = \attachment_url_to_postid( $src );
|
||||
|
||||
if ( 0 === $img_id ) {
|
||||
$count = 0;
|
||||
$src = \strtok( $src, '?' );
|
||||
$img_id = \attachment_url_to_postid( $src );
|
||||
}
|
||||
|
||||
if ( 0 === $img_id ) {
|
||||
$count = 0;
|
||||
$src = \preg_replace( '/-(?:\d+x\d+)(\.[a-zA-Z]+)$/', '$1', $src, 1, $count );
|
||||
if ( $count > 0 ) {
|
||||
$img_id = \attachment_url_to_postid( $src );
|
||||
}
|
||||
}
|
||||
|
||||
if ( 0 === $img_id ) {
|
||||
$src = \preg_replace( '/(\.[a-zA-Z]+)$/', '-scaled$1', $src );
|
||||
$img_id = \attachment_url_to_postid( $src );
|
||||
}
|
||||
|
||||
if ( 0 !== $img_id ) {
|
||||
$images[] = array(
|
||||
'id' => $img_id,
|
||||
'alt' => $tags->get_attribute( 'alt' ),
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if ( \count( $media['image'] ) <= $max_images ) {
|
||||
$media['image'] = \array_merge( $media['image'], $images );
|
||||
}
|
||||
|
||||
return $media;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transforms a WordPress attachment array to ActivityStreams attachment format.
|
||||
*
|
||||
* @param array $media The WordPress attachment array with 'id', optional 'alt', and optional 'icon'.
|
||||
*
|
||||
* @return array The ActivityStreams attachment array.
|
||||
*/
|
||||
protected function transform_attachment( $media ) {
|
||||
if ( ! isset( $media['id'] ) ) {
|
||||
return $media;
|
||||
}
|
||||
|
||||
$id = $media['id'];
|
||||
$attachment = array();
|
||||
$mime_type = \get_post_mime_type( $id );
|
||||
$media_type = \strtok( $mime_type, '/' );
|
||||
|
||||
// Switching on image/audio/video.
|
||||
switch ( $media_type ) {
|
||||
case 'image':
|
||||
$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 ) {
|
||||
$image = array(
|
||||
'type' => 'Image',
|
||||
'url' => \esc_url( $thumbnail[0] ),
|
||||
'mediaType' => \esc_attr( $mime_type ),
|
||||
);
|
||||
|
||||
if ( ! empty( $media['alt'] ) ) {
|
||||
$image['name'] = \html_entity_decode( \wp_strip_all_tags( $media['alt'] ), ENT_QUOTES, 'UTF-8' );
|
||||
} else {
|
||||
$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' );
|
||||
}
|
||||
}
|
||||
|
||||
// Add EXIF metadata using Schema.org exifData property (FEP-ee3a).
|
||||
$exif_data = $this->get_exif_data( $id );
|
||||
if ( $exif_data ) {
|
||||
$image['exifData'] = $exif_data;
|
||||
}
|
||||
|
||||
$attachment = $image;
|
||||
}
|
||||
break;
|
||||
|
||||
case 'audio':
|
||||
case 'video':
|
||||
$meta = \wp_get_attachment_metadata( $id );
|
||||
$attachment = array(
|
||||
'type' => \ucfirst( $media_type ),
|
||||
'mediaType' => \esc_attr( $mime_type ),
|
||||
'url' => \esc_url( \wp_get_attachment_url( $id ) ),
|
||||
'name' => \esc_attr( \get_the_title( $id ) ),
|
||||
);
|
||||
|
||||
// Height and width for videos.
|
||||
if ( isset( $meta['width'], $meta['height'] ) ) {
|
||||
$attachment['width'] = \esc_attr( $meta['width'] );
|
||||
$attachment['height'] = \esc_attr( $meta['height'] );
|
||||
}
|
||||
|
||||
// Use poster image from the block, or fall back to the transformer icon.
|
||||
if ( ! empty( $media['icon'] ) ) {
|
||||
$attachment['icon'] = \esc_url_raw( $media['icon'] );
|
||||
} elseif ( \method_exists( $this, 'get_icon' ) && $this->get_icon() ) {
|
||||
$attachment['icon'] = object_to_uri( $this->get_icon() );
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the attachment for a post.
|
||||
*
|
||||
* @param array $attachment The attachment.
|
||||
* @param int $id The attachment ID.
|
||||
*
|
||||
* @return array The filtered attachment.
|
||||
*/
|
||||
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 'large' by default.
|
||||
*
|
||||
* @return array|false Array of image data, or boolean false if no image is available.
|
||||
*/
|
||||
protected function get_attachment_image_src( $id, $image_size = 'large' ) {
|
||||
/**
|
||||
* 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 'large' 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 'large' by default.
|
||||
*/
|
||||
\do_action( 'activitypub_get_image_post', $id, $image_size );
|
||||
|
||||
return $image;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get EXIF metadata for an image attachment using Schema.org exifData property.
|
||||
*
|
||||
* Returns an array of PropertyValue objects as defined in FEP-ee3a.
|
||||
*
|
||||
* @link https://codeberg.org/fediverse/fep/src/branch/main/fep/ee3a/fep-ee3a.md
|
||||
*
|
||||
* @param int $attachment_id The attachment ID.
|
||||
*
|
||||
* @return array|null Array of PropertyValue objects or null if no EXIF data available.
|
||||
*/
|
||||
protected function get_exif_data( $attachment_id ) {
|
||||
$metadata = \wp_get_attachment_metadata( $attachment_id );
|
||||
|
||||
if ( empty( $metadata['image_meta'] ) ) {
|
||||
return null;
|
||||
}
|
||||
|
||||
$image_meta = $metadata['image_meta'];
|
||||
$exif_data = array();
|
||||
|
||||
// Map WordPress image_meta to FEP-ee3a EXIF field names.
|
||||
if ( ! empty( $image_meta['created_timestamp'] ) ) {
|
||||
$exif_data[] = array(
|
||||
'@type' => 'PropertyValue',
|
||||
'name' => 'DateTime',
|
||||
'value' => \gmdate( 'Y:m:d H:i:s', (int) $image_meta['created_timestamp'] ),
|
||||
);
|
||||
}
|
||||
|
||||
if ( ! empty( $image_meta['shutter_speed'] ) ) {
|
||||
$shutter_speed = (float) $image_meta['shutter_speed'];
|
||||
// Format shutter speed as a fraction (e.g., "1/100") for speeds faster than 1 second.
|
||||
if ( $shutter_speed > 0 && $shutter_speed < 1 ) {
|
||||
$value = '1/' . \round( 1 / $shutter_speed );
|
||||
} elseif ( $shutter_speed >= 1 ) {
|
||||
$value = (string) $shutter_speed;
|
||||
}
|
||||
if ( isset( $value ) ) {
|
||||
$exif_data[] = array(
|
||||
'@type' => 'PropertyValue',
|
||||
'name' => 'ExposureTime',
|
||||
'value' => $value,
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
if ( ! empty( $image_meta['aperture'] ) ) {
|
||||
$exif_data[] = array(
|
||||
'@type' => 'PropertyValue',
|
||||
'name' => 'FNumber',
|
||||
'value' => 'f/' . (float) $image_meta['aperture'],
|
||||
);
|
||||
}
|
||||
|
||||
if ( ! empty( $image_meta['focal_length'] ) ) {
|
||||
$exif_data[] = array(
|
||||
'@type' => 'PropertyValue',
|
||||
'name' => 'FocalLength',
|
||||
'value' => (string) (float) $image_meta['focal_length'],
|
||||
);
|
||||
}
|
||||
|
||||
if ( ! empty( $image_meta['iso'] ) ) {
|
||||
$exif_data[] = array(
|
||||
'@type' => 'PropertyValue',
|
||||
'name' => 'PhotographicSensitivity',
|
||||
'value' => (string) (int) $image_meta['iso'],
|
||||
);
|
||||
}
|
||||
|
||||
if ( ! empty( $image_meta['camera'] ) ) {
|
||||
$exif_data[] = array(
|
||||
'@type' => 'PropertyValue',
|
||||
'name' => 'Model',
|
||||
'value' => \sanitize_text_field( $image_meta['camera'] ),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the EXIF data for an image attachment.
|
||||
*
|
||||
* @param array $exif_data Array of PropertyValue objects for Schema.org exifData.
|
||||
* @param array $image_meta The WordPress image_meta array.
|
||||
* @param int $attachment_id The attachment ID.
|
||||
*
|
||||
* @return array The filtered EXIF data array.
|
||||
*/
|
||||
$exif_data = \apply_filters( 'activitypub_image_exif', $exif_data, $image_meta, $attachment_id );
|
||||
|
||||
return ! empty( $exif_data ) ? $exif_data : null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter attachments to ensure uniqueness based on their ID.
|
||||
*
|
||||
* @param array $attachments Array of attachments with 'id' field.
|
||||
*
|
||||
* @return array Array with duplicate attachments removed.
|
||||
*/
|
||||
protected function filter_unique_attachments( $attachments ) {
|
||||
$seen_ids = array();
|
||||
|
||||
return \array_filter(
|
||||
$attachments,
|
||||
static function ( $attachment ) use ( &$seen_ids ) {
|
||||
if ( isset( $attachment['id'] ) && ! in_array( $attachment['id'], $seen_ids, true ) ) {
|
||||
$seen_ids[] = $attachment['id'];
|
||||
return true;
|
||||
}
|
||||
return false;
|
||||
}
|
||||
);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,419 @@
|
||||
<?php
|
||||
/**
|
||||
* WordPress Comment Transformer file.
|
||||
*
|
||||
* @package Activitypub
|
||||
*/
|
||||
|
||||
namespace Activitypub\Transformer;
|
||||
|
||||
use Activitypub\Collection\Actors;
|
||||
use Activitypub\Collection\Replies;
|
||||
use Activitypub\Comment as Comment_Utils;
|
||||
use Activitypub\Model\Blog;
|
||||
use Activitypub\Sanitize;
|
||||
use Activitypub\Webfinger;
|
||||
|
||||
use function Activitypub\get_comment_ancestors;
|
||||
use function Activitypub\get_rest_url_by_path;
|
||||
use function Activitypub\is_single_user;
|
||||
use function Activitypub\was_comment_received;
|
||||
|
||||
/**
|
||||
* WordPress Comment Transformer.
|
||||
*
|
||||
* The Comment Transformer is responsible for transforming a WP_Comment object into different
|
||||
* Object-Types.
|
||||
*
|
||||
* Currently supported are:
|
||||
*
|
||||
* - Activitypub\Activity\Base_Object
|
||||
*/
|
||||
class Comment extends Base {
|
||||
/**
|
||||
* The User as Actor Object.
|
||||
*
|
||||
* @var \Activitypub\Activity\Actor
|
||||
*/
|
||||
private $actor_object = null;
|
||||
|
||||
/**
|
||||
* Transforms the WP_Comment object to an ActivityPub Object.
|
||||
*
|
||||
* @return \Activitypub\Activity\Base_Object The ActivityPub Object.
|
||||
*/
|
||||
public function to_object() {
|
||||
$comment = $this->item;
|
||||
$object = parent::to_object();
|
||||
|
||||
$object->set_url( $this->get_id() );
|
||||
$object->set_type( 'Note' );
|
||||
|
||||
$published = \strtotime( $comment->comment_date_gmt );
|
||||
$object->set_published( \gmdate( ACTIVITYPUB_DATE_TIME_RFC3339, $published ) );
|
||||
|
||||
$updated = \get_comment_meta( $comment->comment_ID, 'activitypub_comment_modified', true );
|
||||
if ( $updated > $published ) {
|
||||
$object->set_updated( \gmdate( ACTIVITYPUB_DATE_TIME_RFC3339, $updated ) );
|
||||
}
|
||||
|
||||
$object->set_content_map(
|
||||
array(
|
||||
$this->get_locale() => $this->get_content(),
|
||||
)
|
||||
);
|
||||
|
||||
return $object;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the content visibility.
|
||||
*
|
||||
* @return string The content visibility.
|
||||
*/
|
||||
public function get_content_visibility() {
|
||||
if ( $this->content_visibility ) {
|
||||
return $this->content_visibility;
|
||||
}
|
||||
|
||||
$comment = $this->item;
|
||||
$post = \get_post( $comment->comment_post_ID );
|
||||
|
||||
if ( ! $post ) {
|
||||
return ACTIVITYPUB_CONTENT_VISIBILITY_PUBLIC;
|
||||
}
|
||||
|
||||
$content_visibility = \get_post_meta( $post->ID, 'activitypub_content_visibility', true );
|
||||
|
||||
if ( ! $content_visibility ) {
|
||||
return ACTIVITYPUB_CONTENT_VISIBILITY_PUBLIC;
|
||||
}
|
||||
|
||||
$this->content_visibility = $content_visibility;
|
||||
|
||||
return $this->content_visibility;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 the comment was received via ActivityPub, return the author URL.
|
||||
if ( was_comment_received( $this->item ) ) {
|
||||
return $this->item->comment_author_url;
|
||||
}
|
||||
|
||||
return $this->get_actor_object()->get_id();
|
||||
}
|
||||
|
||||
/**
|
||||
* 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() {
|
||||
$comment = $this->item;
|
||||
$content = $comment->comment_content;
|
||||
$mentions = '';
|
||||
|
||||
foreach ( $this->extract_reply_context() as $acct => $url ) {
|
||||
$mentions .= sprintf(
|
||||
'<a rel="mention" class="u-url mention" href="%1$s" title="%2$s">%3$s</a> ',
|
||||
esc_url( $url ),
|
||||
esc_attr( $acct ),
|
||||
esc_html( '@' . strtok( $acct, '@' ) )
|
||||
);
|
||||
}
|
||||
$content = $mentions . $content;
|
||||
|
||||
/**
|
||||
* Filter the content of the comment.
|
||||
*
|
||||
* @param string $content The content of the comment.
|
||||
* @param \WP_Comment $comment The comment object.
|
||||
* @param array $args The arguments.
|
||||
*
|
||||
* @return string The filtered content of the comment.
|
||||
*/
|
||||
$content = \apply_filters( 'comment_text', $content, $comment, array() );
|
||||
$content = Sanitize::clean_html( $content );
|
||||
$content = Sanitize::strip_whitespace( $content );
|
||||
|
||||
/**
|
||||
* Filter the content of the comment.
|
||||
*
|
||||
* @param string $content The content of the comment.
|
||||
* @param \WP_Comment $comment The comment object.
|
||||
*
|
||||
* @return string The filtered content of the comment.
|
||||
*/
|
||||
return \apply_filters( 'activitypub_the_content', $content, $comment );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the in-reply-to for the ActivityPub Item.
|
||||
*
|
||||
* @return false|string|null The URL of the in-reply-to.
|
||||
*/
|
||||
protected function get_in_reply_to() {
|
||||
$comment = $this->item;
|
||||
$parent_comment = null;
|
||||
|
||||
if ( $comment->comment_parent ) {
|
||||
$parent_comment = \get_comment( $comment->comment_parent );
|
||||
}
|
||||
|
||||
if ( $parent_comment ) {
|
||||
$in_reply_to = Comment_Utils::get_source_id( $parent_comment->comment_ID );
|
||||
if ( ! $in_reply_to && ! empty( $parent_comment->user_id ) ) {
|
||||
$in_reply_to = Comment_Utils::generate_id( $parent_comment );
|
||||
}
|
||||
} else {
|
||||
$in_reply_to = \get_permalink( $comment->comment_post_ID );
|
||||
}
|
||||
|
||||
return $in_reply_to;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the ID of the ActivityPub Object.
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitypub/#obj-id
|
||||
* @see https://github.com/tootsuite/mastodon/issues/13879
|
||||
*
|
||||
* @return string ActivityPub URI for comment
|
||||
*/
|
||||
protected function get_id() {
|
||||
$comment = $this->item;
|
||||
return Comment_Utils::generate_id( $comment );
|
||||
}
|
||||
|
||||
/**
|
||||
* 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.
|
||||
*/
|
||||
protected 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->user_id );
|
||||
|
||||
if ( $user && ! is_wp_error( $user ) ) {
|
||||
$this->actor_object = $user;
|
||||
return $user;
|
||||
}
|
||||
|
||||
return $blog_user;
|
||||
}
|
||||
|
||||
/**
|
||||
* Helper function to get the @-Mentions from the comment content.
|
||||
*
|
||||
* @return array The list of @-Mentions.
|
||||
*/
|
||||
protected function get_mentions() {
|
||||
\add_filter( 'activitypub_extract_mentions', array( $this, 'extract_reply_context' ) );
|
||||
|
||||
/**
|
||||
* Filter the mentions in the comment.
|
||||
*
|
||||
* @param array $mentions The list of mentions.
|
||||
* @param string $content The content of the comment.
|
||||
* @param \WP_Comment $comment The comment object.
|
||||
*
|
||||
* @return array The filtered list of mentions.
|
||||
*/
|
||||
return apply_filters( 'activitypub_extract_mentions', array(), $this->item->comment_content, $this->item );
|
||||
}
|
||||
|
||||
/**
|
||||
* Gets the ancestors of the comment, but only the ones that are ActivityPub comments.
|
||||
*
|
||||
* @return array The list of ancestors.
|
||||
*/
|
||||
protected function get_comment_ancestors() {
|
||||
$ancestors = get_comment_ancestors( $this->item );
|
||||
|
||||
// Now that we have the full tree of ancestors, only return the ones received from the fediverse.
|
||||
return array_filter(
|
||||
$ancestors,
|
||||
static function ( $comment_id ) {
|
||||
return \get_comment_meta( $comment_id, 'protocol', true ) === 'activitypub';
|
||||
}
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Collect all other Users that participated in this comment-thread
|
||||
* to send them a notification about the new reply.
|
||||
*
|
||||
* @param array $mentions Optional. The already mentioned ActivityPub users. Default empty array.
|
||||
*
|
||||
* @return array The list of all Repliers.
|
||||
*/
|
||||
public function extract_reply_context( $mentions = array() ) {
|
||||
// Check if `$this->item` is a WP_Comment.
|
||||
if ( 'WP_Comment' !== get_class( $this->item ) ) {
|
||||
return $mentions;
|
||||
}
|
||||
|
||||
$ancestors = $this->get_comment_ancestors();
|
||||
if ( ! $ancestors ) {
|
||||
return $mentions;
|
||||
}
|
||||
|
||||
foreach ( $ancestors as $comment_id ) {
|
||||
$comment = \get_comment( $comment_id );
|
||||
if ( $comment && ! empty( $comment->comment_author_url ) ) {
|
||||
$acct = Webfinger::uri_to_acct( $comment->comment_author_url );
|
||||
if ( $acct && ! is_wp_error( $acct ) ) {
|
||||
$acct = str_replace( 'acct:', '@', $acct );
|
||||
$mentions[ $acct ] = $comment->comment_author_url;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return $mentions;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the updated date of the comment.
|
||||
*
|
||||
* @return string|null The updated date of the comment.
|
||||
*/
|
||||
public function get_updated() {
|
||||
$updated = \get_comment_meta( $this->item->comment_ID, 'activitypub_comment_modified', true );
|
||||
$published = \get_comment_meta( $this->item->comment_ID, 'activitypub_comment_published', true );
|
||||
|
||||
if ( $updated > $published ) {
|
||||
return \gmdate( ACTIVITYPUB_DATE_TIME_RFC3339, $updated );
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the published date of the comment.
|
||||
*
|
||||
* @return string The published date of the comment.
|
||||
*/
|
||||
public function get_published() {
|
||||
return \gmdate( ACTIVITYPUB_DATE_TIME_RFC3339, \strtotime( $this->item->comment_date_gmt ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the URL of the comment.
|
||||
*
|
||||
* @return string The URL of the comment.
|
||||
*/
|
||||
public function get_url() {
|
||||
return $this->get_id();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the type of the comment.
|
||||
*
|
||||
* @return string The type of the comment.
|
||||
*/
|
||||
public function get_type() {
|
||||
return 'Note';
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the context of the post.
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-context
|
||||
*
|
||||
* @return string The context of the post.
|
||||
*/
|
||||
protected function get_context() {
|
||||
if ( $this->item->comment_post_ID ) {
|
||||
return get_rest_url_by_path( sprintf( 'posts/%d/context', $this->item->comment_post_ID ) );
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the replies Collection.
|
||||
*
|
||||
* @return array|null The replies collection on success or null on failure.
|
||||
*/
|
||||
public function get_replies() {
|
||||
return Replies::get_collection( $this->item );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the attachment for the comment.
|
||||
*
|
||||
* Extracts images from comment content and returns them as ActivityPub attachments.
|
||||
*
|
||||
* @return array The attachments array for ActivityPub.
|
||||
*/
|
||||
protected function get_attachment() {
|
||||
$max_media = \get_option( 'activitypub_max_image_attachments', \ACTIVITYPUB_MAX_IMAGE_ATTACHMENTS );
|
||||
|
||||
/**
|
||||
* Filters the maximum number of media attachments allowed in a comment.
|
||||
*
|
||||
* @param int $max_media Maximum number of media attachments.
|
||||
* @param \WP_Comment $item The comment object.
|
||||
*/
|
||||
$max_media = (int) \apply_filters( 'activitypub_max_image_attachments', $max_media, $this->item );
|
||||
|
||||
if ( 0 === $max_media ) {
|
||||
return array();
|
||||
}
|
||||
|
||||
$media = array(
|
||||
'image' => array(),
|
||||
'audio' => array(),
|
||||
'video' => array(),
|
||||
);
|
||||
|
||||
// Get comment content and parse for image embeds.
|
||||
$media = $this->parse_html_images( $media, $max_media, $this->item->comment_content );
|
||||
$media = $this->filter_unique_attachments( $media['image'] );
|
||||
$media = \array_slice( $media, 0, $max_media );
|
||||
|
||||
/**
|
||||
* Filter the attachment IDs for a comment.
|
||||
*
|
||||
* @param array $media The media array.
|
||||
* @param \WP_Comment $item The comment object.
|
||||
*
|
||||
* @return array The filtered attachment IDs.
|
||||
*/
|
||||
$media = \apply_filters( 'activitypub_comment_attachment_ids', $media, $this->item );
|
||||
|
||||
// Transform to ActivityStreams format using Base class method.
|
||||
$attachments = \array_filter( \array_map( array( $this, 'transform_attachment' ), $media ) );
|
||||
|
||||
/**
|
||||
* Filter the attachments for a comment.
|
||||
*
|
||||
* @param array $attachments The attachments.
|
||||
* @param \WP_Comment $item The comment object.
|
||||
*
|
||||
* @return array The filtered attachments.
|
||||
*/
|
||||
return \apply_filters( 'activitypub_comment_attachments', $attachments, $this->item );
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,125 @@
|
||||
<?php
|
||||
/**
|
||||
* Transformer Factory Class file.
|
||||
*
|
||||
* @package Activitypub
|
||||
*/
|
||||
|
||||
namespace Activitypub\Transformer;
|
||||
|
||||
use Activitypub\Activity\Base_Object;
|
||||
use Activitypub\Comment as Comment_Helper;
|
||||
use Activitypub\Http;
|
||||
|
||||
use function Activitypub\get_user_id;
|
||||
use function Activitypub\is_post_disabled;
|
||||
use function Activitypub\user_can_activitypub;
|
||||
|
||||
/**
|
||||
* Transformer Factory.
|
||||
*/
|
||||
class Factory {
|
||||
/**
|
||||
* Get the transformer for a given object.
|
||||
*
|
||||
* @param mixed $data The object to transform.
|
||||
*
|
||||
* @return Base|\WP_Error The transformer to use, or an error.
|
||||
*/
|
||||
public static function get_transformer( $data ) {
|
||||
// Early return for WP_Error objects.
|
||||
if ( \is_wp_error( $data ) ) {
|
||||
return $data;
|
||||
}
|
||||
|
||||
if ( \is_string( $data ) && \filter_var( $data, FILTER_VALIDATE_URL ) ) {
|
||||
$response = Http::get_remote_object( $data );
|
||||
|
||||
if ( \is_wp_error( $response ) ) {
|
||||
return $response;
|
||||
}
|
||||
|
||||
$class = 'json';
|
||||
$data = $response;
|
||||
} elseif ( \is_array( $data ) || \is_string( $data ) ) {
|
||||
$class = 'json';
|
||||
} elseif ( \is_object( $data ) ) {
|
||||
$class = \get_class( $data );
|
||||
} else {
|
||||
return new \WP_Error( 'invalid_object', __( 'Invalid object', 'activitypub' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Filter the transformer for a given object.
|
||||
*
|
||||
* Add your own transformer based on the object class or the object type.
|
||||
*
|
||||
* Example usage:
|
||||
*
|
||||
* // Filter be object class
|
||||
* add_filter( 'activitypub_transformer', function( $transformer, $object, $object_class ) {
|
||||
* if ( $object_class === 'WP_Post' ) {
|
||||
* return new My_Post_Transformer( $object );
|
||||
* }
|
||||
* return $transformer;
|
||||
* }, 10, 3 );
|
||||
*
|
||||
* // Filter be object type
|
||||
* add_filter( 'activitypub_transformer', function( $transformer, $object, $object_class ) {
|
||||
* if ( $object->post_type === 'event' ) {
|
||||
* return new My_Event_Transformer( $object );
|
||||
* }
|
||||
* return $transformer;
|
||||
* }, 10, 3 );
|
||||
*
|
||||
* @param null|Base $transformer The transformer to use. Default null.
|
||||
* @param mixed $data The object to transform.
|
||||
* @param string $object_class The class of the object to transform.
|
||||
*
|
||||
* @return mixed The transformer to use.
|
||||
*/
|
||||
$transformer = \apply_filters( 'activitypub_transformer', null, $data, $class );
|
||||
|
||||
if ( $transformer ) {
|
||||
if (
|
||||
! \is_object( $transformer ) ||
|
||||
! $transformer instanceof Base
|
||||
) {
|
||||
return new \WP_Error( 'invalid_transformer', __( 'Invalid transformer', 'activitypub' ) );
|
||||
}
|
||||
|
||||
return $transformer;
|
||||
}
|
||||
|
||||
// Use default transformer.
|
||||
switch ( $class ) {
|
||||
case 'WP_Post':
|
||||
if ( 'attachment' === $data->post_type && ! is_post_disabled( $data ) ) {
|
||||
return new Attachment( $data );
|
||||
} elseif ( ! is_post_disabled( $data ) && get_user_id( $data->post_author ) ) {
|
||||
return new Post( $data );
|
||||
}
|
||||
break;
|
||||
case 'WP_Comment':
|
||||
if ( Comment_Helper::should_be_federated( $data ) ) {
|
||||
return new Comment( $data );
|
||||
}
|
||||
break;
|
||||
case 'WP_User':
|
||||
if ( user_can_activitypub( $data->ID ) ) {
|
||||
return new User( $data );
|
||||
}
|
||||
break;
|
||||
case 'WP_Term':
|
||||
return new Term( $data );
|
||||
case 'json':
|
||||
return new Json( $data );
|
||||
}
|
||||
|
||||
if ( $data instanceof Base_Object ) {
|
||||
return new Activity_Object( $data );
|
||||
}
|
||||
|
||||
return new \WP_Error( 'invalid_object', __( 'Invalid object', 'activitypub' ) );
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,41 @@
|
||||
<?php
|
||||
/**
|
||||
* String Transformer Class file.
|
||||
*
|
||||
* @package Activitypub
|
||||
*/
|
||||
|
||||
namespace Activitypub\Transformer;
|
||||
|
||||
use function Activitypub\is_activity;
|
||||
use function Activitypub\is_actor;
|
||||
|
||||
/**
|
||||
* String Transformer Class file.
|
||||
*/
|
||||
class Json extends Activity_Object {
|
||||
|
||||
/**
|
||||
* JSON constructor.
|
||||
*
|
||||
* @param string|array $item The item that should be transformed.
|
||||
*/
|
||||
public function __construct( $item ) {
|
||||
if ( \is_string( $item ) ) {
|
||||
$item = \json_decode( $item, true );
|
||||
}
|
||||
|
||||
// Check if the item is an Activity or an Object.
|
||||
if ( is_activity( $item ) ) {
|
||||
$class = '\Activitypub\Activity\Activity';
|
||||
} elseif ( is_actor( $item ) ) {
|
||||
$class = '\Activitypub\Activity\Actor';
|
||||
} else {
|
||||
$class = '\Activitypub\Activity\Base_Object';
|
||||
}
|
||||
|
||||
$object = $class::init_from_array( $item );
|
||||
|
||||
parent::__construct( $object );
|
||||
}
|
||||
}
|
||||
File diff suppressed because it is too large
Load Diff
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
/**
|
||||
* Term Transformer Class file.
|
||||
*
|
||||
* @package Activitypub
|
||||
*/
|
||||
|
||||
namespace Activitypub\Transformer;
|
||||
|
||||
/**
|
||||
* Term Transformer Class.
|
||||
*/
|
||||
class Term extends Base {
|
||||
/**
|
||||
* Transforms the WP_Term object to an OrderedCollection.
|
||||
*
|
||||
* @see \Activitypub\Activity\Base_Object
|
||||
*
|
||||
* @return \Activitypub\Activity\Base_Object|\WP_Error The OrderedCollection or WP_Error on failure.
|
||||
*/
|
||||
public function to_object() {
|
||||
$base_object = new \Activitypub\Activity\Base_Object();
|
||||
$base_object->{'@context'} = 'https://www.w3.org/ns/activitystreams';
|
||||
$base_object->set_type( 'OrderedCollection' );
|
||||
$base_object->set_id( $this->get_id() );
|
||||
$base_object->set_url( $this->get_url() );
|
||||
|
||||
return $base_object;
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the OrderedCollection ID.
|
||||
*
|
||||
* @return string The OrderedCollection ID.
|
||||
*/
|
||||
public function to_id() {
|
||||
return $this->get_id();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the stable ID of the Term.
|
||||
*
|
||||
* Uses term_id query parameter to ensure the ID remains stable
|
||||
* even if the term slug is changed.
|
||||
*
|
||||
* @return string The Term's stable ID.
|
||||
*/
|
||||
public function get_id() {
|
||||
return \add_query_arg( 'term_id', $this->item->term_id, \home_url( '/' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the URL of the Term.
|
||||
*
|
||||
* @return string The Term's URL (term link).
|
||||
*/
|
||||
public function get_url() {
|
||||
$term_link = \get_term_link( $this->item );
|
||||
|
||||
if ( \is_wp_error( $term_link ) ) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return \esc_url( $term_link );
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,35 @@
|
||||
<?php
|
||||
/**
|
||||
* User Transformer Class file.
|
||||
*
|
||||
* @package Activitypub
|
||||
*/
|
||||
|
||||
namespace Activitypub\Transformer;
|
||||
|
||||
use Activitypub\Collection\Actors;
|
||||
|
||||
/**
|
||||
* User Transformer Class.
|
||||
*/
|
||||
class User extends Base {
|
||||
/**
|
||||
* Transforms the WP_User object to an Actor.
|
||||
*
|
||||
* @see \Activitypub\Activity\Actor
|
||||
*
|
||||
* @return \Activitypub\Activity\Base_Object|\WP_Error The Actor or WP_Error on failure.
|
||||
*/
|
||||
public function to_object() {
|
||||
return $this->transform_object_properties( Actors::get_by_id( $this->item->ID ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the Actor ID.
|
||||
*
|
||||
* @return string The Actor ID.
|
||||
*/
|
||||
public function to_id() {
|
||||
return Actors::get_by_id( $this->item->ID )->get_id();
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user