PluginProbe
ActivityPub / 5.7.0
ActivityPub v5.7.0
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.7.0, at includes/activity/class-activity.php

270 lines 7.0 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\Activity\Extended_Object\Event;
13 use Activitypub\Activity\Extended_Object\Place;
14
15 /**
16 * \Activitypub\Activity\Activity implements the common
17 * attributes of an Activity.
18 *
19 * @see https://www.w3.org/TR/activitystreams-core/#activities
20 * @see https://www.w3.org/TR/activitystreams-core/#intransitiveactivities
21 */
22 class Activity extends Base_Object {
23 const JSON_LD_CONTEXT = array(
24 'https://www.w3.org/ns/activitystreams',
25 );
26
27 /**
28 * The default types for Activities.
29 *
30 * @see https://www.w3.org/TR/activitystreams-vocabulary/#activity-types
31 *
32 * @var array
33 */
34 const TYPES = array(
35 'Accept',
36 'Add',
37 'Announce',
38 'Arrive',
39 'Block',
40 'Create',
41 'Delete',
42 'Dislike',
43 'Follow',
44 'Flag',
45 'Ignore',
46 'Invite',
47 'Join',
48 'Leave',
49 'Like',
50 'Listen',
51 'Move',
52 'Offer',
53 'Read',
54 'Reject',
55 'Remove',
56 'TentativeAccept',
57 'TentativeReject',
58 'Travel',
59 'Undo',
60 'Update',
61 'View',
62 );
63
64 /**
65 * The type of the object.
66 *
67 * @var string
68 */
69 protected $type = 'Activity';
70
71 /**
72 * Describes the direct object of the activity.
73 * For instance, in the activity "John added a movie to his
74 * wishlist", the object of the activity is the movie added.
75 *
76 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-object-term
77 *
78 * @var string|Base_Object|null
79 */
80 protected $object;
81
82 /**
83 * Describes one or more entities that either performed or are
84 * expected to perform the activity.
85 * Any single activity can have multiple actors.
86 * The actor MAY be specified using an indirect Link.
87 *
88 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-actor
89 *
90 * @var string|array
91 */
92 protected $actor;
93
94 /**
95 * The indirect object, or target, of the activity.
96 * The precise meaning of the target is largely dependent on the
97 * type of action being described but will often be the object of
98 * the English preposition "to".
99 * For instance, in the activity "John added a movie to his
100 * wishlist", the target of the activity is John's wishlist.
101 * An activity can have more than one target.
102 *
103 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-target
104 *
105 * @var string|array
106 */
107 protected $target;
108
109 /**
110 * Describes the result of the activity.
111 * For instance, if a particular action results in the creation of
112 * a new resource, the result property can be used to describe
113 * that new resource.
114 *
115 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-result
116 *
117 * @var string|Base_Object
118 */
119 protected $result;
120
121 /**
122 * Identifies a Collection containing objects considered to be responses
123 * to this object.
124 * WordPress has a strong core system of approving replies. We only include
125 * approved replies here.
126 *
127 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-replies
128 *
129 * @var array
130 */
131 protected $replies;
132
133 /**
134 * An indirect object of the activity from which the
135 * activity is directed.
136 * The precise meaning of the origin is the object of the English
137 * preposition "from".
138 * For instance, in the activity "John moved an item to List B
139 * from List A", the origin of the activity is "List A".
140 *
141 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-origin
142 *
143 * @var string|array
144 */
145 protected $origin;
146
147 /**
148 * One or more objects used (or to be used) in the completion of an
149 * Activity.
150 *
151 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-instrument
152 *
153 * @var string|array
154 */
155 protected $instrument;
156
157 /**
158 * Set the object and copy Object properties to the Activity.
159 *
160 * Any to, bto, cc, bcc, and audience properties specified on the object
161 * MUST be copied over to the new "Create" activity by the server.
162 *
163 * @see https://www.w3.org/TR/activitypub/#object-without-create
164 *
165 * @param array|string|Base_Object|Activity|Actor|null $data Activity object.
166 */
167 public function set_object( $data ) {
168 $object = $data;
169
170 // Convert array to appropriate object type.
171 if ( is_array( $data ) ) {
172 $type = $data['type'] ?? null;
173
174 if ( in_array( $type, self::TYPES, true ) ) {
175 $object = self::init_from_array( $data );
176 } elseif ( in_array( $type, Actor::TYPES, true ) ) {
177 $object = Actor::init_from_array( $data );
178 } elseif ( in_array( $type, Base_Object::TYPES, true ) ) {
179 switch ( $type ) {
180 case 'Event':
181 $object = Event::init_from_array( $data );
182 break;
183 case 'Place':
184 $object = Place::init_from_array( $data );
185 break;
186 default:
187 $object = Base_Object::init_from_array( $data );
188 break;
189 }
190 } else {
191 $object = Generic_Object::init_from_array( $data );
192 }
193 }
194
195 $this->set( 'object', $object );
196 $this->pre_fill_activity_from_object();
197 }
198
199 /**
200 * Fills the Activity with the specified activity object.
201 */
202 public function pre_fill_activity_from_object() {
203 $object = $this->get_object();
204
205 // Check if `$data` is a URL and use it to generate an ID then.
206 if ( is_string( $object ) && filter_var( $object, FILTER_VALIDATE_URL ) && ! $this->get_id() ) {
207 $this->set( 'id', $object . '#activity-' . strtolower( $this->get_type() ) . '-' . time() );
208
209 return;
210 }
211
212 // Check if `$data` is an object and copy some properties otherwise do nothing.
213 if ( ! is_object( $object ) ) {
214 return;
215 }
216
217 foreach ( array( 'to', 'bto', 'cc', 'bcc', 'audience' ) as $i ) {
218 $value = $object->get( $i );
219 if ( $value && ! $this->get( $i ) ) {
220 $this->set( $i, $value );
221 }
222 }
223
224 if ( $object->get_published() && ! $this->get_published() ) {
225 $this->set( 'published', $object->get_published() );
226 }
227
228 if ( $object->get_updated() && ! $this->get_updated() ) {
229 $this->set( 'updated', $object->get_updated() );
230 }
231
232 if ( $object->get_attributed_to() && ! $this->get_actor() ) {
233 $this->set( 'actor', $object->get_attributed_to() );
234 }
235
236 if ( $this->get_type() !== 'Announce' && $object->get_in_reply_to() && ! $this->get_in_reply_to() ) {
237 $this->set( 'in_reply_to', $object->get_in_reply_to() );
238 }
239
240 if ( $object->get_id() && ! $this->get_id() ) {
241 $id = strtok( $object->get_id(), '#' );
242 if ( $object->get_updated() ) {
243 $updated = $object->get_updated();
244 } elseif ( $object->get_published() ) {
245 $updated = $object->get_published();
246 } else {
247 $updated = time();
248 }
249 $this->set( 'id', $id . '#activity-' . strtolower( $this->get_type() ) . '-' . $updated );
250 }
251 }
252
253 /**
254 * The context of an Activity is usually just the context of the object it contains.
255 *
256 * @return array $context A compacted JSON-LD context.
257 */
258 public function get_json_ld_context() {
259 if ( $this->object instanceof Base_Object ) {
260 $class = get_class( $this->object );
261 if ( $class && $class::JSON_LD_CONTEXT ) {
262 // Without php 5.6 support this could be just: 'return $this->object::JSON_LD_CONTEXT;'.
263 return $class::JSON_LD_CONTEXT;
264 }
265 }
266
267 return static::JSON_LD_CONTEXT;
268 }
269 }
270