[ 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{ openByDefault: bool } $attributes The block attributes.
  10   * @param string                        $content   The block content.
  11   * @return string Returns the updated markup.
  12   */
  13  function block_core_accordion_item_render( array $attributes, string $content ): string {
  14      if ( '' === $content ) {
  15          return $content;
  16      }
  17  
  18      $p         = new WP_HTML_Tag_Processor( $content );
  19      $unique_id = wp_unique_id( 'accordion-item-' );
  20  
  21      // Initialize the state of the item on the server using a closure,
  22      // since we need to get derived state based on the current context.
  23      wp_interactivity_state(
  24          'core/accordion',
  25          array(
  26              'isOpen' => function () {
  27                  $context = wp_interactivity_get_context();
  28                  return $context['openByDefault'];
  29              },
  30          )
  31      );
  32  
  33      if ( $p->next_tag( array( 'class_name' => 'wp-block-accordion-item' ) ) ) {
  34          $open_by_default = $attributes['openByDefault'] ? 'true' : 'false';
  35          $p->set_attribute( 'data-wp-context', '{ "id": "' . $unique_id . '", "openByDefault": ' . $open_by_default . ' }' );
  36          $p->set_attribute( 'data-wp-class--is-open', 'state.isOpen' );
  37          $p->set_attribute( 'data-wp-init', 'callbacks.initAccordionItems' );
  38          $p->set_attribute( 'data-wp-on-window--hashchange', 'callbacks.hashChange' );
  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( 'id', $unique_id );
  43              $p->set_attribute( 'aria-controls', $unique_id . '-panel' );
  44              $p->set_attribute( 'data-wp-bind--aria-expanded', 'state.isOpen' );
  45  
  46              if ( $p->next_tag( array( 'class_name' => 'wp-block-accordion-panel' ) ) ) {
  47                  $p->set_attribute( 'id', $unique_id . '-panel' );
  48                  $p->set_attribute( 'aria-labelledby', $unique_id );
  49                  $p->set_attribute( 'data-wp-bind--hidden', 'state.isHidden' );
  50                  $p->set_attribute( 'data-wp-on--beforematch', 'actions.handleBeforeMatch' );
  51  
  52                  // Only modify content if all directives have been set.
  53                  $content = $p->get_updated_html();
  54              }
  55          }
  56      }
  57  
  58      /*
  59       * If an Accordion Item is collapsed by default, ensure any contained IMG has fetchpriority=low to deprioritize it
  60       * from contending with resources in the critical rendering path. In contrast, remove the loading attribute to
  61       * prevent the image from not being available when the item is expanded.
  62       */
  63      if ( ! $attributes['openByDefault'] ) {
  64          $processor = new WP_HTML_Tag_Processor( $content );
  65          while ( $processor->next_tag( 'IMG' ) ) {
  66              $processor->set_attribute( 'fetchpriority', 'low' );
  67          }
  68          $content = $processor->get_updated_html();
  69      }
  70  
  71      return $content;
  72  }
  73  
  74  /**
  75   * Registers the `core/accordion-item` block on server.
  76   *
  77   * @since 6.9.0
  78   */
  79  function register_block_core_accordion_item() {
  80      register_block_type_from_metadata(
  81          __DIR__ . '/accordion-item',
  82          array(
  83              'render_callback' => 'block_core_accordion_item_render',
  84          )
  85      );
  86  }
  87  add_action( 'init', 'register_block_core_accordion_item' );


Generated : Mon Jul 27 08:20:18 2026 Cross-referenced by PHPXref