| 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\Select as Element; |
| 11 |
|
| 12 |
defined( 'ABSPATH' ) || exit; |
| 13 |
|
| 14 |
/** |
| 15 |
* Class that displays a language switcher as a selector. |
| 16 |
* |
| 17 |
* @since 3.9 |
| 18 |
*/ |
| 19 |
class Select extends Abstract_Layout { |
| 20 |
/** |
| 21 |
* Returns the markup of the switcher. |
| 22 |
* |
| 23 |
* The `<select>` tag is always output. `show_wrapper` only controls the outer |
| 24 |
* `<div>` and its screen-reader label. |
| 25 |
* |
| 26 |
* @since 3.9 |
| 27 |
* |
| 28 |
* @return string |
| 29 |
*/ |
| 30 |
public function get(): string { |
| 31 |
$out = ''; |
| 32 |
|
| 33 |
foreach ( $this->get_elements() as $element ) { |
| 34 |
$out .= $element->get(); |
| 35 |
} |
| 36 |
|
| 37 |
if ( empty( $out ) ) { |
| 38 |
return $out; |
| 39 |
} |
| 40 |
|
| 41 |
Assets::enqueue_frontend_scripts(); |
| 42 |
|
| 43 |
$cr = $this->settings->preserve_spacing ? "\n" : ''; |
| 44 |
$select = sprintf( |
| 45 |
'<select class="pll-switcher-select" id="%1$s">%2$s</select>', |
| 46 |
esc_attr( $this->settings->unique_id ), |
| 47 |
"{$cr}{$out}" |
| 48 |
); |
| 49 |
|
| 50 |
if ( ! $this->settings->show_wrapper ) { |
| 51 |
return "{$cr}{$select}{$cr}"; |
| 52 |
} |
| 53 |
|
| 54 |
$out = sprintf( |
| 55 |
'<div class="%1$s"><label class="screen-reader-text" for="%2$s">%3$s</label>%4$s</div>', |
| 56 |
esc_attr( implode( ' ', $this->get_wrapper_classes() ) ), |
| 57 |
esc_attr( $this->settings->unique_id ), |
| 58 |
esc_html( __( 'Choose a language', 'polylang' ) ), |
| 59 |
$select |
| 60 |
); |
| 61 |
|
| 62 |
return "{$cr}{$out}{$cr}"; |
| 63 |
} |
| 64 |
|
| 65 |
/** |
| 66 |
* Returns an instance of `Element\Select`. |
| 67 |
* |
| 68 |
* @since 3.9 |
| 69 |
* |
| 70 |
* @param PLL_Language $language Instance of `PLL_Language`. |
| 71 |
* @return Element |
| 72 |
*/ |
| 73 |
protected function get_element( PLL_Language $language ): Element { |
| 74 |
return new Element( $language, $this->settings, $this->links ); |
| 75 |
} |
| 76 |
} |
| 77 |
|