2020-04-07 13:03:04 +00:00
|
|
|
<?php
|
|
|
|
namespace Activitypub\Model;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ActivityPub Post Class
|
|
|
|
*
|
|
|
|
* @author Matthias Pfefferle
|
|
|
|
*
|
|
|
|
* @see https://www.w3.org/TR/activitypub/
|
|
|
|
*/
|
|
|
|
class Activity {
|
|
|
|
private $context = array( 'https://www.w3.org/ns/activitystreams' );
|
|
|
|
private $published = '';
|
|
|
|
private $id = '';
|
|
|
|
private $type = 'Create';
|
|
|
|
private $actor = '';
|
|
|
|
private $to = array( 'https://www.w3.org/ns/activitystreams#Public' );
|
|
|
|
private $cc = array( 'https://www.w3.org/ns/activitystreams#Public' );
|
|
|
|
private $object = null;
|
|
|
|
|
|
|
|
const TYPE_SIMPLE = 'simple';
|
|
|
|
const TYPE_FULL = 'full';
|
|
|
|
const TYPE_NONE = 'none';
|
|
|
|
|
|
|
|
public function __construct( $type = 'Create', $context = self::TYPE_SIMPLE ) {
|
|
|
|
if ( 'none' === $context ) {
|
|
|
|
$this->context = null;
|
|
|
|
} elseif ( 'full' === $context ) {
|
|
|
|
$this->context = \Activitypub\get_context();
|
|
|
|
}
|
|
|
|
|
|
|
|
$this->type = \ucfirst( $type );
|
2022-08-19 16:15:51 +00:00
|
|
|
$this->published = \gmdate( 'Y-m-d\TH:i:s\Z', \strtotime( 'now' ) );
|
2020-04-07 13:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function __call( $method, $params ) {
|
|
|
|
$var = \strtolower( \substr( $method, 4 ) );
|
|
|
|
|
|
|
|
if ( \strncasecmp( $method, 'get', 3 ) === 0 ) {
|
|
|
|
return $this->$var;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( \strncasecmp( $method, 'set', 3 ) === 0 ) {
|
|
|
|
$this->$var = $params[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function from_post( $object ) {
|
|
|
|
$this->object = $object;
|
2020-12-25 19:23:08 +00:00
|
|
|
if ( isset( $object['published'] ) ) {
|
|
|
|
$this->published = $object['published'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isset( $object['attributedTo'] ) ) {
|
|
|
|
$this->actor = $object['attributedTo'];
|
|
|
|
}
|
|
|
|
|
2022-03-21 13:34:54 +00:00
|
|
|
$type = \strtolower( $this->type );
|
|
|
|
|
2020-12-25 19:23:08 +00:00
|
|
|
if ( isset( $object['id'] ) ) {
|
2022-03-21 13:34:54 +00:00
|
|
|
$this->id = add_query_arg( 'activity', $type, $object['id'] );
|
2020-12-25 19:23:08 +00:00
|
|
|
}
|
2020-04-07 13:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function from_comment( $object ) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-12-25 19:23:08 +00:00
|
|
|
public function to_comment() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function from_remote_array( $array ) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2020-04-07 13:03:04 +00:00
|
|
|
public function to_array() {
|
2022-12-19 23:08:11 +00:00
|
|
|
$array = array_filter( \get_object_vars( $this ) );
|
2020-04-07 13:03:04 +00:00
|
|
|
|
|
|
|
if ( $this->context ) {
|
|
|
|
$array = array( '@context' => $this->context ) + $array;
|
|
|
|
}
|
|
|
|
|
|
|
|
unset( $array['context'] );
|
|
|
|
|
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
2020-12-25 19:23:08 +00:00
|
|
|
/**
|
|
|
|
* Convert to JSON
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2020-04-07 13:03:04 +00:00
|
|
|
public function to_json() {
|
2020-12-25 19:23:08 +00:00
|
|
|
return \wp_json_encode( $this->to_array(), \JSON_HEX_TAG | \JSON_HEX_AMP | \JSON_HEX_QUOT );
|
2020-04-07 13:03:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
public function to_simple_array() {
|
|
|
|
$activity = array(
|
|
|
|
'@context' => $this->context,
|
|
|
|
'type' => $this->type,
|
|
|
|
'actor' => $this->actor,
|
|
|
|
'object' => $this->object,
|
|
|
|
'to' => $this->to,
|
|
|
|
'cc' => $this->cc,
|
|
|
|
);
|
|
|
|
|
|
|
|
if ( $this->id ) {
|
|
|
|
$activity['id'] = $this->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $activity;
|
|
|
|
}
|
|
|
|
|
|
|
|
public function to_simple_json() {
|
2020-12-25 19:23:08 +00:00
|
|
|
return \wp_json_encode( $this->to_simple_array(), \JSON_HEX_TAG | \JSON_HEX_AMP | \JSON_HEX_QUOT );
|
2020-04-07 13:03:04 +00:00
|
|
|
}
|
|
|
|
}
|