| @@ -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 |
| @@ -254,21 +262,8 @@ | ||
| 254 | 262 | */ |
| 255 | 263 | protected $published; |
| 256 | 264 | |
| 257 | 265 | /** |
| 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 | 266 | * The date and time describing the actual or expected starting time |
| 272 | 267 | * of the object. |
| 273 | 268 | * When used with an Activity object, for instance, the startTime |
| 274 | 269 | * property specifies the moment the activity began |
| @@ -437,8 +432,21 @@ | ||
| 437 | 432 | */ |
| 438 | 433 | protected $source; |
| 439 | 434 | |
| 440 | 435 | /** |
| 436 | + * A Collection containing objects considered to be responses to | |
| 437 | + * this object. | |
| 438 | + * | |
| 439 | + * @see https://www.w3.org/TR/activitystreams-vocabulary/#dfn-replies | |
| 440 | + * | |
| 441 | + * @var string | |
| 442 | + * | Collection | |
| 443 | + * | Link | |
| 444 | + * | null | |
| 445 | + */ | |
| 446 | + protected $replies; | |
| 447 | + | |
| 448 | + /** | |
| 441 | 449 | * Magic function to implement getter and setter |
| 442 | 450 | * |
| 443 | 451 | * @param string $method The method name. |
| 444 | 452 | * @param string $params The method params. |
| @@ -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,40 @@ | ||
| 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 ); | |
| 685 | + $options = \JSON_HEX_TAG | \JSON_HEX_AMP | \JSON_HEX_QUOT; | |
| 675 | 686 | |
| 676 | - return \wp_json_encode( $array, \JSON_HEX_TAG | \JSON_HEX_AMP | \JSON_HEX_QUOT ); | |
| 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; | |
| 677 | 713 | } |
| 678 | 714 | } |