wordpress-activitypub/includes/managers/class-transformers-manager.php

148 lines
3.9 KiB
PHP
Raw Normal View History

<?php
/**
* Inspired by the way elementor handles addons.
*
* @link https://github.com/elementor/elementor/
*/
namespace ActivityPub;
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`.
*
* @since 1.0.0
* @access private
*
2023-11-18 13:40:27 +01:00
* @var \ActivityPub\Transformer_Base[]
*/
2023-11-18 13:40:27 +01:00
private $transformers = array();
/**
* 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-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() {
require ACTIVITYPUB_PLUGIN_DIR . 'includes/base/class-transformer-base.php';
}
/**
* 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-18 13:40:27 +01:00
* @return bool True if the ActivityPub transformer was registered.
*/
2023-11-18 13:40:27 +01:00
public function register( Transformer_Base $transformer_instance) {
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-18 13:40:27 +01:00
return false;
}
2023-11-18 13:40:27 +01:00
$transformer_name = $transformer_instance->name;
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-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.
*/
$should_register = apply_filters( 'activitypub/transformers/is_transformer_enabled', true, $transformer_instance );
if ( ! $should_register ) {
return false;
}
$this->_transformers[ $transformer_name ] = $transformer_instance;
return true;
}
/**
* Register controls.
*
* This method creates a list of all the supported controls by requiring the
* control files and initializing each one of them.
*
* The list of supported controls includes the regular controls and the group
* controls.
*
* External developers can register new controls by hooking to the
* `elementor/controls/controls_registered` action.
*
* @since 3.1.0
* @access private
*/
private function register_controls() {
}
}