updated plugin ActivityPub
version 5.8.0
This commit is contained in:
@ -0,0 +1,159 @@
|
||||
<?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;
|
||||
}
|
||||
|
||||
$activity_object = $this->set_audience( $activity_object );
|
||||
|
||||
return $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 );
|
||||
}
|
||||
}
|
@ -24,11 +24,11 @@ class Attachment extends Post {
|
||||
* @return array The Attachments.
|
||||
*/
|
||||
protected function get_attachment() {
|
||||
$mime_type = get_post_mime_type( $this->wp_object->ID );
|
||||
$media_type = preg_replace( '/(\/[a-zA-Z]+)/i', '', $mime_type );
|
||||
$type = '';
|
||||
$mime_type = \get_post_mime_type( $this->item->ID );
|
||||
$mime_type_parts = \explode( '/', $mime_type );
|
||||
$type = '';
|
||||
|
||||
switch ( $media_type ) {
|
||||
switch ( $mime_type_parts[0] ) {
|
||||
case 'audio':
|
||||
case 'video':
|
||||
$type = 'Document';
|
||||
@ -40,11 +40,11 @@ class Attachment extends Post {
|
||||
|
||||
$attachment = array(
|
||||
'type' => $type,
|
||||
'url' => wp_get_attachment_url( $this->wp_object->ID ),
|
||||
'url' => wp_get_attachment_url( $this->item->ID ),
|
||||
'mediaType' => $mime_type,
|
||||
);
|
||||
|
||||
$alt = \get_post_meta( $this->wp_object->ID, '_wp_attachment_image_alt', true );
|
||||
$alt = \get_post_meta( $this->item->ID, '_wp_attachment_image_alt', true );
|
||||
if ( $alt ) {
|
||||
$attachment['name'] = $alt;
|
||||
}
|
||||
|
@ -7,12 +7,13 @@
|
||||
|
||||
namespace Activitypub\Transformer;
|
||||
|
||||
use WP_Post;
|
||||
use WP_Comment;
|
||||
use WP_Post;
|
||||
use WP_Term;
|
||||
|
||||
use Activitypub\Activity\Activity;
|
||||
use Activitypub\Collection\Actors;
|
||||
use Activitypub\Activity\Base_Object;
|
||||
use Activitypub\Collection\Replies;
|
||||
|
||||
/**
|
||||
* WordPress Base Transformer.
|
||||
@ -26,67 +27,182 @@ abstract class Base {
|
||||
*
|
||||
* 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 $wp_object The WordPress object.
|
||||
* @param WP_Post|WP_Comment|Base_Object|string|array|WP_term $item The item that should be transformed.
|
||||
*
|
||||
* @return Base
|
||||
*/
|
||||
public static function transform( $wp_object ) {
|
||||
return new static( $wp_object );
|
||||
public static function transform( $item ) {
|
||||
return new static( $item );
|
||||
}
|
||||
|
||||
/**
|
||||
* Base constructor.
|
||||
*
|
||||
* @param WP_Post|WP_Comment $wp_object The WordPress object.
|
||||
* @param WP_Post|WP_Comment|Base_Object|string|array|WP_Term $item The item that should be transformed.
|
||||
*/
|
||||
public function __construct( $wp_object ) {
|
||||
$this->wp_object = $wp_object;
|
||||
public function __construct( $item ) {
|
||||
$this->item = $item;
|
||||
$this->wp_object = $item;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform all properties with available get(ter) functions.
|
||||
*
|
||||
* @param Base_Object|object $activitypub_object The ActivityPub Object.
|
||||
* @param Base_Object $activity_object The ActivityPub Object.
|
||||
*
|
||||
* @return Base_Object|object
|
||||
* @return Base_Object|\WP_Error The transformed ActivityPub Object or WP_Error on failure.
|
||||
*/
|
||||
protected function transform_object_properties( $activitypub_object ) {
|
||||
$vars = $activitypub_object->get_object_var_keys();
|
||||
protected function transform_object_properties( $activity_object ) {
|
||||
if ( ! $activity_object || \is_wp_error( $activity_object ) ) {
|
||||
return $activity_object;
|
||||
}
|
||||
|
||||
$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 ( \method_exists( $this, $getter ) ) {
|
||||
$value = \call_user_func( array( $this, $getter ) );
|
||||
|
||||
if ( isset( $value ) ) {
|
||||
if ( null !== $value ) {
|
||||
$setter = 'set_' . $var;
|
||||
|
||||
call_user_func( array( $activitypub_object, $setter ), $value );
|
||||
/**
|
||||
* 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 );
|
||||
}
|
||||
}
|
||||
}
|
||||
return $activitypub_object;
|
||||
|
||||
return $activity_object;
|
||||
}
|
||||
|
||||
/**
|
||||
* Transform the WordPress Object into an ActivityPub Object.
|
||||
* Transform the item into an ActivityPub Object.
|
||||
*
|
||||
* @return Base_Object|object The ActivityPub Object.
|
||||
* @return Base_Object|object The Activity-Object.
|
||||
*/
|
||||
public function to_object() {
|
||||
$activitypub_object = new Base_Object();
|
||||
$activity_object = new Base_Object();
|
||||
$activity_object = $this->transform_object_properties( $activity_object );
|
||||
|
||||
return $this->transform_object_properties( $activitypub_object );
|
||||
if ( \is_wp_error( $activity_object ) ) {
|
||||
return $activity_object;
|
||||
}
|
||||
|
||||
$activity_object = $this->set_audience( $activity_object );
|
||||
|
||||
return $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';
|
||||
$actor = Actors::get_by_resource( $this->get_attributed_to() );
|
||||
if ( ! $actor || is_wp_error( $actor ) ) {
|
||||
$followers = null;
|
||||
} else {
|
||||
$followers = $actor->get_followers();
|
||||
}
|
||||
$mentions = array_values( $this->get_mentions() );
|
||||
|
||||
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 );
|
||||
break;
|
||||
case ACTIVITYPUB_CONTENT_VISIBILITY_QUIET_PUBLIC:
|
||||
$activity_object->add_to( $followers );
|
||||
$activity_object->add_to( $mentions );
|
||||
$activity_object->add_cc( $public );
|
||||
break;
|
||||
case ACTIVITYPUB_CONTENT_VISIBILITY_PRIVATE:
|
||||
$activity_object->add_to( $mentions );
|
||||
}
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -106,7 +222,7 @@ abstract class Base {
|
||||
$activity->set_object( $object );
|
||||
|
||||
// Use simple Object (only ID-URI) for Like and Announce.
|
||||
if ( in_array( $type, array( 'Like', 'Announce' ), true ) ) {
|
||||
if ( 'Like' === $type ) {
|
||||
$activity->set_object( $object->get_id() );
|
||||
}
|
||||
|
||||
@ -114,26 +230,159 @@ abstract class Base {
|
||||
}
|
||||
|
||||
/**
|
||||
* Get the ID of the WordPress Object.
|
||||
* Returns a generic locale based on the Blog settings.
|
||||
*
|
||||
* @return string The locale of the blog.
|
||||
*/
|
||||
abstract protected function get_id();
|
||||
protected function get_locale() {
|
||||
$lang = \strtolower( \strtok( \get_locale(), '_-' ) );
|
||||
|
||||
/**
|
||||
* Get the replies Collection.
|
||||
*/
|
||||
public function get_replies() {
|
||||
return Replies::get_collection( $this->wp_object );
|
||||
if ( $this->item instanceof \WP_Post ) {
|
||||
/**
|
||||
* Deprecates the `activitypub_post_locale` filter.
|
||||
*
|
||||
* @param string $lang The locale of the post.
|
||||
* @param mixed $item The post object.
|
||||
*
|
||||
* @return string The filtered locale of the post.
|
||||
*/
|
||||
$lang = apply_filters_deprecated(
|
||||
'activitypub_post_locale',
|
||||
array(
|
||||
$lang,
|
||||
$this->item->ID,
|
||||
$this->item,
|
||||
),
|
||||
'5.4.0',
|
||||
'activitypub_locale',
|
||||
'Use the `activitypub_locale` filter instead.'
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* 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 ID of the WordPress Object.
|
||||
* Returns the default media type for an Object.
|
||||
*
|
||||
* @return string The media type.
|
||||
*/
|
||||
abstract public function get_wp_user_id();
|
||||
public function get_media_type() {
|
||||
return 'text/html';
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the User-ID of the WordPress Post.
|
||||
* Returns the content map for the post.
|
||||
*
|
||||
* @param int $user_id The new user ID.
|
||||
* @return array|null The content map for the post or null if not set.
|
||||
*/
|
||||
abstract public function change_wp_user_id( $user_id );
|
||||
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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
@ -10,10 +10,12 @@ namespace Activitypub\Transformer;
|
||||
use Activitypub\Webfinger;
|
||||
use Activitypub\Comment as Comment_Utils;
|
||||
use Activitypub\Model\Blog;
|
||||
use Activitypub\Collection\Users;
|
||||
use Activitypub\Collection\Actors;
|
||||
use Activitypub\Collection\Replies;
|
||||
|
||||
use function Activitypub\is_single_user;
|
||||
use function Activitypub\get_rest_url_by_path;
|
||||
use function Activitypub\was_comment_received;
|
||||
use function Activitypub\get_comment_ancestors;
|
||||
|
||||
/**
|
||||
@ -28,43 +30,30 @@ use function Activitypub\get_comment_ancestors;
|
||||
*/
|
||||
class Comment extends Base {
|
||||
/**
|
||||
* Returns the User-ID of the WordPress Comment.
|
||||
* The User as Actor Object.
|
||||
*
|
||||
* @return int The User-ID of the WordPress Comment
|
||||
* @var \Activitypub\Activity\Actor
|
||||
*/
|
||||
public function get_wp_user_id() {
|
||||
return $this->wp_object->user_id;
|
||||
}
|
||||
|
||||
/**
|
||||
* Change the User-ID of the WordPress Comment.
|
||||
*
|
||||
* @param int $user_id The new user ID.
|
||||
*/
|
||||
public function change_wp_user_id( $user_id ) {
|
||||
$this->wp_object->user_id = $user_id;
|
||||
}
|
||||
private $actor_object = null;
|
||||
|
||||
/**
|
||||
* Transforms the WP_Comment object to an ActivityPub Object.
|
||||
*
|
||||
* @see \Activitypub\Activity\Base_Object
|
||||
*
|
||||
* @return \Activitypub\Activity\Base_Object The ActivityPub Object.
|
||||
*/
|
||||
public function to_object() {
|
||||
$comment = $this->wp_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( 'Y-m-d\TH:i:s\Z', $published ) );
|
||||
$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( 'Y-m-d\TH:i:s\Z', $updated ) );
|
||||
$object->set_updated( \gmdate( ACTIVITYPUB_DATE_TIME_RFC3339, $updated ) );
|
||||
}
|
||||
|
||||
$object->set_content_map(
|
||||
@ -72,18 +61,38 @@ class Comment extends Base {
|
||||
$this->get_locale() => $this->get_content(),
|
||||
)
|
||||
);
|
||||
$path = sprintf( 'actors/%d/followers', intval( $comment->comment_author ) );
|
||||
|
||||
$object->set_to(
|
||||
array(
|
||||
'https://www.w3.org/ns/activitystreams#Public',
|
||||
get_rest_url_by_path( $path ),
|
||||
)
|
||||
);
|
||||
|
||||
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.
|
||||
*
|
||||
@ -92,12 +101,12 @@ class Comment extends Base {
|
||||
* @return string The User-URL.
|
||||
*/
|
||||
protected function get_attributed_to() {
|
||||
if ( is_single_user() ) {
|
||||
$user = new Blog();
|
||||
return $user->get_url();
|
||||
// If the comment was received via ActivityPub, return the author URL.
|
||||
if ( was_comment_received( $this->item ) ) {
|
||||
return $this->item->comment_author_url;
|
||||
}
|
||||
|
||||
return Users::get_by_id( $this->wp_object->user_id )->get_url();
|
||||
return $this->get_actor_object()->get_id();
|
||||
}
|
||||
|
||||
/**
|
||||
@ -108,8 +117,19 @@ class Comment extends Base {
|
||||
* @return string The content.
|
||||
*/
|
||||
protected function get_content() {
|
||||
$comment = $this->wp_object;
|
||||
$content = $comment->comment_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.
|
||||
@ -141,7 +161,7 @@ class Comment extends Base {
|
||||
* @return false|string|null The URL of the in-reply-to.
|
||||
*/
|
||||
protected function get_in_reply_to() {
|
||||
$comment = $this->wp_object;
|
||||
$comment = $this->item;
|
||||
$parent_comment = null;
|
||||
|
||||
if ( $comment->comment_parent ) {
|
||||
@ -169,53 +189,37 @@ class Comment extends Base {
|
||||
* @return string ActivityPub URI for comment
|
||||
*/
|
||||
protected function get_id() {
|
||||
$comment = $this->wp_object;
|
||||
$comment = $this->item;
|
||||
return Comment_Utils::generate_id( $comment );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns a list of Mentions, used in the Comment.
|
||||
* Returns the User-Object of the Author of the Post.
|
||||
*
|
||||
* @see https://docs.joinmastodon.org/spec/activitypub/#Mention
|
||||
* If `single_user` mode is enabled, the Blog-User is returned.
|
||||
*
|
||||
* @return array The list of Mentions.
|
||||
* @return \Activitypub\Activity\Actor The User-Object.
|
||||
*/
|
||||
protected function get_cc() {
|
||||
$cc = array();
|
||||
|
||||
$mentions = $this->get_mentions();
|
||||
if ( $mentions ) {
|
||||
foreach ( $mentions as $url ) {
|
||||
$cc[] = $url;
|
||||
}
|
||||
protected function get_actor_object() {
|
||||
if ( $this->actor_object ) {
|
||||
return $this->actor_object;
|
||||
}
|
||||
|
||||
return array_unique( $cc );
|
||||
}
|
||||
$blog_user = new Blog();
|
||||
$this->actor_object = $blog_user;
|
||||
|
||||
/**
|
||||
* 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 = 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;
|
||||
}
|
||||
if ( is_single_user() ) {
|
||||
return $blog_user;
|
||||
}
|
||||
|
||||
return \array_unique( $tags, SORT_REGULAR );
|
||||
$user = Actors::get_by_id( $this->item->user_id );
|
||||
|
||||
if ( $user && ! is_wp_error( $user ) ) {
|
||||
$this->actor_object = $user;
|
||||
return $user;
|
||||
}
|
||||
|
||||
return $blog_user;
|
||||
}
|
||||
|
||||
/**
|
||||
@ -235,7 +239,7 @@ class Comment extends Base {
|
||||
*
|
||||
* @return array The filtered list of mentions.
|
||||
*/
|
||||
return apply_filters( 'activitypub_extract_mentions', array(), $this->wp_object->comment_content, $this->wp_object );
|
||||
return apply_filters( 'activitypub_extract_mentions', array(), $this->item->comment_content, $this->item );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -244,7 +248,7 @@ class Comment extends Base {
|
||||
* @return array The list of ancestors.
|
||||
*/
|
||||
protected function get_comment_ancestors() {
|
||||
$ancestors = get_comment_ancestors( $this->wp_object );
|
||||
$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(
|
||||
@ -259,13 +263,13 @@ class Comment extends Base {
|
||||
* Collect all other Users that participated in this comment-thread
|
||||
* to send them a notification about the new reply.
|
||||
*
|
||||
* @param array $mentions The already mentioned ActivityPub users.
|
||||
* @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 ) {
|
||||
// Check if `$this->wp_object` is a WP_Comment.
|
||||
if ( 'WP_Comment' !== get_class( $this->wp_object ) ) {
|
||||
public function extract_reply_context( $mentions = array() ) {
|
||||
// Check if `$this->item` is a WP_Comment.
|
||||
if ( 'WP_Comment' !== get_class( $this->item ) ) {
|
||||
return $mentions;
|
||||
}
|
||||
|
||||
@ -289,23 +293,69 @@ class Comment extends Base {
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the locale of the post.
|
||||
* Returns the updated date of the comment.
|
||||
*
|
||||
* @return string The locale of the post.
|
||||
* @return string|null The updated date of the comment.
|
||||
*/
|
||||
public function get_locale() {
|
||||
$comment_id = $this->wp_object->ID;
|
||||
$lang = \strtolower( \strtok( \get_locale(), '_-' ) );
|
||||
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 );
|
||||
|
||||
/**
|
||||
* Filter the locale of the comment.
|
||||
*
|
||||
* @param string $lang The locale of the comment.
|
||||
* @param int $comment_id The comment ID.
|
||||
* @param \WP_Post $post The comment object.
|
||||
*
|
||||
* @return string The filtered locale of the comment.
|
||||
*/
|
||||
return apply_filters( 'activitypub_comment_locale', $lang, $comment_id, $this->wp_object );
|
||||
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 );
|
||||
}
|
||||
}
|
||||
|
@ -8,6 +8,11 @@
|
||||
namespace Activitypub\Transformer;
|
||||
|
||||
use WP_Error;
|
||||
use Activitypub\Http;
|
||||
use Activitypub\Comment as Comment_Helper;
|
||||
|
||||
use function Activitypub\is_post_disabled;
|
||||
use function Activitypub\user_can_activitypub;
|
||||
|
||||
/**
|
||||
* Transformer Factory.
|
||||
@ -21,12 +26,23 @@ class Factory {
|
||||
* @return Base|WP_Error The transformer to use, or an error.
|
||||
*/
|
||||
public static function get_transformer( $data ) {
|
||||
if ( ! \is_object( $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' ) );
|
||||
}
|
||||
|
||||
$class = \get_class( $data );
|
||||
|
||||
/**
|
||||
* Filter the transformer for a given object.
|
||||
*
|
||||
@ -50,9 +66,9 @@ class Factory {
|
||||
* return $transformer;
|
||||
* }, 10, 3 );
|
||||
*
|
||||
* @param Base $transformer The transformer to use.
|
||||
* @param mixed $data The object to transform.
|
||||
* @param string $object_class The class of the object to transform.
|
||||
* @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.
|
||||
*/
|
||||
@ -72,14 +88,30 @@ class Factory {
|
||||
// Use default transformer.
|
||||
switch ( $class ) {
|
||||
case 'WP_Post':
|
||||
if ( 'attachment' === $data->post_type ) {
|
||||
if ( 'attachment' === $data->post_type && ! is_post_disabled( $data ) ) {
|
||||
return new Attachment( $data );
|
||||
} elseif ( ! is_post_disabled( $data ) ) {
|
||||
return new Post( $data );
|
||||
}
|
||||
return new Post( $data );
|
||||
break;
|
||||
case 'WP_Comment':
|
||||
return new Comment( $data );
|
||||
default:
|
||||
return null;
|
||||
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 'json':
|
||||
return new Json( $data );
|
||||
}
|
||||
|
||||
if ( $data instanceof \Activitypub\Activity\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_actor;
|
||||
use function Activitypub\is_activity;
|
||||
|
||||
/**
|
||||
* 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,41 @@
|
||||
<?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() {
|
||||
$activity_object = $this->transform_object_properties( Actors::get_by_id( $this->item->ID ) );
|
||||
|
||||
if ( \is_wp_error( $activity_object ) ) {
|
||||
return $activity_object;
|
||||
}
|
||||
|
||||
return $activity_object;
|
||||
}
|
||||
|
||||
/**
|
||||
* 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