PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / 1.1.0
Translate and Go multilingual – Automatic AI translation – wpLingua v1.1.0
2.16.7 2.16.6 2.16.5 2.16.4 2.16.3 2.16.2 2.16.1 2.16.0 2.15.2 2.15.1 2.15.0 2.14.3 2.14.2 2.14.1 2.14.0 2.13.1 2.13.0 2.12.3 2.12.2 2.12.1 trunk 1.0.3 1.0.4 1.0.5 1.1.0 All 112 releases
wplingua / inc / switcher.php

switcher.php in Translate and Go multilingual – Automatic AI translation – wpLingua 1.1.0, at inc/switcher.php

520 lines 12.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 // If this file is called directly, abort.
4 if ( ! defined( 'WPINC' ) ) {
5 die;
6 }
7
8
9 /**
10 * Get wpLingua switcher option : Insert
11 *
12 * @return string
13 */
14 function wplng_get_switcher_insert() {
15
16 $insert = get_option( 'wplng_insert' );
17 $is_valid = false;
18
19 if ( ! empty( $insert ) ) {
20 $valid_insert = wplng_data_switcher_valid_insert();
21 foreach ( $valid_insert as $id => $name ) {
22 if ( $insert === $id ) {
23 $is_valid = true;
24 break;
25 }
26 }
27 }
28
29 if ( ! $is_valid ) {
30 $insert = 'bottom-left';
31 }
32
33 $insert = apply_filters(
34 'wplng_switcher_insert',
35 $insert
36 );
37
38 return $insert;
39 }
40
41
42 /**
43 * Get wpLingua switcher option : Theme
44 *
45 * @return string
46 */
47 function wplng_get_switcher_theme() {
48
49 $theme = get_option( 'wplng_theme' );
50 $is_valid = false;
51
52 if ( ! empty( $theme ) ) {
53
54 $valid_theme = wplng_data_switcher_valid_theme();
55
56 foreach ( $valid_theme as $id => $name ) {
57 if ( $theme === $id ) {
58 $is_valid = true;
59 break;
60 }
61 }
62 }
63
64 if ( ! $is_valid ) {
65 $theme = 'light-double-square';
66 }
67
68 $theme = apply_filters(
69 'wplng_switcher_theme',
70 $theme
71 );
72
73 return $theme;
74 }
75
76
77 /**
78 * Get wpLingua switcher option : Style
79 *
80 * @return string
81 */
82 function wplng_get_switcher_style() {
83
84 $style = get_option( 'wplng_style' );
85 $is_valid = false;
86
87 if ( ! empty( $style ) ) {
88
89 $valid_style = wplng_data_switcher_valid_style();
90
91 foreach ( $valid_style as $id => $name ) {
92 if ( $style === $id ) {
93 $is_valid = true;
94 break;
95 }
96 }
97 }
98
99 if ( ! $is_valid ) {
100 $style = 'list';
101 }
102
103 $style = apply_filters(
104 'wplng_switcher_style',
105 $style
106 );
107
108 return $style;
109 }
110
111
112 /**
113 * Get wpLingua switcher option : Name format
114 *
115 * @return void
116 */
117 function wplng_get_switcher_name_format() {
118
119 $name_format = get_option( 'wplng_name_format' );
120 $is_valid = false;
121
122 if ( ! empty( $name_format ) ) {
123
124 $valid_name_format = wplng_data_switcher_valid_name_format();
125
126 foreach ( $valid_name_format as $id => $name ) {
127 if ( $name_format === $id ) {
128 $is_valid = true;
129 break;
130 }
131 }
132 }
133
134 if ( ! $is_valid ) {
135 $name_format = 'name';
136 }
137
138 if ( wplng_get_switcher_flags_style() === 'none'
139 && 'none' === $name_format
140 ) {
141 $name_format = 'name';
142 }
143
144 $name_format = apply_filters(
145 'wplng_switcher_name_format',
146 $name_format
147 );
148
149 return $name_format;
150 }
151
152
153 /**
154 * Get wpLingua switcher option : Flags style
155 *
156 * @return string
157 */
158 function wplng_get_switcher_flags_style() {
159
160 $flags_style = get_option( 'wplng_flags_style' );
161 $is_valid = false;
162
163 if ( ! empty( $flags_style ) ) {
164
165 $valid_flags_style = wplng_data_switcher_valid_flags_style();
166
167 foreach ( $valid_flags_style as $id => $name ) {
168 if ( $flags_style === $id ) {
169 $is_valid = true;
170 break;
171 }
172 }
173 }
174
175 if ( ! $is_valid ) {
176 $flags_style = 'rectangular';
177 }
178
179 return $flags_style;
180 }
181
182
183 /**
184 * Get wpLingua switcher option : Class
185 *
186 * @param array $arg
187 * @return string
188 */
189 function wplng_get_switcher_class( $arg = array() ) {
190
191 $class = '';
192
193 /**
194 * Define insert class (list, block)
195 */
196 if ( ! empty( $arg['insert'] )
197 && array_key_exists( $arg['insert'], wplng_data_switcher_valid_insert() )
198 ) {
199 $class = 'insert-' . $arg['insert'];
200 } else {
201 $class = 'insert-' . wplng_get_switcher_insert();
202 }
203
204 /**
205 * Define style class (list, block)
206 */
207 if ( ! empty( $arg['style'] )
208 && array_key_exists( $arg['style'], wplng_data_switcher_valid_style() )
209 ) {
210 $class .= ' style-' . $arg['style'];
211 } else {
212 $class .= ' style-' . wplng_get_switcher_style();
213 }
214
215 /**
216 * Define language name class (id, name, none)
217 */
218
219 if ( ! empty( $arg['title'] )
220 && array_key_exists( $arg['title'], wplng_data_switcher_valid_name_format() )
221 ) {
222 $class .= ' title-' . $arg['title'];
223 } else {
224 $class .= ' title-' . wplng_get_switcher_name_format();
225 }
226
227 /**
228 * Define theme class (light, dark)
229 */
230 if ( ! empty( $arg['theme'] )
231 && array_key_exists( $arg['theme'], wplng_data_switcher_valid_theme() )
232 ) {
233 $class .= ' theme-' . $arg['theme'];
234 } else {
235 $class .= ' theme-' . wplng_get_switcher_theme();
236 }
237
238 /**
239 * Define flags theme class (id, name, none)
240 */
241 if ( ! empty( $arg['flags'] )
242 && array_key_exists( $arg['flags'], wplng_data_switcher_valid_flags_style() )
243 ) {
244 $class .= ' flags-' . $arg['flags'];
245 } else {
246 $class .= ' flags-' . wplng_get_switcher_flags_style();
247 }
248
249 /**
250 * Define additional class (id, name, none)
251 */
252 if ( ! empty( $arg['class'] ) ) {
253 $class .= ' ' . $arg['class'];
254 }
255
256 return esc_attr( $class );
257 }
258
259
260 /**
261 * Print HTML of wpLingua switcher if it's inserted automaticaly
262 *
263 * @return string
264 */
265 function wplng_switcher_wp_footer() {
266
267 if ( ! wplng_url_is_translatable()
268 || 'none' === wplng_get_switcher_insert()
269 ) {
270 return;
271 }
272
273 echo wplng_get_switcher_html(
274 array(
275 'class' => 'insert-auto',
276 )
277 );
278 }
279
280
281 /**
282 * Print HTML of wpLingua switcher
283 *
284 * @param array $arg
285 * @return string
286 */
287 function wplng_get_switcher_html( $arg = array() ) {
288
289 if ( ! wplng_url_is_translatable() && ! is_admin() ) {
290 return '';
291 }
292
293 $language_website = wplng_get_language_website();
294 $language_current_id = wplng_get_language_current_id();
295 $languages_target = wplng_get_languages_target();
296 $class = wplng_get_switcher_class( $arg );
297 $flags_style = wplng_get_switcher_flags_style();
298 $url_website = wplng_get_url_original();
299 $flags_show = true;
300
301 if ( is_admin() ) {
302 $flags_show = true;
303 $url_website = '#';
304 } elseif ( ! empty( $arg['flags'] ) && 'none' === $arg['flags'] ) {
305 $flags_show = false;
306 } elseif ( 'none' === $flags_style ) {
307 $flags_show = false;
308 }
309
310 if ( empty( $languages_target ) ) {
311 return '';
312 }
313
314 /**
315 * Translate language names
316 */
317
318 // Untranslated target language names
319 foreach ( $languages_target as $key => $language_target ) {
320 $languages_target[ $key ]['original'] = wplng_get_language_name_untranslated(
321 $language_target
322 );
323 }
324
325 // Untranslated website language name
326 $language_website['original'] = wplng_get_language_name_untranslated(
327 $language_website
328 );
329
330 // Translate target language names
331 foreach ( $languages_target as $key => $language_target ) {
332 $languages_target[ $key ]['name'] = wplng_get_language_name_translated(
333 $language_target,
334 $language_current_id
335 );
336 }
337
338 // Translate website language name
339 $language_website['name'] = wplng_get_language_name_translated(
340 $language_website,
341 $language_current_id
342 );
343
344 /**
345 * Change flags if necessary
346 */
347
348 if ( ! empty( $arg['flags'] )
349 && ( 'none' !== $arg['flags'] )
350 && ( $flags_style !== $arg['flags'] )
351 ) {
352
353 // Change flags on website languages
354 if ( ! empty( $language_website['flag'] ) ) {
355 $language_website['flag'] = str_replace(
356 '/wplingua/assets/images/' . $flags_style . '/',
357 '/wplingua/assets/images/' . $arg['flags'] . '/',
358 $language_website['flag']
359 );
360 }
361
362 // Change flags on target languages
363 foreach ( $languages_target as $key => $language_target ) {
364
365 if ( empty( $language_target['flag'] ) ) {
366 continue;
367 }
368
369 $languages_target[ $key ]['flag'] = str_replace(
370 '/wplingua/assets/images/' . $flags_style . '/',
371 '/wplingua/assets/images/' . $arg['flags'] . '/',
372 $language_target['flag']
373 );
374 }
375 }
376
377 /**
378 * Create the switcher HTML
379 */
380
381 $html = '<div class="' . esc_attr( 'wplng-switcher ' . $class ) . '">';
382 $html .= '<div class="switcher-content">';
383 $html .= '<div class="wplng-languages">';
384
385 // Create link for website language
386
387 if ( $language_website['id'] === $language_current_id ) {
388
389 $html .= '<a class="wplng-language website after current" ';
390 $html .= 'href="' . esc_url( $url_website ) . '" ';
391 $html .= 'onclick="event.preventDefault();">';
392
393 } elseif ( is_admin()
394 || ( 0 < strpos( $url_website, '/?et_fb=1' ) )
395 ) {
396
397 $html .= '<a class="wplng-language website after" ';
398 $html .= 'href="' . esc_url( $url_website ) . '" ';
399 $html .= 'onclick="event.preventDefault();">';
400
401 } else {
402
403 $html .= '<a class="wplng-language website after" ';
404 $html .= 'href="' . esc_url( $url_website ) . '">';
405 }
406
407 if ( $flags_show && ! empty( $language_website['flag'] ) ) {
408 $html .= '<img ';
409 $html .= 'src="' . esc_url( $language_website['flag'] ) . '" ';
410 $html .= 'alt="' . esc_attr( $language_website['name'] ) . '">';
411 }
412
413 $html .= '<span class="language-id">' . esc_html( $language_website['id'] ) . '</span>';
414 $html .= '<span class="language-name">' . esc_html( $language_website['name'] ) . '</span>';
415 $html .= '<span class="language-original">' . esc_html( $language_website['original'] ) . '</span>';
416 $html .= '</a>';
417
418 // Create link for each target languages
419 foreach ( $languages_target as $key => $language_target ) {
420
421 $url = wplng_get_url_current_for_language( $language_target['id'] );
422
423 if ( $language_target['id'] === $language_current_id ) {
424
425 $html .= '<a ';
426 $html .= 'class="wplng-language current" ';
427 $html .= 'href="' . esc_url( $url ) . '" ';
428 $html .= 'onclick="event.preventDefault();">';
429
430 } elseif ( is_admin()
431 || ( 0 < strpos( $url, '/?et_fb=1' ) )
432 ) {
433
434 $html .= '<a ';
435 $html .= 'class="wplng-language" ';
436 $html .= 'href="' . esc_url( $url ) . '" ';
437 $html .= 'onclick="event.preventDefault();">';
438
439 } else {
440
441 $html .= '<a ';
442 $html .= 'class="wplng-language" ';
443 $html .= 'href="' . esc_url( $url ) . '">';
444
445 }
446
447 if ( $flags_show && ! empty( $language_target['flag'] ) ) {
448 $html .= '<img ';
449 $html .= 'src="' . esc_url( $language_target['flag'] ) . '" ';
450 $html .= 'alt="' . esc_attr( $language_target['name'] ) . '">';
451 }
452
453 $html .= '<span class="language-id">' . esc_html( $language_target['id'] ) . '</span>';
454 $html .= '<span class="language-name">' . esc_html( $language_target['name'] ) . '</span>';
455 $html .= '<span class="language-original">' . esc_html( $language_target['original'] ) . '</span>';
456 $html .= '</a>';
457 }
458
459 $html .= '</div>'; // End .wplng-languages
460
461 // Create link for current language
462 if ( $language_website['id'] === $language_current_id ) {
463
464 $html .= '<a class="wplng-language wplng-language-current" ';
465 $html .= 'href="' . esc_url( $url_website ) . '" ';
466 $html .= 'onclick="event.preventDefault();">';
467
468 if ( $flags_show && ! empty( $language_website['flag'] ) ) {
469 $html .= '<img ';
470 $html .= 'src="' . esc_url( $language_website['flag'] ) . '" ';
471 $html .= 'alt="' . esc_attr( $language_website['name'] ) . '">';
472 }
473
474 $html .= '<span class="language-id">' . esc_html( $language_website['id'] ) . '</span>';
475 $html .= '<span class="language-name">' . esc_html( $language_website['name'] ) . '</span>';
476 $html .= '<span class="language-original">' . esc_html( $language_website['original'] ) . '</span>';
477 $html .= '</a>';
478
479 } else {
480
481 foreach ( $languages_target as $key => $language_target ) {
482
483 if ( $language_target['id'] !== $language_current_id ) {
484 continue;
485 }
486
487 $url = wplng_get_url_current_for_language( $language_target['id'] );
488
489 $html .= '<a ';
490 $html .= 'class="wplng-language wplng-language-current" ';
491 $html .= 'href="' . esc_url( $url ) . '" ';
492 $html .= 'onclick="event.preventDefault();">';
493
494 if ( $flags_show && ! empty( $language_target['flag'] ) ) {
495 $html .= '<img ';
496 $html .= 'src="' . esc_url( $language_target['flag'] ) . '" ';
497 $html .= 'alt="' . esc_attr( $language_target['name'] ) . '">';
498 }
499
500 $html .= '<span class="language-id">' . esc_html( $language_target['id'] ) . '</span>';
501 $html .= '<span class="language-name">' . esc_html( $language_target['name'] ) . '</span>';
502 $html .= '<span class="language-original">' . esc_html( $language_target['original'] ) . '</span>';
503 $html .= '</a>';
504 break;
505 }
506 }
507
508 $html .= '</div>'; // End .switcher-content
509 $html .= '</div>'; // End .wplng-switcher
510
511 $html = apply_filters(
512 'wplng_switcher_html',
513 $html,
514 $language_website,
515 $languages_target
516 );
517
518 return $html;
519 }
520