PluginProbe
Translate and Go multilingual – Automatic AI translation – wpLingua / 2.16.6
Translate and Go multilingual – Automatic AI translation – wpLingua v2.16.6
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 1.1.1 All 111 releases
wplingua / inc / switcher.php

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

559 lines 13.2 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-center';
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-simple-smooth';
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 || (
136 'none' === wplng_get_switcher_flags_style()
137 && 'none' === $name_format
138 )
139 ) {
140 $name_format = 'original';
141 }
142
143 $name_format = apply_filters(
144 'wplng_switcher_name_format',
145 $name_format
146 );
147
148 return $name_format;
149 }
150
151
152 /**
153 * Get wpLingua switcher option : Flags style
154 *
155 * @return string
156 */
157 function wplng_get_switcher_flags_style() {
158
159 $flags_style = get_option( 'wplng_flags_style' );
160 $is_valid = false;
161
162 if ( ! empty( $flags_style ) ) {
163
164 $valid_flags_style = wplng_data_switcher_valid_flags_style();
165
166 foreach ( $valid_flags_style as $id => $name ) {
167 if ( $flags_style === $id ) {
168 $is_valid = true;
169 break;
170 }
171 }
172 }
173
174 if ( ! $is_valid ) {
175 $flags_style = 'rectangular';
176 }
177
178 return $flags_style;
179 }
180
181
182 /**
183 * Get wpLingua switcher option : Class
184 *
185 * @param array $arg
186 * @return string
187 */
188 function wplng_get_switcher_class( $arg = array() ) {
189
190 $class = '';
191
192 /**
193 * Define insert class (list, block)
194 */
195 if ( ! empty( $arg['insert'] )
196 && array_key_exists( $arg['insert'], wplng_data_switcher_valid_insert() )
197 ) {
198 $class = 'insert-' . $arg['insert'];
199 } else {
200 $class = 'insert-' . wplng_get_switcher_insert();
201 }
202
203 /**
204 * Define style class (list, block)
205 */
206 if ( ! empty( $arg['style'] )
207 && array_key_exists( $arg['style'], wplng_data_switcher_valid_style() )
208 ) {
209 $class .= ' style-' . $arg['style'];
210 } else {
211 $class .= ' style-' . wplng_get_switcher_style();
212 }
213
214 /**
215 * Define language name class (id, name, none)
216 */
217
218 if ( ! empty( $arg['title'] )
219 && array_key_exists( $arg['title'], wplng_data_switcher_valid_name_format() )
220 ) {
221 $class .= ' title-' . $arg['title'];
222 } else {
223 $class .= ' title-' . wplng_get_switcher_name_format();
224 }
225
226 /**
227 * Define theme class (light, dark)
228 */
229 if ( ! empty( $arg['theme'] )
230 && array_key_exists( $arg['theme'], wplng_data_switcher_valid_theme() )
231 ) {
232 $class .= ' theme-' . $arg['theme'];
233 } else {
234 $class .= ' theme-' . wplng_get_switcher_theme();
235 }
236
237 /**
238 * Define flags theme class (id, name, none)
239 */
240 if ( ! empty( $arg['flags'] )
241 && array_key_exists( $arg['flags'], wplng_data_switcher_valid_flags_style() )
242 ) {
243 $class .= ' flags-' . $arg['flags'];
244 } else {
245 $class .= ' flags-' . wplng_get_switcher_flags_style();
246 }
247
248 /**
249 * Define additional class (id, name, none)
250 */
251 if ( ! empty( $arg['class'] ) ) {
252 $class .= ' ' . $arg['class'];
253 }
254
255 return $class;
256 }
257
258
259 /**
260 * Print HTML of wpLingua switcher if it's inserted automaticaly
261 *
262 * @return string
263 */
264 function wplng_switcher_wp_footer() {
265
266 if ( ! wplng_url_is_translatable()
267 || 'none' === wplng_get_switcher_insert()
268 ) {
269 return;
270 }
271
272 echo wplng_get_switcher_html(
273 array(
274 'class' => 'insert-auto',
275 )
276 );
277 }
278
279
280 /**
281 * Get HTML of wpLingua switcher
282 *
283 * @param array $arg
284 * @return string
285 */
286 function wplng_get_switcher_html( $arg = array() ) {
287
288 $is_admin = is_admin() || ( defined( 'REST_REQUEST' ) && REST_REQUEST );
289
290 if ( ! $is_admin && ! wplng_url_is_translatable() ) {
291 return '';
292 }
293
294 $language_website = wplng_get_language_website();
295 $language_current_id = wplng_get_language_current_id();
296 $languages_target = wplng_get_languages_target();
297 $class = wplng_get_switcher_class( $arg );
298 $flags_style = wplng_get_switcher_flags_style();
299 $url_website = wplng_get_url_original();
300 $flags_show = true;
301
302 $url_website = remove_query_arg(
303 array(
304 'wplng-mode',
305 'wplng-load',
306 'nocache',
307 ),
308 $url_website
309 );
310
311 if ( $is_admin ) {
312 $flags_show = true;
313 $url_website = '#';
314 } elseif ( ! empty( $arg['flags'] ) && 'none' === $arg['flags'] ) {
315 $flags_show = false;
316 } elseif ( 'none' === $flags_style ) {
317 $flags_show = false;
318 }
319
320 if ( empty( $languages_target ) ) {
321 return '';
322 }
323
324 /**
325 * Translate language names
326 */
327
328 // Untranslated target language names
329 foreach ( $languages_target as $key => $language_target ) {
330 $languages_target[ $key ]['original'] = wplng_get_language_name_untranslated(
331 $language_target
332 );
333 }
334
335 // Untranslated website language name
336 $language_website['original'] = wplng_get_language_name_untranslated(
337 $language_website
338 );
339
340 // Translate target language names
341 foreach ( $languages_target as $key => $language_target ) {
342 $languages_target[ $key ]['name'] = wplng_get_language_name_translated(
343 $language_target,
344 $language_current_id
345 );
346 }
347
348 // Translate website language name
349 $language_website['name'] = wplng_get_language_name_translated(
350 $language_website,
351 $language_current_id
352 );
353
354 /**
355 * Change flags if necessary
356 */
357
358 if ( ! empty( $arg['flags'] )
359 && ( 'none' !== $arg['flags'] )
360 && ( $flags_style !== $arg['flags'] )
361 ) {
362
363 // Change flags on website languages
364 if ( ! empty( $language_website['flag'] ) ) {
365 $language_website['flag'] = str_replace(
366 '/wplingua/assets/images/' . $flags_style . '/',
367 '/wplingua/assets/images/' . $arg['flags'] . '/',
368 $language_website['flag']
369 );
370 }
371
372 // Change flags on target languages
373 foreach ( $languages_target as $key => $language_target ) {
374
375 if ( empty( $language_target['flag'] ) ) {
376 continue;
377 }
378
379 $languages_target[ $key ]['flag'] = str_replace(
380 '/wplingua/assets/images/' . $flags_style . '/',
381 '/wplingua/assets/images/' . $arg['flags'] . '/',
382 $language_target['flag']
383 );
384 }
385 }
386
387 /**
388 * Create the switcher HTML
389 */
390
391 $html = '<div class="' . esc_attr( 'wplng-switcher ' . $class ) . '">';
392 $html .= '<div class="switcher-content">';
393
394 // Create link for current language
395 if ( $language_website['id'] === $language_current_id ) {
396
397 $html .= '<a';
398 $html .= ' class="wplng-language wplng-language-current"';
399 $html .= ' href="' . esc_url( $url_website ) . '"';
400 $html .= ' onclick="event.preventDefault();"';
401 $html .= ' data-wplng-lang-id="' . esc_attr( $language_website['id'] ) . '"';
402 $html .= '>';
403
404 if ( $flags_show && ! empty( $language_website['flag'] ) ) {
405 $html .= '<img';
406 $html .= ' src="' . esc_url( $language_website['flag'] ) . '"';
407 $html .= ' alt="' . esc_attr( $language_website['name'] ) . '"';
408 $html .= '>';
409 }
410
411 $html .= '<span class="language-id">' . esc_html( $language_website['id'] ) . '</span>';
412 $html .= '<span class="language-name">' . esc_html( $language_website['name'] ) . '</span>';
413 $html .= '<span class="language-original">' . esc_html( $language_website['original'] ) . '</span>';
414 $html .= '</a>';
415
416 } else {
417
418 foreach ( $languages_target as $key => $language_target ) {
419
420 if ( $language_target['id'] !== $language_current_id ) {
421 continue;
422 }
423
424 $url = wplng_get_url_current_for_language( $language_target['id'] );
425
426 $html .= '<a';
427 $html .= ' class="wplng-language wplng-language-current"';
428 $html .= ' href="' . esc_url( $url ) . '"';
429 $html .= ' onclick="event.preventDefault();"';
430 $html .= ' data-wplng-lang-id="' . esc_attr( $language_target['id'] ) . '"';
431 $html .= '>';
432
433 if ( $flags_show && ! empty( $language_target['flag'] ) ) {
434 $html .= '<img';
435 $html .= ' src="' . esc_url( $language_target['flag'] ) . '"';
436 $html .= ' alt="' . esc_attr( $language_target['name'] ) . '"';
437 $html .= '>';
438 }
439
440 $html .= '<span class="language-id">' . esc_html( $language_target['id'] ) . '</span>';
441 $html .= '<span class="language-name">' . esc_html( $language_target['name'] ) . '</span>';
442 $html .= '<span class="language-original">' . esc_html( $language_target['original'] ) . '</span>';
443 $html .= '</a>';
444 break;
445 }
446 }
447
448 /**
449 * Languages
450 */
451
452 $html .= '<div class="wplng-languages">';
453
454 // Create link for website language
455
456 if ( $language_website['id'] === $language_current_id ) {
457
458 $html .= '<a';
459 $html .= ' class="wplng-language website after current"';
460 $html .= ' href="' . esc_url( $url_website ) . '"';
461 $html .= ' onclick="event.preventDefault();"';
462 $html .= ' data-wplng-lang-id="' . esc_attr( $language_website['id'] ) . '"';
463 $html .= '>';
464
465 } elseif ( $is_admin || wplng_str_contains( $url_website, '/?et_fb=1' ) ) {
466
467 $html .= '<a';
468 $html .= ' class="wplng-language website after"';
469 $html .= ' href="' . esc_url( $url_website ) . '"';
470 $html .= ' onclick="event.preventDefault();"';
471 $html .= ' data-wplng-lang-id="' . esc_attr( $language_website['id'] ) . '"';
472 $html .= '>';
473
474 } else {
475
476 $html .= '<a';
477 $html .= ' class="wplng-language website after" ';
478 $html .= ' href="' . esc_url( $url_website ) . '"';
479 $html .= ' data-wplng-lang-id="' . esc_attr( $language_website['id'] ) . '"';
480 $html .= '>';
481 }
482
483 if ( $flags_show && ! empty( $language_website['flag'] ) ) {
484 $html .= '<img';
485 $html .= ' src="' . esc_url( $language_website['flag'] ) . '"';
486 $html .= ' alt="' . esc_attr( $language_website['name'] ) . '"';
487 $html .= '>';
488 }
489
490 $html .= '<span class="language-id">' . esc_html( $language_website['id'] ) . '</span>';
491 $html .= '<span class="language-name">' . esc_html( $language_website['name'] ) . '</span>';
492 $html .= '<span class="language-original">' . esc_html( $language_website['original'] ) . '</span>';
493 $html .= '</a>';
494
495 // Create link for each target languages
496 foreach ( $languages_target as $key => $language_target ) {
497
498 $url = '#';
499
500 if ( ! $is_admin ) {
501 $url = wplng_get_url_current_for_language( $language_target['id'] );
502 $url = remove_query_arg( array( 'wplng-load', 'nocache' ), $url );
503 }
504
505 if ( $language_target['id'] === $language_current_id ) {
506
507 $html .= '<a';
508 $html .= ' class="wplng-language current"';
509 $html .= ' href="' . esc_url( $url ) . '"';
510 $html .= ' onclick="event.preventDefault();"';
511 $html .= '>';
512
513 } elseif ( $is_admin || wplng_str_contains( $url, '/?et_fb=1' ) ) {
514
515 $html .= '<a';
516 $html .= ' class="wplng-language"';
517 $html .= ' href="' . esc_url( $url ) . '"';
518 $html .= ' onclick="event.preventDefault();"';
519 $html .= ' data-wplng-lang-id="' . esc_attr( $language_target['id'] ) . '"';
520 $html .= '>';
521
522 } else {
523
524 $html .= '<a';
525 $html .= ' class="wplng-language"';
526 $html .= ' href="' . esc_url( $url ) . '"';
527 $html .= ' data-wplng-lang-id="' . esc_attr( $language_target['id'] ) . '"';
528 $html .= '>';
529
530 }
531
532 if ( $flags_show && ! empty( $language_target['flag'] ) ) {
533 $html .= '<img';
534 $html .= ' src="' . esc_url( $language_target['flag'] ) . '"';
535 $html .= ' alt="' . esc_attr( $language_target['name'] ) . '"';
536 $html .= '>';
537 }
538
539 $html .= '<span class="language-id">' . esc_html( $language_target['id'] ) . '</span>';
540 $html .= '<span class="language-name">' . esc_html( $language_target['name'] ) . '</span>';
541 $html .= '<span class="language-original">' . esc_html( $language_target['original'] ) . '</span>';
542 $html .= '</a>';
543 }
544
545 $html .= '</div>'; // End .wplng-languages
546
547 $html .= '</div>'; // End .switcher-content
548 $html .= '</div>'; // End .wplng-switcher
549
550 $html = apply_filters(
551 'wplng_switcher_html',
552 $html,
553 $language_website,
554 $languages_target
555 );
556
557 return $html;
558 }
559