PluginProbe
Gutenberg / 24.0.0
Gutenberg v24.0.0
24.0.0 23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 All 403 releases
gutenberg / build / scripts / block-library / tab-panel.php

tab-panel.php in Gutenberg 24.0.0, at build/scripts/block-library/tab-panel.php

75 lines 2.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Tab Panel Block
4 *
5 * @package WordPress
6 */
7
8 /**
9 * Render callback for core/tab-panel.
10 *
11 * @since 7.1.0
12 *
13 * @param array $attributes Block attributes.
14 * @param string $content Block content.
15 * @param \WP_Block $block Block instance.
16 *
17 * @return string Updated HTML.
18 */
19 function gutenberg_block_core_tab_panel_render( array $attributes, string $content, \WP_Block $block ): string {
20 $tabs_id = $block->context['core/tabs-id'] ?? '';
21
22 static $tab_counters = array();
23
24 if ( ! isset( $tab_counters[ $tabs_id ] ) ) {
25 $tab_counters[ $tabs_id ] = 1;
26 }
27
28 $tab_number = $tab_counters[ $tabs_id ];
29 ++$tab_counters[ $tabs_id ];
30
31 $tag_processor = new WP_HTML_Tag_Processor( $content );
32 $tag_processor->next_tag( array( 'class_name' => 'wp-block-tab-panel' ) );
33
34 // Use the user's custom anchor if present, otherwise fall back to
35 // the generated position-based ID.
36 $tab_id = (string) $tag_processor->get_attribute( 'id' );
37 if ( empty( $tab_id ) ) {
38 $tab_id = ! empty( $tabs_id )
39 ? $tabs_id . '-tab-' . $tab_number
40 : 'tab-' . $tab_number;
41 $tag_processor->set_attribute( 'id', $tab_id );
42 }
43
44 $tag_processor->set_attribute( 'aria-labelledby', 'tab__' . $tab_id );
45
46 /*
47 * Hiding a panel is left to the client, which is why `hidden` is bound to a
48 * state getter the server cannot resolve rather than set here: a visitor
49 * whose browser never runs the block's script is then given the content of
50 * every panel instead of none of it. `core/accordion` works the same way.
51 */
52 $tag_processor->set_attribute( 'data-wp-bind--hidden', 'state.isHidden' );
53 $tag_processor->set_attribute( 'data-wp-bind--tabindex', 'state.tabIndexAttribute' );
54 $tag_processor->set_attribute( 'data-wp-on--beforematch', 'actions.handleBeforeMatch' );
55
56 return (string) $tag_processor->get_updated_html();
57 }
58
59 /**
60 * Registers the `core/tab-panel` block on the server.
61 *
62 * @hook init
63 *
64 * @since 7.1.0
65 */
66 function gutenberg_register_block_core_tab_panel() {
67 register_block_type_from_metadata(
68 __DIR__ . '/tab-panel',
69 array(
70 'render_callback' => 'gutenberg_block_core_tab_panel_render',
71 )
72 );
73 }
74 add_action( 'init', 'gutenberg_register_block_core_tab_panel', 20 );
75