feat(custom-css-manager): implementar TIPO 3 - CSS Crítico Personalizado

Nuevo sistema de gestión de CSS personalizado con panel admin:
- Admin/CustomCSSManager: CRUD de snippets CSS (crítico/diferido)
- Public/CustomCSSManager: Inyección dinámica en frontend
- Schema JSON para configuración del componente

Migración de CSS estático a BD:
- Tablas APU (~14KB) → snippet diferido en BD
- Tablas Genéricas (~10KB) → snippet diferido en BD
- Comentadas funciones legacy en enqueue-scripts.php

Limpieza de archivos obsoletos:
- Eliminado build-bootstrap-subset.js
- Eliminado migrate-legacy-options.php
- Eliminado minify-css.php
- Eliminado purgecss.config.js

Beneficios:
- CSS editable desde admin sin tocar código
- Soporte crítico (head) y diferido (footer)
- Filtrado por scope (all/home/single/archive)

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
FrankZamora
2025-12-01 15:43:25 -06:00
parent 423aae062c
commit 9cb0dd1491
24 changed files with 1553 additions and 784 deletions

View File

@@ -0,0 +1,74 @@
<?php
declare(strict_types=1);
namespace ROITheme\Admin\CustomCSSManager\Domain\ValueObjects;
use ROITheme\Shared\Domain\Exceptions\ValidationException;
/**
* Value Object para código CSS validado
*/
final class CSSCode
{
private const MAX_SIZE_CRITICAL = 14336; // 14KB para CSS crítico
private const MAX_SIZE_DEFERRED = 102400; // 100KB para CSS diferido
private function __construct(
private readonly string $value
) {}
public static function fromString(string $css): self
{
$sanitized = self::sanitize($css);
self::validate($sanitized);
return new self($sanitized);
}
private static function sanitize(string $css): string
{
// Eliminar etiquetas <style>
$css = preg_replace('/<\/?style[^>]*>/i', '', $css);
// Eliminar comentarios HTML
$css = preg_replace('/<!--.*?-->/s', '', $css);
return trim($css);
}
private static function validate(string $css): void
{
// Detectar código potencialmente peligroso
$dangerous = ['javascript:', 'expression(', '@import', 'behavior:'];
foreach ($dangerous as $pattern) {
if (stripos($css, $pattern) !== false) {
throw new ValidationException("CSS contiene patrón no permitido: {$pattern}");
}
}
}
public function validateForLoadType(LoadType $loadType): void
{
$maxSize = $loadType->isCritical()
? self::MAX_SIZE_CRITICAL
: self::MAX_SIZE_DEFERRED;
if (strlen($this->value) > $maxSize) {
throw new ValidationException(
sprintf('CSS excede el tamaño máximo de %d bytes para tipo %s',
$maxSize,
$loadType->value()
)
);
}
}
public function value(): string
{
return $this->value;
}
public function isEmpty(): bool
{
return empty($this->value);
}
}

View File

@@ -0,0 +1,56 @@
<?php
declare(strict_types=1);
namespace ROITheme\Admin\CustomCSSManager\Domain\ValueObjects;
use ROITheme\Shared\Domain\Exceptions\ValidationException;
/**
* Value Object para tipo de carga CSS
*/
final class LoadType
{
private const VALID_TYPES = ['critical', 'deferred'];
private function __construct(
private readonly string $value
) {}
public static function fromString(string $value): self
{
if (!in_array($value, self::VALID_TYPES, true)) {
throw new ValidationException(
sprintf('LoadType inválido: %s. Valores válidos: %s',
$value,
implode(', ', self::VALID_TYPES)
)
);
}
return new self($value);
}
public static function critical(): self
{
return new self('critical');
}
public static function deferred(): self
{
return new self('deferred');
}
public function isCritical(): bool
{
return $this->value === 'critical';
}
public function isDeferred(): bool
{
return $this->value === 'deferred';
}
public function value(): string
{
return $this->value;
}
}

View File

@@ -0,0 +1,121 @@
<?php
declare(strict_types=1);
namespace ROITheme\Admin\CustomCSSManager\Domain\ValueObjects;
use ROITheme\Shared\Domain\Exceptions\ValidationException;
/**
* Value Object para ID único de snippet CSS
*
* Soporta dos formatos:
* 1. Generado: css_[timestamp]_[random] (ej: "css_1701432000_a1b2c3")
* 2. Legacy/Migración: kebab-case (ej: "cls-tables-apu", "generic-tables")
*
* Esto permite migrar snippets existentes sin romper IDs.
*/
final class SnippetId
{
private const PREFIX = 'css_';
private const PATTERN_GENERATED = '/^css_[0-9]+_[a-z0-9]{6}$/';
private const PATTERN_LEGACY = '/^[a-z0-9]+(-[a-z0-9]+)*$/';
private function __construct(
private readonly string $value
) {}
/**
* Crea SnippetId desde string existente (desde BD)
*
* Acepta tanto IDs generados (css_*) como IDs legacy (kebab-case).
*
* @param string $id ID existente
* @return self
* @throws ValidationException Si el formato es inválido
*/
public static function fromString(string $id): self
{
$id = trim($id);
if (empty($id)) {
throw new ValidationException('El ID del snippet no puede estar vacío');
}
if (strlen($id) > 50) {
throw new ValidationException('El ID del snippet no puede exceder 50 caracteres');
}
// Validar formato generado (css_*)
if (str_starts_with($id, self::PREFIX)) {
if (!preg_match(self::PATTERN_GENERATED, $id)) {
throw new ValidationException(
sprintf('Formato de ID generado inválido: %s. Esperado: css_[timestamp]_[random]', $id)
);
}
return new self($id);
}
// Validar formato legacy (kebab-case)
if (!preg_match(self::PATTERN_LEGACY, $id)) {
throw new ValidationException(
sprintf('Formato de ID inválido: %s. Use kebab-case (ej: cls-tables-apu)', $id)
);
}
return new self($id);
}
/**
* Genera un nuevo SnippetId único
*
* @return self
*/
public static function generate(): self
{
$timestamp = time();
$random = bin2hex(random_bytes(3));
return new self(self::PREFIX . $timestamp . '_' . $random);
}
/**
* Verifica si es un ID generado (vs legacy)
*
* @return bool
*/
public function isGenerated(): bool
{
return str_starts_with($this->value, self::PREFIX);
}
/**
* Obtiene el valor del ID
*
* @return string
*/
public function value(): string
{
return $this->value;
}
/**
* Compara igualdad con otro SnippetId
*
* @param SnippetId $other
* @return bool
*/
public function equals(SnippetId $other): bool
{
return $this->value === $other->value;
}
/**
* Representación string
*
* @return string
*/
public function __toString(): string
{
return $this->value;
}
}