Files
mataba/shop/xshop-theme/inc/customization/options.php
T

62 lines
2.4 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
declare(strict_types=1);
defined('ABSPATH') || exit;
/**
* Theme options store: single option `xshop_settings`.
* Full options UI ships in M6; this file only registers the setting with sanitization.
*/
add_action('init', 'xshop_register_settings');
function xshop_register_settings(): void {
register_setting('xshop_settings', 'xshop_settings', [
'type' => 'array',
'sanitize_callback' => 'xshop_sanitize_settings',
'default' => xshop_default_settings(),
'show_in_rest' => false,
]);
}
function xshop_default_settings(): array {
return [
'color_mode' => 'light', // light|dark|auto
'header_layout' => 'classic', // classic|centered|minimal
'footer_layout' => 'four-col',
'shop_columns' => 3,
'product_card' => 'default',
'enable_dark_mode' => '0',
'custom_css' => '',
];
}
function xshop_sanitize_settings($value): array {
if (!is_array($value)) { $value = []; }
$defaults = xshop_default_settings();
$out = $defaults;
if (isset($value['color_mode'])) {
$out['color_mode'] = in_array($value['color_mode'], ['light','dark','auto'], true) ? $value['color_mode'] : $defaults['color_mode'];
}
if (isset($value['header_layout'])) {
$out['header_layout'] = in_array($value['header_layout'], ['classic','centered','minimal'], true) ? $value['header_layout'] : $defaults['header_layout'];
}
if (isset($value['footer_layout'])) {
$out['footer_layout'] = sanitize_key((string) $value['footer_layout']);
}
if (isset($value['shop_columns'])) {
$out['shop_columns'] = min(6, max(1, absint($value['shop_columns'])));
}
if (isset($value['product_card'])) {
$out['product_card'] = in_array($value['product_card'], ['compact','default','detailed'], true) ? $value['product_card'] : $defaults['product_card'];
}
if (isset($value['enable_dark_mode'])) {
$out['enable_dark_mode'] = $value['enable_dark_mode'] ? '1' : '0';
}
if (isset($value['custom_css'])) {
// Allow CSS – wp_strip_all_tags + wp_kses with empty allow-list would strip everything;
// use sanitization that preserves CSS: wp_strip_all_tags is too aggressive, so allow raw but trim.
$out['custom_css'] = trim(wp_strip_all_tags((string) $value['custom_css']));
}
return $out;
}