| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace WP_Syntex\Polylang\Switcher\Layout; |
| 7 |
|
| 8 |
use PLL_Language; |
| 9 |
use WP_Syntex\Polylang\Switcher\Assets; |
| 10 |
use WP_Syntex\Polylang\Switcher\Element\Nav as Element; |
| 11 |
|
| 12 |
defined( 'ABSPATH' ) || exit; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class that displays a language switcher as a dropdown. |
| 16 |
* |
| 17 |
* @since 3.9 |
| 18 |
*/ |
| 19 |
class Dropdown extends Abstract_Layout { |
| 20 |
/** |
| 21 |
* Returns the markup of the switcher. |
| 22 |
* |
| 23 |
* @since 3.9 |
| 24 |
* |
| 25 |
* @return string |
| 26 |
*/ |
| 27 |
public function get(): string { |
| 28 |
$current_item = $this->get_top_level_item(); |
| 29 |
|
| 30 |
if ( empty( $current_item ) ) { |
| 31 |
return ''; |
| 32 |
} |
| 33 |
|
| 34 |
$out = ''; |
| 35 |
|
| 36 |
foreach ( $this->get_elements() as $element ) { |
| 37 |
$out .= $element->get(); |
| 38 |
} |
| 39 |
|
| 40 |
if ( empty( $out ) || ! $this->settings->show_wrapper ) { |
| 41 |
return $out; |
| 42 |
} |
| 43 |
|
| 44 |
Assets::enqueue_frontend_scripts(); |
| 45 |
|
| 46 |
$cr = $this->settings->preserve_spacing ? "\n" : ''; |
| 47 |
$out = sprintf( |
| 48 |
"{$cr}<div class=\"pll-switcher-inner\">{$cr}%s{$cr}%s{$cr}<ul>{$cr}%s</ul>{$cr}</div>", |
| 49 |
$current_item, |
| 50 |
$this->get_button(), |
| 51 |
$out |
| 52 |
); |
| 53 |
$out = sprintf( |
| 54 |
'<%1$s id="%2$s" class="%3$s" aria-label="%4$s">%5$s</%1$s>', |
| 55 |
$this->get_nav_tag(), |
| 56 |
esc_attr( $this->settings->unique_id ), |
| 57 |
esc_attr( implode( ' ', $this->get_wrapper_classes() ) ), |
| 58 |
/* translators: accessibility text */ |
| 59 |
esc_attr__( 'Choose a language', 'polylang' ), |
| 60 |
$out |
| 61 |
); |
| 62 |
|
| 63 |
return "{$cr}{$out}{$cr}"; |
| 64 |
} |
| 65 |
|
| 66 |
/** |
| 67 |
* Returns an instance of `Element\Nav`. |
| 68 |
* |
| 69 |
* @since 3.9 |
| 70 |
* |
| 71 |
* @param PLL_Language $language Instance of `PLL_Language`. |
| 72 |
* @return Element |
| 73 |
*/ |
| 74 |
protected function get_element( PLL_Language $language ): Element { |
| 75 |
return new Element( $language, $this->settings, $this->links ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Returns the markup of the current item. |
| 80 |
* |
| 81 |
* @since 3.9 |
| 82 |
* |
| 83 |
* @return string |
| 84 |
*/ |
| 85 |
private function get_top_level_item(): string { |
| 86 |
if ( ! empty( $this->links->curlang ) ) { |
| 87 |
$curlang = $this->links->curlang; |
| 88 |
} else { |
| 89 |
$curlang = $this->links->model->languages->get( $this->links->options['default_lang'] ); |
| 90 |
} |
| 91 |
|
| 92 |
if ( empty( $curlang ) ) { |
| 93 |
return ''; |
| 94 |
} |
| 95 |
|
| 96 |
return $this->get_element( $curlang )->get_link(); |
| 97 |
} |
| 98 |
|
| 99 |
/** |
| 100 |
* Returns the markup of the button. |
| 101 |
* |
| 102 |
* @since 3.9 |
| 103 |
* |
| 104 |
* @return string |
| 105 |
*/ |
| 106 |
private function get_button(): string { |
| 107 |
$svg = '<svg xmlns="http://www.w3.org/2000/svg" width="12" height="12" viewBox="0 0 12 12" fill="none" aria-hidden="true" focusable="false"><path d="M1.5 4L6 8L10.5 4" stroke-width="1.5"></path></svg>'; |
| 108 |
return sprintf( |
| 109 |
'<button aria-label="%1$s" class="pll-submenu-toggle">%2$s</button>', |
| 110 |
/* translators: accessibility text */ |
| 111 |
esc_attr__( 'Open/Close languages submenu', 'polylang' ), |
| 112 |
$svg |
| 113 |
); |
| 114 |
} |
| 115 |
} |
| 116 |
|