This commit is contained in:
André Menrath 2023-11-19 20:37:49 +01:00
parent a104b8b6fd
commit 08b9539d89
11 changed files with 56 additions and 29 deletions

View file

@ -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' );

View file

@ -4,7 +4,6 @@ namespace Activitypub;
use Activitypub\Collection\Users;
use Activitypub\Collection\Followers;
use Activitypub\Transformer\Post;
/**
* ActivityPub Scheduler Class

View file

@ -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 ];
}
}

View file

@ -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();

View file

@ -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 );

View file

@ -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 );

View file

@ -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
*

View file

@ -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 );

View file

@ -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' );

View file

@ -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() );

View file

@ -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 );