| 1 |
<?php |
| 2 |
/** |
| 3 |
* @package Polylang |
| 4 |
*/ |
| 5 |
|
| 6 |
namespace WP_Syntex\Polylang\Blocks\Language_Switcher\Standard; |
| 7 |
|
| 8 |
use PLL_Switcher; |
| 9 |
use WP_Syntex\Polylang\Blocks\Language_Switcher\Abstract_Block; |
| 10 |
|
| 11 |
/** |
| 12 |
* Language switcher block. |
| 13 |
* |
| 14 |
* @since 2.8 |
| 15 |
* @since 3.2 Extends now the PLL_Abstract_Language_Switcher_Block abstract class. |
| 16 |
* @since 3.8 Moved to Polylang Core and renamed to Language_Switcher\Standard\Block. |
| 17 |
*/ |
| 18 |
class Block extends Abstract_Block { |
| 19 |
|
| 20 |
/** |
| 21 |
* Returns the language switcher block name with the Polylang's namespace. |
| 22 |
* |
| 23 |
* @since 3.2 |
| 24 |
* |
| 25 |
* @return string The block name. |
| 26 |
*/ |
| 27 |
protected function get_block_name() { |
| 28 |
return 'polylang/language-switcher'; |
| 29 |
} |
| 30 |
|
| 31 |
/** |
| 32 |
* Renders the `polylang/language-switcher` block on server. |
| 33 |
* |
| 34 |
* @since 2.8 |
| 35 |
* @since 3.2 Renamed according to its parent abstract class. |
| 36 |
* @since 3.3 Accepts two new parameters, $content and $block. |
| 37 |
* |
| 38 |
* @param array $attributes The block attributes. |
| 39 |
* @param string $content The saved content. Unused. |
| 40 |
* @param \WP_Block $block The parsed block. Unused. |
| 41 |
* @return string Returns the language switcher. |
| 42 |
*/ |
| 43 |
public function render( $attributes, $content, $block ) { //phpcs:ignore VariableAnalysis.CodeAnalysis.VariableAnalysis.UnusedVariable |
| 44 |
static $dropdown_id = 0; |
| 45 |
++$dropdown_id; |
| 46 |
|
| 47 |
// Sets a unique id for dropdown in PLL_Switcher::the_language(). |
| 48 |
$attributes['dropdown'] = empty( $attributes['dropdown'] ) ? 0 : $dropdown_id; |
| 49 |
|
| 50 |
$attributes = $this->set_attributes_for_block( $attributes ); |
| 51 |
|
| 52 |
$attributes['raw'] = false; |
| 53 |
$switcher = new PLL_Switcher(); |
| 54 |
$switcher_output = $switcher->the_languages( $this->links, $attributes ); |
| 55 |
|
| 56 |
if ( empty( $switcher_output ) ) { |
| 57 |
return ''; |
| 58 |
} |
| 59 |
|
| 60 |
$aria_label = __( 'Choose a language', 'polylang' ); |
| 61 |
if ( $attributes['dropdown'] ) { |
| 62 |
$switcher_output = '<label class="screen-reader-text" for="' . esc_attr( 'lang_choice_' . $attributes['dropdown'] ) . '">' . esc_html( $aria_label ) . '</label>' . $switcher_output; |
| 63 |
|
| 64 |
$wrap_tag = '<div %1$s>%2$s</div>'; |
| 65 |
} else { |
| 66 |
$wrap_tag = '<nav role="navigation" aria-label="' . esc_attr( $aria_label ) . '"><ul %1$s>%2$s</ul></nav>'; |
| 67 |
} |
| 68 |
|
| 69 |
$wrap_attributes = get_block_wrapper_attributes(); |
| 70 |
|
| 71 |
return sprintf( $wrap_tag, $wrap_attributes, $switcher_output ); |
| 72 |
} |
| 73 |
|
| 74 |
/** |
| 75 |
* Returns the path to the block JSON file directory. |
| 76 |
* The directory name being used to register a block. |
| 77 |
* |
| 78 |
* @since 3.8 |
| 79 |
* |
| 80 |
* @return string The path to the block. |
| 81 |
*/ |
| 82 |
protected function get_path(): string { |
| 83 |
return __DIR__; |
| 84 |
} |
| 85 |
} |
| 86 |
|