updated plugin ActivityPub
version 5.8.0
This commit is contained in:
@ -9,13 +9,6 @@
|
||||
|
||||
namespace Activitypub\Activity;
|
||||
|
||||
use WP_Error;
|
||||
use ReflectionClass;
|
||||
use DateTime;
|
||||
|
||||
use function Activitypub\camel_to_snake_case;
|
||||
use function Activitypub\snake_to_camel_case;
|
||||
|
||||
/**
|
||||
* Base_Object is an implementation of one of the
|
||||
* Activity Streams Core Types.
|
||||
@ -27,8 +20,56 @@ use function Activitypub\snake_to_camel_case;
|
||||
* 'Base_' for this reason.
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitystreams-core/#object
|
||||
*
|
||||
* @method string|null get_actor() Gets one or more entities that performed or are expected to perform the activity.
|
||||
* @method string|null get_attributed_to() Gets the entity attributed as the original author.
|
||||
* @method array|null get_attachment() Gets the attachment property of the object.
|
||||
* @method array|null get_cc() Gets the secondary recipients of the object.
|
||||
* @method string|null get_content() Gets the content property of the object.
|
||||
* @method array|null get_icon() Gets the icon property of the object.
|
||||
* @method string|null get_id() Gets the object's unique global identifier.
|
||||
* @method array|null get_image() Gets the image property of the object.
|
||||
* @method array|string|null get_in_reply_to() Gets the objects this object is in reply to.
|
||||
* @method string|null get_name() Gets the natural language name of the object.
|
||||
* @method Base_Object|string|null get_object() Gets the direct object of the activity.
|
||||
* @method string|null get_published() Gets the date and time the object was published in ISO 8601 format.
|
||||
* @method string|null get_summary() Gets the natural language summary of the object.
|
||||
* @method array|null get_tag() Gets the tag property of the object.
|
||||
* @method array|string|null get_to() Gets the primary recipients of the object.
|
||||
* @method string get_type() Gets the type of the object.
|
||||
* @method string|null get_updated() Gets the date and time the object was updated in ISO 8601 format.
|
||||
* @method string|null get_url() Gets the URL of the object.
|
||||
*
|
||||
* @method string|array add_cc( string|array $cc ) Adds one or more entities to the secondary audience of the object.
|
||||
* @method string|array add_to( string|array $to ) Adds one or more entities to the primary audience of the object.
|
||||
*
|
||||
* @method Base_Object set_actor( string|array $actor ) Sets one or more entities that performed the activity.
|
||||
* @method Base_Object set_attachment( array $attachment ) Sets the attachment property of the object.
|
||||
* @method Base_Object set_attributed_to( string $attributed_to ) Sets the entity attributed as the original author.
|
||||
* @method Base_Object set_cc( array|string $cc ) Sets the secondary recipients of the object.
|
||||
* @method Base_Object set_content( string $content ) Sets the content property of the object.
|
||||
* @method Base_Object set_content_map( array $content_map ) Sets the content property of the object.
|
||||
* @method Base_Object set_icon( array $icon ) Sets the icon property of the object.
|
||||
* @method Base_Object set_id( string $id ) Sets the object's unique global identifier.
|
||||
* @method Base_Object set_image( array $image ) Sets the image property of the object.
|
||||
* @method Base_Object set_name( string $name ) Sets the natural language name of the object.
|
||||
* @method Base_Object set_origin( string $origin ) Sets the origin property of the object.
|
||||
* @method Base_Object set_published( string $published ) Sets the date and time the object was published in ISO 8601 format.
|
||||
* @method Base_Object set_sensitive( bool $sensitive ) Sets the sensitive property of the object.
|
||||
* @method Base_Object set_summary( string $summary ) Sets the natural language summary of the object.
|
||||
* @method Base_Object set_summary_map( array|null $summary_map ) Sets the summary property of the object.
|
||||
* @method Base_Object set_target( string $target ) Sets the target property of the object.
|
||||
* @method Base_Object set_to( array|string $to ) Sets the primary recipients of the object.
|
||||
* @method Base_Object set_type( string $type ) Sets the type of the object.
|
||||
* @method Base_Object set_updated( string $updated ) Sets the date and time the object was updated in ISO 8601 format.
|
||||
* @method Base_Object set_url( string $url ) Sets the URL of the object.
|
||||
*/
|
||||
class Base_Object {
|
||||
class Base_Object extends Generic_Object {
|
||||
/**
|
||||
* The JSON-LD context for the object.
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
const JSON_LD_CONTEXT = array(
|
||||
'https://www.w3.org/ns/activitystreams',
|
||||
array(
|
||||
@ -38,13 +79,26 @@ class Base_Object {
|
||||
);
|
||||
|
||||
/**
|
||||
* The object's unique global identifier
|
||||
* The default types for Objects.
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitypub/#obj-id
|
||||
* @see https://www.w3.org/TR/activitystreams-vocabulary/#object-types
|
||||
*
|
||||
* @var string
|
||||
* @var array
|
||||
*/
|
||||
protected $id;
|
||||
const TYPES = array(
|
||||
'Article',
|
||||
'Audio',
|
||||
'Document',
|
||||
'Event',
|
||||
'Image',
|
||||
'Note',
|
||||
'Page',
|
||||
'Place',
|
||||
'Profile',
|
||||
'Relationship',
|
||||
'Tombstone',
|
||||
'Video',
|
||||
);
|
||||
|
||||
/**
|
||||
* The type of the object.
|
||||
@ -61,12 +115,7 @@ class Base_Object {
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-attachment
|
||||
*
|
||||
* @var string
|
||||
* | ObjectType
|
||||
* | Link
|
||||
* | array<ObjectType>
|
||||
* | array<Link>
|
||||
* | null
|
||||
* @var string|null
|
||||
*/
|
||||
protected $attachment;
|
||||
|
||||
@ -77,12 +126,7 @@ class Base_Object {
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-attributedto
|
||||
*
|
||||
* @var string
|
||||
* | ObjectType
|
||||
* | Link
|
||||
* | array<ObjectType>
|
||||
* | array<Link>
|
||||
* | null
|
||||
* @var string|null
|
||||
*/
|
||||
protected $attributed_to;
|
||||
|
||||
@ -92,12 +136,7 @@ class Base_Object {
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-audience
|
||||
*
|
||||
* @var string
|
||||
* | ObjectType
|
||||
* | Link
|
||||
* | array<ObjectType>
|
||||
* | array<Link>
|
||||
* | null
|
||||
* @var string|null
|
||||
*/
|
||||
protected $audience;
|
||||
|
||||
@ -127,10 +166,7 @@ class Base_Object {
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-context
|
||||
*
|
||||
* @var string
|
||||
* | ObjectType
|
||||
* | Link
|
||||
* | null
|
||||
* @var string|null
|
||||
*/
|
||||
protected $context;
|
||||
|
||||
@ -191,12 +227,7 @@ class Base_Object {
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-icon
|
||||
*
|
||||
* @var string
|
||||
* | Image
|
||||
* | Link
|
||||
* | array<Image>
|
||||
* | array<Link>
|
||||
* | null
|
||||
* @var string|array|null
|
||||
*/
|
||||
protected $icon;
|
||||
|
||||
@ -207,12 +238,7 @@ class Base_Object {
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-image-term
|
||||
*
|
||||
* @var string
|
||||
* | Image
|
||||
* | Link
|
||||
* | array<Image>
|
||||
* | array<Link>
|
||||
* | null
|
||||
* @var string|array|null
|
||||
*/
|
||||
protected $image;
|
||||
|
||||
@ -222,12 +248,7 @@ class Base_Object {
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-inreplyto
|
||||
*
|
||||
* @var string
|
||||
* | ObjectType
|
||||
* | Link
|
||||
* | array<ObjectType>
|
||||
* | array<Link>
|
||||
* | null
|
||||
* @var string|null
|
||||
*/
|
||||
protected $in_reply_to;
|
||||
|
||||
@ -237,12 +258,7 @@ class Base_Object {
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-location
|
||||
*
|
||||
* @var string
|
||||
* | ObjectType
|
||||
* | Link
|
||||
* | array<ObjectType>
|
||||
* | array<Link>
|
||||
* | null
|
||||
* @var string|null
|
||||
*/
|
||||
protected $location;
|
||||
|
||||
@ -251,10 +267,7 @@ class Base_Object {
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-preview
|
||||
*
|
||||
* @var string
|
||||
* | ObjectType
|
||||
* | Link
|
||||
* | null
|
||||
* @var string|null
|
||||
*/
|
||||
protected $preview;
|
||||
|
||||
@ -286,10 +299,7 @@ class Base_Object {
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-summary
|
||||
*
|
||||
* @var string
|
||||
* | ObjectType
|
||||
* | Link
|
||||
* | null
|
||||
* @var string|null
|
||||
*/
|
||||
protected $summary;
|
||||
|
||||
@ -299,7 +309,7 @@ class Base_Object {
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-summary
|
||||
*
|
||||
* @var array<string>|null
|
||||
* @var string[]|null
|
||||
*/
|
||||
protected $summary_map;
|
||||
|
||||
@ -312,12 +322,7 @@ class Base_Object {
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-tag
|
||||
*
|
||||
* @var string
|
||||
* | ObjectType
|
||||
* | Link
|
||||
* | array<ObjectType>
|
||||
* | array<Link>
|
||||
* | null
|
||||
* @var string|null
|
||||
*/
|
||||
protected $tag;
|
||||
|
||||
@ -333,11 +338,7 @@ class Base_Object {
|
||||
/**
|
||||
* One or more links to representations of the object.
|
||||
*
|
||||
* @var string
|
||||
* | array<string>
|
||||
* | Link
|
||||
* | array<Link>
|
||||
* | null
|
||||
* @var string|null
|
||||
*/
|
||||
protected $url;
|
||||
|
||||
@ -347,12 +348,7 @@ class Base_Object {
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-to
|
||||
*
|
||||
* @var string
|
||||
* | ObjectType
|
||||
* | Link
|
||||
* | array<ObjectType>
|
||||
* | array<Link>
|
||||
* | null
|
||||
* @var string|array|null
|
||||
*/
|
||||
protected $to;
|
||||
|
||||
@ -362,12 +358,7 @@ class Base_Object {
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-bto
|
||||
*
|
||||
* @var string
|
||||
* | ObjectType
|
||||
* | Link
|
||||
* | array<ObjectType>
|
||||
* | array<Link>
|
||||
* | null
|
||||
* @var string|array|null
|
||||
*/
|
||||
protected $bto;
|
||||
|
||||
@ -377,12 +368,7 @@ class Base_Object {
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-cc
|
||||
*
|
||||
* @var string
|
||||
* | ObjectType
|
||||
* | Link
|
||||
* | array<ObjectType>
|
||||
* | array<Link>
|
||||
* | null
|
||||
* @var string|array|null
|
||||
*/
|
||||
protected $cc;
|
||||
|
||||
@ -392,12 +378,7 @@ class Base_Object {
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-bcc
|
||||
*
|
||||
* @var string
|
||||
* | ObjectType
|
||||
* | Link
|
||||
* | array<ObjectType>
|
||||
* | array<Link>
|
||||
* | null
|
||||
* @var string|array|null
|
||||
*/
|
||||
protected $bcc;
|
||||
|
||||
@ -443,13 +424,30 @@ class Base_Object {
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-replies
|
||||
*
|
||||
* @var string
|
||||
* | Collection
|
||||
* | Link
|
||||
* | null
|
||||
* @var string|array|null
|
||||
*/
|
||||
protected $replies;
|
||||
|
||||
/**
|
||||
* A Collection containing objects considered to be likes for
|
||||
* this object.
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitypub/#likes
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $likes;
|
||||
|
||||
/**
|
||||
* A Collection containing objects considered to be shares for
|
||||
* this object.
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitypub/#shares
|
||||
*
|
||||
* @var array
|
||||
*/
|
||||
protected $shares;
|
||||
|
||||
/**
|
||||
* Used to mark an object as containing sensitive content.
|
||||
* Mastodon displays a content warning, requiring users to click
|
||||
@ -459,51 +457,7 @@ class Base_Object {
|
||||
*
|
||||
* @var boolean
|
||||
*/
|
||||
protected $sensitive = false;
|
||||
|
||||
/**
|
||||
* Magic function to implement getter and setter.
|
||||
*
|
||||
* @param string $method The method name.
|
||||
* @param string $params The method params.
|
||||
*/
|
||||
public function __call( $method, $params ) {
|
||||
$var = \strtolower( \substr( $method, 4 ) );
|
||||
|
||||
if ( \strncasecmp( $method, 'get', 3 ) === 0 ) {
|
||||
if ( ! $this->has( $var ) ) {
|
||||
return new WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
return $this->$var;
|
||||
}
|
||||
|
||||
if ( \strncasecmp( $method, 'set', 3 ) === 0 ) {
|
||||
return $this->set( $var, $params[0] );
|
||||
}
|
||||
|
||||
if ( \strncasecmp( $method, 'add', 3 ) === 0 ) {
|
||||
$this->add( $var, $params[0] );
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Magic function, to transform the object to string.
|
||||
*
|
||||
* @return string The object id.
|
||||
*/
|
||||
public function __toString() {
|
||||
return $this->to_string();
|
||||
}
|
||||
|
||||
/**
|
||||
* Function to transform the object to string.
|
||||
*
|
||||
* @return string The object id.
|
||||
*/
|
||||
public function to_string() {
|
||||
return $this->get_id();
|
||||
}
|
||||
protected $sensitive;
|
||||
|
||||
/**
|
||||
* Generic getter.
|
||||
@ -514,21 +468,10 @@ class Base_Object {
|
||||
*/
|
||||
public function get( $key ) {
|
||||
if ( ! $this->has( $key ) ) {
|
||||
return new WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) );
|
||||
return new \WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
return call_user_func( array( $this, 'get_' . $key ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the object has a key
|
||||
*
|
||||
* @param string $key The key to check.
|
||||
*
|
||||
* @return boolean True if the object has the key.
|
||||
*/
|
||||
public function has( $key ) {
|
||||
return property_exists( $this, $key );
|
||||
return parent::get( $key );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -541,12 +484,10 @@ class Base_Object {
|
||||
*/
|
||||
public function set( $key, $value ) {
|
||||
if ( ! $this->has( $key ) ) {
|
||||
return new WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) );
|
||||
return new \WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$this->$key = $value;
|
||||
|
||||
return $this;
|
||||
return parent::set( $key, $value );
|
||||
}
|
||||
|
||||
/**
|
||||
@ -559,188 +500,9 @@ class Base_Object {
|
||||
*/
|
||||
public function add( $key, $value ) {
|
||||
if ( ! $this->has( $key ) ) {
|
||||
return new WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) );
|
||||
return new \WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
if ( ! isset( $this->$key ) ) {
|
||||
$this->$key = array();
|
||||
}
|
||||
|
||||
$attributes = $this->$key;
|
||||
$attributes[] = $value;
|
||||
|
||||
$this->$key = $attributes;
|
||||
|
||||
return $this->$key;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert JSON input to an array.
|
||||
*
|
||||
* @param string $json The JSON string.
|
||||
*
|
||||
* @return Base_Object An Object built from the JSON string.
|
||||
*/
|
||||
public static function init_from_json( $json ) {
|
||||
$array = \json_decode( $json, true );
|
||||
|
||||
if ( ! is_array( $array ) ) {
|
||||
$array = array();
|
||||
}
|
||||
|
||||
return self::init_from_array( $array );
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert input array to a Base_Object.
|
||||
*
|
||||
* @param array $data The object array.
|
||||
*
|
||||
* @return Base_Object|WP_Error An Object built from the input array or WP_Error when it's not an array.
|
||||
*/
|
||||
public static function init_from_array( $data ) {
|
||||
if ( ! is_array( $data ) ) {
|
||||
return new WP_Error( 'invalid_array', __( 'Invalid array', 'activitypub' ), array( 'status' => 404 ) );
|
||||
}
|
||||
|
||||
$object = new static();
|
||||
|
||||
foreach ( $data as $key => $value ) {
|
||||
$key = camel_to_snake_case( $key );
|
||||
call_user_func( array( $object, 'set_' . $key ), $value );
|
||||
}
|
||||
|
||||
return $object;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert JSON input to an array and pre-fill the object.
|
||||
*
|
||||
* @param string $json The JSON string.
|
||||
*/
|
||||
public function from_json( $json ) {
|
||||
$array = \json_decode( $json, true );
|
||||
|
||||
$this->from_array( $array );
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert JSON input to an array and pre-fill the object.
|
||||
*
|
||||
* @param array $data The array.
|
||||
*/
|
||||
public function from_array( $data ) {
|
||||
foreach ( $data as $key => $value ) {
|
||||
if ( $value ) {
|
||||
$key = camel_to_snake_case( $key );
|
||||
call_user_func( array( $this, 'set_' . $key ), $value );
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert Object to an array.
|
||||
*
|
||||
* It tries to get the object attributes if they exist
|
||||
* and falls back to the getters. Empty values are ignored.
|
||||
*
|
||||
* @param bool $include_json_ld_context Whether to include the JSON-LD context. Default true.
|
||||
*
|
||||
* @return array An array built from the Object.
|
||||
*/
|
||||
public function to_array( $include_json_ld_context = true ) {
|
||||
$array = array();
|
||||
$vars = get_object_vars( $this );
|
||||
|
||||
foreach ( $vars as $key => $value ) {
|
||||
// Ignore all _prefixed keys.
|
||||
if ( '_' === substr( $key, 0, 1 ) ) {
|
||||
continue;
|
||||
}
|
||||
|
||||
// If value is empty, try to get it from a getter.
|
||||
if ( ! $value ) {
|
||||
$value = call_user_func( array( $this, 'get_' . $key ) );
|
||||
}
|
||||
|
||||
if ( is_object( $value ) ) {
|
||||
$value = $value->to_array( false );
|
||||
}
|
||||
|
||||
// If value is still empty, ignore it for the array and continue.
|
||||
if ( isset( $value ) ) {
|
||||
$array[ snake_to_camel_case( $key ) ] = $value;
|
||||
}
|
||||
}
|
||||
|
||||
if ( $include_json_ld_context ) {
|
||||
// Get JsonLD context and move it to '@context' at the top.
|
||||
$array = array_merge( array( '@context' => $this->get_json_ld_context() ), $array );
|
||||
}
|
||||
|
||||
$class = new ReflectionClass( $this );
|
||||
$class = strtolower( $class->getShortName() );
|
||||
|
||||
/**
|
||||
* Filter the array of the ActivityPub object.
|
||||
*
|
||||
* @param array $array The array of the ActivityPub object.
|
||||
* @param string $class The class of the ActivityPub object.
|
||||
* @param int $id The ID of the ActivityPub object.
|
||||
* @param Base_Object $object The ActivityPub object.
|
||||
*
|
||||
* @return array The filtered array of the ActivityPub object.
|
||||
*/
|
||||
$array = \apply_filters( 'activitypub_activity_object_array', $array, $class, $this->id, $this );
|
||||
|
||||
/**
|
||||
* Filter the array of the ActivityPub object by class.
|
||||
*
|
||||
* @param array $array The array of the ActivityPub object.
|
||||
* @param int $id The ID of the ActivityPub object.
|
||||
* @param Base_Object $object The ActivityPub object.
|
||||
*
|
||||
* @return array The filtered array of the ActivityPub object.
|
||||
*/
|
||||
return \apply_filters( "activitypub_activity_{$class}_object_array", $array, $this->id, $this );
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert Object to JSON.
|
||||
*
|
||||
* @param bool $include_json_ld_context Whether to include the JSON-LD context. Default true.
|
||||
*
|
||||
* @return string The JSON string.
|
||||
*/
|
||||
public function to_json( $include_json_ld_context = true ) {
|
||||
$array = $this->to_array( $include_json_ld_context );
|
||||
$options = \JSON_HEX_TAG | \JSON_HEX_AMP | \JSON_HEX_QUOT;
|
||||
|
||||
/**
|
||||
* Options to be passed to json_encode()
|
||||
*
|
||||
* @param int $options The current options flags.
|
||||
*/
|
||||
$options = \apply_filters( 'activitypub_json_encode_options', $options );
|
||||
|
||||
return \wp_json_encode( $array, $options );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the keys of the object vars.
|
||||
*
|
||||
* @return array The keys of the object vars.
|
||||
*/
|
||||
public function get_object_var_keys() {
|
||||
return \array_keys( \get_object_vars( $this ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the JSON-LD context of this object.
|
||||
*
|
||||
* @return array $context A compacted JSON-LD context for the ActivityPub object.
|
||||
*/
|
||||
public function get_json_ld_context() {
|
||||
return static::JSON_LD_CONTEXT;
|
||||
return parent::add( $key, $value );
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user