wordpress-activitypub-event.../includes/class-settings.php

108 lines
3.2 KiB
PHP
Raw Permalink Normal View History

2024-08-27 21:23:33 +02:00
<?php
/**
* General settings class.
*
* This file contains the General class definition, which handles the "General" settings
* page for the ActivityPub Event Extension Plugin, providing options for configuring various general settings.
*
* @package Activitypub_Event_Extensions
* @since 1.0.0
*/
namespace Activitypub_Event_Extensions;
2024-08-27 21:23:33 +02:00
use Activitypub\Activity\Extended_Object\Event;
// Exit if accessed directly.
defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore
/**
* Class responsible for the ActivityPui Event Extension related Settings.
*
* Class responsible for the ActivityPui Event Extension related Settings.
*
* @since 1.0.0
*/
class Settings {
const SETTINGS_SLUG = 'activitypub-event-extensions';
/**
* 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
/**
* Register the settings for the ActivityPub Event Extensions plugin.
*
* @return void
2024-08-27 21:23:33 +02:00
*/
public static function register_settings(): void {
2024-08-27 21:23:33 +02:00
\register_setting(
'activitypub-event-extensions',
'activitypub_event_extensions_default_event_category',
array(
2024-09-02 14:32:02 +02:00
'type' => 'string',
'description' => \__( 'Define your own custom post template', 'activitypub' ),
'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(
'activitypub-event-extensions',
'activitypub_event_extensions_event_category_mappings',
array(
'type' => 'array',
'description' => \__( 'Define your own custom post template', 'activitypub' ),
'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-09-02 14:32:02 +02:00
/**
* Sanitize the target ActivityPub Event category.
*
* @param string $event_category The ActivityPUb event category.
*/
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.
*
* @return array An array that contains only valid mapping pairs.
2024-09-02 14:32:02 +02:00
*/
public static function sanitize_event_category_mappings( $event_category_mappings ): array {
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.
*/
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
}