PluginProbe
ActivityPub / 5.3.1
ActivityPub v5.3.1
9.3.1 9.3.0 9.2.2 9.2.1 9.2.0 9.1.0 9.0.2 9.0.1 9.0.0 8.3.0 8.2.1 8.2.0 8.1.1 1.0.5 1.0.6 1.0.7 1.0.8 1.0.9 1.1.0 1.2.0 1.3.0 2.0.0 2.0.1 2.1.0 2.1.1 All 160 releases
activitypub / includes / activity / class-activity.php

class-activity.php in ActivityPub 5.3.1, at includes/activity/class-activity.php

234 lines 8.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Inspired by the PHP ActivityPub Library by @Landrok
4 *
5 * @link https://github.com/landrok/activitypub
6 *
7 * @package Activitypub
8 */
9
10 namespace Activitypub\Activity;
11
12 use Activitypub\Link;
13
14 use function Activitypub\is_actor;
15 use function Activitypub\is_activity;
16
17 /**
18 * \Activitypub\Activity\Activity implements the common
19 * attributes of an Activity.
20 *
21 * @see https://www.w3.org/TR/activitystreams-core/#activities
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.
56 */
57 class Activity extends Base_Object {
58 const JSON_LD_CONTEXT = array(
59 'https://www.w3.org/ns/activitystreams',
60 );
61
62 /**
63 * The type of the object.
64 *
65 * @var string
66 */
67 protected $type = 'Activity';
68
69 /**
70 * Describes the direct object of the activity.
71 * For instance, in the activity "John added a movie to his
72 * wishlist", the object of the activity is the movie added.
73 *
74 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-object-term
75 *
76 * @var string|Base_Object|null
77 */
78 protected $object;
79
80 /**
81 * Describes one or more entities that either performed or are
82 * expected to perform the activity.
83 * Any single activity can have multiple actors.
84 * The actor MAY be specified using an indirect Link.
85 *
86 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-actor
87 *
88 * @var string|array
89 */
90 protected $actor;
91
92 /**
93 * The indirect object, or target, of the activity.
94 * The precise meaning of the target is largely dependent on the
95 * type of action being described but will often be the object of
96 * the English preposition "to".
97 * For instance, in the activity "John added a movie to his
98 * wishlist", the target of the activity is John's wishlist.
99 * An activity can have more than one target.
100 *
101 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-target
102 *
103 * @var string|array
104 */
105 protected $target;
106
107 /**
108 * Describes the result of the activity.
109 * For instance, if a particular action results in the creation of
110 * a new resource, the result property can be used to describe
111 * that new resource.
112 *
113 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-result
114 *
115 * @var string|Base_Object
116 */
117 protected $result;
118
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 /**
132 * An indirect object of the activity from which the
133 * activity is directed.
134 * The precise meaning of the origin is the object of the English
135 * preposition "from".
136 * For instance, in the activity "John moved an item to List B
137 * from List A", the origin of the activity is "List A".
138 *
139 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-origin
140 *
141 * @var string|array
142 */
143 protected $origin;
144
145 /**
146 * One or more objects used (or to be used) in the completion of an
147 * Activity.
148 *
149 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-instrument
150 *
151 * @var string|array
152 */
153 protected $instrument;
154
155 /**
156 * Set the object and copy Object properties to the Activity.
157 *
158 * Any to, bto, cc, bcc, and audience properties specified on the object
159 * MUST be copied over to the new Create activity by the server.
160 *
161 * @see https://www.w3.org/TR/activitypub/#object-without-create
162 *
163 * @param array|string|Base_Object|Link|null $data Activity object.
164 */
165 public function set_object( $data ) {
166 // Convert array to object.
167 if ( is_array( $data ) ) {
168 $data = Generic_Object::init_from_array( $data );
169 }
170
171 // Set object.
172 $this->set( 'object', $data );
173
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
178 return;
179 }
180
181 // Check if `$data` is an object and copy some properties otherwise do nothing.
182 if ( ! is_object( $data ) ) {
183 return;
184 }
185
186 foreach ( array( 'to', 'bto', 'cc', 'bcc', 'audience' ) as $i ) {
187 $this->set( $i, $data->get( $i ) );
188 }
189
190 if ( $data->get_published() && ! $this->get_published() ) {
191 $this->set( 'published', $data->get_published() );
192 }
193
194 if ( $data->get_updated() && ! $this->get_updated() ) {
195 $this->set( 'updated', $data->get_updated() );
196 }
197
198 if ( $data->get_attributed_to() && ! $this->get_actor() ) {
199 $this->set( 'actor', $data->get_attributed_to() );
200 }
201
202 if ( $data->get_in_reply_to() ) {
203 $this->set( 'in_reply_to', $data->get_in_reply_to() );
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 }
215 }
216
217 /**
218 * The context of an Activity is usually just the context of the object it contains.
219 *
220 * @return array $context A compacted JSON-LD context.
221 */
222 public function get_json_ld_context() {
223 if ( $this->object instanceof Base_Object ) {
224 $class = get_class( $this->object );
225 if ( $class && $class::JSON_LD_CONTEXT ) {
226 // Without php 5.6 support this could be just: 'return $this->object::JSON_LD_CONTEXT;'.
227 return $class::JSON_LD_CONTEXT;
228 }
229 }
230
231 return static::JSON_LD_CONTEXT;
232 }
233 }
234