| 1 |
<?php |
| 2 |
/** |
| 3 |
* Tabs Block |
| 4 |
* |
| 5 |
* @package WordPress |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Extract tabs list from tab-panel innerblocks. |
| 10 |
* |
| 11 |
* @since 7.0.0 |
| 12 |
* |
| 13 |
* @param array $innerblocks Parsed inner blocks of tabs block. |
| 14 |
* |
| 15 |
* @return array List of tabs with id, label, index. |
| 16 |
*/ |
| 17 |
function gutenberg_block_core_tabs_generate_tabs_list( array $innerblocks = array() ): array { |
| 18 |
$tabs_list = array(); |
| 19 |
|
| 20 |
// Find tab-panel block |
| 21 |
foreach ( $innerblocks as $inner_block ) { |
| 22 |
if ( 'core/tab-panel' === ( $inner_block['blockName'] ?? '' ) ) { |
| 23 |
$tab_index = 0; |
| 24 |
foreach ( $inner_block['innerBlocks'] ?? array() as $tab_block ) { |
| 25 |
if ( 'core/tab' === ( $tab_block['blockName'] ?? '' ) ) { |
| 26 |
$attrs = $tab_block['attrs'] ?? array(); |
| 27 |
$tab_label = $attrs['label'] ?? ''; |
| 28 |
|
| 29 |
// Try to get the ID from the rendered content |
| 30 |
$tab_id = $attrs['anchor'] ?? ''; |
| 31 |
if ( empty( $tab_id ) && ! empty( $tab_block['innerHTML'] ) ) { |
| 32 |
$tag_processor = new WP_HTML_Tag_Processor( $tab_block['innerHTML'] ); |
| 33 |
if ( $tag_processor->next_tag( array( 'class_name' => 'wp-block-tab' ) ) ) { |
| 34 |
$tab_id = $tag_processor->get_attribute( 'id' ) ?? ''; |
| 35 |
} |
| 36 |
} |
| 37 |
if ( empty( $tab_id ) ) { |
| 38 |
$tab_id = 'tab-' . $tab_index; |
| 39 |
} |
| 40 |
|
| 41 |
$tabs_list[] = array( |
| 42 |
'id' => esc_attr( $tab_id ), |
| 43 |
'label' => $tab_label, |
| 44 |
'index' => $tab_index, |
| 45 |
); |
| 46 |
++$tab_index; |
| 47 |
} |
| 48 |
} |
| 49 |
break; |
| 50 |
} |
| 51 |
} |
| 52 |
|
| 53 |
return $tabs_list; |
| 54 |
} |
| 55 |
|
| 56 |
/** |
| 57 |
* Filter to provide tabs list context to core/tabs and core/tabs-menu blocks. |
| 58 |
* It is more performant to do this here, once, rather than in the tabs render and tabs context filters. |
| 59 |
* In this way core/tabs is both a provider and a consumer of the core/tabs-list context. |
| 60 |
* |
| 61 |
* @since 7.0.0 |
| 62 |
* |
| 63 |
* @param array $context Default block context. |
| 64 |
* @param array $parsed_block The block being rendered. |
| 65 |
* |
| 66 |
* @return array Modified context. |
| 67 |
*/ |
| 68 |
function gutenberg_block_core_tabs_provide_context( array $context, array $parsed_block ): array { |
| 69 |
if ( 'core/tabs' === $parsed_block['blockName'] ) { |
| 70 |
$tabs_list = gutenberg_block_core_tabs_generate_tabs_list( $parsed_block['innerBlocks'] ?? array() ); |
| 71 |
$context['core/tabs-list'] = $tabs_list; |
| 72 |
$context['core/tabs-id'] = $parsed_block['attrs']['anchor'] ?? wp_unique_id( 'tabs_' ); // Generate a unique ID for each tabs instance. Used for 3rd party extensibility to identify the tabs instance. |
| 73 |
} |
| 74 |
|
| 75 |
return $context; |
| 76 |
} |
| 77 |
add_filter( 'render_block_context', 'gutenberg_block_core_tabs_provide_context', 10, 2 ); |
| 78 |
|
| 79 |
/** |
| 80 |
* Render callback for core/tabs. |
| 81 |
* |
| 82 |
* @since 7.0.0 |
| 83 |
* |
| 84 |
* @param array $attributes Block attributes. |
| 85 |
* @param string $content Block content. |
| 86 |
* @param \WP_Block $block WP_Block instance. |
| 87 |
* |
| 88 |
* @return string Updated HTML. |
| 89 |
*/ |
| 90 |
function gutenberg_block_core_tabs_render_block_callback( array $attributes, string $content, \WP_Block $block ): string { |
| 91 |
$active_tab_index = $attributes['activeTabIndex'] ?? 0; |
| 92 |
$tabs_list = $block->context['core/tabs-list'] ?? array(); |
| 93 |
$tabs_id = $block->context['core/tabs-id'] ?? null; |
| 94 |
|
| 95 |
if ( empty( $tabs_id ) ) { |
| 96 |
// If malformed tabs, return early to avoid errors. |
| 97 |
return ''; |
| 98 |
} |
| 99 |
|
| 100 |
$is_vertical = false; |
| 101 |
|
| 102 |
$tag_processor = new WP_HTML_Tag_Processor( $content ); |
| 103 |
|
| 104 |
$tag_processor->next_tag( array( 'class_name' => 'wp-block-tabs' ) ); |
| 105 |
$tag_processor->set_attribute( 'data-wp-interactive', 'core/tabs/private' ); |
| 106 |
|
| 107 |
// Inspect inside the tabs-menu to see if its vertical or not. |
| 108 |
$tag_processor->set_bookmark( 'core/tabs_wrapper' ); |
| 109 |
while ( $tag_processor->next_tag( array( 'class_name' => 'wp-block-tabs-menu' ) ) ) { |
| 110 |
if ( $tag_processor->has_class( 'is-vertical' ) ) { |
| 111 |
$is_vertical = true; |
| 112 |
break; |
| 113 |
} |
| 114 |
} |
| 115 |
$tag_processor->seek( 'core/tabs_wrapper' ); |
| 116 |
|
| 117 |
$tag_processor->set_attribute( |
| 118 |
'data-wp-context', |
| 119 |
wp_json_encode( |
| 120 |
array( |
| 121 |
'tabsId' => $tabs_id, |
| 122 |
'activeTabIndex' => $active_tab_index, |
| 123 |
'isVertical' => $is_vertical, |
| 124 |
) |
| 125 |
) |
| 126 |
); |
| 127 |
$tag_processor->set_attribute( 'data-wp-init', 'callbacks.onTabsInit' ); |
| 128 |
$tag_processor->set_attribute( 'data-wp-on--keydown', 'actions.handleTabKeyDown' ); |
| 129 |
|
| 130 |
$output = $tag_processor->get_updated_html(); |
| 131 |
|
| 132 |
/** |
| 133 |
* Builds a client side state for just this tabs instance. |
| 134 |
* This allows 3rd party extensibility of tabs while retaining |
| 135 |
* client side state management per core/tabs instance, like context. |
| 136 |
*/ |
| 137 |
wp_interactivity_state( |
| 138 |
'core/tabs/private', |
| 139 |
array( |
| 140 |
$tabs_id => $tabs_list, |
| 141 |
) |
| 142 |
); |
| 143 |
|
| 144 |
return $output; |
| 145 |
} |
| 146 |
|
| 147 |
/** |
| 148 |
* Registers the `core/tabs` block on the server. |
| 149 |
* |
| 150 |
* @since 7.0.0 |
| 151 |
*/ |
| 152 |
function gutenberg_register_block_core_tabs() { |
| 153 |
register_block_type_from_metadata( |
| 154 |
__DIR__ . '/tabs', |
| 155 |
array( |
| 156 |
'render_callback' => 'gutenberg_block_core_tabs_render_block_callback', |
| 157 |
) |
| 158 |
); |
| 159 |
} |
| 160 |
add_action( 'init', 'gutenberg_register_block_core_tabs', 20 ); |
| 161 |
|