[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/blocks/ -> tab-panel.php (source)

   1  <?php
   2  /**
   3   * Tab Panel Block
   4   *
   5   * @package WordPress
   6   */
   7  
   8  /**
   9   * Render callback for core/tab-panel.
  10   *
  11   * @since 7.1.0
  12   *
  13   * @param array     $attributes Block attributes.
  14   * @param string    $content    Block content.
  15   * @param \WP_Block $block      Block instance.
  16   *
  17   * @return string Updated HTML.
  18   */
  19  function block_core_tab_panel_render( array $attributes, string $content, \WP_Block $block ): string {
  20      $tabs_id = $block->context['core/tabs-id'] ?? '';
  21  
  22      static $tab_counters = array();
  23  
  24      if ( ! isset( $tab_counters[ $tabs_id ] ) ) {
  25          $tab_counters[ $tabs_id ] = 0;
  26      }
  27  
  28      $tab_index = $tab_counters[ $tabs_id ];
  29      ++$tab_counters[ $tabs_id ];
  30  
  31      $tag_processor = new WP_HTML_Tag_Processor( $content );
  32      $tag_processor->next_tag( array( 'class_name' => 'wp-block-tab-panel' ) );
  33  
  34      // Use the user's custom anchor if present, otherwise fall back to
  35      // the generated position-based ID.
  36      $tab_id = (string) $tag_processor->get_attribute( 'id' );
  37      if ( empty( $tab_id ) ) {
  38          $tab_id = ! empty( $tabs_id )
  39              ? $tabs_id . '-tab-' . $tab_index
  40              : 'tab-' . $tab_index;
  41          $tag_processor->set_attribute( 'id', $tab_id );
  42      }
  43  
  44      $tag_processor->set_attribute( 'aria-labelledby', 'tab__' . $tab_id );
  45      $tag_processor->set_attribute( 'data-wp-bind--hidden', '!state.isActiveTab' );
  46  
  47      return (string) $tag_processor->get_updated_html();
  48  }
  49  
  50  /**
  51   * Registers the `core/tab-panel` block on the server.
  52   *
  53   * @hook init
  54   *
  55   * @since 7.1.0
  56   */
  57  function register_block_core_tab_panel() {
  58      register_block_type_from_metadata(
  59          __DIR__ . '/tab-panel',
  60          array(
  61              'render_callback' => 'block_core_tab_panel_render',
  62          )
  63      );
  64  }
  65  add_action( 'init', 'register_block_core_tab_panel' );


Generated : Wed Jul 15 08:20:16 2026 Cross-referenced by PHPXref