$end_level ) { $tmp = $start_level; $start_level = $end_level; $end_level = $tmp; } $toc_data = wp_json_encode( array( 'startLevel' => $start_level, 'endLevel' => $end_level, 'contentSelector' => (string) $content_selector, 'excludeSelector' => (string) $exclude_selector, 'minHeadings' => max( 1, $min_headings ), 'collapsible' => 'on' === $collapsible, 'initiallyCollapsed' => 'on' === $initially_collapsed, 'collapsibleSubHeadings' => 'on' === $collapsible_subs, 'subHeadingsCollapsed' => 'on' === $subs_collapsed, 'smoothScroll' => 'on' === $smooth_scroll, 'scrollOffset' => $scroll_offset, 'highlightActive' => 'on' === $highlight_active, 'enableViewMore' => 'on' === $enable_view_more, 'viewMoreCount' => max( 1, $view_more_count ), 'viewMoreText' => (string) $view_more_text, 'viewLessText' => (string) $view_less_text, 'sticky' => 'on' === $sticky, 'stickyOffset' => $sticky_offset, 'stickyMaxHeight' => $sticky_max_height, 'hideOnMobile' => 'on' === $hide_on_mobile, ) ); // AMP / no-JS fallback: a slot that add_heading_ids() fills with the list // once the page HTML exists (AMP strips custom JS). Not with a content or // exclude selector: PHP cannot evaluate CSS selectors, so a server list // would ignore them; the front end builds that list instead. $server_list = ( '' === trim( (string) $content_selector ) && '' === trim( (string) $exclude_selector ) ) ? HeadingIndex::slot( $start_level, $end_level, $min_headings ) : ''; // Module wrapper classes. $preset_slug = preg_replace( '/[^a-z0-9_-]/', '', (string) $style_preset ); $wrapper_classes = array( 'dtq-module', 'dtq-toc', 'dtq-toc-style-' . ( $preset_slug ?: 'default' ) ); if ( 'on' === $sticky ) { $wrapper_classes[] = 'dtq-toc-sticky'; } if ( 'on' === $collapsible && 'on' === $initially_collapsed ) { $wrapper_classes[] = 'dtq-toc-collapsed'; } if ( 'on' === $hide_on_mobile ) { $wrapper_classes[] = 'dtq-toc-hide-mobile'; } if ( 'on' === $collapsible_subs ) { $wrapper_classes[] = 'dtq-toc-collapse-subs'; } $icon_slug = preg_replace( '/[^a-z]/', '', (string) $collapse_icon ); $wrapper_classes[] = 'dtq-toc-icon-' . ( $icon_slug ?: 'chevron' ); // Header. $header_html = ''; if ( 'on' === $show_title || 'on' === $collapsible ) { $title_html = 'on' === $show_title ? sprintf( '%s', esc_html( $title ) ) : ''; $toggle_html = 'on' === $collapsible ? '' : ''; $header_html = sprintf( '
%s%s
', $title_html, $toggle_html ); } // Inline style: accent CSS variable (drives preset accents) + sticky offset. $inline_style = '--dtq-toc-accent:' . esc_attr( $active_color ) . ';'; if ( 'on' === $sticky ) { $inline_style .= 'top:' . (int) $sticky_offset . 'px;'; if ( $sticky_max_height > 0 ) { $inline_style .= '--dtq-toc-sticky-max:' . (int) $sticky_max_height . 'px;'; } } $sticky_style = ' style="' . $inline_style . '"'; // Style components (background pattern/mask, box-shadow overlay). The // method returns its markup; it used to be wrapped in an output buffer as // if it echoed, so the return value was dropped and the options did // nothing on the front end. $style_components = is_callable( array( $elements, 'style_components' ) ) ? (string) $elements->style_components( array( 'attrName' => 'module' ) ) : ''; $children = sprintf( '%1$s
%5$s
', $style_components, esc_attr( implode( ' ', $wrapper_classes ) ), esc_attr( $toc_data ), $sticky_style, $header_html, $server_list // A fixed-format HTML comment; filled by HeadingIndex::fill(). ); return Module::render( array( 'orderIndex' => $block->parsed_block['orderIndex'] ?? 0, 'attrs' => $attrs, 'elements' => $elements, 'id' => $block->parsed_block['id'] ?? '', 'moduleClassName' => 'dtq_table_of_contents', 'name' => $block->block_type->name ?? '', 'classnamesFunction' => array( self::class, 'module_classnames' ), 'stylesComponent' => array( self::class, 'module_styles' ), 'scriptDataComponent' => array( self::class, 'module_script_data' ), 'children' => $children, ) ); } }