From 02fd6af960d2db6bcc6b77d3612ffa404394e080 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Andr=C3=A9=20Menrath?= Date: Tue, 27 Aug 2024 21:23:33 +0200 Subject: [PATCH] wip on category mapping --- .../activitypub/transformer/class-event.php | 24 ++++ includes/admin/class-settings-page.php | 117 ++++++--------- includes/admin/class-settings.php | 65 +++++++++ includes/class-setup.php | 5 +- templates/admin-header.php | 21 --- templates/settings-extractor.php | 13 -- templates/settings-general.php | 13 -- templates/settings.php | 134 ++++++++++++++++++ 8 files changed, 271 insertions(+), 121 deletions(-) create mode 100644 includes/admin/class-settings.php delete mode 100644 templates/admin-header.php delete mode 100644 templates/settings-extractor.php delete mode 100644 templates/settings-general.php create mode 100644 templates/settings.php diff --git a/includes/activitypub/transformer/class-event.php b/includes/activitypub/transformer/class-event.php index e1ff34d..569e194 100644 --- a/includes/activitypub/transformer/class-event.php +++ b/includes/activitypub/transformer/class-event.php @@ -16,8 +16,19 @@ use function Activitypub\get_rest_url_by_path; /** * Base transformer for WordPress event post types to ActivityPub events. + * + * Everything that transforming several WordPress post types that represent events + * have in common, as well as sane defaults for events should be defined here. */ class Event extends Post { + + /** + * The WordPress event taxonomy. + * + * @var string + */ + protected $wp_taxonomy; + /** * Returns the User-URL of the Author of the Post. * @@ -41,6 +52,19 @@ class Event extends Post { return 'Event'; } + /** + * Set the event category, via the mapping setting. + */ + public function get_category() { + $current_category_mapping = \get_option( 'activitypub_event_extensions_event_category_mappings', array() ); + $terms = \get_the_terms( $this->wp_object, $this->wp_taxonomy ); + if ( ! is_wp_error( $terms ) && $terms ) { + return $current_category_mapping[ $terms[0]->slug ]; + } else { + return \get_option( 'activitypub_event_extensions_default_event_category', 'MEETING' ); + } + } + /** * Generic function that converts an WP-Event object to an ActivityPub-Event object. * diff --git a/includes/admin/class-settings-page.php b/includes/admin/class-settings-page.php index 7d7bb59..2cd16dd 100644 --- a/includes/admin/class-settings-page.php +++ b/includes/admin/class-settings-page.php @@ -11,43 +11,29 @@ namespace Activitypub_Event_Extensions\Admin; +use Activitypub_Event_Extensions\Setup; + // Exit if accessed directly. defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore /** - * Class responsible for Event Plugin related admin notices. + * Class responsible for the ActivityPui Event Extension related Settings. * - * Notices for guiding to proper configuration of ActivityPub with event plugins. + * Class responsible for the ActivityPui Event Extension related Settings. * * @since 1.0.0 */ class Settings_Page { - - /** - * TODO: - * - [ ] create settings page - * - [ ] skeleton - * - [ ] Autoloader - * - [ ] Common settings? - * - [ ] Hook points - * - [ ] let transformers hook settings into the page - * - [ ] provide setting-type-classes for hooks - * - [ ] True/False - * - [ ] Number - * - [ ] advanced for mapping - */ - const STATIC = 'Activitypub_Event_Extensions\Admin\Settings_Page'; - const SETTINGS_SLUG = 'activitypub-events'; - + const SETTINGS_SLUG = 'activitypub-event-extensions'; /** * Warning if the plugin is Active and the ActivityPub plugin is not. */ public static function admin_menu() { \add_options_page( 'Activitypub Event Extension', - 'Activitypub Events', + __( 'ActivityPub Events', 'activitypub_event_extensions' ), 'manage_options', self::SETTINGS_SLUG, array( self::STATIC, 'settings_page' ) @@ -70,59 +56,46 @@ class Settings_Page { ); } - public static function settings_page() { - if ( empty( $_GET['tab'] ) ) { - $tab = 'general'; + /** + * Receive the event categories (terms) used by the event plugin. + * + * @param array $event_plugin Contains info about a certain event plugin. + * + * @return array An array of Terms. + */ + private static function get_event_terms( $event_plugin ) { + if ( isset( $event_plugin['taxonomy'] ) ) { + $event_terms = get_terms( + array( + 'taxonomy' => $event_plugin['taxonomy'], + 'hide_empty' => true, + ) + ); + return $event_terms; } else { - $tab = sanitize_key( $_GET['tab'] ); - } - - /* - submenu_options = { - tab => {name => '' - active => true|false} - } - */ - - // TODO: generate this somehow. - // Maybe with filters, similar as with the settings! - $submenu_options = array( - 'general' => array( - 'name' => 'General', - 'active' => false, - ), - 'events_manager' => array( - 'name' => 'Events Manager', - 'active' => false, - ), - 'gatherpress' => array( - 'name' => 'Gatherpress', - 'active' => false, - ), - 'the_events_calendar' => array( - 'name' => 'The Events Calendar', - 'active' => false, - ), - 'vsel' => array( - 'name' => 'VS Event', - 'active' => false, - ), - ); - - $submenu_options[ $tab ]['active'] = true; - - $args = array( - 'slug' => self::SETTINGS_SLUG, - 'options' => $submenu_options, - ); - - switch ( $tab ) { - case 'general': - \load_template( ACTIVITYPUB_EVENT_EXTENSIONS_PLUGIN_DIR . 'templates/settings-general.php', true, $args ); - break; - default: - \load_template( ACTIVITYPUB_EVENT_EXTENSIONS_PLUGIN_DIR . 'templates/settings-extractor.php', true, $args ); - break; + return array(); } } + + /** + * Settings page. + */ + public static function settings_page() { + $plugin_setup = Setup::get_instance(); + + $event_plugins = $plugin_setup->get_active_event_plugins(); + + $event_terms = array(); + + foreach ( $event_plugins as $event_plugin_name => $events_plugin_info ) { + $event_terms[ $event_plugin_name ] = self::get_event_terms( $events_plugin_info ); + } + + $args = array( + 'slug' => self::SETTINGS_SLUG, + 'event_terms' => $event_terms, + ); + + \load_template( ACTIVITYPUB_EVENT_EXTENSIONS_PLUGIN_DIR . 'templates/settings.php', true, $args ); + } } diff --git a/includes/admin/class-settings.php b/includes/admin/class-settings.php new file mode 100644 index 0000000..4b79127 --- /dev/null +++ b/includes/admin/class-settings.php @@ -0,0 +1,65 @@ + 'string', + 'description' => \__( 'Define your own custom post template', 'activitypub' ), + 'show_in_rest' => true, + 'default' => 'MEETING', + ) + ); + + \register_setting( + 'activitypub-event-extensions', + 'activitypub_event_extensions_event_category_mappings', + array( + 'type' => 'array', + 'description' => \__( 'Define your own custom post template', 'activitypub' ), + 'default' => array(), + 'sanitize_callback' => function ( $event_category_mappings ) { + $allowed_mappings = Event::DEFAULT_EVENT_CATEGORIES; + foreach ( $event_category_mappings as $key => $value ) { + if ( ! in_array( $value, $allowed_mappings, true ) ) { + unset( $event_category_mappings[ $key ] ); + } + } + return $event_category_mappings; + }, + ) + ); + } +} diff --git a/includes/class-setup.php b/includes/class-setup.php index 5a8fa14..207cdb5 100644 --- a/includes/class-setup.php +++ b/includes/class-setup.php @@ -135,17 +135,18 @@ class Setup { register_activation_hook( ACTIVITYPUB_EVENT_EXTENSIONS_PLUGIN_FILE, array( $this, 'activate' ) ); add_action( 'admin_init', array( $this, 'do_admin_notices' ) ); + add_action( 'admin_init', array( Settings::class, 'register_settings' ) ); // If we don't have any active event plugins, or the ActivityPub plugin is not enabled, abort here. if ( empty( $this->active_event_plugins ) || ! $this->activitypub_plugin_is_active ) { return; } - add_action( 'admin_menu', array( Settings_Page::STATIC, 'admin_menu' ) ); + add_action( 'admin_menu', array( Settings_Page::class, 'admin_menu' ) ); add_filter( 'plugin_action_links_' . ACTIVITYPUB_EVENT_EXTENSIONS_PLUGIN_BASENAME, - array( Settings_Page::STATIC, 'settings_link' ) + array( Settings_Page::class, 'settings_link' ) ); add_filter( 'activitypub_transformer', array( $this, 'register_activitypub_event_transformer' ), 10, 3 ); } diff --git a/templates/admin-header.php b/templates/admin-header.php deleted file mode 100644 index a201095..0000000 --- a/templates/admin-header.php +++ /dev/null @@ -1,21 +0,0 @@ - -
-
-

-
- - -
-
diff --git a/templates/settings-extractor.php b/templates/settings-extractor.php deleted file mode 100644 index ac089a5..0000000 --- a/templates/settings-extractor.php +++ /dev/null @@ -1,13 +0,0 @@ - \ No newline at end of file diff --git a/templates/settings-general.php b/templates/settings-general.php deleted file mode 100644 index ac089a5..0000000 --- a/templates/settings-general.php +++ /dev/null @@ -1,13 +0,0 @@ - \ No newline at end of file diff --git a/templates/settings.php b/templates/settings.php new file mode 100644 index 0000000..6e0a6cb --- /dev/null +++ b/templates/settings.php @@ -0,0 +1,134 @@ + 'tribe_events_cat', + 'hide_empty' => true, + ) +); + +$default_event_category_strings = array( + 'ARTS' => __( 'Arts', 'activitypub-event-extensions' ), + 'BOOK_CLUBS' => __( 'Book clubs', 'activitypub-event-extensions' ), + 'BUSINESS' => __( 'Business', 'activitypub-event-extensions' ), + 'CAUSES' => __( 'Causes', 'activitypub-event-extensions' ), + 'COMEDY' => __( 'Comedy', 'activitypub-event-extensions' ), + 'CRAFTS' => __( 'Crafts', 'activitypub-event-extensions' ), + 'FOOD_DRINK' => __( 'Food & Drink', 'activitypub-event-extensions' ), + 'HEALTH' => __( 'Health', 'activitypub-event-extensions' ), + 'MUSIC' => __( 'Music', 'activitypub-event-extensions' ), + 'AUTO_BOAT_AIR' => __( 'Auto, boat and air', 'activitypub-event-extensions' ), + 'COMMUNITY' => __( 'Community', 'activitypub-event-extensions' ), + 'FAMILY_EDUCATION' => __( 'Family & Education', 'activitypub-event-extensions' ), + 'FASHION_BEAUTY' => __( 'Fashion & Beauty', 'activitypub-event-extensions' ), + 'FILM_MEDIA' => __( 'Film & Media', 'activitypub-event-extensions' ), + 'GAMES' => __( 'Games', 'activitypub-event-extensions' ), + 'LANGUAGE_CULTURE' => __( 'Language & Culture', 'activitypub-event-extensions' ), + 'LEARNING' => __( 'Learning', 'activitypub-event-extensions' ), + 'LGBTQ' => __( 'LGBTQ', 'activitypub-event-extensions' ), + 'MOVEMENTS_POLITICS' => __( 'Movements and politics', 'activitypub-event-extensions' ), + 'NETWORKING' => __( 'Networking', 'activitypub-event-extensions' ), + 'PARTY' => __( 'Party', 'activitypub-event-extensions' ), + 'PERFORMING_VISUAL_ARTS' => __( 'Performing & Visual Arts', 'activitypub-event-extensions' ), + 'PETS' => __( 'Pets', 'activitypub-event-extensions' ), + 'PHOTOGRAPHY' => __( 'Photography', 'activitypub-event-extensions' ), + 'OUTDOORS_ADVENTURE' => __( 'Outdoors & Adventure', 'activitypub-event-extensions' ), + 'SPIRITUALITY_RELIGION_BELIEFS' => __( 'Spirituality, Religion & Beliefs', 'activitypub-event-extensions' ), + 'SCIENCE_TECH' => __( 'Science & Tech', 'activitypub-event-extensions' ), + 'SPORTS' => __( 'Sports', 'activitypub-event-extensions' ), + 'THEATRE' => __( 'Theatre', 'activitypub-event-extensions' ), + 'MEETING' => __( 'Meeting', 'activitypub-event-extensions' ), // Default value in federation. + 'DEFAULT' => __( 'Default', 'activitypub-event-extensions' ), // Internal default for overrides. +); + +$selected_default_event_category = \get_option( 'activitypub_event_extensions_default_event_category', 'MEETING' ); +$current_category_mapping = \get_option( 'activitypub_event_extensions_event_category_mappings', array() ); +?> + +
+
+ + +
+ +

+ +

+ + + + + + +
+ +
+ +

+ +

+ + + + + + + + +
name ); ?> + +
+
+ +
+