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 +57 -309 2.0.05.3.1 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 *
@@ -24,19 +20,25 @@
24 20 * 'Base_' for this reason.
25 21 *
26 22 * @see https://www.w3.org/TR/activitystreams-core/#object
27 23 */
28 -class Base_Object {
24 +class Base_Object extends Generic_Object {
29 25 /**
30 - * The object's unique global identifier
26 + * The JSON-LD context for the object.
31 27 *
32 - * @see https://www.w3.org/TR/activitypub/#obj-id
33 - *
34 - * @var string
28 + * @var array
35 29 */
36 - protected $id;
30 + const JSON_LD_CONTEXT = array(
31 + 'https://www.w3.org/ns/activitystreams',
32 + array(
33 + 'Hashtag' => 'as:Hashtag',
34 + 'sensitive' => 'as:sensitive',
35 + ),
36 + );
37 37
38 38 /**
39 + * The type of the object.
40 + *
39 41 * @var string
40 42 */
41 43 protected $type = 'Object';
42 44
@@ -47,14 +49,9 @@
47 49 * similar to attachments in email.
48 50 *
49 51 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-attachment
50 52 *
51 - * @var string
52 - * | ObjectType
53 - * | Link
54 - * | array<ObjectType>
55 - * | array<Link>
56 - * | null
53 + * @var string|null
57 54 */
58 55 protected $attachment;
59 56
60 57 /**
@@ -63,14 +60,9 @@
63 60 * object might be attributed to the completion of another activity.
64 61 *
65 62 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-attributedto
66 63 *
67 - * @var string
68 - * | ObjectType
69 - * | Link
70 - * | array<ObjectType>
71 - * | array<Link>
72 - * | null
64 + * @var string|null
73 65 */
74 66 protected $attributed_to;
75 67
76 68 /**
@@ -78,14 +70,9 @@
78 70 * entities for which the object can considered to be relevant.
79 71 *
80 72 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-audience
81 73 *
82 - * @var string
83 - * | ObjectType
84 - * | Link
85 - * | array<ObjectType>
86 - * | array<Link>
87 - * | null
74 + * @var string|null
88 75 */
89 76 protected $audience;
90 77
91 78 /**
@@ -113,12 +100,9 @@
113 100 * project or event.
114 101 *
115 102 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-context
116 103 *
117 - * @var string
118 - * | ObjectType
119 - * | Link
120 - * | null
104 + * @var string|null
121 105 */
122 106 protected $context;
123 107
124 108 /**
@@ -177,14 +161,9 @@
177 161 * at a small size.
178 162 *
179 163 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-icon
180 164 *
181 - * @var string
182 - * | Image
183 - * | Link
184 - * | array<Image>
185 - * | array<Link>
186 - * | null
165 + * @var string|array|null
187 166 */
188 167 protected $icon;
189 168
190 169 /**
@@ -193,14 +172,9 @@
193 172 * or display size limitations assumed.
194 173 *
195 174 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-image-term
196 175 *
197 - * @var string
198 - * | Image
199 - * | Link
200 - * | array<Image>
201 - * | array<Link>
202 - * | null
176 + * @var string|array|null
203 177 */
204 178 protected $image;
205 179
206 180 /**
@@ -208,14 +182,9 @@
208 182 * response.
209 183 *
210 184 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-inreplyto
211 185 *
212 - * @var string
213 - * | ObjectType
214 - * | Link
215 - * | array<ObjectType>
216 - * | array<Link>
217 - * | null
186 + * @var string|null
218 187 */
219 188 protected $in_reply_to;
220 189
221 190 /**
@@ -223,14 +192,9 @@
223 192 * object.
224 193 *
225 194 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-location
226 195 *
227 - * @var string
228 - * | ObjectType
229 - * | Link
230 - * | array<ObjectType>
231 - * | array<Link>
232 - * | null
196 + * @var string|null
233 197 */
234 198 protected $location;
235 199
236 200 /**
@@ -237,12 +201,9 @@
237 201 * An entity that provides a preview of this object.
238 202 *
239 203 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-preview
240 204 *
241 - * @var string
242 - * | ObjectType
243 - * | Link
244 - * | null
205 + * @var string|null
245 206 */
246 207 protected $preview;
247 208
248 209 /**
@@ -272,12 +233,9 @@
272 233 * Multiple language tagged summaries MAY be provided.
273 234 *
274 235 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-summary
275 236 *
276 - * @var string
277 - * | ObjectType
278 - * | Link
279 - * | null
237 + * @var string|null
280 238 */
281 239 protected $summary;
282 240
283 241 /**
@@ -285,9 +243,9 @@
285 243 * values.
286 244 *
287 245 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-summary
288 246 *
289 - * @var array<string>|null
247 + * @var string[]|null
290 248 */
291 249 protected $summary_map;
292 250
293 251 /**
@@ -298,14 +256,9 @@
298 256 * associated by reference.
299 257 *
300 258 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-tag
301 259 *
302 - * @var string
303 - * | ObjectType
304 - * | Link
305 - * | array<ObjectType>
306 - * | array<Link>
307 - * | null
260 + * @var string|null
308 261 */
309 262 protected $tag;
310 263
311 264 /**
@@ -319,13 +272,9 @@
319 272
320 273 /**
321 274 * One or more links to representations of the object.
322 275 *
323 - * @var string
324 - * | array<string>
325 - * | Link
326 - * | array<Link>
327 - * | null
276 + * @var string|null
328 277 */
329 278 protected $url;
330 279
331 280 /**
@@ -333,14 +282,9 @@
333 282 * of an Object
334 283 *
335 284 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-to
336 285 *
337 - * @var string
338 - * | ObjectType
339 - * | Link
340 - * | array<ObjectType>
341 - * | array<Link>
342 - * | null
286 + * @var string|array|null
343 287 */
344 288 protected $to;
345 289
346 290 /**
@@ -348,14 +292,9 @@
348 292 * Object.
349 293 *
350 294 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-bto
351 295 *
352 - * @var string
353 - * | ObjectType
354 - * | Link
355 - * | array<ObjectType>
356 - * | array<Link>
357 - * | null
296 + * @var string|array|null
358 297 */
359 298 protected $bto;
360 299
361 300 /**
@@ -363,14 +302,9 @@
363 302 * Object.
364 303 *
365 304 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-cc
366 305 *
367 - * @var string
368 - * | ObjectType
369 - * | Link
370 - * | array<ObjectType>
371 - * | array<Link>
372 - * | null
306 + * @var string|array|null
373 307 */
374 308 protected $cc;
375 309
376 310 /**
@@ -378,14 +312,9 @@
378 312 * audience of this Object.
379 313 *
380 314 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-bcc
381 315 *
382 - * @var string
383 - * | ObjectType
384 - * | Link
385 - * | array<ObjectType>
386 - * | array<Link>
387 - * | null
316 + * @var string|array|null
388 317 */
389 318 protected $bcc;
390 319
391 320 /**
@@ -419,9 +348,9 @@
419 348 * future editing by clients.
420 349 *
421 350 * @see https://www.w3.org/TR/activitypub/#source-property
422 351 *
423 - * @var ObjectType
352 + * @var array
424 353 */
425 354 protected $source;
426 355
427 356 /**
@@ -429,60 +358,42 @@
429 358 * this object.
430 359 *
431 360 * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-replies
432 361 *
433 - * @var string
434 - * | Collection
435 - * | Link
436 - * | null
362 + * @var string|array|null
437 363 */
438 364 protected $replies;
439 365
440 366 /**
441 - * Magic function to implement getter and setter
367 + * A Collection containing objects considered to be likes for
368 + * this object.
442 369 *
443 - * @param string $method The method name.
444 - * @param string $params The method params.
370 + * @see https://www.w3.org/TR/activitypub/#likes
445 371 *
446 - * @return void
372 + * @var array
447 373 */
448 - public function __call( $method, $params ) {
449 - $var = \strtolower( \substr( $method, 4 ) );
374 + protected $likes;
450 375
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 - }
455 -
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 376 /**
469 - * Magic function, to transform the object to string.
377 + * A Collection containing objects considered to be shares for
378 + * this object.
470 379 *
471 - * @return string The object id.
380 + * @see https://www.w3.org/TR/activitypub/#shares
381 + *
382 + * @var array
472 383 */
473 - public function __toString() {
474 - return $this->to_string();
475 - }
384 + protected $shares;
476 385
477 386 /**
478 - * 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.
479 390 *
480 - * @return string The object id.
391 + * @see https://docs.joinmastodon.org/spec/activitypub/#sensitive
392 + *
393 + * @var boolean
481 394 */
482 - public function to_string() {
483 - return $this->get_id();
484 - }
395 + protected $sensitive;
485 396
486 397 /**
487 398 * Generic getter.
488 399 *
@@ -491,26 +402,15 @@
491 402 * @return mixed The value.
492 403 */
493 404 public function get( $key ) {
494 405 if ( ! $this->has( $key ) ) {
495 - 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 ) );
496 407 }
497 408
498 - return call_user_func( array( $this, 'get_' . $key ) );
409 + return parent::get( $key );
499 410 }
500 411
501 412 /**
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 413 * Generic setter.
514 414 *
515 415 * @param string $key The key to set.
516 416 * @param string $value The value to set.
@@ -518,14 +418,12 @@
518 418 * @return mixed The value.
519 419 */
520 420 public function set( $key, $value ) {
521 421 if ( ! $this->has( $key ) ) {
522 - 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 ) );
523 423 }
524 424
525 - $this->$key = $value;
526 -
527 - return $this->$key;
425 + return parent::set( $key, $value );
528 426 }
529 427
530 428 /**
531 429 * Generic adder.
@@ -536,160 +434,10 @@
536 434 * @return mixed The value.
537 435 */
538 436 public function add( $key, $value ) {
539 437 if ( ! $this->has( $key ) ) {
540 - 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 ) );
541 439 }
542 440
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 - call_user_func( array( $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 - call_user_func( array( $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 - $options = \JSON_HEX_TAG | \JSON_HEX_AMP | \JSON_HEX_QUOT;
676 -
677 - /*
678 - * Options to be passed to json_encode()
679 - *
680 - * @param int $options The current options flags
681 - */
682 - $options = \apply_filters( 'activitypub_json_encode_options', $options );
683 -
684 - return \wp_json_encode( $array, $options );
685 - }
686 -
687 - /**
688 - * Returns the keys of the object vars.
689 - *
690 - * @return array The keys of the object vars.
691 - */
692 - public function get_object_var_keys() {
693 - return \array_keys( \get_object_vars( $this ) );
441 + return parent::add( $key, $value );
694 442 }
695 443 }