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