floatina/floatina/includes/class-settings.php
2025-10-03 23:54:13 +02:00

218 lines
8.9 KiB
PHP
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

<?php
if (!defined('ABSPATH')) exit;
/************************************************************************************
* DEFAULTS
************************************************************************************/
function floatina_defaults_settings() {
return [
'position' => 'right',
'enable_home' => 1,
'enable_voice' => 1,
'enable_text' => 1,
'enable_news' => 1,
'enable_faq' => 1,
'voice_iframe' => '',
'voice_mode_popup' => 0,
'text_iframe' => '',
'text_mode_popup' => 0,
'home_extra' => '',
];
}
function floatina_defaults_style() {
return [
'button_color' => '#1e88ff',
'accent_color' => '#1e66ff',
'border_color' => '#e6e8eb',
'nav_active_bg' => '#eef5ff',
'grad_start' => '#ffffff',
'grad_end' => '#f5f7ff',
'icon_color' => '#0f172a',
'text_color' => '#0f172a',
'muted_color' => '#475569',
];
}
/************************************************************************************
* SANITIZERS
************************************************************************************/
function floatina_sanitize_settings($input){
$old = get_option('floatina_settings', []);
$out = array_merge(floatina_defaults_settings(), is_array($old)?$old:[]);
$chk = function($k){ return isset($_POST['floatina_settings'][$k]) ? 1 : 0; };
if (isset($input['position'])) {
$out['position'] = in_array($input['position'], ['left','right'], true) ? $input['position'] : 'right';
}
$out['enable_home'] = $chk('enable_home');
$out['enable_voice'] = $chk('enable_voice');
$out['enable_text'] = $chk('enable_text');
$out['enable_news'] = $chk('enable_news');
$out['enable_faq'] = $chk('enable_faq');
if (array_key_exists('voice_iframe',$input)) $out['voice_iframe'] = esc_url_raw($input['voice_iframe']);
$out['voice_mode_popup'] = $chk('voice_mode_popup');
if (array_key_exists('text_iframe',$input)) $out['text_iframe'] = esc_url_raw($input['text_iframe']);
$out['text_mode_popup'] = $chk('text_mode_popup');
if (array_key_exists('home_extra',$input)) $out['home_extra'] = wp_kses_post($input['home_extra']);
return $out;
}
function floatina_sanitize_style($input){
$old = get_option('floatina_style', []);
$out = array_merge(floatina_defaults_style(), is_array($old)?$old:[]);
foreach ($out as $k=>$v){
if (array_key_exists($k,$input)) {
$val = sanitize_hex_color(trim((string)$input[$k]));
if ($val) $out[$k] = $val;
}
}
return $out;
}
/************************************************************************************
* ADMIN MENU
************************************************************************************/
add_action('admin_menu', function () {
add_menu_page('Floatina','Floatina','manage_options','floatina','floatina_render_settings','dashicons-layout',81);
});
/************************************************************************************
* REGISTER SETTINGS
************************************************************************************/
add_action('admin_init', function () {
register_setting('floatina_group_settings', 'floatina_settings', [
'type'=>'array','sanitize_callback'=>'floatina_sanitize_settings'
]);
register_setting('floatina_group_style', 'floatina_style', [
'type'=>'array','sanitize_callback'=>'floatina_sanitize_style'
]);
add_settings_section('floatina_section_settings','Settings','__return_false','floatina-settings');
add_settings_section('floatina_section_style','Style','__return_false','floatina-style');
});
/************************************************************************************
* SETTINGS PAGE RENDERER
************************************************************************************/
function floatina_render_settings(){
if (!current_user_can('manage_options')) return;
$tab = isset($_GET['tab']) ? sanitize_key($_GET['tab']) : 'settings';
if (!in_array($tab, ['settings','style'], true)) $tab = 'settings';
$s = get_option('floatina_settings', floatina_defaults_settings());
$c = get_option('floatina_style', floatina_defaults_style());
?>
<div class="wrap fi-admin">
<h1>Floatina</h1>
<h2 class="fi-admin-tabs">
<a class="fi-tab <?php echo $tab==='settings'?'is-active':''; ?>" href="<?php echo esc_url(admin_url('admin.php?page=floatina&tab=settings')); ?>">Settings</a>
<a class="fi-tab <?php echo $tab==='style'?'is-active':''; ?>" href="<?php echo esc_url(admin_url('admin.php?page=floatina&tab=style')); ?>">Style</a>
</h2>
<?php if ($tab==='settings'): ?>
<form method="post" action="options.php" class="fi-form">
<?php settings_fields('floatina_group_settings'); ?>
<div class="fi-field">
<label class="fi-label">Panel position</label>
<div class="fi-segment">
<label><input type="radio" name="floatina_settings[position]" value="right" <?php checked($s['position'],'right'); ?>><span>Right</span></label>
<label><input type="radio" name="floatina_settings[position]" value="left" <?php checked($s['position'],'left'); ?>><span>Left</span></label>
</div>
<p class="description">Choose bottom-right or bottom-left placement.</p>
</div>
<?php
$toggles = [
'enable_home' => 'Enable Home tab',
'enable_voice' => 'Enable Voice tab',
'enable_text' => 'Enable Text tab',
'enable_news' => 'Enable News tab',
'enable_faq' => 'Enable FAQ tab',
];
foreach ($toggles as $key=>$label): ?>
<div class="fi-field">
<label class="fi-label"><?php echo esc_html($label); ?></label>
<label class="fi-toggle">
<input type="checkbox" name="floatina_settings[<?php echo esc_attr($key); ?>]" value="1" <?php checked( (int)$s[$key], 1 ); ?> />
<span class="fi-tgl"></span>
</label>
</div>
<?php endforeach; ?>
<hr class="fi-sep" />
<div class="fi-field">
<label class="fi-label">Voice URL</label>
<input type="url" class="regular-text" name="floatina_settings[voice_iframe]" value="<?php echo esc_attr($s['voice_iframe']); ?>" />
</div>
<div class="fi-field">
<label class="fi-label">Voice mode (Popup if blocked)</label>
<label class="fi-toggle">
<input type="checkbox" name="floatina_settings[voice_mode_popup]" value="1" <?php checked($s['voice_mode_popup'],1); ?> />
<span class="fi-tgl"></span>
</label>
<p class="description">Open in new tab when iframe is blocked.</p>
</div>
<div class="fi-field">
<label class="fi-label">Text URL</label>
<input type="url" class="regular-text" name="floatina_settings[text_iframe]" value="<?php echo esc_attr($s['text_iframe']); ?>" />
</div>
<div class="fi-field">
<label class="fi-label">Text mode (Popup if blocked)</label>
<label class="fi-toggle">
<input type="checkbox" name="floatina_settings[text_mode_popup]" value="1" <?php checked($s['text_mode_popup'],1); ?> />
<span class="fi-tgl"></span>
</label>
<p class="description">Open in new tab when iframe is blocked.</p>
</div>
<hr class="fi-sep" />
<div class="fi-field">
<label class="fi-label">Home extra block</label>
<?php
wp_editor($s['home_extra'], 'floatina_home_extra', [
'textarea_name' => 'floatina_settings[home_extra]',
'textarea_rows' => 6,
'media_buttons' => true,
'tinymce' => true,
]);
?>
<p class="description">Optional block under the Hi card on Home. Leave empty to hide.</p>
</div>
<?php submit_button('Save settings'); ?>
</form>
<?php else: ?>
<form method="post" action="options.php" class="fi-form">
<?php settings_fields('floatina_group_style'); ?>
<div class="fi-grid2">
<?php
$labels = [
'button_color'=>'FAB color','accent_color'=>'Accent color','border_color'=>'Border color',
'nav_active_bg'=>'Tab active background','grad_start'=>'Header gradient start','grad_end'=>'Header gradient end',
'icon_color'=>'Tab icon/text color','text_color'=>'Body text color','muted_color'=>'Muted text color'
];
foreach ($labels as $key=>$label): ?>
<div class="fi-field">
<label class="fi-label"><?php echo esc_html($label); ?></label>
<input type="text" class="fi-color" name="floatina_style[<?php echo esc_attr($key); ?>]" value="<?php echo esc_attr($c[$key]); ?>">
</div>
<?php endforeach; ?>
</div>
<?php submit_button('Save style'); ?>
</form>
<?php endif; ?>
</div>
<?php
}