| 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 |
|
| 13 |
// prevents the function to be called from admin side where $polylang is not defined |
| 14 |
if (!isset($polylang)) |
| 15 |
return; |
| 16 |
|
| 17 |
extract($args); |
| 18 |
extract($instance); |
| 19 |
|
| 20 |
$title = apply_filters('widget_title', $title, $instance, $this->id_base); |
| 21 |
|
| 22 |
echo "$before_widget\n"; |
| 23 |
if ($title) |
| 24 |
echo $before_title . $title . $after_title; |
| 25 |
$polylang->the_languages($instance); |
| 26 |
echo "$after_widget\n"; |
| 27 |
|
| 28 |
// javascript to switch the language when using a dropdown list |
| 29 |
// keep the things simple for now as we switch to the posts page |
| 30 |
if ($dropdown) { |
| 31 |
$url = home_url('?lang='); |
| 32 |
$home_url = get_option('home'); |
| 33 |
$options = get_option('polylang'); |
| 34 |
$default = $options['hide_default'] ? esc_js($options['default_lang']) : ''; |
| 35 |
|
| 36 |
$js = " |
| 37 |
<script type='text/javascript'> |
| 38 |
var d = document.getElementById('lang_choice'); |
| 39 |
d.onchange = function() { |
| 40 |
if (this.value == '$default') |
| 41 |
location.href = '$home_url'; |
| 42 |
else |
| 43 |
location.href ='$url'+this.value; |
| 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('show_names', 'show_flags','dropdown', 'force_home') 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( 'title' => '', 'show_names' => 1, 'show_flags' => 0, 'dropdown' => 0, 'force_home' => 0) ); |
| 64 |
|
| 65 |
// title |
| 66 |
$title = sprintf('<p><label for="%1$s">%2$s</label><input class="widefat" id="%1$s" name="%3$s" type="text" value="%4$s" /></p>', |
| 67 |
$this->get_field_id('title'), __('Title:', 'polylang'), $this->get_field_name('title'), esc_attr($instance['title'])); |
| 68 |
|
| 69 |
$widget_options = array ( |
| 70 |
'show_names' => __('Displays language names', 'polylang'), |
| 71 |
'show_flags' => __('Displays flags', 'polylang'), |
| 72 |
'dropdown' => __('Displays as dropdown', 'polylang'), |
| 73 |
'force_home' => __('Forces link to front page', 'polylang') |
| 74 |
); |
| 75 |
|
| 76 |
$fields = ''; |
| 77 |
foreach ($widget_options as $key=>$str) |
| 78 |
$fields .= sprintf('<input type="checkbox" class="checkbox" id="%1$s" name="%2$s" %3$s /> <label for="%1$s">%4$s</label><br />', |
| 79 |
$this->get_field_id($key), $this->get_field_name($key), $instance[$key] ? 'checked="checked"' : '', esc_html($str)); |
| 80 |
|
| 81 |
echo $title.'<p>'.$fields.'</p>'; |
| 82 |
} |
| 83 |
} |
| 84 |
?> |
| 85 |
|