feat(admin): Add Google AdSense Auto Ads support to theme-settings

- Add adsense group to theme-settings.json schema (v1.2.0)
- Add adsense_publisher_id and adsense_auto_ads fields
- Add AdSense card UI in ThemeSettingsFormBuilder
- Add field mappings in ThemeSettingsFieldMapper
- Add renderAdSenseAutoAds() method in ThemeSettingsRenderer
- Inject AdSense script in wp_head when configured

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
FrankZamora
2025-11-26 22:06:42 -06:00
parent 7cc5f194e9
commit f35b60ed4e
4 changed files with 95 additions and 6 deletions

View File

@@ -28,6 +28,10 @@ final class ThemeSettingsFieldMapper implements FieldMapperInterface
'themeSettingsGaTrackingId' => ['group' => 'analytics', 'attribute' => 'ga_tracking_id'], 'themeSettingsGaTrackingId' => ['group' => 'analytics', 'attribute' => 'ga_tracking_id'],
'themeSettingsGaAnonymizeIp' => ['group' => 'analytics', 'attribute' => 'ga_anonymize_ip'], 'themeSettingsGaAnonymizeIp' => ['group' => 'analytics', 'attribute' => 'ga_anonymize_ip'],
// AdSense
'themeSettingsAdsensePublisherId' => ['group' => 'adsense', 'attribute' => 'adsense_publisher_id'],
'themeSettingsAdsenseAutoAds' => ['group' => 'adsense', 'attribute' => 'adsense_auto_ads'],
// Custom Code // Custom Code
'themeSettingsCustomCss' => ['group' => 'custom_code', 'attribute' => 'custom_css'], 'themeSettingsCustomCss' => ['group' => 'custom_code', 'attribute' => 'custom_css'],
'themeSettingsCustomJsHeader' => ['group' => 'custom_code', 'attribute' => 'custom_js_header'], 'themeSettingsCustomJsHeader' => ['group' => 'custom_code', 'attribute' => 'custom_js_header'],

View File

@@ -9,7 +9,7 @@ use ROITheme\Admin\Infrastructure\Ui\AdminDashboardRenderer;
* FormBuilder para Theme Settings * FormBuilder para Theme Settings
* *
* RESPONSABILIDAD: Generar formulario de configuraciones globales del tema * RESPONSABILIDAD: Generar formulario de configuraciones globales del tema
* (analytics, codigo personalizado) * (analytics, adsense, codigo personalizado)
* *
* NOTA: Logo/branding se gestiona desde el componente navbar * NOTA: Logo/branding se gestiona desde el componente navbar
* *
@@ -29,9 +29,10 @@ final class ThemeSettingsFormBuilder
$html .= '<div class="row g-3">'; $html .= '<div class="row g-3">';
// Columna izquierda - Analytics // Columna izquierda - Analytics + AdSense
$html .= '<div class="col-lg-6">'; $html .= '<div class="col-lg-6">';
$html .= $this->buildAnalyticsGroup($componentId); $html .= $this->buildAnalyticsGroup($componentId);
$html .= $this->buildAdSenseGroup($componentId);
$html .= '</div>'; $html .= '</div>';
// Columna derecha - Custom Code // Columna derecha - Custom Code
@@ -55,7 +56,7 @@ final class ThemeSettingsFormBuilder
$html .= ' Configuraciones Globales del Tema'; $html .= ' Configuraciones Globales del Tema';
$html .= ' </h3>'; $html .= ' </h3>';
$html .= ' <p class="mb-0 small" style="opacity: 0.85;">'; $html .= ' <p class="mb-0 small" style="opacity: 0.85;">';
$html .= ' Analytics y Codigo Personalizado'; $html .= ' Analytics, AdSense y Codigo Personalizado';
$html .= ' </p>'; $html .= ' </p>';
$html .= ' </div>'; $html .= ' </div>';
$html .= ' <button type="button" class="btn btn-sm btn-outline-light btn-reset-defaults" data-component="theme-settings">'; $html .= ' <button type="button" class="btn btn-sm btn-outline-light btn-reset-defaults" data-component="theme-settings">';
@@ -96,6 +97,34 @@ final class ThemeSettingsFormBuilder
return $html; return $html;
} }
private function buildAdSenseGroup(string $componentId): string
{
$html = '<div class="card shadow-sm mb-3" style="border-left: 4px solid #1e3a5f;">';
$html .= ' <div class="card-body">';
$html .= ' <h5 class="fw-bold mb-3" style="color: #1e3a5f;">';
$html .= ' <i class="bi bi-badge-ad me-2" style="color: #FF8600;"></i>';
$html .= ' Google AdSense';
$html .= ' </h5>';
$publisherId = $this->renderer->getFieldValue($componentId, 'adsense', 'adsense_publisher_id', '');
$html .= $this->buildTextInput('themeSettingsAdsensePublisherId', 'Publisher ID', 'bi-key', $publisherId);
$html .= ' <div class="form-text small mb-2">Formato: ca-pub-1234567890123456</div>';
$autoAds = $this->renderer->getFieldValue($componentId, 'adsense', 'adsense_auto_ads', false);
$html .= $this->buildSwitch('themeSettingsAdsenseAutoAds', 'Activar Auto Ads', 'bi-magic', $autoAds);
$html .= ' <div class="alert alert-info small mb-0 mt-2">';
$html .= ' <i class="bi bi-info-circle me-1"></i>';
$html .= ' Auto Ads permite que Google coloque anuncios automaticamente en las mejores ubicaciones.';
$html .= ' </div>';
$html .= ' </div>';
$html .= '</div>';
return $html;
}
private function buildCustomCodeGroup(string $componentId): string private function buildCustomCodeGroup(string $componentId): string
{ {
$html = '<div class="card shadow-sm mb-3" style="border-left: 4px solid #1e3a5f;">'; $html = '<div class="card shadow-sm mb-3" style="border-left: 4px solid #1e3a5f;">';

View File

@@ -20,6 +20,7 @@ use ROITheme\Shared\Domain\Entities\Component;
* *
* Responsabilidades: * Responsabilidades:
* - Generar script de Google Analytics * - Generar script de Google Analytics
* - Generar script de Google AdSense Auto Ads
* - Generar CSS personalizado * - Generar CSS personalizado
* - Generar JavaScript para header * - Generar JavaScript para header
* - Generar JavaScript para footer * - Generar JavaScript para footer
@@ -54,6 +55,7 @@ final class ThemeSettingsRenderer implements RendererInterface
* *
* Incluye: * Incluye:
* - Google Analytics script (si configurado) * - Google Analytics script (si configurado)
* - Google AdSense Auto Ads script (si configurado)
* - Custom CSS (si configurado) * - Custom CSS (si configurado)
* - Custom JS Header (si configurado) * - Custom JS Header (si configurado)
* *
@@ -75,6 +77,12 @@ final class ThemeSettingsRenderer implements RendererInterface
$output .= $gaOutput . "\n"; $output .= $gaOutput . "\n";
} }
// Google AdSense Auto Ads
$adsenseOutput = $this->renderAdSenseAutoAds($data);
if (!empty($adsenseOutput)) {
$output .= $adsenseOutput . "\n";
}
// Custom CSS // Custom CSS
$cssOutput = $this->renderCustomCSS($data); $cssOutput = $this->renderCustomCSS($data);
if (!empty($cssOutput)) { if (!empty($cssOutput)) {
@@ -237,6 +245,34 @@ ga("send", "pageview");
return false; return false;
} }
/**
* Genera el script de Google AdSense Auto Ads
*
* @param array $data Datos del componente
* @return string Script de AdSense o vacio si no configurado
*/
private function renderAdSenseAutoAds(array $data): string
{
$publisherId = trim($data['adsense']['adsense_publisher_id'] ?? '');
$autoAdsEnabled = ($data['adsense']['adsense_auto_ads'] ?? false) === true;
// Solo mostrar si tiene publisher ID y auto ads esta activado
if (empty($publisherId) || !$autoAdsEnabled) {
return '';
}
// Validar formato del publisher ID (ca-pub-XXXXXXXXXX)
if (!preg_match('/^ca-pub-\d+$/', $publisherId)) {
return '';
}
return sprintf(
'<!-- Google AdSense Auto Ads (ROI Theme) -->
<script async src="https://pagead2.googlesyndication.com/pagead/js/adsbygoogle.js?client=%s" crossorigin="anonymous"></script>',
esc_attr($publisherId)
);
}
/** /**
* Genera el CSS personalizado * Genera el CSS personalizado
* *

View File

@@ -1,7 +1,7 @@
{ {
"component_name": "theme-settings", "component_name": "theme-settings",
"version": "1.1.0", "version": "1.2.0",
"description": "Configuraciones globales del tema: analytics y codigo personalizado", "description": "Configuraciones globales del tema: analytics, adsense y codigo personalizado",
"groups": { "groups": {
"analytics": { "analytics": {
"label": "Analytics", "label": "Analytics",
@@ -23,9 +23,29 @@
} }
} }
}, },
"adsense": {
"label": "Google AdSense",
"priority": 20,
"fields": {
"adsense_publisher_id": {
"type": "text",
"label": "Publisher ID",
"default": "",
"editable": true,
"description": "ID de publicador de AdSense (ej: ca-pub-1234567890123456)"
},
"adsense_auto_ads": {
"type": "boolean",
"label": "Activar Auto Ads",
"default": false,
"editable": true,
"description": "Permite que Google coloque anuncios automaticamente en las mejores ubicaciones"
}
}
},
"custom_code": { "custom_code": {
"label": "Codigo Personalizado", "label": "Codigo Personalizado",
"priority": 20, "priority": 30,
"fields": { "fields": {
"custom_css": { "custom_css": {
"type": "textarea", "type": "textarea",