PluginProbe
Gutenberg / 22.9.0
Gutenberg v22.9.0
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 / details.php

details.php in Gutenberg 22.9.0, at build/scripts/block-library/details.php

48 lines 1.5 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2 /**
3 * Server-side rendering of the `core/details` block.
4 *
5 * @package WordPress
6 */
7
8 /**
9 * Sets fetchpriority="low" on all IMG tags within the collapsed Details block.
10 *
11 * Images in a collapsed Details block are hidden until the block is expanded, so they should
12 * not compete with any resources in the critical rendering path, such as the LCP element image.
13 *
14 * @since 7.0.0
15 *
16 * @param string $block_content The block content.
17 * @param array $block The full block, including name and attributes.
18 * @return string Modified HTML with fetchpriority="low" on all IMG tags when the showContent attribute is false.
19 */
20 function gutenberg_block_core_details_set_img_fetchpriority_low( $block_content, array $block ): string {
21 if ( ! is_string( $block_content ) ) {
22 return '';
23 }
24
25 // If the Details block is open by default, short-circuit to let core add fetchpriority=high if appropriate.
26 if ( $block['attrs']['showContent'] ?? false ) {
27 return $block_content;
28 }
29
30 $tags = new WP_HTML_Tag_Processor( $block_content );
31 while ( $tags->next_tag( 'IMG' ) ) {
32 $tags->set_attribute( 'fetchpriority', 'low' );
33 }
34 return $tags->get_updated_html();
35 }
36
37 add_filter( 'render_block_core/details', 'gutenberg_block_core_details_set_img_fetchpriority_low', 10, 2 );
38
39 /**
40 * Registers the `core/details` block on server.
41 *
42 * @since 7.0.0
43 */
44 function gutenberg_register_block_core_details() {
45 register_block_type_from_metadata( __DIR__ . '/details' );
46 }
47 add_action( 'init', 'gutenberg_register_block_core_details', 20 );
48