PluginProbe
Polylang / 3.1.4
Polylang v3.1.4
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 +136 -63 2.7.43.1.4 View file →
@@ -1,5 +1,8 @@
1 1 <?php
2 +/**
3 + * @package Polylang
4 + */
2 5
3 6 /**
4 7 * A class to display a language switcher on frontend
5 8 *
@@ -5,16 +8,37 @@
5 8 *
6 9 * @since 1.2
7 10 */
8 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 + );
9 28
10 29 /**
30 + * @var PLL_Links
31 + */
32 + protected $links;
33 +
34 + /**
11 35 * Returns options available for the language switcher - menu or widget
12 36 * either strings to display the options or default values
13 37 *
14 38 * @since 0.7
15 39 *
16 - * @param string $type optional either 'menu' or 'widget', defaults to 'widget'
40 + * @param string $type optional either 'menu', 'widget' or 'block', defaults to 'widget'
17 41 * @param string $key optional either 'string' or 'default', defaults to 'string'
18 42 * @return array list of switcher options strings or default values
19 43 */
20 44 public static function get_switcher_options( $type = 'widget', $key = 'string' ) {
@@ -25,36 +49,95 @@
25 49 'force_home' => array( 'string' => __( 'Forces link to front page', 'polylang' ), 'default' => 0 ),
26 50 'hide_current' => array( 'string' => __( 'Hides the current language', 'polylang' ), 'default' => 0 ),
27 51 'hide_if_no_translation' => array( 'string' => __( 'Hides languages with no translation', 'polylang' ), 'default' => 0 ),
28 52 );
53 + return wp_list_pluck( $options, $key );
54 + }
29 55
30 - return wp_list_pluck( $options, $key );
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'];
31 74 }
32 75
33 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 + /**
34 113 * Get the language elements for use in a walker
35 114 *
36 - * @see list of parameters accepted in $args documented for PLL_Switcher::the_languages
37 - *
38 115 * @since 1.2
39 116 *
40 - * @param object $links instance of PLL_Frontend_Links
41 - * @param array $args
42 - * @return array
117 + * @param array $args Arguments passed to {@see PLL_Switcher::the_languages()}.
118 + * @return array Language switcher elements.
43 119 */
44 - protected function get_elements( $links, $args ) {
120 + protected function get_elements( $args ) {
45 121 $first = true;
46 122 $out = array();
47 123
48 - 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 ) {
49 125 $id = (int) $language->term_id;
50 126 $order = (int) $language->term_group;
51 127 $slug = $language->slug;
52 128 $locale = $language->get_locale( 'display' );
53 - $classes = array( 'lang-item', 'lang-item-' . $id, 'lang-item-' . esc_attr( $slug ) );
54 - $url = null; // Avoids potential notice
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;
55 138
56 - if ( $current_lang = $links->curlang->slug == $slug ) {
139 + if ( $current_lang ) {
57 140 if ( $args['hide_current'] && ! ( $args['dropdown'] && ! $args['raw'] ) ) {
58 141 continue; // Hide current language except for dropdown
59 142 } else {
60 143 $classes[] = 'current-lang';
@@ -60,13 +143,9 @@
60 143 $classes[] = 'current-lang';
61 144 }
62 145 }
63 146
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 ) ) {
65 - $url = get_permalink( $tr_id );
66 - } elseif ( null === $args['post_id'] ) {
67 - $url = $links->get_translation_url( $language );
68 - }
147 + $url = $this->get_link( $language, $args );
69 148
70 149 if ( $no_translation = empty( $url ) ) {
71 150 $classes[] = 'no-translation';
72 151 }
@@ -75,11 +154,11 @@
75 154 * Filter the link in the language switcher
76 155 *
77 156 * @since 0.7
78 157 *
79 - * @param string $url the link
80 - * @param string $slug language code
81 - * @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
82 161 */
83 162 $url = apply_filters( 'pll_the_language_link', $url, $slug, $language->locale );
84 163
85 164 // Hide if no translation exists
@@ -86,12 +165,12 @@
86 165 if ( empty( $url ) && $args['hide_if_no_translation'] ) {
87 166 continue;
88 167 }
89 168
90 - $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
91 170
92 171 $name = $args['show_names'] || ! $args['show_flags'] || $args['raw'] ? ( 'slug' == $args['display_names_as'] ? $slug : $language->name ) : '';
93 - $flag = $args['raw'] && ! $args['show_flags'] ? $language->flag_url : ( $args['show_flags'] ? $language->flag : '' );
172 + $flag = $args['raw'] && ! $args['show_flags'] ? $language->get_display_flag_url() : ( $args['show_flags'] ? $language->get_display_flag() : '' );
94 173
95 174 if ( $first ) {
96 175 $classes[] = 'lang-item-first';
97 176 $first = false;
@@ -96,9 +175,9 @@
96 175 $classes[] = 'lang-item-first';
97 176 $first = false;
98 177 }
99 178
100 - $out[ $slug ] = compact( 'id', 'order', 'slug', 'locale', 'name', 'url', 'flag', 'current_lang', 'no_translation', 'classes' );
179 + $out[ $slug ] = compact( 'id', 'order', 'slug', 'locale', 'name', 'url', 'flag', 'current_lang', 'no_translation', 'classes', 'link_classes' );
101 180 }
102 181
103 182 return $out;
104 183 }
@@ -104,48 +183,38 @@
104 183 }
105 184
106 185 /**
107 186 * Displays a language switcher
108 - * or returns the raw elements to build a custom language switcher
187 + * or returns the raw elements to build a custom language switcher.
109 188 *
110 - * List of parameters accepted in $args:
189 + * @since 0.1
111 190 *
112 - * dropdown => the list is displayed as dropdown if set, defaults to 0
113 - * echo => echoes the list if set to 1, defaults to 1
114 - * hide_if_empty => hides languages with no posts ( or pages ) if set to 1, defaults to 1
115 - * show_flags => displays flags if set to 1, defaults to 0
116 - * 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
118 - * force_home => will always link to home in translated language if set to 1, defaults to 0
119 - * hide_if_no_translation => hide the link if there is no translation if set to 1, defaults to 0
120 - * hide_current => hide the current language if set to 1, defaults to 0
121 - * post_id => returns links to translations of post defined by post_id if set, defaults not set
122 - * 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
191 + * @param PLL_Links $links Instance of PLL_Links.
192 + * @param array $args {
193 + * Optional array of arguments.
124 194 *
125 - * @since 0.1
126 - *
127 - * @param object $links instance of PLL_Frontend_Links
128 - * @param array $args
195 + * @type int $dropdown The list is displayed as dropdown if set, defaults to 0.
196 + * @type int $echo Echoes the list if set to 1, defaults to 1.
197 + * @type int $hide_if_empty Hides languages with no posts ( or pages ) if set to 1, defaults to 1.
198 + * @type int $show_flags Displays flags if set to 1, defaults to 0.
199 + * @type int $show_names Shows language names if set to 1, defaults to 1.
200 + * @type string $display_names_as Whether to display the language name or its slug, valid options are 'slug' and 'name', defaults to name.
201 + * @type int $force_home Will always link to home in translated language if set to 1, defaults to 0.
202 + * @type int $hide_if_no_translation Hides the link if there is no translation if set to 1, defaults to 0.
203 + * @type int $hide_current Hides the current language if set to 1, defaults to 0.
204 + * @type int $post_id Returns links to the translations of the post defined by post_id if set, defaults not set.
205 + * @type int $raw Return a raw array instead of html markup if set to 1, defaults to 0.
206 + * @type string $item_spacing Whether to preserve or discard whitespace between list items, valid options are 'preserve' and 'discard', defaults to 'preserve'.
207 + * @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.
208 + * @type string $admin_current_lang The current language code in an admin context. Need to set the admin_render to 1, defaults not set.
209 + * @type string[] $classes A list of CSS classes to set to each elements outputted.
210 + * @type string[] $link_classes A list of CSS classes to set to each link outputted.
211 + * }
129 212 * @return string|array either the html markup of the switcher or the raw elements to build a custom language switcher
130 213 */
131 - public function the_languages( $links, $args = '' ) {
132 - $defaults = array(
133 - 'dropdown' => 0, // display as list and not as dropdown
134 - 'echo' => 1, // echoes the list
135 - 'hide_if_empty' => 1, // hides languages with no posts ( or pages )
136 - 'menu' => 0, // not for nav menu ( this argument is deprecated since v1.1.1 )
137 - 'show_flags' => 0, // don't show flags
138 - 'show_names' => 1, // show language names
139 - 'display_names_as' => 'name', // valid options are slug and name
140 - 'force_home' => 0, // tries to find a translation
141 - 'hide_if_no_translation' => 0, // don't hide the link if there is no translation
142 - 'hide_current' => 0, // don't hide current language
143 - 'post_id' => null, // if not null, link to translations of post defined by post_id
144 - '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 - );
147 - $args = wp_parse_args( $args, $defaults );
214 + public function the_languages( $links, $args = array() ) {
215 + $this->links = $links;
216 + $args = wp_parse_args( $args, self::DEFAULTS );
148 217
149 218 /**
150 219 * Filter the arguments of the 'pll_the_languages' template tag
151 220 *
@@ -154,14 +223,19 @@
154 223 * @param array $args
155 224 */
156 225 $args = apply_filters( 'pll_the_languages_args', $args );
157 226
227 + // Force not to hide the language for the widget preview even if the option is checked.
228 + if ( $this->links instanceof PLL_Admin_Links ) {
229 + $args['hide_if_no_translation'] = 0;
230 + }
231 +
158 232 // Prevents showing empty options in dropdown
159 233 if ( $args['dropdown'] ) {
160 234 $args['show_names'] = 1;
161 235 }
162 236
163 - $elements = $this->get_elements( $links, $args );
237 + $elements = $this->get_elements( $args );
164 238
165 239 if ( $args['raw'] ) {
166 240 return $elements;
167 241 }
@@ -168,11 +242,10 @@
168 242
169 243 if ( $args['dropdown'] ) {
170 244 $args['name'] = 'lang_choice_' . $args['dropdown'];
171 245 $walker = new PLL_Walker_Dropdown();
172 - $args['selected'] = $links->curlang->slug;
173 - }
174 - else {
246 + $args['selected'] = $this->get_current_language( $args );
247 + } else {
175 248 $walker = new PLL_Walker_List();
176 249 }
177 250
178 251 /**
@@ -185,9 +258,9 @@
185 258 */
186 259 $out = apply_filters( 'pll_the_languages', $walker->walk( $elements, -1, $args ), $args );
187 260
188 261 // Javascript to switch the language when using a dropdown list
189 - if ( $args['dropdown'] ) {
262 + if ( $args['dropdown'] && 0 === $args['admin_render'] ) {
190 263 // Accept only few valid characters for the urls_x variable name ( as the widget id includes '-' which is invalid )
191 264 $out .= sprintf(
192 265 '<script type="text/javascript">
193 266 //<![CDATA[