Files
roi-theme/wp-content/themes/apus-theme/inc/toc.php
FrankZamora 7fa51e2462 [FASE 3] Completar INSTALACIONES - Issues #86-90
Implementación y optimización completa de componentes FASE 3: TOC, CTA A/B Testing, Modal, Related Posts/Share, y JavaScript.

**Issue #86 - TOC (Table of Contents) Completo:**
- sidebar.php: Integrado TOC directamente en sidebar
- inc/toc.php: Eliminado hook innecesario apus_display_toc()
- TOC funcional con ScrollSpy IntersectionObserver
- Sticky positioning (top: 5.5rem)
- Scrollbar personalizado (6px, #cbd5e0)
- Smooth scroll con prefers-reduced-motion

**Issue #87 - CTA A/B Testing Validado:**
- Rotación 50/50 con Math.random() < 0.5 
- Solo una variante visible 
- Tracking Google Analytics con gtag() 
- 2 variantes: A (Catálogo) y B (Membresía) 

**Issue #88 - Modal de Contacto Validado:**
- Carga dinámica con fetch() 
- Validación campos obligatorios y email regex 
- Estados del botón (spinner) 
- Cierre automático después de 2s 
- Tracking GA 
- ⚠️ PENDIENTE: Configurar URL webhook real

**Issue #90 - Related Posts y Share Buttons Validados:**
- Related Posts: 12 posts, fondo #f8f9fa 
- Paginación: 8 items 
- Share Buttons: 6 redes con URLs correctas 

**Issue #89 - Optimización JavaScript:**
- Eliminado código duplicado de TOC (63 líneas)
- TOC ahora manejado solo por toc.js (superior)
- Agregados comentarios explicativos
- Event listeners verificados (sin memory leaks)
- Sintaxis PHP validada: 0 errores

**Estadísticas:**
- Archivos modificados: 3 (sidebar.php, inc/toc.php, main.js)
- Archivos creados: 1 (FASE-3-COMPLETION-REPORT.md)
- Código eliminado: 107 líneas
- Código agregado: 25 líneas
- Net: -82 líneas (código más limpio)
- Issues completados: 5 (#86, #87, #88, #89, #90)

**Validación:**
 Sintaxis PHP: 0 errores (sidebar.php, inc/toc.php)
 TOC funcional con ScrollSpy
 CTA A/B Testing con tracking
 Modal con validación completa
 Related Posts y Share Buttons funcionales
 JavaScript optimizado

**Configuraciones pendientes:**
⚠️ URL de webhook en main.js (líneas 79 y 163)

Closes #86, #87, #88, #89, #90
Relacionado con: #85 (FASE 3 principal)

🤖 Generated with [Claude Code](https://claude.com/claude-code)

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-05 22:48:09 -06:00

203 lines
5.6 KiB
PHP

<?php
/**
* Table of Contents (TOC) Functions
*
* This file contains functions to automatically generate a table of contents
* from post content headings (H2 and H3).
*
* @package Apus_Theme
* @since 1.0.0
*/
// Exit if accessed directly
if (!defined('ABSPATH')) {
exit;
}
/**
* Extract headings from post content
*
* Parses the content and extracts all H2 and H3 headings with their text.
*
* @param string $content Post content
* @return array Array of headings with level, text, and ID
*/
function apus_extract_headings($content) {
if (empty($content)) {
return array();
}
$headings = array();
// Match H2 and H3 tags with their content
preg_match_all('/<h([23])(?:[^>]*)>(.*?)<\/h\1>/i', $content, $matches, PREG_SET_ORDER);
foreach ($matches as $index => $match) {
$level = (int) $match[1]; // 2 or 3
$text = strip_tags($match[2]); // Remove any HTML tags inside heading
// Generate a clean ID from the heading text
$id = apus_generate_heading_id($text, $index);
$headings[] = array(
'level' => $level,
'text' => $text,
'id' => $id,
);
}
return $headings;
}
/**
* Generate a clean ID for a heading
*
* Creates a URL-friendly ID from heading text.
*
* @param string $text Heading text
* @param int $index Index of the heading (for uniqueness)
* @return string Clean ID
*/
function apus_generate_heading_id($text, $index) {
// Remove special characters and convert to lowercase
$id = sanitize_title($text);
// If ID is empty, use a fallback
if (empty($id)) {
$id = 'heading-' . $index;
}
return $id;
}
/**
* Generate HTML for Table of Contents
*
* Creates a nested list structure from the headings array.
*
* @param array $headings Array of headings from apus_extract_headings()
* @return string HTML for the table of contents
*/
function apus_generate_toc($headings) {
// Get minimum headings required from theme options
$min_headings = (int) apus_get_option('toc_min_headings', 2);
if (empty($headings) || count($headings) < $min_headings) {
return ''; // Don't show TOC if there are fewer headings than required
}
// Get custom TOC title from theme options
$toc_title = apus_get_toc_title();
$toc_html = '<nav class="apus-toc" aria-label="' . esc_attr($toc_title) . '">';
$toc_html .= '<div class="apus-toc-header">';
$toc_html .= '<h2 class="apus-toc-title">' . esc_html($toc_title) . '</h2>';
$toc_html .= '<button class="apus-toc-toggle" aria-expanded="true" aria-controls="apus-toc-list">';
$toc_html .= '<span class="toggle-icon" aria-hidden="true"></span>';
$toc_html .= '<span class="screen-reader-text">' . esc_html__('Toggle Table of Contents', 'apus-theme') . '</span>';
$toc_html .= '</button>';
$toc_html .= '</div>';
$toc_html .= '<ol class="apus-toc-list" id="apus-toc-list">';
$current_level = 2;
$open_sublists = 0;
foreach ($headings as $index => $heading) {
$level = $heading['level'];
$text = esc_html($heading['text']);
$id = esc_attr($heading['id']);
// Handle level changes
if ($level > $current_level) {
// Open nested list for H3
$toc_html .= '<ol class="apus-toc-sublist">';
$open_sublists++;
} elseif ($level < $current_level && $open_sublists > 0) {
// Close nested list when going back to H2
$toc_html .= '</li></ol></li>';
$open_sublists--;
} elseif ($index > 0) {
// Close previous item
$toc_html .= '</li>';
}
$toc_html .= '<li class="apus-toc-item apus-toc-level-' . $level . '">';
$toc_html .= '<a href="#' . $id . '" class="apus-toc-link">' . $text . '</a>';
$current_level = $level;
}
// Close any open lists
$toc_html .= '</li>';
while ($open_sublists > 0) {
$toc_html .= '</ol></li>';
$open_sublists--;
}
$toc_html .= '</ol>';
$toc_html .= '</nav>';
return $toc_html;
}
/**
* Add IDs to headings in content
*
* Modifies the post content to add ID attributes to H2 and H3 headings.
*
* @param string $content Post content
* @return string Modified content with IDs added to headings
*/
function apus_add_heading_ids($content) {
if (empty($content)) {
return $content;
}
// Extract headings first to get consistent IDs
$headings = apus_extract_headings($content);
if (empty($headings)) {
return $content;
}
// Replace headings with versions that include IDs
$heading_index = 0;
$content = preg_replace_callback(
'/<h([23])(?:[^>]*)>(.*?)<\/h\1>/i',
function($matches) use ($headings, &$heading_index) {
if (!isset($headings[$heading_index])) {
return $matches[0];
}
$level = $matches[1];
$text = $matches[2];
$id = $headings[$heading_index]['id'];
$heading_index++;
return '<h' . $level . ' id="' . esc_attr($id) . '">' . $text . '</h' . $level . '>';
},
$content
);
return $content;
}
/**
* Modify post content to add heading IDs
*
* Filters the_content to add IDs to headings for TOC linking.
*
* @param string $content Post content
* @return string Modified content
*/
function apus_filter_content_add_heading_ids($content) {
// Only apply to single posts
if (!is_single()) {
return $content;
}
return apus_add_heading_ids($content);
}
add_filter('the_content', 'apus_filter_content_add_heading_ids', 10);