| @@ -9,30 +9,35 @@ | ||
| 9 | 9 | * Extract tabs list from tab-panel innerblocks. |
| 10 | 10 | * |
| 11 | 11 | * @since 7.0.0 |
| 12 | 12 | * |
| 13 | - * @param array $innerblocks Parsed inner blocks of tabs block. | |
| 14 | - * @param string $tabs_id Unique ID for the tabs instance, used to generate tab IDs. | |
| 13 | + * @param array $innerblocks Parsed inner blocks of tabs block. | |
| 15 | 14 | * |
| 16 | 15 | * @return array List of tabs with id, label, index. |
| 17 | 16 | */ |
| 18 | -function gutenberg_block_core_tabs_generate_tabs_list( array $innerblocks = array(), string $tabs_id = '' ): array { | |
| 17 | +function gutenberg_block_core_tabs_generate_tabs_list( array $innerblocks = array() ): array { | |
| 19 | 18 | $tabs_list = array(); |
| 20 | 19 | |
| 21 | 20 | // Find tab-panel block |
| 22 | 21 | foreach ( $innerblocks as $inner_block ) { |
| 23 | - if ( 'core/tab-panels' === ( $inner_block['blockName'] ?? '' ) ) { | |
| 22 | + if ( 'core/tab-panel' === ( $inner_block['blockName'] ?? '' ) ) { | |
| 24 | 23 | $tab_index = 0; |
| 25 | 24 | foreach ( $inner_block['innerBlocks'] ?? array() as $tab_block ) { |
| 26 | - if ( 'core/tab-panel' === ( $tab_block['blockName'] ?? '' ) ) { | |
| 25 | + if ( 'core/tab' === ( $tab_block['blockName'] ?? '' ) ) { | |
| 27 | 26 | $attrs = $tab_block['attrs'] ?? array(); |
| 28 | 27 | $tab_label = $attrs['label'] ?? ''; |
| 29 | 28 | |
| 30 | - $tab_id = ! empty( $attrs['anchor'] ) | |
| 31 | - ? $attrs['anchor'] | |
| 32 | - : ( ! empty( $tabs_id ) | |
| 33 | - ? $tabs_id . '-tab-' . $tab_index | |
| 34 | - : 'tab-' . $tab_index ); | |
| 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 | + } | |
| 35 | 40 | |
| 36 | 41 | $tabs_list[] = array( |
| 37 | 42 | 'id' => esc_attr( $tab_id ), |
| 38 | 43 | 'label' => $tab_label, |
| @@ -48,9 +53,9 @@ | ||
| 48 | 53 | return $tabs_list; |
| 49 | 54 | } |
| 50 | 55 | |
| 51 | 56 | /** |
| 52 | - * Filter to provide tabs list context to core/tabs and core/tab-list blocks. | |
| 57 | + * Filter to provide tabs list context to core/tabs and core/tabs-menu blocks. | |
| 53 | 58 | * It is more performant to do this here, once, rather than in the tabs render and tabs context filters. |
| 54 | 59 | * In this way core/tabs is both a provider and a consumer of the core/tabs-list context. |
| 55 | 60 | * |
| 56 | 61 | * @since 7.0.0 |
| @@ -61,15 +66,11 @@ | ||
| 61 | 66 | * @return array Modified context. |
| 62 | 67 | */ |
| 63 | 68 | function gutenberg_block_core_tabs_provide_context( array $context, array $parsed_block ): array { |
| 64 | 69 | if ( 'core/tabs' === $parsed_block['blockName'] ) { |
| 65 | - // Generate a unique ID for the tabs instance first, so it can be used | |
| 66 | - // to derive stable tab IDs. Used for 3rd party extensibility to identify | |
| 67 | - // the tabs instance. | |
| 68 | - $tabs_id = $parsed_block['attrs']['anchor'] ?? wp_unique_id( 'tabs_' ); | |
| 69 | - $tabs_list = gutenberg_block_core_tabs_generate_tabs_list( $parsed_block['innerBlocks'] ?? array(), $tabs_id ); | |
| 70 | + $tabs_list = gutenberg_block_core_tabs_generate_tabs_list( $parsed_block['innerBlocks'] ?? array() ); | |
| 70 | 71 | $context['core/tabs-list'] = $tabs_list; |
| 71 | - $context['core/tabs-id'] = $tabs_id; | |
| 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. | |
| 72 | 73 | } |
| 73 | 74 | |
| 74 | 75 | return $context; |
| 75 | 76 | } |
| @@ -102,11 +103,11 @@ | ||
| 102 | 103 | |
| 103 | 104 | $tag_processor->next_tag( array( 'class_name' => 'wp-block-tabs' ) ); |
| 104 | 105 | $tag_processor->set_attribute( 'data-wp-interactive', 'core/tabs/private' ); |
| 105 | 106 | |
| 106 | - // Inspect inside the tab-list to see if its vertical or not. | |
| 107 | + // Inspect inside the tabs-menu to see if its vertical or not. | |
| 107 | 108 | $tag_processor->set_bookmark( 'core/tabs_wrapper' ); |
| 108 | - while ( $tag_processor->next_tag( array( 'class_name' => 'wp-block-tabs-list' ) ) ) { | |
| 109 | + while ( $tag_processor->next_tag( array( 'class_name' => 'wp-block-tabs-menu' ) ) ) { | |
| 109 | 110 | if ( $tag_processor->has_class( 'is-vertical' ) ) { |
| 110 | 111 | $is_vertical = true; |
| 111 | 112 | break; |
| 112 | 113 | } |