PluginProbe
Polylang / trunk
Polylang vtrunk
3.8.10 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 All 234 releases
← All changes | src/modules/Blocks/Language_Switcher/Navigation/Block.php +74 -82 3.8.10 → trunk View file →
@@ -5,11 +5,15 @@
5 5
6 6 namespace WP_Syntex\Polylang\Blocks\Language_Switcher\Navigation;
7 7
8 8 use WP_Block;
9 -use PLL_Switcher;
9 +use PLL_Language;
10 10 use SplObjectStorage;
11 11 use WP_HTML_Tag_Processor;
12 +use WP_Syntex\Polylang\Switcher\Switcher;
13 +use WP_Syntex\Polylang\Switcher\Element\Abstract_Element;
14 +use WP_Syntex\Polylang\Switcher\Element\Nav as Element;
15 +use WP_Syntex\Polylang\Switcher\Settings\Menu as Settings;
12 16 use WP_Syntex\Polylang\Blocks\Language_Switcher\Abstract_Block;
13 17
14 18 /**
15 19 * Language switcher block for navigation.
@@ -28,17 +32,16 @@
28 32 /**
29 33 * Trusted locale and label, keyed by the inner navigation blocks created by this switcher.
30 34 * Blocks parsed from saved content are never the same instances, so they can't be found here.
31 35 *
32 - * @var SplObjectStorage
33 - * @phpstan-var SplObjectStorage<WP_Block, array{locale: string, label: string}>
36 + * @var SplObjectStorage<WP_Block, array{locale: string, label: string}>
34 37 */
35 - private $internal_blocks;
38 + private SplObjectStorage $internal_blocks;
36 39
37 40 /**
38 41 * Constructor.
39 42 *
40 - * @since 3.8.10
43 + * @since 3.9
41 44 *
42 45 * @param \PLL_Base $polylang Polylang object.
43 46 */
44 47 public function __construct( &$polylang ) {
@@ -59,29 +62,36 @@
59 62
60 63 add_action( 'rest_api_init', array( $this, 'register_switcher_menu_item_options_meta_rest_field' ) );
61 64 add_filter( 'render_block_core/navigation-link', array( $this, 'render_custom_attributes' ), 10, 3 );
62 65 add_filter( 'render_block_core/navigation-submenu', array( $this, 'render_custom_attributes' ), 10, 3 );
63 - add_action( 'init', array( $this, 'register_editor_style' ) );
66 + add_action( 'init', array( $this, 'register_styles' ) );
64 67
65 68 return $this;
66 69 }
67 70
68 71 /**
69 - * Registers the editor style for the navigation language switcher block.
72 + * Registers the styles for the navigation language switcher block.
70 73 *
71 74 * @since 3.8
72 75 *
73 76 * @return void
74 77 */
75 - public function register_editor_style(): void {
78 + public function register_styles(): void {
76 79 $suffix = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min';
77 80
78 81 wp_register_style(
79 - 'pll-navigation-language-switcher-editor-style',
80 - plugins_url( 'css/build/navigation-language-switcher-editor-style' . $suffix . '.css', POLYLANG_ROOT_FILE ),
82 + 'pll-navigation-language-switcher',
83 + plugins_url( 'css/build/navigation-language-switcher' . $suffix . '.css', POLYLANG_ROOT_FILE ),
81 84 array(),
82 85 POLYLANG_VERSION
83 86 );
87 +
88 + wp_register_style(
89 + 'pll-navigation-language-switcher-editor',
90 + plugins_url( 'css/build/navigation-language-switcher-editor' . $suffix . '.css', POLYLANG_ROOT_FILE ),
91 + array(),
92 + POLYLANG_VERSION
93 + );
84 94 }
85 95
86 96 /**
87 97 * Returns the navigation language switcher block name with the Polylang's namespace.
@@ -105,59 +115,38 @@
105 115 * @param WP_Block $block The parsed block.
106 116 * @return string The HTML string output to serve.
107 117 */
108 118 public function render( $attributes, $content, $block ) {
109 - $attributes = $this->set_attributes_for_block( $attributes );
110 - $switcher = new PLL_Switcher();
111 - $switcher_elements = (array) $switcher->the_languages( $this->links, array_merge( $attributes, array( 'raw' => true ) ) );
119 + $settings = new Settings( $attributes );
120 + $elements = ( new Switcher( $settings, $this->links ) )->get_elements();
112 121
113 - if ( empty( $switcher_elements ) ) {
122 + if ( empty( $elements ) ) {
114 123 return '';
115 124 }
116 125
117 - $generated_class = wp_apply_generated_classname_support( $block->block_type )['class'];
126 + $attributes['className'] .= ' ' . wp_apply_generated_classname_support( $block->block_type )['class'];
118 127
119 - if ( $attributes['dropdown'] ) {
120 - $inner_nav_link_blocks = array();
121 - $top_level_lang = reset( $switcher_elements );
122 - foreach ( $switcher_elements as $switcher_element ) {
123 - $inner_nav_link_blocks[] = $this->create_inner_block(
124 - 'core/navigation-link',
125 - $this->get_core_block_attributes( $switcher_element ),
126 - $switcher_element,
127 - $block->context,
128 - $attributes
129 - );
128 + if ( 'dropdown' === $settings->layout ) {
129 + $top_level_element = $this->get_top_level_element( $settings );
130 130
131 - if ( $switcher_element['current_lang'] && ! $attributes['hide_current'] ) {
132 - $top_level_lang = $switcher_element;
133 - }
131 + if ( empty( $top_level_element ) ) {
132 + return '';
134 133 }
135 134
136 - $submenu_attributes = $this->get_core_block_attributes( $top_level_lang );
137 - $submenu_attributes['className'] .= ' ' . $generated_class;
138 - $submenu_block = $this->create_inner_block(
139 - 'core/navigation-submenu',
140 - $submenu_attributes,
141 - $top_level_lang,
142 - $block->context,
143 - $attributes,
144 - $inner_nav_link_blocks
145 - );
146 - $output = $submenu_block->render();
135 + $inner_nav_link_blocks = array();
136 + $attributes_wo_classname = array_diff_key( $attributes, array( 'className' => '' ) );
137 +
138 + foreach ( $elements as $element ) {
139 + $inner_nav_link_blocks[] = $this->create_inner_block( 'core/navigation-link', $attributes_wo_classname, $element, $block->context );
140 + }
141 +
142 + $submenu_block = $this->create_inner_block( 'core/navigation-submenu', $attributes, $top_level_element, $block->context, $inner_nav_link_blocks );
143 + $output = $submenu_block->render();
147 144 } else {
148 145 $output = '';
149 146
150 - foreach ( $switcher_elements as $switcher_element ) {
151 - $link_attributes = $this->get_core_block_attributes( $switcher_element );
152 - $link_attributes['className'] .= ' ' . $generated_class;
153 - $output .= $this->create_inner_block(
154 - 'core/navigation-link',
155 - $link_attributes,
156 - $switcher_element,
157 - $block->context,
158 - $attributes
159 - )->render();
147 + foreach ( $elements as $element ) {
148 + $output .= $this->create_inner_block( 'core/navigation-link', $attributes, $element, $block->context )->render();
160 149 }
161 150 }
162 151
163 152 return $output;
@@ -256,23 +245,22 @@
256 245
257 246 /**
258 247 * Creates an inner navigation block and stores the locale and label to render for it.
259 248 *
260 - * @since 3.8.10
249 + * @since 3.9
261 250 *
262 - * @param string $block_name Core block name.
263 - * @param array $core_attributes Attributes to be rendered by core.
264 - * @param array $switcher_item Array of a switcher item data.
265 - * @param array $context Block context.
266 - * @param array $attributes Array of polylang/navigation-language-switcher attributes.
267 - * @param WP_Block[] $inner_blocks Optional inner blocks.
251 + * @param string $block_name Core block name.
252 + * @param array $attributes Array of polylang/navigation-language-switcher attributes.
253 + * @param Abstract_Element $element Switcher element.
254 + * @param array $context Block context.
255 + * @param WP_Block[] $inner_blocks Optional inner blocks.
268 256 * @return WP_Block
269 257 */
270 - private function create_inner_block( $block_name, $core_attributes, $switcher_item, $context, $attributes, $inner_blocks = array() ) {
258 + private function create_inner_block( string $block_name, array $attributes, Abstract_Element $element, array $context, array $inner_blocks = array() ): WP_Block {
271 259 $block = new WP_Block(
272 260 array(
273 261 'blockName' => $block_name,
274 - 'attrs' => $core_attributes,
262 + 'attrs' => $this->get_core_block_attributes( $attributes, $element ),
275 263 'innerBlocks' => $inner_blocks,
276 264 ),
277 265 $context
278 266 );
@@ -279,10 +267,10 @@
279 267
280 268 $this->internal_blocks->offsetSet(
281 269 $block,
282 270 array(
283 - 'locale' => $switcher_item['locale'],
284 - 'label' => $this->get_link_label( $switcher_item, $attributes ),
271 + 'locale' => $element->locale,
272 + 'label' => $this->apply_flag_styles_to_markup( $element->get_label(), $attributes ),
285 273 )
286 274 );
287 275
288 276 return $block;
@@ -288,31 +276,23 @@
288 276 return $block;
289 277 }
290 278
291 279 /**
292 - * Builds the language switcher link label from a trusted switcher item.
280 + * Returns the top-level language element for a dropdown layout.
293 281 *
294 - * @since 3.8.10
282 + * @since 3.9
295 283 *
296 - * @param array $switcher_item Array of a switcher item data.
297 - * @param array $attributes Array of polylang/navigation-language-switcher attributes.
298 - * @return string
284 + * @param Settings $settings Switcher settings.
285 + * @return Element|null
299 286 */
300 - private function get_link_label( $switcher_item, $attributes ) {
301 - $show_flags = ! empty( $attributes['show_flags'] );
302 - $show_names = ! empty( $attributes['show_names'] );
303 - $link_label = '';
287 + private function get_top_level_element( Settings $settings ): ?Element {
288 + $curlang = $this->links->curlang ?? $this->model->get_default_language();
304 289
305 - if ( $show_flags ) {
306 - $link_label .= $switcher_item['flag'];
290 + if ( ! $curlang instanceof PLL_Language ) {
291 + return null;
307 292 }
308 293
309 - if ( $show_names ) {
310 - $name = esc_html( $switcher_item['name'] );
311 - $link_label .= $show_flags ? ' ' . $name : $name;
312 - }
313 -
314 - return $link_label;
294 + return new Element( $curlang, $settings, $this->links );
315 295 }
316 296
317 297 /**
318 298 * Returns attributes that fit for core/navigation-link or core/navigation-submenu and specific to polylang/navigation-language-switcher.
@@ -318,15 +298,27 @@
318 298 * Returns attributes that fit for core/navigation-link or core/navigation-submenu and specific to polylang/navigation-language-switcher.
319 299 *
320 300 * @since 3.6
321 301 *
322 - * @param array $switcher_item Array of a switcher item data.
302 + * @param array $attributes Array of polylang/navigation-language-switcher attributes.
303 + * @param Abstract_Element $element Switcher element.
323 304 * @return array Attributes to be rendered by core.
324 305 */
325 - private function get_core_block_attributes( $switcher_item ) {
306 + private function get_core_block_attributes( array $attributes, Abstract_Element $element ): array {
307 + $class_name = trim( implode( ' ', $element->item_classes ) );
308 +
309 + if ( ! empty( $attributes['show_flags'] ) ) {
310 + $aspect_ratio = $attributes['flag_aspect_ratio'] ?? '3:2';
311 + $class_name .= ' pll-aspect-ratio-' . str_replace( ':', '', $aspect_ratio );
312 + }
313 +
314 + if ( ! empty( $attributes['className'] ) ) {
315 + $class_name .= ' ' . $attributes['className'];
316 + }
317 +
326 318 return array(
327 319 'label' => static::PLACEHOLDER,
328 - 'url' => $switcher_item['url'],
329 - 'className' => trim( implode( ' ', (array) $switcher_item['classes'] ) ),
320 + 'url' => $element->url,
321 + 'className' => $class_name,
330 322 );
331 323 }
332 324 }