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 +152 -78 2.83.6.7 View file →
@@ -8,10 +8,31 @@
8 8 *
9 9 * @since 1.2
10 10 */
11 11 class PLL_Switcher {
12 + const DEFAULTS = array(
13 + 'dropdown' => 0, // Display as list and not as dropdown.
14 + 'echo' => 1, // Echoes the list.
15 + 'hide_if_empty' => 1, // Hides languages with no posts (or pages).
16 + 'show_flags' => 0, // Don't show flags.
17 + 'show_names' => 1, // Show language names.
18 + 'display_names_as' => 'name', // Display the language name.
19 + 'force_home' => 0, // Tries to find a translation.
20 + 'hide_if_no_translation' => 0, // Don't hide the link if there is no translation.
21 + 'hide_current' => 0, // Don't hide the current language.
22 + 'post_id' => null, // Link to the translations of the current page.
23 + 'raw' => 0, // Build the language switcher.
24 + 'item_spacing' => 'preserve', // Preserve whitespace between list items.
25 + 'admin_render' => 0, // Make the switcher in a frontend context.
26 + 'admin_current_lang' => null, // Use the global current language.
27 + );
12 28
13 29 /**
30 + * @var PLL_Links|null
31 + */
32 + protected $links;
33 +
34 + /**
14 35 * Returns options available for the language switcher - menu or widget
15 36 * either strings to display the options or default values
16 37 *
17 38 * @since 0.7
@@ -32,31 +53,89 @@
32 53 return wp_list_pluck( $options, $key );
33 54 }
34 55
35 56 /**
57 + * Returns the current language code.
58 + *
59 + * @since 3.0
60 + *
61 + * @param array $args Arguments passed to {@see PLL_Switcher::the_languages()}.
62 + * @return string
63 + */
64 + protected function get_current_language( $args ) {
65 + if ( $args['admin_current_lang'] ) {
66 + return $args['admin_current_lang'];
67 + }
68 +
69 + if ( isset( $this->links->curlang ) ) {
70 + return $this->links->curlang->slug;
71 + }
72 +
73 + return $this->links->options['default_lang'];
74 + }
75 +
76 + /**
77 + * Returns the link for a given language.
78 + *
79 + * @since 3.0
80 + *
81 + * @param PLL_Language $language Language.
82 + * @param array $args Arguments passed to {@see PLL_Switcher::the_languages()}.
83 + * @return string|null
84 + */
85 + protected function get_link( $language, $args ) {
86 + global $post;
87 +
88 + // Priority to the post passed in parameters.
89 + if ( null !== $args['post_id'] ) {
90 + $tr_id = $this->links->model->post->get( $args['post_id'], $language );
91 + if ( $tr_id && $this->links->model->post->current_user_can_read( $tr_id ) ) {
92 + return get_permalink( $tr_id );
93 + }
94 + }
95 +
96 + // If we are on frontend.
97 + if ( $this->links instanceof PLL_Frontend_Links ) {
98 + return $this->links->get_translation_url( $language );
99 + }
100 +
101 + // For blocks in posts in REST requests.
102 + if ( $post instanceof WP_Post ) {
103 + $tr_id = $this->links->model->post->get( $post->ID, $language );
104 + if ( $tr_id && $this->links->model->post->current_user_can_read( $tr_id ) ) {
105 + return get_permalink( $tr_id );
106 + }
107 + }
108 +
109 + return null;
110 + }
111 +
112 + /**
36 113 * Get the language elements for use in a walker
37 114 *
38 - * @see list of parameters accepted in $args documented for PLL_Switcher::the_languages
39 - *
40 115 * @since 1.2
41 116 *
42 - * @param object $links instance of PLL_Frontend_Links
43 - * @param array $args
44 - * @return array
117 + * @param array $args Arguments passed to {@see PLL_Switcher::the_languages()}.
118 + * @return array Language switcher elements.
45 119 */
46 - protected function get_elements( $links, $args ) {
120 + protected function get_elements( $args ) {
47 121 $first = true;
48 122 $out = array();
49 123
50 - foreach ( $links->model->get_languages_list( array( 'hide_empty' => $args['hide_if_empty'] ) ) as $language ) {
124 + foreach ( $this->links->model->get_languages_list( array( 'hide_empty' => $args['hide_if_empty'] ) ) as $language ) {
51 125 $id = (int) $language->term_id;
52 126 $order = (int) $language->term_group;
53 127 $slug = $language->slug;
54 128 $locale = $language->get_locale( 'display' );
55 - $classes = array( 'lang-item', 'lang-item-' . $id, 'lang-item-' . esc_attr( $slug ) );
56 - $url = null; // Avoids potential notice
57 - $curlang = 0 === $args['admin_render'] ? $links->curlang->slug : $args['admin_current_lang'];
58 - $current_lang = $curlang == $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();
137 + $current_lang = $this->get_current_language( $args ) === $slug;
59 138
60 139 if ( $current_lang ) {
61 140 if ( $args['hide_current'] && ! ( $args['dropdown'] && ! $args['raw'] ) ) {
62 141 continue; // Hide current language except for dropdown
@@ -64,13 +143,9 @@
64 143 $classes[] = 'current-lang';
65 144 }
66 145 }
67 146
68 - if ( null !== $args['post_id'] && ( $tr_id = $links->model->post->get( $args['post_id'], $language ) ) && $links->model->post->current_user_can_read( $tr_id ) ) {
69 - $url = get_permalink( $tr_id );
70 - } elseif ( null === $args['post_id'] && 0 === $args['admin_render'] ) {
71 - $url = $links->get_translation_url( $language );
72 - }
147 + $url = $this->get_link( $language, $args );
73 148
74 149 if ( $no_translation = empty( $url ) ) {
75 150 $classes[] = 'no-translation';
76 151 }
@@ -79,11 +154,11 @@
79 154 * Filter the link in the language switcher
80 155 *
81 156 * @since 0.7
82 157 *
83 - * @param string $url the link
84 - * @param string $slug language code
85 - * @param string $locale language locale
158 + * @param string|null $url The link, null if no translation was found.
159 + * @param string $slug The language code.
160 + * @param string $locale The language locale
86 161 */
87 162 $url = apply_filters( 'pll_the_language_link', $url, $slug, $language->locale );
88 163
89 164 // Hide if no translation exists
@@ -90,19 +165,26 @@
90 165 if ( empty( $url ) && $args['hide_if_no_translation'] ) {
91 166 continue;
92 167 }
93 168
94 - $url = empty( $url ) || $args['force_home'] ? $links->get_home_url( $language ) : $url; // If the page is not translated, link to the home page
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
95 170
96 171 $name = $args['show_names'] || ! $args['show_flags'] || $args['raw'] ? ( 'slug' == $args['display_names_as'] ? $slug : $language->name ) : '';
97 - $flag = $args['raw'] && ! $args['show_flags'] ? $language->get_display_flag_url() : ( $args['show_flags'] ? $language->get_display_flag() : '' );
98 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 +
99 181 if ( $first ) {
100 182 $classes[] = 'lang-item-first';
101 183 $first = false;
102 184 }
103 185
104 - $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' );
105 187 }
106 188
107 189 return $out;
108 190 }
@@ -108,53 +190,40 @@
108 190 }
109 191
110 192 /**
111 193 * Displays a language switcher
112 - * or returns the raw elements to build a custom language switcher
194 + * or returns the raw elements to build a custom language switcher.
113 195 *
114 - * List of parameters accepted in $args:
196 + * @since 0.1
115 197 *
116 - * dropdown => the list is displayed as dropdown if set, defaults to 0
117 - * echo => echoes the list if set to 1, defaults to 1
118 - * hide_if_empty => hides languages with no posts ( or pages ) if set to 1, defaults to 1
119 - * show_flags => displays flags if set to 1, defaults to 0
120 - * show_names => show language names if set to 1, defaults to 1
121 - * display_names_as => whether to display the language name or its slug, valid options are 'slug' and 'name', defaults to name
122 - * force_home => will always link to home in translated language if set to 1, defaults to 0
123 - * hide_if_no_translation => hide the link if there is no translation if set to 1, defaults to 0
124 - * hide_current => hide the current language if set to 1, defaults to 0
125 - * post_id => returns links to translations of post defined by post_id if set, defaults not set
126 - * raw => return a raw array instead of html markup if set to 1, defaults to 0
127 - * item_spacing => whether to preserve or discard whitespace between list items, valid options are 'preserve' and 'discard', defaults to preserve
128 - * 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
129 - * admin_current_lang => the current language code in an admin context. Need to set the admin_render to 1, defaults not set
198 + * @param PLL_Links $links Instance of PLL_Links.
199 + * @param array $args {
200 + * Optional array of arguments.
130 201 *
131 - * @since 0.1
132 - *
133 - * @param object $links instance of PLL_Frontend_Links
134 - * @param array $args
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.
218 + * }
135 219 * @return string|array either the html markup of the switcher or the raw elements to build a custom language switcher
136 220 */
137 - public function the_languages( $links, $args = '' ) {
138 - $defaults = array(
139 - 'dropdown' => 0, // display as list and not as dropdown
140 - 'echo' => 1, // echoes the list
141 - 'hide_if_empty' => 1, // hides languages with no posts ( or pages )
142 - 'menu' => 0, // not for nav menu ( this argument is deprecated since v1.1.1 )
143 - 'show_flags' => 0, // don't show flags
144 - 'show_names' => 1, // show language names
145 - 'display_names_as' => 'name', // valid options are slug and name
146 - 'force_home' => 0, // tries to find a translation
147 - 'hide_if_no_translation' => 0, // don't hide the link if there is no translation
148 - 'hide_current' => 0, // don't hide current language
149 - 'post_id' => null, // if not null, link to translations of post defined by post_id
150 - 'raw' => 0, // set this to true to build your own custom language switcher
151 - 'item_spacing' => 'preserve', // 'preserve' or 'discard' whitespace between list items
152 - 'admin_render' => 0, // make the switcher in an frontend context
153 - 'admin_current_lang' => null, // use when admin_render is set to 1, if not null use it instead of the current language
154 - );
155 - $args = wp_parse_args( $args, $defaults );
221 + public function the_languages( $links, $args = array() ) {
156 222
223 + $this->links = $links;
224 + $args = wp_parse_args( $args, self::DEFAULTS );
225 +
157 226 /**
158 227 * Filter the arguments of the 'pll_the_languages' template tag
159 228 *
160 229 * @since 1.5
@@ -162,14 +231,19 @@
162 231 * @param array $args
163 232 */
164 233 $args = apply_filters( 'pll_the_languages_args', $args );
165 234
166 - // Prevents showing empty options in dropdown
167 - 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'] ) {
168 242 $args['show_names'] = 1;
169 243 }
170 244
171 - $elements = $this->get_elements( $links, $args );
245 + $elements = $this->get_elements( $args );
172 246
173 247 if ( $args['raw'] ) {
174 248 return $elements;
175 249 }
@@ -175,13 +249,19 @@
175 249 }
176 250
177 251 if ( $args['dropdown'] ) {
178 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 );
179 256 $walker = new PLL_Walker_Dropdown();
180 - $args['selected'] = 0 === $args['admin_render'] ? $links->curlang->slug : $args['admin_current_lang'];
257 + } else {
258 + $walker = new PLL_Walker_List();
181 259 }
182 - else {
183 - $walker = new PLL_Walker_List();
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;
184 264 }
185 265
186 266 /**
187 267 * Filter the whole html markup returned by the 'pll_the_languages' template tag
@@ -192,22 +272,16 @@
192 272 * @param array $args arguments passed to the template tag
193 273 */
194 274 $out = apply_filters( 'pll_the_languages', $walker->walk( $elements, -1, $args ), $args );
195 275
196 - // Javascript to switch the language when using a dropdown list
276 + // Javascript to switch the language when using a dropdown list.
197 277 if ( $args['dropdown'] && 0 === $args['admin_render'] ) {
198 - // 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).
199 279 $out .= sprintf(
200 - '<script type="text/javascript">
201 - //<![CDATA[
202 - var %1$s = %2$s;
203 - document.getElementById( "%3$s" ).onchange = function() {
204 - location.href = %1$s[this.value];
205 - }
206 - //]]>
280 + '<script%1$s>
281 + document.getElementById( "%2$s" ).addEventListener( "change", function ( event ) { location.href = event.currentTarget.value; } )
207 282 </script>',
208 - 'urls_' . preg_replace( '#[^a-zA-Z0-9]#', '', $args['dropdown'] ),
209 - wp_json_encode( wp_list_pluck( $elements, 'url' ) ),
283 + current_theme_supports( 'html5', 'script' ) ? '' : ' type="text/javascript"',
210 284 esc_js( $args['name'] )
211 285 );
212 286 }
213 287