From b509b1a2b4d1228b244bc7ef1e2039915c072dac Mon Sep 17 00:00:00 2001 From: FrankZamora Date: Sat, 6 Dec 2025 23:08:24 -0600 Subject: [PATCH] fix(php): toc fallback to raw content when filtered has no headings MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit When plugins like Thrive Visual Editor transform content for non-logged users, headings may be removed from the filtered content. This fix uses raw post_content as fallback when filtered content has no headings but raw content does. Also removes temporary debug logging added for diagnosis. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude Opus 4.5 --- .../Ui/TableOfContentsRenderer.php | 45 ++++++------------- .../EvaluateComponentVisibilityUseCase.php | 11 ----- .../EvaluatePageVisibilityUseCase.php | 14 +----- .../Services/PageVisibilityHelper.php | 10 +---- .../Services/WordPressPageTypeDetector.php | 12 ----- 5 files changed, 16 insertions(+), 76 deletions(-) diff --git a/Public/TableOfContents/Infrastructure/Ui/TableOfContentsRenderer.php b/Public/TableOfContents/Infrastructure/Ui/TableOfContentsRenderer.php index 98afecb2..392ebbbe 100644 --- a/Public/TableOfContents/Infrastructure/Ui/TableOfContentsRenderer.php +++ b/Public/TableOfContents/Infrastructure/Ui/TableOfContentsRenderer.php @@ -43,30 +43,20 @@ final class TableOfContentsRenderer implements RendererInterface { $data = $component->getData(); - // DEBUG TOC: Log all visibility checks - $isLoggedIn = is_user_logged_in(); - $debugPrefix = "TOC DEBUG [" . ($isLoggedIn ? "LOGGED" : "GUEST") . "]"; - if (!$this->isEnabled($data)) { - error_log("{$debugPrefix}: SKIP - isEnabled=false"); return ''; } - $shouldShow = PageVisibilityHelper::shouldShow(self::COMPONENT_NAME); - if (!$shouldShow) { - error_log("{$debugPrefix}: SKIP - PageVisibilityHelper::shouldShow=false"); + if (!PageVisibilityHelper::shouldShow(self::COMPONENT_NAME)) { return ''; } $tocItems = $this->generateTocItems($data); if (empty($tocItems)) { - error_log("{$debugPrefix}: SKIP - tocItems empty"); return ''; } - error_log("{$debugPrefix}: RENDER - passed all checks, items=" . count($tocItems)); - $css = $this->generateCSS($data); $html = $this->buildHTML($data, $tocItems); $script = $this->buildScript($data); @@ -117,31 +107,26 @@ final class TableOfContentsRenderer implements RendererInterface { global $post; - // DEBUG: Track content processing - $isLoggedIn = is_user_logged_in(); - $debugPrefix = "TOC generateTocFromContent [" . ($isLoggedIn ? "LOGGED" : "GUEST") . "]"; - if (!$post || empty($post->post_content)) { - error_log("{$debugPrefix}: SKIP - no post or empty content"); return []; } - error_log("{$debugPrefix}: post_id={$post->ID}, raw_content_length=" . strlen($post->post_content)); - - // Check if raw content has headings - $rawHeadingCount = preg_match_all('/]*>/i', $post->post_content, $rawMatches); - error_log("{$debugPrefix}: raw_headings_count={$rawHeadingCount}"); - + // Intentar primero con contenido filtrado (respeta shortcodes, etc.) $content = apply_filters('the_content', $post->post_content); - error_log("{$debugPrefix}: filtered_content_length=" . strlen($content)); + // Verificar si el contenido filtrado tiene headings + $hasFilteredHeadings = preg_match('/]*>/i', $content); - // Check if filtered content has headings - $filteredHeadingCount = preg_match_all('/]*>/i', $content, $filteredMatches); - error_log("{$debugPrefix}: filtered_headings_count={$filteredHeadingCount}"); - - // Log first 500 chars of filtered content for diagnosis - error_log("{$debugPrefix}: filtered_content_preview=" . substr(strip_tags($content), 0, 300)); + // FIX: Si el contenido filtrado no tiene headings pero el raw si, + // usar el contenido raw. Esto ocurre cuando plugins como Thrive + // transforman el contenido para usuarios no logueados. + if (!$hasFilteredHeadings) { + $hasRawHeadings = preg_match('/]*>/i', $post->post_content); + if ($hasRawHeadings) { + // Usar wpautop para dar formato basico al contenido raw + $content = wpautop($post->post_content); + } + } $dom = new DOMDocument(); libxml_use_internal_errors(true); @@ -157,8 +142,6 @@ final class TableOfContentsRenderer implements RendererInterface $headings = $xpath->query($xpathQuery); - error_log("{$debugPrefix}: headings_found=" . $headings->length); - if ($headings->length === 0) { return []; } diff --git a/Shared/Application/UseCases/EvaluateComponentVisibility/EvaluateComponentVisibilityUseCase.php b/Shared/Application/UseCases/EvaluateComponentVisibility/EvaluateComponentVisibilityUseCase.php index c078db14..aad59f18 100644 --- a/Shared/Application/UseCases/EvaluateComponentVisibility/EvaluateComponentVisibilityUseCase.php +++ b/Shared/Application/UseCases/EvaluateComponentVisibility/EvaluateComponentVisibilityUseCase.php @@ -36,16 +36,9 @@ final class EvaluateComponentVisibilityUseCase */ public function execute(string $componentName): bool { - // DEBUG: Log for TOC only - $debugToc = ($componentName === 'table-of-contents'); - // Paso 1: Verificar visibilidad por tipo de pagina $visibleByPageType = $this->pageVisibilityUseCase->execute($componentName); - if ($debugToc) { - error_log("EvaluateComponentVisibility [{$componentName}]: visibleByPageType=" . ($visibleByPageType ? "true" : "false")); - } - if (!$visibleByPageType) { return false; } @@ -53,10 +46,6 @@ final class EvaluateComponentVisibilityUseCase // Paso 2: Verificar exclusiones $isExcluded = $this->exclusionsUseCase->execute($componentName); - if ($debugToc) { - error_log("EvaluateComponentVisibility [{$componentName}]: isExcluded=" . ($isExcluded ? "true" : "false")); - } - // Mostrar si NO esta excluido return !$isExcluded; } diff --git a/Shared/Application/UseCases/EvaluatePageVisibility/EvaluatePageVisibilityUseCase.php b/Shared/Application/UseCases/EvaluatePageVisibility/EvaluatePageVisibilityUseCase.php index 69c8b1b6..14c54829 100644 --- a/Shared/Application/UseCases/EvaluatePageVisibility/EvaluatePageVisibilityUseCase.php +++ b/Shared/Application/UseCases/EvaluatePageVisibility/EvaluatePageVisibilityUseCase.php @@ -28,27 +28,15 @@ final class EvaluatePageVisibilityUseCase { $config = $this->visibilityRepository->getVisibilityConfig($componentName); - // DEBUG: Log for TOC only - $debugToc = ($componentName === 'table-of-contents'); - $usingDefaults = false; - if (empty($config)) { // Usar defaults especificos por componente si existen $config = VisibilityDefaults::getForComponent($componentName); - $usingDefaults = true; } $pageType = $this->pageTypeDetector->detect(); $visibilityField = $pageType->toVisibilityField(); - $fieldValue = $config[$visibilityField] ?? true; - $result = $this->toBool($fieldValue); - if ($debugToc) { - error_log("EvaluatePageVisibility [{$componentName}]: pageType=" . $pageType->value() . ", field={$visibilityField}, fieldValue=" . var_export($fieldValue, true) . ", result=" . ($result ? "true" : "false") . ", usingDefaults=" . ($usingDefaults ? "true" : "false")); - error_log("EvaluatePageVisibility [{$componentName}]: config=" . json_encode($config)); - } - - return $result; + return $this->toBool($config[$visibilityField] ?? true); } private function toBool(mixed $value): bool diff --git a/Shared/Infrastructure/Services/PageVisibilityHelper.php b/Shared/Infrastructure/Services/PageVisibilityHelper.php index 08b0b26e..cab954f8 100644 --- a/Shared/Infrastructure/Services/PageVisibilityHelper.php +++ b/Shared/Infrastructure/Services/PageVisibilityHelper.php @@ -43,15 +43,7 @@ final class PageVisibilityHelper $container = DIContainer::getInstance(); $useCase = $container->getEvaluateComponentVisibilityUseCase(); - $result = $useCase->execute($componentName); - - // DEBUG: Log visibility evaluation - if ($componentName === 'table-of-contents') { - $isLoggedIn = is_user_logged_in(); - error_log("PageVisibilityHelper DEBUG [{$componentName}] [" . ($isLoggedIn ? "LOGGED" : "GUEST") . "]: result=" . ($result ? "true" : "false")); - } - - return $result; + return $useCase->execute($componentName); } /** diff --git a/Shared/Infrastructure/Services/WordPressPageTypeDetector.php b/Shared/Infrastructure/Services/WordPressPageTypeDetector.php index 55085906..7dcaf162 100644 --- a/Shared/Infrastructure/Services/WordPressPageTypeDetector.php +++ b/Shared/Infrastructure/Services/WordPressPageTypeDetector.php @@ -15,18 +15,6 @@ final class WordPressPageTypeDetector implements PageTypeDetectorInterface { public function detect(): PageType { - // DEBUG: Log all checks for diagnosis - $isLoggedIn = is_user_logged_in(); - $debugData = [ - 'is_front_page' => is_front_page(), - 'is_home' => is_home(), - 'is_single' => is_single(), - 'is_page' => is_page(), - 'is_search' => is_search(), - 'is_archive' => is_archive(), - ]; - error_log("PageTypeDetector DEBUG [" . ($isLoggedIn ? "LOGGED" : "GUEST") . "]: " . json_encode($debugData)); - if ($this->isHome()) { return PageType::home(); }