diff --git a/includes/class-activity-dispatcher.php b/includes/class-activity-dispatcher.php index 462a75a..a62155a 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\Post; +use Activitypub\Transformer_Manager; use function Activitypub\is_single_user; use function Activitypub\is_user_disabled; @@ -65,7 +65,9 @@ class Activity_Dispatcher { return; } - $object = Post::transform( $wp_post )->to_object(); + $transformer = Transformer_Manager::get_transformer( $wp_post ); + $transformer->transform( $wp_post ); + $transformer->to_object(); $activity = new Activity(); $activity->set_type( $type ); @@ -101,7 +103,9 @@ class Activity_Dispatcher { return; } - $object = Post::transform( $wp_post )->to_object(); + $transformer = Transformer_Manager::get_transformer( $wp_post ); + $transformer->transform( $wp_post ); + $transformer->to_object(); $activity = new Activity(); $activity->set_type( 'Announce' ); diff --git a/includes/class-scheduler.php b/includes/class-scheduler.php index 63f9273..a58e5b9 100644 --- a/includes/class-scheduler.php +++ b/includes/class-scheduler.php @@ -4,7 +4,6 @@ namespace Activitypub; use Activitypub\Collection\Users; use Activitypub\Collection\Followers; -use Activitypub\Transformer\Post; /** * ActivityPub Scheduler Class diff --git a/includes/class-transformers-manager.php b/includes/class-transformers-manager.php index 4266a8f..93a8b98 100644 --- a/includes/class-transformers-manager.php +++ b/includes/class-transformers-manager.php @@ -255,5 +255,24 @@ class Transformers_Manager { } + /** + * Get the right ActivityPub transformer. + * + * @since 1.0.0 + * @access public + * + * @param WP_Post $wp_post The WordPress Post. + * + * @return Transformer_Base|null Registered transformers. + */ + public function get_transformer( $wp_post ) { + if ( is_null( $this->transformers[ ] ) ) { + return null; + } + + return new $this->transformers[ $wp_post ]; + } + + } diff --git a/includes/model/class-post.php b/includes/model/class-post.php index 33c1e31..6d34ab9 100644 --- a/includes/model/class-post.php +++ b/includes/model/class-post.php @@ -31,7 +31,7 @@ class Post { */ // phpcs:ignore Generic.CodeAnalysis.UnusedFunctionParameter.FoundAfterLastUsed, VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable public function __construct( $post, $post_author = null ) { - _deprecated_function( __CLASS__, '1.0.0', '\Activitypub\Transformer\Post' ); + _deprecated_function( __CLASS__, '1.0.0', '\Activitypub\Transformer_Post' ); $this->post = $post; $this->object = Post_Transformer::transform( $post )->to_object(); diff --git a/includes/rest/class-collection.php b/includes/rest/class-collection.php index 365641c..80fd6f4 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\Post; +use Activitypub\Transformer_Manager; use Activitypub\Activity\Activity; use Activitypub\Collection\Users as User_Collection; @@ -168,7 +168,7 @@ class Collection { ); foreach ( $posts as $post ) { - $response['orderedItems'][] = Post::transform( $post )->to_object()->to_array(); + $response['orderedItems'][] = Transformer_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 d640d17..4dbd81a 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\Post; +use Activitypub\Transformer_Manager; use Activitypub\Activity\Activity; use Activitypub\Collection\Users as User_Collection; @@ -105,7 +105,9 @@ class Outbox { ); foreach ( $posts as $post ) { - $post = Post::transform( $post )->to_object(); + $transformer = Transformer_Manager::get_transformer( $wp_post ); + $transformer->transform( $wp_post ); + $post = $transformer->to_object(); $activity = new Activity(); $activity->set_type( 'Create' ); $activity->set_context( null ); diff --git a/includes/transformer/class-post.php b/includes/transformer/class-post.php index 16319b0..5e3429b 100644 --- a/includes/transformer/class-post.php +++ b/includes/transformer/class-post.php @@ -41,28 +41,28 @@ class Transformer_Post extends Transformer_Base { * * @return void */ - public static function transform( WP_Post $wp_post ) { - return new static( $wp_post ); + public function transform( WP_Post $wp_post ) { + $this->wp_post = $wp_post; } + /** + * Getter function for the name of the transformer. + * + * @return string name + */ public function get_name() { return 'activitypub/default'; } + /** + * Getter function for the display name (label/title) of the transformer. + * + * @return string name + */ public function get_label() { return 'Built-In'; } - /** - * - * - * @param WP_Post $wp_post - */ - // TODO - // public function __construct( WP_Post $wp_post ) { - // $this->wp_post = $wp_post; - // } - /** * Transforms the WP_Post object to an ActivityPub Object * diff --git a/templates/post-json.php b/templates/post-json.php index 89467c4..6461b28 100644 --- a/templates/post-json.php +++ b/templates/post-json.php @@ -2,8 +2,10 @@ // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited $post = \get_post(); -$object = new \Activitypub\Transformer\Post( $post ); -$json = \array_merge( array( '@context' => \Activitypub\get_context() ), $object->to_object()->to_array() ); +$transformer = \Activitypub\Transformer_Manager::get_transformer( $post ); +$transformer->transform( $wp_post ); + +$json = \array_merge( array( '@context' => \Activitypub\get_context() ), $transformer->to_object()->to_array() ); // filter output $json = \apply_filters( 'activitypub_json_post_array', $json ); diff --git a/tests/test-class-activitypub-activity.php b/tests/test-class-activitypub-activity.php index ba9f5a2..c6cf11a 100644 --- a/tests/test-class-activitypub-activity.php +++ b/tests/test-class-activitypub-activity.php @@ -16,8 +16,9 @@ class Test_Activitypub_Activity extends WP_UnitTestCase { }, 10 ); - - $activitypub_post = \Activitypub\Transformer\Post::transform( get_post( $post ) )->to_object(); + + $wp_post = get_post( $post ); + $activitypub_post = \Activitypub\Transformer_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 0b5ee6d..509d816 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\Post::transform( get_post( $post ) )->to_object(); + $activitypub_post = \Activitypub\Transformer_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\Post::transform( get_post( $post ) )->to_object(); + $activitypub_post = \Activitypub\Transformer_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 2d1c2f9..7b8ead9 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\Post::transform( get_post( $post ) )->to_object(); + $activitypub_post = \Activitypub\Transformer_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\Post::transform( \get_post( $post ) )->to_object(); + $activitypub_post = \Activitypub\Transformer_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 );