PluginProbe
Polylang / 1.8.5
Polylang v1.8.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
polylang / include / switcher.php

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

183 lines 7.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 srings or default values
19 */
20 static public function get_switcher_options( $type = 'widget', $key = 'string' ) {
21 $options = array(
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 ),
26 'hide_if_no_translation' => array( 'string' => __( 'Hides languages with no translation', 'polylang' ), 'default' => 0 ),
27 );
28
29 if ( 'menu' != $type ) {
30 $options = array( 'dropdown' => array( 'string' => __( 'Displays as dropdown', 'polylang' ), 'default' => 0 ) ) + $options;
31 }
32
33 return wp_list_pluck( $options, $key );
34 }
35
36 /*
37 * get the language elements for use in a walker
38 *
39 * list of parameters accepted in $args:
40 * @see PLL_Switcher::the_languages
41 *
42 * @since 1.2
43 *
44 * @param object $links instance of PLL_Frontend_Links
45 * @param array $args
46 * @return array
47 */
48 protected function get_elements( $links, $args ) {
49 foreach ( $links->model->get_languages_list( array( 'hide_empty' => $args['hide_if_empty'] ) ) as $language ) {
50 $id = (int) $language->term_id;
51 $slug = $language->slug;
52 $locale = $language->get_locale( 'display' );
53 $classes = array( 'lang-item', 'lang-item-' . $id, 'lang-item-' . esc_attr( $slug ) );
54 $url = null; // avoids potential notice
55
56 if ( $current_lang = $links->curlang->slug == $slug ) {
57 if ( $args['hide_current'] && ! $args['dropdown'] ) {
58 continue; // hide current language except for dropdown
59 }
60 else {
61 $classes[] = 'current-lang';
62 }
63 }
64
65 if ( null !== $args['post_id'] && ( $tr_id = $links->model->post->get( $args['post_id'], $language ) ) && $links->current_user_can_read( $tr_id ) ) {
66 $url = get_permalink( $tr_id );
67 } elseif ( null === $args['post_id'] ) {
68 $url = $links->get_translation_url( $language );
69 }
70
71 if ( $no_translation = empty( $url ) ) {
72 $classes[] = 'no-translation';
73 }
74
75 $url = apply_filters( 'pll_the_language_link', $url, $slug, $language->locale );
76
77 // hide if no translation exists
78 if ( empty( $url ) && $args['hide_if_no_translation'] ) {
79 continue;
80 }
81
82 $url = empty( $url ) || $args['force_home'] ? $links->get_home_url( $language ) : $url ; // if the page is not translated, link to the home page
83
84 $name = $args['show_names'] || ! $args['show_flags'] || $args['raw'] ? ( 'slug' == $args['display_names_as'] ? $slug : $language->name ) : '';
85 $flag = $args['raw'] && ! $args['show_flags'] ? $language->flag_url : ( $args['show_flags'] ? $language->flag : '' );
86
87 $out[ $slug ] = compact( 'id', 'slug', 'locale', 'name', 'url', 'flag', 'current_lang', 'no_translation', 'classes' );
88 }
89
90 return empty( $out ) ? array() : $out;
91 }
92
93 /*
94 * displays a language switcher
95 * or returns the raw elements to build a custom language switcher
96 *
97 * list of parameters accepted in $args:
98 *
99 * dropdown => the list is displayed as dropdown if set, defaults to 0
100 * echo => echoes the list if set to 1, defaults to 1
101 * hide_if_empty => hides languages with no posts ( or pages ) if set to 1, defaults to 1
102 * show_flags => displays flags if set to 1, defaults to 0
103 * show_names => show language names if set to 1, defaults to 1
104 * display_names_as => wether to display the language name or its slug, valid options are 'slug' and 'name', defaults to name
105 * force_home => will always link to home in translated language if set to 1, defaults to 0
106 * hide_if_no_translation => hide the link if there is no translation if set to 1, defaults to 0
107 * hide_current => hide the current language if set to 1, defaults to 0
108 * post_id => returns links to translations of post defined by post_id if set, defaults not set
109 * raw => return a raw array instead of html markup if set to 1, defaults to 0
110 *
111 * @since 0.1
112 *
113 * @param object $links instance of PLL_Frontend_Links
114 * @param array $args
115 * @return string|array either the html markup of the switcher or the raw elements to build a custom language switcher
116 */
117 public function the_languages( $links, $args = '' ) {
118 $defaults = array(
119 'dropdown' => 0, // display as list and not as dropdown
120 'echo' => 1, // echoes the list
121 'hide_if_empty' => 1, // hides languages with no posts ( or pages )
122 'menu' => 0, // not for nav menu ( this argument is deprecated since v1.1.1 )
123 'show_flags' => 0, // don't show flags
124 'show_names' => 1, // show language names
125 'display_names_as' => 'name', // valid options are slug and name
126 'force_home' => 0, // tries to find a translation
127 'hide_if_no_translation' => 0, // don't hide the link if there is no translation
128 'hide_current' => 0, // don't hide current language
129 'post_id' => null, // if not null, link to translations of post defined by post_id
130 'raw' => 0, // set this to true to build your own custom language switcher
131 );
132 $args = wp_parse_args( $args, $defaults );
133 $args = apply_filters( 'pll_the_languages_args', $args );
134
135 // prevents showing empty options in dropdown
136 if ( $args['dropdown'] ) {
137 $args['show_names'] = 1;
138 }
139
140 $elements = $this->get_elements( $links, $args );
141
142 if ( $args['raw'] ) {
143 return $elements;
144 }
145
146 if ( $args['dropdown'] ) {
147 $args['name'] = 'lang_choice_' . $args['dropdown'];
148 $walker = new PLL_Walker_Dropdown();
149 $args['selected'] = $links->curlang->slug;
150 }
151 else {
152 $walker = new PLL_Walker_List();
153 }
154
155 $out = apply_filters( 'pll_the_languages', $walker->walk( $elements, $args ), $args );
156
157 // javascript to switch the language when using a dropdown list
158 if ( $args['dropdown'] ) {
159 foreach ( $links->model->get_languages_list() as $language ) {
160 $urls[ $language->slug ] = $args['force_home'] || ( $url = $links->get_translation_url( $language ) ) === null ? $links->get_home_url( $language ) : $url;
161 }
162
163 // accept only few valid characters for the urls_x variable name ( as the widget id includes '-' which is invalid )
164 $out .= sprintf( '
165 <script type="text/javascript">
166 //<![CDATA[
167 var %1$s = %2$s;
168 document.getElementById( "%3$s" ).onchange = function() {
169 location.href = %1$s[this.value];
170 }
171 //]]>
172 </script>',
173 'urls_' . preg_replace( '#[^a-zA-Z0-9]#', '', $args['dropdown'] ), json_encode( $urls ), esc_js( $args['name'] )
174 );
175 }
176
177 if ( $args['echo'] ) {
178 echo $out;
179 }
180 return $out;
181 }
182 }
183