PluginProbe
Polylang / 2.3.6
Polylang v2.3.6
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 +24 -19 2.7.42.3.6 View file →
@@ -16,11 +16,11 @@
16 16 * @param string $type optional either 'menu' or 'widget', defaults to 'widget'
17 17 * @param string $key optional either 'string' or 'default', defaults to 'string'
18 18 * @return array list of switcher options strings or default values
19 19 */
20 - public static function get_switcher_options( $type = 'widget', $key = 'string' ) {
20 + static public function get_switcher_options( $type = 'widget', $key = 'string' ) {
21 21 $options = array(
22 - 'dropdown' => array( 'string' => __( 'Displays as a dropdown', 'polylang' ), 'default' => 0 ),
22 + 'dropdown' => array( 'string' => __( 'Displays as dropdown', 'polylang' ), 'default' => 0 ),
23 23 'show_names' => array( 'string' => __( 'Displays language names', 'polylang' ), 'default' => 1 ),
24 24 'show_flags' => array( 'string' => __( 'Displays flags', 'polylang' ), 'default' => 0 ),
25 25 'force_home' => array( 'string' => __( 'Forces link to front page', 'polylang' ), 'default' => 0 ),
26 26 'hide_current' => array( 'string' => __( 'Hides the current language', 'polylang' ), 'default' => 0 ),
@@ -32,9 +32,10 @@
32 32
33 33 /**
34 34 * Get the language elements for use in a walker
35 35 *
36 - * @see list of parameters accepted in $args documented for PLL_Switcher::the_languages
36 + * List of parameters accepted in $args:
37 + * @see PLL_Switcher::the_languages
37 38 *
38 39 * @since 1.2
39 40 *
40 41 * @param object $links instance of PLL_Frontend_Links
@@ -41,10 +42,10 @@
41 42 * @param array $args
42 43 * @return array
43 44 */
44 45 protected function get_elements( $links, $args ) {
46 +
45 47 $first = true;
46 - $out = array();
47 48
48 49 foreach ( $links->model->get_languages_list( array( 'hide_empty' => $args['hide_if_empty'] ) ) as $language ) {
49 50 $id = (int) $language->term_id;
50 51 $order = (int) $language->term_group;
@@ -52,17 +53,23 @@
52 53 $locale = $language->get_locale( 'display' );
53 54 $classes = array( 'lang-item', 'lang-item-' . $id, 'lang-item-' . esc_attr( $slug ) );
54 55 $url = null; // Avoids potential notice
55 56
57 + if ( $first ) {
58 + $classes[] = 'lang-item-first';
59 + $first = false;
60 + }
61 +
56 62 if ( $current_lang = $links->curlang->slug == $slug ) {
57 63 if ( $args['hide_current'] && ! ( $args['dropdown'] && ! $args['raw'] ) ) {
58 64 continue; // Hide current language except for dropdown
59 - } else {
65 + }
66 + else {
60 67 $classes[] = 'current-lang';
61 68 }
62 69 }
63 70
64 - if ( null !== $args['post_id'] && ( $tr_id = $links->model->post->get( $args['post_id'], $language ) ) && $links->model->post->current_user_can_read( $tr_id ) ) {
71 + if ( null !== $args['post_id'] && ( $tr_id = $links->model->post->get( $args['post_id'], $language ) ) && $links->current_user_can_read( $tr_id ) ) {
65 72 $url = get_permalink( $tr_id );
66 73 } elseif ( null === $args['post_id'] ) {
67 74 $url = $links->get_translation_url( $language );
68 75 }
@@ -91,17 +98,12 @@
91 98
92 99 $name = $args['show_names'] || ! $args['show_flags'] || $args['raw'] ? ( 'slug' == $args['display_names_as'] ? $slug : $language->name ) : '';
93 100 $flag = $args['raw'] && ! $args['show_flags'] ? $language->flag_url : ( $args['show_flags'] ? $language->flag : '' );
94 101
95 - if ( $first ) {
96 - $classes[] = 'lang-item-first';
97 - $first = false;
98 - }
99 -
100 102 $out[ $slug ] = compact( 'id', 'order', 'slug', 'locale', 'name', 'url', 'flag', 'current_lang', 'no_translation', 'classes' );
101 103 }
102 104
103 - return $out;
105 + return empty( $out ) ? array() : $out;
104 106 }
105 107
106 108 /**
107 109 * Displays a language switcher
@@ -182,15 +184,20 @@
182 184 *
183 185 * @param string $html html returned/outputted by the template tag
184 186 * @param array $args arguments passed to the template tag
185 187 */
186 - $out = apply_filters( 'pll_the_languages', $walker->walk( $elements, -1, $args ), $args );
188 + $out = apply_filters( 'pll_the_languages', $walker->walk( $elements, $args ), $args );
187 189
188 190 // Javascript to switch the language when using a dropdown list
189 191 if ( $args['dropdown'] ) {
192 + foreach ( $links->model->get_languages_list() as $language ) {
193 + $url = $links->get_translation_url( $language );
194 + $urls[ $language->slug ] = $args['force_home'] || empty( $url ) ? $links->get_home_url( $language ) : $url;
195 + }
196 +
190 197 // Accept only few valid characters for the urls_x variable name ( as the widget id includes '-' which is invalid )
191 - $out .= sprintf(
192 - '<script type="text/javascript">
198 + $out .= sprintf( '
199 + <script type="text/javascript">
193 200 //<![CDATA[
194 201 var %1$s = %2$s;
195 202 document.getElementById( "%3$s" ).onchange = function() {
196 203 location.href = %1$s[this.value];
@@ -196,16 +203,14 @@
196 203 location.href = %1$s[this.value];
197 204 }
198 205 //]]>
199 206 </script>',
200 - 'urls_' . preg_replace( '#[^a-zA-Z0-9]#', '', $args['dropdown'] ),
201 - wp_json_encode( wp_list_pluck( $elements, 'url' ) ),
202 - esc_js( $args['name'] )
207 + 'urls_' . preg_replace( '#[^a-zA-Z0-9]#', '', $args['dropdown'] ), json_encode( $urls ), esc_js( $args['name'] )
203 208 );
204 209 }
205 210
206 211 if ( $args['echo'] ) {
207 - echo $out; // phpcs:ignore WordPress.Security.EscapeOutput
212 + echo $out;
208 213 }
209 214 return $out;
210 215 }
211 216 }