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:
@@ -28,6 +28,15 @@ use ROITheme\Shared\Infrastructure\Services\WordPressPageTypeDetector;
|
||||
use ROITheme\Shared\Infrastructure\Persistence\WordPress\WordPressPageVisibilityRepository;
|
||||
use ROITheme\Shared\Application\UseCases\EvaluatePageVisibility\EvaluatePageVisibilityUseCase;
|
||||
use ROITheme\Shared\Infrastructure\Services\MigratePageVisibilityService;
|
||||
// Exclusion System (Plan 99.11)
|
||||
use ROITheme\Shared\Domain\Contracts\ExclusionRepositoryInterface;
|
||||
use ROITheme\Shared\Domain\Contracts\PageContextProviderInterface;
|
||||
use ROITheme\Shared\Domain\Contracts\ServerRequestProviderInterface;
|
||||
use ROITheme\Shared\Infrastructure\Persistence\WordPress\WordPressExclusionRepository;
|
||||
use ROITheme\Shared\Infrastructure\Services\WordPressPageContextProvider;
|
||||
use ROITheme\Shared\Infrastructure\Services\WordPressServerRequestProvider;
|
||||
use ROITheme\Shared\Application\UseCases\EvaluateExclusions\EvaluateExclusionsUseCase;
|
||||
use ROITheme\Shared\Application\UseCases\EvaluateComponentVisibility\EvaluateComponentVisibilityUseCase;
|
||||
|
||||
/**
|
||||
* DIContainer - Contenedor de Inyección de Dependencias
|
||||
@@ -363,4 +372,75 @@ final class DIContainer
|
||||
}
|
||||
return $this->instances['migratePageVisibilityService'];
|
||||
}
|
||||
|
||||
// ===============================
|
||||
// Exclusion System (Plan 99.11)
|
||||
// ===============================
|
||||
|
||||
/**
|
||||
* Obtiene el proveedor de request HTTP
|
||||
*
|
||||
* Encapsula acceso a $_SERVER
|
||||
*/
|
||||
public function getServerRequestProvider(): ServerRequestProviderInterface
|
||||
{
|
||||
if (!isset($this->instances['serverRequestProvider'])) {
|
||||
$this->instances['serverRequestProvider'] = new WordPressServerRequestProvider();
|
||||
}
|
||||
return $this->instances['serverRequestProvider'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtiene el repositorio de exclusiones
|
||||
*/
|
||||
public function getExclusionRepository(): ExclusionRepositoryInterface
|
||||
{
|
||||
if (!isset($this->instances['exclusionRepository'])) {
|
||||
$this->instances['exclusionRepository'] = new WordPressExclusionRepository($this->wpdb);
|
||||
}
|
||||
return $this->instances['exclusionRepository'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtiene el proveedor de contexto de página
|
||||
*/
|
||||
public function getPageContextProvider(): PageContextProviderInterface
|
||||
{
|
||||
if (!isset($this->instances['pageContextProvider'])) {
|
||||
$this->instances['pageContextProvider'] = new WordPressPageContextProvider(
|
||||
$this->getServerRequestProvider()
|
||||
);
|
||||
}
|
||||
return $this->instances['pageContextProvider'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtiene el caso de uso de evaluación de exclusiones
|
||||
*/
|
||||
public function getEvaluateExclusionsUseCase(): EvaluateExclusionsUseCase
|
||||
{
|
||||
if (!isset($this->instances['evaluateExclusionsUseCase'])) {
|
||||
$this->instances['evaluateExclusionsUseCase'] = new EvaluateExclusionsUseCase(
|
||||
$this->getExclusionRepository(),
|
||||
$this->getPageContextProvider()
|
||||
);
|
||||
}
|
||||
return $this->instances['evaluateExclusionsUseCase'];
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtiene el caso de uso orquestador de visibilidad completa
|
||||
*
|
||||
* Combina visibilidad por tipo de página + exclusiones
|
||||
*/
|
||||
public function getEvaluateComponentVisibilityUseCase(): EvaluateComponentVisibilityUseCase
|
||||
{
|
||||
if (!isset($this->instances['evaluateComponentVisibilityUseCase'])) {
|
||||
$this->instances['evaluateComponentVisibilityUseCase'] = new EvaluateComponentVisibilityUseCase(
|
||||
$this->getEvaluatePageVisibilityUseCase(),
|
||||
$this->getEvaluateExclusionsUseCase()
|
||||
);
|
||||
}
|
||||
return $this->instances['evaluateComponentVisibilityUseCase'];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,147 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ROITheme\Shared\Infrastructure\Persistence\WordPress;
|
||||
|
||||
use ROITheme\Shared\Domain\Contracts\ExclusionRepositoryInterface;
|
||||
use ROITheme\Shared\Domain\ValueObjects\ExclusionRuleSet;
|
||||
use ROITheme\Shared\Domain\ValueObjects\CategoryExclusion;
|
||||
use ROITheme\Shared\Domain\ValueObjects\PostIdExclusion;
|
||||
use ROITheme\Shared\Domain\ValueObjects\UrlPatternExclusion;
|
||||
use ROITheme\Shared\Domain\Constants\ExclusionDefaults;
|
||||
|
||||
/**
|
||||
* Implementacion WordPress del repositorio de exclusiones
|
||||
*
|
||||
* Almacena exclusiones en wp_roi_theme_component_settings
|
||||
* con group_name = '_exclusions'
|
||||
*
|
||||
* @package ROITheme\Shared\Infrastructure\Persistence\WordPress
|
||||
*/
|
||||
final class WordPressExclusionRepository implements ExclusionRepositoryInterface
|
||||
{
|
||||
private const TABLE_SUFFIX = 'roi_theme_component_settings';
|
||||
|
||||
public function __construct(
|
||||
private readonly \wpdb $wpdb
|
||||
) {}
|
||||
|
||||
public function getExclusions(string $componentName): ExclusionRuleSet
|
||||
{
|
||||
$table = $this->wpdb->prefix . self::TABLE_SUFFIX;
|
||||
$groupName = ExclusionDefaults::GROUP_NAME;
|
||||
|
||||
$results = $this->wpdb->get_results(
|
||||
$this->wpdb->prepare(
|
||||
"SELECT attribute_name, attribute_value
|
||||
FROM {$table}
|
||||
WHERE component_name = %s
|
||||
AND group_name = %s",
|
||||
$componentName,
|
||||
$groupName
|
||||
),
|
||||
ARRAY_A
|
||||
);
|
||||
|
||||
if (empty($results)) {
|
||||
return ExclusionRuleSet::empty($componentName);
|
||||
}
|
||||
|
||||
$data = [];
|
||||
foreach ($results as $row) {
|
||||
$data[$row['attribute_name']] = $row['attribute_value'];
|
||||
}
|
||||
|
||||
return $this->hydrateExclusions($componentName, $data);
|
||||
}
|
||||
|
||||
public function saveExclusions(ExclusionRuleSet $exclusions): void
|
||||
{
|
||||
$componentName = $exclusions->getComponentName();
|
||||
|
||||
$data = [
|
||||
'exclusions_enabled' => $exclusions->isEnabled() ? '1' : '0',
|
||||
'exclude_categories' => $exclusions->getCategoryExclusion()->serialize(),
|
||||
'exclude_post_ids' => $exclusions->getPostIdExclusion()->serialize(),
|
||||
'exclude_url_patterns' => $exclusions->getUrlPatternExclusion()->serialize(),
|
||||
];
|
||||
|
||||
foreach ($data as $field => $value) {
|
||||
$this->upsertField($componentName, $field, $value);
|
||||
}
|
||||
}
|
||||
|
||||
public function hasExclusions(string $componentName): bool
|
||||
{
|
||||
$table = $this->wpdb->prefix . self::TABLE_SUFFIX;
|
||||
$groupName = ExclusionDefaults::GROUP_NAME;
|
||||
|
||||
$count = $this->wpdb->get_var($this->wpdb->prepare(
|
||||
"SELECT COUNT(*) FROM {$table}
|
||||
WHERE component_name = %s
|
||||
AND group_name = %s",
|
||||
$componentName,
|
||||
$groupName
|
||||
));
|
||||
|
||||
return (int) $count > 0;
|
||||
}
|
||||
|
||||
private function hydrateExclusions(string $componentName, array $data): ExclusionRuleSet
|
||||
{
|
||||
$enabled = ($data['exclusions_enabled'] ?? '0') === '1';
|
||||
|
||||
$categoryExclusion = CategoryExclusion::fromJson($data['exclude_categories'] ?? '[]');
|
||||
$postIdExclusion = PostIdExclusion::fromJson($data['exclude_post_ids'] ?? '[]');
|
||||
$urlPatternExclusion = UrlPatternExclusion::fromJson($data['exclude_url_patterns'] ?? '[]');
|
||||
|
||||
return new ExclusionRuleSet(
|
||||
$componentName,
|
||||
$enabled,
|
||||
$categoryExclusion,
|
||||
$postIdExclusion,
|
||||
$urlPatternExclusion
|
||||
);
|
||||
}
|
||||
|
||||
private function upsertField(string $componentName, string $field, string $value): void
|
||||
{
|
||||
$table = $this->wpdb->prefix . self::TABLE_SUFFIX;
|
||||
$groupName = ExclusionDefaults::GROUP_NAME;
|
||||
|
||||
$exists = $this->wpdb->get_var($this->wpdb->prepare(
|
||||
"SELECT COUNT(*) FROM {$table}
|
||||
WHERE component_name = %s
|
||||
AND group_name = %s
|
||||
AND attribute_name = %s",
|
||||
$componentName,
|
||||
$groupName,
|
||||
$field
|
||||
));
|
||||
|
||||
if ($exists) {
|
||||
$this->wpdb->update(
|
||||
$table,
|
||||
[
|
||||
'attribute_value' => $value,
|
||||
'updated_at' => current_time('mysql'),
|
||||
],
|
||||
[
|
||||
'component_name' => $componentName,
|
||||
'group_name' => $groupName,
|
||||
'attribute_name' => $field,
|
||||
]
|
||||
);
|
||||
} else {
|
||||
$this->wpdb->insert($table, [
|
||||
'component_name' => $componentName,
|
||||
'group_name' => $groupName,
|
||||
'attribute_name' => $field,
|
||||
'attribute_value' => $value,
|
||||
'is_editable' => 1,
|
||||
'created_at' => current_time('mysql'),
|
||||
'updated_at' => current_time('mysql'),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -6,11 +6,11 @@ namespace ROITheme\Shared\Infrastructure\Services;
|
||||
use ROITheme\Shared\Infrastructure\Di\DIContainer;
|
||||
|
||||
/**
|
||||
* Facade/Helper para evaluar visibilidad de componentes
|
||||
* Facade/Helper para evaluar visibilidad completa de componentes
|
||||
*
|
||||
* PROPÓSITO:
|
||||
* Permite que los Renderers existentes evalúen visibilidad sin modificar sus constructores.
|
||||
* Actúa como un Service Locator limitado a este único propósito.
|
||||
* PROPOSITO:
|
||||
* Permite que los Renderers existentes evaluen visibilidad sin modificar sus constructores.
|
||||
* Ahora incluye tanto visibilidad por tipo de pagina como reglas de exclusion.
|
||||
*
|
||||
* USO EN RENDERERS:
|
||||
* ```php
|
||||
@@ -19,17 +19,41 @@ use ROITheme\Shared\Infrastructure\Di\DIContainer;
|
||||
* }
|
||||
* ```
|
||||
*
|
||||
* FLUJO:
|
||||
* 1. Verifica visibilidad por tipo de pagina (home, posts, pages, etc.)
|
||||
* 2. Verifica reglas de exclusion (categorias, IDs, patrones URL)
|
||||
* 3. Retorna true SOLO si pasa ambas verificaciones
|
||||
*
|
||||
* @package ROITheme\Shared\Infrastructure\Services
|
||||
*/
|
||||
final class PageVisibilityHelper
|
||||
{
|
||||
/**
|
||||
* Evalúa si un componente debe mostrarse en la página actual
|
||||
* Evalua si un componente debe mostrarse en la pagina actual
|
||||
*
|
||||
* Incluye verificacion de:
|
||||
* - Visibilidad por tipo de pagina (Plan 99.10)
|
||||
* - Reglas de exclusion (Plan 99.11)
|
||||
*
|
||||
* @param string $componentName Nombre del componente (kebab-case)
|
||||
* @return bool True si debe mostrarse
|
||||
*/
|
||||
public static function shouldShow(string $componentName): bool
|
||||
{
|
||||
$container = DIContainer::getInstance();
|
||||
$useCase = $container->getEvaluateComponentVisibilityUseCase();
|
||||
|
||||
return $useCase->execute($componentName);
|
||||
}
|
||||
|
||||
/**
|
||||
* Evalua SOLO visibilidad por tipo de pagina (sin exclusiones)
|
||||
*
|
||||
* @deprecated Usar shouldShow() que incluye exclusiones
|
||||
* @param string $componentName Nombre del componente (kebab-case)
|
||||
* @return bool True si debe mostrarse segun tipo de pagina
|
||||
*/
|
||||
public static function shouldShowByPageType(string $componentName): bool
|
||||
{
|
||||
$container = DIContainer::getInstance();
|
||||
$useCase = $container->getEvaluatePageVisibilityUseCase();
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ROITheme\Shared\Infrastructure\Services;
|
||||
|
||||
use ROITheme\Shared\Domain\Contracts\PageContextProviderInterface;
|
||||
use ROITheme\Shared\Domain\Contracts\ServerRequestProviderInterface;
|
||||
|
||||
/**
|
||||
* Implementacion WordPress del proveedor de contexto de pagina
|
||||
*
|
||||
* Obtiene informacion del post/pagina actual usando funciones de WordPress.
|
||||
*
|
||||
* v1.1: Renombrado de WordPressExclusionEvaluator
|
||||
* Inyecta ServerRequestProviderInterface (no accede a $_SERVER directamente)
|
||||
*
|
||||
* @package ROITheme\Shared\Infrastructure\Services
|
||||
*/
|
||||
final class WordPressPageContextProvider implements PageContextProviderInterface
|
||||
{
|
||||
public function __construct(
|
||||
private readonly ServerRequestProviderInterface $requestProvider
|
||||
) {}
|
||||
|
||||
public function getCurrentContext(): array
|
||||
{
|
||||
$postId = $this->getCurrentPostId();
|
||||
|
||||
return [
|
||||
'post_id' => $postId,
|
||||
'categories' => $this->getPostCategories($postId),
|
||||
'url' => $this->getCurrentUrl(),
|
||||
'request_uri' => $this->requestProvider->getRequestUri(),
|
||||
'post_type' => $this->getCurrentPostType($postId),
|
||||
];
|
||||
}
|
||||
|
||||
private function getCurrentPostId(): int
|
||||
{
|
||||
if (is_singular()) {
|
||||
return get_the_ID() ?: 0;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<array{term_id: int, slug: string, name: string}>
|
||||
*/
|
||||
private function getPostCategories(int $postId): array
|
||||
{
|
||||
if ($postId === 0) {
|
||||
return [];
|
||||
}
|
||||
|
||||
$categories = get_the_category($postId);
|
||||
|
||||
if (empty($categories)) {
|
||||
return [];
|
||||
}
|
||||
|
||||
return array_map(function (\WP_Term $term): array {
|
||||
return [
|
||||
'term_id' => $term->term_id,
|
||||
'slug' => $term->slug,
|
||||
'name' => $term->name,
|
||||
];
|
||||
}, $categories);
|
||||
}
|
||||
|
||||
private function getCurrentUrl(): string
|
||||
{
|
||||
global $wp;
|
||||
|
||||
if (isset($wp->request)) {
|
||||
return home_url($wp->request);
|
||||
}
|
||||
|
||||
return home_url(add_query_arg([], false));
|
||||
}
|
||||
|
||||
private function getCurrentPostType(int $postId): string
|
||||
{
|
||||
if ($postId === 0) {
|
||||
return '';
|
||||
}
|
||||
|
||||
return get_post_type($postId) ?: '';
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,23 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ROITheme\Shared\Infrastructure\Services;
|
||||
|
||||
use ROITheme\Shared\Domain\Contracts\ServerRequestProviderInterface;
|
||||
|
||||
/**
|
||||
* Implementacion WordPress del proveedor de request HTTP
|
||||
*
|
||||
* Encapsula el acceso a $_SERVER.
|
||||
*
|
||||
* v1.1: Nuevo - extrae logica de acceso a superglobales
|
||||
*
|
||||
* @package ROITheme\Shared\Infrastructure\Services
|
||||
*/
|
||||
final class WordPressServerRequestProvider implements ServerRequestProviderInterface
|
||||
{
|
||||
public function getRequestUri(): string
|
||||
{
|
||||
return $_SERVER['REQUEST_URI'] ?? '';
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user