Skip to content

Commit

Permalink
Fix outbox not using object types other than Base_Object
Browse files Browse the repository at this point in the history
  • Loading branch information
Menrath committed Feb 4, 2025
1 parent 885b228 commit c363eec
Show file tree
Hide file tree
Showing 6 changed files with 41 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
* Negotiation of ActivityPub requests for custom post types when queried by the ActivityPub ID.
* Avoid PHP warnings when using Debug mode and when the `actor` is not set.
* No longer creates Outbox items when importing content/users.
* Outbox is now capable of sending other object types that are not Base_Object.

## [5.0.0] - 2025-02-03

Expand Down
8 changes: 5 additions & 3 deletions includes/activity/class-activity.php
Original file line number Diff line number Diff line change
Expand Up @@ -151,14 +151,16 @@ class Activity extends Base_Object {
* @see https://www.w3.org/TR/activitypub/#object-without-create
*
* @param array|string|Base_Object|Link|null $data Activity object.
* @param string|null $object_class The full class path to a child of Base_Object.
*
* @return void
*/
public function set_object( $data ) {
public function set_object( $data, $object_class = null ) {
// Convert array to object.
if ( is_array( $data ) ) {
// Check if the item is an Activity or an Object.
if ( is_activity( $data ) ) {
if ( $object_class && class_exists( $object_class ) ) {
$data = $object_class::init_from_array( $data );
} elseif ( is_activity( $data ) ) { // Check if the item is an Activity or an Object.
$data = self::init_from_array( $data );
} elseif ( is_actor( $data ) ) {
$data = Actor::init_from_array( $data );
Expand Down
22 changes: 22 additions & 0 deletions includes/class-activitypub.php
Original file line number Diff line number Diff line change
Expand Up @@ -594,6 +594,28 @@ private static function register_post_types() {
)
);

/**
* Register ActivityPub object class for Outbox items.
*
* @see https://www.w3.org/TR/activitystreams-vocabulary/#object-types
*/
\register_post_meta(
Outbox::POST_TYPE,
'_activitypub_object_class',
array(
'type' => 'string',
'description' => 'The full class name of the object class',
'single' => true,
'sanitize_callback' => function ( $value ) {
if ( class_exists( $value ) ) {
return $value;
}

return \Activitypub\Activity\Base_Object::class;
},
)
);

\register_post_meta(
Outbox::POST_TYPE,
'_activitypub_activity_actor',
Expand Down
7 changes: 4 additions & 3 deletions includes/class-dispatcher.php
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ public static function process_outbox( $id ) {
break;
}

$type = \get_post_meta( $outbox_item->ID, '_activitypub_activity_type', true );
$activity = new Activity();
$type = \get_post_meta( $outbox_item->ID, '_activitypub_activity_type', true );
$object_class = \get_post_meta( $outbox_item->ID, '_activitypub_object_class', true );
$activity = new Activity();
$activity->set_type( $type );
$activity->set_id( $outbox_item->guid );
// Pre-fill the Activity with data (for example cc and to).
$activity->set_object( \json_decode( $outbox_item->post_content, true ) );
$activity->set_object( \json_decode( $outbox_item->post_content, true ), $object_class );
$activity->set_actor( Actors::get_by_id( $outbox_item->post_author )->get_id() );

// Use simple Object (only ID-URI) for Like and Announce.
Expand Down
8 changes: 8 additions & 0 deletions includes/collection/class-outbox.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

namespace Activitypub\Collection;

use Activitypub\Activity\Base_Object;

/**
* ActivityPub Outbox Collection
*
Expand Down Expand Up @@ -52,6 +54,12 @@ public static function add( $activity_object, $activity_type, $user_id, $content
),
);

$object_class = get_class( $activity_object );

if ( Base_Object::class !== $object_class ) {
$outbox_item['meta_input']['_activitypub_object_class'] = str_replace( '\\', '\\\\', $object_class );
}

$has_kses = false !== \has_filter( 'content_save_pre', 'wp_filter_post_kses' );
if ( $has_kses ) {
// Prevent KSES from corrupting JSON in post_content.
Expand Down
1 change: 1 addition & 0 deletions readme.txt
Original file line number Diff line number Diff line change
Expand Up @@ -140,6 +140,7 @@ For reasons of data protection, it is not possible to see the followers of other
* Fixed: Negotiation of ActivityPub requests for custom post types when queried by the ActivityPub ID.
* Fixed: Avoid PHP warnings when using Debug mode and when the `actor` is not set.
* Fixed: No longer creates Outbox items when importing content/users.
* Fixed: Outbox is now capable of sending other object types that are not Base_Object.

= 5.0.0 =

Expand Down

0 comments on commit c363eec

Please sign in to comment.