| 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 |
|