add basic draft of the mapper setting

This commit is contained in:
André Menrath 2023-11-19 17:32:41 +01:00
parent da3453a069
commit 043e0a3703
5 changed files with 214 additions and 2 deletions

View file

@ -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.
*

View file

@ -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',

View file

@ -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;
}
}

View file

@ -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

View file

@ -237,6 +237,127 @@
<?php \do_settings_fields( 'activitypub', 'activity' ); ?>
</div>
<!-- OUR FORK HERE -->
<div class="box">
<h3><?php \esc_html_e( 'Enable ActivityPub support for post type', 'activitypub' ); ?></h3>
<table class="form-table">
<tbody>
<tr>
<th scope="row">
<?php \esc_html_e( 'Mapping', 'activitypub' ); ?>
</th>
<td>
<?php \esc_html_e( 'Enable ActivityPub support for a certain post type by selecting one of the available ActivityPub transformers.', 'activitypub' ); ?>
<?php $all_public_post_types = \get_post_types( array( 'public' => 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
?>
<script>
document.addEventListener('DOMContentLoaded', function () {
var radioGroups = {};
var radioButtons = document.querySelectorAll('input[type="radio"]');
radioButtons.forEach(function (radioButton) {
radioButton.addEventListener('click', function () {
var name = this.name;
if (!radioGroups[name]) {
radioGroups[name] = this;
} else {
radioGroups[name].checked = false;
radioGroups[name] = this;
}
});
});
});
</script>
<table>
<thead>
<tr>
<th></th>
<?php
// Generate column headers based on transformer objects
foreach ($transformers as $transformer) {
echo '<th>' . htmlspecialchars($transformer->get_label()) . '</th>';
}
?>
</tr>
</thead>
<tbody>
<?php
// Generate rows based on post types and transformers
foreach ($all_public_post_types as $post_type) {
echo '<tr>';
echo '<td><strong>' . htmlspecialchars($post_type->label) . '</strong></td>';
// 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 '<td><input type="radio" name="activitypub_transformer_mapping[' . $post_type->name . ']" value="' . $transformer->get_name() . '"' . $is_selected . $disabled_attribute . '></td>';
}
echo '</tr>';
}
?>
</tbody>
</table>
</td>
</tr>
</tbody>
</table>
</div>
<!-- OUR FORK ENDS HERE -->
<div class="box">
<h3><?php \esc_html_e( 'Server', 'activitypub' ); ?></h3>
<table class="form-table">