/
uls-functions.php
706 lines (643 loc) · 24.2 KB
/
uls-functions.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
<?php
/**
* This file contains general functions to use in themes or other plugins.
*/
/**
* Get the general options saved for the plugin.
*
* @return array associative array with the options for the plugin.
*/
function uls_get_options(){
//get the general options
$options = get_option('uls_settings');
//default values
$defaults = array(
'default_backend_language' => null,
'default_frontend_language' => null,
'user_backend_configuration' => true,
'user_frontend_configuration' => true,
'backend_language_field_name' => 'uls_backend_language',
'frontend_language_field_name' => 'uls_frontend_language',
'url_type' => 'prefix',
);
//merge with default values
return array_merge($defaults, $options);
}
/**
* This function returns the URL used in the browser.
*
* @return string URL in the browser.
*/
function uls_get_browser_url(){
if(!isset($_SERVER['HTTP_HOST']) || !isset($_SERVER['REQUEST_URI'])) {
return false;
}
$url =(isset($_SERVER["HTTPS"]) && $_SERVER["HTTPS"]=="on") ? "https://" : "http://";
$url .= $_SERVER["HTTP_HOST"] . $_SERVER["REQUEST_URI"];
return $url;
}
/**
* Return the permalink of the translation link of a post.
*
* @param $post_id integer id of post.
* @param $language string language of translation. If it is null or invalid, current language loaded in the page is used.
*
* @return string the permalink of the translation link of a post.
*/
function uls_get_permalink($post_id, $language = null){
$translation_id = uls_get_post_translation_id($post_id, $language);
return empty($translation_id) ? get_permalink($post_id) : get_permalink($translation_id);
}
/**
* Return the HTML link of the translation of a post.
*
* @param $post_id integer id of post. If it is null, then it converts the current URL with the language specified.
* @param $language string language of translation. If it is null or invalid, current language loaded in the page is used.
* @param $label string inner text of the link.
* @param $class string text to include as class parameter in the link
*
* @return string the HTML link of the translation link of a post.
*/
function uls_get_link($post_id = null, $language = null, $label = null, $class='uls-link' ){
// instance the atribute
$translation_url = "#";
if ($post_id == null) {
if (is_home() || is_front_page() || is_archive() || is_search() || is_author() || is_category() || is_tag() || is_date()) {
$url = uls_get_browser_url();
$translation_url = uls_get_url_translated($url, $language);
}
else if (is_search()) {
$url = get_home_url();
$url .= "?s=".get_search_query();
$translation_url = uls_get_url_translated($url, $language);
}
}
else {
$translation_id = uls_get_post_translation_id($post_id, $language);
if(empty($translation_id))
$translation_id = $post_id;
//set conversion of permalinks to true
global $uls_permalink_convertion;
$uls_permalink_convertion = true;
$translation_url = uls_get_url_translated(get_permalink($translation_id), $language);
//reset conversion of permalinks
$uls_permalink_convertion = false;
$title = get_the_title($translation_id);
}
if(null == $label)
return '<a class="' . $class . '" href="' . $translation_url . '" >' . $title . '</a>';
else
return '<a class="' . $class . '" href="' . $translation_url . '" >' . $label . '</a>';
}
/**
* Add shortcode to get link.
*/
add_shortcode('uls-link', 'uls_link_shortcode');
function uls_link_shortcode($atts){
extract( shortcode_atts( array(
'id' => null,
'lang' => uls_get_user_language(),
'post_type' => 'page',
'title' => null,
'label' => null
), $atts ) );
//get post
if(null == $id && null != $title){
$post = get_page_by_title($title, 'OBJECT', $post_type);
$id = $post->ID;
}
if(null != $id)
return uls_get_link($id, $lang, $label);
return null;
}
/**
* This function creates a set of links with available languages.
*
* @param $url string base URL to convert.
* @param $url_type string type of language flag to add in the URL (query_var, prefix, subdomain)
* @param $type string type of links to generate (links, select) (TO-DO: select isn't available yet.)
* @param $only_lang_name if it is true, then the links don't contain the country name.
* @param $class string additional CSS class to add in the div of links generated.
*
* @return string returns the HTML code with links to translated versions.
*/
function uls_language_link_switch($url = null, $url_type = 'prefix', $type = 'links', $only_lang_name = true, $class = null){
//if URL is null, then it uses the current URL
if(null == $url)
$url = uls_get_browser_url();
//get the available languages
$available_languages = uls_get_available_languages();
$class = (null == $class) ? 'uls-language-link-switch' : 'uls-language-link-switch ' . $class;
//get the current language
$current_language = uls_get_user_language();
if('' == $current_language)
$current_language = uls_get_site_language();
//set conversion of permalinks to true
global $uls_permalink_convertion;
$uls_permalink_convertion = true;
ob_start();
?>
<div class="<?php echo $class; ?>">
<?php
include 'uls-languages.php';
foreach($available_languages as $code):
if(isset($country_languages[$code])){
if($only_lang_name)
$label = substr($country_languages[$code], 0, strpos($country_languages[$code], ' '));
else
$label = $country_languages[$code];
}
else{
$label = $code;
}
$displayed = false;
if($code == $current_language):
$displayed = true;
?>
<span class="<?php echo 'selected-language'?>"><?php echo __($label, 'user-language-switch'); ?></span>
<?php else:
//$current_post_id = empty($post->ID) ? url_to_postid($url) : $post->ID;
$current_post_id = url_to_postid($url);
if(0 == $current_post_id && !is_home()){
wp_reset_query();
the_post();
$current_post_id = get_the_ID();
}
//if the current page has a post related
if(0 != $current_post_id):
$translation_id = uls_get_post_translation_id($current_post_id, $code);
if(false !== $translation_id):
$displayed = true;
?>
<a href="<?php echo uls_get_url_translated(get_permalink($translation_id), $code); ?>"><?php echo __($label, 'user-language-switch'); ?></a>
<?php endif; //translation url?>
<?php endif; //current_post_id?>
<?php endif;
//if the translation isn't displayed yet
if(!$displayed):
//get the translation based on the URL
$translation_url = uls_get_url_map_translation($url);
if(false !== $translation_url): ?>
<a href="<?php echo uls_get_url_translated($translation_url, $code); ?>"><?php echo __($label, 'user-language-switch'); ?></a>
<?php else: ?>
<a href="<?php echo uls_get_url_translated($url, $code); ?>"><?php echo __($label, 'user-language-switch'); ?></a>
<?php endif; ?>
<?php endif; //displayed ?>
<?php endforeach; ?>
</div>
<?php
$res = ob_get_contents();
ob_end_clean();
//reset conversion of permalinks
$uls_permalink_convertion = false;
return $res;
}
/**
* Add shortcode to add language versions
*/
add_shortcode('uls-language-selector', 'uls_language_selector_shortcode');
function uls_language_selector_shortcode($atts){
extract( shortcode_atts( array(
'url' => null,
'type' => 'links',
'url_type' => 'prefix',
'only_language' => true,
'class' => null
), $atts ) );
//if URL is null, then it uses the current URL
if(null == $url)
$url = uls_get_browser_url();
if("false" == $only_language)
$only_language = false;
return uls_language_link_switch($url, $url_type, $type, $only_language, $class);
}
/**
* This function creates a form to update language selection of an user to display in the front end side. If user isn't logged in or can't change the language, then it returns null.
*
* @param $default_language string language code used as default value of the input selector. If it is null, then the language saved by the user is selected.
* @param $label string label to use for the language field.
* @param $submit_label string label to use in the button to submit the form.
* @param $usccess_message string Message to display if language is saved successfully.
* @param $error_message string Message to display if language isn't saved.
*
* @return mixed HTML code of the form as a string. If user isn't logged in or user can't choose a language(settings of the plugin) then null is returned.
*/
function uls_create_user_language_switch_form($default_language = null, $label = null, $submit_label = null, $success_message = null, $error_message = null){
//check if user is logged in
if( ! is_user_logged_in() )
return null;
//check if the user can change the language
$options = get_option('uls_settings');
$type = 'frontend';
if( ! $options["user_{$type}_configuration"])
return null;
//get default values
$label = empty($label) ? __('Language', 'user-language-switch') : $label;
$submit_label = empty($submit_label) ? __('Save', 'user-language-switch') : $submit_label;
$success_message = empty($success_message) ? __('Language saved', 'user-language-switch') : $success_message;
$error_message = empty($error_message) ? __('Error saving language', 'user-language-switch') : $error_message;
//get user's language
$language = get_user_meta(get_current_user_id(), "uls_{$type}_language", true);
//set the default language if the user doesn't have a preference
if(empty($language))
$language = $options["default_{$type}_language"];
//available languages
$available_languages = uls_get_available_languages();
ob_start();
//include some JS libraries
wp_enqueue_script('jquery-form');
?>
<div class="uls-user-language-form-div">
<form id="uls_user_language_form" action="<?php echo admin_url('admin-ajax.php'); ?>" method="POST">
<input type="hidden" name="action" value="uls_user_language_switch" />
<?php if(function_exists("wp_nonce_field")): ?>
<?php wp_nonce_field('uls_user_language_switch','uls_wpnonce'); ?>
<?php endif; ?>
<label for="uls_language"><?php echo $label; ?></label>
<select id="uls_language" name="<?php echo $options['frontend_language_field_name']; ?>">
<?php foreach($available_languages as $langName => $langCode): ?>
<?php if($langCode == $language): ?>
<option value="<?php echo $langCode; ?>" selected="selected"><?php echo $langName; ?></option>
<?php else: ?>
<option value="<?php echo $langCode; ?>"><?php echo $langName; ?></option>
<?php endif; ?>
<?php endforeach; ?>
</select>
<div class="uls_submit_div">
<input type="submit" class="btn" value="<?php echo $submit_label; ?>" />
</div>
</form>
<div id="uls_user_language_message" class="uls_user_language_message"></div>
<script>
jQuery(document).ready(function(){
jQuery("form#uls_user_language_form").ajaxForm({
beforeSubmit: function(arr, $form, options) {
jQuery("div#uls_user_language_message").html("");
},
success: function (responseText, statusText, xhr, $form){
var $response = jQuery.parseJSON(responseText);
if($response.success)
jQuery("div#uls_user_language_message").html("<p class='success'><?php echo $success_message; ?></p>");
else
jQuery("div#uls_user_language_message").html("<p class='error'><?php echo $error_message; ?></p>");
},
error: function(){
jQuery("div#uls_user_language_message").html("<p class='error'><?php echo $error_message; ?></p>");
}
});
});
</script>
</div>
<?php
$res = ob_get_contents();
ob_end_clean();
return $res;
}
/**
* This function save the selection of a language by an user. It gets parameter values from POST variables.
*/
add_action('wp_ajax_uls_user_language_switch', 'uls_save_user_language');
function uls_save_user_language(){
//check parameters
if(empty($_POST) || ( function_exists('wp_verify_nonce') && !wp_verify_nonce($_POST['uls_wpnonce'],'uls_user_language_switch') )){
echo json_encode(array('success' => false));
exit;
}
//save settings for the user
$options = get_option('uls_settings');
//if user can save settings and there is a value
if($options["user_frontend_configuration"] && !empty($_POST[$options['frontend_language_field_name']]) && uls_valid_language($_POST[$options['frontend_language_field_name']]))
update_user_meta(get_current_user_id(), $options['frontend_language_field_name'], $_POST[$options['frontend_language_field_name']]);
echo json_encode(array('success' => true));
exit;
}
/**
* This function get the URL of the translation of an URL. It retrieve the URL from the mapping saved in the options page.
*
* @param $url string URL of the page to get the translation.
* @param $language string language of translation. If it is null or invalid, current language loaded in the page is used.
*
* @return string it returns the URL of the translation or false if the URL isn't contained in the mapping.
*/
function uls_get_url_map_translation($url, $language = null){
//get language
if(!uls_valid_language($language))
$language = uls_get_user_language();
//get the mappging
$options = get_option('uls_settings');
if(isset($options['translation_mapping'][$language][$url]))
return $options['translation_mapping'][$language][$url];
return false;
}
/*
* This shortcode print the languages flags where the user want add the code
* This shortcode have a value cols, this value is to print the shortcode with a number of cols
* @param $atts is the number of cols with you want to show flags
*/
add_shortcode('uls-languages-flags', 'uls_tap_shortcode_show_languages');
function uls_tap_shortcode_show_languages($atts) {
$atts = shortcode_atts( array( 'cols' => 3), $atts );
$options = get_option('uls_settings');
// call the function that print the style for the flags
ob_start();
uls_tab_background_color_picker_shortcode($atts['cols']);
$shortcode_class = 'tab_background_color_picker_shortcode';
include('uls-tab-template.php');
$shortcode = ob_get_contents();
ob_end_clean();
return do_shortcode( $shortcode );
}
/*
* This function is for create the styles conditions for the shortcode
* @param: $no_flags is the number of cols to show the flags
*/
function uls_tab_background_color_picker_shortcode($no_flags) {
$options = get_option('uls_settings');
// value for tabStyle
$TabStyle = "";
// default or not tab background color
$TabBackground = isset($options['tab_color_picker_language_switch']) ? $options['tab_color_picker_language_switch'] : 'rgba(255, 255, 255, 0);';
$TabBackground = "background-color: ".$TabBackground.";";
// select the style for the position flags
$TabStyle = '#tab_background_color_picker_shortcode{ '.
$TabBackground.
'position: relative;
padding: 5px;
border-radius: 0 0 15px 15px;
z-index: 0;
width: '.( ($no_flags * 33) + 10).'px;
}
#tab_background_color_picker_shortcode .tab_flag {
width: 33px;
float: left;
}';
?>
<style type="text/css">
<?= $TabStyle; ?>
</style>
<?php
}
add_action('wp_footer', 'tap_user_language_switch');
// uls-tab-user-language-switch include the template to show flags
function tap_user_language_switch() {
$options = get_option('uls_settings');
if( isset($options['activate_tab_language_switch']) && $options['activate_tab_language_switch']){
$languages = uls_get_available_languages();
$position = $options['tab_position_language_switch'];
if ( is_home() || is_archive() || is_search() || is_category() || is_tag() || is_author() || is_date() )
$postId = null;
else
$postId = get_post()->ID;
include('uls-tab-template.php');
}
}
// this function is for create the styles conditions
add_action( 'wp_enqueue_scripts', 'uls_tab_background_color_picker' );
function uls_tab_background_color_picker() {
$options = get_option('uls_settings');
$position = isset($options['tab_position_language_switch']) ? $options['tab_position_language_switch'] : 'RM';
// value for tabStyle
$TabStyle = "";
// default or not tab background color
$TabBackground = isset($options['tab_color_picker_language_switch']) ? $options['tab_color_picker_language_switch'] : 'rgba(255, 255, 255, 0);';
$TabBackground = "background-color: ".$TabBackground.";";
// default or not tab fixed or absoluted
$TabFixed = isset($options['fixed_position_language_switch']) ? "position: fixed;" : "position: absolute;";
$bodyRelative = isset($options['fixed_position_language_switch']) ? "" : "position: relative;";
// select the style for the position flags
switch($position) {
case 'TL':
$TabStyle = "#tab_background_color_picker{
top: 0;
left: 0;
width: auto; ".
$TabFixed.
$TabBackground."
padding: 5px 5px 5px 10px;
border-radius: 0 0 15px 15px;
z-index: 10000000000;
}
#tab_background_color_picker
.tab_flag {
display: inline;
}";
break;
case 'TC':
$TabStyle = "#tab_background_color_picker{
top: 0;
left: 50%;
width: auto;".
$TabFixed.
$TabBackground."
padding: 5px 5px 5px 10px;
border-radius: 0 0 15px 15px;
z-index: 10000000000;
}
#tab_background_color_picker
.tab_flag {
display: inline;
}";
break;
case 'TR':
$TabStyle = "#tab_background_color_picker{
top: 0;
right: 0;
width: auto;".
$TabFixed.
$TabBackground."
padding: 5px 5px 5px 10px;
border-radius: 0 0 15px 15px;
z-index: 10000000000;
}
#tab_background_color_picker
.tab_flag {
display: inline;
}";
break;
case 'BL':
$TabStyle = "#tab_background_color_picker{
bottom: 0;
left: 0;
width: auto;".
$TabFixed.
$TabBackground."
padding: 5px 5px 5px 10px;
border-radius: 15px 15px 0 0;
z-index: 10000000000;
}
#tab_background_color_picker
.tab_flag {
display: inline;
}
body {
$bodyRelative
}";
break;
case 'BC':
$TabStyle = "#tab_background_color_picker{
bottom: 0;
left: 50%;
width: auto;".
$TabFixed.
$TabBackground."
padding: 5px 5px 5px 10px;
border-radius: 15px 15px 0 0;
z-index: 10000000000;
}
#tab_background_color_picker
.tab_flag {
display: inline;
}
body {
$bodyRelative
}";
break;
case 'BR':
$TabStyle = "#tab_background_color_picker{
bottom: 0;
right: 0;
width: auto;".
$TabFixed.
$TabBackground."
padding: 5px 5px 5px 10px;
border-radius: 15px 15px 0 0;
z-index: 10000000000;
}
#tab_background_color_picker
.tab_flag {
display: inline;
}
body {
$bodyRelative
}";
break;
case 'LT':
$TabStyle = "#tab_background_color_picker{
top: 0;
left: 0;
height: auto;".
$TabFixed.
$TabBackground."
padding: 5px 5px 5px 10px;
border-radius: 0 15px 15px 0;
z-index: 10000000000;
}";
break;
case 'LM':
$TabStyle = "#tab_background_color_picker{
top: 50%;
left: 0;
height: auto;".
$TabFixed.
$TabBackground."
padding: 5px 5px 5px 10px;
border-radius: 0 15px 15px 0;
z-index: 10000000000;
}";
break;
case 'LB':
$TabStyle = "#tab_background_color_picker{
bottom: 0;
left: 0;
height: auto;".
$TabFixed.
$TabBackground."
padding: 5px 5px 5px 10px;
border-radius: 0 15px 15px 0;
z-index: 10000000000;
}";
break;
case 'RT':
$TabStyle = "#tab_background_color_picker{
top: 0;
right: 0;
height: auto;".
$TabFixed.
$TabBackground."
padding: 5px 4px 5px 10px;
border-radius: 15px 0 0 15px;
z-index: 10000000000;
}";
break;
case 'RM':
$TabStyle = "#tab_background_color_picker{
top: 50%;
right: 0;
height: auto;".
$TabFixed.
$TabBackground."
padding: 5px 4px 5px 10px;
border-radius: 15px 0 0 15px;
z-index: 10000000000;
}";
break;
case 'RB':
$TabStyle = "#tab_background_color_picker{
bottom: 0;
right: 0;
height: auto;".
$TabFixed.
$TabBackground."
padding: 5px 4px 5px 10px;
border-radius: 15px 0 0 15px;
z-index: 10000000000;
}";
break;
}
?>
<style type="text/css">
<?= $TabStyle; ?>
</style>
<?php
}
// this functin action is for register sidebar if the checkbox in backend is enable
function uls_register_sidebar_laguages() {
global $wp_registered_sidebars;
$languages = uls_get_available_languages(); // get the all languages available in the wp
$options = get_option('uls_settings');
// delete language that is actually in the side, from available languages
$lang_code = uls_get_site_language();
foreach ( $languages as $lang_name)
if ( ($lang_name = array_search($lang_code, $languages)) !== false )
unset($languages[$lang_name]);
// create the N_sidebar X available_languages, but fir ask if the enable checkbox is true
if ( !isset($options['enable_translation_sidebars_language_switch']) || $options['enable_translation_sidebars_language_switch'] ) {
if ( function_exists('register_sidebar') ) {
$temporal_sidebars = $wp_registered_sidebars;
foreach ( $temporal_sidebars as $sidebar => $items) {
foreach ( $languages as $lang_name => $lang_code ) {
register_sidebar(array(
'name' => $items['name'] .' / '. $lang_name,
'id' => strtolower("uls_".$items['id'].'_'.$lang_code),
'description' => __($items['description']. ' / '.$lang_name, 'user-language-switch'),
'before_widget' => $items['before_widget'],
'after_widget' => $items['after_widget'],
'before_title' => $items['before_title'],
'after_title' => $items['after_title'],
));
}
}
}
}
}
add_action( 'widgets_init', 'uls_register_sidebar_laguages', 999 );
function uls_organize_widgets_sidebars($sidebars_widgets) {
$options = get_option('uls_settings');
if (!is_admin()) {
if ( !isset($options['enable_translation_sidebars_language_switch']) || $options['enable_translation_sidebars_language_switch'] ) {
$lang_code = strtolower('_'.uls_get_user_language());
$uls_code = 'uls_';
foreach ($sidebars_widgets as $sidebar => $widgets) {
if ( substr($sidebar,0,3) != $uls_code ) {
if ( !empty($sidebars_widgets[$uls_code.$sidebar.$lang_code]) ) {
$uls_widgets = $sidebars_widgets[$uls_code.$sidebar.$lang_code];
$sidebars_widgets[$sidebar] = $uls_widgets;
}
}
}
}
}
return $sidebars_widgets;
}
add_filter ( 'sidebars_widgets', 'uls_organize_widgets_sidebars', 10, 1);
?>