wip: fixes

This commit is contained in:
André Menrath 2023-11-20 14:12:47 +01:00
parent d07faaa1dc
commit 14b15ff707
11 changed files with 52 additions and 41 deletions

View file

@ -5,7 +5,7 @@ use WP_Post;
use Activitypub\Activity\Activity; use Activitypub\Activity\Activity;
use Activitypub\Collection\Users; use Activitypub\Collection\Users;
use Activitypub\Collection\Followers; use Activitypub\Collection\Followers;
use Activitypub\Transformer_Manager; use Activitypub\Transformers_Manager;
use function Activitypub\is_single_user; use function Activitypub\is_single_user;
use function Activitypub\is_user_disabled; use function Activitypub\is_user_disabled;
@ -65,7 +65,7 @@ class Activity_Dispatcher {
return; return;
} }
$transformer = Transformer_Manager::get_transformer( $wp_post ); $transformer = Transformers_Manager::get_transformer( $wp_post );
$transformer->transform( $wp_post ); $transformer->transform( $wp_post );
$transformer->to_object(); $transformer->to_object();
@ -103,7 +103,7 @@ class Activity_Dispatcher {
return; return;
} }
$transformer = Transformer_Manager::get_transformer( $wp_post ); $transformer = Transformers_Manager::get_transformer( $wp_post );
$transformer->transform( $wp_post ); $transformer->transform( $wp_post );
$transformer->to_object(); $transformer->to_object();

View file

@ -6,11 +6,36 @@
*/ */
namespace Activitypub; namespace Activitypub;
use WP_Post;
/** /**
* Base class to implement WordPress to ActivityPub transformers. * Base class to implement WordPress to ActivityPub transformers.
*/ */
abstract class Transformer_Base { 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. * Get the supported WP post_types that the transformer can use as an input.
* *

View file

@ -212,7 +212,7 @@ class Transformers_Manager {
$class_name = ucfirst( $transformer_name ); $class_name = ucfirst( $transformer_name );
$class_name = '\Activitypub\\Transformer_' . $class_name; $class_name = '\Activitypub\Transformer_' . $class_name;
$this->register( new $class_name() ); $this->register( new $class_name() );
} }
@ -224,7 +224,7 @@ class Transformers_Manager {
* *
* @since version_number_transformer_management_placeholder * @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 ); do_action( 'activitypub/transformers/register', $this );
} }
@ -266,11 +266,17 @@ class Transformers_Manager {
* @return Transformer_Base|null Registered transformers. * @return Transformer_Base|null Registered transformers.
*/ */
public function get_transformer( $wp_post ) { public function get_transformer( $wp_post ) {
if ( is_null( $this->transformers[ ] ) ) { if ( is_null( $this->transformers ) ) {
return null; $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 ];
} }

View file

@ -34,7 +34,7 @@ class Post {
_deprecated_function( __CLASS__, '1.0.0', '\Activitypub\Transformer_Post' ); _deprecated_function( __CLASS__, '1.0.0', '\Activitypub\Transformer_Post' );
$this->post = $post; $this->post = $post;
$this->object = Post_Transformer::transform( $post )->to_object(); $this->object = Post_Transformer->set_wp_post( $post )->to_object();
} }
/** /**

View file

@ -4,7 +4,7 @@ namespace Activitypub\Rest;
use WP_Error; use WP_Error;
use WP_REST_Server; use WP_REST_Server;
use WP_REST_Response; use WP_REST_Response;
use Activitypub\Transformer_Manager; use Activitypub\Transformers_Manager;
use Activitypub\Activity\Activity; use Activitypub\Activity\Activity;
use Activitypub\Collection\Users as User_Collection; use Activitypub\Collection\Users as User_Collection;
@ -168,7 +168,7 @@ class Collection {
); );
foreach ( $posts as $post ) { 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 ); $rest_response = new WP_REST_Response( $response, 200 );

View file

@ -5,7 +5,7 @@ use stdClass;
use WP_Error; use WP_Error;
use WP_REST_Server; use WP_REST_Server;
use WP_REST_Response; use WP_REST_Response;
use Activitypub\Transformer_Manager; use Activitypub\Transformers_Manager;
use Activitypub\Activity\Activity; use Activitypub\Activity\Activity;
use Activitypub\Collection\Users as User_Collection; use Activitypub\Collection\Users as User_Collection;
@ -105,7 +105,7 @@ class Outbox {
); );
foreach ( $posts as $post ) { foreach ( $posts as $post ) {
$transformer = Transformer_Manager::get_transformer( $wp_post ); $transformer = Transformers_Manager::get_transformer( $wp_post );
$transformer->transform( $wp_post ); $transformer->transform( $wp_post );
$post = $transformer->to_object(); $post = $transformer->to_object();
$activity = new Activity(); $activity = new Activity();

View file

@ -24,27 +24,6 @@ use function Activitypub\site_supports_blocks;
* - Activitypub\Activity\Base_Object * - Activitypub\Activity\Base_Object
*/ */
class Transformer_Post extends Transformer_Base { 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. * Getter function for the name of the transformer.
* *

View file

@ -2,8 +2,9 @@
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited // phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$post = \get_post(); $post = \get_post();
$transformer = \Activitypub\Transformer_Manager::get_transformer( $post ); $transformer_manager = \Activitypub\Transformers_Manager::instance();
$transformer->transform( $wp_post ); $transformer = $transformer_manager->get_transformer( $post );
$transformer->set_wp_post( $post );
$json = \array_merge( array( '@context' => \Activitypub\get_context() ), $transformer->to_object()->to_array() ); $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 ); $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 = new \Activitypub\Activity\Activity();
$activitypub_activity->set_type( 'Create' ); $activitypub_activity->set_type( 'Create' );

View file

@ -10,13 +10,13 @@ class Test_Activitypub_Post extends WP_UnitTestCase {
$permalink = \get_permalink( $post ); $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() ); $this->assertEquals( $permalink, $activitypub_post->get_id() );
\wp_trash_post( $post ); \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() ); $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 ); $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 = new Activitypub\Activity\Activity( 'Create' );
$activitypub_activity->set_type( 'Create' ); $activitypub_activity->set_type( 'Create' );
$activitypub_activity->set_object( $activitypub_post ); $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 = \get_author_posts_url( 2 );
$remote_actor_inbox = Activitypub\get_rest_url_by_path( '/inbox' ); $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 = new Activitypub\Activity\Activity();
$activitypub_activity->set_type( 'Create' ); $activitypub_activity->set_type( 'Create' );
$activitypub_activity->set_object( $activitypub_post ); $activitypub_activity->set_object( $activitypub_post );