Files
roi-theme/Public/CustomCSSManager/Infrastructure/Services/CustomCSSInjector.php
FrankZamora 9f0ae9fcb6 debug: add detailed logging to CustomCSSInjector
Temporary logging to diagnose why CSS is not injecting in production

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-12-01 16:32:43 -06:00

141 lines
4.0 KiB
PHP

<?php
declare(strict_types=1);
namespace ROITheme\Public\CustomCSSManager\Infrastructure\Services;
use ROITheme\Public\CustomCSSManager\Application\UseCases\GetCriticalSnippetsUseCase;
use ROITheme\Public\CustomCSSManager\Application\UseCases\GetDeferredSnippetsUseCase;
/**
* Servicio que inyecta CSS en wp_head y wp_footer
*
* NO lee archivos CSS físicos - todo viene de BD
*/
final class CustomCSSInjector
{
public function __construct(
private readonly GetCriticalSnippetsUseCase $getCriticalUseCase,
private readonly GetDeferredSnippetsUseCase $getDeferredUseCase
) {}
/**
* Registra hooks de WordPress
*/
public function register(): void
{
// CSS crítico: priority 2 (después de componentes, antes de theme-settings)
add_action('wp_head', [$this, 'injectCriticalCSS'], 2);
// CSS diferido: priority alta en footer
add_action('wp_footer', [$this, 'injectDeferredCSS'], 10);
}
/**
* Inyecta CSS crítico inline en <head>
*/
public function injectCriticalCSS(): void
{
error_log('ROI CustomCSS: injectCriticalCSS CALLED');
$pageType = $this->getCurrentPageType();
error_log('ROI CustomCSS: pageType=' . $pageType);
$snippets = $this->getCriticalUseCase->execute($pageType);
error_log('ROI CustomCSS: critical snippets count=' . count($snippets));
if (empty($snippets)) {
error_log('ROI CustomCSS: NO critical snippets, returning');
return;
}
$css = $this->combineSnippets($snippets);
error_log('ROI CustomCSS: critical CSS length=' . strlen($css));
if (!empty($css)) {
printf(
'<style id="roi-custom-critical-css">%s</style>' . "\n",
$css
);
}
}
/**
* Inyecta CSS diferido en footer
*/
public function injectDeferredCSS(): void
{
error_log('ROI CustomCSS: injectDeferredCSS CALLED');
$pageType = $this->getCurrentPageType();
error_log('ROI CustomCSS: deferred pageType=' . $pageType);
$snippets = $this->getDeferredUseCase->execute($pageType);
error_log('ROI CustomCSS: deferred snippets count=' . count($snippets));
if (empty($snippets)) {
error_log('ROI CustomCSS: NO deferred snippets, returning');
return;
}
$css = $this->combineSnippets($snippets);
error_log('ROI CustomCSS: deferred CSS length=' . strlen($css));
if (!empty($css)) {
printf(
'<style id="roi-custom-deferred-css">%s</style>' . "\n",
$css
);
}
}
/**
* Combina múltiples snippets en un solo string CSS
*
* Aplica sanitización para prevenir inyección de HTML malicioso.
*/
private function combineSnippets(array $snippets): string
{
$parts = [];
foreach ($snippets as $snippet) {
if (!empty($snippet['css'])) {
// Sanitizar CSS: eliminar tags HTML/script
$cleanCSS = wp_strip_all_tags($snippet['css']);
// Eliminar caracteres potencialmente peligrosos
$cleanCSS = preg_replace('/<[^>]*>/', '', $cleanCSS);
$cleanName = sanitize_text_field($snippet['name'] ?? $snippet['id']);
$parts[] = sprintf(
"/* %s */\n%s",
$cleanName,
$cleanCSS
);
}
}
return implode("\n\n", $parts);
}
/**
* Detecta tipo de página actual
*/
private function getCurrentPageType(): string
{
if (is_front_page() || is_home()) {
return 'home';
}
if (is_single()) {
return 'posts';
}
if (is_page()) {
return 'pages';
}
if (is_archive() || is_category() || is_tag()) {
return 'archives';
}
return 'all';
}
}