| @@ -7,33 +7,43 @@ | ||
| 7 | 7 | |
| 8 | 8 | /** |
| 9 | 9 | * Extract tabs list from tab-panel innerblocks. |
| 10 | 10 | * |
| 11 | - * @since 7.1.0 | |
| 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 | - * @return array List of tab IDs. | |
| 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'] ?? '' ) ) { | |
| 27 | - $attrs = $tab_block['attrs'] ?? array(); | |
| 25 | + if ( 'core/tab' === ( $tab_block['blockName'] ?? '' ) ) { | |
| 26 | + $attrs = $tab_block['attrs'] ?? array(); | |
| 27 | + $tab_label = $attrs['label'] ?? ''; | |
| 28 | 28 | |
| 29 | - $tab_id = ! empty( $attrs['anchor'] ) | |
| 30 | - ? $attrs['anchor'] | |
| 31 | - : ( ! empty( $tabs_id ) | |
| 32 | - ? $tabs_id . '-tab-' . $tab_index | |
| 33 | - : '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 | + } | |
| 34 | 40 | |
| 35 | - $tabs_list[] = esc_attr( $tab_id ); | |
| 41 | + $tabs_list[] = array( | |
| 42 | + 'id' => esc_attr( $tab_id ), | |
| 43 | + 'label' => $tab_label, | |
| 44 | + 'index' => $tab_index, | |
| 45 | + ); | |
| 36 | 46 | ++$tab_index; |
| 37 | 47 | } |
| 38 | 48 | } |
| 39 | 49 | break; |
| @@ -43,13 +53,13 @@ | ||
| 43 | 53 | return $tabs_list; |
| 44 | 54 | } |
| 45 | 55 | |
| 46 | 56 | /** |
| 47 | - * 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. | |
| 48 | 58 | * It is more performant to do this here, once, rather than in the tabs render and tabs context filters. |
| 49 | 59 | * In this way core/tabs is both a provider and a consumer of the core/tabs-list context. |
| 50 | 60 | * |
| 51 | - * @since 7.1.0 | |
| 61 | + * @since 7.0.0 | |
| 52 | 62 | * |
| 53 | 63 | * @param array $context Default block context. |
| 54 | 64 | * @param array $parsed_block The block being rendered. |
| 55 | 65 | * |
| @@ -56,14 +66,11 @@ | ||
| 56 | 66 | * @return array Modified context. |
| 57 | 67 | */ |
| 58 | 68 | function gutenberg_block_core_tabs_provide_context( array $context, array $parsed_block ): array { |
| 59 | 69 | if ( 'core/tabs' === $parsed_block['blockName'] ) { |
| 60 | - // Generate a unique ID for the tabs instance first, so it can be used | |
| 61 | - // to derive stable tab IDs. | |
| 62 | - $tabs_id = $parsed_block['attrs']['anchor'] ?? wp_unique_id( 'tabs_' ); | |
| 63 | - $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() ); | |
| 64 | 71 | $context['core/tabs-list'] = $tabs_list; |
| 65 | - $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. | |
| 66 | 73 | } |
| 67 | 74 | |
| 68 | 75 | return $context; |
| 69 | 76 | } |
| @@ -71,9 +78,9 @@ | ||
| 71 | 78 | |
| 72 | 79 | /** |
| 73 | 80 | * Render callback for core/tabs. |
| 74 | 81 | * |
| 75 | - * @since 7.1.0 | |
| 82 | + * @since 7.0.0 | |
| 76 | 83 | * |
| 77 | 84 | * @param array $attributes Block attributes. |
| 78 | 85 | * @param string $content Block content. |
| 79 | 86 | * @param \WP_Block $block WP_Block instance. |
| @@ -89,13 +96,25 @@ | ||
| 89 | 96 | // If malformed tabs, return early to avoid errors. |
| 90 | 97 | return ''; |
| 91 | 98 | } |
| 92 | 99 | |
| 100 | + $is_vertical = false; | |
| 101 | + | |
| 93 | 102 | $tag_processor = new WP_HTML_Tag_Processor( $content ); |
| 94 | 103 | |
| 95 | 104 | $tag_processor->next_tag( array( 'class_name' => 'wp-block-tabs' ) ); |
| 96 | - $tag_processor->set_attribute( 'data-wp-interactive', 'core/tabs' ); | |
| 105 | + $tag_processor->set_attribute( 'data-wp-interactive', 'core/tabs/private' ); | |
| 97 | 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 | + | |
| 98 | 117 | $tag_processor->set_attribute( |
| 99 | 118 | 'data-wp-context', |
| 100 | 119 | wp_json_encode( |
| 101 | 120 | array( |
| @@ -100,8 +119,9 @@ | ||
| 100 | 119 | wp_json_encode( |
| 101 | 120 | array( |
| 102 | 121 | 'tabsId' => $tabs_id, |
| 103 | 122 | 'activeTabIndex' => $active_tab_index, |
| 123 | + 'isVertical' => $is_vertical, | |
| 104 | 124 | ) |
| 105 | 125 | ) |
| 106 | 126 | ); |
| 107 | 127 | $tag_processor->set_attribute( 'data-wp-init', 'callbacks.onTabsInit' ); |
| @@ -109,13 +129,14 @@ | ||
| 109 | 129 | |
| 110 | 130 | $output = $tag_processor->get_updated_html(); |
| 111 | 131 | |
| 112 | 132 | /** |
| 113 | - * Builds a client side state for just this tabs instance, so each | |
| 114 | - * core/tabs block manages its own state, like context. | |
| 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. | |
| 115 | 136 | */ |
| 116 | 137 | wp_interactivity_state( |
| 117 | - 'core/tabs', | |
| 138 | + 'core/tabs/private', | |
| 118 | 139 | array( |
| 119 | 140 | $tabs_id => $tabs_list, |
| 120 | 141 | ) |
| 121 | 142 | ); |
| @@ -125,9 +146,9 @@ | ||
| 125 | 146 | |
| 126 | 147 | /** |
| 127 | 148 | * Registers the `core/tabs` block on the server. |
| 128 | 149 | * |
| 129 | - * @since 7.1.0 | |
| 150 | + * @since 7.0.0 | |
| 130 | 151 | */ |
| 131 | 152 | function gutenberg_register_block_core_tabs() { |
| 132 | 153 | register_block_type_from_metadata( |
| 133 | 154 | __DIR__ . '/tabs', |