| @@ -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,9 +30,9 @@ | ||
| 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 ) { | |
| 34 | + function widget( $args, $instance ) { | |
| 38 | 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,19 +39,19 @@ | ||
| 42 | 39 | $title = empty( $instance['title'] ) ? '' : $instance['title']; |
| 43 | 40 | /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ |
| 44 | 41 | $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); |
| 45 | 42 | |
| 46 | - echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput | |
| 43 | + echo $args['before_widget']; | |
| 47 | 44 | if ( $title ) { |
| 48 | - echo $args['before_title'] . $title . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput | |
| 45 | + echo $args['before_title'] . $title . $args['after_title']; | |
| 49 | 46 | } |
| 50 | 47 | if ( $instance['dropdown'] ) { |
| 51 | - echo '<label class="screen-reader-text" for="' . esc_attr( 'lang_choice_' . $instance['dropdown'] ) . '">' . esc_html__( 'Choose a language', 'polylang' ) . '</label>'; | |
| 52 | - echo $list; // phpcs:ignore WordPress.Security.EscapeOutput | |
| 48 | + echo '<label class="screen-reader-text" for="' . esc_attr( 'lang_choice_' . $instance['dropdown'] ) . '">' . esc_html__( 'Choose a language', 'polylang' ). '</label>'; | |
| 49 | + echo $list; | |
| 53 | 50 | } else { |
| 54 | - echo "<ul>\n" . $list . "</ul>\n"; // phpcs:ignore WordPress.Security.EscapeOutput | |
| 51 | + echo "<ul>\n" . $list . "</ul>\n"; | |
| 55 | 52 | } |
| 56 | - echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput | |
| 53 | + echo $args['after_widget']; | |
| 57 | 54 | } |
| 58 | 55 | } |
| 59 | 56 | |
| 60 | 57 | /** |
| @@ -65,10 +62,10 @@ | ||
| 65 | 62 | * @param array $new_instance New settings for this instance as input by the user via form() |
| 66 | 63 | * @param array $old_instance Old settings for this instance |
| 67 | 64 | * @return array Settings to save or bool false to cancel saving |
| 68 | 65 | */ |
| 69 | - public function update( $new_instance, $old_instance ) { | |
| 70 | - $instance = array( 'title' => sanitize_text_field( $new_instance['title'] ) ); | |
| 66 | + function update( $new_instance, $old_instance ) { | |
| 67 | + $instance['title'] = strip_tags( $new_instance['title'] ); | |
| 71 | 68 | foreach ( array_keys( PLL_Switcher::get_switcher_options( 'widget' ) ) as $key ) { |
| 72 | 69 | $instance[ $key ] = ! empty( $new_instance[ $key ] ) ? 1 : 0; |
| 73 | 70 | } |
| 74 | 71 | |
| @@ -81,9 +78,9 @@ | ||
| 81 | 78 | * @since 0.4 |
| 82 | 79 | * |
| 83 | 80 | * @param array $instance Current settings |
| 84 | 81 | */ |
| 85 | - public function form( $instance ) { | |
| 82 | + function form( $instance ) { | |
| 86 | 83 | // Default values |
| 87 | 84 | $instance = wp_parse_args( (array) $instance, array_merge( array( 'title' => '' ), PLL_Switcher::get_switcher_options( 'widget', 'default' ) ) ); |
| 88 | 85 | |
| 89 | 86 | // Title |
| @@ -88,27 +85,30 @@ | ||
| 88 | 85 | |
| 89 | 86 | // Title |
| 90 | 87 | printf( |
| 91 | 88 | '<p><label for="%1$s">%2$s</label><input class="widefat" id="%1$s" name="%3$s" type="text" value="%4$s" /></p>', |
| 92 | - esc_attr( $this->get_field_id( 'title' ) ), | |
| 89 | + $this->get_field_id( 'title' ), | |
| 93 | 90 | esc_html__( 'Title:', 'polylang' ), |
| 94 | - esc_attr( $this->get_field_name( 'title' ) ), | |
| 91 | + $this->get_field_name( 'title' ), | |
| 95 | 92 | esc_attr( $instance['title'] ) |
| 96 | 93 | ); |
| 97 | 94 | |
| 95 | + $fields = ''; | |
| 98 | 96 | foreach ( PLL_Switcher::get_switcher_options( 'widget' ) as $key => $str ) { |
| 99 | - printf( | |
| 97 | + $fields .= sprintf( | |
| 100 | 98 | '<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>', |
| 101 | - esc_attr( $this->get_field_id( $key ) ), | |
| 102 | - esc_attr( $this->get_field_name( $key ) ), | |
| 103 | - checked( $instance[ $key ], true, false ), | |
| 99 | + $this->get_field_id( $key ), | |
| 100 | + $this->get_field_name( $key ), | |
| 101 | + $instance[ $key ] ? ' checked="checked"' : '', | |
| 104 | 102 | esc_html( $str ), |
| 105 | - in_array( $key, array( 'show_names', 'show_flags', 'hide_current' ) ) ? sprintf( ' class="no-dropdown-%s"', esc_attr( $this->id ) ) : '', | |
| 106 | - ( ! empty( $instance['dropdown'] ) && in_array( $key, array( 'show_names', 'show_flags', 'hide_current' ) ) ? ' style="display:none;"' : '' ), | |
| 107 | - esc_attr( 'pll-' . $key ) | |
| 103 | + in_array( $key, array( 'show_names', 'show_flags', 'hide_current' ) ) ? ' class="no-dropdown-' . $this->id . '"' : '', | |
| 104 | + ! empty( $instance['dropdown'] ) && in_array( $key, array( 'show_names', 'show_flags', 'hide_current' ) ) ? ' style="display:none;"' : '', | |
| 105 | + 'pll-' . $key | |
| 108 | 106 | ); |
| 109 | 107 | } |
| 110 | 108 | |
| 109 | + echo $fields; | |
| 110 | + | |
| 111 | 111 | // FIXME echoing script in form is not very clean |
| 112 | 112 | // but it does not work if enqueued properly : |
| 113 | 113 | // clicking save on a widget makes this code unreachable for the just saved widget ( ?! ) |
| 114 | 114 | $this->admin_print_script(); |
| @@ -125,10 +125,9 @@ | ||
| 125 | 125 | if ( $done ) { |
| 126 | 126 | return; |
| 127 | 127 | } |
| 128 | 128 | |
| 129 | - $done = true; | |
| 130 | - ?> | |
| 129 | + $done = true; ?> | |
| 131 | 130 | <script type='text/javascript'> |
| 132 | 131 | //<![CDATA[ |
| 133 | 132 | jQuery( document ).ready( function( $ ) { |
| 134 | 133 | function pll_toggle( a, test ) { |
| @@ -137,9 +136,9 @@ | ||
| 137 | 136 | |
| 138 | 137 | // Remove all options if dropdown is checked |
| 139 | 138 | $( '.widgets-sortables,.control-section-sidebar' ).on( 'change', '.pll-dropdown', function() { |
| 140 | 139 | var this_id = $( this ).parent().parent().parent().children( '.widget-id' ).attr( 'value' ); |
| 141 | - pll_toggle( $( '.no-dropdown-' + this_id ), true != $( this ).prop( 'checked' ) ); | |
| 140 | + pll_toggle( $( '.no-dropdown-' + this_id ), 'checked' != $( this ).attr( 'checked' ) ); | |
| 142 | 141 | } ); |
| 143 | 142 | |
| 144 | 143 | // Disallow unchecking both show names and show flags |
| 145 | 144 | var options = ['-show_flags', '-show_names']; |
| @@ -145,9 +144,9 @@ | ||
| 145 | 144 | var options = ['-show_flags', '-show_names']; |
| 146 | 145 | $.each( options, function( i, v ) { |
| 147 | 146 | $( '.widgets-sortables,.control-section-sidebar' ).on( 'change', '.pll' + v, function() { |
| 148 | 147 | var this_id = $( this ).parent().parent().parent().children( '.widget-id' ).attr( 'value' ); |
| 149 | - if ( true != $( this ).prop( 'checked' ) ) { | |
| 148 | + if ( 'checked' != $( this ).attr( 'checked' ) ) { | |
| 150 | 149 | $( '#widget-' + this_id + options[ 1-i ] ).prop( 'checked', true ); |
| 151 | 150 | } |
| 152 | 151 | } ); |
| 153 | 152 | } ); |
| @@ -152,8 +151,7 @@ | ||
| 152 | 151 | } ); |
| 153 | 152 | } ); |
| 154 | 153 | } ); |
| 155 | 154 | //]]> |
| 156 | - </script> | |
| 157 | - <?php | |
| 155 | + </script><?php | |
| 158 | 156 | } |
| 159 | 157 | } |