PluginProbe
ActivityPub / 3.2.2
ActivityPub v3.2.2
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 +30 -11 2.0.0 → 3.2.2 View file →
@@ -8,8 +8,9 @@
8 8 namespace Activitypub\Activity;
9 9
10 10 use WP_Error;
11 11 use ReflectionClass;
12 +use DateTime;
12 13
13 14 use function Activitypub\camel_to_snake_case;
14 15 use function Activitypub\snake_to_camel_case;
15 16
@@ -25,8 +26,15 @@
25 26 *
26 27 * @see https://www.w3.org/TR/activitystreams-core/#object
27 28 */
28 29 class Base_Object {
30 + const JSON_LD_CONTEXT = array(
31 + 'https://www.w3.org/ns/activitystreams',
32 + array(
33 + 'Hashtag' => 'as:Hashtag',
34 + ),
35 + );
36 +
29 37 /**
30 38 * The object's unique global identifier
31 39 *
32 40 * @see https://www.w3.org/TR/activitypub/#obj-id
@@ -456,9 +464,9 @@
456 464 return $this->$var;
457 465 }
458 466
459 467 if ( \strncasecmp( $method, 'set', 3 ) === 0 ) {
460 - $this->set( $var, $params[0] );
468 + return $this->set( $var, $params[0] );
461 469 }
462 470
463 471 if ( \strncasecmp( $method, 'add', 3 ) === 0 ) {
464 472 $this->add( $var, $params[0] );
@@ -523,9 +531,9 @@
523 531 }
524 532
525 533 $this->$key = $value;
526 534
527 - return $this->$key;
535 + return $this;
528 536 }
529 537
530 538 /**
531 539 * Generic adder.
@@ -621,11 +629,13 @@
621 629 *
622 630 * It tries to get the object attributes if they exist
623 631 * and falls back to the getters. Empty values are ignored.
624 632 *
633 + * @param bool $include_json_ld_context Whether to include the JSON-LD context. Default true.
634 + *
625 635 * @return array An array built from the Object.
626 636 */
627 - public function to_array() {
637 + public function to_array( $include_json_ld_context = true ) {
628 638 $array = array();
629 639 $vars = get_object_vars( $this );
630 640
631 641 foreach ( $vars as $key => $value ) {
@@ -639,9 +649,9 @@
639 649 $value = call_user_func( array( $this, 'get_' . $key ) );
640 650 }
641 651
642 652 if ( is_object( $value ) ) {
643 - $value = $value->to_array();
653 + $value = $value->to_array( false );
644 654 }
645 655
646 656 // if value is still empty, ignore it for the array and continue.
647 657 if ( isset( $value ) ) {
@@ -648,13 +658,11 @@
648 658 $array[ snake_to_camel_case( $key ) ] = $value;
649 659 }
650 660 }
651 661
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 );
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 );
657 665 }
658 666
659 667 $class = new ReflectionClass( $this );
660 668 $class = strtolower( $class->getShortName() );
@@ -667,12 +675,14 @@
667 675
668 676 /**
669 677 * Convert Object to JSON.
670 678 *
679 + * @param bool $include_json_ld_context Whether to include the JSON-LD context. Default true.
680 + *
671 681 * @return string The JSON string.
672 682 */
673 - public function to_json() {
674 - $array = $this->to_array();
683 + public function to_json( $include_json_ld_context = true ) {
684 + $array = $this->to_array( $include_json_ld_context );
675 685 $options = \JSON_HEX_TAG | \JSON_HEX_AMP | \JSON_HEX_QUOT;
676 686
677 687 /*
678 688 * Options to be passed to json_encode()
@@ -690,6 +700,15 @@
690 700 * @return array The keys of the object vars.
691 701 */
692 702 public function get_object_var_keys() {
693 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;
694 713 }
695 714 }