PluginProbe
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot / 4.9.2
BetterDocs – AI Documentation, Knowledge Base, MCP Server, Docs, Wikis, FAQ & Chatbot v4.9.2
4.9.2 4.9.1 4.9.0 4.8.2 4.8.1 4.8.0 4.7.0 4.6.2 4.6.1 4.6.0 4.5.6 4.5.5 4.5.4 4.5.3 4.5.2 4.5.1 4.5.0 4.4.1 4.4.0 3.3.4 3.4.0 3.4.1 3.4.2 3.5.0 3.5.1 All 200 releases
← All changes | includes/Shortcodes/ToC.php +151 -103 3.3.44.9.2 View file →
@@ -5,127 +5,175 @@
5 5 use WPDeveloper\BetterDocs\Utils\Node;
6 6 use WPDeveloper\BetterDocs\Core\Shortcode;
7 7
8 8 class ToC extends Shortcode {
9 - protected $html_attributes = [];
9 + protected $html_attributes = [];
10 10
11 - public function get_name() {
12 - return 'betterdocs_toc';
13 - }
11 + public function get_name() {
12 + return 'betterdocs_toc';
13 + }
14 14
15 - public function get_style_depends() {
16 - return ['betterdocs-toc'];
17 - }
15 + public function get_style_depends() {
16 + return [ 'betterdocs-toc' ];
17 + }
18 18
19 - /**
20 - * Summary of default_attributes
21 - * @return array
22 - */
23 - public function default_attributes() {
24 - return [
25 - 'post_type' => 'docs',
26 - 'post_id' => get_the_ID(),
27 - 'htags' => '1,2,3,4,5,6',
28 - 'hierarchy' => '',
29 - 'list_number' => '',
30 - 'collapsible_on_mobile' => $this->settings->get( 'collapsible_toc_mobile', false ),
31 - 'toc_title' => ''
32 - ];
33 - }
19 + /**
20 + * Summary of default_attributes
21 + * @return array
22 + */
23 + public function default_attributes() {
24 + return [
25 + 'post_type' => 'docs',
26 + 'post_id' => get_the_ID(),
27 + 'htags' => '1,2,3,4,5,6',
28 + 'hierarchy' => '',
29 + 'list_number' => '',
30 + 'collapsible_on_mobile' => $this->settings->get( 'collapsible_toc_mobile', false ),
31 + 'toc_title' => ''
32 + ];
33 + }
34 34
35 - public function render( $atts, $content = null ) {
36 - $this->views( 'shortcodes/toc' );
37 - }
35 + public function render( $atts, $content = null ) {
36 + // Check if post is password protected and user hasn't provided correct password
37 + if ( post_password_required( $this->attributes['post_id'] ) ) {
38 + // Don't show ToC for password-protected posts until password is provided
39 + return '';
40 + }
38 41
39 - public function view_params() {
40 - return [
41 - 'post' => get_post( $this->attributes['post_id'] )
42 - ];
43 - }
42 + $this->views( 'shortcodes/toc' );
43 + }
44 44
45 - /**
46 - * This method is responsible for re-arranging the TOC data based on hierarchy or non-hierarchy
47 - *
48 - * @param string $post_content
49 - * @param string $htag_support
50 - * @return Node|null
51 - */
52 - public function format_toc_data( $post_content, $htag_support, $toc_hierarchy ) {
53 - $matches = [];
45 + public function view_params() {
46 + return [
47 + 'post' => get_post( $this->attributes['post_id'] )
48 + ];
49 + }
54 50
55 - if ( $htag_support != '' ) {
56 - preg_match_all( '/(<h([' . $htag_support . ']{1})[^>]*>).*<\/h\2>/msuU', $post_content, $matches, PREG_SET_ORDER );
57 - }
51 + /**
52 + * Process content for TOC generation without triggering heavy content filters
53 + * This handles special characters while avoiding memory issues from plugins like WPML
54 + *
55 + * @param string $content Raw post content
56 + * @return string Processed content
57 + */
58 + public function process_content_for_toc( $content ) {
59 + // Check if we should use the full content filter (for backward compatibility)
60 + $use_full_filter = apply_filters( 'betterdocs_toc_use_full_content_filter', false );
58 61
59 - if ( ! empty( $matches ) ) {
60 - /*
61 - |--------------------------------------------------------------------------
62 - | Backtracking Algorithm Using Iteration | Main Login For Hierarchy TOC
63 - |--------------------------------------------------------------------------
64 - |
65 - | Initially an object with key null and empty item of arrays are inserted into the stack of arrays.
66 - | When inside the loop condition for the first time, the last stack value is assigned in a variable $last_data.
67 - | And a new node object $new_data which is instantiated and the tag number is inserted for comparison as key, and empty items
68 - | as a array are inserted into items property of the new node object. On the 'if' condition it checks, if the current tag number
69 - | is smaller or equal to the last stack number. If the condition is true, the it enters into the while loop condition, which also
70 - | checks if the current last stack tag number is greater or equal to the current tag number. It pops values from the stack until and unless the
71 - | condition becomes false. The while loop condition becomes false only when the last stack value tag number becomes null and the last stack number is not
72 - | greater or equal to the current node tag number.
73 - |
74 - | Backtracking occurs when the current tag_number $number[2] is less than or equal to the stacks last tag_number which is assigned as $last_data
75 - |
76 - | And then the last value is inserted as the new last_data, and the new_data node is inserted into the last_data node items.
77 - | Additionally the $new_data is inserted into the stack to keep track of the used node. Somehow if the if condition becomes false
78 - | then the stack last data is taken, and the new_data is inserted into the last_data->items and the new_data is inserted into the stack.
79 - |
80 - */
62 + if ( $use_full_filter ) {
63 + // Use the full content filter if explicitly enabled
64 + // phpcs:ignore WordPress.NamingConventions.PrefixAllGlobals.NonPrefixedHooknameFound -- intentional WP core filter integration to render content as the platform would.
65 + return apply_filters( 'the_content', $content );
66 + }
81 67
82 - $dynamic_toc_title_switch = $this->settings->get( 'toc_dynamic_title' );
83 - $stack = [];
84 - $root = new Node();
85 - $root->key = null;
86 - $root->items = [];
87 - $tag_counter = 0;
68 + // Apply only essential content processing filters that handle special characters
69 + // without triggering heavy processing from plugins like WPML, page builders, etc.
88 70
89 - array_push( $stack, $root );
71 + // Handle shortcodes first (but don't execute them, just remove them to avoid conflicts)
72 + $content = strip_shortcodes( $content );
90 73
91 - foreach ( $matches as $number ) {
92 - $last_data = $stack[count( $stack ) - 1];
93 - $current_tag_number = isset( $number[2] ) ? $number[2] : '';
94 - $current_title = isset( $number[0] ) ? $number[0] : '';
95 - $current_tag = isset( $number[1] ) ? $number[1] : '';
74 + // Decode HTML entities to handle special characters properly
75 + $content = html_entity_decode( $content, ENT_QUOTES, 'UTF-8' );
96 76
97 - $heading_name = preg_replace( '/<[^<]+?>/', '', $current_title );
98 - $heading_name = ! empty( $heading_name ) ? strtolower( str_replace( " ", '-', preg_replace('/<[^>]+>|[^a-zA-Z\s\d]+/', "", html_entity_decode( $heading_name ) ) ) ) : '';
99 - preg_match('/id="(.+?)"/', $current_title, $matches_id);
100 - $heading_id = isset( $matches_id[1] ) ? strtolower( $matches_id[1] ) : '';
101 - $tag_number = ! empty( $heading_id ) ? $heading_id : ( ! empty( $heading_name ) && $this->settings->get( 'toc_dynamic_title' ) ? $heading_name : $tag_counter . '-toc-title' );
77 + // Convert line breaks to proper HTML if needed
78 + $content = wpautop( $content );
102 79
103 - $new_data = new Node();
104 - $new_data->key = $current_tag_number;
105 - $new_data->tag = $current_tag;
106 - $new_data->title = $current_title;
107 - $new_data->tag_number = $tag_number;
108 - $new_data->items = [];
80 + // Apply specific filters that are safe and necessary for TOC generation
81 + // These are lightweight filters that handle character encoding and basic formatting
82 + $content = apply_filters( 'betterdocs_toc_content_processing', $content );
109 83
110 - if ( $last_data->key != null && $current_tag_number <= $last_data->key ) {
111 - while ( $stack[count( $stack ) - 1]->key != null && $stack[count( $stack ) - 1]->key >= $current_tag_number ) {
112 - array_pop( $stack );
113 - }
114 - $last_data = $stack[count( $stack ) - 1];
115 - }
84 + // Additional safety: limit content size to prevent memory issues
85 + $max_content_length = apply_filters( 'betterdocs_toc_max_content_length', 500000 ); // 500KB default
86 + if ( strlen( $content ) > $max_content_length ) {
87 + $content = substr( $content, 0, $max_content_length );
88 + }
116 89
117 - array_push( $last_data->items, $new_data );
90 + return $content;
91 + }
118 92
119 - if ( $toc_hierarchy != 'off' && $toc_hierarchy != '' ) {
120 - array_push( $stack, $new_data );
121 - }
93 + /**
94 + * This method is responsible for re-arranging the TOC data based on hierarchy or non-hierarchy
95 + *
96 + * @param string $post_content
97 + * @param string $htag_support
98 + * @return Node|null
99 + */
100 + public function format_toc_data( $post_content, $htag_support, $toc_hierarchy ) {
101 + $matches = [];
122 102
123 - $tag_counter++;
124 - }
103 + if ( $htag_support != '' ) {
104 + preg_match_all( '/(<h([' . $htag_support . ']{1})[^>]*>).*<\/h\2>/msuU', $post_content, $matches, PREG_SET_ORDER );
105 + }
125 106
126 - return $root;
127 - }
107 + if ( ! empty( $matches ) ) {
108 + /*
109 + |--------------------------------------------------------------------------
110 + | Backtracking Algorithm Using Iteration | Main Login For Hierarchy TOC
111 + |--------------------------------------------------------------------------
112 + |
113 + | Initially an object with key null and empty item of arrays are inserted into the stack of arrays.
114 + | When inside the loop condition for the first time, the last stack value is assigned in a variable $last_data.
115 + | And a new node object $new_data which is instantiated and the tag number is inserted for comparison as key, and empty items
116 + | as a array are inserted into items property of the new node object. On the 'if' condition it checks, if the current tag number
117 + | is smaller or equal to the last stack number. If the condition is true, the it enters into the while loop condition, which also
118 + | checks if the current last stack tag number is greater or equal to the current tag number. It pops values from the stack until and unless the
119 + | condition becomes false. The while loop condition becomes false only when the last stack value tag number becomes null and the last stack number is not
120 + | greater or equal to the current node tag number.
121 + |
122 + | Backtracking occurs when the current tag_number $number[2] is less than or equal to the stacks last tag_number which is assigned as $last_data
123 + |
124 + | And then the last value is inserted as the new last_data, and the new_data node is inserted into the last_data node items.
125 + | Additionally the $new_data is inserted into the stack to keep track of the used node. Somehow if the if condition becomes false
126 + | then the stack last data is taken, and the new_data is inserted into the last_data->items and the new_data is inserted into the stack.
127 + |
128 + */
128 129
129 - return null;
130 - }
130 + $dynamic_toc_title_switch = $this->settings->get( 'toc_dynamic_title' );
131 + $stack = [];
132 + $root = new Node();
133 + $root->key = null;
134 + $root->items = [];
135 + $tag_counter = 0;
136 +
137 + array_push( $stack, $root );
138 +
139 + foreach ( $matches as $number ) {
140 + $last_data = $stack[ count( $stack ) - 1 ];
141 + $current_tag_number = isset( $number[2] ) ? $number[2] : '';
142 + $current_title = isset( $number[0] ) ? $number[0] : '';
143 + $current_tag = isset( $number[1] ) ? $number[1] : '';
144 +
145 + $heading_name = preg_replace( '/<[^<]+?>/', '', $current_title );
146 + $heading_name = ! empty( $heading_name ) ? strtolower( str_replace( ' ', '-', preg_replace( '/[^\p{L}\p{N}\s]/u', '', $heading_name ) ) ) : '';
147 + preg_match( '/id="(.+?)"/', $current_title, $matches_id );
148 + $heading_id = isset( $matches_id[1] ) ? strtolower( $matches_id[1] ) : '';
149 + $tag_number = ! empty( $heading_id ) ? $heading_id : ( ! empty( $heading_name ) && $dynamic_toc_title_switch ? $heading_name : $tag_counter . '-toc-title' );
150 +
151 + $new_data = new Node();
152 + $new_data->key = $current_tag_number;
153 + $new_data->tag = $current_tag;
154 + $new_data->title = $current_title;
155 + $new_data->tag_number = $tag_number;
156 + $new_data->items = [];
157 +
158 + if ( $last_data->key != null && $current_tag_number <= $last_data->key ) {
159 + while ( $stack[ count( $stack ) - 1 ]->key != null && $stack[ count( $stack ) - 1 ]->key >= $current_tag_number ) {
160 + array_pop( $stack );
161 + }
162 + $last_data = $stack[ count( $stack ) - 1 ];
163 + }
164 +
165 + array_push( $last_data->items, $new_data );
166 +
167 + if ( $toc_hierarchy != 'off' && $toc_hierarchy != '' ) {
168 + array_push( $stack, $new_data );
169 + }
170 +
171 + ++$tag_counter;
172 + }
173 +
174 + return $root;
175 + }
176 +
177 + return null;
178 + }
131 179 }