PluginProbe
Polylang / 0.9.3
Polylang v0.9.3
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.9.3, at include/widget.php

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