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
← All changes | includes/activity/class-base-object.php +54 -325 2.1.05.3.1 View file →
@@ -2,19 +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 -use DateTime;
13 -
14 -use function Activitypub\camel_to_snake_case;
15 -use function Activitypub\snake_to_camel_case;
16 -
17 12 /**
18 13 * Base_Object is an implementation of one of the
19 14 * Activity Streams Core Types.
20 15 *
@@ -25,28 +20,27 @@
25 20 * 'Base_' for this reason.
26 21 *
27 22 * @see https://www.w3.org/TR/activitystreams-core/#object
28 23 */
29 -class Base_Object {
24 +class Base_Object extends Generic_Object {
25 + /**
26 + * The JSON-LD context for the object.
27 + *
28 + * @var array
29 + */
30 30 const JSON_LD_CONTEXT = array(
31 31 'https://www.w3.org/ns/activitystreams',
32 32 array(
33 - 'Hashtag' => 'as:Hashtag',
33 + 'Hashtag' => 'as:Hashtag',
34 + 'sensitive' => 'as:sensitive',
34 35 ),
35 36 );
36 37
37 38 /**
38 - * The object's unique global identifier
39 + * The type of the object.
39 40 *
40 - * @see https://www.w3.org/TR/activitypub/#obj-id
41 - *
42 41 * @var string
43 42 */
44 - protected $id;
45 -
46 - /**
47 - * @var string
48 - */
49 43 protected $type = 'Object';
50 44
51 45 /**
52 46 * A resource attached or related to an object that potentially
@@ -55,14 +49,9 @@
55 49 * similar to attachments in email.
56 50 *
57 51 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-attachment
58 52 *
59 - * @var string
60 - * | ObjectType
61 - * | Link
62 - * | array<ObjectType>
63 - * | array<Link>
64 - * | null
53 + * @var string|null
65 54 */
66 55 protected $attachment;
67 56
68 57 /**
@@ -71,14 +60,9 @@
71 60 * object might be attributed to the completion of another activity.
72 61 *
73 62 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-attributedto
74 63 *
75 - * @var string
76 - * | ObjectType
77 - * | Link
78 - * | array<ObjectType>
79 - * | array<Link>
80 - * | null
64 + * @var string|null
81 65 */
82 66 protected $attributed_to;
83 67
84 68 /**
@@ -86,14 +70,9 @@
86 70 * entities for which the object can considered to be relevant.
87 71 *
88 72 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-audience
89 73 *
90 - * @var string
91 - * | ObjectType
92 - * | Link
93 - * | array<ObjectType>
94 - * | array<Link>
95 - * | null
74 + * @var string|null
96 75 */
97 76 protected $audience;
98 77
99 78 /**
@@ -121,12 +100,9 @@
121 100 * project or event.
122 101 *
123 102 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-context
124 103 *
125 - * @var string
126 - * | ObjectType
127 - * | Link
128 - * | null
104 + * @var string|null
129 105 */
130 106 protected $context;
131 107
132 108 /**
@@ -185,14 +161,9 @@
185 161 * at a small size.
186 162 *
187 163 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-icon
188 164 *
189 - * @var string
190 - * | Image
191 - * | Link
192 - * | array<Image>
193 - * | array<Link>
194 - * | null
165 + * @var string|array|null
195 166 */
196 167 protected $icon;
197 168
198 169 /**
@@ -201,14 +172,9 @@
201 172 * or display size limitations assumed.
202 173 *
203 174 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-image-term
204 175 *
205 - * @var string
206 - * | Image
207 - * | Link
208 - * | array<Image>
209 - * | array<Link>
210 - * | null
176 + * @var string|array|null
211 177 */
212 178 protected $image;
213 179
214 180 /**
@@ -216,14 +182,9 @@
216 182 * response.
217 183 *
218 184 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-inreplyto
219 185 *
220 - * @var string
221 - * | ObjectType
222 - * | Link
223 - * | array<ObjectType>
224 - * | array<Link>
225 - * | null
186 + * @var string|null
226 187 */
227 188 protected $in_reply_to;
228 189
229 190 /**
@@ -231,14 +192,9 @@
231 192 * object.
232 193 *
233 194 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-location
234 195 *
235 - * @var string
236 - * | ObjectType
237 - * | Link
238 - * | array<ObjectType>
239 - * | array<Link>
240 - * | null
196 + * @var string|null
241 197 */
242 198 protected $location;
243 199
244 200 /**
@@ -245,12 +201,9 @@
245 201 * An entity that provides a preview of this object.
246 202 *
247 203 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-preview
248 204 *
249 - * @var string
250 - * | ObjectType
251 - * | Link
252 - * | null
205 + * @var string|null
253 206 */
254 207 protected $preview;
255 208
256 209 /**
@@ -280,12 +233,9 @@
280 233 * Multiple language tagged summaries MAY be provided.
281 234 *
282 235 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-summary
283 236 *
284 - * @var string
285 - * | ObjectType
286 - * | Link
287 - * | null
237 + * @var string|null
288 238 */
289 239 protected $summary;
290 240
291 241 /**
@@ -293,9 +243,9 @@
293 243 * values.
294 244 *
295 245 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-summary
296 246 *
297 - * @var array<string>|null
247 + * @var string[]|null
298 248 */
299 249 protected $summary_map;
300 250
301 251 /**
@@ -306,14 +256,9 @@
306 256 * associated by reference.
307 257 *
308 258 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-tag
309 259 *
310 - * @var string
311 - * | ObjectType
312 - * | Link
313 - * | array<ObjectType>
314 - * | array<Link>
315 - * | null
260 + * @var string|null
316 261 */
317 262 protected $tag;
318 263
319 264 /**
@@ -327,13 +272,9 @@
327 272
328 273 /**
329 274 * One or more links to representations of the object.
330 275 *
331 - * @var string
332 - * | array<string>
333 - * | Link
334 - * | array<Link>
335 - * | null
276 + * @var string|null
336 277 */
337 278 protected $url;
338 279
339 280 /**
@@ -341,14 +282,9 @@
341 282 * of an Object
342 283 *
343 284 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-to
344 285 *
345 - * @var string
346 - * | ObjectType
347 - * | Link
348 - * | array<ObjectType>
349 - * | array<Link>
350 - * | null
286 + * @var string|array|null
351 287 */
352 288 protected $to;
353 289
354 290 /**
@@ -356,14 +292,9 @@
356 292 * Object.
357 293 *
358 294 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-bto
359 295 *
360 - * @var string
361 - * | ObjectType
362 - * | Link
363 - * | array<ObjectType>
364 - * | array<Link>
365 - * | null
296 + * @var string|array|null
366 297 */
367 298 protected $bto;
368 299
369 300 /**
@@ -371,14 +302,9 @@
371 302 * Object.
372 303 *
373 304 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-cc
374 305 *
375 - * @var string
376 - * | ObjectType
377 - * | Link
378 - * | array<ObjectType>
379 - * | array<Link>
380 - * | null
306 + * @var string|array|null
381 307 */
382 308 protected $cc;
383 309
384 310 /**
@@ -386,14 +312,9 @@
386 312 * audience of this Object.
387 313 *
388 314 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-bcc
389 315 *
390 - * @var string
391 - * | ObjectType
392 - * | Link
393 - * | array<ObjectType>
394 - * | array<Link>
395 - * | null
316 + * @var string|array|null
396 317 */
397 318 protected $bcc;
398 319
399 320 /**
@@ -427,9 +348,9 @@
427 348 * future editing by clients.
428 349 *
429 350 * @see https://www.w3.org/TR/activitypub/#source-property
430 351 *
431 - * @var ObjectType
352 + * @var array
432 353 */
433 354 protected $source;
434 355
435 356 /**
@@ -437,60 +358,42 @@
437 358 * this object.
438 359 *
439 360 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-replies
440 361 *
441 - * @var string
442 - * | Collection
443 - * | Link
444 - * | null
362 + * @var string|array|null
445 363 */
446 364 protected $replies;
447 365
448 366 /**
449 - * Magic function to implement getter and setter
367 + * A Collection containing objects considered to be likes for
368 + * this object.
450 369 *
451 - * @param string $method The method name.
452 - * @param string $params The method params.
370 + * @see https://www.w3.org/TR/activitypub/#likes
453 371 *
454 - * @return void
372 + * @var array
455 373 */
456 - public function __call( $method, $params ) {
457 - $var = \strtolower( \substr( $method, 4 ) );
374 + protected $likes;
458 375
459 - if ( \strncasecmp( $method, 'get', 3 ) === 0 ) {
460 - if ( ! $this->has( $var ) ) {
461 - return new WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) );
462 - }
463 -
464 - return $this->$var;
465 - }
466 -
467 - if ( \strncasecmp( $method, 'set', 3 ) === 0 ) {
468 - return $this->set( $var, $params[0] );
469 - }
470 -
471 - if ( \strncasecmp( $method, 'add', 3 ) === 0 ) {
472 - $this->add( $var, $params[0] );
473 - }
474 - }
475 -
476 376 /**
477 - * Magic function, to transform the object to string.
377 + * A Collection containing objects considered to be shares for
378 + * this object.
478 379 *
479 - * @return string The object id.
380 + * @see https://www.w3.org/TR/activitypub/#shares
381 + *
382 + * @var array
480 383 */
481 - public function __toString() {
482 - return $this->to_string();
483 - }
384 + protected $shares;
484 385
485 386 /**
486 - * Function to transform the object to string.
387 + * Used to mark an object as containing sensitive content.
388 + * Mastodon displays a content warning, requiring users to click
389 + * through to view the content.
487 390 *
488 - * @return string The object id.
391 + * @see https://docs.joinmastodon.org/spec/activitypub/#sensitive
392 + *
393 + * @var boolean
489 394 */
490 - public function to_string() {
491 - return $this->get_id();
492 - }
395 + protected $sensitive;
493 396
494 397 /**
495 398 * Generic getter.
496 399 *
@@ -499,26 +402,15 @@
499 402 * @return mixed The value.
500 403 */
501 404 public function get( $key ) {
502 405 if ( ! $this->has( $key ) ) {
503 - return new WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) );
406 + return new \WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) );
504 407 }
505 408
506 - return call_user_func( array( $this, 'get_' . $key ) );
409 + return parent::get( $key );
507 410 }
508 411
509 412 /**
510 - * Check if the object has a key
511 - *
512 - * @param string $key The key to check.
513 - *
514 - * @return boolean True if the object has the key.
515 - */
516 - public function has( $key ) {
517 - return property_exists( $this, $key );
518 - }
519 -
520 - /**
521 413 * Generic setter.
522 414 *
523 415 * @param string $key The key to set.
524 416 * @param string $value The value to set.
@@ -526,14 +418,12 @@
526 418 * @return mixed The value.
527 419 */
528 420 public function set( $key, $value ) {
529 421 if ( ! $this->has( $key ) ) {
530 - return new WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) );
422 + return new \WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) );
531 423 }
532 424
533 - $this->$key = $value;
534 -
535 - return $this;
425 + return parent::set( $key, $value );
536 426 }
537 427
538 428 /**
539 429 * Generic adder.
@@ -544,171 +434,10 @@
544 434 * @return mixed The value.
545 435 */
546 436 public function add( $key, $value ) {
547 437 if ( ! $this->has( $key ) ) {
548 - return new WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) );
438 + return new \WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) );
549 439 }
550 440
551 - if ( ! isset( $this->$key ) ) {
552 - $this->$key = array();
553 - }
554 -
555 - $attributes = $this->$key;
556 - $attributes[] = $value;
557 -
558 - $this->$key = $attributes;
559 -
560 - return $this->$key;
561 - }
562 -
563 - /**
564 - * Convert JSON input to an array.
565 - *
566 - * @return string The JSON string.
567 - *
568 - * @return \Activitypub\Activity\Base_Object An Object built from the JSON string.
569 - */
570 - public static function init_from_json( $json ) {
571 - $array = \json_decode( $json, true );
572 -
573 - if ( ! is_array( $array ) ) {
574 - $array = array();
575 - }
576 -
577 - return self::init_from_array( $array );
578 - }
579 -
580 - /**
581 - * Convert JSON input to an array.
582 - *
583 - * @return string The object array.
584 - *
585 - * @return \Activitypub\Activity\Base_Object An Object built from the JSON string.
586 - */
587 - public static function init_from_array( $array ) {
588 - if ( ! is_array( $array ) ) {
589 - return new WP_Error( 'invalid_array', __( 'Invalid array', 'activitypub' ), array( 'status' => 404 ) );
590 - }
591 -
592 - $object = new static();
593 -
594 - foreach ( $array as $key => $value ) {
595 - $key = camel_to_snake_case( $key );
596 - call_user_func( array( $object, 'set_' . $key ), $value );
597 - }
598 -
599 - return $object;
600 - }
601 -
602 - /**
603 - * Convert JSON input to an array and pre-fill the object.
604 - *
605 - * @param string $json The JSON string.
606 - */
607 - public function from_json( $json ) {
608 - $array = \json_decode( $json, true );
609 -
610 - $this->from_array( $array );
611 - }
612 -
613 - /**
614 - * Convert JSON input to an array and pre-fill the object.
615 - *
616 - * @param array $array The array.
617 - */
618 - public function from_array( $array ) {
619 - foreach ( $array as $key => $value ) {
620 - if ( $value ) {
621 - $key = camel_to_snake_case( $key );
622 - call_user_func( array( $this, 'set_' . $key ), $value );
623 - }
624 - }
625 - }
626 -
627 - /**
628 - * Convert Object to an array.
629 - *
630 - * It tries to get the object attributes if they exist
631 - * and falls back to the getters. Empty values are ignored.
632 - *
633 - * @param bool $include_json_ld_context Whether to include the JSON-LD context. Default true.
634 - *
635 - * @return array An array built from the Object.
636 - */
637 - public function to_array( $include_json_ld_context = true ) {
638 - $array = array();
639 - $vars = get_object_vars( $this );
640 -
641 - foreach ( $vars as $key => $value ) {
642 - // ignotre all _prefixed keys.
643 - if ( '_' === substr( $key, 0, 1 ) ) {
644 - continue;
645 - }
646 -
647 - // if value is empty, try to get it from a getter.
648 - if ( ! $value ) {
649 - $value = call_user_func( array( $this, 'get_' . $key ) );
650 - }
651 -
652 - if ( is_object( $value ) ) {
653 - $value = $value->to_array( false );
654 - }
655 -
656 - // if value is still empty, ignore it for the array and continue.
657 - if ( isset( $value ) ) {
658 - $array[ snake_to_camel_case( $key ) ] = $value;
659 - }
660 - }
661 -
662 - if ( $include_json_ld_context ) {
663 - // Get JsonLD context and move it to '@context' at the top.
664 - $array = array_merge( array( '@context' => $this->get_json_ld_context() ), $array );
665 - }
666 -
667 - $class = new ReflectionClass( $this );
668 - $class = strtolower( $class->getShortName() );
669 -
670 - $array = \apply_filters( 'activitypub_activity_object_array', $array, $class, $this->id, $this );
671 - $array = \apply_filters( "activitypub_activity_{$class}_object_array", $array, $this->id, $this );
672 -
673 - return $array;
674 - }
675 -
676 - /**
677 - * Convert Object to JSON.
678 - *
679 - * @param bool $include_json_ld_context Whether to include the JSON-LD context. Default true.
680 - *
681 - * @return string The JSON string.
682 - */
683 - public function to_json( $include_json_ld_context = true ) {
684 - $array = $this->to_array( $include_json_ld_context );
685 - $options = \JSON_HEX_TAG | \JSON_HEX_AMP | \JSON_HEX_QUOT;
686 -
687 - /*
688 - * Options to be passed to json_encode()
689 - *
690 - * @param int $options The current options flags
691 - */
692 - $options = \apply_filters( 'activitypub_json_encode_options', $options );
693 -
694 - return \wp_json_encode( $array, $options );
695 - }
696 -
697 - /**
698 - * Returns the keys of the object vars.
699 - *
700 - * @return array The keys of the object vars.
701 - */
702 - public function get_object_var_keys() {
703 - return \array_keys( \get_object_vars( $this ) );
704 - }
705 -
706 - /**
707 - * Returns the JSON-LD context of this object.
708 - *
709 - * @return array $context A compacted JSON-LD context for the ActivityPub object.
710 - */
711 - public function get_json_ld_context() {
712 - return static::JSON_LD_CONTEXT;
441 + return parent::add( $key, $value );
713 442 }
714 443 }