| 1 |
<?php |
| 2 |
|
| 3 |
// the language switcher widget |
| 4 |
class Polylang_Widget extends WP_Widget { |
| 5 |
function __construct() { |
| 6 |
parent::__construct('polylang', __('Language Switcher', 'polylang'), array( 'description' => __( 'Displays a language switcher', 'polylang'))); |
| 7 |
} |
| 8 |
|
| 9 |
// displays the widget |
| 10 |
function widget($args, $instance) { |
| 11 |
global $polylang; |
| 12 |
if (!(isset($polylang) && $polylang->get_languages_list() && $list = $polylang->the_languages(array_merge($instance, array('echo' => 0))))) |
| 13 |
return; |
| 14 |
|
| 15 |
extract($args); |
| 16 |
extract($instance); |
| 17 |
|
| 18 |
echo "$before_widget\n"; |
| 19 |
if ($title = apply_filters('widget_title', $title, $instance, $this->id_base)) |
| 20 |
echo $before_title . $title . $after_title; |
| 21 |
echo $dropdown ? $list : "<ul>\n" . $list . "</ul>\n"; |
| 22 |
echo "$after_widget\n"; |
| 23 |
|
| 24 |
// javascript to switch the language when using a dropdown list |
| 25 |
if ($dropdown) { |
| 26 |
foreach ($polylang->get_languages_list() as $language) { |
| 27 |
$url = $force_home || ($url = $polylang->get_translation_url($language)) == null ? $polylang->get_home_url($language) : $url; |
| 28 |
$urls[] = '"'.esc_js($language->slug).'":"'.esc_url($url).'"'; |
| 29 |
} |
| 30 |
|
| 31 |
$urls = implode(',', $urls); |
| 32 |
|
| 33 |
$js = " |
| 34 |
<script type='text/javascript'> |
| 35 |
//<![CDATA[ |
| 36 |
var urls = {{$urls}}; |
| 37 |
var d = document.getElementById('lang_choice'); |
| 38 |
d.onchange = function() { |
| 39 |
for (var i in urls) { |
| 40 |
if (this.value == i) |
| 41 |
location.href = urls[i]; |
| 42 |
} |
| 43 |
} |
| 44 |
//]]> |
| 45 |
</script>"; |
| 46 |
|
| 47 |
echo $js; |
| 48 |
} |
| 49 |
} |
| 50 |
|
| 51 |
// updates the widget options |
| 52 |
function update( $new_instance, $old_instance ) { |
| 53 |
$instance['title'] = strip_tags($new_instance['title']); |
| 54 |
foreach (array_keys($GLOBALS['polylang']->get_switcher_options('widget')) as $key) |
| 55 |
$instance[$key] = !empty($new_instance[$key]) ? 1 : 0; |
| 56 |
|
| 57 |
return $instance; |
| 58 |
} |
| 59 |
|
| 60 |
// displays the widget form |
| 61 |
function form($instance) { |
| 62 |
// default values |
| 63 |
$instance = wp_parse_args( (array)$instance, array_merge(array('title' => ''), $GLOBALS['polylang']->get_switcher_options('widget', 'default')) ); |
| 64 |
|
| 65 |
// title |
| 66 |
$title = sprintf( |
| 67 |
'<p><label for="%1$s">%2$s</label><input class="widefat" id="%1$s" name="%3$s" type="text" value="%4$s" /></p>', |
| 68 |
$this->get_field_id('title'), |
| 69 |
__('Title:', 'polylang'), |
| 70 |
$this->get_field_name('title'), |
| 71 |
esc_attr($instance['title']) |
| 72 |
); |
| 73 |
|
| 74 |
$fields = ''; |
| 75 |
foreach ($GLOBALS['polylang']->get_switcher_options('widget') as $key => $str) |
| 76 |
$fields .= sprintf( |
| 77 |
'<input type="checkbox" class="checkbox" id="%1$s" name="%2$s" %3$s /> <label for="%1$s">%4$s</label><br />', |
| 78 |
$this->get_field_id($key), |
| 79 |
$this->get_field_name($key), |
| 80 |
$instance[$key] ? 'checked="checked"' : '', |
| 81 |
esc_html($str) |
| 82 |
); |
| 83 |
|
| 84 |
echo $title.'<p>'.$fields.'</p>'; |
| 85 |
} |
| 86 |
} |
| 87 |
|