| 1 |
<?php |
| 2 |
/** |
| 3 |
* Tab List Block |
| 4 |
* |
| 5 |
* @package WordPress |
| 6 |
*/ |
| 7 |
|
| 8 |
/** |
| 9 |
* Render callback for core/tab-list. |
| 10 |
* |
| 11 |
* Injects IAPI directives into the saved button HTML. The buttons already |
| 12 |
* carry color/border/padding styles from save.js; this callback adds |
| 13 |
* tab-specific attributes (id, aria-controls, context) and interactivity |
| 14 |
* directives using data from the tabs-list context. |
| 15 |
* |
| 16 |
* @since 7.1.0 |
| 17 |
* |
| 18 |
* @param array $attributes Block attributes. |
| 19 |
* @param string $content Block content (rendered buttons from save.js). |
| 20 |
* @param \WP_Block $block WP_Block instance. |
| 21 |
* |
| 22 |
* @return string Updated HTML. |
| 23 |
*/ |
| 24 |
function gutenberg_block_core_tab_list_render_callback( array $attributes, string $content, \WP_Block $block ): string { |
| 25 |
$tabs_list = $block->context['core/tabs-list'] ?? array(); |
| 26 |
|
| 27 |
if ( empty( $tabs_list ) ) { |
| 28 |
return $content; |
| 29 |
} |
| 30 |
|
| 31 |
$tag_processor = new WP_HTML_Tag_Processor( $content ); |
| 32 |
$tab_index = 0; |
| 33 |
|
| 34 |
while ( $tag_processor->next_tag( 'button' ) ) { |
| 35 |
$tab_id = $tabs_list[ $tab_index ] ?? null; |
| 36 |
|
| 37 |
if ( null === $tab_id ) { |
| 38 |
break; |
| 39 |
} |
| 40 |
|
| 41 |
$tag_processor->set_attribute( 'id', 'tab__' . $tab_id ); |
| 42 |
$tag_processor->set_attribute( 'aria-controls', $tab_id ); |
| 43 |
$tag_processor->set_attribute( 'data-wp-on--click', 'actions.handleTabClick' ); |
| 44 |
$tag_processor->set_attribute( 'data-wp-on--keydown', 'actions.handleTabKeyDown' ); |
| 45 |
$tag_processor->set_attribute( 'data-wp-bind--aria-selected', 'state.isActiveTab' ); |
| 46 |
$tag_processor->set_attribute( 'data-wp-bind--tabindex', 'state.tabIndexAttribute' ); |
| 47 |
$tag_processor->set_attribute( |
| 48 |
'data-wp-context', |
| 49 |
wp_json_encode( array( 'tabIndex' => $tab_index ) ) |
| 50 |
); |
| 51 |
|
| 52 |
++$tab_index; |
| 53 |
} |
| 54 |
|
| 55 |
return $tag_processor->get_updated_html(); |
| 56 |
} |
| 57 |
|
| 58 |
/** |
| 59 |
* Registers the `core/tab-list` block on the server. |
| 60 |
* |
| 61 |
* @since 7.1.0 |
| 62 |
*/ |
| 63 |
function gutenberg_register_block_core_tab_list() { |
| 64 |
register_block_type_from_metadata( |
| 65 |
__DIR__ . '/tab-list', |
| 66 |
array( |
| 67 |
'render_callback' => 'gutenberg_block_core_tab_list_render_callback', |
| 68 |
) |
| 69 |
); |
| 70 |
} |
| 71 |
add_action( 'init', 'gutenberg_register_block_core_tab_list', 20 ); |
| 72 |
|