2023-11-17 16:28:04 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Inspired by the way elementor handles addons.
|
|
|
|
*
|
|
|
|
* @link https://github.com/elementor/elementor/
|
|
|
|
*/
|
|
|
|
|
2023-11-19 19:50:23 +01:00
|
|
|
namespace Activitypub;
|
2023-11-17 16:28:04 +01:00
|
|
|
|
2023-11-18 15:00:28 +01:00
|
|
|
use function Activitypub\camel_to_snake_case;
|
2023-11-19 19:50:23 +01:00
|
|
|
use function Activitypub\snake_to_camel_case;
|
2023-11-18 15:00:28 +01:00
|
|
|
|
2023-11-17 16:28:04 +01:00
|
|
|
if ( ! defined( 'ABSPATH' ) ) {
|
|
|
|
exit; // Exit if accessed directly.
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* ActivityPub transformers manager.
|
|
|
|
*
|
|
|
|
* ActivityPub transformers manager handler class is responsible for registering and
|
|
|
|
* initializing all the supported WP-Pobject to ActivityPub transformers.
|
|
|
|
*
|
|
|
|
* @since version_number_transformer_management_placeholder
|
|
|
|
*/
|
|
|
|
|
|
|
|
class Transformers_Manager {
|
|
|
|
/**
|
|
|
|
* Transformers.
|
|
|
|
*
|
2023-11-18 13:40:27 +01:00
|
|
|
* Holds the list of all the ActivityPub transformers. Default is `null`.
|
2023-11-17 16:28:04 +01:00
|
|
|
*
|
2023-11-19 19:50:23 +01:00
|
|
|
* @since version_number_transformer_management_placeholder
|
2023-11-17 16:28:04 +01:00
|
|
|
* @access private
|
|
|
|
*
|
2023-11-18 13:40:27 +01:00
|
|
|
* @var \ActivityPub\Transformer_Base[]
|
2023-11-17 16:28:04 +01:00
|
|
|
*/
|
2023-11-19 19:50:23 +01:00
|
|
|
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();
|
|
|
|
}
|
2023-11-17 16:28:04 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Transformers manager constructor.
|
|
|
|
*
|
|
|
|
* Initializing ActivityPub transformers manager.
|
|
|
|
*
|
|
|
|
* @since version_number_transformer_management_placeholder
|
|
|
|
* @access public
|
|
|
|
*/
|
|
|
|
public function __construct() {
|
|
|
|
$this->require_files();
|
2023-11-18 13:40:27 +01:00
|
|
|
}
|
2023-11-17 16:28:04 +01:00
|
|
|
|
2023-11-18 13:40:27 +01:00
|
|
|
/**
|
|
|
|
* Require files.
|
|
|
|
*
|
|
|
|
* Require ActivityPub transformer base class.
|
|
|
|
*
|
|
|
|
* @since version_number_transformer_management_placeholder
|
|
|
|
* @access private
|
|
|
|
*/
|
|
|
|
private function require_files() {
|
2023-11-19 19:50:23 +01:00
|
|
|
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 ] );
|
2023-11-17 16:28:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a transformer.
|
|
|
|
*
|
|
|
|
* @since version_number_transformer_management_placeholder
|
|
|
|
* @access public
|
|
|
|
*
|
2023-11-18 13:40:27 +01:00
|
|
|
* @param \ActivityPub\Transformer_Base $transformer_instance ActivityPub Transformer.
|
2023-11-17 16:28:04 +01:00
|
|
|
*
|
2023-11-18 13:40:27 +01:00
|
|
|
* @return bool True if the ActivityPub transformer was registered.
|
2023-11-17 16:28:04 +01:00
|
|
|
*/
|
2023-11-18 13:40:27 +01:00
|
|
|
public function register( Transformer_Base $transformer_instance) {
|
2023-11-17 16:28:04 +01:00
|
|
|
|
2023-11-18 13:40:27 +01:00
|
|
|
if ( ! $transformer_instance instanceof Transformer_Base ) {
|
|
|
|
_doing_it_wrong(
|
|
|
|
__METHOD__,
|
|
|
|
__( 'ActivityPub transformer instance must be a implementation of the \ActivityPub\Transformer_Base class.' ),
|
|
|
|
'version_number_transformer_management_placeholder'
|
2023-11-17 16:28:04 +01:00
|
|
|
);
|
2023-11-18 13:40:27 +01:00
|
|
|
return false;
|
2023-11-17 16:28:04 +01:00
|
|
|
}
|
2023-11-18 13:40:27 +01:00
|
|
|
|
2023-11-19 19:50:23 +01:00
|
|
|
$transformer_name = $transformer_instance->get_name();
|
2023-11-18 13:40:27 +01:00
|
|
|
if ( preg_match( '/[A-Z]+/', $transformer_name ) ) {
|
|
|
|
_doing_it_wrong(
|
|
|
|
__METHOD__,
|
|
|
|
__( 'ActivityPub transformer names must not contain uppercase characters.' ),
|
|
|
|
'version_number_transformer_management_placeholder'
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
$name_matcher = '/^[a-z0-9-]+\/[a-z0-9-]+$/';
|
|
|
|
if ( ! preg_match( $name_matcher, $transformer_name ) ) {
|
|
|
|
_doing_it_wrong(
|
|
|
|
__METHOD__,
|
|
|
|
__( 'ActivityPub transformer names must contain a namespace prefix. Example: my-plugin/my-custom-transformer' ),
|
|
|
|
'version_number_transformer_management_placeholder'
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $this->is_registered( $transformer_name ) ) {
|
|
|
|
_doing_it_wrong(
|
|
|
|
__METHOD__,
|
|
|
|
/* translators: %s: Block name. */
|
|
|
|
sprintf( __( 'ActivityPub transformer with name "%s" is already registered.' ), $transformer_name ),
|
|
|
|
'version_number_transformer_management_placeholder'
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
2023-11-17 16:28:04 +01:00
|
|
|
|
2023-11-18 13:40:27 +01:00
|
|
|
/**
|
|
|
|
* Should the ActivityPub transformer be registered.
|
|
|
|
*
|
|
|
|
* @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.
|
|
|
|
*/
|
2023-11-19 19:50:23 +01:00
|
|
|
// $should_register = apply_filters( 'activitypub/transformers/is_transformer_enabled', true, $transformer_instance );
|
2023-11-18 13:40:27 +01:00
|
|
|
|
2023-11-19 19:50:23 +01:00
|
|
|
// if ( ! $should_register ) {
|
|
|
|
// return false;
|
|
|
|
// }
|
2023-11-18 13:40:27 +01:00
|
|
|
|
2023-11-19 19:50:23 +01:00
|
|
|
$this->transformers[ $transformer_name ] = $transformer_instance;
|
2023-11-18 13:40:27 +01:00
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
2023-11-18 15:00:28 +01:00
|
|
|
* Init transformers.
|
2023-11-18 13:40:27 +01:00
|
|
|
*
|
2023-11-18 15:00:28 +01:00
|
|
|
* Initialize ActivityPub transformer manager.
|
|
|
|
*
|
|
|
|
* Include the builtin transformers by default and add third party ones.
|
2023-11-18 13:40:27 +01:00
|
|
|
*
|
2023-11-18 15:00:28 +01:00
|
|
|
* @since version_number_transformer_management_placeholder
|
2023-11-18 13:40:27 +01:00
|
|
|
* @access private
|
2023-11-18 15:00:28 +01:00
|
|
|
*/
|
|
|
|
private function init_transformers() {
|
|
|
|
$builtin_transformers = [
|
|
|
|
'post'
|
|
|
|
];
|
|
|
|
|
2023-11-19 19:50:23 +01:00
|
|
|
$this->transformers = [];
|
2023-11-18 15:00:28 +01:00
|
|
|
|
|
|
|
foreach ( $builtin_transformers as $transformer_name ) {
|
2023-11-19 19:50:23 +01:00
|
|
|
include ACTIVITYPUB_PLUGIN_DIR . 'includes/transformer/class-' . $transformer_name . '.php';
|
2023-11-18 15:00:28 +01:00
|
|
|
|
2023-11-19 19:50:23 +01:00
|
|
|
$class_name = ucfirst( $transformer_name );
|
2023-11-18 15:00:28 +01:00
|
|
|
|
2023-11-19 19:50:23 +01:00
|
|
|
$class_name = '\Activitypub\\Transformer_' . $class_name;
|
2023-11-18 15:00:28 +01:00
|
|
|
|
|
|
|
$this->register( new $class_name() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* After the transformers are registered.
|
|
|
|
*
|
2023-11-19 19:50:23 +01:00
|
|
|
* Fires after the built-in Activitypub transformers are registered.
|
2023-11-18 15:00:28 +01:00
|
|
|
*
|
|
|
|
* @since version_number_transformer_management_placeholder
|
|
|
|
*
|
|
|
|
* @param Transformer_Manager $this The widgets manager.
|
|
|
|
*/
|
|
|
|
do_action( 'activitypub/transformers/register', $this );
|
2023-11-17 16:28:04 +01:00
|
|
|
}
|
|
|
|
|
2023-11-18 15:00:28 +01:00
|
|
|
|
2023-11-19 17:32:41 +01:00
|
|
|
/**
|
|
|
|
* Get available ActivityPub transformers.
|
|
|
|
*
|
2023-11-19 19:50:23 +01:00
|
|
|
* Retrieve the registered transformers list.
|
2023-11-19 17:32:41 +01:00
|
|
|
*
|
|
|
|
* @since 1.0.0
|
|
|
|
* @access public
|
|
|
|
*
|
2023-11-19 19:50:23 +01:00
|
|
|
* @param string $transformers Optional. Widget name. Default is null.
|
2023-11-19 17:32:41 +01:00
|
|
|
*
|
2023-11-19 19:50:23 +01:00
|
|
|
* @return Transformer_Base|Transformer_Base[]|null Registered transformers.
|
2023-11-19 17:32:41 +01:00
|
|
|
*/
|
2023-11-19 19:50:23 +01:00
|
|
|
public function get_transformers( $transformer = null ) {
|
|
|
|
if ( is_null( $this->transformers ) ) {
|
|
|
|
$this->init_transformers();
|
2023-11-19 17:32:41 +01:00
|
|
|
}
|
|
|
|
|
2023-11-19 19:50:23 +01:00
|
|
|
if ( null !== $transformer ) {
|
|
|
|
return isset( $this->transformers[ $transformer ] ) ? $this->transformers[ $transformer ] : null;
|
2023-11-19 17:32:41 +01:00
|
|
|
}
|
|
|
|
|
2023-11-19 19:50:23 +01:00
|
|
|
return $this->transformers;
|
2023-11-19 17:32:41 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2023-11-18 15:00:28 +01:00
|
|
|
}
|
|
|
|
|