From 4d5cc1a58ce50dddc3e7cf47a9ccffe8fb0dab40 Mon Sep 17 00:00:00 2001 From: FrankZamora Date: Fri, 28 Nov 2025 10:31:49 -0600 Subject: [PATCH] feat(adsense): agregar opcion para ocultar anuncios a usuarios logueados MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit - Nuevo campo hide_for_logged_in en schema visibility group - Switch en panel de administracion con icono bi-person-lock - Mapeo en FieldMapper para persistencia - Validacion en roi_render_ad_slot, roi_render_rail_ads, roi_enqueue_adsense_script y roi_inject_content_ads - No carga script ni muestra ads si usuario tiene sesion activa 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude --- .../AdsensePlacementFieldMapper.php | 1 + .../Ui/AdsensePlacementFormBuilder.php | 7 ++++ Inc/adsense-placement.php | 35 +++++++++++++++++++ Schemas/adsense-placement.json | 7 ++++ 4 files changed, 50 insertions(+) diff --git a/Admin/AdsensePlacement/Infrastructure/FieldMapping/AdsensePlacementFieldMapper.php b/Admin/AdsensePlacement/Infrastructure/FieldMapping/AdsensePlacementFieldMapper.php index c4abf4ec..270400a9 100644 --- a/Admin/AdsensePlacement/Infrastructure/FieldMapping/AdsensePlacementFieldMapper.php +++ b/Admin/AdsensePlacement/Infrastructure/FieldMapping/AdsensePlacementFieldMapper.php @@ -19,6 +19,7 @@ final class AdsensePlacementFieldMapper implements FieldMapperInterface 'adsense-placementEnabled' => ['group' => 'visibility', 'attribute' => 'is_enabled'], 'adsense-placementShowOnMobile' => ['group' => 'visibility', 'attribute' => 'show_on_mobile'], 'adsense-placementShowOnDesktop' => ['group' => 'visibility', 'attribute' => 'show_on_desktop'], + 'adsense-placementHideForLoggedIn' => ['group' => 'visibility', 'attribute' => 'hide_for_logged_in'], // ANALYTICS (Google Analytics) 'adsense-placementAnalyticsEnabled' => ['group' => 'analytics', 'attribute' => 'analytics_enabled'], diff --git a/Admin/AdsensePlacement/Infrastructure/Ui/AdsensePlacementFormBuilder.php b/Admin/AdsensePlacement/Infrastructure/Ui/AdsensePlacementFormBuilder.php index 77164716..71ca7b81 100644 --- a/Admin/AdsensePlacement/Infrastructure/Ui/AdsensePlacementFormBuilder.php +++ b/Admin/AdsensePlacement/Infrastructure/Ui/AdsensePlacementFormBuilder.php @@ -86,6 +86,13 @@ final class AdsensePlacementFormBuilder $html .= ' '; $html .= ''; + // Opcion para ocultar anuncios a usuarios logueados + $html .= '
'; + $hideForLoggedIn = $this->renderer->getFieldValue($cid, 'visibility', 'hide_for_logged_in', false); + $html .= $this->buildSwitch($cid . 'HideForLoggedIn', 'Ocultar para usuarios logueados', $hideForLoggedIn, 'bi-person-lock'); + $html .= 'No mostrar anuncios a usuarios con sesion iniciada en WordPress'; + $html .= '
'; + $html .= ' '; $html .= ''; diff --git a/Inc/adsense-placement.php b/Inc/adsense-placement.php index f9e29bac..4cdfe4b9 100644 --- a/Inc/adsense-placement.php +++ b/Inc/adsense-placement.php @@ -37,6 +37,11 @@ function roi_render_ad_slot(string $location): string return ''; } + // Verificar si ocultar para usuarios logueados + if (roi_should_hide_for_logged_in($settings)) { + return ''; + } + // Verificar exclusiones if (roi_is_ad_excluded($settings)) { return ''; @@ -55,6 +60,21 @@ function roi_render_ad_slot(string $location): string } } +/** + * Verifica si se deben ocultar anuncios para usuarios logueados + */ +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; +} + /** * Verifica si el contenido actual esta excluido */ @@ -108,6 +128,11 @@ function roi_render_rail_ads(): string return ''; } + // Verificar si ocultar para usuarios logueados + if (roi_should_hide_for_logged_in($settings)) { + return ''; + } + // Verificar exclusiones if (roi_is_ad_excluded($settings)) { return ''; @@ -153,6 +178,11 @@ function roi_enqueue_adsense_script(): void return; } + // Verificar si ocultar para usuarios logueados + if (roi_should_hide_for_logged_in($settings)) { + return; + } + $publisherId = $settings['content']['publisher_id'] ?? ''; if (empty($publisherId)) { return; @@ -201,6 +231,11 @@ function roi_inject_content_ads(string $content): string return $content; } + // Verificar si ocultar para usuarios logueados + if (roi_should_hide_for_logged_in($settings)) { + return $content; + } + // Verificar exclusiones if (roi_is_ad_excluded($settings)) { return $content; diff --git a/Schemas/adsense-placement.json b/Schemas/adsense-placement.json index 4c4eb060..e9f563bf 100644 --- a/Schemas/adsense-placement.json +++ b/Schemas/adsense-placement.json @@ -28,6 +28,13 @@ "default": true, "editable": true, "description": "Muestra anuncios en pantallas moviles (<992px)" + }, + "hide_for_logged_in": { + "type": "boolean", + "label": "Ocultar para usuarios logueados", + "default": false, + "editable": true, + "description": "No mostrar anuncios a usuarios con sesion iniciada en WordPress" } } },