[ 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          $p->set_attribute( 'data-wp-on-window--hashchange', 'callbacks.hashChange' );
  40  
  41          if ( $p->next_tag( array( 'class_name' => 'wp-block-accordion-heading__toggle' ) ) ) {
  42              $p->set_attribute( 'data-wp-on--click', 'actions.toggle' );
  43              $p->set_attribute( 'data-wp-on--keydown', 'actions.handleKeyDown' );
  44              $p->set_attribute( 'id', $unique_id );
  45              $p->set_attribute( 'aria-controls', $unique_id . '-panel' );
  46              $p->set_attribute( 'data-wp-bind--aria-expanded', 'state.isOpen' );
  47  
  48              if ( $p->next_tag( array( 'class_name' => 'wp-block-accordion-panel' ) ) ) {
  49                  $p->set_attribute( 'id', $unique_id . '-panel' );
  50                  $p->set_attribute( 'aria-labelledby', $unique_id );
  51                  $p->set_attribute( 'data-wp-bind--inert', '!state.isOpen' );
  52  
  53                  // Only modify content if all directives have been set.
  54                  $content = $p->get_updated_html();
  55              }
  56          }
  57      }
  58  
  59      return $content;
  60  }
  61  
  62  /**
  63   * Registers the `core/accordion-item` block on server.
  64   *
  65   * @since 6.9.0
  66   */
  67  function register_block_core_accordion_item() {
  68      register_block_type_from_metadata(
  69          __DIR__ . '/accordion-item',
  70          array(
  71              'render_callback' => 'block_core_accordion_item_render',
  72          )
  73      );
  74  }
  75  add_action( 'init', 'register_block_core_accordion_item' );


Generated : Wed May 6 08:20:15 2026 Cross-referenced by PHPXref