Skip to content

Commit 1f6d0be

Browse files
committed
Add anchor injection for non-inline TOC consumers
Synced from knowledgebase-pro. Adds heading anchor IDs when using floating TOC, sidebar widget, or TOC block without inline TOC enabled. Also updates dev dependencies (prettier-config, terser, @wordpress/icons).
1 parent d4cc5f5 commit 1f6d0be

11 files changed

Lines changed: 85 additions & 12 deletions

File tree

‎CLAUDE.md‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ This file provides guidance to Claude Code (claude.ai/code) when working with co
44

55
## Plugin Overview
66

7-
WebberZone Knowledge Base Pro (v3.0.0) is a WordPress plugin (namespace `WebberZone\Knowledge_Base`) that creates a multi-product knowledge base system. It uses a freemium model via Freemius integration — free core features with premium features in `/includes/pro/`.
7+
WebberZone Knowledge Base Pro (v3.0.1 in development) is a WordPress plugin (namespace `WebberZone\Knowledge_Base`) that creates a multi-product knowledge base system. It uses a freemium model via Freemius integration — free core features with premium features in `/includes/pro/`.
88

99
- **Plugin entry**: `knowledgebase.php` (defines constants, loads Freemius via `load-freemius.php`, registers autoloader, and directly requires `includes/options-api.php` and `includes/functions.php`)
1010
- **PHP**: 7.4+ | **WordPress**: 6.7+

‎includes/frontend/class-toc.php‎

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,14 @@
2020
*/
2121
class TOC {
2222

23+
/**
24+
* Whether heading anchors have been injected on this page load.
25+
*
26+
* @since 3.0.1
27+
* @var bool
28+
*/
29+
private static bool $anchors_injected = false;
30+
2331
/**
2432
* Whether the TOC has been injected on this page load.
2533
*
@@ -34,11 +42,72 @@ class TOC {
3442
* @since 3.0.0
3543
*/
3644
public function __construct() {
45+
Hook_Registry::add_filter( 'the_content', array( $this, 'inject_anchors' ) );
46+
3747
if ( \wzkb_get_option( 'show_toc', false ) ) {
3848
Hook_Registry::add_filter( 'the_content', array( $this, 'inject_toc' ) );
3949
}
4050
}
4151

52+
/**
53+
* Filter callback: add anchor IDs to headings in article content.
54+
*
55+
* Only runs when a non-inline TOC consumer is active (floating TOC option, sidebar widget,
56+
* or block in the post content). The inline TOC manages its own anchor injection via
57+
* inject_toc(). Preserves any existing heading id attributes added by third-party TOC
58+
* blocks (e.g. Kadence, Stackable) so no duplicate anchors are introduced.
59+
*
60+
* @since 3.0.1
61+
*
62+
* @param string $content Post content.
63+
* @return string Content with anchor IDs added to headings, or original content unchanged.
64+
*/
65+
public function inject_anchors( string $content ): string {
66+
if ( self::$anchors_injected || ! is_singular( 'wz_knowledgebase' ) || ! in_the_loop() || ! is_main_query() ) {
67+
return $content;
68+
}
69+
70+
if ( ! self::needs_anchor_injection() ) {
71+
return $content;
72+
}
73+
74+
$result = self::process_content( $content, array( 'min_headings' => 1 ) );
75+
76+
if ( empty( $result['toc'] ) ) {
77+
return $content;
78+
}
79+
80+
self::$anchors_injected = true;
81+
82+
return $result['content'];
83+
}
84+
85+
/**
86+
* Whether any non-inline TOC consumer is active on the current page.
87+
*
88+
* Checks for: floating TOC option, the TOC sidebar widget, and the TOC block embedded in
89+
* the post content. The inline TOC is excluded because inject_toc() handles anchors itself.
90+
*
91+
* @since 3.0.1
92+
*
93+
* @return bool
94+
*/
95+
private static function needs_anchor_injection(): bool {
96+
if ( \wzkb_get_option( 'show_floating_toc' ) ) {
97+
return true;
98+
}
99+
100+
if ( is_active_widget( false, false, 'widget_wzkb_toc', true ) ) {
101+
return true;
102+
}
103+
104+
if ( has_block( 'knowledgebase/toc' ) ) {
105+
return true;
106+
}
107+
108+
return false;
109+
}
110+
42111
/**
43112
* Filter callback: prepend TOC to article content.
44113
*

‎includes/frontend/css/styles/classic-rtl.css‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -916,7 +916,8 @@ nav.pagination {
916916
}
917917

918918
/* Table of Contents */
919-
.wzkb-toc--inline {
919+
.wzkb-toc--inline,
920+
.wzkb-toc--block {
920921
margin-bottom: 1rem;
921922
background: var(--wzkb-bg-light);
922923
border: 1px solid var(--wzkb-border-light);

‎includes/frontend/css/styles/classic-rtl.min.css‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎includes/frontend/css/styles/classic.css‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -905,7 +905,8 @@ nav.pagination {
905905
}
906906

907907
/* Table of Contents */
908-
.wzkb-toc--inline {
908+
.wzkb-toc--inline,
909+
.wzkb-toc--block {
909910
margin-bottom: 1rem;
910911
background: var(--wzkb-bg-light);
911912
border: 1px solid var(--wzkb-border-light);

‎includes/frontend/css/styles/classic.min.css‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎includes/frontend/css/styles/vibrant-rtl.css‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1002,7 +1002,8 @@ nav.pagination {
10021002
}
10031003

10041004
/* Table of Contents */
1005-
.wzkb-toc--inline {
1005+
.wzkb-toc--inline,
1006+
.wzkb-toc--block {
10061007
margin-bottom: 1rem;
10071008
background: var(--wzkb-bg-light);
10081009
border: 1px solid var(--wzkb-border-light);

‎includes/frontend/css/styles/vibrant-rtl.min.css‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎includes/frontend/css/styles/vibrant.css‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -978,7 +978,8 @@ nav.pagination {
978978
}
979979

980980
/* Table of Contents */
981-
.wzkb-toc--inline {
981+
.wzkb-toc--inline,
982+
.wzkb-toc--block {
982983
margin-bottom: 1rem;
983984
background: var(--wzkb-bg-light);
984985
border: 1px solid var(--wzkb-border-light);

‎includes/frontend/css/styles/vibrant.min.css‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)