| 1 |
<?php |
| 2 |
namespace Activitypub; |
| 3 |
|
| 4 |
/** |
| 5 |
* ActivityPub Post Class |
| 6 |
* |
| 7 |
* @author Matthias Pfefferle |
| 8 |
* |
| 9 |
* @see https://www.w3.org/TR/activitypub/ |
| 10 |
*/ |
| 11 |
class Activity { |
| 12 |
private $context = array( 'https://www.w3.org/ns/activitystreams' ); |
| 13 |
private $published = ''; |
| 14 |
private $id = ''; |
| 15 |
private $type = 'Create'; |
| 16 |
private $actor = ''; |
| 17 |
private $to = array( 'https://www.w3.org/ns/activitystreams#Public' ); |
| 18 |
private $cc = array( 'https://www.w3.org/ns/activitystreams#Public' ); |
| 19 |
private $object = null; |
| 20 |
|
| 21 |
const TYPE_SIMPLE = 'simple'; |
| 22 |
const TYPE_FULL = 'full'; |
| 23 |
const TYPE_NONE = 'none'; |
| 24 |
|
| 25 |
public function __construct( $type = 'Create', $context = self::TYPE_SIMPLE ) { |
| 26 |
if ( 'none' === $context ) { |
| 27 |
$this->context = null; |
| 28 |
} elseif ( 'full' === $context ) { |
| 29 |
$this->context = \Activitypub\get_context(); |
| 30 |
} |
| 31 |
|
| 32 |
$this->type = ucfirst( $type ); |
| 33 |
$this->published = date( 'Y-m-d\TH:i:s\Z', strtotime( 'now' ) ); |
| 34 |
} |
| 35 |
|
| 36 |
public function __call( $method, $params ) { |
| 37 |
$var = strtolower( substr( $method, 4 ) ); |
| 38 |
|
| 39 |
if ( strncasecmp( $method, 'get', 3 ) === 0 ) { |
| 40 |
return $this->$var; |
| 41 |
} |
| 42 |
|
| 43 |
if ( strncasecmp( $method, 'set', 3 ) === 0 ) { |
| 44 |
$this->$var = $params[0]; |
| 45 |
} |
| 46 |
} |
| 47 |
|
| 48 |
public function from_post( $object ) { |
| 49 |
$this->object = $object; |
| 50 |
$this->published = $object['published']; |
| 51 |
$this->actor = $object['attributedTo']; |
| 52 |
$this->id = $object['id']; |
| 53 |
} |
| 54 |
|
| 55 |
public function from_comment( $object ) { |
| 56 |
|
| 57 |
} |
| 58 |
|
| 59 |
public function to_array() { |
| 60 |
$array = get_object_vars( $this ); |
| 61 |
|
| 62 |
if ( $this->context ) { |
| 63 |
$array = array( '@context' => $this->context ) + $array; |
| 64 |
} |
| 65 |
|
| 66 |
unset( $array['context'] ); |
| 67 |
|
| 68 |
return $array; |
| 69 |
} |
| 70 |
|
| 71 |
public function to_json() { |
| 72 |
return wp_json_encode( $this->to_array(), JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_QUOT ); |
| 73 |
} |
| 74 |
|
| 75 |
public function to_simple_array() { |
| 76 |
$activity = array( |
| 77 |
'@context' => $this->context, |
| 78 |
'type' => $this->type, |
| 79 |
'actor' => $this->actor, |
| 80 |
'object' => $this->object, |
| 81 |
'to' => $this->to, |
| 82 |
'cc' => $this->cc, |
| 83 |
); |
| 84 |
|
| 85 |
if ( $this->id ) { |
| 86 |
$activity['id'] = $this->id; |
| 87 |
} |
| 88 |
|
| 89 |
return $activity; |
| 90 |
} |
| 91 |
|
| 92 |
public function to_simple_json() { |
| 93 |
return wp_json_encode( $this->to_simple_array(), JSON_HEX_TAG | JSON_HEX_AMP | JSON_HEX_QUOT ); |
| 94 |
} |
| 95 |
} |
| 96 |
|