PluginProbe
SimpleTOC – Table of Contents Block / 7.3.1
SimpleTOC – Table of Contents Block v7.3.1
7.3.1 7.2.0 7.3.0 7.1.1 7.1.0 7.0.10 7.0.9 7.0.8 7.0.7 7.0.6 7.0.4 7.0.5 5.0.21.1 5.0.22 5.0.23 5.0.24 5.0.25 5.0.25.1 5.0.27 5.0.28 5.0.29 5.0.3 5.0.30 5.0.31 5.0.32 All 160 releases
← All changes | plugin.php +940 -319 5.0.21.17.3.1 View file →
@@ -1,430 +1,1051 @@
1 1 <?php
2 -
3 2 /**
4 3 * Plugin Name: SimpleTOC - Table of Contents Block
5 4 * Plugin URI: https://marc.tv/simpletoc-wordpress-inhaltsverzeichnis-plugin-gutenberg/
6 - * Description: Adds a basic "Table of Contents" Gutenberg block.
7 - * Version: 5.0.22
5 + * Description: SEO-friendly Table of Contents Gutenberg block. No JavaScript or CSS by default.
6 + * Version: 7.3.1
7 + * Requires at least: 6.2
8 + * Requires PHP: 7.3
8 9 * Author: Marc Tönsing
9 - * Author URI: https://marc.tv
10 + * Author URI: https://toensing.com
10 11 * Text Domain: simpletoc
11 12 * License: GPL v2 or later
12 13 * License URI: http://www.gnu.org/licenses/gpl-2.0.html
13 - *
14 + *
15 + * @package simpletoc
14 16 */
15 17
18 +namespace MToensing\SimpleTOC;
19 +
20 +require_once __DIR__ . '/simpletoc-admin-settings.php';
21 +require_once __DIR__ . '/simpletoc-class-headline-ids.php';
22 +
23 +const DEFAULT_BOX_COLOR = '#ebebeb';
24 +const SIMPLETOC_VERSION = '7.3.1';
25 +
16 26 /**
17 - * Initalise frontend and backend and register block
18 - **/
27 + * Prevents direct execution of the plugin file.
28 + * If a WordPress function does not exist, it means that the file has not been run by WordPress.
29 + */
30 +if ( ! defined( 'ABSPATH' ) || ! function_exists( 'add_filter' ) ) {
31 + header( 'Status: 403 Forbidden' );
32 + header( 'HTTP/1.1 403 Forbidden' );
33 + exit;
34 +}
19 35
20 -function register_simpletoc_block()
21 -{
36 +/**
37 + * Registers the SimpleTOC block, adds a filter for plugin row meta, and sets script translations.
38 + *
39 + * This function registers the SimpleTOC block by specifying the build directory and render callback function.
40 + * It also sets the script translations for the block editor script and adds a filter for the plugin row meta.
41 + */
42 +function register_simpletoc_block() {
22 43
23 - wp_set_script_translations( 'simpletoc-toc-editor-script', 'simpletoc' );
44 + if ( function_exists( 'wp_set_script_translations' ) ) {
45 + wp_set_script_translations( 'simpletoc-toc-editor-script', 'simpletoc' );
46 + }
24 47
25 - add_filter('plugin_row_meta', __NAMESPACE__ . '\\simpletoc_plugin_meta', 10, 2);
48 + add_filter( 'plugin_row_meta', __NAMESPACE__ . '\simpletoc_plugin_meta', 10, 2 );
26 49
27 - register_block_type( __DIR__ . '/build' , [
28 - 'render_callback' => __NAMESPACE__ . '\\render_callback'
29 - ]);
50 + register_block_type(
51 + __DIR__ . '/build',
52 + array(
53 + 'render_callback' => __NAMESPACE__ . '\render_callback_simpletoc',
54 + )
55 + );
30 56
57 + wp_add_inline_script(
58 + 'simpletoc-toc-editor-script',
59 + 'window.simpletocEditorSettings = ' . wp_json_encode(
60 + array(
61 + 'settingsUrl' => admin_url( 'options-general.php?page=simpletoc' ),
62 + )
63 + ) . ';',
64 + 'before'
65 + );
31 66 }
32 67
33 -add_action( 'init', 'register_simpletoc_block' );
68 +add_action( 'init', __NAMESPACE__ . '\register_simpletoc_block' );
34 69
35 70 /**
36 - * Filter to add plugins to the TOC list for Rank Math plugin
71 + * Adds SimpleTOC-specific block editor settings.
37 72 *
38 - * @param array TOC plugins.
73 + * @param array $editor_settings Default editor settings.
74 + * @param \WP_Block_Editor_Context $editor_context Editor context.
75 + *
76 + * @return array
39 77 */
78 +function add_simpletoc_block_editor_settings( $editor_settings, $editor_context ) {
79 + $editor_settings['simpletocSettingsUrl'] = admin_url( 'options-general.php?page=simpletoc' );
40 80
41 -add_filter('rank_math/researches/toc_plugins', function ($toc_plugins) {
42 - $toc_plugins['simpletoc/plugin.php'] = 'SimpleTOC';
43 - return $toc_plugins;
44 -});
81 + return $editor_settings;
82 +}
45 83
46 -add_filter( 'the_content', 'simpletoc_addIDstoContent', 1 );
47 -
48 -function simpletoc_addIDstoContent( $content ) {
84 +add_filter( 'block_editor_settings_all', __NAMESPACE__ . '\add_simpletoc_block_editor_settings', 10, 2 );
49 85
50 - $blocks = parse_blocks( $content );
86 +/**
87 + * Inject potentially missing translations into the block-editor i18n
88 + * collection.
89 + *
90 + * This keeps the plugin backwards compatible, in case the user did not
91 + * update translations on their website (yet).
92 + *
93 + * @param string|false|null $translations JSON-encoded translation data. Default null.
94 + * @param string|false $file Path to the translation file to load. False if there isn't one.
95 + * @param string $handle Name of the script to register a translation domain to.
96 + * @param string $domain The text domain.
97 + *
98 + * @return string|false|null JSON string
99 + */
100 +add_filter(
101 + 'load_script_translations',
102 + function ( $translations, $file, $handle, $domain ) {
103 + if ( 'simpletoc' === $domain && $translations ) {
104 + // List of translations that we inject into the block-editor JS.
105 + $dynamic_translations = array(
106 + 'Table of Contents' => __( 'Table of Contents', 'simpletoc' ),
107 + );
51 108
52 - $blocks = addIDstoBlocks_recursive( $blocks );
109 + $changed = false;
110 + $obj = json_decode( $translations, true );
53 111
54 - $content = serialize_blocks( $blocks );
55 -
56 - return $content;
112 + // Confirm that the translation JSON is valid.
113 + if ( isset( $obj['locale_data'] ) && isset( $obj['locale_data']['messages'] ) ) {
114 + $messages = $obj['locale_data']['messages'];
115 +
116 + // Inject dynamic translations, when needed.
117 + foreach ( $dynamic_translations as $key => $locale ) {
118 + if ( empty( $messages[ $key ] )
119 + || ! is_array( $messages[ $key ] )
120 + || ! array_key_exists( 0, $messages[ $key ] )
121 + || $locale !== $messages[ $key ][0]
122 + ) {
123 + $messages[ $key ] = array( $locale );
124 + $changed = true;
125 + }
126 + }
127 +
128 + // Only modify the translations string when locales did change.
129 + if ( $changed ) {
130 + $obj['locale_data']['messages'] = $messages;
131 + $translations = wp_json_encode( $obj );
132 + }
133 + }
134 + }
135 +
136 + return $translations;
137 + },
138 + 10,
139 + 4
140 +);
141 +
142 +/**
143 + * Sets the default value of translatable attributes.
144 + *
145 + * Values inside block.json are static strings that are not translated. This
146 + * filter inserts relevant translations i
147 + *
148 + * @param array $settings Array of determined settings for registering a block type.
149 + * @param array $metadata Metadata provided for registering a block type.
150 + *
151 + * @return array Modified settings array.
152 + */
153 +add_filter(
154 + 'block_type_metadata_settings',
155 + function ( $settings, $metadata ) {
156 + if ( 'simpletoc/toc' === $metadata['name'] ) {
157 + $settings['attributes']['title_text']['default'] = __( 'Table of Contents', 'simpletoc' );
158 + }
159 +
160 + return $settings;
161 + },
162 + 10,
163 + 2
164 +);
165 +
166 +/**
167 + * Filter to add plugins to the TOC list for Rank Math plugin.
168 + *
169 + * @param array $toc_plugins TOC plugins.
170 + */
171 +add_filter(
172 + 'rank_math/researches/toc_plugins',
173 + function ( $toc_plugins ) {
174 + $toc_plugins['simpletoc/plugin.php'] = 'SimpleTOC';
175 + return $toc_plugins;
176 + }
177 +);
178 +
179 +
180 +
181 +/**
182 + * Adds IDs to the headings of the provided post content using a recursive block structure.
183 + *
184 + * @param string $content The content to add IDs to.
185 + * @return string The content with IDs added to its headings
186 + */
187 +function simpletoc_add_ids_to_content( $content ) {
188 +
189 + $blocks = parse_blocks( $content );
190 +
191 + $blocks = add_ids_to_blocks_recursive( $blocks );
192 +
193 + $content = serialize_blocks( $blocks );
194 +
195 + return $content;
57 196 }
58 197
59 -function addIDstoBlocks_recursive( $blocks ) {
198 +add_filter( 'the_content', __NAMESPACE__ . '\simpletoc_add_ids_to_content', 1 );
60 199
61 - foreach ( $blocks as &$block ) {
62 - if (isset($block['blockName']) && ( $block['blockName'] === 'core/heading' || $block['blockName'] === 'generateblocks/headline') && isset($block['innerHTML']) && isset($block['innerContent']) && isset($block['innerContent'][0]) ){
63 - $block['innerHTML'] = addAnchorAttribute($block['innerHTML']);
64 - $block['innerContent'][0] = addAnchorAttribute($block['innerContent'][0]);
65 - } else if( isset($block['attrs']['ref']) ){
66 - // search in reusable blocks (this is not finished because I ran out of ideas.)
67 - // $reusable_block_id = $block['attrs']['ref'];
68 - // $reusable_block_content = parse_blocks(get_post($reusable_block_id)->post_content);
69 - } else if ( ! empty( $block['innerBlocks'] ) ) {
70 - // search in groups
71 - $block['innerBlocks'] = addIDstoBlocks_recursive( $block['innerBlocks'] );
72 - }
73 - }
200 +/**
201 + * Recursively adds IDs to the headings of a nested block structure.
202 + *
203 + * @param array $blocks The blocks to add IDs to.
204 + * @return array The blocks with IDs added to their headings
205 + */
206 +function add_ids_to_blocks_recursive( $blocks ) {
74 207
75 - return $blocks;
208 + $supported_blocks = array(
209 + 'core/heading',
210 + 'generateblocks/text',
211 + 'generateblocks/headline',
212 + );
213 +
214 + /**
215 + * Filter to add supported blocks for IDs.
216 + *
217 + * @param array $supported_blocks The array of supported blocks.
218 + */
219 + $supported_blocks = apply_filters( 'simpletoc_supported_blocks_for_ids', $supported_blocks );
220 +
221 + // Need two separate instances so that IDs aren't double counted.
222 + $inner_html_id_instance = new SimpleTOC_Headline_Ids();
223 + $inner_content_id_instance = new SimpleTOC_Headline_Ids();
224 +
225 + foreach ( $blocks as &$block ) {
226 + if ( isset( $block['blockName'] ) && in_array( $block['blockName'], $supported_blocks, true ) && isset( $block['innerHTML'] ) && isset( $block['innerContent'] ) && isset( $block['innerContent'][0] ) ) {
227 + $block['innerHTML'] = add_anchor_attribute( $block['innerHTML'], $inner_html_id_instance, $block );
228 + $block['innerContent'][0] = add_anchor_attribute( $block['innerContent'][0], $inner_content_id_instance, $block );
229 + } elseif ( ! empty( $block['innerBlocks'] ) ) {
230 + // search in groups.
231 + $block['innerBlocks'] = add_ids_to_blocks_recursive( $block['innerBlocks'] );
232 + }
233 + }
234 +
235 + return $blocks;
76 236 }
77 237
78 238 /**
79 - * Render block output
239 + * Renders a Table of Contents block for a post
80 240 *
241 + * @param array $attributes An array of attributes for the Table of Contents block.
242 + * @return string The HTML output for the Table of Contents block
81 243 */
244 +function render_callback_simpletoc( $attributes ) {
245 + $is_backend = defined( 'REST_REQUEST' ) && REST_REQUEST && 'edit' === filter_input( INPUT_GET, 'context' );
246 + $title_text = $attributes['title_text'] ? esc_html( trim( $attributes['title_text'] ) ) : __( 'Table of Contents', 'simpletoc' );
247 + $alignclass = ! empty( $attributes['align'] ) ? 'align' . $attributes['align'] : '';
248 + $title_level = $attributes['title_level'];
249 + $global_box_style_enabled = apply_filters( 'simpletoc_box_style_enabled', false ) || true === (bool) get_option( 'simpletoc_box_style_enabled', false );
250 + $legacy_box_style_enabled = ! empty( $attributes['box_style'] );
251 + $typography_enabled = ! empty( $attributes['fontSize'] ) || ! empty( $attributes['style']['typography'] );
252 + $wrapper_classes = array( 'simpletoc' );
253 + $wrapper_style = '';
82 254
83 -function render_callback( $attributes )
84 -{
255 + if ( $typography_enabled ) {
256 + $wrapper_classes[] = 'has-simpletoc-typography';
257 + }
85 258
86 - $is_backend = defined('REST_REQUEST') && true === REST_REQUEST && 'edit' === filter_input(INPUT_GET, 'context');
259 + if ( $global_box_style_enabled || $legacy_box_style_enabled ) {
260 + $wrapper_classes[] = 'is-style-boxed';
261 + $wrapper_classes[] = 'has-simpletoc-box-style';
87 262
88 - $alignclass = '';
89 - if ( isset ($attributes['align']) ) {
90 - $align = $attributes['align'];
91 - $alignclass = 'align' . $align;
92 - }
263 + if ( $global_box_style_enabled ) {
264 + $wrapper_classes[] = 'has-background';
265 + $wrapper_style = safecss_filter_attr( 'background-color:' . DEFAULT_BOX_COLOR . ';' );
266 + } elseif ( ! empty( $attributes['box_color'] ) ) {
267 + $wrapper_classes[] = 'has-background';
268 + $wrapper_style = safecss_filter_attr( 'background-color:' . $attributes['box_color'] . ';' );
269 + } else {
270 + $wrapper_classes[] = 'has-background';
271 + $wrapper_style = safecss_filter_attr( 'background-color:' . DEFAULT_BOX_COLOR . ';' );
272 + }
273 + }
93 274
94 - $className = '';
95 - if ( isset( $attributes['className'] ) ) {
96 - $className = strip_tags(htmlspecialchars($attributes['className'] ));
97 - }
275 + $wrapper_attrs = get_block_wrapper_attributes(
276 + array(
277 + 'class' => implode( ' ', $wrapper_classes ),
278 + 'style' => $wrapper_style,
279 + )
280 + );
281 + $pre_html = '<div role="navigation" aria-label="' . esc_attr__( 'Table of Contents', 'simpletoc' ) . '" ' . $wrapper_attrs . '>';
282 + $post_html = '</div>';
98 283
99 - $pre_html = '';
100 - $post_html = '';
101 - if ( $className != '' ) {
102 - $pre_html = '<div class="simpletoc ' . $className . '">';
103 - $post_html = '</div>';
104 - }
105 -
106 - $post = get_post();
107 - if ( is_null($post) || is_null($post->post_content) ) {
108 - $blocks = '';
109 - } else {
110 - $blocks = parse_blocks($post->post_content);
111 - }
284 + $post = get_post();
285 + $blocks = ! is_null( $post ) && ! is_null( $post->post_content ) ? parse_blocks( $post->post_content ) : '';
112 286
113 - if ( empty($blocks) ) {
114 - $html = '';
115 - if( $is_backend == true ) {
116 - if ($attributes['no_title'] == false) {
117 - $html = '<h2 class="simpletoc-title ' . $alignclass . '">' . __('Table of Contents', 'simpletoc') . '</h2>';
118 - }
119 -
120 - $html .= '<p class="components-notice is-warning ' . $alignclass . '">' . __('No blocks found.', 'simpletoc') . ' ' . __('Save or update post first.', 'simpletoc') . '</p>';
121 - }
122 - return $html;
123 - }
287 + $headings = array_reverse( filter_headings_recursive( $blocks ) );
288 + $headings = simpletoc_add_pagenumber( $blocks, $headings );
289 + $headings_clean = array_map( 'trim', $headings );
290 + $toc_html = generate_toc( $headings_clean, $attributes );
124 291
125 - $headings = array_reverse(filter_headings_recursive($blocks));
292 + if ( empty( $blocks ) ) {
293 + 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 );
294 + }
126 295
127 - // enrich headings with pages as a data-attribute
128 - $headings = simpletoc_add_pagenumber($blocks, $headings);
296 + if ( empty( $headings_clean ) ) {
297 + 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 );
298 + }
129 299
130 - $headings_clean = array_map('trim', $headings);
300 + if ( empty( $toc_html ) ) {
301 + 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 );
302 + }
131 303
132 - if ( empty( $headings_clean ) ) {
133 - $html = '';
134 - if( $is_backend == true ) {
304 + return $pre_html . $toc_html . $post_html;
305 +}
135 306
136 - if ($attributes['no_title'] == false) {
137 - $html = '<h2 class="simpletoc-title ' . $alignclass . '">' . __('Table of Contents', 'simpletoc') . '</h2>';
138 - }
139 -
140 - $html .= '<p class="components-notice is-warning ' . $alignclass . '">' . __('No headings found.', 'simpletoc') . ' ' . __('Save or update post first.', 'simpletoc') . '</p>';
141 - }
142 - return $html;
143 - }
307 +/**
308 + * Generates an HTML message for empty blocks cases in the Table of Contents.
309 + *
310 + * @param bool $is_backend Indicates if the request is from the backend (i.e., the WordPress editor).
311 + * @param array $attributes An array of attributes for the Table of Contents block.
312 + * @param int $title_level The heading level for the Table of Contents title.
313 + * @param string $alignclass The CSS class for alignment of the Table of Contents block.
314 + * @param string $title_text The text for the Table of Contents title.
315 + * @param string $warning_text1 The first part of the warning message to be displayed.
316 + * @param string $warning_text2 The second part of the warning message to be displayed.
317 + * @param string $wrapper_attrs Block wrapper attributes.
318 + *
319 + * @return string The HTML output for the empty blocks message.
320 + */
321 +function get_empty_blocks_message( $is_backend, $attributes, $title_level, $alignclass, $title_text, $warning_text1, $warning_text2, $wrapper_attrs = '' ) {
322 + $html = '';
144 323
145 - $toclist = generateToc($headings_clean, $attributes);
324 + if ( $is_backend ) {
325 + $html .= '<div role="navigation" aria-label="' . esc_attr__( 'Table of Contents', 'simpletoc' ) . '" ' . $wrapper_attrs . '>';
326 + $html .= sprintf( '<h%d class="%s">%s</h%d>', $title_level, esc_attr( trim( 'simpletoc-title ' . $alignclass ) ), $title_text, $title_level );
327 + $html .= sprintf( '<p class="components-notice is-warning %s">%s %s</p>', esc_attr( $alignclass ), esc_html( $warning_text1 ), esc_html( $warning_text2 ) );
328 + $html .= '</div>';
329 + }
146 330
147 - if ( empty( $toclist ) ) {
148 - $html = '';
149 - if( $is_backend == true ) {
331 + return $html;
332 +}
150 333
151 - if ($attributes['no_title'] == false) {
152 - $html = '<h2 class="simpletoc-title ' . $alignclass . '">' . __('Table of Contents', 'simpletoc') . '</h2>';
153 - }
154 -
155 - $html .= '<p class="components-notice is-warning ' . $alignclass . '">' . __('No headings found.', 'simpletoc') . ' ' . __('Check minimal and maximum level block settings.', 'simpletoc') . '</p>';
156 - }
157 - return $html;
334 +/**
335 + * Adds page numbers to headings in the provided blocks array.
336 + *
337 + * @param array $blocks The array of blocks to process.
338 + * @param array $headings The array of headings to add page numbers to.
339 + * @return array The modified headings array with page numbers added.
340 + */
341 +function simpletoc_add_pagenumber( $blocks, $headings ) {
342 + $pages = 1;
158 343
159 - }
344 + if ( ! is_array( $blocks ) ) {
345 + return $headings;
346 + }
160 347
161 - $output = $pre_html . $toclist . $post_html;
348 + foreach ( $blocks as $block => $inner_block ) {
349 + // count nextpage blocks.
350 + if ( isset( $blocks[ $block ]['blockName'] ) && 'core/nextpage' === $blocks[ $block ]['blockName'] ) {
351 + ++$pages;
352 + }
162 353
163 - return $output;
354 + if ( isset( $blocks[ $block ]['blockName'] ) && 'core/heading' === $blocks[ $block ]['blockName'] ) {
355 + // make sure its a headline.
356 + foreach ( $headings as $heading => &$inner_heading ) {
357 + if ( $inner_heading === $blocks[ $block ]['innerHTML'] ) {
358 + $inner_heading = simpletoc_add_page_number_to_headline( $blocks[ $block ]['innerHTML'], $pages );
359 + }
360 + }
361 + }
362 + }
363 + return $headings;
164 364 }
165 365
366 +/**
367 + * Return all headings with a recursive walk through all blocks.
368 + * This includes groups and reusable block with groups within reusable blocks.
369 + *
370 + * @param array[] $blocks The blocks to filter headings from.
371 + * @return array[]
372 + */
373 +function filter_headings_recursive( $blocks ) {
374 + $arr = array();
166 375
167 -function simpletoc_add_pagenumber( $blocks, $headings ){
168 - $pages = 1;
376 + if ( ! is_array( $blocks ) ) {
377 + return $arr;
378 + }
169 379
170 - foreach ($blocks as $block => $innerBlock) {
171 -
172 - // count nextpage blocks
173 - if (isset($blocks[$block]['blockName']) && $blocks[$block]['blockName'] === 'core/nextpage' ){
174 - $pages++;
175 - }
380 + // allow developers to ignore specific blocks.
381 + $ignored_blocks = apply_filters( 'simpletoc_excluded_blocks', array() );
176 382
177 - if (isset($blocks[$block]['blockName']) && $blocks[$block]["blockName"] === 'core/heading') {
178 - // make sure its a headline.
179 - foreach ($headings as $heading => &$innerHeading){
180 - if($innerHeading == $blocks[$block]["innerHTML"]){
181 - $innerHeading = preg_replace("/(<h1|<h2|<h3|<h4|<h5|<h6)/i", '$1 data-page="' . $pages . '"', $blocks[$block]["innerHTML"]);
182 - }
183 - }
184 - }
185 - }
186 - return $headings;
383 + foreach ( $blocks as $inner_block ) {
384 + if ( is_array( $inner_block ) ) {
385 + // if block is ignored, skip.
386 + if ( isset( $inner_block['blockName'] ) && in_array( $inner_block['blockName'], $ignored_blocks, true ) ) {
387 + continue;
388 + }
389 +
390 + if ( isset( $inner_block['attrs']['ref'] ) ) {
391 + // search in reusable blocks.
392 + $post = get_post( $inner_block['attrs']['ref'] );
393 + if ( $post ) {
394 + $e_arr = parse_blocks( $post->post_content );
395 + $arr = array_merge( filter_headings_recursive( $e_arr ), $arr );
396 + }
397 + } else {
398 + // search in groups.
399 + $arr = array_merge( filter_headings_recursive( $inner_block ), $arr );
400 + }
401 + } else {
402 + if ( isset( $blocks['blockName'] ) && ( 'core/heading' === $blocks['blockName'] ) && 'core/heading' !== $inner_block && simpletoc_is_heading_html( $inner_block ) ) {
403 + $arr[] = $inner_block;
404 + }
405 +
406 + $supported_third_party_blocks = array(
407 + 'generateblocks/headline', /* GenerateBlocks 1.x */
408 + 'generateblocks/text', /* GenerateBlocks 2.0 */
409 + );
410 +
411 + /**
412 + * Filter to add supported third party blocks.
413 + *
414 + * @param array $supported_third_party_blocks The array of supported third party blocks.
415 + * @return array The modified array of supported third party blocks.
416 + */
417 + $supported_third_party_blocks = apply_filters(
418 + 'simpletoc_supported_third_party_blocks',
419 + $supported_third_party_blocks
420 + );
421 +
422 + if ( isset( $blocks['blockName'] ) && in_array( $blocks['blockName'], $supported_third_party_blocks, true ) && 'core/heading' !== $inner_block && simpletoc_is_heading_html( $inner_block ) ) {
423 + $inner_block = simpletoc_maybe_replace_generateblocks_dynamic_tags( $inner_block, $blocks );
424 + $arr[] = $inner_block;
425 + }
426 + }
427 + }
428 +
429 + return $arr;
187 430 }
188 431
189 432 /**
190 - * Return all headings with a recursive walk through all blocks.
191 - * This includes groups and reusable block with groups within reusable blocks.
433 + * Replaces GenerateBlocks dynamic tags in heading HTML before SimpleTOC uses it in the TOC.
434 + *
435 + * @param string $html The heading HTML.
436 + * @param array $block The parsed block data.
437 + * @return string The heading HTML with GenerateBlocks dynamic tags resolved when available.
192 438 */
439 +function simpletoc_maybe_replace_generateblocks_dynamic_tags( $html, $block ) {
440 + if ( ! class_exists( '\GenerateBlocks_Register_Dynamic_Tag' ) || false === strpos( $html, '{{' ) ) {
441 + return $html;
442 + }
193 443
194 -function filter_headings_recursive( $blocks )
195 -{
196 - $arr = array();
444 + return \GenerateBlocks_Register_Dynamic_Tag::replace_tags( $html, $block, null );
445 +}
197 446
198 - foreach ($blocks as $block => $innerBlock) {
447 +/**
448 + * Gets heading HTML used for anchor generation.
449 + *
450 + * @param string $html The original heading HTML.
451 + * @param array $block The parsed block data.
452 + * @return string The heading HTML to use for anchor generation.
453 + */
454 +function simpletoc_get_heading_html_for_anchor( $html, $block ) {
455 + $heading_html = simpletoc_maybe_replace_generateblocks_dynamic_tags( $html, $block );
199 456
200 - if (is_array($innerBlock)) {
457 + if ( '' === trim( wp_strip_all_tags( $heading_html ) ) ) {
458 + return $html;
459 + }
201 460
202 - if (isset($innerBlock['attrs']['ref'])) {
203 - // search in reusable blocks
204 - $e_arr = parse_blocks(get_post($innerBlock['attrs']['ref'])->post_content);
205 - $arr = array_merge(filter_headings_recursive($e_arr), $arr);
206 - } else {
207 - // search in groups
208 - $arr = array_merge(filter_headings_recursive($innerBlock), $arr);
209 - }
210 - } else {
461 + return $heading_html;
462 +}
211 463
212 - if (isset($blocks['blockName']) && ( $blocks['blockName'] === 'core/heading' ) && $innerBlock !== 'core/heading') {
213 - // make sure its a headline.
214 - if (preg_match("/(<h1|<h2|<h3|<h4|<h5|<h6)/i", $innerBlock)) {
215 - $arr[] = $innerBlock;
216 - }
217 - }
464 +/**
465 + * Sanitizes a string to be used as an anchor attribute in HTML by removing punctuation, non-breaking spaces, umlauts, and accents,
466 + * and replacing whitespace and other characters with dashes.
467 + *
468 + * @param string $string_to_sanitize The input string to be sanitized.
469 + * @return string The sanitized string encoded for use in a URL.
470 + */
471 +function simpletoc_sanitize_string( $string_to_sanitize ) {
472 + // remove punctuation.
473 + $zero_punctuation = preg_replace( '/\p{P}/u', '', $string_to_sanitize );
474 + // remove non-breaking spaces.
475 + $html_wo_nbs = str_replace( '&nbsp;', ' ', $zero_punctuation );
476 + // remove umlauts and accents.
477 + $string_without_accents = remove_accents( $html_wo_nbs );
478 + // Sanitizes a title, replacing whitespace and a few other characters with dashes.
479 + // Already returns a URL-safe, percent-encoded slug for non-ASCII input, so no
480 + // further rawurlencode() is needed (that would double-encode the string).
481 + return sanitize_title_with_dashes( $string_without_accents );
482 +}
218 483
219 - if (isset($blocks['blockName']) && ( $blocks['blockName'] === 'generateblocks/headline' ) && $innerBlock !== 'core/heading') {
220 - // make sure its a headline.
221 - if (preg_match("/(<h1|<h2|<h3|<h4|<h5|<h6)/i", $innerBlock)) {
222 - $arr[] = $innerBlock;
223 - }
224 - }
225 - }
226 - }
484 +/**
485 + * Add additional plugin meta links to the SimpleTOC plugin page.
486 + *
487 + * @param array $links An array of plugin meta links.
488 + * @param string $file The plugin file path.
489 + * @return array The modified array of plugin meta links.
490 + */
491 +function simpletoc_plugin_meta( $links, $file ) {
227 492
228 - return $arr;
229 -
493 + if ( false !== strpos( $file, 'simpletoc' ) ) {
494 + $links = array_merge( $links, array( '<a href="https://wordpress.org/support/plugin/simpletoc">' . esc_html__( 'Support', 'simpletoc' ) . '</a>' ) );
495 + $links = array_merge( $links, array( '<a href="https://marc.tv/out/donate">' . esc_html__( 'Donate', 'simpletoc' ) . '</a>' ) );
496 + $links = array_merge( $links, array( '<a href="https://wordpress.org/support/plugin/simpletoc/reviews/#new-post">' . esc_html__( 'Write a review', 'simpletoc' ) . '&nbsp;⭐️⭐️⭐️⭐️⭐️</a>' ) );
497 + }
498 +
499 + return $links;
230 500 }
231 501
232 502 /**
233 - * Remove all problematic characters for toc links
503 + * Loads the WordPress HTML Tag Processor when available.
504 + *
505 + * @return bool True when the HTML Tag Processor can be used.
234 506 */
507 +function simpletoc_load_html_tag_processor() {
508 + if ( class_exists( '\WP_HTML_Tag_Processor' ) ) {
509 + return true;
510 + }
235 511
236 -function simpletoc_sanitize_string( $string )
237 -{
238 - // remove punctuation
239 - $zero_punctuation = preg_replace("/\p{P}/u", "", $string);
240 - // remove non-breaking spaces
241 - $html_wo_nbs = str_replace("&nbsp;", " ", $zero_punctuation);
242 - // remove umlauts and accents
243 - $string_without_accents = remove_accents($html_wo_nbs);
244 - // Sanitizes a title, replacing whitespace and a few other characters with dashes.
245 - $sanitized_string = sanitize_title_with_dashes($string_without_accents);
246 - // Encode for use in an url
247 - $urlencoded = urlencode($sanitized_string);
248 - return $urlencoded;
512 + if ( defined( 'ABSPATH' ) && defined( 'WPINC' ) ) {
513 + $html_tag_processor_file = ABSPATH . WPINC . '/html-api/class-wp-html-tag-processor.php';
514 +
515 + if ( file_exists( $html_tag_processor_file ) ) {
516 + require_once $html_tag_processor_file;
517 + }
518 + }
519 +
520 + return class_exists( '\WP_HTML_Tag_Processor' );
249 521 }
250 522
251 -function simpletoc_plugin_meta( $links, $file )
252 -{
523 +/**
524 + * Creates an HTML Tag Processor for a valid HTML fragment.
525 + *
526 + * Parsed block content can contain non-string placeholders for nested blocks.
527 + * The WordPress HTML API accepts strings only.
528 + *
529 + * @param mixed $html The HTML fragment to inspect.
530 + * @return \WP_HTML_Tag_Processor|null The processor, or null when unavailable or invalid.
531 + */
532 +function simpletoc_get_html_tag_processor( $html ) {
533 + if ( ! is_string( $html ) || ! simpletoc_load_html_tag_processor() ) {
534 + return null;
535 + }
253 536
254 - if (false !== strpos($file, 'simpletoc')) {
255 - $links = array_merge($links, array('<a href="https://wordpress.org/support/plugin/simpletoc">' . __('Support', 'simpletoc') . '</a>'));
256 - $links = array_merge($links, array('<a href="https://marc.tv/out/donate">' . __('Donate', 'simpletoc') . '</a>'));
257 - $links = array_merge($links, array('<a href="https://wordpress.org/support/plugin/simpletoc/reviews/#new-post">' . __('Write a review', 'simpletoc') . '&nbsp;⭐️⭐️⭐️⭐️⭐️</a>'));
258 - }
537 + return new \WP_HTML_Tag_Processor( $html );
538 +}
259 539
260 - return $links;
540 +/**
541 + * Returns true when the provided HTML contains a heading tag.
542 + *
543 + * @param string $html The HTML to inspect.
544 + * @return bool True when the HTML contains a heading tag.
545 + */
546 +function simpletoc_is_heading_html( $html ) {
547 + return false !== simpletoc_get_heading_depth( $html );
261 548 }
262 549
263 -function addAnchorAttribute( $html )
264 -{
550 +/**
551 + * Gets the first heading depth from an HTML fragment.
552 + *
553 + * @param string $html The HTML to inspect.
554 + * @return int|false The heading depth, or false when no heading was found.
555 + */
556 +function simpletoc_get_heading_depth( $html ) {
557 + $processor = simpletoc_get_html_tag_processor( $html );
265 558
266 - // remove non-breaking space entites from input HTML
267 - $html_wo_nbs = str_replace("&nbsp;", " ", $html);
559 + if ( $processor ) {
268 560
269 - // Thank you Nick Diego
270 - if (!$html_wo_nbs) {
271 - return $html;
272 - }
561 + while ( $processor->next_tag() ) {
562 + $tag_name = $processor->get_tag();
273 563
274 - libxml_use_internal_errors(TRUE);
275 - $dom = new \DOMDocument();
276 - @$dom->loadHTML($html_wo_nbs, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
564 + if ( in_array( $tag_name, array( 'H1', 'H2', 'H3', 'H4', 'H5', 'H6' ), true ) ) {
565 + return (int) substr( $tag_name, 1 );
566 + }
567 + }
568 + }
277 569
278 - // use xpath to select the Heading html tags.
279 - $xpath = new \DOMXPath($dom);
280 - $tags = $xpath->evaluate("//*[self::h1 or self::h2 or self::h3 or self::h4 or self::h5 or self::h6]");
570 + return false;
571 +}
281 572
282 - // Loop through all the found tags
283 - foreach ($tags as $tag) {
284 - // if tag already has an attribute "id" defined, no need for creating a new one
285 - if (!empty($tag->getAttribute('id'))) {continue;}
286 - // Set id attribute
287 - $heading_text = strip_tags($html);
288 - $anchor = simpletoc_sanitize_string($heading_text);
289 - $tag->setAttribute("id", $anchor);
290 - }
573 +/**
574 + * Adds a data-page attribute to the first heading in an HTML fragment.
575 + *
576 + * @param string $html The heading HTML.
577 + * @param int $page_number The page number to set.
578 + * @return string The updated HTML.
579 + */
580 +function simpletoc_add_page_number_to_headline( $html, $page_number ) {
581 + $processor = simpletoc_get_html_tag_processor( $html );
291 582
292 - // Save the HTML changes
293 - $content = utf8_decode($dom->saveHTML($dom->documentElement));
583 + if ( $processor ) {
294 584
295 - return $content;
585 + while ( $processor->next_tag() ) {
586 + if ( ! in_array( $processor->get_tag(), array( 'H1', 'H2', 'H3', 'H4', 'H5', 'H6' ), true ) ) {
587 + continue;
588 + }
589 +
590 + $processor->set_attribute( 'data-page', (string) $page_number );
591 + return $processor->get_updated_html();
592 + }
593 + }
594 +
595 + return $html;
296 596 }
297 597
298 -function generateToc( $headings, $attributes )
299 -{
598 +/**
599 + * Checks whether the first heading in an HTML fragment has the provided class.
600 + *
601 + * @param string $html The heading HTML.
602 + * @param string $class_name The class name to find.
603 + * @return bool True when the class exists.
604 + */
605 +function simpletoc_heading_has_class( $html, $class_name ) {
606 + $processor = simpletoc_get_html_tag_processor( $html );
300 607
301 - $list = '';
302 - $html = '';
303 - $min_depth = 6;
304 - $listtype = 'ul';
305 - $absolute_url = '';
306 - $inital_depth = 6;
307 - $link_class = '';
308 - $styles = '';
608 + if ( $processor ) {
309 609
310 - $alignclass = '';
311 - if ( isset ($attributes['align']) ) {
312 - $align = $attributes['align'];
313 - $alignclass = 'align' . $align;
314 - }
610 + while ( $processor->next_tag() ) {
611 + if ( ! in_array( $processor->get_tag(), array( 'H1', 'H2', 'H3', 'H4', 'H5', 'H6' ), true ) ) {
612 + continue;
613 + }
315 614
316 - if ($attributes['remove_indent'] == true) {
317 - $styles = 'style="padding-left:0;list-style:none;"';
318 - }
319 -
320 - if ($attributes['add_smooth'] == true) {
321 - $link_class = 'class="smooth-scroll"';
322 - }
615 + $class_attribute = $processor->get_attribute( 'class' );
323 616
324 - if ($attributes['use_ol'] == true) {
325 - $listtype = 'ol';
326 - }
617 + if ( ! is_string( $class_attribute ) ) {
618 + return false;
619 + }
327 620
328 - if ($attributes['use_absolute_urls'] == true) {
329 - $absolute_url = get_permalink();
330 - }
621 + return in_array( $class_name, preg_split( '/\s+/', trim( $class_attribute ) ), true );
622 + }
623 + }
331 624
332 - foreach ($headings as $line => $headline) {
333 - if ($min_depth > $headings[$line][2]) {
334 - // search for lowest level
335 - $min_depth = (int) $headings[$line][2];
336 - $inital_depth = $min_depth;
337 - }
338 - }
625 + return false;
626 +}
339 627
340 - /* If there is a custom min level, make sure it is the baseline. */
341 - if ($attributes['min_level'] > $min_depth) {
342 - $min_depth = $attributes['min_level'];
343 - }
628 +/**
629 + * Adds an ID attribute to all Heading tags in the provided HTML.
630 + *
631 + * @param string $html The HTML content to modify.
632 + * @param SimpleTOC_Headline_Ids $headline_class_instance The instance of the SimpleTOC_Headline_Ids class.
633 + * @param array $block The parsed block data.
634 + * @return string The modified HTML content with ID attributes added to the Heading tags
635 + */
636 +function add_anchor_attribute( $html, $headline_class_instance = null, $block = array() ) {
637 + if ( ! is_string( $html ) ) {
638 + return $html;
639 + }
344 640
345 - $itemcount = 0;
641 + // remove non-breaking space entites from input HTML.
642 + $html_wo_nbs = str_replace( '&nbsp;', ' ', $html );
346 643
347 - foreach ($headings as $line => $headline) {
644 + // Thank you Nick Diego.
645 + if ( ! $html_wo_nbs ) {
646 + return $html;
647 + }
348 648
349 - $title = strip_tags($headline);
350 - $page = '';
351 - $dom = new \DOMDocument();
352 - @$dom->loadHTML($headline, LIBXML_HTML_NOIMPLIED | LIBXML_HTML_NODEFDTD);
353 - $xpath = new \DOMXPath($dom);
354 - $nodes = $xpath->query('//*/@data-page');
649 + $processor = simpletoc_get_html_tag_processor( $html_wo_nbs );
355 650
356 - if ( isset($nodes[0] ) && $nodes[0]->nodeValue > 1) {
357 - $page = $nodes[0]->nodeValue . '/';
358 - $absolute_url = get_permalink();
359 - }
651 + if ( $processor ) {
360 652
361 - $link = simpletoc_sanitize_string($title);
362 - if (isset($nodes[0]) && !empty($nodes[0]->ownerElement->getAttribute('id'))) {
363 - // if the node already has an attribute id, use that as anchor
364 - $link = $nodes[0]->ownerElement->getAttribute('id');
365 - }
653 + while ( $processor->next_tag() ) {
654 + if ( ! in_array( $processor->get_tag(), array( 'H1', 'H2', 'H3', 'H4', 'H5', 'H6' ), true ) ) {
655 + continue;
656 + }
366 657
367 - $this_depth = (int) $headings[$line][2];
368 - if (isset($headings[$line + 1][2])) {
369 - $next_depth = (int) $headings[$line + 1][2];
370 - } else {
371 - $next_depth = '';
372 - }
658 + // If tag already has an attribute "id" defined, no need for creating a new one.
659 + if ( ! empty( $processor->get_attribute( 'id' ) ) ) {
660 + continue;
661 + }
373 662
374 - // skip this heading because a max depth is set.
375 - if ($this_depth > $attributes['max_level'] or strpos($headline, 'class="simpletoc-hidden') > 0) {
376 - goto closelist;
377 - }
663 + $heading_html = simpletoc_get_heading_html_for_anchor( $html, $block );
664 + $heading_text = trim( wp_strip_all_tags( $heading_html ) );
665 + $anchor = $headline_class_instance->get_headline_anchor( $heading_text );
666 + $processor->set_attribute( 'id', $anchor );
667 + }
378 668
379 - // skip this heading because a min depth is set.
380 - if( $this_depth < $attributes['min_level'] ){
381 - continue;
382 - }
669 + return $processor->get_updated_html();
670 + }
383 671
384 - $itemcount++;
672 + return $html;
673 +}
385 674
386 - // start list
387 - if ($this_depth == $min_depth) {
388 - $list .= "<li>\n";
389 - } else {
390 - // we are not as base level. Start opening levels until base is reached.
391 - for ($min_depth; $min_depth < $this_depth; $min_depth++) {
392 - $list .= "\n\t\t<" . $listtype . "><li>\n";
393 - }
394 - }
395 -
396 - $list .= "<a " . $link_class . " href=\"" . $absolute_url . esc_html($page) . "#" . $link . "\">" . $title . "</a>";
675 +/**
676 + * Generates a table of contents based on the provided headings and attributes
677 + *
678 + * @param array $headings An array of headings to include in the table of contents.
679 + * @param array $attributes An array of attributes to customize the output.
680 + * @return string The generated table of contents as HTML
681 + */
682 +function generate_toc( $headings, $attributes ) {
683 + $list = '';
684 + $html = '';
685 + $min_depth = 6;
686 + $initial_depth = 6;
687 + $align_class = isset( $attributes['align'] ) ? 'align' . $attributes['align'] : '';
688 + $styles = $attributes['remove_indent'] ? 'style="padding-left:0;list-style:none;"' : '';
689 + $list_type = $attributes['use_ol'] ? 'ol' : 'ul';
690 + $global_absolut_urls_enabled = get_option( 'simpletoc_absolute_urls_enabled', false );
691 + $absolute_url = $attributes['use_absolute_urls'] || $global_absolut_urls_enabled ? get_permalink() : '';
397 692
398 - closelist:
399 - // close lists
400 - // check if this is not the last heading
401 - if ($line != count($headings) - 1) {
402 - // do we need to close the door behind us?
403 - if ($min_depth > $next_depth) {
404 - // If yes, how many times?
405 - for ($min_depth; $min_depth > $next_depth; $min_depth--) {
406 - $list .= "</li></" . $listtype . ">\n";
407 - }
408 - }
409 - if ($min_depth == $next_depth) {
410 - $list .= "</li>";
411 - }
412 - // last heading
413 - } else {
414 - for ($inital_depth; $inital_depth < $this_depth; $inital_depth++) {
415 - $list .= "</li></" . $listtype . ">\n";
416 - }
417 - }
418 - }
693 + $toc_headings = get_included_toc_headings( $headings, $attributes );
694 + list($min_depth, $initial_depth) = find_min_depth( $toc_headings, $attributes );
419 695
420 - if ($attributes['no_title'] == false) {
421 - $html = "<h2 class=\"simpletoc-title\">" . __("Table of Contents", "simpletoc") . "</h2>";
422 - }
423 - $html .= "<" . $listtype . " class=\"simpletoc-list\" " . $styles ." " . $alignclass .">\n" . $list . "</li></" . $listtype . ">";
696 + $item_count = count( $toc_headings );
697 + $list = render_toc_list_items( $toc_headings, $list_type, $absolute_url, $min_depth );
424 698
425 - if($itemcount < 1) {
426 - $html = '';
427 - }
699 + $html = add_accordion_start( $html, $attributes, $item_count, $align_class );
700 + $html = add_hidden_markup_start( $html, $attributes, $item_count, $align_class );
701 + $html = add_smooth( $html, $attributes );
428 702
429 - return $html;
703 + // Add the table of contents list to the output if the list is not empty.
704 + if ( ! empty( $list ) ) {
705 + $html_class = 'simpletoc-list';
706 + if ( ! empty( $align_class ) ) {
707 + $html_class .= " $align_class";
708 + }
709 +
710 + $html_style = '';
711 + if ( ! empty( $styles ) ) {
712 + $html_style = " $styles";
713 + }
714 +
715 + $html .= "<$list_type class=\"$html_class\"$html_style>\n$list</$list_type>";
716 + }
717 +
718 + $html = add_accordion_end( $html, $attributes );
719 + $html = add_hidden_markup_end( $html, $attributes );
720 +
721 + // return an emtpy string if stripped result is empty.
722 + if ( empty( trim( wp_strip_all_tags( $html ) ) ) ) {
723 + $html = '';
724 + }
725 +
726 + return $html;
727 +}
728 +
729 +/**
730 + * Finds the minimum depth level of headings in the provided array and adjusts it based on the provided attributes
731 + *
732 + * @param array $headings An array of headings to search through.
733 + * @param array $attributes An array of attributes to adjust the minimum depth level.
734 + * @return array An array containing the minimum depth level and the initial depth level.
735 + */
736 +function find_min_depth( $headings, $attributes ) {
737 + $min_depth = 6;
738 + $initial_depth = 6;
739 +
740 + foreach ( $headings as $line => $headline ) {
741 + $this_depth = is_array( $headline ) && isset( $headline['depth'] ) ? (int) $headline['depth'] : (int) $headings[ $line ][2];
742 + if ( $min_depth > $this_depth ) {
743 + $min_depth = $this_depth;
744 + $initial_depth = $min_depth;
745 + }
746 + }
747 +
748 + if ( $attributes['min_level'] > $min_depth ) {
749 + $min_depth = $attributes['min_level'];
750 + $initial_depth = $min_depth;
751 + }
752 +
753 + return array( $min_depth, $initial_depth );
754 +}
755 +
756 +/**
757 + * Determines if a given headline should be excluded based on the provided attributes.
758 + *
759 + * @param string $headline The headline to check for exclusion.
760 + * @param array $attributes An array of attributes to use for exclusion.
761 + * @param int $this_depth The depth level of the headline.
762 + * @return bool True if the headline should be excluded, false otherwise.
763 + */
764 +function should_exclude_headline( $headline, $attributes, $this_depth ) {
765 + $exclude_headline = simpletoc_heading_has_class( $headline, 'simpletoc-hidden' );
766 +
767 + return ( $this_depth > $attributes['max_level'] || $exclude_headline || $this_depth < $attributes['min_level'] );
768 +}
769 +
770 +/**
771 + * Filters headings down to TOC-visible entries while preserving anchor generation order.
772 + *
773 + * @param array $headings An array of headings to include in the table of contents.
774 + * @param array $attributes An array of attributes to customize the output.
775 + * @return array[] The headings that should be rendered in the table of contents.
776 + */
777 +function get_included_toc_headings( $headings, $attributes ) {
778 + $toc_headings = array();
779 + $headline_ids = new SimpleTOC_Headline_Ids();
780 +
781 + foreach ( $headings as $headline ) {
782 + $this_depth = simpletoc_get_heading_depth( $headline );
783 +
784 + if ( false === $this_depth ) {
785 + continue;
786 + }
787 +
788 + $title = trim( wp_strip_all_tags( $headline ) );
789 + $custom_id = extract_id( $headline );
790 + $link = $custom_id ? $custom_id : $headline_ids->get_headline_anchor( $title );
791 +
792 + if ( should_exclude_headline( $headline, $attributes, $this_depth ) ) {
793 + continue;
794 + }
795 +
796 + $toc_headings[] = array(
797 + 'headline' => $headline,
798 + 'depth' => $this_depth,
799 + 'title' => $title,
800 + 'link' => $link,
801 + );
802 + }
803 +
804 + return $toc_headings;
805 +}
806 +
807 +/**
808 + * Renders nested TOC list item markup from already-filtered headings.
809 + *
810 + * @param array[] $toc_headings The headings that should be rendered in the table of contents.
811 + * @param string $list_type The type of list to be created, either "ul" (unordered list) or "ol".
812 + * @param string $absolute_url The optional absolute URL prefix for links.
813 + * @param int $min_depth The minimum heading depth included in the table of contents.
814 + * @return string The rendered nested list item markup.
815 + */
816 +function render_toc_list_items( $toc_headings, $list_type, $absolute_url, $min_depth ) {
817 + $list = '';
818 + $current_depth = null;
819 +
820 + foreach ( $toc_headings as $toc_heading ) {
821 + $this_depth = $toc_heading['depth'];
822 +
823 + if ( null === $current_depth ) {
824 + $current_depth = $this_depth;
825 + $list .= '<li>';
826 + } elseif ( $this_depth > $current_depth ) {
827 + for ( $current_depth; $current_depth < $this_depth; $current_depth++ ) {
828 + $list .= "\n<" . $list_type . ">\n<li>";
829 + }
830 + } elseif ( $this_depth === $current_depth ) {
831 + $list .= "</li>\n<li>";
832 + } else {
833 + for ( $current_depth; $current_depth > $this_depth; $current_depth-- ) {
834 + $list .= "</li>\n</" . $list_type . ">\n";
835 + }
836 + $list .= "</li>\n<li>";
837 + }
838 +
839 + $page = get_page_number_from_headline( $toc_heading['headline'] );
840 + $url = $absolute_url . $page . '#' . $toc_heading['link'];
841 + $href = $absolute_url ? esc_url( $url ) : esc_attr( $url );
842 + $list .= '<a href="' . $href . '">' . esc_html( $toc_heading['title'] ) . '</a>' . PHP_EOL;
843 + }
844 +
845 + if ( null !== $current_depth ) {
846 + for ( $current_depth; $current_depth > $min_depth; $current_depth-- ) {
847 + $list .= "</li>\n</" . $list_type . ">\n";
848 + }
849 + $list .= '</li>';
850 + }
851 +
852 + return $list;
853 +}
854 +
855 +/**
856 + * Adds smooth scrolling styles to the output HTML, if enabled by global option or block attribute.
857 + *
858 + * @param string $html The HTML string to which the styles will be added.
859 + * @param array $attributes An array of block attributes.
860 + * @return string The modified HTML string with the added smooth scrolling styles.
861 + */
862 +function add_smooth( $html, $attributes ) {
863 + // Add smooth scrolling styles, if enabled by global option or block attribute.
864 + $is_smooth_enabled = $attributes['add_smooth'] || true === (bool) get_option( 'simpletoc_smooth_enabled', false );
865 + $html .= $is_smooth_enabled ? '<style>html { scroll-behavior: smooth; }</style>' : '';
866 +
867 + return $html;
868 +}
869 +
870 +/**
871 + * Enqueues the necessary CSS and JS files for the accordion functionality on the frontend.
872 + */
873 +function enqueue_accordion_frontend() {
874 + wp_enqueue_script(
875 + 'simpletoc-accordion',
876 + plugin_dir_url( __FILE__ ) . 'assets/accordion.js',
877 + array(),
878 + SIMPLETOC_VERSION,
879 + true
880 + );
881 +
882 + wp_enqueue_style(
883 + 'simpletoc-accordion',
884 + plugin_dir_url( __FILE__ ) . 'assets/accordion.css',
885 + array(),
886 + SIMPLETOC_VERSION
887 + );
888 +}
889 +
890 +/**
891 + * Adds the opening HTML tag(s) for the hidden markup element and the table of contents title, if applicable.
892 + *
893 + * @param string $html The HTML string to add the opening tag(s) to.
894 + * @param array $attributes The attributes of the table of contents block.
895 + * @param int $itemcount The number of items in the table of contents.
896 + * @param string $alignclass The alignment class for the table of contents block.
897 + */
898 +function add_hidden_markup_start( $html, $attributes, $itemcount, $alignclass ) { // phpcs:ignore.
899 + $is_hidden_enabled = $attributes['hidden'];
900 +
901 + if ( $is_hidden_enabled ) {
902 + $title_text = $attributes['title_text'] ? esc_html( trim( $attributes['title_text'] ) ) : esc_html__( 'Table of Contents', 'simpletoc' );
903 + $hidden_start = '<details class="simpletoc">
904 + <summary>' . $title_text . '</summary>';
905 + $html .= $hidden_start;
906 + }
907 +
908 + // If there are no items in the table of contents, return an empty string.
909 + if ( $itemcount < 1 ) {
910 + return '';
911 + }
912 +
913 + return $html;
914 +}
915 +
916 +/**
917 + * Adds the closing HTML tag(s) for the hidden markup element if the hidden markup is enabled.
918 + *
919 + * @param string $html The HTML string to add the closing tag(s) to.
920 + * @param array $attributes The attributes of the table of contents block.
921 + * @return string The modified HTML string with the closing tag(s) added.
922 + */
923 +function add_hidden_markup_end( $html, $attributes ) {
924 + $is_hidden_enabled = $attributes['hidden'];
925 +
926 + if ( $is_hidden_enabled ) {
927 + $html .= '</details>';
928 + }
929 +
930 + return $html;
931 +}
932 +
933 +/**
934 + * Adds the opening HTML tag(s) for the accordion element and the table of contents title, if applicable.
935 + *
936 + * @param string $html The HTML string to add the opening tag(s) to.
937 + * @param array $attributes The attributes of the table of contents block.
938 + * @param int $itemcount The number of items in the table of contents.
939 + * @param string $alignclass The alignment class for the table of contents block.
940 + */
941 +function add_accordion_start( $html, $attributes, $itemcount, $alignclass ) {
942 + // Check if accordion is enabled either through the function arguments or the options.
943 + $is_accordion_enabled = $attributes['accordion'] || true === (bool) get_option( 'simpletoc_accordion_enabled', false );
944 + $is_hidden_enabled = $attributes['hidden'];
945 + $title_text = $attributes['title_text'] ? esc_html( trim( $attributes['title_text'] ) ) : esc_html__( 'Table of Contents', 'simpletoc' );
946 +
947 + // Start and end HTML for accordion, if enabled.
948 + $accordion_start = '';
949 + if ( $is_accordion_enabled ) {
950 + enqueue_accordion_frontend();
951 + $accordion_start = '<h2 class="simpletoc-accordion-heading"><button type="button" aria-expanded="false" aria-controls="simpletoc-content-container" class="simpletoc-collapsible">' . $title_text . '<span class="simpletoc-icon" aria-hidden="true"></span></button></h2><div id="simpletoc-content-container" class="simpletoc-content">';
952 + }
953 +
954 + // Add the accordion start HTML to the output.
955 + $html .= $accordion_start;
956 +
957 + // Add the table of contents title, if not hidden and not in accordion mode.
958 + $show_title = ! $attributes['no_title'] && ! $is_accordion_enabled && ! $is_hidden_enabled;
959 + if ( $show_title ) {
960 + $title_tag = $attributes['title_level'] > 0 ? "h{$attributes['title_level']}" : 'p';
961 + $title_tag = wp_strip_all_tags( $title_tag );
962 + $html_class = 'simpletoc-title';
963 +
964 + if ( ! empty( $alignclass ) ) {
965 + $html_class .= " $alignclass";
966 + }
967 +
968 + $html = "<$title_tag class=\"$html_class\">$title_text</$title_tag>\n";
969 + }
970 +
971 + // If there are no items in the table of contents, return an empty string.
972 + if ( $itemcount < 1 ) {
973 + return '';
974 + }
975 +
976 + return $html;
977 +}
978 +
979 +/**
980 + * Adds the closing HTML tag(s) for the accordion element if the accordion is enabled.
981 + *
982 + * @param string $html The HTML string to add the closing tag(s) to.
983 + * @param array $attributes The attributes of the table of contents block.
984 + * @return string The modified HTML string with the closing tag(s) added
985 + */
986 +function add_accordion_end( $html, $attributes ) {
987 + // Check if accordion is enabled either through the function arguments or the options.
988 + $is_accordion_enabled = $attributes['accordion'] || true === (bool) get_option( 'simpletoc_accordion_enabled', false );
989 +
990 + if ( $is_accordion_enabled ) {
991 + $html .= '</div>';
992 + }
993 +
994 + return $html;
995 +}
996 +
997 +/**
998 + * Extracts the ID value from the provided heading HTML string.
999 + *
1000 + * @param string $headline The heading HTML string to extract the ID value from.
1001 + * @return string|false Returns the extracted ID value, or false if no ID value is found.
1002 + */
1003 +function extract_id( $headline ) {
1004 + $processor = simpletoc_get_html_tag_processor( $headline );
1005 +
1006 + if ( $processor ) {
1007 +
1008 + while ( $processor->next_tag() ) {
1009 + if ( ! in_array( $processor->get_tag(), array( 'H1', 'H2', 'H3', 'H4', 'H5', 'H6' ), true ) ) {
1010 + continue;
1011 + }
1012 +
1013 + $id_value = $processor->get_attribute( 'id' );
1014 +
1015 + if ( is_string( $id_value ) ) {
1016 + return $id_value;
1017 + }
1018 + }
1019 + }
1020 +
1021 + return false;
1022 +}
1023 +
1024 +/**
1025 + * Gets the page number from a headline string.
1026 + *
1027 + * @param string $headline The headline string.
1028 + * @return string The page number (in the format "X/") if it exists and is greater than 1, or an empty string otherwise.
1029 + */
1030 +function get_page_number_from_headline( $headline ) {
1031 + $processor = simpletoc_get_html_tag_processor( $headline );
1032 +
1033 + if ( $processor ) {
1034 +
1035 + while ( $processor->next_tag() ) {
1036 + if ( ! in_array( $processor->get_tag(), array( 'H1', 'H2', 'H3', 'H4', 'H5', 'H6' ), true ) ) {
1037 + continue;
1038 + }
1039 +
1040 + $page_number = (int) $processor->get_attribute( 'data-page' );
1041 +
1042 + if ( $page_number > 1 ) {
1043 + return esc_html( $page_number . '/' );
1044 + }
1045 + }
1046 +
1047 + return '';
1048 + }
1049 +
1050 + return '';
430 1051 }