PluginProbe
Polylang / 3.1.4
Polylang v3.1.4
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 +35 -53 2.7.33.1.4 View file →
@@ -1,5 +1,8 @@
1 1 <?php
2 +/**
3 + * @package Polylang
4 + */
2 5
3 6 /**
4 7 * The language switcher widget
5 8 *
@@ -29,28 +32,52 @@
29 32 * @since 0.1
30 33 *
31 34 * @param array $args Display arguments including before_title, after_title, before_widget, and after_widget.
32 35 * @param array $instance The settings for the particular instance of the widget
36 + * @return void
33 37 */
34 38 public function widget( $args, $instance ) {
35 - // Sets a unique id for dropdown
36 - $instance['dropdown'] = empty( $instance['dropdown'] ) ? 0 : $args['widget_id'];
39 + // Sets a unique id for dropdown.
40 + $instance['dropdown'] = empty( $instance['dropdown'] ) ? 0 : $this->id;
37 41
38 42 if ( $list = pll_the_languages( array_merge( $instance, array( 'echo' => 0 ) ) ) ) {
39 43 $title = empty( $instance['title'] ) ? '' : $instance['title'];
44 +
40 45 /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */
41 46 $title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
42 47
43 48 echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput
49 +
44 50 if ( $title ) {
45 51 echo $args['before_title'] . $title . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput
46 52 }
53 +
54 + // The title may be filtered: Strip out HTML and make sure the aria-label is never empty.
55 + $aria_label = trim( wp_strip_all_tags( $title ) );
56 + if ( ! $aria_label ) {
57 + $aria_label = __( 'Choose a language', 'polylang' );
58 + }
59 +
47 60 if ( $instance['dropdown'] ) {
48 - echo '<label class="screen-reader-text" for="' . esc_attr( 'lang_choice_' . $instance['dropdown'] ) . '">' . esc_html__( 'Choose a language', 'polylang' ) . '</label>';
61 + echo '<label class="screen-reader-text" for="' . esc_attr( 'lang_choice_' . $instance['dropdown'] ) . '">' . esc_html( $aria_label ) . '</label>';
49 62 echo $list; // phpcs:ignore WordPress.Security.EscapeOutput
50 63 } else {
64 + $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml';
65 +
66 + /** This filter is documented in wp-includes/widgets/class-wp-nav-menu-widget.php */
67 + $format = apply_filters( 'navigation_widgets_format', $format );
68 +
69 + if ( 'html5' === $format ) {
70 + echo '<nav role="navigation" aria-label="' . esc_attr( $aria_label ) . '">';
71 + }
72 +
51 73 echo "<ul>\n" . $list . "</ul>\n"; // phpcs:ignore WordPress.Security.EscapeOutput
74 +
75 + if ( 'html5' === $format ) {
76 + echo '</nav>';
77 + }
52 78 }
79 +
53 80 echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput
54 81 }
55 82 }
56 83
@@ -62,9 +89,9 @@
62 89 * @param array $new_instance New settings for this instance as input by the user via form()
63 90 * @param array $old_instance Old settings for this instance
64 91 * @return array Settings to save or bool false to cancel saving
65 92 */
66 - public function update( $new_instance, $old_instance ) {
93 + public function update( $new_instance, $old_instance ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable
67 94 $instance = array( 'title' => sanitize_text_field( $new_instance['title'] ) );
68 95 foreach ( array_keys( PLL_Switcher::get_switcher_options( 'widget' ) ) as $key ) {
69 96 $instance[ $key ] = ! empty( $new_instance[ $key ] ) ? 1 : 0;
70 97 }
@@ -72,13 +99,14 @@
72 99 return $instance;
73 100 }
74 101
75 102 /**
76 - * Displays the widget form
103 + * Displays the widget form.
77 104 *
78 105 * @since 0.4
79 106 *
80 - * @param array $instance Current settings
107 + * @param array $instance Current settings.
108 + * @return string
81 109 */
82 110 public function form( $instance ) {
83 111 // Default values
84 112 $instance = wp_parse_args( (array) $instance, array_merge( array( 'title' => '' ), PLL_Switcher::get_switcher_options( 'widget', 'default' ) ) );
@@ -104,53 +132,7 @@
104 132 esc_attr( 'pll-' . $key )
105 133 );
106 134 }
107 135
108 - // FIXME echoing script in form is not very clean
109 - // but it does not work if enqueued properly :
110 - // clicking save on a widget makes this code unreachable for the just saved widget ( ?! )
111 - $this->admin_print_script();
112 - }
113 -
114 - /**
115 - * Add javascript to control the language switcher options
116 - *
117 - * @since 1.3
118 - */
119 - public function admin_print_script() {
120 - static $done = false;
121 -
122 - if ( $done ) {
123 - return;
124 - }
125 -
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 - }
134 -
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 - } );
140 -
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
136 + return ''; // Because the parent class returns a string, however not used.
155 137 }
156 138 }