feat(03-01): create settings class with WordPress Settings API
- Settings class with singleton pattern - Registered 5 settings: receiver_email, captcha_provider, captcha_site_key, captcha_secret_key, thankyou_url - Proper sanitization callbacks for each setting type - Helper method get_option() for clean API access - Integrated with admin menu structure
This commit is contained in:
@@ -86,13 +86,8 @@ class Umzugsliste_Admin_Menu {
|
||||
* Settings page callback
|
||||
*/
|
||||
public function settings_page() {
|
||||
?>
|
||||
<div class="wrap">
|
||||
<h1>Umzugsliste Einstellungen</h1>
|
||||
<div class="notice notice-info">
|
||||
<p><?php esc_html_e( 'Einstellungen werden in Phase 3 implementiert.', 'umzugsliste' ); ?></p>
|
||||
</div>
|
||||
</div>
|
||||
<?php
|
||||
// Delegate to settings class
|
||||
$settings = Umzugsliste_Settings::get_instance();
|
||||
$settings->render_settings_page();
|
||||
}
|
||||
}
|
||||
|
||||
138
includes/class-settings.php
Normal file
138
includes/class-settings.php
Normal file
@@ -0,0 +1,138 @@
|
||||
<?php
|
||||
/**
|
||||
* Settings Management
|
||||
*/
|
||||
|
||||
// Exit if accessed directly
|
||||
if ( ! defined( 'ABSPATH' ) ) {
|
||||
exit;
|
||||
}
|
||||
|
||||
/**
|
||||
* Settings class
|
||||
*/
|
||||
class Umzugsliste_Settings {
|
||||
|
||||
/**
|
||||
* Single instance
|
||||
*/
|
||||
private static $instance = null;
|
||||
|
||||
/**
|
||||
* Get instance
|
||||
*/
|
||||
public static function get_instance() {
|
||||
if ( null === self::$instance ) {
|
||||
self::$instance = new self();
|
||||
}
|
||||
return self::$instance;
|
||||
}
|
||||
|
||||
/**
|
||||
* Constructor
|
||||
*/
|
||||
private function __construct() {
|
||||
add_action( 'admin_init', array( $this, 'register_settings' ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Register settings with WordPress Settings API
|
||||
*/
|
||||
public function register_settings() {
|
||||
// Register receiver email setting
|
||||
register_setting(
|
||||
'umzugsliste_settings',
|
||||
'umzugsliste_receiver_email',
|
||||
array(
|
||||
'type' => 'string',
|
||||
'sanitize_callback' => 'sanitize_email',
|
||||
'default' => '',
|
||||
)
|
||||
);
|
||||
|
||||
// Register captcha provider setting
|
||||
register_setting(
|
||||
'umzugsliste_settings',
|
||||
'umzugsliste_captcha_provider',
|
||||
array(
|
||||
'type' => 'string',
|
||||
'sanitize_callback' => 'sanitize_text_field',
|
||||
'default' => 'none',
|
||||
)
|
||||
);
|
||||
|
||||
// Register captcha site key setting
|
||||
register_setting(
|
||||
'umzugsliste_settings',
|
||||
'umzugsliste_captcha_site_key',
|
||||
array(
|
||||
'type' => 'string',
|
||||
'sanitize_callback' => array( $this, 'sanitize_api_key' ),
|
||||
'default' => '',
|
||||
)
|
||||
);
|
||||
|
||||
// Register captcha secret key setting
|
||||
register_setting(
|
||||
'umzugsliste_settings',
|
||||
'umzugsliste_captcha_secret_key',
|
||||
array(
|
||||
'type' => 'string',
|
||||
'sanitize_callback' => array( $this, 'sanitize_api_key' ),
|
||||
'default' => '',
|
||||
)
|
||||
);
|
||||
|
||||
// Register thank you URL setting
|
||||
register_setting(
|
||||
'umzugsliste_settings',
|
||||
'umzugsliste_thankyou_url',
|
||||
array(
|
||||
'type' => 'string',
|
||||
'sanitize_callback' => 'esc_url_raw',
|
||||
'default' => home_url(),
|
||||
)
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Sanitize API key (trim whitespace)
|
||||
*/
|
||||
public function sanitize_api_key( $value ) {
|
||||
return sanitize_text_field( trim( $value ) );
|
||||
}
|
||||
|
||||
/**
|
||||
* Render settings page
|
||||
*/
|
||||
public function render_settings_page() {
|
||||
// Check user capabilities
|
||||
if ( ! current_user_can( 'edit_posts' ) ) {
|
||||
return;
|
||||
}
|
||||
|
||||
?>
|
||||
<div class="wrap">
|
||||
<h1>Umzugsliste Einstellungen</h1>
|
||||
<form method="post" action="options.php">
|
||||
<?php
|
||||
settings_fields( 'umzugsliste_settings' );
|
||||
do_settings_sections( 'umzugsliste_settings' );
|
||||
submit_button();
|
||||
?>
|
||||
</form>
|
||||
</div>
|
||||
<?php
|
||||
}
|
||||
|
||||
/**
|
||||
* Get option helper method
|
||||
*
|
||||
* @param string $key Option key (without umzugsliste_ prefix)
|
||||
* @param mixed $default Default value
|
||||
* @return mixed Option value
|
||||
*/
|
||||
public static function get_option( $key, $default = '' ) {
|
||||
return get_option( 'umzugsliste_' . $key, $default );
|
||||
}
|
||||
}
|
||||
@@ -52,6 +52,7 @@ class Umzugsliste {
|
||||
private function load_dependencies() {
|
||||
require_once UMZUGSLISTE_PLUGIN_DIR . 'includes/class-cpt.php';
|
||||
require_once UMZUGSLISTE_PLUGIN_DIR . 'includes/class-admin-menu.php';
|
||||
require_once UMZUGSLISTE_PLUGIN_DIR . 'includes/class-settings.php';
|
||||
}
|
||||
|
||||
/**
|
||||
@@ -73,6 +74,9 @@ class Umzugsliste {
|
||||
if ( is_admin() ) {
|
||||
Umzugsliste_Admin_Menu::get_instance();
|
||||
}
|
||||
|
||||
// Initialize settings
|
||||
Umzugsliste_Settings::get_instance();
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user