updated plugin ActivityPub version 5.8.0

This commit is contained in:
2025-04-29 21:19:06 +00:00
committed by Gitium
parent 19dfd317cc
commit fdfbf76539
166 changed files with 14119 additions and 7163 deletions

View File

@ -9,7 +9,8 @@
namespace Activitypub\Activity;
use Activitypub\Link;
use Activitypub\Activity\Extended_Object\Event;
use Activitypub\Activity\Extended_Object\Place;
/**
* \Activitypub\Activity\Activity implements the common
@ -23,6 +24,43 @@ class Activity extends Base_Object {
'https://www.w3.org/ns/activitystreams',
);
/**
* The default types for Activities.
*
* @see https://www.w3.org/TR/activitystreams-vocabulary/#activity-types
*
* @var array
*/
const TYPES = array(
'Accept',
'Add',
'Announce',
'Arrive',
'Block',
'Create',
'Delete',
'Dislike',
'Follow',
'Flag',
'Ignore',
'Invite',
'Join',
'Leave',
'Like',
'Listen',
'Move',
'Offer',
'Read',
'Reject',
'Remove',
'TentativeAccept',
'TentativeReject',
'Travel',
'Undo',
'Update',
'View',
);
/**
* The type of the object.
*
@ -37,10 +75,7 @@ class Activity extends Base_Object {
*
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-object-term
*
* @var string
* | Base_Object
* | Link
* | null
* @var string|Base_Object|null
*/
protected $object;
@ -52,11 +87,7 @@ class Activity extends Base_Object {
*
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-actor
*
* @var string
* | \ActivityPhp\Type\Extended\AbstractActor
* | array<Actor>
* | array<Link>
* | Link
* @var string|array
*/
protected $actor;
@ -71,11 +102,7 @@ class Activity extends Base_Object {
*
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-target
*
* @var string
* | ObjectType
* | array<ObjectType>
* | Link
* | array<Link>
* @var string|array
*/
protected $target;
@ -87,10 +114,7 @@ class Activity extends Base_Object {
*
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-result
*
* @var string
* | ObjectType
* | Link
* | null
* @var string|Base_Object
*/
protected $result;
@ -103,9 +127,6 @@ class Activity extends Base_Object {
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-replies
*
* @var array
* | ObjectType
* | Link
* | null
*/
protected $replies;
@ -119,10 +140,7 @@ class Activity extends Base_Object {
*
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-origin
*
* @var string
* | ObjectType
* | Link
* | null
* @var string|array
*/
protected $origin;
@ -132,10 +150,7 @@ class Activity extends Base_Object {
*
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-instrument
*
* @var string
* | ObjectType
* | Link
* | null
* @var string|array
*/
protected $instrument;
@ -143,53 +158,93 @@ class Activity extends Base_Object {
* Set the object and copy Object properties to the Activity.
*
* Any to, bto, cc, bcc, and audience properties specified on the object
* MUST be copied over to the new Create activity by the server.
* MUST be copied over to the new "Create" activity by the server.
*
* @see https://www.w3.org/TR/activitypub/#object-without-create
*
* @param array|string|Base_Object|Link|null $data Activity object.
*
* @return void
* @param array|string|Base_Object|Activity|Actor|null $data Activity object.
*/
public function set_object( $data ) {
// Convert array to object.
$object = $data;
// Convert array to appropriate object type.
if ( is_array( $data ) ) {
$data = self::init_from_array( $data );
$type = $data['type'] ?? null;
if ( in_array( $type, self::TYPES, true ) ) {
$object = self::init_from_array( $data );
} elseif ( in_array( $type, Actor::TYPES, true ) ) {
$object = Actor::init_from_array( $data );
} elseif ( in_array( $type, Base_Object::TYPES, true ) ) {
switch ( $type ) {
case 'Event':
$object = Event::init_from_array( $data );
break;
case 'Place':
$object = Place::init_from_array( $data );
break;
default:
$object = Base_Object::init_from_array( $data );
break;
}
} else {
$object = Generic_Object::init_from_array( $data );
}
}
// Set object.
$this->set( 'object', $data );
$this->set( 'object', $object );
$this->pre_fill_activity_from_object();
}
if ( ! is_object( $data ) ) {
/**
* Fills the Activity with the specified activity object.
*/
public function pre_fill_activity_from_object() {
$object = $this->get_object();
// Check if `$data` is a URL and use it to generate an ID then.
if ( is_string( $object ) && filter_var( $object, FILTER_VALIDATE_URL ) && ! $this->get_id() ) {
$this->set( 'id', $object . '#activity-' . strtolower( $this->get_type() ) . '-' . time() );
return;
}
// Check if `$data` is an object and copy some properties otherwise do nothing.
if ( ! is_object( $object ) ) {
return;
}
foreach ( array( 'to', 'bto', 'cc', 'bcc', 'audience' ) as $i ) {
$this->set( $i, $data->get( $i ) );
$value = $object->get( $i );
if ( $value && ! $this->get( $i ) ) {
$this->set( $i, $value );
}
}
if ( $data->get_published() && ! $this->get_published() ) {
$this->set( 'published', $data->get_published() );
if ( $object->get_published() && ! $this->get_published() ) {
$this->set( 'published', $object->get_published() );
}
if ( $data->get_updated() && ! $this->get_updated() ) {
$this->set( 'updated', $data->get_updated() );
if ( $object->get_updated() && ! $this->get_updated() ) {
$this->set( 'updated', $object->get_updated() );
}
if ( $data->get_attributed_to() && ! $this->get_actor() ) {
$this->set( 'actor', $data->get_attributed_to() );
if ( $object->get_attributed_to() && ! $this->get_actor() ) {
$this->set( 'actor', $object->get_attributed_to() );
}
if ( $data->get_in_reply_to() ) {
$this->set( 'in_reply_to', $data->get_in_reply_to() );
if ( $this->get_type() !== 'Announce' && $object->get_in_reply_to() && ! $this->get_in_reply_to() ) {
$this->set( 'in_reply_to', $object->get_in_reply_to() );
}
if ( $data->get_id() && ! $this->get_id() ) {
$id = strtok( $data->get_id(), '#' );
if ( $data->get_updated() ) {
$updated = $data->get_updated();
if ( $object->get_id() && ! $this->get_id() ) {
$id = strtok( $object->get_id(), '#' );
if ( $object->get_updated() ) {
$updated = $object->get_updated();
} elseif ( $object->get_published() ) {
$updated = $object->get_published();
} else {
$updated = $data->get_published();
$updated = time();
}
$this->set( 'id', $id . '#activity-' . strtolower( $this->get_type() ) . '-' . $updated );
}

View File

@ -43,12 +43,39 @@ class Actor extends Base_Object {
'@id' => 'lemmy:moderators',
'@type' => '@id',
),
'alsoKnownAs' => array(
'@id' => 'as:alsoKnownAs',
'@type' => '@id',
),
'movedTo' => array(
'@id' => 'as:movedTo',
'@type' => '@id',
),
'attributionDomains' => array(
'@id' => 'toot:attributionDomains',
'@type' => '@id',
),
'postingRestrictedToMods' => 'lemmy:postingRestrictedToMods',
'discoverable' => 'toot:discoverable',
'indexable' => 'toot:indexable',
),
);
/**
* The default types for Actors.
*
* @see https://www.w3.org/TR/activitystreams-vocabulary/#actor-types
*
* @var array
*/
const TYPES = array(
'Application',
'Group',
'Organization',
'Person',
'Service',
);
/**
* The type of the object.
*
@ -62,8 +89,7 @@ class Actor extends Base_Object {
*
* @see https://www.w3.org/TR/activitypub/#inbox
*
* @var string
* | null
* @var string|null
*/
protected $inbox;
@ -73,8 +99,7 @@ class Actor extends Base_Object {
*
* @see https://www.w3.org/TR/activitypub/#outbox
*
* @var string
* | null
* @var string|null
*/
protected $outbox;
@ -175,13 +200,26 @@ class Actor extends Base_Object {
protected $manually_approves_followers = false;
/**
* Used to mark an object as containing sensitive content.
* Mastodon displays a content warning, requiring users to click
* through to view the content.
* Domains allowed to use `fediverse:creator` for this actor in
* published articles.
*
* @see https://docs.joinmastodon.org/spec/activitypub/#sensitive
* @see https://blog.joinmastodon.org/2024/07/highlighting-journalism-on-mastodon/
*
* @var boolean
* @var array
*/
protected $sensitive = null;
protected $attribution_domains = null;
/**
* The target of the actor.
*
* @var string|null
*/
protected $moved_to;
/**
* The alsoKnownAs of the actor.
*
* @var array
*/
protected $also_known_as;
}

View File

@ -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 );
}
}

View File

@ -0,0 +1,325 @@
<?php
/**
* Generic Object.
*
* @package Activitypub
*/
namespace Activitypub\Activity;
use function Activitypub\camel_to_snake_case;
use function Activitypub\snake_to_camel_case;
/**
* Generic Object.
*
* This class is used to create Generic Objects.
* It is used to create objects that might be unknown by the plugin but
* conform to the ActivityStreams vocabulary.
*
* @since 5.3.0
*/
#[\AllowDynamicProperties]
class Generic_Object {
/**
* The JSON-LD context for the object.
*
* @var array
*/
const JSON_LD_CONTEXT = array(
'https://www.w3.org/ns/activitystreams',
);
/**
* The object's unique global identifier
*
* @see https://www.w3.org/TR/activitypub/#obj-id
*
* @var string
*/
protected $id;
/**
* 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();
}
/**
* 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 null;
}
return $this->$var;
}
if ( \strncasecmp( $method, 'set', 3 ) === 0 ) {
return $this->set( $var, $params[0] );
}
if ( \strncasecmp( $method, 'add', 3 ) === 0 ) {
return $this->add( $var, $params[0] );
}
}
/**
* Generic getter.
*
* @param string $key The key to get.
*
* @return mixed The value.
*/
public function get( $key ) {
return call_user_func( array( $this, 'get_' . $key ) );
}
/**
* Generic setter.
*
* @param string $key The key to set.
* @param string $value The value to set.
*
* @return mixed The value.
*/
public function set( $key, $value ) {
$this->$key = $value;
return $this;
}
/**
* Generic adder.
*
* @param string $key The key to set.
* @param mixed $value The value to add.
*
* @return mixed The value.
*/
public function add( $key, $value ) {
if ( empty( $value ) ) {
return;
}
if ( ! isset( $this->$key ) ) {
$this->$key = array();
}
if ( is_string( $this->$key ) ) {
$this->$key = array( $this->$key );
}
$attributes = $this->$key;
if ( is_array( $value ) ) {
$attributes = array_merge( $attributes, $value );
} else {
$attributes[] = $value;
}
$this->$key = array_unique( $attributes );
return $this->$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 );
}
/**
* Convert JSON input to an array.
*
* @param string $json The JSON string.
*
* @return Generic_Object|\WP_Error An Object built from the JSON string or WP_Error when it's not a JSON string.
*/
public static function init_from_json( $json ) {
$array = \json_decode( $json, true );
if ( ! is_array( $array ) ) {
return new \WP_Error( 'invalid_json', __( 'Invalid JSON', 'activitypub' ), array( 'status' => 400 ) );
}
return self::init_from_array( $array );
}
/**
* Convert input array to a Base_Object.
*
* @param array $data The object array.
*
* @return Generic_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' => 400 ) );
}
$object = new static();
$object->from_array( $data );
return $object;
}
/**
* 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 ( null !== $value ) {
$key = camel_to_snake_case( $key );
call_user_func( array( $this, 'set_' . $key ), $value );
}
}
}
/**
* 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 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 ) {
if ( \is_wp_error( $value ) ) {
continue;
}
// 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 string $id The ID of the ActivityPub object.
* @param Generic_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 string $id The ID of the ActivityPub object.
* @param Generic_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 | \JSON_UNESCAPED_SLASHES;
/**
* 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;
}
}

View File

@ -50,7 +50,7 @@ class Event extends Base_Object {
);
/**
* Mobilizon compatible values for repliesModertaionOption.
* Mobilizon compatible values for repliesModerationOption.
*
* @var array
*/
@ -342,15 +342,15 @@ class Event extends Base_Object {
/**
* Custom setter for the event category.
*
* Falls back to Mobilizons default category.
* Falls back to Mobilizon's default category.
*
* @param string $category The category of the event.
* @param bool $mobilizon_compatibilty Optional. Whether the category must be compatibly with Mobilizon. Default true.
* @param string $category The category of the event.
* @param bool $mobilizon_compatibility Optional. Whether the category must be compatibly with Mobilizon. Default true.
*
* @return Event
*/
public function set_category( $category, $mobilizon_compatibilty = true ) {
if ( $mobilizon_compatibilty ) {
public function set_category( $category, $mobilizon_compatibility = true ) {
if ( $mobilizon_compatibility ) {
$this->category = in_array( $category, self::DEFAULT_EVENT_CATEGORIES, true ) ? $category : 'MEETING';
} else {
$this->category = $category;

View File

@ -38,8 +38,8 @@ class Place extends Base_Object {
protected $accuracy;
/**
* Indicates the altitude of a place. The measurement units is indicated using the units property.
* If units is not specified, the default is assumed to be "m" indicating meters.
* Indicates the altitude of a place. The measurement unit is indicated using the unit's property.
* If unit is not specified, the default is assumed to be "m" indicating meters.
*
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-altitude
* @var float xsd:float