2
2
/*
3
3
Plugin Name: Shy's Tutorials & Handbooks
4
4
Description: Create, manage, and restrict access to tutorials and handbooks, with features for manual user assignment and private content sharing.
5
- Version: 1.3
5
+ Version: 1.4
6
6
License: GPLv2
7
7
Author: HackTheDev
8
8
*/
@@ -97,7 +97,7 @@ function thp_register_custom_post_type() {
97
97
}
98
98
add_action ('init ' , 'thp_register_custom_post_type ' );
99
99
100
- // Generate TOC if headings are present
100
+ // Generate nested toc
101
101
function thp_generate_toc ($ content ) {
102
102
global $ post ;
103
103
@@ -109,11 +109,43 @@ function thp_generate_toc($content) {
109
109
$ toc = '<div class="toc-content-container"> ' ;
110
110
$ toc .= '<aside class="toc-widget"> ' ;
111
111
$ toc .= '<h2>Table of Contents</h2><ul class="toc-list"> ' ;
112
+
113
+ // To track the nesting level
114
+ $ current_level = 2 ;
115
+ $ list_stack = array ();
116
+
112
117
foreach ($ matches [2 ] as $ key => $ heading ) {
118
+ $ heading_level = (int )$ matches [1 ][$ key ];
113
119
$ anchor = sanitize_title ($ heading );
120
+
121
+ // Open new lists as needed
122
+ if ($ heading_level > $ current_level ) {
123
+ // Push current list onto stack
124
+ $ list_stack [] = $ toc ;
125
+ $ toc .= '<ul> ' ;
126
+ } elseif ($ heading_level < $ current_level ) {
127
+ // Close lists as needed
128
+ while ($ heading_level < $ current_level ) {
129
+ $ toc .= '</ul> ' ;
130
+ $ current_level --;
131
+ $ toc = array_pop ($ list_stack );
132
+ }
133
+ }
134
+
135
+ // Add the list item
114
136
$ toc .= '<li><a href="# ' . $ anchor . '"> ' . $ heading . '</a></li> ' ;
115
- $ content = str_replace ($ matches [0 ][$ key ], '<h ' . $ matches [1 ][$ key ] . ' id=" ' . $ anchor . '"> ' . $ heading . '</h ' . $ matches [1 ][$ key ] . '> ' , $ content );
137
+
138
+ // Update current level
139
+ $ current_level = $ heading_level ;
140
+ $ content = str_replace ($ matches [0 ][$ key ], '<h ' . $ heading_level . ' id=" ' . $ anchor . '"> ' . $ heading . '</h ' . $ heading_level . '> ' , $ content );
116
141
}
142
+
143
+ // Close any remaining open lists
144
+ while ($ current_level > 2 ) {
145
+ $ toc .= '</ul> ' ;
146
+ $ current_level --;
147
+ }
148
+
117
149
$ toc .= '</ul></aside> ' ;
118
150
$ toc .= '<div class="post-content"> ' ;
119
151
$ toc .= '<h1 class="tutorial_page_title"> ' . get_the_title ($ post ->ID ) . '</h1> ' ; // Include the post title in the content area
@@ -143,6 +175,8 @@ function thp_generate_toc($content) {
143
175
}
144
176
add_filter ('the_content ' , 'thp_generate_toc ' );
145
177
178
+
179
+
146
180
// Add a meta box for hiding a tutorial from general listing
147
181
function thp_add_hide_meta_box () {
148
182
add_meta_box (
@@ -428,6 +462,12 @@ function shy_knowledge_base_shortcode($atts) {
428
462
429
463
?>
430
464
<div class="knowledge-base-container">
465
+
466
+ <style>
467
+ .post-content h1 {
468
+ display: none !important;
469
+ }
470
+ </style>
431
471
<h2 class="knowledge-base-title"><?php echo esc_html (get_the_title ()); ?> </h2>
432
472
433
473
<!-- Search Bar -->
0 commit comments