PluginProbe
ActivityPub / 3.2.4
ActivityPub v3.2.4
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
← All changes | includes/activity/class-activity.php +41 -160 8.3.0 → 3.2.4 View file →
@@ -2,16 +2,13 @@
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
8 6 */
9 7
10 8 namespace Activitypub\Activity;
11 9
12 -use Activitypub\Activity\Extended_Object\Event;
13 -use Activitypub\Activity\Extended_Object\Place;
10 +use Activitypub\Activity\Base_Object;
14 11
15 12 /**
16 13 * \Activitypub\Activity\Activity implements the common
17 14 * attributes of an Activity.
@@ -17,74 +14,15 @@
17 14 * attributes of an Activity.
18 15 *
19 16 * @see https://www.w3.org/TR/activitystreams-core/#activities
20 17 * @see https://www.w3.org/TR/activitystreams-core/#intransitiveactivities
21 - *
22 - * @method string|array get_actor() Gets one or more entities that performed or are expected to perform the activity.
23 - * @method string|array|null get_instrument() Gets one or more objects used in the completion of an Activity.
24 - * @method Base_Object|string|array|null get_object() Gets the direct object of the activity.
25 - * @method string|string[]|null get_origin() Gets the origin property of the activity.
26 - * @method array|null get_replies() Gets the collection of responses to this activity.
27 - * @method string|null get_result() Gets the result property of the activity.
28 - * @method string|string[]|null get_target() Gets the target property of the activity.
29 - *
30 - * @method Activity set_actor( string|array $actor ) Sets one or more entities that performed the activity.
31 - * @method Activity set_instrument( string|array $instrument ) Sets one or more objects used in the completion of an Activity.
32 - * @method Activity set_origin( string|array|null $origin ) Sets the origin property of the activity.
33 - * @method Activity set_replies( array $replies ) Sets the collection of responses to this activity.
34 - * @method Activity set_result( string|null $result ) Sets the result property of the activity.
35 - * @method Activity set_target( string|array|null $target ) Sets the target property of the activity.
36 18 */
37 19 class Activity extends Base_Object {
38 20 const JSON_LD_CONTEXT = array(
39 21 'https://www.w3.org/ns/activitystreams',
40 - array(
41 - 'toot' => 'http://joinmastodon.org/ns#',
42 - 'QuoteRequest' => 'toot:QuoteRequest',
43 - ),
44 22 );
45 23
46 24 /**
47 - * The default types for Activities.
48 - *
49 - * @see https://www.w3.org/TR/activitystreams-vocabulary/#activity-types
50 - *
51 - * @var array
52 - */
53 - const TYPES = array(
54 - 'Accept',
55 - 'Add',
56 - 'Announce',
57 - 'Arrive',
58 - 'Block',
59 - 'Create',
60 - 'Delete',
61 - 'Dislike',
62 - 'Follow',
63 - 'Flag',
64 - 'Ignore',
65 - 'Invite',
66 - 'Join',
67 - 'Leave',
68 - 'Like',
69 - 'Listen',
70 - 'Move',
71 - 'Offer',
72 - 'QuoteRequest', // @see https://codeberg.org/fediverse/fep/src/branch/main/fep/044f/fep-044f.md
73 - 'Read',
74 - 'Reject',
75 - 'Remove',
76 - 'TentativeAccept',
77 - 'TentativeReject',
78 - 'Travel',
79 - 'Undo',
80 - 'Update',
81 - 'View',
82 - );
83 -
84 - /**
85 - * The type of the object.
86 - *
87 25 * @var string
88 26 */
89 27 protected $type = 'Activity';
90 28
@@ -94,9 +32,12 @@
94 32 * wishlist", the object of the activity is the movie added.
95 33 *
96 34 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-object-term
97 35 *
98 - * @var string|Base_Object|array|null
36 + * @var string
37 + * | Base_Object
38 + * | Link
39 + * | null
99 40 */
100 41 protected $object;
101 42
102 43 /**
@@ -106,9 +47,13 @@
106 47 * The actor MAY be specified using an indirect Link.
107 48 *
108 49 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-actor
109 50 *
110 - * @var string|array
51 + * @var string
52 + * | \ActivityPhp\Type\Extended\AbstractActor
53 + * | array<Actor>
54 + * | array<Link>
55 + * | Link
111 56 */
112 57 protected $actor;
113 58
114 59 /**
@@ -121,9 +66,13 @@
121 66 * An activity can have more than one target.
122 67 *
123 68 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-target
124 69 *
125 - * @var string|array|null
70 + * @var string
71 + * | ObjectType
72 + * | array<ObjectType>
73 + * | Link
74 + * | array<Link>
126 75 */
127 76 protected $target;
128 77
129 78 /**
@@ -133,25 +82,16 @@
133 82 * that new resource.
134 83 *
135 84 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-result
136 85 *
137 - * @var string|Base_Object|null
86 + * @var string
87 + * | ObjectType
88 + * | Link
89 + * | null
138 90 */
139 91 protected $result;
140 92
141 93 /**
142 - * Identifies a Collection containing objects considered to be responses
143 - * to this object.
144 - * WordPress has a strong core system of approving replies. We only include
145 - * approved replies here.
146 - *
147 - * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-replies
148 - *
149 - * @var array|null
150 - */
151 - protected $replies;
152 -
153 - /**
154 94 * An indirect object of the activity from which the
155 95 * activity is directed.
156 96 * The precise meaning of the origin is the object of the English
157 97 * preposition "from".
@@ -159,9 +99,12 @@
159 99 * from List A", the origin of the activity is "List A".
160 100 *
161 101 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-origin
162 102 *
163 - * @var string|array|null
103 + * @var string
104 + * | ObjectType
105 + * | Link
106 + * | null
164 107 */
165 108 protected $origin;
166 109
167 110 /**
@@ -169,9 +112,12 @@
169 112 * Activity.
170 113 *
171 114 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-instrument
172 115 *
173 - * @var string|array|null
116 + * @var string
117 + * | ObjectType
118 + * | Link
119 + * | null
174 120 */
175 121 protected $instrument;
176 122
177 123 /**
@@ -177,53 +123,31 @@
177 123 /**
178 124 * Set the object and copy Object properties to the Activity.
179 125 *
180 126 * Any to, bto, cc, bcc, and audience properties specified on the object
181 - * MUST be copied over to the new "Create" activity by the server.
127 + * MUST be copied over to the new Create activity by the server.
182 128 *
183 129 * @see https://www.w3.org/TR/activitypub/#object-without-create
184 130 *
185 - * @param array|string|Base_Object|Activity|Actor|null $data Activity object.
131 + * @param string|Base_Objectr|Link|null $object
132 + *
133 + * @return void
186 134 */
187 - public function set_object( $data ) {
188 - $object = $data;
189 -
190 - // Convert array to appropriate object type.
191 - if ( is_array( $data ) ) {
192 - if ( array_is_list( $data ) ) {
193 - $object = array_map( array( $this, 'maybe_convert_to_object' ), $data );
194 - } else {
195 - $object = $this->maybe_convert_to_object( $data );
196 - }
135 + public function set_object( $object ) {
136 + // convert array to object
137 + if ( is_array( $object ) ) {
138 + $object = self::init_from_array( $object );
197 139 }
198 140
141 + // set object
199 142 $this->set( 'object', $object );
200 - $this->pre_fill_activity_from_object();
201 - }
202 143
203 - /**
204 - * Fills the Activity with the specified activity object.
205 - */
206 - public function pre_fill_activity_from_object() {
207 - $object = $this->get_object();
208 -
209 - // Check if `$object` is a URL and use it to generate an ID then.
210 - if ( is_string( $object ) && filter_var( $object, FILTER_VALIDATE_URL ) && ! $this->get_id() ) {
211 - $this->set( 'id', $object . '#activity-' . strtolower( $this->get_type() ) . '-' . time() );
212 -
213 - return;
214 - }
215 -
216 - // Check if `$object` is an object and copy some properties otherwise do nothing.
217 144 if ( ! is_object( $object ) ) {
218 145 return;
219 146 }
220 147
221 148 foreach ( array( 'to', 'bto', 'cc', 'bcc', 'audience' ) as $i ) {
222 - $value = $object->get( $i );
223 - if ( $value && ! $this->get( $i ) ) {
224 - $this->set( $i, $value );
225 - }
149 + $this->set( $i, $object->get( $i ) );
226 150 }
227 151
228 152 if ( $object->get_published() && ! $this->get_published() ) {
229 153 $this->set( 'published', $object->get_published() );
@@ -236,24 +160,18 @@
236 160 if ( $object->get_attributed_to() && ! $this->get_actor() ) {
237 161 $this->set( 'actor', $object->get_attributed_to() );
238 162 }
239 163
240 - if ( $this->get_type() !== 'Announce' && $object->get_in_reply_to() && ! $this->get_in_reply_to() ) {
164 + if ( $object->get_in_reply_to() ) {
241 165 $this->set( 'in_reply_to', $object->get_in_reply_to() );
242 166 }
243 167
244 - if ( $object->get_interaction_policy() && ! $this->get_interaction_policy() ) {
245 - $this->set( 'interaction_policy', $object->get_interaction_policy() );
246 - }
247 -
248 168 if ( $object->get_id() && ! $this->get_id() ) {
249 169 $id = strtok( $object->get_id(), '#' );
250 170 if ( $object->get_updated() ) {
251 171 $updated = $object->get_updated();
252 - } elseif ( $object->get_published() ) {
172 + } else {
253 173 $updated = $object->get_published();
254 - } else {
255 - $updated = time();
256 174 }
257 175 $this->set( 'id', $id . '#activity-' . strtolower( $this->get_type() ) . '-' . $updated );
258 176 }
259 177 }
@@ -263,52 +181,15 @@
263 181 *
264 182 * @return array $context A compacted JSON-LD context.
265 183 */
266 184 public function get_json_ld_context() {
267 - if ( \is_object( $this->object ) ) {
185 + if ( $this->object instanceof Base_Object ) {
268 186 $class = get_class( $this->object );
269 187 if ( $class && $class::JSON_LD_CONTEXT ) {
270 - // Without php 5.6 support this could be just: 'return $this->object::JSON_LD_CONTEXT;'.
188 + // Without php 5.6 support this could be just: 'return $this->object::JSON_LD_CONTEXT;'
271 189 return $class::JSON_LD_CONTEXT;
272 190 }
273 191 }
274 192
275 193 return static::JSON_LD_CONTEXT;
276 - }
277 -
278 - /**
279 - * Convert data to the appropriate object type if it has an ActivityPub type.
280 - *
281 - * @param array|string|Base_Object|Activity|Actor|null $data The data to convert.
282 - *
283 - * @return Activity|Actor|Base_Object|Generic_Object|string|\WP_Error|null The converted object or original data.
284 - */
285 - private function maybe_convert_to_object( $data ) {
286 - if ( ! is_array( $data ) ) {
287 - return $data;
288 - }
289 -
290 - $type = $data['type'] ?? null;
291 -
292 - if ( in_array( $type, self::TYPES, true ) ) {
293 - $object = self::init_from_array( $data );
294 - } elseif ( in_array( $type, Actor::TYPES, true ) ) {
295 - $object = Actor::init_from_array( $data );
296 - } elseif ( in_array( $type, Base_Object::TYPES, true ) ) {
297 - switch ( $type ) {
298 - case 'Event':
299 - $object = Event::init_from_array( $data );
300 - break;
301 - case 'Place':
302 - $object = Place::init_from_array( $data );
303 - break;
304 - default:
305 - $object = Base_Object::init_from_array( $data );
306 - break;
307 - }
308 - } else {
309 - $object = Generic_Object::init_from_array( $data );
310 - }
311 -
312 - return $object;
313 194 }
314 195 }