| 1 |
<?php |
| 2 |
|
| 3 |
if (!defined('ABSPATH')) { |
| 4 |
exit; |
| 5 |
} |
| 6 |
|
| 7 |
class AIBUI_Translation_Settings |
| 8 |
{ |
| 9 |
const OPTION_KEY = 'aibui_translation_settings'; |
| 10 |
|
| 11 |
public static function init() |
| 12 |
{ |
| 13 |
add_action('admin_post_aibui_save_translation_settings', array(__CLASS__, 'handle_save')); |
| 14 |
} |
| 15 |
|
| 16 |
public static function get_default_settings() |
| 17 |
{ |
| 18 |
return array( |
| 19 |
'enable_switcher' => false, |
| 20 |
'default_lang' => 'en', |
| 21 |
'available_langs' => array('en'), |
| 22 |
'switcher_bg' => '#111827', |
| 23 |
'switcher_text' => '#ffffff', |
| 24 |
'switcher_min_height' => 32, |
| 25 |
'switcher_min_width' => 140, |
| 26 |
'switcher_alpha' => 0.9, |
| 27 |
); |
| 28 |
} |
| 29 |
|
| 30 |
public static function get_settings() |
| 31 |
{ |
| 32 |
$stored = get_option(self::OPTION_KEY, array()); |
| 33 |
if (!is_array($stored)) { |
| 34 |
$stored = array(); |
| 35 |
} |
| 36 |
|
| 37 |
return wp_parse_args($stored, self::get_default_settings()); |
| 38 |
} |
| 39 |
|
| 40 |
public static function handle_save() |
| 41 |
{ |
| 42 |
if (!current_user_can('manage_options')) { |
| 43 |
wp_die(__('You do not have permission to perform this action.', 'ai-builder')); |
| 44 |
} |
| 45 |
|
| 46 |
check_admin_referer('aibui_save_translation_settings'); |
| 47 |
|
| 48 |
$defaults = self::get_default_settings(); |
| 49 |
$supported = array_keys(AIBUI_Translation_Handler::get_supported_languages()); |
| 50 |
|
| 51 |
$enable_switcher = isset($_POST['enable_switcher']); |
| 52 |
|
| 53 |
$default_lang = isset($_POST['default_lang']) ? sanitize_text_field(wp_unslash($_POST['default_lang'])) : $defaults['default_lang']; |
| 54 |
if (!in_array($default_lang, $supported, true)) { |
| 55 |
$default_lang = $defaults['default_lang']; |
| 56 |
} |
| 57 |
|
| 58 |
$available_langs = array(); |
| 59 |
if (!empty($_POST['available_langs'])) { |
| 60 |
$raw_langs = (array) $_POST['available_langs']; |
| 61 |
foreach ($raw_langs as $lang) { |
| 62 |
$lang = sanitize_text_field(wp_unslash($lang)); |
| 63 |
if (in_array($lang, $supported, true)) { |
| 64 |
$available_langs[] = $lang; |
| 65 |
} |
| 66 |
} |
| 67 |
$available_langs = array_unique($available_langs); |
| 68 |
} |
| 69 |
|
| 70 |
if (empty($available_langs)) { |
| 71 |
$available_langs = array($default_lang); |
| 72 |
} |
| 73 |
|
| 74 |
if (!in_array($default_lang, $available_langs, true)) { |
| 75 |
array_unshift($available_langs, $default_lang); |
| 76 |
} |
| 77 |
|
| 78 |
$switcher_bg = isset($_POST['switcher_bg']) ? sanitize_hex_color(wp_unslash($_POST['switcher_bg'])) : $defaults['switcher_bg']; |
| 79 |
if (empty($switcher_bg)) { |
| 80 |
$switcher_bg = $defaults['switcher_bg']; |
| 81 |
} |
| 82 |
|
| 83 |
$switcher_text = isset($_POST['switcher_text']) ? sanitize_hex_color(wp_unslash($_POST['switcher_text'])) : $defaults['switcher_text']; |
| 84 |
if (empty($switcher_text)) { |
| 85 |
$switcher_text = $defaults['switcher_text']; |
| 86 |
} |
| 87 |
|
| 88 |
$switcher_min_height = isset($_POST['switcher_min_height']) ? absint($_POST['switcher_min_height']) : $defaults['switcher_min_height']; |
| 89 |
if ($switcher_min_height <= 0) { |
| 90 |
$switcher_min_height = $defaults['switcher_min_height']; |
| 91 |
} |
| 92 |
|
| 93 |
$switcher_min_width = isset($_POST['switcher_min_width']) ? absint($_POST['switcher_min_width']) : $defaults['switcher_min_width']; |
| 94 |
if ($switcher_min_width <= 0) { |
| 95 |
$switcher_min_width = $defaults['switcher_min_width']; |
| 96 |
} |
| 97 |
|
| 98 |
$switcher_alpha = isset($_POST['switcher_alpha']) ? floatval($_POST['switcher_alpha']) : $defaults['switcher_alpha']; |
| 99 |
if ($switcher_alpha < 0) { |
| 100 |
$switcher_alpha = 0; |
| 101 |
} elseif ($switcher_alpha > 1) { |
| 102 |
$switcher_alpha = 1; |
| 103 |
} |
| 104 |
|
| 105 |
$settings = array( |
| 106 |
'enable_switcher' => $enable_switcher, |
| 107 |
'default_lang' => $default_lang, |
| 108 |
'available_langs' => $available_langs, |
| 109 |
'switcher_bg' => $switcher_bg, |
| 110 |
'switcher_text' => $switcher_text, |
| 111 |
'switcher_min_height' => $switcher_min_height, |
| 112 |
'switcher_min_width' => $switcher_min_width, |
| 113 |
'switcher_alpha' => $switcher_alpha, |
| 114 |
); |
| 115 |
|
| 116 |
update_option(self::OPTION_KEY, $settings); |
| 117 |
|
| 118 |
$redirect = add_query_arg( |
| 119 |
array( |
| 120 |
'page' => 'aibui-translation-settings', |
| 121 |
'translation_saved' => 1, |
| 122 |
), |
| 123 |
admin_url('admin.php') |
| 124 |
); |
| 125 |
|
| 126 |
wp_safe_redirect($redirect); |
| 127 |
exit; |
| 128 |
} |
| 129 |
} |
| 130 |
|
| 131 |
|