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