updated plugin ActivityPub version 8.3.0
This commit is contained in:
@ -18,10 +18,29 @@ use Activitypub\Activity\Extended_Object\Place;
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitystreams-core/#activities
|
||||
* @see https://www.w3.org/TR/activitystreams-core/#intransitiveactivities
|
||||
*
|
||||
* @method string|array get_actor() Gets one or more entities that performed or are expected to perform the activity.
|
||||
* @method string|array|null get_instrument() Gets one or more objects used in the completion of an Activity.
|
||||
* @method Base_Object|string|array|null get_object() Gets the direct object of the activity.
|
||||
* @method string|string[]|null get_origin() Gets the origin property of the activity.
|
||||
* @method array|null get_replies() Gets the collection of responses to this activity.
|
||||
* @method string|null get_result() Gets the result property of the activity.
|
||||
* @method string|string[]|null get_target() Gets the target property of the activity.
|
||||
*
|
||||
* @method Activity set_actor( string|array $actor ) Sets one or more entities that performed the activity.
|
||||
* @method Activity set_instrument( string|array $instrument ) Sets one or more objects used in the completion of an Activity.
|
||||
* @method Activity set_origin( string|array|null $origin ) Sets the origin property of the activity.
|
||||
* @method Activity set_replies( array $replies ) Sets the collection of responses to this activity.
|
||||
* @method Activity set_result( string|null $result ) Sets the result property of the activity.
|
||||
* @method Activity set_target( string|array|null $target ) Sets the target property of the activity.
|
||||
*/
|
||||
class Activity extends Base_Object {
|
||||
const JSON_LD_CONTEXT = array(
|
||||
'https://www.w3.org/ns/activitystreams',
|
||||
array(
|
||||
'toot' => 'http://joinmastodon.org/ns#',
|
||||
'QuoteRequest' => 'toot:QuoteRequest',
|
||||
),
|
||||
);
|
||||
|
||||
/**
|
||||
@ -50,6 +69,7 @@ class Activity extends Base_Object {
|
||||
'Listen',
|
||||
'Move',
|
||||
'Offer',
|
||||
'QuoteRequest', // @see https://codeberg.org/fediverse/fep/src/branch/main/fep/044f/fep-044f.md
|
||||
'Read',
|
||||
'Reject',
|
||||
'Remove',
|
||||
@ -75,7 +95,7 @@ class Activity extends Base_Object {
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-object-term
|
||||
*
|
||||
* @var string|Base_Object|null
|
||||
* @var string|Base_Object|array|null
|
||||
*/
|
||||
protected $object;
|
||||
|
||||
@ -102,7 +122,7 @@ class Activity extends Base_Object {
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-target
|
||||
*
|
||||
* @var string|array
|
||||
* @var string|array|null
|
||||
*/
|
||||
protected $target;
|
||||
|
||||
@ -114,7 +134,7 @@ class Activity extends Base_Object {
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-result
|
||||
*
|
||||
* @var string|Base_Object
|
||||
* @var string|Base_Object|null
|
||||
*/
|
||||
protected $result;
|
||||
|
||||
@ -126,7 +146,7 @@ class Activity extends Base_Object {
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-replies
|
||||
*
|
||||
* @var array
|
||||
* @var array|null
|
||||
*/
|
||||
protected $replies;
|
||||
|
||||
@ -140,7 +160,7 @@ class Activity extends Base_Object {
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-origin
|
||||
*
|
||||
* @var string|array
|
||||
* @var string|array|null
|
||||
*/
|
||||
protected $origin;
|
||||
|
||||
@ -150,7 +170,7 @@ class Activity extends Base_Object {
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-instrument
|
||||
*
|
||||
* @var string|array
|
||||
* @var string|array|null
|
||||
*/
|
||||
protected $instrument;
|
||||
|
||||
@ -162,33 +182,17 @@ class Activity extends Base_Object {
|
||||
*
|
||||
* @see https://www.w3.org/TR/activitypub/#object-without-create
|
||||
*
|
||||
* @param array|string|Base_Object|Activity|Actor|null $data Activity object.
|
||||
* @param array|string|Base_Object|Activity|Actor|null $data Activity object.
|
||||
*/
|
||||
public function set_object( $data ) {
|
||||
$object = $data;
|
||||
|
||||
// Convert array to appropriate object type.
|
||||
if ( is_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;
|
||||
}
|
||||
if ( array_is_list( $data ) ) {
|
||||
$object = array_map( array( $this, 'maybe_convert_to_object' ), $data );
|
||||
} else {
|
||||
$object = Generic_Object::init_from_array( $data );
|
||||
$object = $this->maybe_convert_to_object( $data );
|
||||
}
|
||||
}
|
||||
|
||||
@ -202,14 +206,14 @@ class Activity extends Base_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.
|
||||
// Check if `$object` 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.
|
||||
// Check if `$object` is an object and copy some properties otherwise do nothing.
|
||||
if ( ! is_object( $object ) ) {
|
||||
return;
|
||||
}
|
||||
@ -237,6 +241,10 @@ class Activity extends Base_Object {
|
||||
$this->set( 'in_reply_to', $object->get_in_reply_to() );
|
||||
}
|
||||
|
||||
if ( $object->get_interaction_policy() && ! $this->get_interaction_policy() ) {
|
||||
$this->set( 'interaction_policy', $object->get_interaction_policy() );
|
||||
}
|
||||
|
||||
if ( $object->get_id() && ! $this->get_id() ) {
|
||||
$id = strtok( $object->get_id(), '#' );
|
||||
if ( $object->get_updated() ) {
|
||||
@ -256,7 +264,7 @@ class Activity extends Base_Object {
|
||||
* @return array $context A compacted JSON-LD context.
|
||||
*/
|
||||
public function get_json_ld_context() {
|
||||
if ( $this->object instanceof Base_Object ) {
|
||||
if ( \is_object( $this->object ) ) {
|
||||
$class = get_class( $this->object );
|
||||
if ( $class && $class::JSON_LD_CONTEXT ) {
|
||||
// Without php 5.6 support this could be just: 'return $this->object::JSON_LD_CONTEXT;'.
|
||||
@ -266,4 +274,41 @@ class Activity extends Base_Object {
|
||||
|
||||
return static::JSON_LD_CONTEXT;
|
||||
}
|
||||
|
||||
/**
|
||||
* Convert data to the appropriate object type if it has an ActivityPub type.
|
||||
*
|
||||
* @param array|string|Base_Object|Activity|Actor|null $data The data to convert.
|
||||
*
|
||||
* @return Activity|Actor|Base_Object|Generic_Object|string|\WP_Error|null The converted object or original data.
|
||||
*/
|
||||
private function maybe_convert_to_object( $data ) {
|
||||
if ( ! is_array( $data ) ) {
|
||||
return $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 );
|
||||
}
|
||||
|
||||
return $object;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user