2018-12-20 11:33:08 +01:00
|
|
|
<?php
|
2019-11-18 20:57:00 +01:00
|
|
|
namespace Activitypub\Model;
|
2019-02-24 12:07:41 +01:00
|
|
|
|
2018-12-20 11:33:08 +01:00
|
|
|
/**
|
|
|
|
* ActivityPub Post Class
|
|
|
|
*
|
|
|
|
* @author Matthias Pfefferle
|
2019-02-24 13:01:28 +01:00
|
|
|
*
|
|
|
|
* @see https://www.w3.org/TR/activitypub/
|
2018-12-20 11:33:08 +01:00
|
|
|
*/
|
2019-02-24 12:07:41 +01:00
|
|
|
class Activity {
|
2018-12-20 11:33:08 +01:00
|
|
|
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 ) {
|
2019-02-24 12:07:41 +01:00
|
|
|
$this->context = \Activitypub\get_context();
|
2018-12-20 11:33:08 +01:00
|
|
|
}
|
|
|
|
|
2019-09-27 10:12:59 +02:00
|
|
|
$this->type = \ucfirst( $type );
|
2019-12-01 21:20:26 +01:00
|
|
|
$this->published = \date( 'Y-m-d\TH:i:s\Z', \strtotime( 'now' ) );
|
2018-12-20 11:33:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function __call( $method, $params ) {
|
2019-09-27 10:12:59 +02:00
|
|
|
$var = \strtolower( \substr( $method, 4 ) );
|
2018-12-20 11:33:08 +01:00
|
|
|
|
2019-09-27 10:12:59 +02:00
|
|
|
if ( \strncasecmp( $method, 'get', 3 ) === 0 ) {
|
2018-12-20 11:33:08 +01:00
|
|
|
return $this->$var;
|
|
|
|
}
|
|
|
|
|
2019-09-27 10:12:59 +02:00
|
|
|
if ( \strncasecmp( $method, 'set', 3 ) === 0 ) {
|
2018-12-20 11:33:08 +01:00
|
|
|
$this->$var = $params[0];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public function from_post( $object ) {
|
|
|
|
$this->object = $object;
|
2020-05-23 12:34:11 +02:00
|
|
|
if ( isset( $object['published'] ) ) {
|
|
|
|
$this->published = $object['published'];
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( isset( $object['attributedTo'] ) ) {
|
|
|
|
$this->actor = $object['attributedTo'];
|
|
|
|
}
|
|
|
|
|
2022-01-17 11:03:30 +01:00
|
|
|
$type = \strtolower( $this->type );
|
|
|
|
|
2020-05-23 12:34:11 +02:00
|
|
|
if ( isset( $object['id'] ) ) {
|
2022-01-17 11:03:30 +01:00
|
|
|
$this->id = add_query_arg( 'activity', $type, $object['id'] );
|
2020-05-23 12:34:11 +02:00
|
|
|
}
|
2018-12-20 11:33:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function from_comment( $object ) {
|
2021-02-18 07:12:32 +01:00
|
|
|
$this->object = $object;
|
|
|
|
$this->published = $object['published'];
|
|
|
|
$this->actor = $object['attributedTo'];
|
|
|
|
$this->id = $object['id'] . '-activity';
|
|
|
|
$this->cc = $object['cc'];
|
|
|
|
$this->tag = $object['tag'];
|
2018-12-20 11:33:08 +01:00
|
|
|
}
|
|
|
|
|
2020-07-21 09:23:35 +02:00
|
|
|
public function to_comment() {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
public function from_remote_array( $array ) {
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2018-12-20 11:33:08 +01:00
|
|
|
public function to_array() {
|
2019-09-27 10:12:59 +02:00
|
|
|
$array = \get_object_vars( $this );
|
2018-12-20 11:33:08 +01:00
|
|
|
|
|
|
|
if ( $this->context ) {
|
|
|
|
$array = array( '@context' => $this->context ) + $array;
|
|
|
|
}
|
|
|
|
|
|
|
|
unset( $array['context'] );
|
|
|
|
|
|
|
|
return $array;
|
|
|
|
}
|
|
|
|
|
2020-07-21 09:23:35 +02:00
|
|
|
/**
|
|
|
|
* Convert to JSON
|
|
|
|
*
|
|
|
|
* @return void
|
|
|
|
*/
|
2018-12-20 11:33:08 +01:00
|
|
|
public function to_json() {
|
2020-05-12 19:42:09 +02:00
|
|
|
return \wp_json_encode( $this->to_array(), \JSON_HEX_TAG | \JSON_HEX_AMP | \JSON_HEX_QUOT );
|
2018-12-20 11:33:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function to_simple_array() {
|
2019-03-09 01:22:49 +01:00
|
|
|
$activity = array(
|
2018-12-20 11:33:08 +01:00
|
|
|
'@context' => $this->context,
|
|
|
|
'type' => $this->type,
|
|
|
|
'actor' => $this->actor,
|
|
|
|
'object' => $this->object,
|
|
|
|
'to' => $this->to,
|
2019-03-14 23:10:11 +01:00
|
|
|
'cc' => $this->cc,
|
2018-12-20 11:33:08 +01:00
|
|
|
);
|
2019-03-09 01:22:49 +01:00
|
|
|
|
|
|
|
if ( $this->id ) {
|
|
|
|
$activity['id'] = $this->id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $activity;
|
2018-12-20 11:33:08 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
public function to_simple_json() {
|
2020-05-12 19:42:09 +02:00
|
|
|
return \wp_json_encode( $this->to_simple_array(), \JSON_HEX_TAG | \JSON_HEX_AMP | \JSON_HEX_QUOT );
|
2018-12-20 11:33:08 +01:00
|
|
|
}
|
|
|
|
}
|