PluginProbe
ActivityPub / 4.4.0
ActivityPub v4.4.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 4.4.0, at includes/activity/class-activity.php

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