PluginProbe
Polylang / 0.5
Polylang v0.5
3.8.9 3.8.8 3.8.7 3.8.6 3.8.5 3.8.4 3.8.3 2.7 2.7.0.1 2.7.1 2.7.2 2.7.3 2.7.4 2.8 2.8.1 2.8.2 2.8.3 2.8.4 2.9 2.9.1 2.9.2 3.0 3.0.1 3.0.2 3.0.3 All 233 releases
polylang / include / widget.php

widget.php in Polylang 0.5, at include/widget.php

80 lines 2.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 extract($args);
13 extract($instance);
14
15 $title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base);
16
17 echo "$before_widget\n";
18 if ($title)
19 echo $before_title . $title . $after_title;
20 $polylang->the_languages($instance);
21 echo "$after_widget\n";
22
23 // javascript to switch the language when using a dropdown list
24 // keep the things simple for now as we switch to the posts page
25 if ($dropdown) {
26 $url = home_url('?lang=');
27 $home_url = get_option('home');
28 $options = get_option('polylang');
29 $default = $options['hide_default'] ? esc_js($options['default_lang']) : '';
30
31 $js = "
32 <script type='text/javascript'>
33 var d = document.getElementById('lang_choice');
34 d.onchange = function() {
35 if (this.value == '$default')
36 location.href = '$home_url';
37 else
38 location.href ='$url'+this.value;
39 }
40 </script>";
41
42 echo $js;
43 }
44 }
45
46 // updates the widget options
47 function update( $new_instance, $old_instance ) {
48 $instance['title'] = strip_tags($new_instance['title']);
49 foreach ( array('show_names', 'show_flags','dropdown', 'force_home') as $key)
50 $instance[$key] = !empty($new_instance[$key]) ? 1 : 0;
51
52 return $instance;
53 }
54
55 // displays the widget form
56 function form($instance) {
57 // default values
58 $instance = wp_parse_args( (array) $instance, array( 'title' => '', 'show_names' => 1, 'show_flags' => 0, 'dropdown' => 0, 'force_home' => 0) );
59
60 // title
61 $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>',
62 $this->get_field_id('title'), __('Title:', 'polylang'), $this->get_field_name('title'), esc_attr($instance['title']));
63
64 $widget_options = array (
65 'show_names' => __('Displays language names', 'polylang'),
66 'show_flags' => __('Displays flags', 'polylang'),
67 'dropdown' => __('Displays as dropdown', 'polylang'),
68 'force_home' => __('Forces link to front page', 'polylang')
69 );
70
71 $fields = '';
72 foreach ($widget_options as $key=>$str)
73 $fields .= sprintf('<input type="checkbox" class="checkbox" id="%1$s" name="%2$s" %3$s /> <label for="%1$s">%4$s</label><br />',
74 $this->get_field_id($key), $this->get_field_name($key), $instance[$key] ? 'checked="checked"' : '', esc_html($str));
75
76 echo $title.'<p>'.$fields.'</p>';
77 }
78 }
79 ?>
80