PluginProbe
Gutenberg / 23.3.1
Gutenberg v23.3.1
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 7.4.0 All 402 releases
gutenberg / build / scripts / block-library / tab.php

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

71 lines 2.1 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Tab Block
4 *
5 * @package WordPress
6 */
7
8 /**
9 * Render callback for core/tab.
10 *
11 * Injects the tab label and IAPI directives into the saved button HTML.
12 * Per-item context (index, id, label) is provided by the parent tab-list
13 * render callback before this is called.
14 *
15 * @since 7.0.0
16 *
17 * @param array $attributes Block attributes.
18 * @param string $content Block content (styled button from save.js).
19 * @param \WP_Block $block WP_Block instance.
20 *
21 * @return string Updated HTML.
22 */
23 function gutenberg_block_core_tab_render_callback( array $attributes, string $content, \WP_Block $block ): string {
24 $tab_index = $block->context['core/tab-index'] ?? 0;
25 $tab_id = $block->context['core/tab-id'] ?? '';
26 $tab_label = $block->context['core/tab-label'] ?? '';
27
28 if ( empty( $tab_id ) ) {
29 $tab_id = 'tab-' . $tab_index;
30 }
31
32 // Add Interactivity API directives and tab-specific attributes to the button.
33 $tag_processor = new WP_HTML_Tag_Processor( $content );
34
35 if ( $tag_processor->next_tag() ) {
36 $tag_processor->set_attribute( 'id', 'tab__' . $tab_id );
37 $tag_processor->set_attribute( 'aria-controls', $tab_id );
38 $tag_processor->set_attribute( 'data-wp-on--click', 'actions.handleTabClick' );
39 $tag_processor->set_attribute( 'data-wp-on--keydown', 'actions.handleTabKeyDown' );
40 $tag_processor->set_attribute( 'data-wp-bind--aria-selected', 'state.isActiveTab' );
41 $tag_processor->set_attribute( 'data-wp-bind--tabindex', 'state.tabIndexAttribute' );
42 $tag_processor->set_attribute(
43 'data-wp-context',
44 wp_json_encode( array( 'tabIndex' => $tab_index ) )
45 );
46 }
47
48 // Inject the tab label into the button.
49 return preg_replace(
50 '/(<button\b[^>]*>).*?(<\/button>)/s',
51 '$1<span>' . wp_kses_post( $tab_label ) . '</span>$2',
52 $tag_processor->get_updated_html(),
53 1
54 );
55 }
56
57 /**
58 * Registers the `core/tab` block on the server.
59 *
60 * @since 7.0.0
61 */
62 function gutenberg_register_block_core_tab() {
63 register_block_type_from_metadata(
64 __DIR__ . '/tab',
65 array(
66 'render_callback' => 'gutenberg_block_core_tab_render_callback',
67 )
68 );
69 }
70 add_action( 'init', 'gutenberg_register_block_core_tab', 20 );
71