CSS injection is working correctly in production: - roi-custom-deferred-css appears in page output - 2 snippets with 24KB of CSS being injected 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
127 lines
3.3 KiB
PHP
127 lines
3.3 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
|
|
{
|
|
$pageType = $this->getCurrentPageType();
|
|
$snippets = $this->getCriticalUseCase->execute($pageType);
|
|
|
|
if (empty($snippets)) {
|
|
return;
|
|
}
|
|
|
|
$css = $this->combineSnippets($snippets);
|
|
|
|
if (!empty($css)) {
|
|
printf(
|
|
'<style id="roi-custom-critical-css">%s</style>' . "\n",
|
|
$css
|
|
);
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Inyecta CSS diferido en footer
|
|
*/
|
|
public function injectDeferredCSS(): void
|
|
{
|
|
$pageType = $this->getCurrentPageType();
|
|
$snippets = $this->getDeferredUseCase->execute($pageType);
|
|
|
|
if (empty($snippets)) {
|
|
return;
|
|
}
|
|
|
|
$css = $this->combineSnippets($snippets);
|
|
|
|
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';
|
|
}
|
|
}
|