| @@ -8,9 +8,9 @@ | ||
| 8 | 8 | * |
| 9 | 9 | * @since 1.2 |
| 10 | 10 | */ |
| 11 | 11 | class PLL_Switcher { |
| 12 | - const DEFAULTS = array( | |
| 12 | + public const DEFAULTS = array( | |
| 13 | 13 | 'dropdown' => 0, // Display as list and not as dropdown. |
| 14 | 14 | 'echo' => 1, // Echoes the list. |
| 15 | 15 | 'hide_if_empty' => 1, // Hides languages with no posts (or pages). |
| 16 | 16 | 'show_flags' => 0, // Don't show flags. |
| @@ -26,9 +26,9 @@ | ||
| 26 | 26 | 'admin_current_lang' => null, // Use the global current language. |
| 27 | 27 | ); |
| 28 | 28 | |
| 29 | 29 | /** |
| 30 | - * @var PLL_Links | |
| 30 | + * @var PLL_Links|null | |
| 31 | 31 | */ |
| 32 | 32 | protected $links; |
| 33 | 33 | |
| 34 | 34 | /** |
| @@ -121,13 +121,21 @@ | ||
| 121 | 121 | $first = true; |
| 122 | 122 | $out = array(); |
| 123 | 123 | |
| 124 | 124 | foreach ( $this->links->model->get_languages_list( array( 'hide_empty' => $args['hide_if_empty'] ) ) as $language ) { |
| 125 | - $id = (int) $language->term_id; | |
| 126 | - $order = (int) $language->term_group; | |
| 127 | - $slug = $language->slug; | |
| 128 | - $locale = $language->get_locale( 'display' ); | |
| 129 | - $classes = array( 'lang-item', 'lang-item-' . $id, 'lang-item-' . esc_attr( $slug ) ); | |
| 125 | + $id = (int) $language->term_id; | |
| 126 | + $order = (int) $language->term_group; | |
| 127 | + $slug = $language->slug; | |
| 128 | + $locale = $language->get_locale( 'display' ); | |
| 129 | + $is_rtl = $language->is_rtl; | |
| 130 | + $item_classes = array( 'lang-item', 'lang-item-' . $id, 'lang-item-' . esc_attr( $slug ) ); | |
| 131 | + $classes = isset( $args['classes'] ) && is_array( $args['classes'] ) ? | |
| 132 | + array_merge( | |
| 133 | + $item_classes, | |
| 134 | + $args['classes'] | |
| 135 | + ) : | |
| 136 | + $item_classes; | |
| 137 | + $link_classes = $args['link_classes'] ?? array(); | |
| 130 | 138 | $current_lang = $this->get_current_language( $args ) === $slug; |
| 131 | 139 | |
| 132 | 140 | if ( $current_lang ) { |
| 133 | 141 | if ( $args['hide_current'] && ! ( $args['dropdown'] && ! $args['raw'] ) ) { |
| @@ -161,16 +169,23 @@ | ||
| 161 | 169 | |
| 162 | 170 | $url = empty( $url ) || $args['force_home'] ? $this->links->get_home_url( $language ) : $url; // If the page is not translated, link to the home page |
| 163 | 171 | |
| 164 | 172 | $name = $args['show_names'] || ! $args['show_flags'] || $args['raw'] ? ( 'slug' == $args['display_names_as'] ? $slug : $language->name ) : ''; |
| 165 | - $flag = $args['raw'] && ! $args['show_flags'] ? $language->get_display_flag_url() : ( $args['show_flags'] ? $language->get_display_flag() : '' ); | |
| 166 | 173 | |
| 174 | + if ( $args['raw'] && ! $args['show_flags'] ) { | |
| 175 | + $flag = $language->get_display_flag_url(); | |
| 176 | + } elseif ( $args['show_flags'] ) { | |
| 177 | + $flag = $language->get_display_flag( empty( $args['show_names'] ) ? 'alt' : 'no-alt' ); | |
| 178 | + } else { | |
| 179 | + $flag = ''; | |
| 180 | + } | |
| 181 | + | |
| 167 | 182 | if ( $first ) { |
| 168 | 183 | $classes[] = 'lang-item-first'; |
| 169 | 184 | $first = false; |
| 170 | 185 | } |
| 171 | 186 | |
| 172 | - $out[ $slug ] = compact( 'id', 'order', 'slug', 'locale', 'name', 'url', 'flag', 'current_lang', 'no_translation', 'classes' ); | |
| 187 | + $out[ $slug ] = compact( 'id', 'order', 'slug', 'locale', 'is_rtl', 'name', 'url', 'flag', 'current_lang', 'no_translation', 'classes', 'link_classes' ); | |
| 173 | 188 | } |
| 174 | 189 | |
| 175 | 190 | return $out; |
| 176 | 191 | } |
| @@ -184,26 +199,29 @@ | ||
| 184 | 199 | * @param PLL_Links $links Instance of PLL_Links. |
| 185 | 200 | * @param array $args { |
| 186 | 201 | * Optional array of arguments. |
| 187 | 202 | * |
| 188 | - * @type int $dropdown The list is displayed as dropdown if set, defaults to 0. | |
| 189 | - * @type int $echo Echoes the list if set to 1, defaults to 1. | |
| 190 | - * @type int $hide_if_empty Hides languages with no posts ( or pages ) if set to 1, defaults to 1. | |
| 191 | - * @type int $show_flags Displays flags if set to 1, defaults to 0. | |
| 192 | - * @type int $show_names Shows language names if set to 1, defaults to 1. | |
| 193 | - * @type string $display_names_as Whether to display the language name or its slug, valid options are 'slug' and 'name', defaults to name. | |
| 194 | - * @type int $force_home Will always link to home in translated language if set to 1, defaults to 0. | |
| 195 | - * @type int $hide_if_no_translation Hides the link if there is no translation if set to 1, defaults to 0. | |
| 196 | - * @type int $hide_current Hides the current language if set to 1, defaults to 0. | |
| 197 | - * @type int $post_id Returns links to the translations of the post defined by post_id if set, defaults not set. | |
| 198 | - * @type int $raw Return a raw array instead of html markup if set to 1, defaults to 0. | |
| 199 | - * @type string $item_spacing Whether to preserve or discard whitespace between list items, valid options are 'preserve' and 'discard', defaults to 'preserve'. | |
| 200 | - * @type int $admin_render Allows to force the current language code in an admin context if set, default to 0. Need to set the admin_current_lang argument below. | |
| 201 | - * @type string $admin_current_lang The current language code in an admin context. Need to set the admin_render to 1, defaults not set. | |
| 203 | + * @type int $dropdown The list is displayed as dropdown if set, defaults to 0. | |
| 204 | + * @type int $echo Echoes the list if set to 1, defaults to 1. | |
| 205 | + * @type int $hide_if_empty Hides languages with no posts ( or pages ) if set to 1, defaults to 1. | |
| 206 | + * @type int $show_flags Displays flags if set to 1, defaults to 0. | |
| 207 | + * @type int $show_names Shows language names if set to 1, defaults to 1. | |
| 208 | + * @type string $display_names_as Whether to display the language name or its slug, valid options are 'slug' and 'name', defaults to name. | |
| 209 | + * @type int $force_home Will always link to home in translated language if set to 1, defaults to 0. | |
| 210 | + * @type int $hide_if_no_translation Hides the link if there is no translation if set to 1, defaults to 0. | |
| 211 | + * @type int $hide_current Hides the current language if set to 1, defaults to 0. | |
| 212 | + * @type int $post_id Returns links to the translations of the post defined by post_id if set, defaults not set. | |
| 213 | + * @type int $raw Return a raw array instead of html markup if set to 1, defaults to 0. | |
| 214 | + * @type string $item_spacing Whether to preserve or discard whitespace between list items, valid options are 'preserve' and 'discard', defaults to 'preserve'. | |
| 215 | + * @type int $admin_render Allows to force the current language code in an admin context if set, default to 0. Need to set the admin_current_lang argument below. | |
| 216 | + * @type string $admin_current_lang The current language code in an admin context. Need to set the admin_render to 1, defaults not set. | |
| 217 | + * @type string[] $classes A list of CSS classes to set to each elements outputted. | |
| 218 | + * @type string[] $link_classes A list of CSS classes to set to each link outputted. | |
| 202 | 219 | * } |
| 203 | 220 | * @return string|array either the html markup of the switcher or the raw elements to build a custom language switcher |
| 204 | 221 | */ |
| 205 | 222 | public function the_languages( $links, $args = array() ) { |
| 223 | + | |
| 206 | 224 | $this->links = $links; |
| 207 | 225 | $args = wp_parse_args( $args, self::DEFAULTS ); |
| 208 | 226 | |
| 209 | 227 | /** |
| @@ -214,10 +232,15 @@ | ||
| 214 | 232 | * @param array $args |
| 215 | 233 | */ |
| 216 | 234 | $args = apply_filters( 'pll_the_languages_args', $args ); |
| 217 | 235 | |
| 218 | - // Prevents showing empty options in dropdown | |
| 219 | - if ( $args['dropdown'] ) { | |
| 236 | + // Force not to hide the language for the widget preview even if the option is checked. | |
| 237 | + if ( $this->links instanceof PLL_Admin_Links ) { | |
| 238 | + $args['hide_if_no_translation'] = 0; | |
| 239 | + } | |
| 240 | + | |
| 241 | + // Prevents showing empty options in `<select>`. | |
| 242 | + if ( $args['dropdown'] && ! $args['raw'] ) { | |
| 220 | 243 | $args['show_names'] = 1; |
| 221 | 244 | } |
| 222 | 245 | |
| 223 | 246 | $elements = $this->get_elements( $args ); |
| @@ -227,14 +250,21 @@ | ||
| 227 | 250 | } |
| 228 | 251 | |
| 229 | 252 | if ( $args['dropdown'] ) { |
| 230 | 253 | $args['name'] = 'lang_choice_' . $args['dropdown']; |
| 254 | + $args['class'] = 'pll-switcher-select'; | |
| 255 | + $args['value'] = 'url'; | |
| 256 | + $args['selected'] = $this->get_link( $this->links->model->get_language( $this->get_current_language( $args ) ), $args ); | |
| 231 | 257 | $walker = new PLL_Walker_Dropdown(); |
| 232 | - $args['selected'] = $this->get_current_language( $args ); | |
| 233 | 258 | } else { |
| 234 | 259 | $walker = new PLL_Walker_List(); |
| 235 | 260 | } |
| 236 | 261 | |
| 262 | + // Cast each element to stdClass because $walker::walk() expects an array of objects. | |
| 263 | + foreach ( $elements as $i => $element ) { | |
| 264 | + $elements[ $i ] = (object) $element; | |
| 265 | + } | |
| 266 | + | |
| 237 | 267 | /** |
| 238 | 268 | * Filter the whole html markup returned by the 'pll_the_languages' template tag |
| 239 | 269 | * |
| 240 | 270 | * @since 0.8 |
| @@ -243,22 +273,16 @@ | ||
| 243 | 273 | * @param array $args arguments passed to the template tag |
| 244 | 274 | */ |
| 245 | 275 | $out = apply_filters( 'pll_the_languages', $walker->walk( $elements, -1, $args ), $args ); |
| 246 | 276 | |
| 247 | - // Javascript to switch the language when using a dropdown list | |
| 277 | + // Javascript to switch the language when using a dropdown list. | |
| 248 | 278 | if ( $args['dropdown'] && 0 === $args['admin_render'] ) { |
| 249 | - // Accept only few valid characters for the urls_x variable name ( as the widget id includes '-' which is invalid ) | |
| 279 | + // Accept only few valid characters for the urls_x variable name (as the widget id includes '-' which is invalid). | |
| 250 | 280 | $out .= sprintf( |
| 251 | - '<script type="text/javascript"> | |
| 252 | - //<![CDATA[ | |
| 253 | - var %1$s = %2$s; | |
| 254 | - document.getElementById( "%3$s" ).onchange = function() { | |
| 255 | - location.href = %1$s[this.value]; | |
| 256 | - } | |
| 257 | - //]]> | |
| 281 | + '<script%1$s> | |
| 282 | + document.getElementById( "%2$s" ).addEventListener( "change", function ( event ) { location.href = event.currentTarget.value; } ) | |
| 258 | 283 | </script>', |
| 259 | - 'urls_' . preg_replace( '#[^a-zA-Z0-9]#', '', $args['dropdown'] ), | |
| 260 | - wp_json_encode( wp_list_pluck( $elements, 'url' ) ), | |
| 284 | + current_theme_supports( 'html5', 'script' ) ? '' : ' type="text/javascript"', | |
| 261 | 285 | esc_js( $args['name'] ) |
| 262 | 286 | ); |
| 263 | 287 | } |
| 264 | 288 | |