PluginProbe
AI Builder – Generate pages, blocks, images & translate with AI / trunk
AI Builder – Generate pages, blocks, images & translate with AI vtrunk
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 2.3.10 2.3.11 All 121 releases
ai-builder / includes / class-translation-switcher.php

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

148 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
16 /**
17 * Render callback for the Language Switcher block.
18 */
19 public function render_block($attributes = array(), $content = '', $block = null)
20 {
21 if (!$this->manager->is_switcher_enabled()) {
22 return '';
23 }
24
25 $languages = $this->get_ordered_languages();
26 if (count($languages) <= 1) {
27 return '';
28 }
29
30 $settings = AIBUI_Translation_Settings::get_settings();
31 $supported = AIBUI_Translation_Handler::SUPPORTED_LANGUAGES;
32 $current = $this->manager->get_current_language();
33 $select_id = 'aibui-lang-select-' . wp_generate_uuid4();
34
35 $bg_color = !empty($settings['switcher_bg']) ? $settings['switcher_bg'] : '#5686c9';
36 $text_color = !empty($settings['switcher_text']) ? $settings['switcher_text'] : '#ffffff';
37 $min_height = !empty($settings['switcher_min_height']) ? (int) $settings['switcher_min_height'] : 30;
38 $min_width = !empty($settings['switcher_min_width']) ? (int) $settings['switcher_min_width'] : 90;
39 $alpha = isset($settings['switcher_alpha']) ? (float) $settings['switcher_alpha'] : 0.9;
40 $alpha = max(0, min(1, $alpha));
41
42 $bg_rgba = sprintf('rgba(17, 24, 39, %.2f)', $alpha);
43 if (preg_match('/^#([0-9a-fA-F]{6})$/', $bg_color, $m)) {
44 $hex = $m[1];
45 $r = hexdec(substr($hex, 0, 2));
46 $g = hexdec(substr($hex, 2, 2));
47 $b = hexdec(substr($hex, 4, 2));
48 $bg_rgba = sprintf('rgba(%d, %d, %d, %.2f)', $r, $g, $b, $alpha);
49 }
50
51 $attr_bg = isset($attributes['backgroundColor']) && $attributes['backgroundColor'] !== ''
52 ? sanitize_text_field($attributes['backgroundColor'])
53 : '';
54 $attr_text = isset($attributes['textColor']) && $attributes['textColor'] !== ''
55 ? sanitize_text_field($attributes['textColor'])
56 : '';
57 $attr_min_width = isset($attributes['minWidth']) && is_numeric($attributes['minWidth'])
58 ? (int) $attributes['minWidth']
59 : null;
60 $attr_min_height = isset($attributes['minHeight']) && is_numeric($attributes['minHeight'])
61 ? (int) $attributes['minHeight']
62 : null;
63 $default_radius = 22;
64 $attr_radius = isset($attributes['borderRadius']) && is_numeric($attributes['borderRadius'])
65 ? (int) $attributes['borderRadius']
66 : null;
67
68 $style_vars = array(
69 '--aibui-switcher-bg' => $attr_bg !== '' ? $attr_bg : $bg_rgba,
70 '--aibui-switcher-text' => $attr_text !== '' ? $attr_text : $text_color,
71 '--aibui-switcher-min-height' => ($attr_min_height !== null ? $attr_min_height : $min_height) . 'px',
72 '--aibui-switcher-min-width' => ($attr_min_width !== null ? $attr_min_width : $min_width) . 'px',
73 '--aibui-switcher-radius' => ($attr_radius !== null ? $attr_radius : $default_radius) . 'px',
74 );
75
76 $style_attr = '';
77 foreach ($style_vars as $var => $value) {
78 if ($value !== '' && $value !== null) {
79 $style_attr .= $var . ':' . $value . ';';
80 }
81 }
82
83 $classes = 'aibui-lang-switcher-block aibui-lang-switcher-block--no-label';
84
85 $wrapper_attributes = function_exists('get_block_wrapper_attributes')
86 ? get_block_wrapper_attributes(array(
87 'class' => trim($classes),
88 'style' => $style_attr,
89 ))
90 : sprintf('class="%s" style="%s"', esc_attr(trim($classes)), esc_attr($style_attr));
91
92 ob_start();
93 ?>
94 <div <?php echo $wrapper_attributes; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped ?>>
95 <form method="get" class="aibui-lang-switcher__form">
96 <label for="<?php echo esc_attr($select_id); ?>" class="screen-reader-text">
97 <?php esc_html_e('Select language', 'ai-builder'); ?>
98 </label>
99 <select id="<?php echo esc_attr($select_id); ?>" name="ai_lang" onchange="this.form.submit()">
100 <?php foreach ($languages as $lang_code) :
101 $label = $supported[$lang_code] ?? strtoupper($lang_code);
102 ?>
103 <option value="<?php echo esc_attr($lang_code); ?>" <?php selected($current, $lang_code); ?>>
104 <?php echo esc_html($label); ?>
105 </option>
106 <?php endforeach; ?>
107 </select>
108 <?php $this->render_preserved_query_fields(); ?>
109 </form>
110 </div>
111 <?php
112 return ob_get_clean();
113 }
114
115 private function get_ordered_languages()
116 {
117 $available = $this->manager->get_available_languages();
118 $default = $this->manager->get_default_language();
119
120 $available = array_values(array_unique(array_filter((array) $available)));
121
122 if ($default && in_array($default, $available, true)) {
123 $available = array_values(array_diff($available, array($default)));
124 array_unshift($available, $default);
125 } elseif ($default) {
126 array_unshift($available, $default);
127 }
128
129 return $available;
130 }
131
132 private function render_preserved_query_fields()
133 {
134 $query = $_GET;
135 unset($query['ai_lang']);
136 foreach ($query as $key => $value) {
137 if (is_array($value)) {
138 foreach ($value as $val) {
139 printf('<input type="hidden" name="%s[]" value="%s" />', esc_attr($key), esc_attr(wp_unslash($val)));
140 }
141 } else {
142 printf('<input type="hidden" name="%s" value="%s" />', esc_attr($key), esc_attr(wp_unslash($value)));
143 }
144 }
145 }
146 }
147
148