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 / table-of-contents.php

table-of-contents.php in Gutenberg 23.2.2, at build/scripts/block-library/table-of-contents.php

45 lines 1.2 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/table-of-contents` block.
4 *
5 * @package WordPress
6 */
7
8 /**
9 * Adds an aria-label to the table of contents block content.
10 *
11 * @param array $attributes Attributes of the block being rendered.
12 * @param string $content Content of the block being rendered.
13 *
14 * @return string The content of the block being rendered.
15 */
16 function gutenberg_block_core_table_of_contents_render( $attributes, $content ) {
17 if ( ! $content ) {
18 return $content;
19 }
20
21 // Get the aria-label from block attributes, or fallback to localized default.
22 $aria_label = empty( $attributes['ariaLabel'] ) ? __( 'Table of Contents' ) : wp_strip_all_tags( $attributes['ariaLabel'] );
23
24 $p = new WP_HTML_Tag_Processor( $content );
25
26 if ( $p->next_tag( 'nav' ) ) {
27 $p->set_attribute( 'aria-label', $aria_label );
28 }
29
30 return $p->get_updated_html();
31 }
32
33 /**
34 * Registers the `core/table-of-contents` block on the server.
35 */
36 function gutenberg_register_block_core_table_of_contents() {
37 register_block_type_from_metadata(
38 __DIR__ . '/table-of-contents',
39 array(
40 'render_callback' => 'gutenberg_block_core_table_of_contents_render',
41 )
42 );
43 }
44 add_action( 'init', 'gutenberg_register_block_core_table_of_contents', 20 );
45