| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
/** |
| 7 |
* A class to display a language switcher on frontend |
| 8 |
* |
| 9 |
* @since 1.2 |
| 10 |
*/ |
| 11 |
class PLL_Switcher { |
| 12 |
public 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 |
|
| 29 |
/** |
| 30 |
* @var PLL_Links|null |
| 31 |
*/ |
| 32 |
protected $links; |
| 33 |
|
| 34 |
/** |
| 35 |
* Returns options available for the language switcher - menu or widget |
| 36 |
* either strings to display the options or default values |
| 37 |
* |
| 38 |
* @since 0.7 |
| 39 |
* |
| 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 |
| 43 |
*/ |
| 44 |
public static function get_switcher_options( $type = 'widget', $key = 'string' ) { |
| 45 |
$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 ), |
| 52 |
); |
| 53 |
return wp_list_pluck( $options, $key ); |
| 54 |
} |
| 55 |
|
| 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 |
/** |
| 113 |
* Get the language elements for use in a walker |
| 114 |
* |
| 115 |
* @since 1.2 |
| 116 |
* |
| 117 |
* @param array $args Arguments passed to {@see PLL_Switcher::the_languages()}. |
| 118 |
* @return array Language switcher elements. |
| 119 |
*/ |
| 120 |
protected function get_elements( $args ) { |
| 121 |
$first = true; |
| 122 |
$out = array(); |
| 123 |
|
| 124 |
foreach ( $this->links->model->get_languages_list( array( 'hide_empty' => $args['hide_if_empty'] ) ) as $language ) { |
| 125 |
$id = (int) $language->term_id; |
| 126 |
$order = (int) $language->term_group; |
| 127 |
$slug = $language->slug; |
| 128 |
$locale = $language->get_locale( 'display' ); |
| 129 |
$is_rtl = $language->is_rtl; |
| 130 |
$item_classes = array( 'lang-item', 'lang-item-' . $id, 'lang-item-' . esc_attr( $slug ) ); |
| 131 |
$classes = isset( $args['classes'] ) && is_array( $args['classes'] ) ? |
| 132 |
array_merge( |
| 133 |
$item_classes, |
| 134 |
$args['classes'] |
| 135 |
) : |
| 136 |
$item_classes; |
| 137 |
$link_classes = $args['link_classes'] ?? array(); |
| 138 |
$current_lang = $this->get_current_language( $args ) === $slug; |
| 139 |
|
| 140 |
if ( $current_lang ) { |
| 141 |
if ( $args['hide_current'] && ! ( $args['dropdown'] && ! $args['raw'] ) ) { |
| 142 |
continue; // Hide current language except for dropdown |
| 143 |
} else { |
| 144 |
$classes[] = 'current-lang'; |
| 145 |
} |
| 146 |
} |
| 147 |
|
| 148 |
$url = $this->get_link( $language, $args ); |
| 149 |
|
| 150 |
if ( $no_translation = empty( $url ) ) { |
| 151 |
$classes[] = 'no-translation'; |
| 152 |
} |
| 153 |
|
| 154 |
/** |
| 155 |
* Filter the link in the language switcher |
| 156 |
* |
| 157 |
* @since 0.7 |
| 158 |
* |
| 159 |
* @param string|null $url The link, null if no translation was found. |
| 160 |
* @param string $slug The language code. |
| 161 |
* @param string $locale The language locale |
| 162 |
*/ |
| 163 |
$url = apply_filters( 'pll_the_language_link', $url, $slug, $language->locale ); |
| 164 |
|
| 165 |
// Hide if no translation exists |
| 166 |
if ( empty( $url ) && $args['hide_if_no_translation'] ) { |
| 167 |
continue; |
| 168 |
} |
| 169 |
|
| 170 |
$url = empty( $url ) || $args['force_home'] ? $this->links->get_home_url( $language ) : $url; // If the page is not translated, link to the home page |
| 171 |
|
| 172 |
$name = $args['show_names'] || ! $args['show_flags'] || $args['raw'] ? ( 'slug' == $args['display_names_as'] ? $slug : $language->name ) : ''; |
| 173 |
|
| 174 |
if ( $args['raw'] && ! $args['show_flags'] ) { |
| 175 |
$flag = $language->get_display_flag_url(); |
| 176 |
} elseif ( $args['show_flags'] ) { |
| 177 |
$flag = $language->get_display_flag( empty( $args['show_names'] ) ? 'alt' : 'no-alt' ); |
| 178 |
} else { |
| 179 |
$flag = ''; |
| 180 |
} |
| 181 |
|
| 182 |
if ( $first ) { |
| 183 |
$classes[] = 'lang-item-first'; |
| 184 |
$first = false; |
| 185 |
} |
| 186 |
|
| 187 |
$out[ $slug ] = compact( 'id', 'order', 'slug', 'locale', 'is_rtl', 'name', 'url', 'flag', 'current_lang', 'no_translation', 'classes', 'link_classes' ); |
| 188 |
} |
| 189 |
|
| 190 |
return $out; |
| 191 |
} |
| 192 |
|
| 193 |
/** |
| 194 |
* Displays a language switcher |
| 195 |
* or returns the raw elements to build a custom language switcher. |
| 196 |
* |
| 197 |
* @since 0.1 |
| 198 |
* |
| 199 |
* @param PLL_Links $links Instance of PLL_Links. |
| 200 |
* @param array $args { |
| 201 |
* Optional array of arguments. |
| 202 |
* |
| 203 |
* @type int $dropdown The list is displayed as dropdown if set, defaults to 0. |
| 204 |
* @type int $echo Echoes the list if set to 1, defaults to 1. |
| 205 |
* @type int $hide_if_empty Hides languages with no posts ( or pages ) if set to 1, defaults to 1. |
| 206 |
* @type int $show_flags Displays flags if set to 1, defaults to 0. |
| 207 |
* @type int $show_names Shows language names if set to 1, defaults to 1. |
| 208 |
* @type string $display_names_as Whether to display the language name or its slug, valid options are 'slug' and 'name', defaults to name. |
| 209 |
* @type int $force_home Will always link to home in translated language if set to 1, defaults to 0. |
| 210 |
* @type int $hide_if_no_translation Hides the link if there is no translation if set to 1, defaults to 0. |
| 211 |
* @type int $hide_current Hides the current language if set to 1, defaults to 0. |
| 212 |
* @type int $post_id Returns links to the translations of the post defined by post_id if set, defaults not set. |
| 213 |
* @type int $raw Return a raw array instead of html markup if set to 1, defaults to 0. |
| 214 |
* @type string $item_spacing Whether to preserve or discard whitespace between list items, valid options are 'preserve' and 'discard', defaults to 'preserve'. |
| 215 |
* @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. |
| 216 |
* @type string $admin_current_lang The current language code in an admin context. Need to set the admin_render to 1, defaults not set. |
| 217 |
* @type string[] $classes A list of CSS classes to set to each elements outputted. |
| 218 |
* @type string[] $link_classes A list of CSS classes to set to each link outputted. |
| 219 |
* } |
| 220 |
* @return string|array either the html markup of the switcher or the raw elements to build a custom language switcher |
| 221 |
*/ |
| 222 |
public function the_languages( $links, $args = array() ) { |
| 223 |
|
| 224 |
$this->links = $links; |
| 225 |
$args = wp_parse_args( $args, self::DEFAULTS ); |
| 226 |
|
| 227 |
/** |
| 228 |
* Filter the arguments of the 'pll_the_languages' template tag |
| 229 |
* |
| 230 |
* @since 1.5 |
| 231 |
* |
| 232 |
* @param array $args |
| 233 |
*/ |
| 234 |
$args = apply_filters( 'pll_the_languages_args', $args ); |
| 235 |
|
| 236 |
// Force not to hide the language for the widget preview even if the option is checked. |
| 237 |
if ( $this->links instanceof PLL_Admin_Links ) { |
| 238 |
$args['hide_if_no_translation'] = 0; |
| 239 |
} |
| 240 |
|
| 241 |
// Prevents showing empty options in `<select>`. |
| 242 |
if ( $args['dropdown'] && ! $args['raw'] ) { |
| 243 |
$args['show_names'] = 1; |
| 244 |
} |
| 245 |
|
| 246 |
$elements = $this->get_elements( $args ); |
| 247 |
|
| 248 |
if ( $args['raw'] ) { |
| 249 |
return $elements; |
| 250 |
} |
| 251 |
|
| 252 |
if ( $args['dropdown'] ) { |
| 253 |
$args['name'] = 'lang_choice_' . $args['dropdown']; |
| 254 |
$args['class'] = 'pll-switcher-select'; |
| 255 |
$args['value'] = 'url'; |
| 256 |
$args['selected'] = $this->get_link( $this->links->model->get_language( $this->get_current_language( $args ) ), $args ); |
| 257 |
$walker = new PLL_Walker_Dropdown(); |
| 258 |
} else { |
| 259 |
$walker = new PLL_Walker_List(); |
| 260 |
} |
| 261 |
|
| 262 |
// Cast each element to stdClass because $walker::walk() expects an array of objects. |
| 263 |
foreach ( $elements as $i => $element ) { |
| 264 |
$elements[ $i ] = (object) $element; |
| 265 |
} |
| 266 |
|
| 267 |
/** |
| 268 |
* Filter the whole html markup returned by the 'pll_the_languages' template tag |
| 269 |
* |
| 270 |
* @since 0.8 |
| 271 |
* |
| 272 |
* @param string $html html returned/outputted by the template tag |
| 273 |
* @param array $args arguments passed to the template tag |
| 274 |
*/ |
| 275 |
$out = apply_filters( 'pll_the_languages', $walker->walk( $elements, -1, $args ), $args ); |
| 276 |
|
| 277 |
// Javascript to switch the language when using a dropdown list. |
| 278 |
if ( $args['dropdown'] && 0 === $args['admin_render'] ) { |
| 279 |
// Accept only few valid characters for the urls_x variable name (as the widget id includes '-' which is invalid). |
| 280 |
$out .= sprintf( |
| 281 |
'<script%1$s> |
| 282 |
document.getElementById( "%2$s" ).addEventListener( "change", function ( event ) { location.href = event.currentTarget.value; } ) |
| 283 |
</script>', |
| 284 |
current_theme_supports( 'html5', 'script' ) ? '' : ' type="text/javascript"', |
| 285 |
esc_js( $args['name'] ) |
| 286 |
); |
| 287 |
} |
| 288 |
|
| 289 |
if ( $args['echo'] ) { |
| 290 |
echo $out; // phpcs:ignore WordPress.Security.EscapeOutput |
| 291 |
} |
| 292 |
return $out; |
| 293 |
} |
| 294 |
} |
| 295 |
|