Files
roi-theme/Public/ThemeSettings/Infrastructure/Ui/ThemeSettingsRenderer.php
FrankZamora b70e11be62 refactor: move Analytics from ThemeSettings to AdsensePlacement
- Remove Analytics and AdSense tabs from theme-settings component
- Add Analytics group to adsense-placement component
- Add roi_enqueue_analytics_script() for GA4/UA support
- Clean up ThemeSettings to only handle custom code (CSS/JS)
- Update FormBuilders and FieldMappers accordingly

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-27 18:43:39 -06:00

161 lines
4.1 KiB
PHP

<?php
declare(strict_types=1);
namespace ROITheme\Public\ThemeSettings\Infrastructure\Ui;
use ROITheme\Shared\Domain\Contracts\RendererInterface;
use ROITheme\Shared\Domain\Entities\Component;
/**
* ThemeSettingsRenderer
*
* Renderizador del componente Theme Settings.
* A diferencia de otros componentes, no renderiza HTML visual
* sino que genera codigo para inyectar en wp_head y wp_footer.
*
* NOTA: Este es un componente especial que solo maneja codigo personalizado.
* Analytics y AdSense se gestionan desde el componente adsense-placement.
*
* Responsabilidades:
* - Generar CSS personalizado
* - Generar JavaScript para header
* - Generar JavaScript para footer
*
* @package ROITheme\Public\ThemeSettings\Infrastructure\Ui
*/
final class ThemeSettingsRenderer implements RendererInterface
{
/**
* {@inheritDoc}
*
* Para este componente, render() no se usa directamente.
* Se usan los metodos especificos: renderHeadContent() y renderFooterContent()
*/
public function render(Component $component): string
{
// Este componente no renderiza HTML visual
// Los contenidos se inyectan via hooks wp_head y wp_footer
return '';
}
/**
* {@inheritDoc}
*/
public function supports(string $componentType): bool
{
return $componentType === 'theme-settings';
}
/**
* Genera contenido para wp_head
*
* Incluye:
* - Custom CSS (si configurado)
* - Custom JS Header (si configurado)
*
* @param array $data Datos del componente desde BD
* @return string Contenido para wp_head
*/
public function renderHeadContent(array $data): string
{
$output = '';
// Custom CSS
$cssOutput = $this->renderCustomCSS($data);
if (!empty($cssOutput)) {
$output .= $cssOutput . "\n";
}
// Custom JS Header
$jsHeaderOutput = $this->renderCustomJSHeader($data);
if (!empty($jsHeaderOutput)) {
$output .= $jsHeaderOutput . "\n";
}
return $output;
}
/**
* Genera contenido para wp_footer
*
* Incluye:
* - Custom JS Footer (si configurado)
*
* @param array $data Datos del componente desde BD
* @return string Contenido para wp_footer
*/
public function renderFooterContent(array $data): string
{
return $this->renderCustomJSFooter($data);
}
/**
* Genera el CSS personalizado
*
* @param array $data Datos del componente
* @return string Bloque style o vacio si no hay CSS
*/
private function renderCustomCSS(array $data): string
{
$css = trim($data['custom_code']['custom_css'] ?? '');
if (empty($css)) {
return '';
}
return sprintf(
'<!-- Custom CSS (ROI Theme) -->
<style id="roi-theme-custom-css">
%s
</style>',
$css // No escapar CSS - usuario avanzado responsable
);
}
/**
* Genera el JavaScript personalizado para header
*
* @param array $data Datos del componente
* @return string Bloque script o vacio si no hay JS
*/
private function renderCustomJSHeader(array $data): string
{
$js = trim($data['custom_code']['custom_js_header'] ?? '');
if (empty($js)) {
return '';
}
return sprintf(
'<!-- Custom JS Header (ROI Theme) -->
<script id="roi-theme-custom-js-header">
%s
</script>',
$js // No escapar JS - usuario avanzado responsable
);
}
/**
* Genera el JavaScript personalizado para footer
*
* @param array $data Datos del componente
* @return string Bloque script o vacio si no hay JS
*/
private function renderCustomJSFooter(array $data): string
{
$js = trim($data['custom_code']['custom_js_footer'] ?? '');
if (empty($js)) {
return '';
}
return sprintf(
'<!-- Custom JS Footer (ROI Theme) -->
<script id="roi-theme-custom-js-footer">
%s
</script>',
$js // No escapar JS - usuario avanzado responsable
);
}
}