Automatically set the wp_post when returning a transformer instance for the wp_post

This commit is contained in:
André Menrath 2023-11-20 19:36:45 +01:00
parent 3ae71bb18c
commit 6d40ebf30e
8 changed files with 19 additions and 14 deletions

View file

@ -66,8 +66,7 @@ class Activity_Dispatcher {
}
$transformer = \Activitypub\Transformers_Manager::instance()->get_transformer( $wp_post );
$transformer->set_wp_post( $wp_post );
$transformer->to_object();
$object = $transformer->to_object();
$activity = new Activity();
$activity->set_type( $type );
@ -104,8 +103,7 @@ class Activity_Dispatcher {
}
$transformer = \Activitypub\Transformers_Manager::instance()->get_transformer( $wp_post );
$transformer->set_wp_post( $wp_post );
$transformer->to_object();
$object = $transformer->to_object();
$activity = new Activity();
$activity->set_type( 'Announce' );

View file

@ -27,6 +27,11 @@ if ( ! defined( 'ABSPATH' ) ) {
*/
class Transformers_Manager {
const DEFAULT_TRANSFORMER_MAPPING = array(
'post' => 'activitypub/default',
'page' => 'activitypub/default'
);
/**
* Transformers.
*
@ -259,6 +264,8 @@ class Transformers_Manager {
/**
* Get the mapped ActivityPub transformer.
*
* Returns a new instance of the needed WordPress to ActivityPub transformer.
*
* @since version_number_transformer_management_placeholder
* @access public
@ -271,9 +278,11 @@ class Transformers_Manager {
switch ( get_class( $object ) ) {
case 'WP_Post':
$post_type = get_post_type( $object );
$transformer_mapping = \get_option( 'activitypub_transformer_mapping', array( 'post' => 'activitypub/default', 'page' => 'activitypub/default' ) ) ? \get_option( 'activitypub_transformer_mapping', array( 'post' => 'activitypub/default', 'page' => 'activitypub/default' ) ) : array();
$transformer_mapping = \get_option( 'activitypub_transformer_mapping', DEFAULT_TRANSFORMER_MAPPING );
$transformer_name = $transformer_mapping[ $post_type ];
return new ( $this->get_transformers( $transformer_name ) );
$transformer_instance = new ( $this->get_transformers( $transformer_name ) );
$transformer_instance->set_wp_post( $object );
return $transformer_instance;
case 'WP_Comment':
return new Comment( $object );
default:

View file

@ -168,7 +168,7 @@ class Collection {
);
foreach ( $posts as $post ) {
$response['orderedItems'][] = Transformers_Manager::instance()->get_transformer( $post )->set_wp_post( $wp_post )->to_object()->to_array();
$response['orderedItems'][] = Transformers_Manager::instance()->get_transformer( $post )->to_object()->to_array();
}
$rest_response = new WP_REST_Response( $response, 200 );

View file

@ -106,7 +106,6 @@ class Outbox {
foreach ( $posts as $post ) {
$transformer = \Activitypub\Transformers_Manager::instance()->get_transformer( $post );
$transformer->set_wp_post( $wp_post );
$post = $transformer->to_object();
$activity = new Activity();
$activity->set_type( 'Create' );

View file

@ -3,7 +3,6 @@
$post = \get_post();
$transformer = \Activitypub\Transformers_Manager::instance()->get_transformer( $post );
$transformer->set_wp_post( $post );
$json = \array_merge( array( '@context' => \Activitypub\get_context() ), $transformer->to_object()->to_array() );

View file

@ -18,7 +18,7 @@ class Test_Activitypub_Activity extends WP_UnitTestCase {
);
$wp_post = get_post( $post );
$activitypub_post = \Activitypub\Transformers_Manager::instance()->get_transformer( $post )->set_wp_post( $wp_post )->to_object();
$activitypub_post = \Activitypub\Transformers_Manager::instance()->get_transformer( $post )->to_object();
$activitypub_activity = new \Activitypub\Activity\Activity();
$activitypub_activity->set_type( 'Create' );

View file

@ -10,13 +10,13 @@ class Test_Activitypub_Post extends WP_UnitTestCase {
$permalink = \get_permalink( $post );
$activitypub_post = \Activitypub\Transformers_Manager::instance()->get_transformer( get_post( $post ) )->set_wp_post( get_post( $post ) )->to_object();
$activitypub_post = \Activitypub\Transformers_Manager::instance()->get_transformer( get_post( $post ) )->to_object();
$this->assertEquals( $permalink, $activitypub_post->get_id() );
\wp_trash_post( $post );
$activitypub_post = \Activitypub\Transformers_Manager::instance()->get_transformer( get_post( $post ) )->set_wp_post( get_post( $post ) )->to_object();
$activitypub_post = \Activitypub\Transformers_Manager::instance()->get_transformer( get_post( $post ) )->to_object();
$this->assertEquals( $permalink, $activitypub_post->get_id() );

View file

@ -10,7 +10,7 @@ class Test_Activitypub_Signature_Verification extends WP_UnitTestCase {
)
);
$remote_actor = \get_author_posts_url( 2 );
$activitypub_post = \Activitypub\Transformers_Manager::instance()->get_transformer( get_post( $post ) )->set_wp_post( get_post( $post ) )->to_object();
$activitypub_post = \Activitypub\Transformers_Manager::instance()->get_transformer( get_post( $post ) )->to_object();
$activitypub_activity = new Activitypub\Activity\Activity( 'Create' );
$activitypub_activity->set_type( 'Create' );
$activitypub_activity->set_object( $activitypub_post );
@ -82,7 +82,7 @@ class Test_Activitypub_Signature_Verification extends WP_UnitTestCase {
);
$remote_actor = \get_author_posts_url( 2 );
$remote_actor_inbox = Activitypub\get_rest_url_by_path( '/inbox' );
$activitypub_post = \Activitypub\Transformers_Manager::instance()->get_transformer( get_post( $post ) )->set_wp_post( get_post( $post ) )->to_object();
$activitypub_post = \Activitypub\Transformers_Manager::instance()->get_transformer( get_post( $post ) )->to_object();
$activitypub_activity = new Activitypub\Activity\Activity();
$activitypub_activity->set_type( 'Create' );
$activitypub_activity->set_object( $activitypub_post );