PluginProbe
Polylang / 2.1
Polylang v2.1
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 +23 -27 2.7.22.1 View file →
@@ -14,13 +14,13 @@
14 14 * @since 0.7
15 15 *
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 - * @return array list of switcher options strings or default values
18 + * @return array list of switcher options srings 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 }
@@ -86,22 +93,17 @@
86 93 if ( empty( $url ) && $args['hide_if_no_translation'] ) {
87 94 continue;
88 95 }
89 96
90 - $url = empty( $url ) || $args['force_home'] ? $links->get_home_url( $language ) : $url; // If the page is not translated, link to the home page
97 + $url = empty( $url ) || $args['force_home'] ? $links->get_home_url( $language ) : $url ; // If the page is not translated, link to the home page
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
@@ -113,15 +115,14 @@
113 115 * echo => echoes the list if set to 1, defaults to 1
114 116 * hide_if_empty => hides languages with no posts ( or pages ) if set to 1, defaults to 1
115 117 * show_flags => displays flags if set to 1, defaults to 0
116 118 * show_names => show language names if set to 1, defaults to 1
117 - * display_names_as => whether to display the language name or its slug, valid options are 'slug' and 'name', defaults to name
119 + * display_names_as => wether to display the language name or its slug, valid options are 'slug' and 'name', defaults to name
118 120 * force_home => will always link to home in translated language if set to 1, defaults to 0
119 121 * hide_if_no_translation => hide the link if there is no translation if set to 1, defaults to 0
120 122 * hide_current => hide the current language if set to 1, defaults to 0
121 123 * post_id => returns links to translations of post defined by post_id if set, defaults not set
122 124 * raw => return a raw array instead of html markup if set to 1, defaults to 0
123 - * item_spacing => whether to preserve or discard whitespace between list items, valid options are 'preserve' and 'discard', defaults to preserve
124 125 *
125 126 * @since 0.1
126 127 *
127 128 * @param object $links instance of PLL_Frontend_Links
@@ -141,9 +142,8 @@
141 142 'hide_if_no_translation' => 0, // don't hide the link if there is no translation
142 143 'hide_current' => 0, // don't hide current language
143 144 'post_id' => null, // if not null, link to translations of post defined by post_id
144 145 'raw' => 0, // set this to true to build your own custom language switcher
145 - 'item_spacing' => 'preserve', // 'preserve' or 'discard' whitespace between list items
146 146 );
147 147 $args = wp_parse_args( $args, $defaults );
148 148
149 149 /**
@@ -179,17 +179,15 @@
179 179 * Filter the whole html markup returned by the 'pll_the_languages' template tag
180 180 *
181 181 * @since 0.8
182 182 *
183 - * @param string $html html returned/outputted by the template tag
183 + * @param string $html html returned/outputed by the template tag
184 184 * @param array $args arguments passed to the template tag
185 185 */
186 - $out = apply_filters( 'pll_the_languages', $walker->walk( $elements, -1, $args ), $args );
186 + $out = apply_filters( 'pll_the_languages', $walker->walk( $elements, $args ), $args );
187 187
188 188 // Javascript to switch the language when using a dropdown list
189 189 if ( $args['dropdown'] ) {
190 - $urls = array();
191 -
192 190 foreach ( $links->model->get_languages_list() as $language ) {
193 191 $url = $links->get_translation_url( $language );
194 192 $urls[ $language->slug ] = $args['force_home'] || empty( $url ) ? $links->get_home_url( $language ) : $url;
195 193 }
@@ -194,10 +192,10 @@
194 192 $urls[ $language->slug ] = $args['force_home'] || empty( $url ) ? $links->get_home_url( $language ) : $url;
195 193 }
196 194
197 195 // Accept only few valid characters for the urls_x variable name ( as the widget id includes '-' which is invalid )
198 - $out .= sprintf(
199 - '<script type="text/javascript">
196 + $out .= sprintf( '
197 + <script type="text/javascript">
200 198 //<![CDATA[
201 199 var %1$s = %2$s;
202 200 document.getElementById( "%3$s" ).onchange = function() {
203 201 location.href = %1$s[this.value];
@@ -203,16 +201,14 @@
203 201 location.href = %1$s[this.value];
204 202 }
205 203 //]]>
206 204 </script>',
207 - 'urls_' . preg_replace( '#[^a-zA-Z0-9]#', '', $args['dropdown'] ),
208 - wp_json_encode( $urls ),
209 - esc_js( $args['name'] )
205 + 'urls_' . preg_replace( '#[^a-zA-Z0-9]#', '', $args['dropdown'] ), json_encode( $urls ), esc_js( $args['name'] )
210 206 );
211 207 }
212 208
213 209 if ( $args['echo'] ) {
214 - echo $out; // phpcs:ignore WordPress.Security.EscapeOutput
210 + echo $out;
215 211 }
216 212 return $out;
217 213 }
218 214 }