PluginProbe
Polylang / 1.5
Polylang v1.5
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 +137 -212 3.0.21.5 View file →
@@ -1,270 +1,195 @@
1 1 <?php
2 -/**
3 - * @package Polylang
4 - */
5 2
6 -/**
7 - * A class to display a language switcher on frontend
3 +/*
4 + * a class to display a language switcher on frontend
8 5 *
9 6 * @since 1.2
10 7 */
11 8 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 - );
28 9
29 - /**
30 - * @var PLL_Links
31 - */
32 - protected $links;
33 -
34 - /**
35 - * Returns options available for the language switcher - menu or widget
10 + /*
11 + * returns options available for the language switcher - menu or widget
36 12 * either strings to display the options or default values
37 13 *
38 14 * @since 0.7
39 15 *
40 - * @param string $type optional either 'menu', 'widget' or 'block', defaults to 'widget'
41 - * @param string $key optional either 'string' or 'default', defaults to 'string'
42 - * @return array list of switcher options strings or default values
16 + * @param string $type optional either 'menu' or 'widget', defaults to 'widget'
17 + * @param string $key optional either 'string' or 'default', defaults to 'string'
18 + * @return array list of switcher options srings or default values
43 19 */
44 - public static function get_switcher_options( $type = 'widget', $key = 'string' ) {
20 + static public function get_switcher_options($type = 'widget', $key ='string') {
45 21 $options = array(
46 - 'dropdown' => array( 'string' => __( 'Displays as a dropdown', 'polylang' ), 'default' => 0 ),
47 - 'show_names' => array( 'string' => __( 'Displays language names', 'polylang' ), 'default' => 1 ),
48 - 'show_flags' => array( 'string' => __( 'Displays flags', 'polylang' ), 'default' => 0 ),
49 - 'force_home' => array( 'string' => __( 'Forces link to front page', 'polylang' ), 'default' => 0 ),
50 - 'hide_current' => array( 'string' => __( 'Hides the current language', 'polylang' ), 'default' => 0 ),
51 - 'hide_if_no_translation' => array( 'string' => __( 'Hides languages with no translation', 'polylang' ), 'default' => 0 ),
22 + 'show_names' => array('string' => __('Displays language names', 'polylang'), 'default' => 1),
23 + 'show_flags' => array('string' => __('Displays flags', 'polylang'), 'default' => 0),
24 + 'force_home' => array('string' => __('Forces link to front page', 'polylang'), 'default' => 0),
25 + 'hide_current' => array('string' => __('Hides the current language', 'polylang'), 'default' => 0),
52 26 );
53 - return wp_list_pluck( $options, $key );
54 - }
55 27
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 - }
28 + if ($type != 'menu')
29 + $options = array('dropdown' => array('string' => __('Displays as dropdown', 'polylang'), 'default' => 0)) + $options;
68 30
69 - if ( isset( $this->links->curlang ) ) {
70 - return $this->links->curlang->slug;
71 - }
72 -
73 - return $this->links->options['default_lang'];
31 + return array_map(create_function('$v', "return \$v['$key'];"), $options);
74 32 }
75 33
76 - /**
77 - * Returns the link for a given language.
34 + /*
35 + * get the language elements for use in a walker
78 36 *
79 - * @since 3.0
37 + * list of parameters accepted in $args:
38 + * @see PLL_Switcher::the_languages
80 39 *
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 - /**
113 - * Get the language elements for use in a walker
114 - *
115 40 * @since 1.2
116 41 *
117 - * @param array $args Arguments passed to {@see PLL_Switcher::the_languages()}.
118 - * @return array Language switcher elements.
42 + * @param object $links instance of PLL_Frontend_Links
43 + * @param array $args
44 + * @return array
119 45 */
120 - protected function get_elements( $args ) {
121 - $first = true;
122 - $out = array();
46 + protected function get_elements($links, $args) {
47 + extract($args);
123 48
124 - foreach ( $this->links->model->get_languages_list( array( 'hide_empty' => $args['hide_if_empty'] ) ) as $language ) {
49 + foreach ($links->model->get_languages_list(array('hide_empty' => $hide_if_empty)) as $language) {
125 50 $id = (int) $language->term_id;
126 - $order = (int) $language->term_group;
127 51 $slug = $language->slug;
128 - $locale = $language->get_locale( 'display' );
129 - $classes = array( 'lang-item', 'lang-item-' . $id, 'lang-item-' . esc_attr( $slug ) );
130 - $current_lang = $this->get_current_language( $args ) === $slug;
52 + $classes = array('lang-item', 'lang-item-' . esc_attr($id), 'lang-item-' . esc_attr($slug));
131 53
132 - if ( $current_lang ) {
133 - if ( $args['hide_current'] && ! ( $args['dropdown'] && ! $args['raw'] ) ) {
134 - continue; // Hide current language except for dropdown
135 - } else {
54 + if ($current_lang = pll_current_language() == $slug) {
55 + if ($hide_current && !$dropdown)
56 + continue; // hide current language except for dropdown
57 + else
136 58 $classes[] = 'current-lang';
137 - }
138 59 }
139 60
140 - $url = $this->get_link( $language, $args );
61 + $url = $post_id !== null && ($tr_id = $links->model->get_post($post_id, $language)) && $links->current_user_can_read($tr_id) ? get_permalink($tr_id) :
62 + ($post_id === null && !$force_home ? $links->get_translation_url($language) : null);
141 63
142 - if ( $no_translation = empty( $url ) ) {
64 + if ($no_translation = empty($url))
143 65 $classes[] = 'no-translation';
144 - }
145 66
146 - /**
147 - * Filter the link in the language switcher
148 - *
149 - * @since 0.7
150 - *
151 - * @param string|null $url The link, null if no translation was found.
152 - * @param string $slug The language code.
153 - * @param string $locale The language locale
154 - */
155 - $url = apply_filters( 'pll_the_language_link', $url, $slug, $language->locale );
67 + $url = apply_filters('pll_the_language_link', $url, $slug, $language->locale);
156 68
157 - // Hide if no translation exists
158 - if ( empty( $url ) && $args['hide_if_no_translation'] ) {
69 + // hide if no translation exists
70 + if (empty($url) && $hide_if_no_translation)
159 71 continue;
160 - }
161 72
162 - $url = empty( $url ) || $args['force_home'] ? $this->links->get_home_url( $language ) : $url; // If the page is not translated, link to the home page
73 + $url = empty($url) ? $links->get_home_url($language) : $url ; // if the page is not translated, link to the home page
163 74
164 - $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() : '' );
75 + $name = $show_names || !$show_flags || $raw ? ($display_names_as == 'slug' ? $slug : $language->name) : '';
76 + $flag = $raw && !$show_flags ? $language->flag_url : ($show_flags ? $language->flag : '');
166 77
167 - if ( $first ) {
168 - $classes[] = 'lang-item-first';
169 - $first = false;
170 - }
171 -
172 - $out[ $slug ] = compact( 'id', 'order', 'slug', 'locale', 'name', 'url', 'flag', 'current_lang', 'no_translation', 'classes' );
78 + $out[] = compact('id', 'slug', 'name', 'url', 'flag', 'current_lang', 'no_translation', 'classes');
173 79 }
80 + return empty($out) ? array() : $out;
174 81
175 - return $out;
176 82 }
177 83
178 - /**
179 - * Displays a language switcher
180 - * or returns the raw elements to build a custom language switcher.
84 + /*
85 + * displays a language switcher
86 + * or returns the raw elements to build a custom language switcher
181 87 *
88 + * list of parameters accepted in $args:
89 + *
90 + * dropdown => the list is displayed as dropdown if set to 1, defaults to 0
91 + * echo => echoes the list if set to 1, defaults to 1
92 + * hide_if_empty => hides languages with no posts (or pages) if set to 1, defaults to 1
93 + * show_flags => displays flags if set to 1, defaults to 0
94 + * show_names => show language names if set to 1, defaults to 1
95 + * display_names_as => wether to display the language name or its slug, valid options are 'slug' and 'name', defaults to name
96 + * force_home => will always link to home in translated language if set to 1, defaults to 0
97 + * hide_if_no_translation => hide the link if there is no translation if set to 1, defaults to 0
98 + * hide_current => hide the current language if set to 1, defaults to 0
99 + * post_id => returns links to translations of post defined by post_id if set, defaults not set
100 + * raw => return a raw array instead of html markup if set to 1, defaults to 0
101 + *
182 102 * @since 0.1
183 103 *
184 - * @param PLL_Links $links Instance of PLL_Links.
185 - * @param array $args {
186 - * Optional array of arguments.
187 - *
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 - * }
104 + * @param object $links instance of PLL_Frontend_Links
105 + * @param array $args
203 106 * @return string|array either the html markup of the switcher or the raw elements to build a custom language switcher
204 107 */
205 - public function the_languages( $links, $args = array() ) {
206 - $this->links = $links;
207 - $args = wp_parse_args( $args, self::DEFAULTS );
108 + public function the_languages($links, $args = '') {
109 + $defaults = array(
110 + 'dropdown' => 0, // display as list and not as dropdown
111 + 'echo' => 1, // echoes the list
112 + 'hide_if_empty' => 1, // hides languages with no posts (or pages)
113 + 'menu' => 0, // not for nav menu (this argument is deprecated since v1.1.1)
114 + 'show_flags' => 0, // don't show flags
115 + 'show_names' => 1, // show language names
116 + 'display_names_as' => 'name', // valid options are slug and name
117 + 'force_home' => 0, // tries to find a translation
118 + 'hide_if_no_translation' => 0, // don't hide the link if there is no translation
119 + 'hide_current' => 0, // don't hide current language
120 + 'post_id' => null, // if not null, link to translations of post defined by post_id
121 + 'raw' => 0, // set this to true to build your own custom language switcher
122 + );
123 + $args = wp_parse_args($args, $defaults);
124 + $args = apply_filters('pll_the_languages_args', $args);
125 + $elements = $this->get_elements($links, $args);
208 126
209 - /**
210 - * Filter the arguments of the 'pll_the_languages' template tag
211 - *
212 - * @since 1.5
213 - *
214 - * @param array $args
215 - */
216 - $args = apply_filters( 'pll_the_languages_args', $args );
127 + if ($args['raw'])
128 + return $elements;
217 129
218 - // Prevents showing empty options in dropdown
219 - if ( $args['dropdown'] ) {
220 - $args['show_names'] = 1;
130 + if ($args['dropdown']) {
131 + $walker = new PLL_Walker_Dropdown();
132 + $args['selected'] = pll_current_language();
221 133 }
134 + else
135 + $walker = new PLL_Walker_List();
222 136
223 - $elements = $this->get_elements( $args );
137 + $out = apply_filters('pll_the_languages', $walker->walk($elements, $args), $args);
224 138
225 - if ( $args['raw'] ) {
226 - return $elements;
227 - }
139 + if ($args['echo'])
140 + echo $out;
141 + return $out;
142 + }
143 +}
228 144
229 - if ( $args['dropdown'] ) {
230 - $args['name'] = 'lang_choice_' . $args['dropdown'];
231 - $walker = new PLL_Walker_Dropdown();
232 - $args['selected'] = $this->get_current_language( $args );
233 - } else {
234 - $walker = new PLL_Walker_List();
235 - }
145 +/*
146 + * displays a language list
147 + *
148 + * @since 1.2
149 + */
150 +class PLL_Walker_List extends Walker {
151 + var $db_fields = array ('parent' => 'parent', 'id' => 'id');
236 152
237 - /**
238 - * Filter the whole html markup returned by the 'pll_the_languages' template tag
239 - *
240 - * @since 0.8
241 - *
242 - * @param string $html html returned/outputted by the template tag
243 - * @param array $args arguments passed to the template tag
244 - */
245 - $out = apply_filters( 'pll_the_languages', $walker->walk( $elements, -1, $args ), $args );
153 + /*
154 + * outputs one element
155 + *
156 + * @since 1.2
157 + *
158 + * @see Walker::start_el
159 + */
160 + function start_el( &$output, $element, $depth = 0, $args = array(), $current_object_id = 0 ) {
161 + $output .= sprintf(
162 + "\t".'<li class="%s"><a hreflang="%s" href="%s">%s</a></li>'."\n",
163 + implode(' ', $element->classes),
164 + esc_attr($element->slug),
165 + esc_url($element->url),
166 + $args['show_flags'] && $args['show_names'] ? $element->flag.'&nbsp;'.esc_html($element->name) : $element->flag.esc_html($element->name)
167 + );
168 + }
246 169
247 - // Javascript to switch the language when using a dropdown list
248 - 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 )
250 - $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 - //]]>
258 - </script>',
259 - 'urls_' . preg_replace( '#[^a-zA-Z0-9]#', '', $args['dropdown'] ),
260 - wp_json_encode( wp_list_pluck( $elements, 'url' ) ),
261 - esc_js( $args['name'] )
262 - );
263 - }
170 + /*
171 + * overrides Walker::display_element as it expects an object with a parent property
172 + *
173 + * @since 1.2
174 + *
175 + * @see Walker::display_element
176 + */
177 + function display_element( $element, &$children_elements, $max_depth, $depth = 0, $args, &$output ) {
178 + $element = (object) $element; // make sure we have an object
179 + $element->parent = $element->id = 0; // don't care about this
180 + parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output );
181 + }
264 182
265 - if ( $args['echo'] ) {
266 - echo $out; // phpcs:ignore WordPress.Security.EscapeOutput
267 - }
268 - return $out;
183 + /*
184 + * overrides Walker:walk to set depth argument
185 + *
186 + * @since 1.2
187 + *
188 + * @param array $elements elements to display
189 + * @param array $args
190 + * @return string
191 + */
192 + function walk($elements, $args = array()) {
193 + return parent::walk($elements, -1, $args);
269 194 }
270 195 }