[ Index ]

PHP Cross Reference of WordPress Trunk (Updated Daily)

Search

title

Body

[close]

/wp-includes/blocks/ -> accordion-item.php (source)

   1  <?php
   2  
   3  /**
   4   * Server-side rendering of the `core/accordion-item` block.
   5   *
   6   * @package WordPress
   7   * @since 6.9.0
   8   *
   9   * @param array $attributes The block attributes.
  10   * @param string $content The block content.
  11   *
  12   * @return string Returns the updated markup.
  13   */
  14  function block_core_accordion_item_render( $attributes, $content ) {
  15      if ( ! $content ) {
  16          return $content;
  17      }
  18  
  19      $p         = new WP_HTML_Tag_Processor( $content );
  20      $unique_id = wp_unique_id( 'accordion-item-' );
  21  
  22      // Initialize the state of the item on the server using a closure,
  23      // since we need to get derived state based on the current context.
  24      wp_interactivity_state(
  25          'core/accordion',
  26          array(
  27              'isOpen' => function () {
  28                  $context = wp_interactivity_get_context();
  29                  return $context['openByDefault'];
  30              },
  31          )
  32      );
  33  
  34      if ( $p->next_tag( array( 'class_name' => 'wp-block-accordion-item' ) ) ) {
  35          $open_by_default = $attributes['openByDefault'] ? 'true' : 'false';
  36          $p->set_attribute( 'data-wp-context', '{ "id": "' . $unique_id . '", "openByDefault": ' . $open_by_default . ' }' );
  37          $p->set_attribute( 'data-wp-class--is-open', 'state.isOpen' );
  38          $p->set_attribute( 'data-wp-init', 'callbacks.initAccordionItems' );
  39  
  40          if ( $p->next_tag( array( 'class_name' => 'wp-block-accordion-heading__toggle' ) ) ) {
  41              $p->set_attribute( 'data-wp-on--click', 'actions.toggle' );
  42              $p->set_attribute( 'data-wp-on--keydown', 'actions.handleKeyDown' );
  43              $p->set_attribute( 'id', $unique_id );
  44              $p->set_attribute( 'aria-controls', $unique_id . '-panel' );
  45              $p->set_attribute( 'data-wp-bind--aria-expanded', 'state.isOpen' );
  46  
  47              if ( $p->next_tag( array( 'class_name' => 'wp-block-accordion-panel' ) ) ) {
  48                  $p->set_attribute( 'id', $unique_id . '-panel' );
  49                  $p->set_attribute( 'aria-labelledby', $unique_id );
  50                  $p->set_attribute( 'data-wp-bind--inert', '!state.isOpen' );
  51  
  52                  // Only modify content if all directives have been set.
  53                  $content = $p->get_updated_html();
  54              }
  55          }
  56      }
  57  
  58      return $content;
  59  }
  60  
  61  /**
  62   * Registers the `core/accordion-item` block on server.
  63   *
  64   * @since 6.9.0
  65   */
  66  function register_block_core_accordion_item() {
  67      register_block_type_from_metadata(
  68          __DIR__ . '/accordion-item',
  69          array(
  70              'render_callback' => 'block_core_accordion_item_render',
  71          )
  72      );
  73  }
  74  add_action( 'init', 'register_block_core_accordion_item' );


Generated : Fri Oct 24 08:20:05 2025 Cross-referenced by PHPXref