require_files(); } /** * Require files. * * Require ActivityPub transformer base class. * * @since version_number_transformer_management_placeholder * @access private */ private function require_files() { require ACTIVITYPUB_PLUGIN_DIR . 'includes/class-transformer-base.php'; } /** * 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 * * @param \ActivityPub\Transformer_Base $transformer_instance ActivityPub Transformer. * * @return bool True if the ActivityPub transformer was registered. */ public function register( Transformer_Base $transformer_instance) { 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' ); return false; } $transformer_name = $transformer_instance->get_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; } /** * 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 */ private function init_transformers() { $builtin_transformers = [ 'post' ]; $this->transformers = []; foreach ( $builtin_transformers as $transformer_name ) { include ACTIVITYPUB_PLUGIN_DIR . 'includes/transformer/class-' . $transformer_name . '.php'; $class_name = ucfirst( $transformer_name ); $class_name = '\Activitypub\Transformer_' . $class_name; $this->register( new $class_name() ); } /** * After the transformers are registered. * * Fires after the built-in Activitypub transformers are registered. * * @since version_number_transformer_management_placeholder * * @param Transformers_Manager $this The widgets manager. */ do_action( 'activitypub/transformers/register', $this ); } /** * Get available ActivityPub transformers. * * Retrieve the registered transformers list. * * @since 1.0.0 * @access public * * @param string $transformers Optional. Widget name. Default is null. * * @return Transformer_Base|Transformer_Base[]|null Registered transformers. */ public function get_transformers( $transformer = null ) { if ( is_null( $this->transformers ) ) { $this->init_transformers(); } if ( null !== $transformer ) { return isset( $this->transformers[ $transformer ] ) ? $this->transformers[ $transformer ] : null; } return $this->transformers; } /** * Get the right ActivityPub transformer. * * @since 1.0.0 * @access public * * @param WP_Post $wp_post The WordPress Post. * * @return Transformer_Base|null Registered transformers. */ public function get_transformer( $wp_post ) { if ( is_null( $this->transformers ) ) { $this->init_transformers(); } if ( null !== $transformer ) { return isset( $this->transformers[ $transformer ] ) ? $this->transformers[ $transformer ] : null; } $post_type = get_post_type( $wp_post ); $transformer_mapping = \get_option( 'activitypub_transformer_mapping', array( 'post' => 'activitypub/default', 'page' => 'activitypub/default' ) ) ? \get_option( 'activitypub_transformer_mapping', array( 'post' => 'activitypub/default', 'page' => 'activitypub/default' ) ) : array(); $transformer_name = $transformer_mapping[ $post_type ]; return new $this->transformers[ $transformer_name ]; } }