| 1 |
<?php |
| 2 |
/** |
| 3 |
* Tabs Menu Block |
| 4 |
* |
| 5 |
* @package WordPress |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Render callback for core/tabs-menu. |
| 10 |
* |
| 11 |
* Re-renders each tabs-menu-item inner block with per-item context (index, id, |
| 12 |
* label) injected from the tabs-list, so the tabs-menu-item render callback |
| 13 |
* can add the correct IAPI directives for each button. |
| 14 |
* |
| 15 |
* @since 7.0.0 |
| 16 |
* |
| 17 |
* @param array $attributes Block attributes. |
| 18 |
* @param string $content Block content (rendered inner blocks from save.js). |
| 19 |
* @param \WP_Block $block WP_Block instance. |
| 20 |
* |
| 21 |
* @return string Updated HTML. |
| 22 |
*/ |
| 23 |
function gutenberg_block_core_tabs_menu_render_callback( array $attributes, string $content, \WP_Block $block ): string { |
| 24 |
$tabs_list = $block->context['core/tabs-list'] ?? array(); |
| 25 |
|
| 26 |
if ( empty( $tabs_list ) ) { |
| 27 |
return $content; |
| 28 |
} |
| 29 |
|
| 30 |
// Re-render each tabs-menu-item with per-item context (index, id, label). |
| 31 |
// Match by anchor so the correct tab is found even when the two lists |
| 32 |
// are in different orders. |
| 33 |
$buttons_html = ''; |
| 34 |
|
| 35 |
foreach ( $block->parsed_block['innerBlocks'] ?? array() as $parsed_menu_item ) { |
| 36 |
if ( 'core/tabs-menu-item' !== ( $parsed_menu_item['blockName'] ?? '' ) ) { |
| 37 |
continue; |
| 38 |
} |
| 39 |
|
| 40 |
// Find the tab anchor from the menu item anchor (e.g. "tab-1-button" → "tab-1"). |
| 41 |
$menu_item_anchor = $parsed_menu_item['attrs']['anchor'] ?? ''; |
| 42 |
$tab_anchor = preg_replace( '/-button$/', '', $menu_item_anchor ); |
| 43 |
|
| 44 |
// Find the matching tab in $tabs_list by id. |
| 45 |
$tab = null; |
| 46 |
$tab_index = 0; |
| 47 |
foreach ( $tabs_list as $index => $candidate ) { |
| 48 |
if ( ( $candidate['id'] ?? '' ) === $tab_anchor ) { |
| 49 |
$tab = $candidate; |
| 50 |
$tab_index = $index; |
| 51 |
break; |
| 52 |
} |
| 53 |
} |
| 54 |
|
| 55 |
// Skip menu items with no matching tab. |
| 56 |
if ( null === $tab ) { |
| 57 |
continue; |
| 58 |
} |
| 59 |
|
| 60 |
$item_context = array_merge( |
| 61 |
$block->context, |
| 62 |
array( |
| 63 |
'core/tabs-menu-item-index' => $tab_index, |
| 64 |
'core/tabs-menu-item-id' => $tab['id'] ?? '', |
| 65 |
'core/tabs-menu-item-label' => $tab['label'] ?? '', |
| 66 |
) |
| 67 |
); |
| 68 |
|
| 69 |
$menu_item_block = new WP_Block( $parsed_menu_item, $item_context ); |
| 70 |
$buttons_html .= $menu_item_block->render(); |
| 71 |
} |
| 72 |
|
| 73 |
// Rebuild the wrapper using get_block_wrapper_attributes(). |
| 74 |
$wrapper_attributes = get_block_wrapper_attributes( array( 'role' => 'tablist' ) ); |
| 75 |
return sprintf( '<div %s>%s</div>', $wrapper_attributes, $buttons_html ); |
| 76 |
} |
| 77 |
|
| 78 |
/** |
| 79 |
* Registers the `core/tabs-menu` block on the server. |
| 80 |
* |
| 81 |
* @since 7.0.0 |
| 82 |
*/ |
| 83 |
function gutenberg_register_block_core_tabs_menu() { |
| 84 |
register_block_type_from_metadata( |
| 85 |
__DIR__ . '/tabs-menu', |
| 86 |
array( |
| 87 |
'render_callback' => 'gutenberg_block_core_tabs_menu_render_callback', |
| 88 |
) |
| 89 |
); |
| 90 |
} |
| 91 |
add_action( 'init', 'gutenberg_register_block_core_tabs_menu', 20 ); |
| 92 |
|