*/ // Exit if accessed directly if (!defined('ABSPATH')) { exit; } /** * Gutenberg Block class. */ class AJEJSMTA_Block { /** * Constructor. */ public function __construct() { $this->init_hooks(); } /** * Initialize hooks. * * @return void */ private function init_hooks() { add_action('init', array($this, 'register_block')); add_action('enqueue_block_editor_assets', array($this, 'enqueue_block_assets')); } /** * Enqueue block editor assets. * * @return void */ public function enqueue_block_assets() { // Enqueue block editor JavaScript wp_enqueue_script( 'ajejsmta-block-editor', AJEJSMTA_URL . 'assets/js/block-editor.js', array('wp-blocks', 'wp-element', 'wp-editor', 'wp-components', 'wp-i18n', 'wp-server-side-render'), AJEJSMTA_VERSION, true ); // Enqueue frontend CSS for block preview in editor wp_enqueue_style( 'ajejsmta-frontend-preview', AJEJSMTA_URL . 'assets/css/frontend.css', array(), AJEJSMTA_VERSION ); } /** * Register the Gutenberg block. * * @return void */ public function register_block() { // Check if Gutenberg is available if (!function_exists('register_block_type')) { return; } // Register block register_block_type('ajejey-smart-toc/toc', array( 'render_callback' => array($this, 'render_block'), 'attributes' => array( 'template' => array( 'type' => 'string', 'default' => 'minimal', ), 'showToggle' => array( 'type' => 'boolean', 'default' => true, ), 'headingLevels' => array( 'type' => 'array', 'default' => array('h2', 'h3', 'h4'), ), 'minHeadings' => array( 'type' => 'number', 'default' => 3, ), ), )); } /** * Render the block. * * @param array $attributes Block attributes. * @return string Block HTML. */ public function render_block($attributes) { global $post; if (!$post) { return ''; } // Get the frontend instance $frontend = new AJEJSMTA_Frontend(); // Generate TOC with custom attributes if provided $toc = $this->generate_custom_toc($post->post_content, $attributes); return $toc; } /** * Generate TOC with custom attributes. * * @param string $content Post content. * @param array $attributes Block attributes. * @return string TOC HTML. */ private function generate_custom_toc($content, $attributes) { // Extract headings based on block attributes $headings = $this->extract_headings($content, $attributes); if (empty($headings)) { return ''; } // Check minimum heading requirement $min_headings = isset($attributes['minHeadings']) ? (int) $attributes['minHeadings'] : 3; if (count($headings) < $min_headings) { return ''; } // Build TOC HTML return $this->build_toc_html($headings, $attributes); } /** * Extract headings from content. * * @param string $content Post content. * @param array $attributes Block attributes. * @return array Array of headings. */ private function extract_headings($content, $attributes) { $headings = array(); $used_ids = array(); // Get allowed heading levels from attributes $allowed_levels = isset($attributes['headingLevels']) ? $attributes['headingLevels'] : array('h2', 'h3', 'h4'); // Create regex pattern $pattern = '/<(' . implode('|', $allowed_levels) . ')[^>]*>(.*?)<\/\1>/i'; // Find all headings if (preg_match_all($pattern, $content, $matches, PREG_SET_ORDER)) { foreach ($matches as $match) { $level = strtolower($match[1]); $text = strip_tags($match[2]); $id = $this->generate_heading_id($text, $used_ids); $headings[] = array( 'level' => $level, 'text' => $text, 'id' => $id, ); } } return $headings; } /** * Generate unique ID for heading. * * @param string $text Heading text. * @param array $used_ids Array of already used IDs. * @return string Unique ID. */ private function generate_heading_id($text, &$used_ids) { // Convert to lowercase with UTF-8 support $id = mb_strtolower($text, 'UTF-8'); // Replace spaces and special chars $id = preg_replace('/[^\p{L}\p{N}\s-]/u', '', $id); $id = preg_replace('/\s+/', '-', $id); $id = preg_replace('/-+/', '-', $id); $id = trim($id, '-'); // Ensure starts with letter if (!empty($id) && !preg_match('/^[a-z]/i', $id)) { $id = 'heading-' . $id; } $id = 'toc-' . $id; // Handle empty if (empty($id) || $id === 'toc-') { $id = 'toc-heading-' . (count($used_ids) + 1); } // Handle duplicates $original_id = $id; $counter = 2; while (in_array($id, $used_ids)) { $id = $original_id . '-' . $counter; $counter++; } $used_ids[] = $id; return $id; } /** * Build TOC HTML. * * @param array $headings Array of headings. * @param array $attributes Block attributes. * @return string TOC HTML. */ private function build_toc_html($headings, $attributes) { if (empty($headings)) { return ''; } $template = isset($attributes['template']) ? $attributes['template'] : 'minimal'; $show_toggle = isset($attributes['showToggle']) ? $attributes['showToggle'] : true; // Start container $html = '
'; // Header $html .= '
'; $html .= '

' . esc_html__('Table of Contents', 'ajejey-smart-toc') . '

'; if ($show_toggle) { $html .= ''; } $html .= '
'; // List $html .= ''; $html .= '
'; return $html; } }