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
← All changes | includes/activity/class-base-object.php +129 -299 1.2.0 → 5.7.0 View file →
@@ -2,18 +2,14 @@
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 WP_Error;
11 -use ReflectionClass;
12 -
13 -use function Activitypub\camel_to_snake_case;
14 -use function Activitypub\snake_to_camel_case;
15 -
16 12 /**
17 13 * Base_Object is an implementation of one of the
18 14 * Activity Streams Core Types.
19 15 *
@@ -23,20 +19,91 @@
23 19 * Note: Object is a reserved keyword in PHP. It has been suffixed with
24 20 * 'Base_' for this reason.
25 21 *
26 22 * @see https://www.w3.org/TR/activitystreams-core/#object
23 + *
24 + * @method string|null get_actor() Gets one or more entities that performed or are expected to perform the activity.
25 + * @method string|null get_attributed_to() Gets the entity attributed as the original author.
26 + * @method array|null get_attachment() Gets the attachment property of the object.
27 + * @method array|null get_cc() Gets the secondary recipients of the object.
28 + * @method string|null get_content() Gets the content property of the object.
29 + * @method array|null get_icon() Gets the icon property of the object.
30 + * @method string|null get_id() Gets the object's unique global identifier.
31 + * @method array|null get_image() Gets the image property of the object.
32 + * @method array|string|null get_in_reply_to() Gets the objects this object is in reply to.
33 + * @method string|null get_name() Gets the natural language name of the object.
34 + * @method Base_Object|string|null get_object() Gets the direct object of the activity.
35 + * @method string|null get_published() Gets the date and time the object was published in ISO 8601 format.
36 + * @method string|null get_summary() Gets the natural language summary of the object.
37 + * @method array|null get_tag() Gets the tag property of the object.
38 + * @method array|string|null get_to() Gets the primary recipients of the object.
39 + * @method string get_type() Gets the type of the object.
40 + * @method string|null get_updated() Gets the date and time the object was updated in ISO 8601 format.
41 + * @method string|null get_url() Gets the URL of the object.
42 + *
43 + * @method string|array add_cc( string|array $cc ) Adds one or more entities to the secondary audience of the object.
44 + * @method string|array add_to( string|array $to ) Adds one or more entities to the primary audience of the object.
45 + *
46 + * @method Base_Object set_actor( string|array $actor ) Sets one or more entities that performed the activity.
47 + * @method Base_Object set_attachment( array $attachment ) Sets the attachment property of the object.
48 + * @method Base_Object set_attributed_to( string $attributed_to ) Sets the entity attributed as the original author.
49 + * @method Base_Object set_cc( array|string $cc ) Sets the secondary recipients of the object.
50 + * @method Base_Object set_content( string $content ) Sets the content property of the object.
51 + * @method Base_Object set_content_map( array $content_map ) Sets the content property of the object.
52 + * @method Base_Object set_icon( array $icon ) Sets the icon property of the object.
53 + * @method Base_Object set_id( string $id ) Sets the object's unique global identifier.
54 + * @method Base_Object set_image( array $image ) Sets the image property of the object.
55 + * @method Base_Object set_name( string $name ) Sets the natural language name of the object.
56 + * @method Base_Object set_origin( string $origin ) Sets the origin property of the object.
57 + * @method Base_Object set_published( string $published ) Sets the date and time the object was published in ISO 8601 format.
58 + * @method Base_Object set_sensitive( bool $sensitive ) Sets the sensitive property of the object.
59 + * @method Base_Object set_summary( string $summary ) Sets the natural language summary of the object.
60 + * @method Base_Object set_summary_map( array|null $summary_map ) Sets the summary property of the object.
61 + * @method Base_Object set_target( string $target ) Sets the target property of the object.
62 + * @method Base_Object set_to( array|string $to ) Sets the primary recipients of the object.
63 + * @method Base_Object set_type( string $type ) Sets the type of the object.
64 + * @method Base_Object set_updated( string $updated ) Sets the date and time the object was updated in ISO 8601 format.
65 + * @method Base_Object set_url( string $url ) Sets the URL of the object.
27 66 */
28 -class Base_Object {
67 +class Base_Object extends Generic_Object {
29 68 /**
30 - * The object's unique global identifier
69 + * The JSON-LD context for the object.
31 70 *
32 - * @see https://www.w3.org/TR/activitypub/#obj-id
71 + * @var array
72 + */
73 + const JSON_LD_CONTEXT = array(
74 + 'https://www.w3.org/ns/activitystreams',
75 + array(
76 + 'Hashtag' => 'as:Hashtag',
77 + 'sensitive' => 'as:sensitive',
78 + ),
79 + );
80 +
81 + /**
82 + * The default types for Objects.
33 83 *
34 - * @var string
84 + * @see https://www.w3.org/TR/activitystreams-vocabulary/#object-types
85 + *
86 + * @var array
35 87 */
36 - protected $id;
88 + const TYPES = array(
89 + 'Article',
90 + 'Audio',
91 + 'Document',
92 + 'Event',
93 + 'Image',
94 + 'Note',
95 + 'Page',
96 + 'Place',
97 + 'Profile',
98 + 'Relationship',
99 + 'Tombstone',
100 + 'Video',
101 + );
37 102
38 103 /**
104 + * The type of the object.
105 + *
39 106 * @var string
40 107 */
41 108 protected $type = 'Object';
42 109
@@ -47,14 +114,9 @@
47 114 * similar to attachments in email.
48 115 *
49 116 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-attachment
50 117 *
51 - * @var string
52 - * | ObjectType
53 - * | Link
54 - * | array<ObjectType>
55 - * | array<Link>
56 - * | null
118 + * @var string|null
57 119 */
58 120 protected $attachment;
59 121
60 122 /**
@@ -63,14 +125,9 @@
63 125 * object might be attributed to the completion of another activity.
64 126 *
65 127 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-attributedto
66 128 *
67 - * @var string
68 - * | ObjectType
69 - * | Link
70 - * | array<ObjectType>
71 - * | array<Link>
72 - * | null
129 + * @var string|null
73 130 */
74 131 protected $attributed_to;
75 132
76 133 /**
@@ -78,14 +135,9 @@
78 135 * entities for which the object can considered to be relevant.
79 136 *
80 137 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-audience
81 138 *
82 - * @var string
83 - * | ObjectType
84 - * | Link
85 - * | array<ObjectType>
86 - * | array<Link>
87 - * | null
139 + * @var string|null
88 140 */
89 141 protected $audience;
90 142
91 143 /**
@@ -113,12 +165,9 @@
113 165 * project or event.
114 166 *
115 167 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-context
116 168 *
117 - * @var string
118 - * | ObjectType
119 - * | Link
120 - * | null
169 + * @var string|null
121 170 */
122 171 protected $context;
123 172
124 173 /**
@@ -177,14 +226,9 @@
177 226 * at a small size.
178 227 *
179 228 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-icon
180 229 *
181 - * @var string
182 - * | Image
183 - * | Link
184 - * | array<Image>
185 - * | array<Link>
186 - * | null
230 + * @var string|array|null
187 231 */
188 232 protected $icon;
189 233
190 234 /**
@@ -193,14 +237,9 @@
193 237 * or display size limitations assumed.
194 238 *
195 239 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-image-term
196 240 *
197 - * @var string
198 - * | Image
199 - * | Link
200 - * | array<Image>
201 - * | array<Link>
202 - * | null
241 + * @var string|array|null
203 242 */
204 243 protected $image;
205 244
206 245 /**
@@ -208,14 +247,9 @@
208 247 * response.
209 248 *
210 249 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-inreplyto
211 250 *
212 - * @var string
213 - * | ObjectType
214 - * | Link
215 - * | array<ObjectType>
216 - * | array<Link>
217 - * | null
251 + * @var string|null
218 252 */
219 253 protected $in_reply_to;
220 254
221 255 /**
@@ -223,14 +257,9 @@
223 257 * object.
224 258 *
225 259 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-location
226 260 *
227 - * @var string
228 - * | ObjectType
229 - * | Link
230 - * | array<ObjectType>
231 - * | array<Link>
232 - * | null
261 + * @var string|null
233 262 */
234 263 protected $location;
235 264
236 265 /**
@@ -237,12 +266,9 @@
237 266 * An entity that provides a preview of this object.
238 267 *
239 268 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-preview
240 269 *
241 - * @var string
242 - * | ObjectType
243 - * | Link
244 - * | null
270 + * @var string|null
245 271 */
246 272 protected $preview;
247 273
248 274 /**
@@ -254,21 +280,8 @@
254 280 */
255 281 protected $published;
256 282
257 283 /**
258 - * A Collection containing objects considered to be responses to
259 - * this object.
260 - *
261 - * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-replies
262 - *
263 - * @var string
264 - * | Collection
265 - * | Link
266 - * | null
267 - */
268 - protected $replies;
269 -
270 - /**
271 284 * The date and time describing the actual or expected starting time
272 285 * of the object.
273 286 * When used with an Activity object, for instance, the startTime
274 287 * property specifies the moment the activity began
@@ -285,12 +298,9 @@
285 298 * Multiple language tagged summaries MAY be provided.
286 299 *
287 300 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-summary
288 301 *
289 - * @var string
290 - * | ObjectType
291 - * | Link
292 - * | null
302 + * @var string|null
293 303 */
294 304 protected $summary;
295 305
296 306 /**
@@ -298,9 +308,9 @@
298 308 * values.
299 309 *
300 310 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-summary
301 311 *
302 - * @var array<string>|null
312 + * @var string[]|null
303 313 */
304 314 protected $summary_map;
305 315
306 316 /**
@@ -311,14 +321,9 @@
311 321 * associated by reference.
312 322 *
313 323 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-tag
314 324 *
315 - * @var string
316 - * | ObjectType
317 - * | Link
318 - * | array<ObjectType>
319 - * | array<Link>
320 - * | null
325 + * @var string|null
321 326 */
322 327 protected $tag;
323 328
324 329 /**
@@ -332,13 +337,9 @@
332 337
333 338 /**
334 339 * One or more links to representations of the object.
335 340 *
336 - * @var string
337 - * | array<string>
338 - * | Link
339 - * | array<Link>
340 - * | null
341 + * @var string|null
341 342 */
342 343 protected $url;
343 344
344 345 /**
@@ -346,14 +347,9 @@
346 347 * of an Object
347 348 *
348 349 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-to
349 350 *
350 - * @var string
351 - * | ObjectType
352 - * | Link
353 - * | array<ObjectType>
354 - * | array<Link>
355 - * | null
351 + * @var string|array|null
356 352 */
357 353 protected $to;
358 354
359 355 /**
@@ -361,14 +357,9 @@
361 357 * Object.
362 358 *
363 359 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-bto
364 360 *
365 - * @var string
366 - * | ObjectType
367 - * | Link
368 - * | array<ObjectType>
369 - * | array<Link>
370 - * | null
361 + * @var string|array|null
371 362 */
372 363 protected $bto;
373 364
374 365 /**
@@ -376,14 +367,9 @@
376 367 * Object.
377 368 *
378 369 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-cc
379 370 *
380 - * @var string
381 - * | ObjectType
382 - * | Link
383 - * | array<ObjectType>
384 - * | array<Link>
385 - * | null
371 + * @var string|array|null
386 372 */
387 373 protected $cc;
388 374
389 375 /**
@@ -391,14 +377,9 @@
391 377 * audience of this Object.
392 378 *
393 379 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-bcc
394 380 *
395 - * @var string
396 - * | ObjectType
397 - * | Link
398 - * | array<ObjectType>
399 - * | array<Link>
400 - * | null
381 + * @var string|array|null
401 382 */
402 383 protected $bcc;
403 384
404 385 /**
@@ -432,57 +413,52 @@
432 413 * future editing by clients.
433 414 *
434 415 * @see https://www.w3.org/TR/activitypub/#source-property
435 416 *
436 - * @var ObjectType
417 + * @var array
437 418 */
438 419 protected $source;
439 420
440 421 /**
441 - * Magic function to implement getter and setter
422 + * A Collection containing objects considered to be responses to
423 + * this object.
442 424 *
443 - * @param string $method The method name.
444 - * @param string $params The method params.
425 + * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-replies
445 426 *
446 - * @return void
427 + * @var string|array|null
447 428 */
448 - public function __call( $method, $params ) {
449 - $var = \strtolower( \substr( $method, 4 ) );
429 + protected $replies;
450 430
451 - if ( \strncasecmp( $method, 'get', 3 ) === 0 ) {
452 - if ( ! $this->has( $var ) ) {
453 - return new WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) );
454 - }
431 + /**
432 + * A Collection containing objects considered to be likes for
433 + * this object.
434 + *
435 + * @see https://www.w3.org/TR/activitypub/#likes
436 + *
437 + * @var array
438 + */
439 + protected $likes;
455 440
456 - return $this->$var;
457 - }
458 -
459 - if ( \strncasecmp( $method, 'set', 3 ) === 0 ) {
460 - $this->set( $var, $params[0] );
461 - }
462 -
463 - if ( \strncasecmp( $method, 'add', 3 ) === 0 ) {
464 - $this->add( $var, $params[0] );
465 - }
466 - }
467 -
468 441 /**
469 - * Magic function, to transform the object to string.
442 + * A Collection containing objects considered to be shares for
443 + * this object.
470 444 *
471 - * @return string The object id.
445 + * @see https://www.w3.org/TR/activitypub/#shares
446 + *
447 + * @var array
472 448 */
473 - public function __toString() {
474 - return $this->to_string();
475 - }
449 + protected $shares;
476 450
477 451 /**
478 - * Function to transform the object to string.
452 + * Used to mark an object as containing sensitive content.
453 + * Mastodon displays a content warning, requiring users to click
454 + * through to view the content.
479 455 *
480 - * @return string The object id.
456 + * @see https://docs.joinmastodon.org/spec/activitypub/#sensitive
457 + *
458 + * @var boolean
481 459 */
482 - public function to_string() {
483 - return $this->get_id();
484 - }
460 + protected $sensitive;
485 461
486 462 /**
487 463 * Generic getter.
488 464 *
@@ -491,26 +467,15 @@
491 467 * @return mixed The value.
492 468 */
493 469 public function get( $key ) {
494 470 if ( ! $this->has( $key ) ) {
495 - return new WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) );
471 + return new \WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) );
496 472 }
497 473
498 - return call_user_func( array( $this, 'get_' . $key ) );
474 + return parent::get( $key );
499 475 }
500 476
501 477 /**
502 - * Check if the object has a key
503 - *
504 - * @param string $key The key to check.
505 - *
506 - * @return boolean True if the object has the key.
507 - */
508 - public function has( $key ) {
509 - return property_exists( $this, $key );
510 - }
511 -
512 - /**
513 478 * Generic setter.
514 479 *
515 480 * @param string $key The key to set.
516 481 * @param string $value The value to set.
@@ -518,14 +483,12 @@
518 483 * @return mixed The value.
519 484 */
520 485 public function set( $key, $value ) {
521 486 if ( ! $this->has( $key ) ) {
522 - return new WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) );
487 + return new \WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) );
523 488 }
524 489
525 - $this->$key = $value;
526 -
527 - return $this->$key;
490 + return parent::set( $key, $value );
528 491 }
529 492
530 493 /**
531 494 * Generic adder.
@@ -536,143 +499,10 @@
536 499 * @return mixed The value.
537 500 */
538 501 public function add( $key, $value ) {
539 502 if ( ! $this->has( $key ) ) {
540 - return new WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) );
503 + return new \WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) );
541 504 }
542 505
543 - if ( ! isset( $this->$key ) ) {
544 - $this->$key = array();
545 - }
546 -
547 - $attributes = $this->$key;
548 - $attributes[] = $value;
549 -
550 - $this->$key = $attributes;
551 -
552 - return $this->$key;
553 - }
554 -
555 - /**
556 - * Convert JSON input to an array.
557 - *
558 - * @return string The JSON string.
559 - *
560 - * @return \Activitypub\Activity\Base_Object An Object built from the JSON string.
561 - */
562 - public static function init_from_json( $json ) {
563 - $array = \json_decode( $json, true );
564 -
565 - if ( ! is_array( $array ) ) {
566 - $array = array();
567 - }
568 -
569 - return self::init_from_array( $array );
570 - }
571 -
572 - /**
573 - * Convert JSON input to an array.
574 - *
575 - * @return string The object array.
576 - *
577 - * @return \Activitypub\Activity\Base_Object An Object built from the JSON string.
578 - */
579 - public static function init_from_array( $array ) {
580 - if ( ! is_array( $array ) ) {
581 - return new WP_Error( 'invalid_array', __( 'Invalid array', 'activitypub' ), array( 'status' => 404 ) );
582 - }
583 -
584 - $object = new static();
585 -
586 - foreach ( $array as $key => $value ) {
587 - $key = camel_to_snake_case( $key );
588 - $object->set( $key, $value );
589 - }
590 -
591 - return $object;
592 - }
593 -
594 - /**
595 - * Convert JSON input to an array and pre-fill the object.
596 - *
597 - * @param string $json The JSON string.
598 - */
599 - public function from_json( $json ) {
600 - $array = \json_decode( $json, true );
601 -
602 - $this->from_array( $array );
603 - }
604 -
605 - /**
606 - * Convert JSON input to an array and pre-fill the object.
607 - *
608 - * @param array $array The array.
609 - */
610 - public function from_array( $array ) {
611 - foreach ( $array as $key => $value ) {
612 - if ( $value ) {
613 - $key = camel_to_snake_case( $key );
614 - $this->set( $key, $value );
615 - }
616 - }
617 - }
618 -
619 - /**
620 - * Convert Object to an array.
621 - *
622 - * It tries to get the object attributes if they exist
623 - * and falls back to the getters. Empty values are ignored.
624 - *
625 - * @return array An array built from the Object.
626 - */
627 - public function to_array() {
628 - $array = array();
629 - $vars = get_object_vars( $this );
630 -
631 - foreach ( $vars as $key => $value ) {
632 - // ignotre all _prefixed keys.
633 - if ( '_' === substr( $key, 0, 1 ) ) {
634 - continue;
635 - }
636 -
637 - // if value is empty, try to get it from a getter.
638 - if ( ! $value ) {
639 - $value = call_user_func( array( $this, 'get_' . $key ) );
640 - }
641 -
642 - if ( is_object( $value ) ) {
643 - $value = $value->to_array();
644 - }
645 -
646 - // if value is still empty, ignore it for the array and continue.
647 - if ( isset( $value ) ) {
648 - $array[ snake_to_camel_case( $key ) ] = $value;
649 - }
650 - }
651 -
652 - // replace 'context' key with '@context' and move it to the top.
653 - if ( array_key_exists( 'context', $array ) ) {
654 - $context = $array['context'];
655 - unset( $array['context'] );
656 - $array = array_merge( array( '@context' => $context ), $array );
657 - }
658 -
659 - $class = new ReflectionClass( $this );
660 - $class = strtolower( $class->getShortName() );
661 -
662 - $array = \apply_filters( 'activitypub_activity_object_array', $array, $class, $this->id, $this );
663 - $array = \apply_filters( "activitypub_activity_{$class}_object_array", $array, $this->id, $this );
664 -
665 - return $array;
666 - }
667 -
668 - /**
669 - * Convert Object to JSON.
670 - *
671 - * @return string The JSON string.
672 - */
673 - public function to_json() {
674 - $array = $this->to_array();
675 -
676 - return \wp_json_encode( $array, \JSON_HEX_TAG | \JSON_HEX_AMP | \JSON_HEX_QUOT );
506 + return parent::add( $key, $value );
677 507 }
678 508 }