PluginProbe
Polylang / 1.6.2
Polylang v1.6.2
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-languages.php

widget-languages.php in Polylang 1.6.2, at include/widget-languages.php

165 lines 4.6 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * the language switcher widget
5 *
6 * @since 0.1
7 */
8 class PLL_Widget_Languages extends WP_Widget {
9
10 /*
11 * constructor
12 *
13 * @since 0.1
14 */
15 function __construct() {
16 parent::__construct('polylang', __('Language Switcher', 'polylang'), array( 'description' => __( 'Displays a language switcher', 'polylang')));
17 }
18
19 /*
20 * displays the widget
21 *
22 * @since 0.1
23 *
24 * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
25 * @param array $instance The settings for the particular instance of the widget
26 */
27 function widget($args, $instance) {
28 global $polylang;
29 if (!(isset($polylang) && $polylang->model->get_languages_list() && $list = pll_the_languages(array_merge($instance, array('echo' => 0)))))
30 return;
31
32 echo $args['before_widget'];
33 if ($title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base))
34 echo $args['before_title'] . $title . $args['after_title'];
35 echo $instance['dropdown'] ? $list : "<ul>\n" . $list . "</ul>\n";
36 echo $args['after_widget'];
37
38 // javascript to switch the language when using a dropdown list
39 if ($instance['dropdown']) {
40 foreach ($polylang->model->get_languages_list() as $language) {
41 $url = $instance['force_home'] || ($url = $polylang->links->get_translation_url($language)) == null ? $polylang->links->get_home_url($language) : $url;
42 $urls[] = '"'.esc_js($language->slug).'":"'.esc_url($url).'"';
43 }
44
45 $urls = implode(',', $urls);
46
47 $js = "
48 <script type='text/javascript'>
49 //<![CDATA[
50 var urls = {{$urls}};
51 var d = document.getElementById('lang_choice');
52 d.onchange = function() {
53 for (var i in urls) {
54 if (this.value == i)
55 location.href = urls[i];
56 }
57 }
58 //]]>
59 </script>";
60
61 echo $js;
62 }
63 }
64
65 /*
66 * updates the widget options
67 *
68 * @since 0.4
69 *
70 * @param array $new_instance New settings for this instance as input by the user via form()
71 * @param array $old_instance Old settings for this instance
72 * @return array Settings to save or bool false to cancel saving
73 */
74 function update( $new_instance, $old_instance ) {
75 $instance['title'] = strip_tags($new_instance['title']);
76 foreach (array_keys(PLL_Switcher::get_switcher_options('widget')) as $key)
77 $instance[$key] = !empty($new_instance[$key]) ? 1 : 0;
78
79 return $instance;
80 }
81
82 /*
83 * displays the widget form
84 *
85 * @since 0.4
86 *
87 * @param array $instance Current settings
88 */
89 function form($instance) {
90 // default values
91 $instance = wp_parse_args( (array)$instance, array_merge(array('title' => ''), PLL_Switcher::get_switcher_options('widget', 'default')) );
92
93 // title
94 $title = sprintf(
95 '<p><label for="%1$s">%2$s</label><input class="widefat" id="%1$s" name="%3$s" type="text" value="%4$s" /></p>',
96 $this->get_field_id('title'),
97 __('Title:', 'polylang'),
98 $this->get_field_name('title'),
99 esc_attr($instance['title'])
100 );
101
102 $fields = '';
103 foreach (PLL_Switcher::get_switcher_options('widget') as $key => $str)
104 $fields .= sprintf(
105 '<div class = "%5$s" %6$s><input type="checkbox" class="checkbox" id="%1$s" name="%2$s" %3$s/> <label for="%1$s">%4$s</label></div>',
106 $this->get_field_id($key),
107 $this->get_field_name($key),
108 $instance[$key] ? 'checked="checked"' : '',
109 esc_html($str),
110 'dropdown' == $key ? '' : 'no-dropdown-' . $this->id,
111 'dropdown' == $key || empty($instance['dropdown']) ? '' : 'style="display:none;"'
112 );
113
114
115 echo $title.'<p>'.$fields.'</p>';
116
117 // FIXME echoing script in form is not very clean
118 // but it does not work if enqueued properly :
119 // clicking save on a widget makes this code unreachable for the just saved widget (?!)
120 $this->admin_print_script();
121 }
122
123 /*
124 * add javascript to control the language switcher options
125 *
126 * @since 1.3
127 */
128 public function admin_print_script() {
129 static $js = '';
130
131 if ($js)
132 return;
133
134 $js = "
135 <script type='text/javascript'>
136 //<![CDATA[
137 jQuery(document).ready(function($) {
138 function pll_toggle(a, test) {
139 test ? a.show() : a.hide();
140 }
141
142 var widgets = new Array();
143 $('.widget-id').each( function(){
144 var this_id = $(this).attr('value');
145
146 // remove all options if dropdown is checked
147 $('#widget-'+this_id+'-dropdown').change(function() {
148 pll_toggle($('.no-dropdown-'+this_id), 'checked' != $(this).attr('checked'));
149 });
150
151 // disallow unchecking both show names and show flags
152 $('#widget-'+this_id+'-show_flags').change(function() {
153 if ('checked' != $(this).attr('checked'))
154 $('#widget-'+this_id+'-show_names').prop('checked', true);
155 });
156
157 });
158 });
159 //]]>
160 </script>";
161
162 echo $js;
163 }
164 }
165