| @@ -2,14 +2,19 @@ | ||
| 2 | 2 | /** |
| 3 | 3 | * Inspired by the PHP ActivityPub Library by @Landrok |
| 4 | 4 | * |
| 5 | 5 | * @link https://github.com/landrok/activitypub |
| 6 | + * | |
| 7 | + * @package Activitypub | |
| 6 | 8 | */ |
| 7 | 9 | |
| 8 | 10 | namespace Activitypub\Activity; |
| 9 | 11 | |
| 10 | -use Activitypub\Activity\Base_Object; | |
| 12 | +use Activitypub\Link; | |
| 11 | 13 | |
| 14 | +use function Activitypub\is_actor; | |
| 15 | +use function Activitypub\is_activity; | |
| 16 | + | |
| 12 | 17 | /** |
| 13 | 18 | * \Activitypub\Activity\Activity implements the common |
| 14 | 19 | * attributes of an Activity. |
| 15 | 20 | * |
| @@ -14,8 +19,41 @@ | ||
| 14 | 19 | * attributes of an Activity. |
| 15 | 20 | * |
| 16 | 21 | * @see https://www.w3.org/TR/activitystreams-core/#activities |
| 17 | 22 | * @see https://www.w3.org/TR/activitystreams-core/#intransitiveactivities |
| 23 | + * | |
| 24 | + * @method string|array|null get_actor() Gets one or more entities that performed or are expected to perform the activity. | |
| 25 | + * @method string|null get_id() Gets the object's unique global identifier. | |
| 26 | + * @method string get_type() Gets the type of the object. | |
| 27 | + * @method string|null get_name() Gets the natural language name of the object. | |
| 28 | + * @method string|null get_url() Gets the URL of the object. | |
| 29 | + * @method string|null get_summary() Gets the natural language summary of the object. | |
| 30 | + * @method string|null get_published() Gets the date and time the object was published in ISO 8601 format. | |
| 31 | + * @method string|null get_updated() Gets the date and time the object was updated in ISO 8601 format. | |
| 32 | + * @method string|null get_attributed_to() Gets the entity attributed as the original author. | |
| 33 | + * @method array|string|null get_cc() Gets the secondary recipients of the object. | |
| 34 | + * @method array|string|null get_to() Gets the primary recipients of the object. | |
| 35 | + * @method array|null get_attachment() Gets the attachment property of the object. | |
| 36 | + * @method array|null get_icon() Gets the icon property of the object. | |
| 37 | + * @method array|null get_image() Gets the image property of the object. | |
| 38 | + * @method Base_Object|string|null get_object() Gets the direct object of the activity. | |
| 39 | + * @method array|string|null get_in_reply_to() Gets the objects this object is in reply to. | |
| 40 | + * | |
| 41 | + * @method Activity set_actor( string|array $actor ) Sets one or more entities that performed the activity. | |
| 42 | + * @method Activity set_id( string $id ) Sets the object's unique global identifier. | |
| 43 | + * @method Activity set_type( string $type ) Sets the type of the object. | |
| 44 | + * @method Activity set_name( string $name ) Sets the natural language name of the object. | |
| 45 | + * @method Activity set_url( string $url ) Sets the URL of the object. | |
| 46 | + * @method Activity set_summary( string $summary ) Sets the natural language summary of the object. | |
| 47 | + * @method Activity set_published( string $published ) Sets the date and time the object was published in ISO 8601 format. | |
| 48 | + * @method Activity set_updated( string $updated ) Sets the date and time the object was updated in ISO 8601 format. | |
| 49 | + * @method Activity set_attributed_to( string $attributed_to ) Sets the entity attributed as the original author. | |
| 50 | + * @method Activity set_cc( array|string $cc ) Sets the secondary recipients of the object. | |
| 51 | + * @method Activity set_to( array|string $to ) Sets the primary recipients of the object. | |
| 52 | + * @method Activity set_attachment( array $attachment ) Sets the attachment property of the object. | |
| 53 | + * @method Activity set_icon( array $icon ) Sets the icon property of the object. | |
| 54 | + * @method Activity set_image( array $image ) Sets the image property of the object. | |
| 55 | + * @method Activity set_content( string $content ) Sets the content property of the object. | |
| 18 | 56 | */ |
| 19 | 57 | class Activity extends Base_Object { |
| 20 | 58 | const JSON_LD_CONTEXT = array( |
| 21 | 59 | 'https://www.w3.org/ns/activitystreams', |
| @@ -21,8 +59,10 @@ | ||
| 21 | 59 | 'https://www.w3.org/ns/activitystreams', |
| 22 | 60 | ); |
| 23 | 61 | |
| 24 | 62 | /** |
| 63 | + * The type of the object. | |
| 64 | + * | |
| 25 | 65 | * @var string |
| 26 | 66 | */ |
| 27 | 67 | protected $type = 'Activity'; |
| 28 | 68 | |
| @@ -32,12 +72,9 @@ | ||
| 32 | 72 | * wishlist", the object of the activity is the movie added. |
| 33 | 73 | * |
| 34 | 74 | * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-object-term |
| 35 | 75 | * |
| 36 | - * @var string | |
| 37 | - * | Base_Object | |
| 38 | - * | Link | |
| 39 | - * | null | |
| 76 | + * @var string|Base_Object|null | |
| 40 | 77 | */ |
| 41 | 78 | protected $object; |
| 42 | 79 | |
| 43 | 80 | /** |
| @@ -47,13 +84,9 @@ | ||
| 47 | 84 | * The actor MAY be specified using an indirect Link. |
| 48 | 85 | * |
| 49 | 86 | * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-actor |
| 50 | 87 | * |
| 51 | - * @var string | |
| 52 | - * | \ActivityPhp\Type\Extended\AbstractActor | |
| 53 | - * | array<Actor> | |
| 54 | - * | array<Link> | |
| 55 | - * | Link | |
| 88 | + * @var string|array | |
| 56 | 89 | */ |
| 57 | 90 | protected $actor; |
| 58 | 91 | |
| 59 | 92 | /** |
| @@ -66,13 +99,9 @@ | ||
| 66 | 99 | * An activity can have more than one target. |
| 67 | 100 | * |
| 68 | 101 | * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-target |
| 69 | 102 | * |
| 70 | - * @var string | |
| 71 | - * | ObjectType | |
| 72 | - * | array<ObjectType> | |
| 73 | - * | Link | |
| 74 | - * | array<Link> | |
| 103 | + * @var string|array | |
| 75 | 104 | */ |
| 76 | 105 | protected $target; |
| 77 | 106 | |
| 78 | 107 | /** |
| @@ -82,16 +111,25 @@ | ||
| 82 | 111 | * that new resource. |
| 83 | 112 | * |
| 84 | 113 | * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-result |
| 85 | 114 | * |
| 86 | - * @var string | |
| 87 | - * | ObjectType | |
| 88 | - * | Link | |
| 89 | - * | null | |
| 115 | + * @var string|Base_Object | |
| 90 | 116 | */ |
| 91 | 117 | protected $result; |
| 92 | 118 | |
| 93 | 119 | /** |
| 120 | + * Identifies a Collection containing objects considered to be responses | |
| 121 | + * to this object. | |
| 122 | + * WordPress has a strong core system of approving replies. We only include | |
| 123 | + * approved replies here. | |
| 124 | + * | |
| 125 | + * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-replies | |
| 126 | + * | |
| 127 | + * @var array | |
| 128 | + */ | |
| 129 | + protected $replies; | |
| 130 | + | |
| 131 | + /** | |
| 94 | 132 | * An indirect object of the activity from which the |
| 95 | 133 | * activity is directed. |
| 96 | 134 | * The precise meaning of the origin is the object of the English |
| 97 | 135 | * preposition "from". |
| @@ -99,12 +137,9 @@ | ||
| 99 | 137 | * from List A", the origin of the activity is "List A". |
| 100 | 138 | * |
| 101 | 139 | * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-origin |
| 102 | 140 | * |
| 103 | - * @var string | |
| 104 | - * | ObjectType | |
| 105 | - * | Link | |
| 106 | - * | null | |
| 141 | + * @var string|array | |
| 107 | 142 | */ |
| 108 | 143 | protected $origin; |
| 109 | 144 | |
| 110 | 145 | /** |
| @@ -112,12 +147,9 @@ | ||
| 112 | 147 | * Activity. |
| 113 | 148 | * |
| 114 | 149 | * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-instrument |
| 115 | 150 | * |
| 116 | - * @var string | |
| 117 | - * | ObjectType | |
| 118 | - * | Link | |
| 119 | - * | null | |
| 151 | + * @var string|array | |
| 120 | 152 | */ |
| 121 | 153 | protected $instrument; |
| 122 | 154 | |
| 123 | 155 | /** |
| @@ -127,44 +159,60 @@ | ||
| 127 | 159 | * MUST be copied over to the new Create activity by the server. |
| 128 | 160 | * |
| 129 | 161 | * @see https://www.w3.org/TR/activitypub/#object-without-create |
| 130 | 162 | * |
| 131 | - * @param string|Base_Objectr|Link|null $object | |
| 132 | - * | |
| 133 | - * @return void | |
| 163 | + * @param array|string|Base_Object|Link|null $data Activity object. | |
| 134 | 164 | */ |
| 135 | - public function set_object( $object ) { | |
| 136 | - // convert array to object | |
| 137 | - if ( is_array( $object ) ) { | |
| 138 | - $object = self::init_from_array( $object ); | |
| 165 | + public function set_object( $data ) { | |
| 166 | + // Convert array to object. | |
| 167 | + if ( is_array( $data ) ) { | |
| 168 | + $data = Generic_Object::init_from_array( $data ); | |
| 139 | 169 | } |
| 140 | 170 | |
| 141 | - // set object | |
| 142 | - $this->set( 'object', $object ); | |
| 171 | + // Set object. | |
| 172 | + $this->set( 'object', $data ); | |
| 143 | 173 | |
| 144 | - if ( ! is_object( $object ) ) { | |
| 174 | + // Check if `$data` is a URL and use it to generate an ID then. | |
| 175 | + if ( is_string( $data ) && filter_var( $data, FILTER_VALIDATE_URL ) && ! $this->get_id() ) { | |
| 176 | + $this->set( 'id', $data . '#activity-' . strtolower( $this->get_type() ) . '-' . time() ); | |
| 177 | + | |
| 145 | 178 | return; |
| 146 | 179 | } |
| 147 | 180 | |
| 181 | + // Check if `$data` is an object and copy some properties otherwise do nothing. | |
| 182 | + if ( ! is_object( $data ) ) { | |
| 183 | + return; | |
| 184 | + } | |
| 185 | + | |
| 148 | 186 | foreach ( array( 'to', 'bto', 'cc', 'bcc', 'audience' ) as $i ) { |
| 149 | - $this->set( $i, $object->get( $i ) ); | |
| 187 | + $this->set( $i, $data->get( $i ) ); | |
| 150 | 188 | } |
| 151 | 189 | |
| 152 | - if ( $object->get_published() && ! $this->get_published() ) { | |
| 153 | - $this->set( 'published', $object->get_published() ); | |
| 190 | + if ( $data->get_published() && ! $this->get_published() ) { | |
| 191 | + $this->set( 'published', $data->get_published() ); | |
| 154 | 192 | } |
| 155 | 193 | |
| 156 | - if ( $object->get_updated() && ! $this->get_updated() ) { | |
| 157 | - $this->set( 'updated', $object->get_updated() ); | |
| 194 | + if ( $data->get_updated() && ! $this->get_updated() ) { | |
| 195 | + $this->set( 'updated', $data->get_updated() ); | |
| 158 | 196 | } |
| 159 | 197 | |
| 160 | - if ( $object->get_attributed_to() && ! $this->get_actor() ) { | |
| 161 | - $this->set( 'actor', $object->get_attributed_to() ); | |
| 198 | + if ( $data->get_attributed_to() && ! $this->get_actor() ) { | |
| 199 | + $this->set( 'actor', $data->get_attributed_to() ); | |
| 162 | 200 | } |
| 163 | 201 | |
| 164 | - if ( $object->get_id() && ! $this->get_id() ) { | |
| 165 | - $this->set( 'id', $object->get_id() . '#activity' ); | |
| 202 | + if ( $data->get_in_reply_to() ) { | |
| 203 | + $this->set( 'in_reply_to', $data->get_in_reply_to() ); | |
| 166 | 204 | } |
| 205 | + | |
| 206 | + if ( $data->get_id() && ! $this->get_id() ) { | |
| 207 | + $id = strtok( $data->get_id(), '#' ); | |
| 208 | + if ( $data->get_updated() ) { | |
| 209 | + $updated = $data->get_updated(); | |
| 210 | + } else { | |
| 211 | + $updated = $data->get_published(); | |
| 212 | + } | |
| 213 | + $this->set( 'id', $id . '#activity-' . strtolower( $this->get_type() ) . '-' . $updated ); | |
| 214 | + } | |
| 167 | 215 | } |
| 168 | 216 | |
| 169 | 217 | /** |
| 170 | 218 | * The context of an Activity is usually just the context of the object it contains. |
| @@ -174,9 +222,9 @@ | ||
| 174 | 222 | public function get_json_ld_context() { |
| 175 | 223 | if ( $this->object instanceof Base_Object ) { |
| 176 | 224 | $class = get_class( $this->object ); |
| 177 | 225 | if ( $class && $class::JSON_LD_CONTEXT ) { |
| 178 | - // Without php 5.6 support this could be just: 'return $this->object::JSON_LD_CONTEXT;' | |
| 226 | + // Without php 5.6 support this could be just: 'return $this->object::JSON_LD_CONTEXT;'. | |
| 179 | 227 | return $class::JSON_LD_CONTEXT; |
| 180 | 228 | } |
| 181 | 229 | } |
| 182 | 230 | |