fix(php): toc fallback to raw content when filtered has no headings
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 <noreply@anthropic.com>
This commit is contained in:
@@ -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('/<h[2-6][^>]*>/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('/<h[2-6][^>]*>/i', $content);
|
||||
|
||||
// Check if filtered content has headings
|
||||
$filteredHeadingCount = preg_match_all('/<h[2-6][^>]*>/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('/<h[2-6][^>]*>/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 [];
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user