2023-11-20 18:15:54 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* Inspired by the way elementor handles addons.
|
|
|
|
*
|
|
|
|
* @link https://github.com/elementor/elementor/
|
2023-11-22 13:59:39 +01:00
|
|
|
* @package Activitypub
|
2023-11-20 18:15:54 +01:00
|
|
|
*/
|
|
|
|
|
2023-11-22 13:59:39 +01:00
|
|
|
namespace Activitypub\Transformer;
|
2023-11-20 18:15:54 +01:00
|
|
|
|
|
|
|
use WP_Post;
|
|
|
|
use WP_Comment;
|
|
|
|
|
|
|
|
use function Activitypub\camel_to_snake_case;
|
|
|
|
use function Activitypub\snake_to_camel_case;
|
|
|
|
|
|
|
|
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
|
|
|
|
*/
|
2023-11-30 08:36:05 +01:00
|
|
|
class Transformer_Factory {
|
2023-11-20 19:36:45 +01:00
|
|
|
const DEFAULT_TRANSFORMER_MAPPING = array(
|
2023-11-20 23:30:33 +01:00
|
|
|
'post' => ACTIVITYPUB_DEFAULT_TRANSFORMER,
|
|
|
|
'page' => ACTIVITYPUB_DEFAULT_TRANSFORMER,
|
2023-11-20 19:36:45 +01:00
|
|
|
);
|
|
|
|
|
2023-11-20 18:15:54 +01:00
|
|
|
/**
|
|
|
|
* Transformers.
|
|
|
|
*
|
|
|
|
* Holds the list of all the ActivityPub transformers. Default is `null`.
|
|
|
|
*
|
|
|
|
* @since version_number_transformer_management_placeholder
|
|
|
|
* @access private
|
|
|
|
*
|
2023-11-22 13:59:39 +01:00
|
|
|
* @var \ActivityPub\Transformer\Base[]
|
2023-11-20 18:15:54 +01:00
|
|
|
*/
|
|
|
|
private $transformers = null;
|
|
|
|
|
|
|
|
/**
|
2023-11-20 23:35:52 +01:00
|
|
|
* Transformer_Manager instance.
|
2023-11-20 18:15:54 +01:00
|
|
|
*
|
|
|
|
* Holds the transformer instance.
|
|
|
|
*
|
|
|
|
* @since version_number_transformer_management_placeholder
|
|
|
|
* @access protected
|
|
|
|
*
|
2023-11-20 23:35:52 +01:00
|
|
|
* @var Transformer_Manager
|
2023-11-20 18:15:54 +01:00
|
|
|
*/
|
|
|
|
protected static $_instances = [];
|
|
|
|
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Instance.
|
|
|
|
*
|
|
|
|
* Ensures only one instance of the transformer manager class is loaded or can be loaded.
|
|
|
|
*
|
|
|
|
* @since version_number_transformer_management_placeholder
|
|
|
|
* @access public
|
|
|
|
* @static
|
|
|
|
*
|
2023-11-20 23:35:52 +01:00
|
|
|
* @return Transformer_Manager An instance of the class.
|
2023-11-20 18:15:54 +01:00
|
|
|
*/
|
|
|
|
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-22 14:24:25 +01:00
|
|
|
/**
|
2023-11-20 18:15:54 +01:00
|
|
|
* Transformers manager constructor.
|
|
|
|
*
|
|
|
|
* Initializing ActivityPub transformers manager.
|
|
|
|
*
|
|
|
|
* @since version_number_transformer_management_placeholder
|
|
|
|
* @access public
|
2023-11-22 13:59:39 +01:00
|
|
|
*/
|
2023-11-20 18:15:54 +01:00
|
|
|
public function __construct() {
|
|
|
|
$this->require_files();
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Require files.
|
|
|
|
*
|
|
|
|
* Require ActivityPub transformer base class.
|
|
|
|
*
|
|
|
|
* @since version_number_transformer_management_placeholder
|
|
|
|
* @access private
|
2023-11-22 13:59:39 +01:00
|
|
|
*/
|
2023-11-20 18:15:54 +01:00
|
|
|
private function require_files() {
|
2023-11-22 13:59:39 +01:00
|
|
|
require ACTIVITYPUB_PLUGIN_DIR . 'includes/transformer/class-base.php';
|
2023-11-20 18:15:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* 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 ] );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Register a transformer.
|
|
|
|
*
|
|
|
|
* @since version_number_transformer_management_placeholder
|
|
|
|
* @access public
|
|
|
|
*
|
2023-11-22 13:59:39 +01:00
|
|
|
* @param \ActivityPub\Transformer\Base $transformer_instance ActivityPub Transformer.
|
2023-11-20 18:15:54 +01:00
|
|
|
*
|
|
|
|
* @return bool True if the ActivityPub transformer was registered.
|
|
|
|
*/
|
2023-11-22 14:24:25 +01:00
|
|
|
public function register( \ActivityPub\Transformer\Base $transformer_instance ) {
|
2023-11-20 18:15:54 +01:00
|
|
|
|
2023-11-22 13:59:39 +01:00
|
|
|
if ( ! $transformer_instance instanceof \ActivityPub\Transformer\Base ) {
|
2023-11-20 18:15:54 +01:00
|
|
|
_doing_it_wrong(
|
|
|
|
__METHOD__,
|
2023-11-22 14:24:25 +01:00
|
|
|
\esc_html__( 'ActivityPub transformer instance must be a of \ActivityPub\Transformer_Base class.' ),
|
2023-11-20 18:15:54 +01:00
|
|
|
'version_number_transformer_management_placeholder'
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
2023-11-22 13:59:39 +01:00
|
|
|
|
2023-11-20 18:15:54 +01:00
|
|
|
$transformer_name = $transformer_instance->get_name();
|
|
|
|
if ( preg_match( '/[A-Z]+/', $transformer_name ) ) {
|
|
|
|
_doing_it_wrong(
|
|
|
|
__METHOD__,
|
2023-11-22 14:24:25 +01:00
|
|
|
\esc_html__( 'ActivityPub transformer names must not contain uppercase characters.' ),
|
2023-11-20 18:15:54 +01:00
|
|
|
'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__,
|
2023-11-22 14:24:25 +01:00
|
|
|
\esc_html__( 'ActivityPub transformer names must contain a namespace prefix. Example: my-plugin/my-custom-transformer' ),
|
2023-11-20 18:15:54 +01:00
|
|
|
'version_number_transformer_management_placeholder'
|
|
|
|
);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( $this->is_registered( $transformer_name ) ) {
|
|
|
|
_doing_it_wrong(
|
|
|
|
__METHOD__,
|
|
|
|
/* translators: %s: Block name. */
|
2023-11-22 14:24:25 +01:00
|
|
|
sprintf( 'ActivityPub transformer with name "%s" is already registered.', esc_html( $transformer_name ) ),
|
2023-11-20 18:15:54 +01:00
|
|
|
'version_number_transformer_management_placeholder'
|
|
|
|
);
|
|
|
|
return false;
|
2023-11-22 13:59:39 +01:00
|
|
|
}
|
2023-11-20 18:15:54 +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`.
|
2023-11-22 13:59:39 +01:00
|
|
|
* @param \ActivityPub\Transformer\Base $transformer_instance Widget instance.
|
2023-11-20 18:15:54 +01:00
|
|
|
*/
|
|
|
|
// 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 );
|
|
|
|
|
|
|
|
// if ( ! $should_register ) {
|
2023-11-22 14:24:25 +01:00
|
|
|
// return false;
|
2023-11-20 18:15:54 +01:00
|
|
|
// }
|
|
|
|
|
|
|
|
$this->transformers[ $transformer_name ] = $transformer_instance;
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Init transformers.
|
|
|
|
*
|
|
|
|
* Initialize ActivityPub transformer manager.
|
|
|
|
* Include the builtin transformers by default and add third party ones.
|
|
|
|
*
|
|
|
|
* @since version_number_transformer_management_placeholder
|
|
|
|
* @access private
|
2023-11-22 13:59:39 +01:00
|
|
|
*/
|
2023-11-20 18:15:54 +01:00
|
|
|
private function init_transformers() {
|
|
|
|
$builtin_transformers = [
|
2023-11-22 14:24:25 +01:00
|
|
|
'post',
|
2023-11-20 18:15:54 +01:00
|
|
|
];
|
|
|
|
|
|
|
|
$this->transformers = [];
|
|
|
|
|
|
|
|
foreach ( $builtin_transformers as $transformer_name ) {
|
|
|
|
$class_name = ucfirst( $transformer_name );
|
|
|
|
|
2023-11-22 13:59:39 +01:00
|
|
|
$class_name = '\Activitypub\Transformer\\' . $class_name;
|
2023-11-20 18:15:54 +01:00
|
|
|
|
|
|
|
$this->register( new $class_name() );
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Let other transformers register.
|
|
|
|
*
|
|
|
|
* Fires after the built-in Activitypub transformers are registered.
|
|
|
|
*
|
|
|
|
* @since version_number_transformer_management_placeholder
|
|
|
|
*
|
2023-11-30 08:36:05 +01:00
|
|
|
* @param Transformer_Factory $this The widgets manager.
|
2023-11-20 18:15:54 +01:00
|
|
|
*/
|
2023-11-22 14:58:06 +01:00
|
|
|
do_action( 'activitypub_transformers_register', $this );
|
2023-11-20 18:15:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get available ActivityPub transformers.
|
|
|
|
*
|
|
|
|
* Retrieve the registered transformers list. If given a transformer name
|
|
|
|
* it returns the given transformer if it is registered.
|
|
|
|
*
|
|
|
|
* @since version_number_transformer_management_placeholder
|
|
|
|
* @access public
|
|
|
|
*
|
2023-11-25 10:38:22 +01:00
|
|
|
* @param string $transformer_name Optional. Transformer name. Default is null.
|
2023-11-20 18:15:54 +01:00
|
|
|
*
|
2023-11-22 13:59:39 +01:00
|
|
|
* @return Base|Base[]|null Registered transformers.
|
2023-11-25 10:38:22 +01:00
|
|
|
*/
|
2023-11-20 18:15:54 +01:00
|
|
|
public function get_transformers( $transformer_name = null ) {
|
|
|
|
if ( is_null( $this->transformers ) ) {
|
|
|
|
$this->init_transformers();
|
|
|
|
}
|
|
|
|
|
|
|
|
if ( null !== $transformer_name ) {
|
|
|
|
return isset( $this->transformers[ $transformer_name ] ) ? $this->transformers[ $transformer_name ] : null;
|
|
|
|
}
|
|
|
|
|
|
|
|
return $this->transformers;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Get the mapped ActivityPub transformer.
|
2023-11-22 13:59:39 +01:00
|
|
|
*
|
2023-11-20 19:36:45 +01:00
|
|
|
* Returns a new instance of the needed WordPress to ActivityPub transformer.
|
2023-11-20 18:15:54 +01:00
|
|
|
*
|
|
|
|
* @since version_number_transformer_management_placeholder
|
|
|
|
* @access public
|
|
|
|
*
|
2023-11-29 23:46:13 +01:00
|
|
|
* @param WP_Post|WP_Comment $wp_object The WordPress Post/Comment.
|
2023-11-20 18:15:54 +01:00
|
|
|
*
|
2023-11-22 13:59:39 +01:00
|
|
|
* @return \ActivityPub\Transformer\Base|null Registered transformers.
|
2023-11-25 10:38:22 +01:00
|
|
|
*/
|
2023-11-29 23:46:13 +01:00
|
|
|
public function transform( $wp_object ) {
|
|
|
|
switch ( get_class( $wp_object ) ) {
|
2023-11-20 18:15:54 +01:00
|
|
|
case 'WP_Post':
|
2023-11-29 23:46:13 +01:00
|
|
|
$post_type = get_post_type( $wp_object );
|
2023-11-20 19:39:13 +01:00
|
|
|
$transformer_mapping = \get_option( 'activitypub_transformer_mapping', self::DEFAULT_TRANSFORMER_MAPPING );
|
2023-11-20 18:15:54 +01:00
|
|
|
$transformer_name = $transformer_mapping[ $post_type ];
|
2023-11-25 10:38:22 +01:00
|
|
|
$transformer_class = $this->get_transformers( $transformer_name );
|
|
|
|
$transformer_instance = new $transformer_class();
|
2023-11-29 23:46:13 +01:00
|
|
|
$transformer_instance->set_wp_post( $wp_object );
|
|
|
|
return $transformer_instance->transform();
|
2023-11-20 18:15:54 +01:00
|
|
|
case 'WP_Comment':
|
2023-11-29 23:46:13 +01:00
|
|
|
return new Comment( $wp_object );
|
2023-11-20 18:15:54 +01:00
|
|
|
default:
|
2023-11-29 23:46:13 +01:00
|
|
|
return apply_filters( 'activitypub_transformer', null, $wp_object, get_class( $wp_object ) );
|
2023-11-20 18:15:54 +01:00
|
|
|
}
|
|
|
|
}
|
2023-11-22 13:59:39 +01:00
|
|
|
}
|