refactoring the settings sanitation

This commit is contained in:
André Menrath 2024-09-02 14:32:02 +02:00
parent 812cbca546
commit 04dad65bfa
2 changed files with 47 additions and 16 deletions

View file

@ -9,7 +9,7 @@
* Text Domain: activitypub-event-extensions
* License: AGPL-3.0-or-later
*
* ActivityPub tested up to: 2.4.0
* ActivityPub tested up to: 3.1.0
*
* @package activitypub-event-extensions
* @license AGPL-3.0-or-later
@ -23,6 +23,7 @@ define( 'ACTIVITYPUB_EVENT_EXTENSIONS_PLUGIN_BASENAME', plugin_basename( __FILE_
define( 'ACTIVITYPUB_EVENT_EXTENSIONS_PLUGIN_FILE', plugin_dir_path( __FILE__ ) . '/' . basename( __FILE__ ) );
define( 'ACTIVITYPUB_EVENT_EXTENSIONS_PLUGIN_URL', plugin_dir_url( __FILE__ ) );
define( 'ACTIVITYPUB_EVENT_EXTENSIONS_PLUGIN_VERSION', current( get_file_data( __FILE__, array( 'Version' ), 'plugin' ) ) );
define( 'ACTIVITYPUB_EVENT_EXTENSIONS_DOMAIN', 'activitypub-event-extensions' );
// Include and register the autoloader class for automatic loading of plugin classes.
require_once ACTIVITYPUB_EVENT_EXTENSIONS_PLUGIN_DIR . '/includes/class-autoloader.php';

View file

@ -24,10 +24,10 @@ defined( 'ABSPATH' ) || exit; // @codeCoverageIgnore
* @since 1.0.0
*/
class Settings {
const STATIC = 'Activitypub_Event_Extensions\Admin\Settings_Page';
const SETTINGS_SLUG = 'activitypub-event-extensions';
const DEFAULT_EVENT_CATEGORY = 'MEETING';
/**
* Register the settings for the ActivityPub Event Extensions plugin.
*/
@ -39,7 +39,8 @@ class Settings {
'type' => 'string',
'description' => \__( 'Define your own custom post template', 'activitypub' ),
'show_in_rest' => true,
'default' => 'MEETING',
'default' => self::DEFAULT_EVENT_CATEGORY,
'sanitize_callback' => array( self::class, 'sanitize_mapped_event_category' ),
)
);
@ -50,16 +51,45 @@ class Settings {
'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;
},
'sanitize_callback' => array( self::class, 'sanitize_event_category_mappings' ),
)
);
}
/**
* Sanitize the target ActivityPub Event category.
*
* @param string $event_category The ActivityPUb event category.
*/
public static function sanitize_mapped_event_category( $event_category ) {
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.
*/
public static function sanitize_event_category_mappings( $event_category_mappings ) {
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 ) {
$allowed_event_categories = Event::DEFAULT_EVENT_CATEGORIES;
return in_array( $event_category, $allowed_event_categories, true );
}
}