From 043e0a37037d5a9ee1b13c6d4ec7c56782076c6a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Sun, 19 Nov 2023 17:32:41 +0100 Subject: [PATCH] add basic draft of the mapper setting --- includes/base/class-transformer-base.php | 24 +++- includes/class-admin.php | 44 +++++++ .../managers/class-transformers-manager.php | 25 ++++ includes/transformer/class-post.php | 2 +- templates/settings.php | 121 ++++++++++++++++++ 5 files changed, 214 insertions(+), 2 deletions(-) diff --git a/includes/base/class-transformer-base.php b/includes/base/class-transformer-base.php index 890462e..044ba27 100644 --- a/includes/base/class-transformer-base.php +++ b/includes/base/class-transformer-base.php @@ -20,9 +20,31 @@ abstract class Transformer_Base { * @return string[] An array containing all the supported post types. */ public function get_supported_post_types() { - return \get_post_types( ['public' => true], 'names' ); + return \get_post_types( array( 'public' => true ), 'names' ); } + /** + * Get the name of the plugin that registered the transformer. + * + * @see Forked from the WordPress elementor plugin. + * + * @since version_number_transformer_management_placeholder + * @return string Plugin name + */ + private function get_plugin_name_from_transformer_instance( $transformer ) { + $class_reflection = new \ReflectionClass( $transformer ); + + $plugin_basename = plugin_basename( $class_reflection->getFileName() ); + + $plugin_directory = strtok( $plugin_basename, '/' ); + + $plugins_data = get_plugins( '/' . $plugin_directory ); + $plugin_data = array_shift( $plugins_data ); + + return $plugin_data['Name'] ?? esc_html__( 'Unknown', 'activitypub' ); + } + + /** * Get the name used for registering the transformer with the ActivityPub plugin. * diff --git a/includes/class-admin.php b/includes/class-admin.php index cdf0a8c..dbafea2 100644 --- a/includes/class-admin.php +++ b/includes/class-admin.php @@ -169,6 +169,50 @@ class Admin { 'default' => array( 'post', 'pages' ), ) ); + + /** + * Flexible activation of post_types together with mapping ActivityPub transformers. + * + * If a post-type is not mapped to any ActivtiyPub transformer it means it is not activated + * for ActivityPub federation. + * + * @since version_number_transformer_management_placeholder + */ + register_setting( + 'activitypub', + 'activitypub_transformer_mapping', + array( + 'type' => 'array', + 'default' => array( + 'post' => 'note', + ), + 'show_in_rest' => array( + 'schema' => array( + 'type' => 'array', + 'items' => array( + 'type' => 'string', + ), + ), + ), + 'sanitize_callback' => function ( $value ) { + // Check if $value is an array + if (!is_array($value)) { + return array(); + } + $value_keys = array_keys( $value ); + + $all_public_post_types = \get_post_types( array( 'public' => true ), 'names' ); + + // Unset the keys that are missing in $keysToCheck + foreach ( array_diff( $value_keys, $all_public_post_types ) as $missing_key ) { + unset($value[$missing_key]); + } + // var_dump($value); + return $value; + + } + ) + ); \register_setting( 'activitypub', 'activitypub_blog_user_identifier', diff --git a/includes/managers/class-transformers-manager.php b/includes/managers/class-transformers-manager.php index 983e95a..941b09f 100644 --- a/includes/managers/class-transformers-manager.php +++ b/includes/managers/class-transformers-manager.php @@ -169,5 +169,30 @@ class Transformers_Manager { } + /** + * Get available ActivityPub transformers. + * + * Retrieve the registered widget types list. + * + * @since 1.0.0 + * @access public + * + * @param string $widget_name Optional. Widget name. Default is null. + * + * @return Widget_Base|Widget_Base[]|null Registered widget types. + */ + public function get_widget_types( $widget_name = null ) { + if ( is_null( $this->_widget_types ) ) { + $this->init_widgets(); + } + + if ( null !== $widget_name ) { + return isset( $this->_widget_types[ $widget_name ] ) ? $this->_widget_types[ $widget_name ] : null; + } + + return $this->_widget_types; + } + + } diff --git a/includes/transformer/class-post.php b/includes/transformer/class-post.php index 2117ce7..684d59c 100644 --- a/includes/transformer/class-post.php +++ b/includes/transformer/class-post.php @@ -14,10 +14,10 @@ use function Activitypub\site_supports_blocks; /** * WordPress Post Transformer - * * The Post Transformer is responsible for transforming a WP_Post object into different othe * Object-Types. * + * * Currently supported are: * * - Activitypub\Activity\Base_Object diff --git a/templates/settings.php b/templates/settings.php index 5ce90e1..95a3297 100644 --- a/templates/settings.php +++ b/templates/settings.php @@ -236,6 +236,127 @@ + + +
+

+ + + + + + + + +
+ + + + + true ), 'objects' ); + $transformer_mapping = \get_option( 'activitypub_transformer_mapping', array( 'default' => 'note' ) ); + + // DUMMY CODE for developing: + class Transformer { + protected $name; + protected $supported_post_types; + protected $label; + + public function __construct(string $name, $label, array $supported_post_types) { + $this->name = $name; + $this->label = $label; + $this->supported_post_types = $supported_post_types; + } + + public function get_name() { + return $this->name; + } + + public function get_label() { + return $this->label; + } + + public function get_settings_page() { + return 'https://event-federation.eu'; + } + + public function get_info_text() { + return 'this is some generic info'; + } + + public function supports_post_type($post_type) { + return in_array($post_type, $this->supported_post_types); + } + } + + $all_public_post_type_names = array_map(function ($object) { + return $object->name; + }, $all_public_post_types); + + $transformer1 = new Transformer('default', 'Built-In', $all_public_post_type_names); + $transformer2 = new Transformer('tribe', 'The Events Calendar', array('tribe_events')); + $transformer3 = new Transformer('generic_event', 'Event', array('tribe_events', 'custom_event')); + + $transformers = array($transformer1, $transformer2, $transformer3); + // END OF DUMMY CODE + ?> + + + + + + + + ' . htmlspecialchars($transformer->get_label()) . ''; + } + ?> + + + + '; + echo ''; + // Generate radio inputs for each transformer, considering support for the post type + foreach ($transformers as $transformer) { + $disabled_attribute = $transformer->supports_post_type( $post_type->name ) ? '' : ' disabled'; + $is_selected = ( is_array( $transformer_mapping ) && isset( $transformer_mapping[ $post_type->name ] ) && $transformer_mapping[ $post_type->name ] === $transformer->get_name() ) ? ' checked ' : ''; + echo ''; + } + + echo ''; + } + ?> + +
' . htmlspecialchars($post_type->label) . '
+ + +
+
+