PluginProbe
Polylang / 2.3.7
Polylang v2.3.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
polylang / include / switcher.php

switcher.php in Polylang 2.3.7, at include/switcher.php

217 lines 8.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /**
4 * A class to display a language switcher on frontend
5 *
6 * @since 1.2
7 */
8 class PLL_Switcher {
9
10 /**
11 * Returns options available for the language switcher - menu or widget
12 * either strings to display the options or default values
13 *
14 * @since 0.7
15 *
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 strings or default values
19 */
20 static public function get_switcher_options( $type = 'widget', $key = 'string' ) {
21 $options = array(
22 'dropdown' => array( 'string' => __( 'Displays as dropdown', 'polylang' ), 'default' => 0 ),
23 'show_names' => array( 'string' => __( 'Displays language names', 'polylang' ), 'default' => 1 ),
24 'show_flags' => array( 'string' => __( 'Displays flags', 'polylang' ), 'default' => 0 ),
25 'force_home' => array( 'string' => __( 'Forces link to front page', 'polylang' ), 'default' => 0 ),
26 'hide_current' => array( 'string' => __( 'Hides the current language', 'polylang' ), 'default' => 0 ),
27 'hide_if_no_translation' => array( 'string' => __( 'Hides languages with no translation', 'polylang' ), 'default' => 0 ),
28 );
29
30 return wp_list_pluck( $options, $key );
31 }
32
33 /**
34 * Get the language elements for use in a walker
35 *
36 * List of parameters accepted in $args:
37 * @see PLL_Switcher::the_languages
38 *
39 * @since 1.2
40 *
41 * @param object $links instance of PLL_Frontend_Links
42 * @param array $args
43 * @return array
44 */
45 protected function get_elements( $links, $args ) {
46
47 $first = true;
48
49 foreach ( $links->model->get_languages_list( array( 'hide_empty' => $args['hide_if_empty'] ) ) as $language ) {
50 $id = (int) $language->term_id;
51 $order = (int) $language->term_group;
52 $slug = $language->slug;
53 $locale = $language->get_locale( 'display' );
54 $classes = array( 'lang-item', 'lang-item-' . $id, 'lang-item-' . esc_attr( $slug ) );
55 $url = null; // Avoids potential notice
56
57 if ( $first ) {
58 $classes[] = 'lang-item-first';
59 $first = false;
60 }
61
62 if ( $current_lang = $links->curlang->slug == $slug ) {
63 if ( $args['hide_current'] && ! ( $args['dropdown'] && ! $args['raw'] ) ) {
64 continue; // Hide current language except for dropdown
65 }
66 else {
67 $classes[] = 'current-lang';
68 }
69 }
70
71 if ( null !== $args['post_id'] && ( $tr_id = $links->model->post->get( $args['post_id'], $language ) ) && $links->current_user_can_read( $tr_id ) ) {
72 $url = get_permalink( $tr_id );
73 } elseif ( null === $args['post_id'] ) {
74 $url = $links->get_translation_url( $language );
75 }
76
77 if ( $no_translation = empty( $url ) ) {
78 $classes[] = 'no-translation';
79 }
80
81 /**
82 * Filter the link in the language switcher
83 *
84 * @since 0.7
85 *
86 * @param string $url the link
87 * @param string $slug language code
88 * @param string $locale language locale
89 */
90 $url = apply_filters( 'pll_the_language_link', $url, $slug, $language->locale );
91
92 // Hide if no translation exists
93 if ( empty( $url ) && $args['hide_if_no_translation'] ) {
94 continue;
95 }
96
97 $url = empty( $url ) || $args['force_home'] ? $links->get_home_url( $language ) : $url; // If the page is not translated, link to the home page
98
99 $name = $args['show_names'] || ! $args['show_flags'] || $args['raw'] ? ( 'slug' == $args['display_names_as'] ? $slug : $language->name ) : '';
100 $flag = $args['raw'] && ! $args['show_flags'] ? $language->flag_url : ( $args['show_flags'] ? $language->flag : '' );
101
102 $out[ $slug ] = compact( 'id', 'order', 'slug', 'locale', 'name', 'url', 'flag', 'current_lang', 'no_translation', 'classes' );
103 }
104
105 return empty( $out ) ? array() : $out;
106 }
107
108 /**
109 * Displays a language switcher
110 * or returns the raw elements to build a custom language switcher
111 *
112 * List of parameters accepted in $args:
113 *
114 * dropdown => the list is displayed as dropdown if set, defaults to 0
115 * echo => echoes the list if set to 1, defaults to 1
116 * hide_if_empty => hides languages with no posts ( or pages ) if set to 1, defaults to 1
117 * show_flags => displays flags if set to 1, defaults to 0
118 * show_names => show language names if set to 1, defaults to 1
119 * display_names_as => whether to display the language name or its slug, valid options are 'slug' and 'name', defaults to name
120 * force_home => will always link to home in translated language if set to 1, defaults to 0
121 * hide_if_no_translation => hide the link if there is no translation if set to 1, defaults to 0
122 * hide_current => hide the current language if set to 1, defaults to 0
123 * post_id => returns links to translations of post defined by post_id if set, defaults not set
124 * raw => return a raw array instead of html markup if set to 1, defaults to 0
125 * item_spacing => whether to preserve or discard whitespace between list items, valid options are 'preserve' and 'discard', defaults to preserve
126 *
127 * @since 0.1
128 *
129 * @param object $links instance of PLL_Frontend_Links
130 * @param array $args
131 * @return string|array either the html markup of the switcher or the raw elements to build a custom language switcher
132 */
133 public function the_languages( $links, $args = '' ) {
134 $defaults = array(
135 'dropdown' => 0, // display as list and not as dropdown
136 'echo' => 1, // echoes the list
137 'hide_if_empty' => 1, // hides languages with no posts ( or pages )
138 'menu' => 0, // not for nav menu ( this argument is deprecated since v1.1.1 )
139 'show_flags' => 0, // don't show flags
140 'show_names' => 1, // show language names
141 'display_names_as' => 'name', // valid options are slug and name
142 'force_home' => 0, // tries to find a translation
143 'hide_if_no_translation' => 0, // don't hide the link if there is no translation
144 'hide_current' => 0, // don't hide current language
145 'post_id' => null, // if not null, link to translations of post defined by post_id
146 'raw' => 0, // set this to true to build your own custom language switcher
147 'item_spacing' => 'preserve', // 'preserve' or 'discard' whitespace between list items
148 );
149 $args = wp_parse_args( $args, $defaults );
150
151 /**
152 * Filter the arguments of the 'pll_the_languages' template tag
153 *
154 * @since 1.5
155 *
156 * @param array $args
157 */
158 $args = apply_filters( 'pll_the_languages_args', $args );
159
160 // Prevents showing empty options in dropdown
161 if ( $args['dropdown'] ) {
162 $args['show_names'] = 1;
163 }
164
165 $elements = $this->get_elements( $links, $args );
166
167 if ( $args['raw'] ) {
168 return $elements;
169 }
170
171 if ( $args['dropdown'] ) {
172 $args['name'] = 'lang_choice_' . $args['dropdown'];
173 $walker = new PLL_Walker_Dropdown();
174 $args['selected'] = $links->curlang->slug;
175 }
176 else {
177 $walker = new PLL_Walker_List();
178 }
179
180 /**
181 * Filter the whole html markup returned by the 'pll_the_languages' template tag
182 *
183 * @since 0.8
184 *
185 * @param string $html html returned/outputted by the template tag
186 * @param array $args arguments passed to the template tag
187 */
188 $out = apply_filters( 'pll_the_languages', $walker->walk( $elements, $args ), $args );
189
190 // Javascript to switch the language when using a dropdown list
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
197 // 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">
200 //<![CDATA[
201 var %1$s = %2$s;
202 document.getElementById( "%3$s" ).onchange = function() {
203 location.href = %1$s[this.value];
204 }
205 //]]>
206 </script>',
207 'urls_' . preg_replace( '#[^a-zA-Z0-9]#', '', $args['dropdown'] ), json_encode( $urls ), esc_js( $args['name'] )
208 );
209 }
210
211 if ( $args['echo'] ) {
212 echo $out;
213 }
214 return $out;
215 }
216 }
217