This commit is contained in:
André Menrath 2023-11-18 13:40:27 +01:00
parent 23dfd11d39
commit 2bd6130eb1
4 changed files with 137 additions and 42 deletions

View file

@ -1,28 +0,0 @@
<?php
/**
* Inspired by the PHP ActivityPub Library by @Landrok
*
* @link https://github.com/landrok/activitypub
*/
namespace Activitypub\Activity;
use WP_Error;
use ReflectionClass;
use function Activitypub\camel_to_snake_case;
use function Activitypub\snake_to_camel_case;
/**
* Base_Object is an implementation of one of the
* Activity Streams Core Types.
*
* The Object is the primary base type for the Activity Streams
* vocabulary.
*
* Note: Object is a reserved keyword in PHP. It has been suffixed with
* 'Base_' for this reason.
*
* @see https://www.w3.org/TR/activitystreams-core/#object
*/
class Base_Object {

View file

@ -0,0 +1,44 @@
<?php
/**
* Inspired by the PHP ActivityPub Library by @Landrok
*
* @link https://github.com/landrok/activitypub
*/
namespace Activitypub\Activity;
use WP_Error;
use ReflectionClass;
use function Activitypub\camel_to_snake_case;
use function Activitypub\snake_to_camel_case;
/**
* Base class to implement WordPress to ActivityPub transformers.
*/
abstract class Transformer_Base {
/**
* Get the supported WP post_types that the transformer can use as an input.
*
* By default all post types are supported.
*
* @since version_number_transformer_management_placeholder
*/
public function get_supported_post_types() {
return \get_post_types( array(), 'names');
}
/**
* Get the name used for registering the transformer with the ActivityPub plugin.
*
* @since version_number_transformer_management_placeholder
*/
abstract public function get_name();
/**
* Get the display name for the ActivityPub transformer.
*
* @since version_number_transformer_management_placeholder
*/
abstract public function get_title();
}

View file

@ -3,6 +3,7 @@ namespace Activitypub;
use WP_User_Query; use WP_User_Query;
use Activitypub\Model\Blog_User; use Activitypub\Model\Blog_User;
use Activitypub\Base\Transformer_Base;
/** /**
* ActivityPub Admin Class * ActivityPub Admin Class
@ -22,6 +23,10 @@ class Admin {
if ( ! is_user_disabled( get_current_user_id() ) ) { if ( ! is_user_disabled( get_current_user_id() ) ) {
\add_action( 'show_user_profile', array( self::class, 'add_profile' ) ); \add_action( 'show_user_profile', array( self::class, 'add_profile' ) );
} }
add_filter( 'activitypub/transformers/is_transformer_enabled', function( $should_register, Transformer_Base $widget_instance ) {
return ! Options::is_element_disabled( $widget_instance->get_name() );
}, 10, 2 );
} }
/** /**

View file

@ -24,14 +24,14 @@ class Transformers_Manager {
/** /**
* Transformers. * Transformers.
* *
* Holds the list of all the transformers. Default is `null`. * Holds the list of all the ActivityPub transformers. Default is `null`.
* *
* @since 1.0.0 * @since 1.0.0
* @access private * @access private
* *
* @var Base_Control[] * @var \ActivityPub\Transformer_Base[]
*/ */
private $controls = null; private $transformers = array();
/** /**
* Transformers manager constructor. * Transformers manager constructor.
@ -43,8 +43,18 @@ class Transformers_Manager {
*/ */
public function __construct() { public function __construct() {
$this->require_files(); $this->require_files();
}
add_action( 'elementor/ajax/register_actions', [ $this, 'register_ajax_actions' ] ); /**
* 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';
} }
/** /**
@ -53,22 +63,86 @@ class Transformers_Manager {
* @since version_number_transformer_management_placeholder * @since version_number_transformer_management_placeholder
* @access public * @access public
* *
* @param \ActivityPub\Base_Transformer $transformer_instance ActivityPub Transformer. * @param \ActivityPub\Transformer_Base $transformer_instance ActivityPub Transformer.
* *
* @return bool True if the transformer was registered. * @return bool True if the ActivityPub transformer was registered.
*/ */
public function register( Base_Transformer $control_instance, $control_id = null ) { public function register( Transformer_Base $transformer_instance) {
// TODO: For BC. Remove in the future. if ( ! $transformer_instance instanceof Transformer_Base ) {
if ( $control_id ) { _doing_it_wrong(
Plugin::instance()->modules_manager->get_modules( 'dev-tools' )->deprecation->deprecated_argument( __METHOD__,
'$control_id', '3.5.0' __( 'ActivityPub transformer instance must be a implementation of the \ActivityPub\Transformer_Base class.' ),
'version_number_transformer_management_placeholder'
); );
} else { return false;
$control_id = $control_instance->get_type();
} }
$this->controls[ $control_id ] = $control_instance; $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;
}
/**
* 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() {
} }
} }