feat(visibility): añadir opción "Ocultar para usuarios logueados" (Plan 99.16)

- Crear UserVisibilityHelper centralizado en Shared/Infrastructure/Services
- Añadir campo hide_for_logged_in en schemas de 4 componentes
- Integrar validación en Renderers: TopBar, LetsTalk, CTASidebar, CTAPost
- Añadir checkbox UI en FormBuilders de los 4 componentes
- Refactorizar adsense-placement.php para usar el helper centralizado
- Deprecar función roi_should_hide_for_logged_in() (backwards compatible)

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
FrankZamora
2025-12-04 18:28:53 -06:00
parent 36d5cf56de
commit ffe6ea8e65
14 changed files with 181 additions and 23 deletions

View File

@@ -138,6 +138,20 @@ final class CtaBoxSidebarFormBuilder
$exclusionPartial = new ExclusionFormPartial($this->renderer);
$html .= $exclusionPartial->render($componentId, 'cta');
// Switch: Ocultar para usuarios logueados (Plan 99.16)
$hideForLoggedIn = $this->renderer->getFieldValue($componentId, 'visibility', 'hide_for_logged_in', false);
$html .= ' <div class="mb-0 mt-3">';
$html .= ' <div class="form-check form-switch">';
$html .= ' <input class="form-check-input" type="checkbox" id="ctaHideForLoggedIn" ';
$html .= checked($hideForLoggedIn, true, false) . '>';
$html .= ' <label class="form-check-label small" for="ctaHideForLoggedIn" style="color: #495057;">';
$html .= ' <i class="bi bi-person-lock me-1" style="color: #FF8600;"></i>';
$html .= ' <strong>Ocultar para usuarios logueados</strong>';
$html .= ' <small class="text-muted d-block">No mostrar a usuarios con sesión iniciada</small>';
$html .= ' </label>';
$html .= ' </div>';
$html .= ' </div>';
$html .= ' </div>';
$html .= '</div>';

View File

@@ -162,6 +162,34 @@ final class CtaLetsTalkFormBuilder
$exclusionPartial = new ExclusionFormPartial($this->renderer);
$html .= $exclusionPartial->render($componentId, 'letsTalk');
// Switch: CSS Crítico
$isCritical = $this->renderer->getFieldValue($componentId, 'visibility', 'is_critical', true);
$html .= ' <div class="mb-2 mt-3">';
$html .= ' <div class="form-check form-switch">';
$html .= ' <input class="form-check-input" type="checkbox" id="ctaLetsTalkIsCritical" ';
$html .= checked($isCritical, true, false) . '>';
$html .= ' <label class="form-check-label small" for="ctaLetsTalkIsCritical" style="color: #495057;">';
$html .= ' <i class="bi bi-lightning-charge me-1" style="color: #FF8600;"></i>';
$html .= ' <strong>CSS Crítico</strong>';
$html .= ' <small class="text-muted d-block">Inyectar CSS en &lt;head&gt; para optimizar LCP</small>';
$html .= ' </label>';
$html .= ' </div>';
$html .= ' </div>';
// Switch: Ocultar para usuarios logueados (Plan 99.16)
$hideForLoggedIn = $this->renderer->getFieldValue($componentId, 'visibility', 'hide_for_logged_in', false);
$html .= ' <div class="mb-0">';
$html .= ' <div class="form-check form-switch">';
$html .= ' <input class="form-check-input" type="checkbox" id="ctaLetsTalkHideForLoggedIn" ';
$html .= checked($hideForLoggedIn, true, false) . '>';
$html .= ' <label class="form-check-label small" for="ctaLetsTalkHideForLoggedIn" style="color: #495057;">';
$html .= ' <i class="bi bi-person-lock me-1" style="color: #FF8600;"></i>';
$html .= ' <strong>Ocultar para usuarios logueados</strong>';
$html .= ' <small class="text-muted d-block">No mostrar a usuarios con sesión iniciada</small>';
$html .= ' </label>';
$html .= ' </div>';
$html .= ' </div>';
$html .= ' </div>';
$html .= '</div>';

View File

@@ -127,6 +127,20 @@ final class CtaPostFormBuilder
$exclusionPartial = new ExclusionFormPartial($this->renderer);
$html .= $exclusionPartial->render($componentId, 'ctaPost');
// Switch: Ocultar para usuarios logueados (Plan 99.16)
$hideForLoggedIn = $this->renderer->getFieldValue($componentId, 'visibility', 'hide_for_logged_in', false);
$html .= ' <div class="mb-0 mt-3">';
$html .= ' <div class="form-check form-switch">';
$html .= ' <input class="form-check-input" type="checkbox" id="ctaPostHideForLoggedIn" ';
$html .= checked($hideForLoggedIn, true, false) . '>';
$html .= ' <label class="form-check-label small" for="ctaPostHideForLoggedIn" style="color: #495057;">';
$html .= ' <i class="bi bi-person-lock me-1" style="color: #FF8600;"></i>';
$html .= ' <strong>Ocultar para usuarios logueados</strong>';
$html .= ' <small class="text-muted d-block">No mostrar a usuarios con sesion iniciada</small>';
$html .= ' </label>';
$html .= ' </div>';
$html .= ' </div>';
$html .= ' </div>';
$html .= '</div>';

View File

@@ -149,7 +149,7 @@ final class TopNotificationBarFormBuilder
// Switch: CSS Crítico
$isCritical = $this->renderer->getFieldValue($componentId, 'visibility', 'is_critical', true);
$html .= ' <div class="mb-0 mt-3">';
$html .= ' <div class="mb-2 mt-3">';
$html .= ' <div class="form-check form-switch">';
$html .= ' <input class="form-check-input" type="checkbox" id="topBarIsCritical" ';
$html .= checked($isCritical, true, false) . '>';
@@ -161,6 +161,20 @@ final class TopNotificationBarFormBuilder
$html .= ' </div>';
$html .= ' </div>';
// Switch: Ocultar para usuarios logueados (Plan 99.16)
$hideForLoggedIn = $this->renderer->getFieldValue($componentId, 'visibility', 'hide_for_logged_in', false);
$html .= ' <div class="mb-0">';
$html .= ' <div class="form-check form-switch">';
$html .= ' <input class="form-check-input" type="checkbox" id="topBarHideForLoggedIn" ';
$html .= checked($hideForLoggedIn, true, false) . '>';
$html .= ' <label class="form-check-label small" for="topBarHideForLoggedIn" style="color: #495057;">';
$html .= ' <i class="bi bi-person-lock me-1" style="color: #FF8600;"></i>';
$html .= ' <strong>Ocultar para usuarios logueados</strong>';
$html .= ' <small class="text-muted d-block">No mostrar a usuarios con sesión iniciada</small>';
$html .= ' </label>';
$html .= ' </div>';
$html .= ' </div>';
$html .= ' </div>';
$html .= '</div>';

View File

@@ -26,6 +26,7 @@ if (!defined('ABSPATH')) {
}
use ROITheme\Shared\Infrastructure\Services\PageVisibilityHelper;
use ROITheme\Shared\Infrastructure\Services\UserVisibilityHelper;
/**
* Renderiza un slot de anuncio en una ubicacion
@@ -49,8 +50,8 @@ function roi_render_ad_slot(string $location): string
return '';
}
// Verificar si ocultar para usuarios logueados
if (roi_should_hide_for_logged_in($settings)) {
// Verificar visibilidad por usuario logueado (Plan 99.16)
if (!UserVisibilityHelper::shouldShowForUser($settings['visibility'] ?? [])) {
return '';
}
@@ -79,17 +80,17 @@ function roi_render_ad_slot(string $location): string
/**
* Verifica si se deben ocultar anuncios para usuarios logueados
*
* @deprecated Plan 99.16: Usar UserVisibilityHelper::shouldShowForUser() en su lugar.
* Esta función se mantiene para compatibilidad hacia atrás.
*
* @param array $settings Configuración del componente
* @return bool true si se debe ocultar, false si se debe mostrar
*/
function roi_should_hide_for_logged_in(array $settings): bool
{
// Si la opcion esta activada Y el usuario esta logueado, ocultar ads
$hideForLoggedIn = $settings['visibility']['hide_for_logged_in'] ?? false;
if ($hideForLoggedIn && is_user_logged_in()) {
return true;
}
return false;
// Delegar a UserVisibilityHelper (Plan 99.16)
return !UserVisibilityHelper::shouldShowForUser($settings['visibility'] ?? []);
}
/**
@@ -145,8 +146,8 @@ function roi_render_rail_ads(): string
return '';
}
// Verificar si ocultar para usuarios logueados
if (roi_should_hide_for_logged_in($settings)) {
// Verificar visibilidad por usuario logueado (Plan 99.16)
if (!UserVisibilityHelper::shouldShowForUser($settings['visibility'] ?? [])) {
return '';
}
@@ -200,8 +201,8 @@ function roi_enqueue_adsense_script(): void
return;
}
// Verificar si ocultar para usuarios logueados
if (roi_should_hide_for_logged_in($settings)) {
// Verificar visibilidad por usuario logueado (Plan 99.16)
if (!UserVisibilityHelper::shouldShowForUser($settings['visibility'] ?? [])) {
return;
}
@@ -258,8 +259,8 @@ function roi_inject_content_ads(string $content): string
return $content;
}
// Verificar si ocultar para usuarios logueados
if (roi_should_hide_for_logged_in($settings)) {
// Verificar visibilidad por usuario logueado (Plan 99.16)
if (!UserVisibilityHelper::shouldShowForUser($settings['visibility'] ?? [])) {
return $content;
}
@@ -463,8 +464,8 @@ function roi_render_anchor_ads(): string
return '';
}
// Verificar si ocultar para usuarios logueados
if (roi_should_hide_for_logged_in($settings)) {
// Verificar visibilidad por usuario logueado (Plan 99.16)
if (!UserVisibilityHelper::shouldShowForUser($settings['visibility'] ?? [])) {
return '';
}
@@ -512,8 +513,8 @@ function roi_render_vignette_ad(): string
return '';
}
// Verificar si ocultar para usuarios logueados
if (roi_should_hide_for_logged_in($settings)) {
// Verificar visibilidad por usuario logueado (Plan 99.16)
if (!UserVisibilityHelper::shouldShowForUser($settings['visibility'] ?? [])) {
return '';
}
@@ -583,8 +584,8 @@ function roi_enqueue_anchor_vignette_scripts(): void
return;
}
// Verificar si ocultar para usuarios logueados
if (roi_should_hide_for_logged_in($settings)) {
// Verificar visibilidad por usuario logueado (Plan 99.16)
if (!UserVisibilityHelper::shouldShowForUser($settings['visibility'] ?? [])) {
return;
}

View File

@@ -7,6 +7,7 @@ use ROITheme\Shared\Domain\Contracts\RendererInterface;
use ROITheme\Shared\Domain\Contracts\CSSGeneratorInterface;
use ROITheme\Shared\Domain\Entities\Component;
use ROITheme\Shared\Infrastructure\Services\PageVisibilityHelper;
use ROITheme\Shared\Infrastructure\Services\UserVisibilityHelper;
/**
* CtaBoxSidebarRenderer - Renderiza caja CTA en sidebar
@@ -51,6 +52,11 @@ final class CtaBoxSidebarRenderer implements RendererInterface
return '';
}
// Validar visibilidad por usuario logueado
if (!UserVisibilityHelper::shouldShowForUser($data['visibility'] ?? [])) {
return '';
}
$css = $this->generateCSS($data);
$html = $this->buildHTML($data);
$script = $this->buildScript();

View File

@@ -7,6 +7,7 @@ use ROITheme\Shared\Domain\Contracts\RendererInterface;
use ROITheme\Shared\Domain\Contracts\CSSGeneratorInterface;
use ROITheme\Shared\Domain\Entities\Component;
use ROITheme\Shared\Infrastructure\Services\PageVisibilityHelper;
use ROITheme\Shared\Infrastructure\Services\UserVisibilityHelper;
/**
* Class CtaLetsTalkRenderer
@@ -61,6 +62,11 @@ final class CtaLetsTalkRenderer implements RendererInterface
return '';
}
// Validar visibilidad por usuario logueado
if (!UserVisibilityHelper::shouldShowForUser($data['visibility'] ?? [])) {
return '';
}
// Generar CSS usando CSSGeneratorService
$css = $this->generateCSS($data);

View File

@@ -7,6 +7,7 @@ use ROITheme\Shared\Domain\Contracts\RendererInterface;
use ROITheme\Shared\Domain\Contracts\CSSGeneratorInterface;
use ROITheme\Shared\Domain\Entities\Component;
use ROITheme\Shared\Infrastructure\Services\PageVisibilityHelper;
use ROITheme\Shared\Infrastructure\Services\UserVisibilityHelper;
/**
* CtaPostRenderer - Renderiza CTA promocional debajo del contenido
@@ -41,6 +42,11 @@ final class CtaPostRenderer implements RendererInterface
return '';
}
// Validar visibilidad por usuario logueado
if (!UserVisibilityHelper::shouldShowForUser($data['visibility'] ?? [])) {
return '';
}
$css = $this->generateCSS($data);
$html = $this->buildHTML($data);

View File

@@ -7,6 +7,7 @@ use ROITheme\Shared\Domain\Contracts\RendererInterface;
use ROITheme\Shared\Domain\Contracts\CSSGeneratorInterface;
use ROITheme\Shared\Domain\Entities\Component;
use ROITheme\Shared\Infrastructure\Services\PageVisibilityHelper;
use ROITheme\Shared\Infrastructure\Services\UserVisibilityHelper;
/**
* Class TopNotificationBarRenderer
@@ -61,6 +62,11 @@ final class TopNotificationBarRenderer implements RendererInterface
return '';
}
// Validar visibilidad por usuario logueado
if (!UserVisibilityHelper::shouldShowForUser($data['visibility'] ?? [])) {
return '';
}
// Generar HTML
$html = $this->buildHTML($data);

View File

@@ -27,6 +27,13 @@
"default": false,
"editable": true,
"description": "Muestra el componente en pantallas < 992px"
},
"hide_for_logged_in": {
"type": "boolean",
"label": "Ocultar para usuarios logueados",
"default": false,
"editable": true,
"description": "No mostrar el CTA a usuarios con sesión iniciada en WordPress"
}
}
},

View File

@@ -35,6 +35,13 @@
"default": true,
"editable": true,
"description": "Inyectar CSS inline en <head> para optimizar LCP (componente above-the-fold)"
},
"hide_for_logged_in": {
"type": "boolean",
"label": "Ocultar para usuarios logueados",
"default": false,
"editable": true,
"description": "No mostrar el botón a usuarios con sesión iniciada en WordPress"
}
}
},

View File

@@ -27,6 +27,13 @@
"default": true,
"editable": true,
"description": "Muestra el componente en pantallas < 992px"
},
"hide_for_logged_in": {
"type": "boolean",
"label": "Ocultar para usuarios logueados",
"default": false,
"editable": true,
"description": "No mostrar el CTA a usuarios con sesión iniciada en WordPress"
}
}
},

View File

@@ -37,6 +37,13 @@
"default": true,
"editable": true,
"description": "Inyectar CSS inline en <head> para optimizar LCP (componente above-the-fold)"
},
"hide_for_logged_in": {
"type": "boolean",
"label": "Ocultar para usuarios logueados",
"default": false,
"editable": true,
"description": "No mostrar la barra a usuarios con sesión iniciada en WordPress"
}
}
},

View File

@@ -0,0 +1,35 @@
<?php
declare(strict_types=1);
namespace ROITheme\Shared\Infrastructure\Services;
/**
* Helper para verificar visibilidad basada en estado de autenticación del usuario.
*
* Similar a PageVisibilityHelper, proporciona método estático para evaluar
* si un componente debe ocultarse para usuarios logueados.
*
* @package ROITheme\Shared\Infrastructure\Services
* @since 1.0.0
*/
final class UserVisibilityHelper
{
/**
* Verifica si el componente debe mostrarse según estado de login.
*
* @param array $visibilityData Datos del grupo 'visibility' del componente
* @return bool true si debe mostrarse, false si debe ocultarse
*/
public static function shouldShowForUser(array $visibilityData): bool
{
$hideForLoggedIn = $visibilityData['hide_for_logged_in'] ?? false;
// Si la opción está activada Y el usuario está logueado, ocultar
if ($hideForLoggedIn && is_user_logged_in()) {
return false;
}
return true;
}
}