Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions plugin.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@

require_once __DIR__ . '/simpletoc-admin-settings.php';
require_once __DIR__ . '/simpletoc-class-headline-ids.php';
require_once __DIR__ . '/simpletoc-class-headline-ids-wrapper.php';

/**
* Prevents direct execution of the plugin file.
Expand Down Expand Up @@ -166,6 +167,8 @@ function simpletoc_add_ids_to_content( $content ) {

add_filter( 'the_content', __NAMESPACE__ . '\simpletoc_add_ids_to_content', 1 );



/**
* Recursively adds IDs to the headings of a nested block structure.
*
Expand All @@ -187,9 +190,9 @@ function add_ids_to_blocks_recursive( $blocks ) {
*/
$supported_blocks = apply_filters( 'simpletoc_supported_blocks_for_ids', $supported_blocks );

// Need two separate instances so that IDs aren't double coubnted.
$inner_html_id_instance = new SimpleTOC_Headline_Ids();
$inner_content_id_instance = new SimpleTOC_Headline_Ids();
// Need two separate instances so that IDs aren't double counted.
$inner_html_id_instance = SimpleTOC_Headline_Ids_Wrapper::get_inner_html_id_instance();
$inner_content_id_instance = SimpleTOC_Headline_Ids_Wrapper::get_inner_content_id_instance();

foreach ( $blocks as &$block ) {
if ( isset( $block['blockName'] ) && in_array( $block['blockName'], $supported_blocks, true ) && isset( $block['innerHTML'] ) && isset( $block['innerContent'] ) && isset( $block['innerContent'][0] ) ) {
Expand Down
5 changes: 5 additions & 0 deletions simpletoc-admin-settings.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,11 @@

namespace MToensing\SimpleTOC;

if ( ! defined( 'ABSPATH' ) ) {
header( 'Status: 403 Forbidden' );
header( 'HTTP/1.1 403 Forbidden' );
exit;
}
/**
* Add SimpleTOC global settings page.
*/
Expand Down
57 changes: 57 additions & 0 deletions simpletoc-class-headline-ids-wrapper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
<?php
/**
* Class to manage headline IDs for the wrapper.
*
* @package simpletoc
*/

namespace MToensing\SimpleTOC;

if ( ! defined( 'ABSPATH' ) ) {
header( 'Status: 403 Forbidden' );
header( 'HTTP/1.1 403 Forbidden' );
exit;
}

/**
* Wrapper for the IDs. This ensures that IDs are not double-counted or lost in recursion.
*/
class SimpleTOC_Headline_Ids_Wrapper {
/**
* The instance of the SimpleTOC_Headline_Ids class for the wrapper.
*
* @var SimpleTOC_Headline_Ids
*/
private static $inner_content_id_instance = null;

/**
* The instance of the SimpleTOC_Headline_Ids class for the wrapper.
*
* @var SimpleTOC_Headline_Ids
*/
private static $inner_html_id_instance = null;

/**
* Get the instance of the SimpleTOC_Headline_Ids class for the wrapper for inner content.
*
* @return SimpleTOC_Headline_Ids
*/
public static function get_inner_content_id_instance() {
if ( null === self::$inner_content_id_instance ) {
self::$inner_content_id_instance = new SimpleTOC_Headline_Ids();
}
return self::$inner_content_id_instance;
}

/**
* Get the instance of the SimpleTOC_Headline_Ids class for the wrapper for inner HTML.
*
* @return SimpleTOC_Headline_Ids
*/
public static function get_inner_html_id_instance() {
if ( null === self::$inner_html_id_instance ) {
self::$inner_html_id_instance = new SimpleTOC_Headline_Ids();
}
return self::$inner_html_id_instance;
}
}
6 changes: 6 additions & 0 deletions simpletoc-class-headline-ids.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,12 @@

namespace MToensing\SimpleTOC;

if ( ! defined( 'ABSPATH' ) ) {
header( 'Status: 403 Forbidden' );
header( 'HTTP/1.1 403 Forbidden' );
exit;
}

/**
* Class to manage headline IDs.
*
Expand Down