updated plugin ActivityPub
version 5.8.0
This commit is contained in:
@ -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
|
||||
);
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user