*/ namespace RankMath\Schema; use WP_Block_Type_Registry; use RankMath\Helper; use RankMath\Traits\Hooker; defined( 'ABSPATH' ) || exit; /** * HowTo Block class. */ class Block_TOC extends Block { use Hooker; /** * Block type name. * * @var string */ private $block_type = 'rank-math/toc-block'; /** * The single instance of the class. * * @var Block_TOC */ protected static $instance = null; /** * Retrieve main Block_TOC instance. * * Ensure only one instance is loaded or can be loaded. * * @return Block_TOC */ public static function get() { if ( is_null( self::$instance ) && ! ( self::$instance instanceof Block_TOC ) ) { self::$instance = new Block_TOC(); } return self::$instance; } /** * The Constructor. */ public function __construct() { if ( WP_Block_Type_Registry::get_instance()->is_registered( $this->block_type ) ) { return; } $this->filter( 'rank_math/schema/block/toc-block', 'add_graph', 10, 2 ); $this->filter( 'render_block_rank-math/toc-block', 'render_toc_block_content', 10, 2 ); $this->filter( 'rank_math/metabox/post/values', 'block_settings_metadata' ); register_block_type( RANK_MATH_PATH . 'includes/modules/schema/blocks/toc/block.json' ); } /** * Add meta data to use in the TOC block. * * @param array $values Aray of tabs. * * @return array */ public function block_settings_metadata( $values ) { $values['tocTitle'] = Helper::get_settings( 'general.toc_block_title' ); $values['tocExcludeHeadings'] = Helper::get_settings( 'general.toc_block_exclude_headings', [] ); $values['listStyle'] = Helper::get_settings( 'general.toc_block_list_style', 'ul' ); return $values; } /** * Add default TOC title. * * @param string $block_content Block content. * @param array $parsed_block The full block, including name and attributes. * * @return string */ public function render_toc_block_content( $block_content, $parsed_block ) { if ( isset( $parsed_block['attrs']['title'] ) ) { return $block_content; } $title = Helper::get_settings( 'general.toc_block_title' ); if ( ! $title ) { return $block_content; } $title_wrapper = $parsed_block['attrs']['titleWrapper'] ?? 'h2'; $block_content = preg_replace_callback( '/(