From 14b15ff707599ca4371751cd4e738c98b5d86bc7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Mon, 20 Nov 2023 14:12:47 +0100 Subject: [PATCH] wip: fixes --- includes/class-activity-dispatcher.php | 6 ++--- includes/class-transformer-base.php | 25 +++++++++++++++++++ includes/class-transformers-manager.php | 16 ++++++++---- includes/model/class-post.php | 2 +- includes/rest/class-collection.php | 4 +-- includes/rest/class-outbox.php | 4 +-- includes/transformer/class-post.php | 21 ---------------- templates/post-json.php | 5 ++-- tests/test-class-activitypub-activity.php | 2 +- tests/test-class-activitypub-post.php | 4 +-- ...typub-rest-post-signature-verification.php | 4 +-- 11 files changed, 52 insertions(+), 41 deletions(-) diff --git a/includes/class-activity-dispatcher.php b/includes/class-activity-dispatcher.php index a62155a..37c3f86 100644 --- a/includes/class-activity-dispatcher.php +++ b/includes/class-activity-dispatcher.php @@ -5,7 +5,7 @@ use WP_Post; use Activitypub\Activity\Activity; use Activitypub\Collection\Users; use Activitypub\Collection\Followers; -use Activitypub\Transformer_Manager; +use Activitypub\Transformers_Manager; use function Activitypub\is_single_user; use function Activitypub\is_user_disabled; @@ -65,7 +65,7 @@ class Activity_Dispatcher { return; } - $transformer = Transformer_Manager::get_transformer( $wp_post ); + $transformer = Transformers_Manager::get_transformer( $wp_post ); $transformer->transform( $wp_post ); $transformer->to_object(); @@ -103,7 +103,7 @@ class Activity_Dispatcher { return; } - $transformer = Transformer_Manager::get_transformer( $wp_post ); + $transformer = Transformers_Manager::get_transformer( $wp_post ); $transformer->transform( $wp_post ); $transformer->to_object(); diff --git a/includes/class-transformer-base.php b/includes/class-transformer-base.php index a828a06..348007e 100644 --- a/includes/class-transformer-base.php +++ b/includes/class-transformer-base.php @@ -6,11 +6,36 @@ */ namespace Activitypub; +use WP_Post; /** * Base class to implement WordPress to ActivityPub transformers. */ abstract class Transformer_Base { + /** + * The WP_Post object. + * + * @var WP_Post + */ + protected $wp_post; + + /** + * Assign WP_Post Object to a specific transformer instance. + * + * This helps to chain the output of the Transformer. + * + * @param WP_Post $wp_post The WP_Post object + * + * @return void + */ + public function set_wp_post( WP_Post $wp_post ) { + if ( $this->supports_post_type( get_post_type( $wp_post ) ) ) { + $this->wp_post = $wp_post; + } else { + //TODO Error, this should not happen. + } + } + /** * Get the supported WP post_types that the transformer can use as an input. * diff --git a/includes/class-transformers-manager.php b/includes/class-transformers-manager.php index 93a8b98..819f383 100644 --- a/includes/class-transformers-manager.php +++ b/includes/class-transformers-manager.php @@ -212,7 +212,7 @@ class Transformers_Manager { $class_name = ucfirst( $transformer_name ); - $class_name = '\Activitypub\\Transformer_' . $class_name; + $class_name = '\Activitypub\Transformer_' . $class_name; $this->register( new $class_name() ); } @@ -224,7 +224,7 @@ class Transformers_Manager { * * @since version_number_transformer_management_placeholder * - * @param Transformer_Manager $this The widgets manager. + * @param Transformers_Manager $this The widgets manager. */ do_action( 'activitypub/transformers/register', $this ); } @@ -266,11 +266,17 @@ class Transformers_Manager { * @return Transformer_Base|null Registered transformers. */ public function get_transformer( $wp_post ) { - if ( is_null( $this->transformers[ ] ) ) { - return null; + if ( is_null( $this->transformers ) ) { + $this->init_transformers(); } - return new $this->transformers[ $wp_post ]; + if ( null !== $transformer ) { + return isset( $this->transformers[ $transformer ] ) ? $this->transformers[ $transformer ] : null; + } + $post_type = get_post_type( $wp_post ); + $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_name = $transformer_mapping[ $post_type ]; + return new $this->transformers[ $transformer_name ]; } diff --git a/includes/model/class-post.php b/includes/model/class-post.php index 6d34ab9..a9aa498 100644 --- a/includes/model/class-post.php +++ b/includes/model/class-post.php @@ -34,7 +34,7 @@ class Post { _deprecated_function( __CLASS__, '1.0.0', '\Activitypub\Transformer_Post' ); $this->post = $post; - $this->object = Post_Transformer::transform( $post )->to_object(); + $this->object = Post_Transformer->set_wp_post( $post )->to_object(); } /** diff --git a/includes/rest/class-collection.php b/includes/rest/class-collection.php index 80fd6f4..e383d3b 100644 --- a/includes/rest/class-collection.php +++ b/includes/rest/class-collection.php @@ -4,7 +4,7 @@ namespace Activitypub\Rest; use WP_Error; use WP_REST_Server; use WP_REST_Response; -use Activitypub\Transformer_Manager; +use Activitypub\Transformers_Manager; use Activitypub\Activity\Activity; use Activitypub\Collection\Users as User_Collection; @@ -168,7 +168,7 @@ class Collection { ); foreach ( $posts as $post ) { - $response['orderedItems'][] = Transformer_Manager::get_transformer( $post )->transform( $post )->to_object()->to_array(); + $response['orderedItems'][] = Transformers_Manager::get_transformer( $post )->transform( $post )->to_object()->to_array(); } $rest_response = new WP_REST_Response( $response, 200 ); diff --git a/includes/rest/class-outbox.php b/includes/rest/class-outbox.php index 4dab13e..a9ebc63 100644 --- a/includes/rest/class-outbox.php +++ b/includes/rest/class-outbox.php @@ -5,7 +5,7 @@ use stdClass; use WP_Error; use WP_REST_Server; use WP_REST_Response; -use Activitypub\Transformer_Manager; +use Activitypub\Transformers_Manager; use Activitypub\Activity\Activity; use Activitypub\Collection\Users as User_Collection; @@ -105,7 +105,7 @@ class Outbox { ); foreach ( $posts as $post ) { - $transformer = Transformer_Manager::get_transformer( $wp_post ); + $transformer = Transformers_Manager::get_transformer( $wp_post ); $transformer->transform( $wp_post ); $post = $transformer->to_object(); $activity = new Activity(); diff --git a/includes/transformer/class-post.php b/includes/transformer/class-post.php index 5e3429b..d6eaa2f 100644 --- a/includes/transformer/class-post.php +++ b/includes/transformer/class-post.php @@ -24,27 +24,6 @@ use function Activitypub\site_supports_blocks; * - Activitypub\Activity\Base_Object */ class Transformer_Post extends Transformer_Base { - - /** - * The WP_Post object. - * - * @var WP_Post - */ - protected $wp_post; - - /** - * Static function to Transform a WP_Post Object. - * - * This helps to chain the output of the Transformer. - * - * @param WP_Post $wp_post The WP_Post object - * - * @return void - */ - public function transform( WP_Post $wp_post ) { - $this->wp_post = $wp_post; - } - /** * Getter function for the name of the transformer. * diff --git a/templates/post-json.php b/templates/post-json.php index 6461b28..06690ae 100644 --- a/templates/post-json.php +++ b/templates/post-json.php @@ -2,8 +2,9 @@ // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited $post = \get_post(); -$transformer = \Activitypub\Transformer_Manager::get_transformer( $post ); -$transformer->transform( $wp_post ); +$transformer_manager = \Activitypub\Transformers_Manager::instance(); +$transformer = $transformer_manager->get_transformer( $post ); +$transformer->set_wp_post( $post ); $json = \array_merge( array( '@context' => \Activitypub\get_context() ), $transformer->to_object()->to_array() ); diff --git a/tests/test-class-activitypub-activity.php b/tests/test-class-activitypub-activity.php index c6cf11a..70ddb33 100644 --- a/tests/test-class-activitypub-activity.php +++ b/tests/test-class-activitypub-activity.php @@ -18,7 +18,7 @@ class Test_Activitypub_Activity extends WP_UnitTestCase { ); $wp_post = get_post( $post ); - $activitypub_post = \Activitypub\Transformer_Manager::get_transforemr( $wp_post )->transform( $wp_post )->to_object(); + $activitypub_post = \Activitypub\Transformers_Manager::get_transforemr( $wp_post )->transform( $wp_post )->to_object(); $activitypub_activity = new \Activitypub\Activity\Activity(); $activitypub_activity->set_type( 'Create' ); diff --git a/tests/test-class-activitypub-post.php b/tests/test-class-activitypub-post.php index 509d816..eccd6bc 100644 --- a/tests/test-class-activitypub-post.php +++ b/tests/test-class-activitypub-post.php @@ -10,13 +10,13 @@ class Test_Activitypub_Post extends WP_UnitTestCase { $permalink = \get_permalink( $post ); - $activitypub_post = \Activitypub\Transformer_Manager::get_transformer( get_post( $post ) )->transform( get_post( $post ) )->to_object(); + $activitypub_post = \Activitypub\Transformers_Manager::get_transformer( get_post( $post ) )->transform( get_post( $post ) )->to_object(); $this->assertEquals( $permalink, $activitypub_post->get_id() ); \wp_trash_post( $post ); - $activitypub_post = \Activitypub\Transformer_Manager::get_transformer( get_post( $post ) )->transform( get_post( $post ) )->to_object(); + $activitypub_post = \Activitypub\Transformers_Manager::get_transformer( get_post( $post ) )->transform( get_post( $post ) )->to_object(); $this->assertEquals( $permalink, $activitypub_post->get_id() ); diff --git a/tests/test-class-activitypub-rest-post-signature-verification.php b/tests/test-class-activitypub-rest-post-signature-verification.php index 7b8ead9..2da383b 100644 --- a/tests/test-class-activitypub-rest-post-signature-verification.php +++ b/tests/test-class-activitypub-rest-post-signature-verification.php @@ -10,7 +10,7 @@ class Test_Activitypub_Signature_Verification extends WP_UnitTestCase { ) ); $remote_actor = \get_author_posts_url( 2 ); - $activitypub_post = \Activitypub\Transformer_Manager::get_transformer( get_post( $post ) )->transform( get_post( $post ) )->to_object(); + $activitypub_post = \Activitypub\Transformers_Manager::get_transformer( get_post( $post ) )->transform( 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\Transformer_Manager::get_transformer( get_post( $post ) )->transform( get_post( $post ) )->to_object(); + $activitypub_post = \Activitypub\Transformers_Manager::get_transformer( get_post( $post ) )->transform( get_post( $post ) )->to_object(); $activitypub_activity = new Activitypub\Activity\Activity(); $activitypub_activity->set_type( 'Create' ); $activitypub_activity->set_object( $activitypub_post );