| @@ -2,9 +2,9 @@ | ||
| 2 | 2 | /** |
| 3 | 3 | * Plugin Name: SimpleTOC - Table of Contents Block |
| 4 | 4 | * Plugin URI: https://marc.tv/simpletoc-wordpress-inhaltsverzeichnis-plugin-gutenberg/ |
| 5 | 5 | * Description: SEO-friendly Table of Contents Gutenberg block. No JavaScript or CSS by default. |
| 6 | - * Version: 7.3.1 | |
| 6 | + * Version: 7.4.1 | |
| 7 | 7 | * Requires at least: 6.2 |
| 8 | 8 | * Requires PHP: 7.3 |
| 9 | 9 | * Author: Marc Tönsing |
| 10 | 10 | * Author URI: https://toensing.com |
| @@ -20,9 +20,9 @@ | ||
| 20 | 20 | require_once __DIR__ . '/simpletoc-admin-settings.php'; |
| 21 | 21 | require_once __DIR__ . '/simpletoc-class-headline-ids.php'; |
| 22 | 22 | |
| 23 | 23 | const DEFAULT_BOX_COLOR = '#ebebeb'; |
| 24 | -const SIMPLETOC_VERSION = '7.3.1'; | |
| 24 | +const SIMPLETOC_VERSION = '7.4.1'; | |
| 25 | 25 | |
| 26 | 26 | /** |
| 27 | 27 | * Prevents direct execution of the plugin file. |
| 28 | 28 | * If a WordPress function does not exist, it means that the file has not been run by WordPress. |
| @@ -57,9 +57,10 @@ | ||
| 57 | 57 | wp_add_inline_script( |
| 58 | 58 | 'simpletoc-toc-editor-script', |
| 59 | 59 | 'window.simpletocEditorSettings = ' . wp_json_encode( |
| 60 | 60 | array( |
| 61 | - 'settingsUrl' => admin_url( 'options-general.php?page=simpletoc' ), | |
| 61 | + 'settingsUrl' => admin_url( 'options-general.php?page=simpletoc' ), | |
| 62 | + 'scrollSpyEnabled' => simpletoc_scroll_spy_enabled(), | |
| 62 | 63 | ) |
| 63 | 64 | ) . ';', |
| 64 | 65 | 'before' |
| 65 | 66 | ); |
| @@ -77,8 +78,10 @@ | ||
| 77 | 78 | */ |
| 78 | 79 | function add_simpletoc_block_editor_settings( $editor_settings, $editor_context ) { |
| 79 | 80 | $editor_settings['simpletocSettingsUrl'] = admin_url( 'options-general.php?page=simpletoc' ); |
| 80 | 81 | |
| 82 | + $editor_settings['simpletocScrollSpyEnabled'] = simpletoc_scroll_spy_enabled(); | |
| 83 | + | |
| 81 | 84 | return $editor_settings; |
| 82 | 85 | } |
| 83 | 86 | |
| 84 | 87 | add_filter( 'block_editor_settings_all', __NAMESPACE__ . '\add_simpletoc_block_editor_settings', 10, 2 ); |
| @@ -221,9 +224,15 @@ | ||
| 221 | 224 | // Need two separate instances so that IDs aren't double counted. |
| 222 | 225 | $inner_html_id_instance = new SimpleTOC_Headline_Ids(); |
| 223 | 226 | $inner_content_id_instance = new SimpleTOC_Headline_Ids(); |
| 224 | 227 | |
| 228 | + $ignored_blocks = simpletoc_get_excluded_blocks(); | |
| 229 | + | |
| 225 | 230 | foreach ( $blocks as &$block ) { |
| 231 | + if ( isset( $block['blockName'] ) && in_array( $block['blockName'], $ignored_blocks, true ) ) { | |
| 232 | + continue; | |
| 233 | + } | |
| 234 | + | |
| 226 | 235 | if ( isset( $block['blockName'] ) && in_array( $block['blockName'], $supported_blocks, true ) && isset( $block['innerHTML'] ) && isset( $block['innerContent'] ) && isset( $block['innerContent'][0] ) ) { |
| 227 | 236 | $block['innerHTML'] = add_anchor_attribute( $block['innerHTML'], $inner_html_id_instance, $block ); |
| 228 | 237 | $block['innerContent'][0] = add_anchor_attribute( $block['innerContent'][0], $inner_content_id_instance, $block ); |
| 229 | 238 | } elseif ( ! empty( $block['innerBlocks'] ) ) { |
| @@ -251,8 +260,12 @@ | ||
| 251 | 260 | $typography_enabled = ! empty( $attributes['fontSize'] ) || ! empty( $attributes['style']['typography'] ); |
| 252 | 261 | $wrapper_classes = array( 'simpletoc' ); |
| 253 | 262 | $wrapper_style = ''; |
| 254 | 263 | |
| 264 | + if ( simpletoc_scroll_spy_enabled() || ! empty( $attributes['scroll_spy'] ) ) { | |
| 265 | + $wrapper_classes[] = 'has-simpletoc-scroll-spy'; | |
| 266 | + } | |
| 267 | + | |
| 255 | 268 | if ( $typography_enabled ) { |
| 256 | 269 | $wrapper_classes[] = 'has-simpletoc-typography'; |
| 257 | 270 | } |
| 258 | 271 | |
| @@ -363,8 +376,22 @@ | ||
| 363 | 376 | return $headings; |
| 364 | 377 | } |
| 365 | 378 | |
| 366 | 379 | /** |
| 380 | + * Gets blocks excluded from both anchor generation and TOC collection. | |
| 381 | + * | |
| 382 | + * Query templates render repeatedly in a different post context. | |
| 383 | + * | |
| 384 | + * @return string[] Excluded block names. | |
| 385 | + */ | |
| 386 | +function simpletoc_get_excluded_blocks() { | |
| 387 | + return apply_filters( | |
| 388 | + 'simpletoc_excluded_blocks', | |
| 389 | + array( 'core/query', 'generateblocks/query-loop', 'generateblocks/query' ) | |
| 390 | + ); | |
| 391 | +} | |
| 392 | + | |
| 393 | +/** | |
| 367 | 394 | * Return all headings with a recursive walk through all blocks. |
| 368 | 395 | * This includes groups and reusable block with groups within reusable blocks. |
| 369 | 396 | * |
| 370 | 397 | * @param array[] $blocks The blocks to filter headings from. |
| @@ -377,9 +404,9 @@ | ||
| 377 | 404 | return $arr; |
| 378 | 405 | } |
| 379 | 406 | |
| 380 | 407 | // allow developers to ignore specific blocks. |
| 381 | - $ignored_blocks = apply_filters( 'simpletoc_excluded_blocks', array() ); | |
| 408 | + $ignored_blocks = simpletoc_get_excluded_blocks(); | |
| 382 | 409 | |
| 383 | 410 | foreach ( $blocks as $inner_block ) { |
| 384 | 411 | if ( is_array( $inner_block ) ) { |
| 385 | 412 | // if block is ignored, skip. |