| @@ -1,89 +1,73 @@ | ||
| 1 | 1 | <?php |
| 2 | -/** | |
| 3 | - * @package Polylang | |
| 4 | - */ | |
| 5 | 2 | |
| 6 | -/** | |
| 7 | - * The language switcher widget | |
| 3 | +/* | |
| 4 | + * the language switcher widget | |
| 8 | 5 | * |
| 9 | 6 | * @since 0.1 |
| 10 | 7 | */ |
| 11 | 8 | class PLL_Widget_Languages extends WP_Widget { |
| 12 | 9 | |
| 13 | - /** | |
| 14 | - * Constructor | |
| 10 | + /* | |
| 11 | + * constructor | |
| 15 | 12 | * |
| 16 | 13 | * @since 0.1 |
| 17 | 14 | */ |
| 18 | - public function __construct() { | |
| 19 | - parent::__construct( | |
| 20 | - 'polylang', | |
| 21 | - __( 'Language switcher', 'polylang' ), | |
| 22 | - array( | |
| 23 | - 'description' => __( 'Displays a language switcher', 'polylang' ), | |
| 24 | - 'customize_selective_refresh' => true, | |
| 25 | - ) | |
| 26 | - ); | |
| 15 | + function __construct() { | |
| 16 | + parent::__construct('polylang', __('Language Switcher', 'polylang'), array( 'description' => __( 'Displays a language switcher', 'polylang'))); | |
| 27 | 17 | } |
| 28 | 18 | |
| 29 | - /** | |
| 30 | - * Displays the widget | |
| 19 | + /* | |
| 20 | + * displays the widget | |
| 31 | 21 | * |
| 32 | 22 | * @since 0.1 |
| 33 | 23 | * |
| 34 | - * @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. | |
| 35 | 25 | * @param array $instance The settings for the particular instance of the widget |
| 36 | - * @return void | |
| 37 | 26 | */ |
| 38 | - public function widget( $args, $instance ) { | |
| 39 | - // Sets a unique id for dropdown. | |
| 40 | - $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; | |
| 41 | 31 | |
| 42 | - if ( $list = pll_the_languages( array_merge( $instance, array( 'echo' => 0 ) ) ) ) { | |
| 43 | - $title = empty( $instance['title'] ) ? '' : $instance['title']; | |
| 32 | + extract($args); | |
| 33 | + extract($instance); | |
| 44 | 34 | |
| 45 | - /** This filter is documented in wp-includes/widgets/class-wp-widget-pages.php */ | |
| 46 | - $title = apply_filters( 'widget_title', $title, $instance, $this->id_base ); | |
| 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"; | |
| 47 | 40 | |
| 48 | - echo $args['before_widget']; // phpcs:ignore WordPress.Security.EscapeOutput | |
| 49 | - | |
| 50 | - if ( $title ) { | |
| 51 | - echo $args['before_title'] . $title . $args['after_title']; // phpcs:ignore WordPress.Security.EscapeOutput | |
| 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).'"'; | |
| 52 | 46 | } |
| 53 | 47 | |
| 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 | - } | |
| 48 | + $urls = implode(',', $urls); | |
| 59 | 49 | |
| 60 | - if ( $instance['dropdown'] ) { | |
| 61 | - echo '<label class="screen-reader-text" for="' . esc_attr( 'lang_choice_' . $instance['dropdown'] ) . '">' . esc_html( $aria_label ) . '</label>'; | |
| 62 | - echo $list; // phpcs:ignore WordPress.Security.EscapeOutput | |
| 63 | - } else { | |
| 64 | - $format = current_theme_supports( 'html5', 'navigation-widgets' ) ? 'html5' : 'xhtml'; | |
| 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>"; | |
| 65 | 63 | |
| 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 | - | |
| 73 | - echo "<ul>\n" . $list . "</ul>\n"; // phpcs:ignore WordPress.Security.EscapeOutput | |
| 74 | - | |
| 75 | - if ( 'html5' === $format ) { | |
| 76 | - echo '</nav>'; | |
| 77 | - } | |
| 78 | - } | |
| 79 | - | |
| 80 | - echo $args['after_widget']; // phpcs:ignore WordPress.Security.EscapeOutput | |
| 64 | + echo $js; | |
| 81 | 65 | } |
| 82 | 66 | } |
| 83 | 67 | |
| 84 | - /** | |
| 85 | - * Updates the widget options | |
| 68 | + /* | |
| 69 | + * updates the widget options | |
| 86 | 70 | * |
| 87 | 71 | * @since 0.4 |
| 88 | 72 | * |
| 89 | 73 | * @param array $new_instance New settings for this instance as input by the user via form() |
| @@ -89,50 +73,95 @@ | ||
| 89 | 73 | * @param array $new_instance New settings for this instance as input by the user via form() |
| 90 | 74 | * @param array $old_instance Old settings for this instance |
| 91 | 75 | * @return array Settings to save or bool false to cancel saving |
| 92 | 76 | */ |
| 93 | - public function update( $new_instance, $old_instance ) { // phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable | |
| 94 | - $instance = array( 'title' => sanitize_text_field( $new_instance['title'] ) ); | |
| 95 | - foreach ( array_keys( PLL_Switcher::get_switcher_options( 'widget' ) ) as $key ) { | |
| 96 | - $instance[ $key ] = ! empty( $new_instance[ $key ] ) ? 1 : 0; | |
| 97 | - } | |
| 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; | |
| 98 | 81 | |
| 99 | 82 | return $instance; |
| 100 | 83 | } |
| 101 | 84 | |
| 102 | - /** | |
| 103 | - * Displays the widget form. | |
| 85 | + /* | |
| 86 | + * displays the widget form | |
| 104 | 87 | * |
| 105 | 88 | * @since 0.4 |
| 106 | 89 | * |
| 107 | - * @param array $instance Current settings. | |
| 108 | - * @return string | |
| 90 | + * @param array $instance Current settings | |
| 109 | 91 | */ |
| 110 | - public function form( $instance ) { | |
| 111 | - // Default values | |
| 112 | - $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')) ); | |
| 113 | 95 | |
| 114 | - // Title | |
| 115 | - printf( | |
| 96 | + // title | |
| 97 | + $title = sprintf( | |
| 116 | 98 | '<p><label for="%1$s">%2$s</label><input class="widefat" id="%1$s" name="%3$s" type="text" value="%4$s" /></p>', |
| 117 | - esc_attr( $this->get_field_id( 'title' ) ), | |
| 118 | - esc_html__( 'Title:', 'polylang' ), | |
| 119 | - esc_attr( $this->get_field_name( 'title' ) ), | |
| 120 | - esc_attr( $instance['title'] ) | |
| 99 | + $this->get_field_id('title'), | |
| 100 | + __('Title:', 'polylang'), | |
| 101 | + $this->get_field_name('title'), | |
| 102 | + esc_attr($instance['title']) | |
| 121 | 103 | ); |
| 122 | 104 | |
| 123 | - foreach ( PLL_Switcher::get_switcher_options( 'widget' ) as $key => $str ) { | |
| 124 | - printf( | |
| 125 | - '<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>', | |
| 126 | - esc_attr( $this->get_field_id( $key ) ), | |
| 127 | - esc_attr( $this->get_field_name( $key ) ), | |
| 128 | - checked( $instance[ $key ], true, false ), | |
| 129 | - esc_html( $str ), | |
| 130 | - in_array( $key, array( 'show_names', 'show_flags', 'hide_current' ) ) ? sprintf( ' class="no-dropdown-%s"', esc_attr( $this->id ) ) : '', | |
| 131 | - ( ! empty( $instance['dropdown'] ) && in_array( $key, array( 'show_names', 'show_flags', 'hide_current' ) ) ? ' style="display:none;"' : '' ), | |
| 132 | - 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;"' | |
| 133 | 115 | ); |
| 134 | - } | |
| 135 | 116 | |
| 136 | - return ''; // Because the parent class returns a string, however not used. | |
| 117 | + | |
| 118 | + echo $title.'<p>'.$fields.'</p>'; | |
| 119 | + | |
| 120 | + // FIXME echoing script in form is not very clean | |
| 121 | + // but it does not work if enqueued properly : | |
| 122 | + // clicking save on a widget makes this code unreachable for the just saved widget (?!) | |
| 123 | + $this->admin_print_script(); | |
| 124 | + } | |
| 125 | + | |
| 126 | + /* | |
| 127 | + * add javascript to control the language switcher options | |
| 128 | + * | |
| 129 | + * @since 1.3 | |
| 130 | + */ | |
| 131 | + public function admin_print_script() { | |
| 132 | + static $js = ''; | |
| 133 | + | |
| 134 | + if ($js) | |
| 135 | + return; | |
| 136 | + | |
| 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 | + } | |
| 144 | + | |
| 145 | + var widgets = new Array(); | |
| 146 | + $('.widget-id').each( function(){ | |
| 147 | + var this_id = $(this).attr('value'); | |
| 148 | + | |
| 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; | |
| 137 | 166 | } |
| 138 | 167 | } |