Skip to content

Commit 7cacd89

Browse files
Menrathpfefferleobenland
authored
Fix outbox not using object types other than Base_Object (#1268)
* Fix outbox not using object types other than Base_Object * Update Following endpoint to extend WP_REST_Controller (#1267) * Move file * Update following endpoint to extend WP_REST_Controller * Inherit from Actor --------- Co-authored-by: Matthias Pfefferle <[email protected]> * Change usage of fediverse symbol (#1239) * Move "⁂" to the end of a line otherwise the links look a bit disorganized * this one mimics the logo * Update Followers endpoint to extend WP_REST_Controller (#1266) * Move file * Update followers endpoint to extend WP_REST_Controlelr * Inherit from Actors_Controller * update schema * Remove debug * Adopt pagination from Outbox Controller Props @pfefferle * Prevent requests that go beyond what's available Props @pfefferle --------- Co-authored-by: Matthias Pfefferle <[email protected]> * Add: Show ActivityPub preview in row actions, when block editor enabled but is not used for post type. (#1273) * Fix outbox not using object types other than Base_Object * Show ActivityPub preview in row actions, when block editor is not used for post type. * Revert "Fix outbox not using object types other than Base_Object" This reverts commit bc5f587. * Improve changelog * ActivityPub Query: Improve third party plugin support (#1272) * check `$activitypub_object` before returning it * make queried object filterable * add phpdoc * added changelog * Fix phpcs issues * allow access to `author` --------- Co-authored-by: Konstantin Obenland <[email protected]> * Add assertions for php unit test of Oubox:add * Add context to test object * Fix test data * Use assertJsonStringEqualsJsonString for comparing two JSON strings. * Fix NodeInfo rel (#1275) * Fix NodeInfo rel * fix tests * Release 5.1.0 (#1276) * No need to set default object class in post meta. * Fix Changelog. * Use an Arbitrary_Object as default * remove post-meta * remove whitespace * Fix PHPCS * rename to Generic Object props @obenland * global namespace * fix some of the tests * Fix tests * Fix phpcs --------- Co-authored-by: Matthias Pfefferle <[email protected]> Co-authored-by: Konstantin Obenland <[email protected]>
1 parent 8c8dd72 commit 7cacd89

File tree

12 files changed

+557
-298
lines changed

12 files changed

+557
-298
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
2626
* Outbox items older than 6 months will be purged to avoid performance issues.
2727
* REST API endpoints for likes and shares.
2828

29+
### Fixed
30+
31+
* Fixed an issue where the outbox could not send object types other than `Base_Object` (introduced in 5.0.0).
32+
2933
### Changed
3034

3135
* Increased probability of Outbox items being processed with the correct author.

includes/activity/class-activity.php

Lines changed: 1 addition & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -165,14 +165,7 @@ class Activity extends Base_Object {
165165
public function set_object( $data ) {
166166
// Convert array to object.
167167
if ( is_array( $data ) ) {
168-
// Check if the item is an Activity or an Object.
169-
if ( is_activity( $data ) ) {
170-
$data = self::init_from_array( $data );
171-
} elseif ( is_actor( $data ) ) {
172-
$data = Actor::init_from_array( $data );
173-
} else {
174-
$data = Base_Object::init_from_array( $data );
175-
}
168+
$data = Generic_Object::init_from_array( $data );
176169
}
177170

178171
// Set object.

includes/activity/class-actor.php

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -176,17 +176,6 @@ class Actor extends Base_Object {
176176
*/
177177
protected $manually_approves_followers = false;
178178

179-
/**
180-
* Used to mark an object as containing sensitive content.
181-
* Mastodon displays a content warning, requiring users to click
182-
* through to view the content.
183-
*
184-
* @see https://docs.joinmastodon.org/spec/activitypub/#sensitive
185-
*
186-
* @var boolean
187-
*/
188-
protected $sensitive = null;
189-
190179
/**
191180
* Domains allowed to use `fediverse:creator` for this actor in
192181
* published articles.

includes/activity/class-base-object.php

Lines changed: 13 additions & 269 deletions
Original file line numberDiff line numberDiff line change
@@ -9,13 +9,6 @@
99

1010
namespace Activitypub\Activity;
1111

12-
use WP_Error;
13-
use ReflectionClass;
14-
use DateTime;
15-
16-
use function Activitypub\camel_to_snake_case;
17-
use function Activitypub\snake_to_camel_case;
18-
1912
/**
2013
* Base_Object is an implementation of one of the
2114
* Activity Streams Core Types.
@@ -28,7 +21,12 @@
2821
*
2922
* @see https://www.w3.org/TR/activitystreams-core/#object
3023
*/
31-
class Base_Object {
24+
class Base_Object extends Generic_Object {
25+
/**
26+
* The JSON-LD context for the object.
27+
*
28+
* @var array
29+
*/
3230
const JSON_LD_CONTEXT = array(
3331
'https://www.w3.org/ns/activitystreams',
3432
array(
@@ -37,15 +35,6 @@ class Base_Object {
3735
),
3836
);
3937

40-
/**
41-
* The object's unique global identifier
42-
*
43-
* @see https://www.w3.org/TR/activitypub/#obj-id
44-
*
45-
* @var string
46-
*/
47-
protected $id;
48-
4938
/**
5039
* The type of the object.
5140
*
@@ -403,51 +392,7 @@ class Base_Object {
403392
*
404393
* @var boolean
405394
*/
406-
protected $sensitive = false;
407-
408-
/**
409-
* Magic function to implement getter and setter.
410-
*
411-
* @param string $method The method name.
412-
* @param string $params The method params.
413-
*/
414-
public function __call( $method, $params ) {
415-
$var = \strtolower( \substr( $method, 4 ) );
416-
417-
if ( \strncasecmp( $method, 'get', 3 ) === 0 ) {
418-
if ( ! $this->has( $var ) ) {
419-
return new WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) );
420-
}
421-
422-
return $this->$var;
423-
}
424-
425-
if ( \strncasecmp( $method, 'set', 3 ) === 0 ) {
426-
return $this->set( $var, $params[0] );
427-
}
428-
429-
if ( \strncasecmp( $method, 'add', 3 ) === 0 ) {
430-
return $this->add( $var, $params[0] );
431-
}
432-
}
433-
434-
/**
435-
* Magic function, to transform the object to string.
436-
*
437-
* @return string The object id.
438-
*/
439-
public function __toString() {
440-
return $this->to_string();
441-
}
442-
443-
/**
444-
* Function to transform the object to string.
445-
*
446-
* @return string The object id.
447-
*/
448-
public function to_string() {
449-
return $this->get_id();
450-
}
395+
protected $sensitive;
451396

452397
/**
453398
* Generic getter.
@@ -458,21 +403,10 @@ public function to_string() {
458403
*/
459404
public function get( $key ) {
460405
if ( ! $this->has( $key ) ) {
461-
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 ) );
462407
}
463408

464-
return call_user_func( array( $this, 'get_' . $key ) );
465-
}
466-
467-
/**
468-
* Check if the object has a key
469-
*
470-
* @param string $key The key to check.
471-
*
472-
* @return boolean True if the object has the key.
473-
*/
474-
public function has( $key ) {
475-
return property_exists( $this, $key );
409+
return parent::get( $key );
476410
}
477411

478412
/**
@@ -485,12 +419,10 @@ public function has( $key ) {
485419
*/
486420
public function set( $key, $value ) {
487421
if ( ! $this->has( $key ) ) {
488-
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 ) );
489423
}
490424

491-
$this->$key = $value;
492-
493-
return $this;
425+
return parent::set( $key, $value );
494426
}
495427

496428
/**
@@ -503,197 +435,9 @@ public function set( $key, $value ) {
503435
*/
504436
public function add( $key, $value ) {
505437
if ( ! $this->has( $key ) ) {
506-
return new WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) );
507-
}
508-
509-
if ( ! isset( $this->$key ) ) {
510-
$this->$key = array();
438+
return new \WP_Error( 'invalid_key', __( 'Invalid key', 'activitypub' ), array( 'status' => 404 ) );
511439
}
512440

513-
if ( is_string( $this->$key ) ) {
514-
$this->$key = array( $this->$key );
515-
}
516-
517-
$attributes = $this->$key;
518-
519-
if ( is_array( $value ) ) {
520-
$attributes = array_merge( $attributes, $value );
521-
} else {
522-
$attributes[] = $value;
523-
}
524-
525-
$this->$key = array_unique( $attributes );
526-
527-
return $this->$key;
528-
}
529-
530-
/**
531-
* Convert JSON input to an array.
532-
*
533-
* @param string $json The JSON string.
534-
*
535-
* @return Base_Object|WP_Error An Object built from the JSON string or WP_Error when it's not a JSON string.
536-
*/
537-
public static function init_from_json( $json ) {
538-
$array = \json_decode( $json, true );
539-
540-
if ( ! is_array( $array ) ) {
541-
return new WP_Error( 'invalid_json', __( 'Invalid JSON', 'activitypub' ), array( 'status' => 400 ) );
542-
}
543-
544-
return self::init_from_array( $array );
545-
}
546-
547-
/**
548-
* Convert input array to a Base_Object.
549-
*
550-
* @param array $data The object array.
551-
*
552-
* @return Base_Object|WP_Error An Object built from the input array or WP_Error when it's not an array.
553-
*/
554-
public static function init_from_array( $data ) {
555-
if ( ! is_array( $data ) ) {
556-
return new WP_Error( 'invalid_array', __( 'Invalid array', 'activitypub' ), array( 'status' => 400 ) );
557-
}
558-
559-
$object = new static();
560-
$object->from_array( $data );
561-
562-
return $object;
563-
}
564-
565-
/**
566-
* Convert JSON input to an array and pre-fill the object.
567-
*
568-
* @param string $json The JSON string.
569-
*/
570-
public function from_json( $json ) {
571-
$array = \json_decode( $json, true );
572-
573-
$this->from_array( $array );
574-
}
575-
576-
/**
577-
* Convert JSON input to an array and pre-fill the object.
578-
*
579-
* @param array $data The array.
580-
*/
581-
public function from_array( $data ) {
582-
foreach ( $data as $key => $value ) {
583-
if ( $value ) {
584-
$key = camel_to_snake_case( $key );
585-
call_user_func( array( $this, 'set_' . $key ), $value );
586-
}
587-
}
588-
}
589-
590-
/**
591-
* Convert Object to an array.
592-
*
593-
* It tries to get the object attributes if they exist
594-
* and falls back to the getters. Empty values are ignored.
595-
*
596-
* @param bool $include_json_ld_context Whether to include the JSON-LD context. Default true.
597-
*
598-
* @return array An array built from the Object.
599-
*/
600-
public function to_array( $include_json_ld_context = true ) {
601-
$array = array();
602-
$vars = get_object_vars( $this );
603-
604-
foreach ( $vars as $key => $value ) {
605-
if ( \is_wp_error( $value ) ) {
606-
continue;
607-
}
608-
609-
// Ignore all _prefixed keys.
610-
if ( '_' === substr( $key, 0, 1 ) ) {
611-
continue;
612-
}
613-
614-
// If value is empty, try to get it from a getter.
615-
if ( ! $value ) {
616-
$value = call_user_func( array( $this, 'get_' . $key ) );
617-
}
618-
619-
if ( is_object( $value ) ) {
620-
$value = $value->to_array( false );
621-
}
622-
623-
// If value is still empty, ignore it for the array and continue.
624-
if ( ! empty( $value ) || false === $value ) {
625-
$array[ snake_to_camel_case( $key ) ] = $value;
626-
}
627-
}
628-
629-
if ( $include_json_ld_context ) {
630-
// Get JsonLD context and move it to '@context' at the top.
631-
$array = array_merge( array( '@context' => $this->get_json_ld_context() ), $array );
632-
}
633-
634-
$class = new ReflectionClass( $this );
635-
$class = strtolower( $class->getShortName() );
636-
637-
/**
638-
* Filter the array of the ActivityPub object.
639-
*
640-
* @param array $array The array of the ActivityPub object.
641-
* @param string $class The class of the ActivityPub object.
642-
* @param int $id The ID of the ActivityPub object.
643-
* @param Base_Object $object The ActivityPub object.
644-
*
645-
* @return array The filtered array of the ActivityPub object.
646-
*/
647-
$array = \apply_filters( 'activitypub_activity_object_array', $array, $class, $this->id, $this );
648-
649-
/**
650-
* Filter the array of the ActivityPub object by class.
651-
*
652-
* @param array $array The array of the ActivityPub object.
653-
* @param int $id The ID of the ActivityPub object.
654-
* @param Base_Object $object The ActivityPub object.
655-
*
656-
* @return array The filtered array of the ActivityPub object.
657-
*/
658-
return \apply_filters( "activitypub_activity_{$class}_object_array", $array, $this->id, $this );
659-
}
660-
661-
/**
662-
* Convert Object to JSON.
663-
*
664-
* @param bool $include_json_ld_context Whether to include the JSON-LD context. Default true.
665-
*
666-
* @return string The JSON string.
667-
*/
668-
public function to_json( $include_json_ld_context = true ) {
669-
$array = $this->to_array( $include_json_ld_context );
670-
$options = \JSON_HEX_TAG | \JSON_HEX_AMP | \JSON_HEX_QUOT;
671-
672-
/**
673-
* Options to be passed to json_encode()
674-
*
675-
* @param int $options The current options flags.
676-
*/
677-
$options = \apply_filters( 'activitypub_json_encode_options', $options );
678-
679-
return \wp_json_encode( $array, $options );
680-
}
681-
682-
/**
683-
* Returns the keys of the object vars.
684-
*
685-
* @return array The keys of the object vars.
686-
*/
687-
public function get_object_var_keys() {
688-
return \array_keys( \get_object_vars( $this ) );
689-
}
690-
691-
/**
692-
* Returns the JSON-LD context of this object.
693-
*
694-
* @return array $context A compacted JSON-LD context for the ActivityPub object.
695-
*/
696-
public function get_json_ld_context() {
697-
return static::JSON_LD_CONTEXT;
441+
return parent::add( $key, $value );
698442
}
699443
}

0 commit comments

Comments
 (0)