PluginProbe
AI Builder – Generate pages, blocks, images & translate with AI / 2.1.7
AI Builder – Generate pages, blocks, images & translate with AI v2.1.7
2.8.0 2.7.10 2.7.9 2.7.8 2.0.8 2.0.9 2.1.0 2.1.1 2.1.10 2.1.11 2.1.12 2.1.2 2.1.3 2.1.4 2.1.5 2.1.6 2.1.7 2.1.8 2.1.9 2.2.0 2.2.1 2.2.2 2.2.3 2.2.4 2.3.0 All 123 releases
ai-builder / includes / class-translation-switcher.php

class-translation-switcher.php in AI Builder – Generate pages, blocks, images & translate with AI 2.1.7, at includes/class-translation-switcher.php

160 lines 5.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 if (!defined('ABSPATH')) {
4 exit;
5 }
6
7 class AIBUI_Translation_Switcher
8 {
9 private $manager;
10
11 public function __construct(AIBUI_Translation_Manager $manager)
12 {
13 $this->manager = $manager;
14
15 add_action('wp_footer', array($this, 'render_switcher'), 20);
16 }
17
18 public function render_switcher()
19 {
20 if (!$this->manager->is_switcher_enabled()) {
21 return;
22 }
23
24 $languages = $this->manager->get_available_languages();
25 if (count($languages) <= 1) {
26 return;
27 }
28
29 $current = $this->manager->get_current_language();
30 $supported = AIBUI_Translation_Handler::SUPPORTED_LANGUAGES;
31 $settings = AIBUI_Translation_Settings::get_settings();
32 $bg_color = !empty($settings['switcher_bg']) ? $settings['switcher_bg'] : '#111827';
33 $text_color = !empty($settings['switcher_text']) ? $settings['switcher_text'] : '#ffffff';
34 $min_height = !empty($settings['switcher_min_height']) ? (int) $settings['switcher_min_height'] : 32;
35 $min_width = !empty($settings['switcher_min_width']) ? (int) $settings['switcher_min_width'] : 140;
36 $alpha = isset($settings['switcher_alpha']) ? (float) $settings['switcher_alpha'] : 0.9;
37 if ($alpha < 0) {
38 $alpha = 0;
39 } elseif ($alpha > 1) {
40 $alpha = 1;
41 }
42
43 // Convert hex to rgba with configurable alpha for transparency.
44 $bg_rgba = sprintf('rgba(17, 24, 39, %.2f)', $alpha); // sensible default
45 if (preg_match('/^#([0-9a-fA-F]{6})$/', $bg_color, $m)) {
46 $hex = $m[1];
47 $r = hexdec(substr($hex, 0, 2));
48 $g = hexdec(substr($hex, 2, 2));
49 $b = hexdec(substr($hex, 4, 2));
50 $bg_rgba = sprintf('rgba(%d, %d, %d, %.2f)', $r, $g, $b, $alpha);
51 }
52
53 $current_url = $this->get_current_url();
54 ?>
55 <div class="aibui-lang-switcher">
56 <form method="get" class="aibui-lang-switcher__form">
57 <label for="aibui-lang-select" class="screen-reader-text"><?php esc_html_e('Select language', 'ai-builder'); ?></label>
58 <select id="aibui-lang-select" name="ai_lang" onchange="this.form.submit()">
59 <?php foreach ($languages as $lang_code) :
60 $label = $supported[$lang_code] ?? strtoupper($lang_code);
61 ?>
62 <option value="<?php echo esc_attr($lang_code); ?>" <?php selected($current, $lang_code); ?>>
63 <?php echo esc_html($label); ?>
64 </option>
65 <?php endforeach; ?>
66 </select>
67 <?php
68 // Preserve other query vars
69 $query = $_GET;
70 unset($query['ai_lang']);
71 foreach ($query as $key => $value) {
72 if (is_array($value)) {
73 foreach ($value as $val) {
74 printf('<input type="hidden" name="%s[]" value="%s" />', esc_attr($key), esc_attr($val));
75 }
76 } else {
77 printf('<input type="hidden" name="%s" value="%s" />', esc_attr($key), esc_attr($value));
78 }
79 }
80 ?>
81 </form>
82 </div>
83 <style>
84 .aibui-lang-switcher {
85 position: fixed;
86 top: 0;
87 right: 0;
88 background: <?php echo esc_attr($bg_rgba); ?>;
89 padding: 0px 14px 10px 14px;
90 border-radius: 0 0 0 16px; /* only bottom-left corner rounded */
91 z-index: 9999;
92 min-height: <?php echo (int) $min_height; ?>px;
93 min-width: <?php echo (int) $min_width; ?>px;
94 display: flex;
95 align-items: center;
96 justify-content: center;
97 cursor: pointer;
98 }
99
100 /* When logged-in admin bar is visible, offset the switcher */
101 body.admin-bar .aibui-lang-switcher {
102 top: 32px;
103 }
104
105 @media screen and (max-width: 782px) {
106 body.admin-bar .aibui-lang-switcher {
107 top: 46px;
108 }
109 }
110
111 .aibui-lang-switcher select {
112 background: transparent;
113 color: <?php echo esc_attr($text_color); ?>;
114 border: none;
115 border-radius: 0;
116 padding: 4px 8px;
117 font-weight: 600;
118 outline: none;
119 box-shadow: none;
120 cursor: pointer;
121 }
122
123 .aibui-lang-switcher select option {
124 color: #000;
125 }
126 </style>
127 <script>
128 (function(){
129 var wrapper = document.querySelector('.aibui-lang-switcher');
130 var select = document.getElementById('aibui-lang-select');
131 if (wrapper && select) {
132 wrapper.addEventListener('click', function(e) {
133 if (e.target !== select) {
134 // Programmatically open the select dropdown
135 if (typeof select.showPicker === 'function') {
136 select.showPicker();
137 } else {
138 // Fallback: focus and simulate mousedown for older browsers
139 select.focus();
140 var evt = new MouseEvent('mousedown', { bubbles: true, cancelable: true, view: window });
141 select.dispatchEvent(evt);
142 }
143 }
144 });
145 }
146 })();
147 </script>
148 <?php
149 }
150
151 private function get_current_url()
152 {
153 $scheme = is_ssl() ? 'https' : 'http';
154 $host = $_SERVER['HTTP_HOST'] ?? '';
155 $uri = $_SERVER['REQUEST_URI'] ?? '';
156 return $scheme . '://' . $host . $uri;
157 }
158 }
159
160