| @@ -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.1.1 | |
| 6 | + * Version: 7.4.0 | |
| 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.1.1'; | |
| 24 | +const SIMPLETOC_VERSION = '7.4.0'; | |
| 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 ); |
| @@ -244,21 +247,30 @@ | ||
| 244 | 247 | function render_callback_simpletoc( $attributes ) { |
| 245 | 248 | $is_backend = defined( 'REST_REQUEST' ) && REST_REQUEST && 'edit' === filter_input( INPUT_GET, 'context' ); |
| 246 | 249 | $title_text = $attributes['title_text'] ? esc_html( trim( $attributes['title_text'] ) ) : __( 'Table of Contents', 'simpletoc' ); |
| 247 | 250 | $alignclass = ! empty( $attributes['align'] ) ? 'align' . $attributes['align'] : ''; |
| 248 | - $class_name = ! empty( $attributes['className'] ) ? wp_strip_all_tags( $attributes['className'] ) : ''; | |
| 249 | 251 | $title_level = $attributes['title_level']; |
| 250 | 252 | $global_box_style_enabled = apply_filters( 'simpletoc_box_style_enabled', false ) || true === (bool) get_option( 'simpletoc_box_style_enabled', false ); |
| 251 | - $box_style_enabled = $global_box_style_enabled || ! empty( $attributes['box_style'] ); | |
| 253 | + $legacy_box_style_enabled = ! empty( $attributes['box_style'] ); | |
| 254 | + $typography_enabled = ! empty( $attributes['fontSize'] ) || ! empty( $attributes['style']['typography'] ); | |
| 252 | 255 | $wrapper_classes = array( 'simpletoc' ); |
| 253 | 256 | $wrapper_style = ''; |
| 254 | 257 | |
| 255 | - if ( $box_style_enabled ) { | |
| 258 | + if ( simpletoc_scroll_spy_enabled() || ! empty( $attributes['scroll_spy'] ) ) { | |
| 259 | + $wrapper_classes[] = 'has-simpletoc-scroll-spy'; | |
| 260 | + } | |
| 261 | + | |
| 262 | + if ( $typography_enabled ) { | |
| 263 | + $wrapper_classes[] = 'has-simpletoc-typography'; | |
| 264 | + } | |
| 265 | + | |
| 266 | + if ( $global_box_style_enabled || $legacy_box_style_enabled ) { | |
| 267 | + $wrapper_classes[] = 'is-style-boxed'; | |
| 256 | 268 | $wrapper_classes[] = 'has-simpletoc-box-style'; |
| 257 | 269 | |
| 258 | 270 | if ( $global_box_style_enabled ) { |
| 259 | 271 | $wrapper_classes[] = 'has-background'; |
| 260 | - $wrapper_style = safecss_filter_attr( 'background-color:' . DEFAULT_BOX_COLOR . ';' ); | |
| 272 | + $wrapper_style = safecss_filter_attr( 'background-color:' . DEFAULT_BOX_COLOR . ';' ); | |
| 261 | 273 | } elseif ( ! empty( $attributes['box_color'] ) ) { |
| 262 | 274 | $wrapper_classes[] = 'has-background'; |
| 263 | 275 | $wrapper_style = safecss_filter_attr( 'background-color:' . $attributes['box_color'] . ';' ); |
| 264 | 276 | } else { |
| @@ -266,9 +278,8 @@ | ||
| 266 | 278 | $wrapper_style = safecss_filter_attr( 'background-color:' . DEFAULT_BOX_COLOR . ';' ); |
| 267 | 279 | } |
| 268 | 280 | } |
| 269 | 281 | |
| 270 | - $wrapper_enabled = apply_filters( 'simpletoc_wrapper_enabled', false ) || true === (bool) get_option( 'simpletoc_wrapper_enabled', false ) || true === (bool) get_option( 'simpletoc_accordion_enabled', false ); | |
| 271 | 282 | $wrapper_attrs = get_block_wrapper_attributes( |
| 272 | 283 | array( |
| 273 | 284 | 'class' => implode( ' ', $wrapper_classes ), |
| 274 | 285 | 'style' => $wrapper_style, |
| @@ -273,11 +284,10 @@ | ||
| 273 | 284 | 'class' => implode( ' ', $wrapper_classes ), |
| 274 | 285 | 'style' => $wrapper_style, |
| 275 | 286 | ) |
| 276 | 287 | ); |
| 277 | - $has_wrapper = ! empty( $class_name ) || $wrapper_enabled || $attributes['accordion'] || $attributes['wrapper'] || $box_style_enabled; | |
| 278 | - $pre_html = $has_wrapper ? '<div role="navigation" aria-label="' . esc_attr__( 'Table of Contents', 'simpletoc' ) . '" ' . $wrapper_attrs . '>' : ''; | |
| 279 | - $post_html = $has_wrapper ? '</div>' : ''; | |
| 288 | + $pre_html = '<div role="navigation" aria-label="' . esc_attr__( 'Table of Contents', 'simpletoc' ) . '" ' . $wrapper_attrs . '>'; | |
| 289 | + $post_html = '</div>'; | |
| 280 | 290 | |
| 281 | 291 | $post = get_post(); |
| 282 | 292 | $blocks = ! is_null( $post ) && ! is_null( $post->post_content ) ? parse_blocks( $post->post_content ) : ''; |
| 283 | 293 | |
| @@ -286,17 +296,17 @@ | ||
| 286 | 296 | $headings_clean = array_map( 'trim', $headings ); |
| 287 | 297 | $toc_html = generate_toc( $headings_clean, $attributes ); |
| 288 | 298 | |
| 289 | 299 | if ( empty( $blocks ) ) { |
| 290 | - return get_empty_blocks_message( $is_backend, $attributes, $title_level, $alignclass, $title_text, __( 'No blocks found.', 'simpletoc' ), __( 'Save or update post first.', 'simpletoc' ), $wrapper_attrs, $has_wrapper ); | |
| 300 | + return get_empty_blocks_message( $is_backend, $attributes, $title_level, $alignclass, $title_text, __( 'No blocks found.', 'simpletoc' ), __( 'Save or update post first.', 'simpletoc' ), $wrapper_attrs ); | |
| 291 | 301 | } |
| 292 | 302 | |
| 293 | 303 | if ( empty( $headings_clean ) ) { |
| 294 | - return get_empty_blocks_message( $is_backend, $attributes, $title_level, $alignclass, $title_text, __( 'No headings found.', 'simpletoc' ), __( 'Save or update post first.', 'simpletoc' ), $wrapper_attrs, $has_wrapper ); | |
| 304 | + return get_empty_blocks_message( $is_backend, $attributes, $title_level, $alignclass, $title_text, __( 'No headings found.', 'simpletoc' ), __( 'Save or update post first.', 'simpletoc' ), $wrapper_attrs ); | |
| 295 | 305 | } |
| 296 | 306 | |
| 297 | 307 | if ( empty( $toc_html ) ) { |
| 298 | - return get_empty_blocks_message( $is_backend, $attributes, $title_level, $alignclass, $title_text, __( 'No headings found.', 'simpletoc' ), __( 'Check minimal and maximum level block settings.', 'simpletoc' ), $wrapper_attrs, $has_wrapper ); | |
| 308 | + return get_empty_blocks_message( $is_backend, $attributes, $title_level, $alignclass, $title_text, __( 'No headings found.', 'simpletoc' ), __( 'Check minimal and maximum level block settings.', 'simpletoc' ), $wrapper_attrs ); | |
| 299 | 309 | } |
| 300 | 310 | |
| 301 | 311 | return $pre_html . $toc_html . $post_html; |
| 302 | 312 | } |
| @@ -310,27 +320,20 @@ | ||
| 310 | 320 | * @param string $alignclass The CSS class for alignment of the Table of Contents block. |
| 311 | 321 | * @param string $title_text The text for the Table of Contents title. |
| 312 | 322 | * @param string $warning_text1 The first part of the warning message to be displayed. |
| 313 | 323 | * @param string $warning_text2 The second part of the warning message to be displayed. |
| 314 | - * @param string $wrapper_attrs Wrapper attributes for the optional block wrapper. | |
| 315 | - * @param bool $has_wrapper Indicates if the wrapper should be rendered. | |
| 324 | + * @param string $wrapper_attrs Block wrapper attributes. | |
| 316 | 325 | * |
| 317 | 326 | * @return string The HTML output for the empty blocks message. |
| 318 | 327 | */ |
| 319 | -function get_empty_blocks_message( $is_backend, $attributes, $title_level, $alignclass, $title_text, $warning_text1, $warning_text2, $wrapper_attrs = '', $has_wrapper = false ) { | |
| 328 | +function get_empty_blocks_message( $is_backend, $attributes, $title_level, $alignclass, $title_text, $warning_text1, $warning_text2, $wrapper_attrs = '' ) { | |
| 320 | 329 | $html = ''; |
| 321 | 330 | |
| 322 | 331 | if ( $is_backend ) { |
| 323 | - if ( $has_wrapper ) { | |
| 324 | - $html .= '<div role="navigation" aria-label="' . esc_attr__( 'Table of Contents', 'simpletoc' ) . '" ' . $wrapper_attrs . '>'; | |
| 325 | - } | |
| 326 | - | |
| 332 | + $html .= '<div role="navigation" aria-label="' . esc_attr__( 'Table of Contents', 'simpletoc' ) . '" ' . $wrapper_attrs . '>'; | |
| 327 | 333 | $html .= sprintf( '<h%d class="%s">%s</h%d>', $title_level, esc_attr( trim( 'simpletoc-title ' . $alignclass ) ), $title_text, $title_level ); |
| 328 | 334 | $html .= sprintf( '<p class="components-notice is-warning %s">%s %s</p>', esc_attr( $alignclass ), esc_html( $warning_text1 ), esc_html( $warning_text2 ) ); |
| 329 | - | |
| 330 | - if ( $has_wrapper ) { | |
| 331 | - $html .= '</div>'; | |
| 332 | - } | |
| 335 | + $html .= '</div>'; | |
| 333 | 336 | } |
| 334 | 337 | |
| 335 | 338 | return $html; |
| 336 | 339 | } |
| @@ -479,12 +482,11 @@ | ||
| 479 | 482 | $html_wo_nbs = str_replace( ' ', ' ', $zero_punctuation ); |
| 480 | 483 | // remove umlauts and accents. |
| 481 | 484 | $string_without_accents = remove_accents( $html_wo_nbs ); |
| 482 | 485 | // Sanitizes a title, replacing whitespace and a few other characters with dashes. |
| 483 | - $sanitized_string = sanitize_title_with_dashes( $string_without_accents ); | |
| 484 | - // Encode for use in an url. | |
| 485 | - $urlencoded = rawurlencode( $sanitized_string ); | |
| 486 | - return $urlencoded; | |
| 486 | + // Already returns a URL-safe, percent-encoded slug for non-ASCII input, so no | |
| 487 | + // further rawurlencode() is needed (that would double-encode the string). | |
| 488 | + return sanitize_title_with_dashes( $string_without_accents ); | |
| 487 | 489 | } |
| 488 | 490 | |
| 489 | 491 | /** |
| 490 | 492 | * Add additional plugin meta links to the SimpleTOC plugin page. |