transformer: move all related code to \Activitypub\Transformer namespace

This commit is contained in:
André Menrath 2023-11-22 08:36:55 +01:00
parent 0501fc5ec7
commit 369b32bc93
13 changed files with 102 additions and 111 deletions

View file

@ -5,7 +5,7 @@ use WP_Post;
use Activitypub\Activity\Activity;
use Activitypub\Collection\Users;
use Activitypub\Collection\Followers;
use Activitypub\Transformers_Manager;
use Activitypub\Transformer\Transformers_Manager;
use function Activitypub\is_single_user;
use function Activitypub\is_user_disabled;
@ -65,7 +65,7 @@ class Activity_Dispatcher {
return;
}
$transformer = \Activitypub\Transformers_Manager::instance()->get_transformer( $wp_post );
$transformer = \Activitypub\Transformer\Transformers_Manager::instance()->get_transformer( $wp_post );
$object = $transformer->to_object();
$activity = new Activity();
@ -102,7 +102,7 @@ class Activity_Dispatcher {
return;
}
$transformer = \Activitypub\Transformers_Manager::instance()->get_transformer( $wp_post );
$transformer = \Activitypub\Transformer\Transformers_Manager::instance()->get_transformer( $wp_post );
$object = $transformer->to_object();
$activity = new Activity();

View file

@ -3,7 +3,7 @@ namespace Activitypub;
use WP_User_Query;
use Activitypub\Model\Blog_User;
use Activitypub\Base\Transformer_Base;
use Activitypub\Base\Transformer\Base as Transformer_Base;
/**
* ActivityPub Admin Class
@ -24,7 +24,8 @@ class Admin {
\add_action( 'show_user_profile', array( self::class, 'add_profile' ) );
}
add_filter( 'activitypub/transformers/is_transformer_enabled', function( $should_register, Transformer_Base $widget_instance ) {
add_filter(
'activitypub/transformers/is_transformer_enabled', function( $should_register, Transformer_Base $widget_instance ) {
return ! Options::is_transformer_disabled( $transformer_instance->get_name() );
}, 10, 2 );
}

View file

@ -1,7 +1,7 @@
<?php
namespace Activitypub\Model;
use Activitypub\Transformer_Post as Post_Transformer;
use Activitypub\Transformer\Post;
/**
* ActivityPub Post Class
@ -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->set_wp_post( $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\Transformers_Manager;
use Activitypub\Transformer\Transformers_Manager;
use Activitypub\Activity\Activity;
use Activitypub\Collection\Users as User_Collection;

View file

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

View file

@ -4,15 +4,13 @@
*
* @link https://github.com/landrok/activitypub
*/
namespace Activitypub;
namespace Activitypub\Transformer;
use WP_Post;
use Activitypub\Collection\Users;
use Activitypub\Model\Blog_User;
use Activitypub\Activity\Base_Object;
use Activitypub\Shortcodes;
use Activitypub\Transformer_Base;
use function Activitypub\esc_hashtag;
use function Activitypub\is_single_user;
@ -22,7 +20,7 @@ use function Activitypub\site_supports_blocks;
/**
* Base class to implement WordPress to ActivityPub transformers.
*/
abstract class Transformer_Base {
abstract class Base {
/**
* The WP_Post object.
*
@ -35,15 +33,14 @@ abstract class Transformer_Base {
*
* This helps to chain the output of the Transformer.
*
* @param WP_Post $wp_post The WP_Post object
*
* @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.
// TODO Error, this should not happen.
}
}
@ -64,7 +61,6 @@ abstract class Transformer_Base {
* Get the name of the plugin that registered the transformer.
*
* @see Forked from the WordPress elementor plugin.
*
* @since version_number_transformer_management_placeholder
* @return string Plugin name
*/
@ -85,7 +81,6 @@ abstract class Transformer_Base {
* Return whether the transformer supports a post type.
*
* @since version_number_transformer_management_placeholder
*
* @return string post_type Post type name.
*/
final public function supports_post_type( $post_type ) {
@ -96,7 +91,6 @@ abstract class Transformer_Base {
* Get the name used for registering the transformer with the ActivityPub plugin.
*
* @since version_number_transformer_management_placeholder
*
* @return string name
*/
abstract public function get_name();
@ -105,7 +99,6 @@ abstract class Transformer_Base {
* Get the display name for the ActivityPub transformer.
*
* @since version_number_transformer_management_placeholder
*
* @return string display name
*/
abstract public function get_label();
@ -300,6 +293,7 @@ abstract class Transformer_Base {
/**
* Recursively get media IDs from blocks.
*
* @param array $blocks The blocks to search for media IDs
* @param array $media_ids The media IDs to append new IDs to
* @param int $max_media The maximum number of media to return.
@ -593,5 +587,4 @@ abstract class Transformer_Base {
return $object;
}
}

View file

@ -1,12 +1,12 @@
<?php
namespace Activitypub;
namespace Activitypub\Transformer;
use WP_Post;
use Activitypub\Collection\Users;
use Activitypub\Model\Blog_User;
use Activitypub\Activity\Base_Object;
use Activitypub\Shortcodes;
use Activitypub\Transformer_Base;
use Activitypub\Transformer\Base;
use function Activitypub\esc_hashtag;
use function Activitypub\is_single_user;
@ -18,12 +18,10 @@ use function Activitypub\site_supports_blocks;
* The Post Transformer is responsible for transforming a WP_Post object into different othe
* Object-Types.
*
*
* Currently supported are:
*
* - Activitypub\Activity\Base_Object
*/
class Transformer_Post extends Transformer_Base {
class Post extends Base {
/**
* Getter function for the name of the transformer.
*
@ -129,5 +127,4 @@ class Transformer_Post extends Transformer_Base {
return \get_option( 'activitypub_custom_post_content', ACTIVITYPUB_CUSTOM_POST_CONTENT );
}
}

View file

@ -3,9 +3,10 @@
* Inspired by the way elementor handles addons.
*
* @link https://github.com/elementor/elementor/
* @package Activitypub
*/
namespace Activitypub;
namespace Activitypub\Transformer;
use WP_Post;
use WP_Comment;
@ -25,7 +26,6 @@ if ( ! defined( 'ABSPATH' ) ) {
*
* @since version_number_transformer_management_placeholder
*/
class Transformers_Manager {
const DEFAULT_TRANSFORMER_MAPPING = array(
'post' => ACTIVITYPUB_DEFAULT_TRANSFORMER,
@ -40,7 +40,7 @@ class Transformers_Manager {
* @since version_number_transformer_management_placeholder
* @access private
*
* @var \ActivityPub\Transformer_Base[]
* @var \ActivityPub\Transformer\Base[]
*/
private $transformers = null;
@ -112,7 +112,7 @@ class Transformers_Manager {
* @access private
*/
private function require_files() {
require ACTIVITYPUB_PLUGIN_DIR . 'includes/class-transformer-base.php';
require ACTIVITYPUB_PLUGIN_DIR . 'includes/transformer/class-base.php';
}
/**
@ -133,13 +133,13 @@ class Transformers_Manager {
* @since version_number_transformer_management_placeholder
* @access public
*
* @param \ActivityPub\Transformer_Base $transformer_instance ActivityPub Transformer.
* @param \ActivityPub\Transformer\Base $transformer_instance ActivityPub Transformer.
*
* @return bool True if the ActivityPub transformer was registered.
*/
public function register( Transformer_Base $transformer_instance) {
public function register( \ActivityPub\Transformer\Base $transformer_instance) {
if ( ! $transformer_instance instanceof Transformer_Base ) {
if ( ! $transformer_instance instanceof \ActivityPub\Transformer\Base ) {
_doing_it_wrong(
__METHOD__,
__( 'ActivityPub transformer instance must be a of \ActivityPub\Transformer_Base class.' ),
@ -184,7 +184,7 @@ class Transformers_Manager {
* @since version_number_transformer_management_placeholder
*
* @param bool $should_register Should the ActivityPub transformer be registered. Default is `true`.
* @param \ActivityPub\Transformer_Base $transformer_instance Widget instance.
* @param \ActivityPub\Transformer\Base $transformer_instance Widget instance.
*/
// TODO: does not implementing this slow down the website? -> compare with gutenberg block registration.
// $should_register = apply_filters( 'activitypub/transformers/is_transformer_enabled', true, $transformer_instance );
@ -219,7 +219,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() );
}
@ -248,7 +248,7 @@ class Transformers_Manager {
*
* @param string $transformers Optional. Transformer name. Default is null.
*
* @return Transformer_Base|Transformer_Base[]|null Registered transformers.
* @return Base|Base[]|null Registered transformers.
*/
public function get_transformers( $transformer_name = null ) {
if ( is_null( $this->transformers ) ) {
@ -272,7 +272,7 @@ class Transformers_Manager {
*
* @param WP_Post|WP_Comment $wp_post The WordPress Post/Comment.
*
* @return Transformer_Base|null Registered transformers.
* @return \ActivityPub\Transformer\Base|null Registered transformers.
*/
public function get_transformer( $object ) {
switch ( get_class( $object ) ) {

View file

@ -2,7 +2,7 @@
// phpcs:ignore WordPress.WP.GlobalVariablesOverride.Prohibited
$post = \get_post();
$transformer = \Activitypub\Transformers_Manager::instance()->get_transformer( $post );
$transformer = \Activitypub\Transformer\Transformers_Manager::instance()->get_transformer( $post );
$json = \array_merge( array( '@context' => \Activitypub\get_context() ), $transformer->to_object()->to_array() );

View file

@ -237,7 +237,7 @@
return $object->name;
}, $all_public_post_types);
$transformer_manager = \Activitypub\Transformers_Manager::instance();
$transformer_manager = \Activitypub\Transformer\Transformers_Manager::instance();
$transformers = $transformer_manager->get_transformers();
?>

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 )->to_object();
$activitypub_post = \Activitypub\Transformer\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 ) )->to_object();
$activitypub_post = \Activitypub\Transformer\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 ) )->to_object();
$activitypub_post = \Activitypub\Transformer\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 ) )->to_object();
$activitypub_post = \Activitypub\Transformer\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 ) )->to_object();
$activitypub_post = \Activitypub\Transformer\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 );