| 1 |
<?php |
| 2 |
|
| 3 |
class Polylang_Widget extends WP_Widget { |
| 4 |
function __construct() { |
| 5 |
$widget_ops = array( 'description' => __( 'Displays a language switcher', 'polylang') ); |
| 6 |
parent::__construct('polylang', __('Language Switcher', 'polylang'), $widget_ops); |
| 7 |
} |
| 8 |
|
| 9 |
// displays the widget |
| 10 |
function widget($args, $instance) { |
| 11 |
extract($args); |
| 12 |
extract($instance); |
| 13 |
|
| 14 |
echo "$before_widget\n"; |
| 15 |
do_action('the_languages', $instance); |
| 16 |
echo "$after_widget\n"; |
| 17 |
|
| 18 |
// javascript to switch the language when using a dropdown list |
| 19 |
// keep the things simple for now as we switch to the posts page |
| 20 |
if ($dropdown) { |
| 21 |
$url = home_url('?lang='); |
| 22 |
$js = " |
| 23 |
<script type='text/javascript'> |
| 24 |
var d = document.getElementById('lang_choice'); |
| 25 |
d.onchange = function() {location.href ='$url'+this.value;} |
| 26 |
</script>"; |
| 27 |
|
| 28 |
echo $js; |
| 29 |
} |
| 30 |
} |
| 31 |
|
| 32 |
// updates the widget options |
| 33 |
function update( $new_instance, $old_instance ) { |
| 34 |
$instance = $old_instance; |
| 35 |
$instance['show_names'] = !empty($new_instance['show_names']) ? 1 : 0; |
| 36 |
$instance['show_flags'] = !empty($new_instance['show_flags']) ? 1 : 0; |
| 37 |
$instance['dropdown'] = !empty($new_instance['dropdown']) ? 1 : 0; |
| 38 |
|
| 39 |
return $instance; |
| 40 |
} |
| 41 |
|
| 42 |
// displays the widget form |
| 43 |
function form($instance) { |
| 44 |
$instance = wp_parse_args( (array) $instance, array( 'show_names' => 1, 'show_flags' => 0, 'dropdown' => 0) ); // default values |
| 45 |
|
| 46 |
// language names checkbox |
| 47 |
$id = $this->get_field_id('show_names'); |
| 48 |
$name = $this->get_field_name('show_names'); |
| 49 |
$output = sprintf('<input type="checkbox" class="checkbox" id="%s" name="%s"%s /> <label for="%s">%s</label><br />', |
| 50 |
$id, $name, $instance['show_names'] ? 'checked="checked"' : '', $id, __('Display language names', 'polylang')); |
| 51 |
|
| 52 |
// flags checkbox |
| 53 |
$id = $this->get_field_id('show_flags'); |
| 54 |
$name = $this->get_field_name('show_flags'); |
| 55 |
$output .= sprintf('<input type="checkbox" class="checkbox" id="%s" name="%s"%s /> <label for="%s">%s</label><br />', |
| 56 |
$id, $name, $instance['show_flags'] ? 'checked="checked"' : '', $id, __('Display flags', 'polylang')); |
| 57 |
|
| 58 |
// dropdown checkbox |
| 59 |
$id = $this->get_field_id('dropdown'); |
| 60 |
$name = $this->get_field_name('dropdown'); |
| 61 |
$output .= sprintf('<input type="checkbox" class="checkbox" id="%s" name="%s"%s /> <label for="%s">%s</label><br />', |
| 62 |
$id, $name, $instance['dropdown'] ? 'checked="checked"' : '', $id, __('Display as dropdown', 'polylang')); |
| 63 |
|
| 64 |
echo '<p>'.$output.'</p>'; |
| 65 |
} |
| 66 |
|
| 67 |
} // Class Polylang_Widget |
| 68 |
|
| 69 |
?> |
| 70 |
|