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