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