2024-08-27 21:23:33 +02:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* General settings class.
|
|
|
|
*
|
|
|
|
* This file contains the General class definition, which handles the "General" settings
|
2024-12-05 16:16:30 +01:00
|
|
|
* page for the Event Bridge for ActivityPub Plugin, providing options for configuring various general settings.
|
2024-08-27 21:23:33 +02:00
|
|
|
*
|
2024-12-05 16:16:30 +01:00
|
|
|
* @package Event_Bridge_For_ActivityPub
|
2024-08-27 21:23:33 +02:00
|
|
|
* @since 1.0.0
|
|
|
|
*/
|
|
|
|
|
2024-12-05 16:16:30 +01:00
|
|
|
namespace Event_Bridge_For_ActivityPub;
|
2024-08-27 21:23:33 +02:00
|
|
|
|
|
|
|
// Exit if accessed directly.
|
|
|
|
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore
|
|
|
|
|
2024-09-28 13:14:10 +02:00
|
|
|
use Activitypub\Activity\Extended_Object\Event;
|
2024-12-15 22:25:28 +01:00
|
|
|
use Event_Bridge_For_ActivityPub\Integrations\Feature_Event_Sources;
|
2024-09-28 13:14:10 +02:00
|
|
|
|
2024-08-27 21:23:33 +02:00
|
|
|
/**
|
|
|
|
* Class responsible for the ActivityPui Event Extension related Settings.
|
|
|
|
*
|
|
|
|
* Class responsible for the ActivityPui Event Extension related Settings.
|
|
|
|
*
|
|
|
|
* @since 1.0.0
|
|
|
|
*/
|
|
|
|
class Settings {
|
2024-12-05 16:16:30 +01:00
|
|
|
const SETTINGS_SLUG = 'event-bridge-for-activitypub';
|
2024-08-27 21:23:33 +02:00
|
|
|
|
2024-09-11 00:38:21 +02:00
|
|
|
/**
|
|
|
|
* The default ActivityPub event category.
|
|
|
|
*
|
|
|
|
* @var string
|
|
|
|
*/
|
2024-09-02 14:32:02 +02:00
|
|
|
const DEFAULT_EVENT_CATEGORY = 'MEETING';
|
|
|
|
|
2024-08-27 21:23:33 +02:00
|
|
|
/**
|
2024-12-05 16:16:30 +01:00
|
|
|
* Register the settings for the Event Bridge for ActivityPub plugin.
|
2024-09-11 00:38:21 +02:00
|
|
|
*
|
|
|
|
* @return void
|
2024-08-27 21:23:33 +02:00
|
|
|
*/
|
2024-09-11 00:38:21 +02:00
|
|
|
public static function register_settings(): void {
|
2024-08-27 21:23:33 +02:00
|
|
|
\register_setting(
|
2024-12-05 16:16:30 +01:00
|
|
|
'event-bridge-for-activitypub',
|
|
|
|
'event_bridge_for_activitypub_default_event_category',
|
2024-08-27 21:23:33 +02:00
|
|
|
array(
|
2024-09-02 14:32:02 +02:00
|
|
|
'type' => 'string',
|
2024-12-05 16:16:30 +01:00
|
|
|
'description' => \__( 'Define your own custom post template', 'event-bridge-for-activitypub' ),
|
2024-09-02 14:32:02 +02:00
|
|
|
'show_in_rest' => true,
|
|
|
|
'default' => self::DEFAULT_EVENT_CATEGORY,
|
|
|
|
'sanitize_callback' => array( self::class, 'sanitize_mapped_event_category' ),
|
2024-08-27 21:23:33 +02:00
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
\register_setting(
|
2024-12-05 16:16:30 +01:00
|
|
|
'event-bridge-for-activitypub',
|
|
|
|
'event_bridge_for_activitypub_event_category_mappings',
|
2024-08-27 21:23:33 +02:00
|
|
|
array(
|
|
|
|
'type' => 'array',
|
2024-12-05 16:16:30 +01:00
|
|
|
'description' => \__( 'Define your own custom post template', 'event-bridge-for-activitypub' ),
|
2024-08-27 21:23:33 +02:00
|
|
|
'default' => array(),
|
2024-09-02 14:32:02 +02:00
|
|
|
'sanitize_callback' => array( self::class, 'sanitize_event_category_mappings' ),
|
2024-08-27 21:23:33 +02:00
|
|
|
)
|
|
|
|
);
|
2024-10-19 16:46:50 +02:00
|
|
|
|
|
|
|
\register_setting(
|
2024-12-05 16:16:30 +01:00
|
|
|
'event-bridge-for-activitypub',
|
|
|
|
'event_bridge_for_activitypub_initially_activated',
|
2024-10-19 16:46:50 +02:00
|
|
|
array(
|
|
|
|
'type' => 'boolean',
|
2024-12-05 16:16:30 +01:00
|
|
|
'description' => \__( 'Whether the plugin just got activated for the first time.', 'event-bridge-for-activitypub' ),
|
2024-10-19 16:46:50 +02:00
|
|
|
'default' => 1,
|
|
|
|
)
|
|
|
|
);
|
2024-12-06 18:27:07 +01:00
|
|
|
|
|
|
|
\register_setting(
|
|
|
|
'event-bridge-for-activitypub',
|
|
|
|
'event_bridge_for_activitypub_summary_type',
|
|
|
|
array(
|
|
|
|
'type' => 'string',
|
|
|
|
'description' => \__( 'Summary type to use for ActivityStreams', 'event-bridge-for-activitypub' ),
|
|
|
|
'show_in_rest' => true,
|
|
|
|
'default' => 'preset',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
|
|
|
\register_setting(
|
|
|
|
'event-bridge-for-activitypub',
|
|
|
|
'event_bridge_for_activitypub_custom_summary',
|
|
|
|
array(
|
|
|
|
'type' => 'string',
|
|
|
|
'description' => \__( 'Define your own custom summary template for events', 'event-bridge-for-activitypub' ),
|
|
|
|
'show_in_rest' => true,
|
|
|
|
'default' => EVENT_BRIDGE_FOR_ACTIVITYPUB_CUSTOM_SUMMARY,
|
|
|
|
)
|
|
|
|
);
|
2024-12-08 17:38:05 +01:00
|
|
|
|
|
|
|
\register_setting(
|
2024-12-10 19:34:15 +01:00
|
|
|
'event-bridge-for-activitypub',
|
2024-12-08 17:38:05 +01:00
|
|
|
'event_bridge_for_activitypub_event_sources_active',
|
|
|
|
array(
|
2024-12-09 18:16:07 +01:00
|
|
|
'type' => 'boolean',
|
|
|
|
'show_in_rest' => true,
|
|
|
|
'description' => \__( 'Whether the event sources feature is activated.', 'event-bridge-for-activitypub' ),
|
|
|
|
'default' => 0,
|
2024-12-08 17:38:05 +01:00
|
|
|
)
|
|
|
|
);
|
2024-12-08 22:36:40 +01:00
|
|
|
|
2024-12-15 09:52:20 +01:00
|
|
|
\register_setting(
|
|
|
|
'event-bridge-for-activitypub',
|
|
|
|
'event_bridge_for_activitypub_event_source_cache_retention',
|
|
|
|
array(
|
|
|
|
'type' => 'int',
|
|
|
|
'show_in_rest' => true,
|
|
|
|
'description' => \__( 'The cache retention period for external event sources.', 'event-bridge-for-activitypub' ),
|
|
|
|
'default' => WEEK_IN_SECONDS,
|
|
|
|
'sanitize_callback' => 'absint',
|
|
|
|
)
|
|
|
|
);
|
|
|
|
|
2024-12-08 22:36:40 +01:00
|
|
|
\register_setting(
|
2024-12-10 19:34:15 +01:00
|
|
|
'event-bridge-for-activitypub',
|
2024-12-15 23:55:13 +01:00
|
|
|
'event_bridge_for_activitypub_integration_used_for_event_sources_feature',
|
2024-12-08 22:36:40 +01:00
|
|
|
array(
|
2024-12-23 21:23:03 +01:00
|
|
|
'type' => 'string',
|
2024-12-08 22:36:40 +01:00
|
|
|
'description' => \__( 'Define which plugin/integration is used for the event sources feature', 'event-bridge-for-activitypub' ),
|
|
|
|
'default' => array(),
|
2024-12-15 23:55:13 +01:00
|
|
|
'sanitize_callback' => array( self::class, 'sanitize_event_plugin_integration_used_for_event_sources' ),
|
2024-12-08 22:36:40 +01:00
|
|
|
)
|
|
|
|
);
|
2024-12-10 19:34:15 +01:00
|
|
|
|
|
|
|
\register_setting(
|
|
|
|
'event-bridge-for-activitypub-event-sources',
|
|
|
|
'event_bridge_for_activitypub_event_sources',
|
|
|
|
array(
|
|
|
|
'type' => 'array',
|
|
|
|
'description' => \__( 'Dummy setting', 'event-bridge-for-activitypub' ),
|
|
|
|
'default' => array(),
|
|
|
|
'sanitize_callback' => 'is_array',
|
|
|
|
)
|
|
|
|
);
|
2024-12-08 22:36:40 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sanitize the option which event plugin.
|
|
|
|
*
|
2024-12-15 23:55:13 +01:00
|
|
|
* @param string $event_plugin_integration The setting.
|
2024-12-08 22:36:40 +01:00
|
|
|
* @return string
|
|
|
|
*/
|
2024-12-15 23:55:13 +01:00
|
|
|
public static function sanitize_event_plugin_integration_used_for_event_sources( $event_plugin_integration ) {
|
|
|
|
if ( ! is_string( $event_plugin_integration ) ) {
|
2024-12-08 22:36:40 +01:00
|
|
|
return '';
|
|
|
|
}
|
|
|
|
$setup = Setup::get_instance();
|
|
|
|
$active_event_plugins = $setup->get_active_event_plugins();
|
|
|
|
|
|
|
|
$valid_options = array();
|
|
|
|
foreach ( $active_event_plugins as $active_event_plugin ) {
|
2024-12-15 22:25:28 +01:00
|
|
|
if ( $active_event_plugin instanceof Feature_Event_Sources ) {
|
2024-12-15 23:55:13 +01:00
|
|
|
$valid_options[] = $active_event_plugin::class;
|
2024-12-08 22:36:40 +01:00
|
|
|
}
|
|
|
|
}
|
2024-12-15 23:55:13 +01:00
|
|
|
if ( in_array( $event_plugin_integration, $valid_options, true ) ) {
|
|
|
|
return $event_plugin_integration;
|
2024-12-08 22:36:40 +01:00
|
|
|
}
|
2024-12-15 23:55:13 +01:00
|
|
|
return Setup::get_default_integration_class_name_used_for_event_sources_feature();
|
2024-08-27 21:23:33 +02:00
|
|
|
}
|
2024-09-02 14:32:02 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Sanitize the target ActivityPub Event category.
|
|
|
|
*
|
|
|
|
* @param string $event_category The ActivityPUb event category.
|
|
|
|
*/
|
2024-09-11 00:38:21 +02:00
|
|
|
public static function sanitize_mapped_event_category( $event_category ): string {
|
2024-09-02 14:32:02 +02:00
|
|
|
return self::is_allowed_event_category( $event_category ) ? $event_category : self::DEFAULT_EVENT_CATEGORY;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sanitization function for the event category mapping setting.
|
|
|
|
*
|
|
|
|
* Currently only the default event categories are allowed to be target of a mapping.
|
|
|
|
*
|
|
|
|
* @param array $event_category_mappings The settings value.
|
2024-09-11 00:38:21 +02:00
|
|
|
*
|
|
|
|
* @return array An array that contains only valid mapping pairs.
|
2024-09-02 14:32:02 +02:00
|
|
|
*/
|
2024-09-11 00:38:21 +02:00
|
|
|
public static function sanitize_event_category_mappings( $event_category_mappings ): array {
|
2024-09-10 18:48:02 +02:00
|
|
|
if ( empty( $event_category_mappings ) ) {
|
|
|
|
return array();
|
|
|
|
}
|
2024-09-02 14:32:02 +02:00
|
|
|
foreach ( $event_category_mappings as $taxonomy_slug => $event_category ) {
|
|
|
|
if ( ! self::is_allowed_event_category( $event_category ) ) {
|
|
|
|
unset( $event_category_mappings[ $taxonomy_slug ] );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $event_category_mappings;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Checks if the given event category is allowed to be target of a mapping.
|
|
|
|
*
|
|
|
|
* @param string $event_category The event category to check.
|
|
|
|
*
|
|
|
|
* @return bool True if allowed, false otherwise.
|
|
|
|
*/
|
2024-09-11 00:38:21 +02:00
|
|
|
private static function is_allowed_event_category( $event_category ): bool {
|
2024-09-02 14:32:02 +02:00
|
|
|
$allowed_event_categories = Event::DEFAULT_EVENT_CATEGORIES;
|
|
|
|
return in_array( $event_category, $allowed_event_categories, true );
|
|
|
|
}
|
2024-08-27 21:23:33 +02:00
|
|
|
}
|