| [ Index ] |
PHP Cross Reference of WordPress Trunk (Updated Daily) |
[Summary view] [Print] [Text view]
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--inert', '!state.isOpen' ); 50 51 // Only modify content if all directives have been set. 52 $content = $p->get_updated_html(); 53 } 54 } 55 } 56 57 /* 58 * If an Accordion Item is collapsed by default, ensure any contained IMG has fetchpriority=low to deprioritize it 59 * from contending with resources in the critical rendering path. In contrast, remove the loading attribute to 60 * prevent the image from not being available when the item is expanded. 61 */ 62 if ( ! $attributes['openByDefault'] ) { 63 $processor = new WP_HTML_Tag_Processor( $content ); 64 while ( $processor->next_tag( 'IMG' ) ) { 65 $processor->set_attribute( 'fetchpriority', 'low' ); 66 } 67 $content = $processor->get_updated_html(); 68 } 69 70 return $content; 71 } 72 73 /** 74 * Registers the `core/accordion-item` block on server. 75 * 76 * @since 6.9.0 77 */ 78 function register_block_core_accordion_item() { 79 register_block_type_from_metadata( 80 __DIR__ . '/accordion-item', 81 array( 82 'render_callback' => 'block_core_accordion_item_render', 83 ) 84 ); 85 } 86 add_action( 'init', 'register_block_core_accordion_item' );
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated : Tue Jul 7 08:20:13 2026 | Cross-referenced by PHPXref |