feat(exclusions): Implement component exclusion system (Plan 99.11)
Adds ability to exclude components from specific: - Categories (by slug or term_id) - Post/Page IDs - URL patterns (substring or regex) Architecture: - Domain: Value Objects (CategoryExclusion, PostIdExclusion, UrlPatternExclusion, ExclusionRuleSet) + Contracts - Application: EvaluateExclusionsUseCase + EvaluateComponentVisibilityUseCase (orchestrator) - Infrastructure: WordPressExclusionRepository, WordPressPageContextProvider, WordPressServerRequestProvider - Admin: ExclusionFormPartial (reusable UI), ExclusionFieldProcessor, JS toggle The PageVisibilityHelper now uses the orchestrator UseCase that combines page-type visibility (Plan 99.10) with exclusion rules. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
100
Shared/Domain/ValueObjects/CategoryExclusion.php
Normal file
100
Shared/Domain/ValueObjects/CategoryExclusion.php
Normal file
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ROITheme\Shared\Domain\ValueObjects;
|
||||
|
||||
/**
|
||||
* Value Object: Exclusion por categoria
|
||||
*
|
||||
* Evalua si un post pertenece a alguna de las categorias excluidas.
|
||||
* Soporta matching por slug o term_id.
|
||||
*
|
||||
* @package ROITheme\Shared\Domain\ValueObjects
|
||||
*/
|
||||
final class CategoryExclusion extends ExclusionRule
|
||||
{
|
||||
/**
|
||||
* @param array<int|string> $excludedCategories Lista de slugs o IDs de categorias
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly array $excludedCategories = []
|
||||
) {}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* Contexto esperado:
|
||||
* - categories: array<array{term_id: int, slug: string, name: string}>
|
||||
*/
|
||||
public function matches(array $context): bool
|
||||
{
|
||||
if (!$this->hasValues()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$postCategories = $context['categories'] ?? [];
|
||||
|
||||
if (empty($postCategories)) {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($postCategories as $category) {
|
||||
// Buscar por slug
|
||||
if (in_array($category['slug'], $this->excludedCategories, true)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Buscar por term_id
|
||||
if (in_array($category['term_id'], $this->excludedCategories, true)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// Buscar por term_id como string (para comparaciones flexibles)
|
||||
if (in_array((string) $category['term_id'], $this->excludedCategories, true)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function hasValues(): bool
|
||||
{
|
||||
return !empty($this->excludedCategories);
|
||||
}
|
||||
|
||||
public function serialize(): string
|
||||
{
|
||||
return json_encode($this->excludedCategories, JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int|string>
|
||||
*/
|
||||
public function getExcludedCategories(): array
|
||||
{
|
||||
return $this->excludedCategories;
|
||||
}
|
||||
|
||||
/**
|
||||
* Crea instancia desde JSON
|
||||
*/
|
||||
public static function fromJson(string $json): self
|
||||
{
|
||||
$decoded = json_decode($json, true);
|
||||
|
||||
if (!is_array($decoded)) {
|
||||
return self::empty();
|
||||
}
|
||||
|
||||
return new self($decoded);
|
||||
}
|
||||
|
||||
/**
|
||||
* Crea instancia vacia
|
||||
*/
|
||||
public static function empty(): self
|
||||
{
|
||||
return new self([]);
|
||||
}
|
||||
}
|
||||
37
Shared/Domain/ValueObjects/ExclusionRule.php
Normal file
37
Shared/Domain/ValueObjects/ExclusionRule.php
Normal file
@@ -0,0 +1,37 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ROITheme\Shared\Domain\ValueObjects;
|
||||
|
||||
/**
|
||||
* Clase base abstracta para reglas de exclusion
|
||||
*
|
||||
* Define el contrato comun para todos los tipos de exclusion.
|
||||
* Cada implementacion concreta define su logica de matching.
|
||||
*
|
||||
* @package ROITheme\Shared\Domain\ValueObjects
|
||||
*/
|
||||
abstract class ExclusionRule
|
||||
{
|
||||
/**
|
||||
* Evalua si el contexto actual coincide con la regla
|
||||
*
|
||||
* @param array<string, mixed> $context Contexto de la pagina actual
|
||||
* @return bool True si el contexto coincide (debe excluirse)
|
||||
*/
|
||||
abstract public function matches(array $context): bool;
|
||||
|
||||
/**
|
||||
* Verifica si la regla tiene valores configurados
|
||||
*
|
||||
* @return bool True si hay valores configurados
|
||||
*/
|
||||
abstract public function hasValues(): bool;
|
||||
|
||||
/**
|
||||
* Serializa los valores para almacenamiento
|
||||
*
|
||||
* @return string JSON string
|
||||
*/
|
||||
abstract public function serialize(): string;
|
||||
}
|
||||
100
Shared/Domain/ValueObjects/ExclusionRuleSet.php
Normal file
100
Shared/Domain/ValueObjects/ExclusionRuleSet.php
Normal file
@@ -0,0 +1,100 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ROITheme\Shared\Domain\ValueObjects;
|
||||
|
||||
/**
|
||||
* Value Object Compuesto: Conjunto de reglas de exclusion
|
||||
*
|
||||
* Agrupa todas las reglas de exclusion para un componente.
|
||||
* Evalua con logica OR (si cualquier regla coincide, se excluye).
|
||||
*
|
||||
* @package ROITheme\Shared\Domain\ValueObjects
|
||||
*/
|
||||
final class ExclusionRuleSet
|
||||
{
|
||||
public function __construct(
|
||||
private readonly string $componentName,
|
||||
private readonly bool $enabled,
|
||||
private readonly CategoryExclusion $categoryExclusion,
|
||||
private readonly PostIdExclusion $postIdExclusion,
|
||||
private readonly UrlPatternExclusion $urlPatternExclusion
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Evalua si el componente debe excluirse segun el contexto actual
|
||||
*
|
||||
* @param array<string, mixed> $context Contexto de la pagina actual
|
||||
* @return bool True si debe excluirse (NO mostrar)
|
||||
*/
|
||||
public function shouldExclude(array $context): bool
|
||||
{
|
||||
if (!$this->enabled) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// Evaluar cada tipo de exclusion (OR logico)
|
||||
if ($this->categoryExclusion->matches($context)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($this->postIdExclusion->matches($context)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($this->urlPatternExclusion->matches($context)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifica si tiene alguna regla configurada
|
||||
*/
|
||||
public function hasAnyRule(): bool
|
||||
{
|
||||
return $this->categoryExclusion->hasValues()
|
||||
|| $this->postIdExclusion->hasValues()
|
||||
|| $this->urlPatternExclusion->hasValues();
|
||||
}
|
||||
|
||||
public function getComponentName(): string
|
||||
{
|
||||
return $this->componentName;
|
||||
}
|
||||
|
||||
public function isEnabled(): bool
|
||||
{
|
||||
return $this->enabled;
|
||||
}
|
||||
|
||||
public function getCategoryExclusion(): CategoryExclusion
|
||||
{
|
||||
return $this->categoryExclusion;
|
||||
}
|
||||
|
||||
public function getPostIdExclusion(): PostIdExclusion
|
||||
{
|
||||
return $this->postIdExclusion;
|
||||
}
|
||||
|
||||
public function getUrlPatternExclusion(): UrlPatternExclusion
|
||||
{
|
||||
return $this->urlPatternExclusion;
|
||||
}
|
||||
|
||||
/**
|
||||
* Crea una instancia sin exclusiones (por defecto)
|
||||
*/
|
||||
public static function empty(string $componentName): self
|
||||
{
|
||||
return new self(
|
||||
$componentName,
|
||||
false,
|
||||
CategoryExclusion::empty(),
|
||||
PostIdExclusion::empty(),
|
||||
UrlPatternExclusion::empty()
|
||||
);
|
||||
}
|
||||
}
|
||||
86
Shared/Domain/ValueObjects/PostIdExclusion.php
Normal file
86
Shared/Domain/ValueObjects/PostIdExclusion.php
Normal file
@@ -0,0 +1,86 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ROITheme\Shared\Domain\ValueObjects;
|
||||
|
||||
/**
|
||||
* Value Object: Exclusion por ID de post/pagina
|
||||
*
|
||||
* Evalua si el post/pagina actual esta en la lista de IDs excluidos.
|
||||
*
|
||||
* @package ROITheme\Shared\Domain\ValueObjects
|
||||
*/
|
||||
final class PostIdExclusion extends ExclusionRule
|
||||
{
|
||||
/**
|
||||
* @param array<int> $excludedPostIds Lista de IDs de posts/paginas
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly array $excludedPostIds = []
|
||||
) {}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* Contexto esperado:
|
||||
* - post_id: int
|
||||
*/
|
||||
public function matches(array $context): bool
|
||||
{
|
||||
if (!$this->hasValues()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$postId = $context['post_id'] ?? 0;
|
||||
|
||||
if ($postId === 0) {
|
||||
return false;
|
||||
}
|
||||
|
||||
return in_array($postId, $this->excludedPostIds, true);
|
||||
}
|
||||
|
||||
public function hasValues(): bool
|
||||
{
|
||||
return !empty($this->excludedPostIds);
|
||||
}
|
||||
|
||||
public function serialize(): string
|
||||
{
|
||||
return json_encode($this->excludedPostIds);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<int>
|
||||
*/
|
||||
public function getExcludedPostIds(): array
|
||||
{
|
||||
return $this->excludedPostIds;
|
||||
}
|
||||
|
||||
/**
|
||||
* Crea instancia desde JSON
|
||||
*/
|
||||
public static function fromJson(string $json): self
|
||||
{
|
||||
$decoded = json_decode($json, true);
|
||||
|
||||
if (!is_array($decoded)) {
|
||||
return self::empty();
|
||||
}
|
||||
|
||||
// Asegurar que son enteros
|
||||
$ids = array_map('intval', $decoded);
|
||||
$ids = array_filter($ids, fn(int $id): bool => $id > 0);
|
||||
|
||||
return new self(array_values($ids));
|
||||
}
|
||||
|
||||
/**
|
||||
* Crea instancia vacia
|
||||
*/
|
||||
public static function empty(): self
|
||||
{
|
||||
return new self([]);
|
||||
}
|
||||
}
|
||||
146
Shared/Domain/ValueObjects/UrlPatternExclusion.php
Normal file
146
Shared/Domain/ValueObjects/UrlPatternExclusion.php
Normal file
@@ -0,0 +1,146 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ROITheme\Shared\Domain\ValueObjects;
|
||||
|
||||
/**
|
||||
* Value Object: Exclusion por patron URL
|
||||
*
|
||||
* Evalua si la URL actual coincide con alguno de los patrones configurados.
|
||||
* Soporta:
|
||||
* - Substring simple: "/privado/" coincide con cualquier URL que contenga ese texto
|
||||
* - Regex: Patrones que empiezan y terminan con "/" son evaluados como regex
|
||||
*
|
||||
* @package ROITheme\Shared\Domain\ValueObjects
|
||||
*/
|
||||
final class UrlPatternExclusion extends ExclusionRule
|
||||
{
|
||||
/**
|
||||
* @param array<string> $urlPatterns Lista de patrones (substring o regex)
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly array $urlPatterns = []
|
||||
) {}
|
||||
|
||||
/**
|
||||
* {@inheritdoc}
|
||||
*
|
||||
* Contexto esperado:
|
||||
* - request_uri: string (URI del request)
|
||||
* - url: string (URL completa, opcional)
|
||||
*/
|
||||
public function matches(array $context): bool
|
||||
{
|
||||
if (!$this->hasValues()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
$requestUri = $context['request_uri'] ?? '';
|
||||
$url = $context['url'] ?? '';
|
||||
|
||||
if ($requestUri === '' && $url === '') {
|
||||
return false;
|
||||
}
|
||||
|
||||
foreach ($this->urlPatterns as $pattern) {
|
||||
if ($this->matchesPattern($pattern, $requestUri, $url)) {
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
/**
|
||||
* Evalua si un patron coincide con el request_uri o url
|
||||
*/
|
||||
private function matchesPattern(string $pattern, string $requestUri, string $url): bool
|
||||
{
|
||||
// Detectar si es regex (empieza con /)
|
||||
if ($this->isRegex($pattern)) {
|
||||
return $this->matchesRegex($pattern, $requestUri);
|
||||
}
|
||||
|
||||
// Substring matching
|
||||
return $this->matchesSubstring($pattern, $requestUri, $url);
|
||||
}
|
||||
|
||||
/**
|
||||
* Detecta si el patron es una expresion regular
|
||||
*/
|
||||
private function isRegex(string $pattern): bool
|
||||
{
|
||||
// Un patron regex debe empezar con / y terminar con / (posiblemente con flags)
|
||||
return preg_match('#^/.+/[gimsux]*$#', $pattern) === 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Evalua coincidencia regex
|
||||
*/
|
||||
private function matchesRegex(string $pattern, string $subject): bool
|
||||
{
|
||||
// Suprimir warnings de regex invalidos
|
||||
$result = @preg_match($pattern, $subject);
|
||||
|
||||
return $result === 1;
|
||||
}
|
||||
|
||||
/**
|
||||
* Evalua coincidencia por substring
|
||||
*/
|
||||
private function matchesSubstring(string $pattern, string $requestUri, string $url): bool
|
||||
{
|
||||
if ($requestUri !== '' && str_contains($requestUri, $pattern)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
if ($url !== '' && str_contains($url, $pattern)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
public function hasValues(): bool
|
||||
{
|
||||
return !empty($this->urlPatterns);
|
||||
}
|
||||
|
||||
public function serialize(): string
|
||||
{
|
||||
return json_encode($this->urlPatterns, JSON_UNESCAPED_SLASHES | JSON_UNESCAPED_UNICODE);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string>
|
||||
*/
|
||||
public function getUrlPatterns(): array
|
||||
{
|
||||
return $this->urlPatterns;
|
||||
}
|
||||
|
||||
/**
|
||||
* Crea instancia desde JSON
|
||||
*/
|
||||
public static function fromJson(string $json): self
|
||||
{
|
||||
$decoded = json_decode($json, true);
|
||||
|
||||
if (!is_array($decoded)) {
|
||||
return self::empty();
|
||||
}
|
||||
|
||||
// Filtrar valores vacios
|
||||
$patterns = array_filter($decoded, fn($p): bool => is_string($p) && $p !== '');
|
||||
|
||||
return new self(array_values($patterns));
|
||||
}
|
||||
|
||||
/**
|
||||
* Crea instancia vacia
|
||||
*/
|
||||
public static function empty(): self
|
||||
{
|
||||
return new self([]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user