Wip: elementor widget like transformer registration

This commit is contained in:
André Menrath 2023-11-19 19:50:23 +01:00
parent 043e0a3703
commit 25e8e7451f
6 changed files with 121 additions and 70 deletions

View file

@ -7,7 +7,7 @@
namespace Activitypub\Activity; namespace Activitypub\Activity;
use ActivityPub\Activity\Base_Object; use Activitypub\Activity\Base_Object;
/** /**
* Event is an implementation of one of the * Event is an implementation of one of the

View file

@ -7,7 +7,7 @@
namespace Activitypub\Activity; namespace Activitypub\Activity;
use ActivityPub\Activity\Base_Object; use Activitypub\Activity\Base_Object;
/** /**
* Note is an implementation of one of the * Note is an implementation of one of the

View file

@ -44,6 +44,16 @@ abstract class Transformer_Base {
return $plugin_data['Name'] ?? esc_html__( 'Unknown', 'activitypub' ); return $plugin_data['Name'] ?? esc_html__( 'Unknown', 'activitypub' );
} }
/**
* 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 ) {
return in_array( $post_type, $this->get_supported_post_types() );
}
/** /**
* Get the name used for registering the transformer with the ActivityPub plugin. * Get the name used for registering the transformer with the ActivityPub plugin.
@ -61,5 +71,5 @@ abstract class Transformer_Base {
* *
* @return string display name * @return string display name
*/ */
abstract public function get_title(); abstract public function get_label();
} }

View file

@ -5,9 +5,10 @@
* @link https://github.com/elementor/elementor/ * @link https://github.com/elementor/elementor/
*/ */
namespace ActivityPub; namespace Activitypub;
use function Activitypub\camel_to_snake_case; use function Activitypub\camel_to_snake_case;
use function Activitypub\snake_to_camel_case;
if ( ! defined( 'ABSPATH' ) ) { if ( ! defined( 'ABSPATH' ) ) {
exit; // Exit if accessed directly. exit; // Exit if accessed directly.
@ -28,12 +29,59 @@ class Transformers_Manager {
* *
* Holds the list of all the ActivityPub transformers. Default is `null`. * Holds the list of all the ActivityPub transformers. Default is `null`.
* *
* @since 1.0.0 * @since version_number_transformer_management_placeholder
* @access private * @access private
* *
* @var \ActivityPub\Transformer_Base[] * @var \ActivityPub\Transformer_Base[]
*/ */
private $transformers = array(); private $transformers = null;
/**
* Module instance.
*
* Holds the transformer instance.
*
* @since version_number_transformer_management_placeholder
* @access protected
*
* @var Module
*/
protected static $_instances = [];
/**
* Instance.
*
* Ensures only one instance of the module class is loaded or can be loaded.
*
* @since version_number_transformer_management_placeholder
* @access public
* @static
*
* @return Module An instance of the class.
*/
public static function instance() {
$class_name = static::class_name();
if ( empty( static::$_instances[ $class_name ] ) ) {
static::$_instances[ $class_name ] = new static();
}
return static::$_instances[ $class_name ];
}
/**
* Class name.
*
* Retrieve the name of the class.
*
* @since version_number_transformer_management_placeholder
* @access public
* @static
*/
public static function class_name() {
return get_called_class();
}
/** /**
* Transformers manager constructor. * Transformers manager constructor.
@ -56,7 +104,23 @@ class Transformers_Manager {
* @access private * @access private
*/ */
private function require_files() { private function require_files() {
require ACTIVITYPUB_PLUGIN_DIR . 'includes/base/class-transformer-base.php'; require ACTIVITYPUB_PLUGIN_DIR . 'includes/class-transformer-base.php';
}
final public function T( $post_type ) {
return in_array($post_type, $this->supported_post_types);
}
/**
* Checks if a transformer is registered.
*
* @since version_number_transformer_management_placeholder
*
* @param string $name Transformer name including namespace.
* @return bool True if the block type is registered, false otherwise.
*/
public function is_registered( $name ) {
return isset( $this->transformers[ $name ] );
} }
/** /**
@ -80,7 +144,7 @@ class Transformers_Manager {
return false; return false;
} }
$transformer_name = $transformer_instance->name; $transformer_name = $transformer_instance->get_name();
if ( preg_match( '/[A-Z]+/', $transformer_name ) ) { if ( preg_match( '/[A-Z]+/', $transformer_name ) ) {
_doing_it_wrong( _doing_it_wrong(
__METHOD__, __METHOD__,
@ -118,13 +182,13 @@ class Transformers_Manager {
* @param bool $should_register Should the ActivityPub transformer be registered. Default is `true`. * @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.
*/ */
$should_register = apply_filters( 'activitypub/transformers/is_transformer_enabled', true, $transformer_instance ); // $should_register = apply_filters( 'activitypub/transformers/is_transformer_enabled', true, $transformer_instance );
if ( ! $should_register ) { // if ( ! $should_register ) {
return false; // return false;
} // }
$this->_transformers[ $transformer_name ] = $transformer_instance; $this->transformers[ $transformer_name ] = $transformer_instance;
return true; return true;
} }
@ -144,14 +208,14 @@ class Transformers_Manager {
'post' 'post'
]; ];
$this->_transformers = []; $this->transformers = [];
foreach ( $builtin_transformers as $transformer_name ) { foreach ( $builtin_transformers as $transformer_name ) {
include ACTIVITYPUB_PLUGIN_DIR . 'includes/transformers/class-' . $transformer_name . '.php'; include ACTIVITYPUB_PLUGIN_DIR . 'includes/transformer/class-' . $transformer_name . '.php';
$class_name = camel_to_snake_case( $transformer_name ); $class_name = ucfirst( $transformer_name );
$class_name = __NAMESPACE__ . '\Transformer_' . $class_name; $class_name = '\Activitypub\\Transformer_' . $class_name;
$this->register( new $class_name() ); $this->register( new $class_name() );
} }
@ -159,7 +223,7 @@ class Transformers_Manager {
/** /**
* After the transformers are registered. * After the transformers are registered.
* *
* Fires after the built-in Actiitypub transformers are registered. * Fires after the built-in Activitypub transformers are registered.
* *
* @since version_number_transformer_management_placeholder * @since version_number_transformer_management_placeholder
* *
@ -172,25 +236,25 @@ class Transformers_Manager {
/** /**
* Get available ActivityPub transformers. * Get available ActivityPub transformers.
* *
* Retrieve the registered widget types list. * Retrieve the registered transformers list.
* *
* @since 1.0.0 * @since 1.0.0
* @access public * @access public
* *
* @param string $widget_name Optional. Widget name. Default is null. * @param string $transformers Optional. Widget name. Default is null.
* *
* @return Widget_Base|Widget_Base[]|null Registered widget types. * @return Transformer_Base|Transformer_Base[]|null Registered transformers.
*/ */
public function get_widget_types( $widget_name = null ) { public function get_transformers( $transformer = null ) {
if ( is_null( $this->_widget_types ) ) { if ( is_null( $this->transformers ) ) {
$this->init_widgets(); $this->init_transformers();
} }
if ( null !== $widget_name ) { if ( null !== $transformer ) {
return isset( $this->_widget_types[ $widget_name ] ) ? $this->_widget_types[ $widget_name ] : null; return isset( $this->transformers[ $transformer ] ) ? $this->transformers[ $transformer ] : null;
} }
return $this->_widget_types; return $this->transformers;
} }

View file

@ -1,11 +1,12 @@
<?php <?php
namespace Activitypub\Transformer; namespace Activitypub;
use WP_Post; use WP_Post;
use Activitypub\Collection\Users; use Activitypub\Collection\Users;
use Activitypub\Model\Blog_User; use Activitypub\Model\Blog_User;
use Activitypub\Activity\Base_Object; use Activitypub\Activity\Base_Object;
use Activitypub\Shortcodes; use Activitypub\Shortcodes;
use Activitypub\Transformer_Base;
use function Activitypub\esc_hashtag; use function Activitypub\esc_hashtag;
use function Activitypub\is_single_user; use function Activitypub\is_single_user;
@ -22,7 +23,7 @@ use function Activitypub\site_supports_blocks;
* *
* - Activitypub\Activity\Base_Object * - Activitypub\Activity\Base_Object
*/ */
class Post { class Transformer_Post extends Transformer_Base {
/** /**
* The WP_Post object. * The WP_Post object.
@ -44,14 +45,25 @@ class Post {
return new static( $wp_post ); return new static( $wp_post );
} }
public function get_name() {
return 'activitypub/default';
}
public function get_label() {
return 'Built-In';
}
abstract
/** /**
* *
* *
* @param WP_Post $wp_post * @param WP_Post $wp_post
*/ */
public function __construct( WP_Post $wp_post ) { // TODO
$this->wp_post = $wp_post; // public function __construct( WP_Post $wp_post ) {
} // $this->wp_post = $wp_post;
// }
/** /**
* Transforms the WP_Post object to an ActivityPub Object * Transforms the WP_Post object to an ActivityPub Object

View file

@ -253,48 +253,13 @@
<?php $all_public_post_types = \get_post_types( array( 'public' => true ), 'objects' ); <?php $all_public_post_types = \get_post_types( array( 'public' => true ), 'objects' );
$transformer_mapping = \get_option( 'activitypub_transformer_mapping', array( 'default' => 'note' ) ); $transformer_mapping = \get_option( 'activitypub_transformer_mapping', array( 'default' => 'note' ) );
// DUMMY CODE for developing:
class Transformer {
protected $name;
protected $supported_post_types;
protected $label;
public function __construct(string $name, $label, array $supported_post_types) {
$this->name = $name;
$this->label = $label;
$this->supported_post_types = $supported_post_types;
}
public function get_name() {
return $this->name;
}
public function get_label() {
return $this->label;
}
public function get_settings_page() {
return 'https://event-federation.eu';
}
public function get_info_text() {
return 'this is some generic info';
}
public function supports_post_type($post_type) {
return in_array($post_type, $this->supported_post_types);
}
}
$all_public_post_type_names = array_map(function ($object) { $all_public_post_type_names = array_map(function ($object) {
return $object->name; return $object->name;
}, $all_public_post_types); }, $all_public_post_types);
$transformer1 = new Transformer('default', 'Built-In', $all_public_post_type_names); $transformer_manager = \Activitypub\Transformers_Manager::instance();
$transformer2 = new Transformer('tribe', 'The Events Calendar', array('tribe_events')); $transformers = $transformer_manager->get_transformers();
$transformer3 = new Transformer('generic_event', 'Event', array('tribe_events', 'custom_event'));
$transformers = array($transformer1, $transformer2, $transformer3);
// END OF DUMMY CODE // END OF DUMMY CODE
?> ?>