PluginProbe
Gutenberg / 23.2.2
Gutenberg v23.2.2
23.9.1 23.9.0 23.8.0 23.7.2 23.7.1 23.7.0 23.6.1 23.6.2 23.6.0 23.5.3 23.5.2 23.5.1 23.5.0 23.4.0 23.3.2 23.3.1 23.3.0 23.2.0 23.2.1 23.2.2 23.1.1 23.1.0 23.0.1 12.6.0 7.4.0 All 402 releases
gutenberg / build / scripts / block-library / accordion-item.php

accordion-item.php in Gutenberg 23.2.2, at build/scripts/block-library/accordion-item.php

87 lines 2.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
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 gutenberg_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 gutenberg_register_block_core_accordion_item() {
79 register_block_type_from_metadata(
80 __DIR__ . '/accordion-item',
81 array(
82 'render_callback' => 'gutenberg_block_core_accordion_item_render',
83 )
84 );
85 }
86 add_action( 'init', 'gutenberg_register_block_core_accordion_item', 20 );
87