| @@ -13,9 +13,9 @@ | ||
| 13 | 13 | /** |
| 14 | 14 | * Adds an aria-label to the table of contents block content. |
| 15 | 15 | * |
| 16 | 16 | * @param array $attributes Attributes of the block being rendered. |
| 17 | - * @param string $content Content of the block being rendered. | |
| 17 | + * @param string $content Content of the block being rendered. | |
| 18 | 18 | * |
| 19 | 19 | * @return string The content of the block being rendered. |
| 20 | 20 | */ |
| 21 | 21 | function gutenberg_block_core_table_of_contents_add_aria_label( $attributes, $content ) { |
| @@ -362,16 +362,29 @@ | ||
| 362 | 362 | |
| 363 | 363 | /** |
| 364 | 364 | * Renders the table of contents block from current post headings. |
| 365 | 365 | * |
| 366 | - * @param array $attributes Attributes of the block being rendered. | |
| 367 | - * @param string $content Content of the block being rendered. | |
| 366 | + * @param array $attributes Attributes of the block being rendered. | |
| 367 | + * @param string $content Content of the block being rendered. | |
| 368 | + * @param WP_Block|null $block Block instance. | |
| 368 | 369 | * |
| 369 | 370 | * @return string The content of the block being rendered. |
| 370 | 371 | */ |
| 371 | -function gutenberg_block_core_table_of_contents_render( $attributes, $content ) { | |
| 372 | +function gutenberg_block_core_table_of_contents_render( $attributes, $content, $block = null ) { | |
| 372 | 373 | global $wp_current_filter; |
| 373 | 374 | |
| 375 | + // Preserve legacy saved markup for old posts that have not been edited and migrated yet. | |
| 376 | + $legacy_content = $content; | |
| 377 | + if ( '' === trim( $legacy_content ) && ! empty( $block->parsed_block['innerHTML'] ) ) { | |
| 378 | + $legacy_content = $block->parsed_block['innerHTML']; | |
| 379 | + } | |
| 380 | + | |
| 381 | + // Once a post is edited and saved, the block migrates to dynamic rendering. | |
| 382 | + if ( '' !== trim( $legacy_content ) ) { | |
| 383 | + return gutenberg_block_core_table_of_contents_add_aria_label( $attributes, $legacy_content ); | |
| 384 | + } | |
| 385 | + | |
| 386 | + // Outside post content rendering, there is no reliable current post to scan. | |
| 374 | 387 | if ( ! is_array( $wp_current_filter ) || ! in_array( 'the_content', $wp_current_filter, true ) ) { |
| 375 | 388 | return gutenberg_block_core_table_of_contents_add_aria_label( $attributes, $content ); |
| 376 | 389 | } |
| 377 | 390 | |