| 1 |
<?php |
| 2 |
/** |
| 3 |
* Tabs Block |
| 4 |
* |
| 5 |
* @package WordPress |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Render callback for core/tab. |
| 10 |
* |
| 11 |
* @since 7.0.0 |
| 12 |
* |
| 13 |
* @param array $attributes Block attributes. |
| 14 |
* @param string $content Block content. |
| 15 |
* |
| 16 |
* @return string Updated HTML. |
| 17 |
*/ |
| 18 |
function gutenberg_block_core_tab_render( array $attributes, string $content ): string { |
| 19 |
$tag_processor = new WP_HTML_Tag_Processor( $content ); |
| 20 |
$tag_processor->next_tag( array( 'class_name' => 'wp-block-tab' ) ); |
| 21 |
$tab_id = (string) $tag_processor->get_attribute( 'id' ); |
| 22 |
// If no id, generate a unique one |
| 23 |
if ( empty( $tab_id ) ) { |
| 24 |
$tab_id = sanitize_title( $attributes['label'] ); |
| 25 |
$tag_processor->set_attribute( 'id', $tab_id ); |
| 26 |
} |
| 27 |
|
| 28 |
/** |
| 29 |
* Add interactivity to the tab element. |
| 30 |
*/ |
| 31 |
$tag_processor->set_attribute( |
| 32 |
'data-wp-interactive', |
| 33 |
'core/tabs/private' |
| 34 |
); |
| 35 |
$tag_processor->set_attribute( |
| 36 |
'data-wp-context', |
| 37 |
wp_json_encode( |
| 38 |
array( |
| 39 |
'tab' => array( |
| 40 |
'id' => $tab_id, |
| 41 |
), |
| 42 |
) |
| 43 |
) |
| 44 |
); |
| 45 |
|
| 46 |
/** |
| 47 |
* Process accessibility and interactivity attributes. |
| 48 |
*/ |
| 49 |
$tag_processor->set_attribute( 'role', 'tabpanel' ); |
| 50 |
$tag_processor->set_attribute( 'aria-labelledby', 'tab__' . $tab_id ); |
| 51 |
$tag_processor->set_attribute( 'data-wp-bind--hidden', '!state.isActiveTab' ); |
| 52 |
$tag_processor->set_attribute( 'tabindex', 0 ); |
| 53 |
|
| 54 |
return (string) $tag_processor->get_updated_html(); |
| 55 |
} |
| 56 |
|
| 57 |
/** |
| 58 |
* Registers the `core/tab` block on the server. |
| 59 |
* |
| 60 |
* @hook init |
| 61 |
* |
| 62 |
* @since 7.0.0 |
| 63 |
*/ |
| 64 |
function gutenberg_register_block_core_tab() { |
| 65 |
register_block_type_from_metadata( |
| 66 |
__DIR__ . '/tab', |
| 67 |
array( |
| 68 |
'render_callback' => 'gutenberg_block_core_tab_render', |
| 69 |
) |
| 70 |
); |
| 71 |
} |
| 72 |
add_action( 'init', 'gutenberg_register_block_core_tab', 20 ); |
| 73 |
|