feat(admin): Add theme-settings component for global configurations
- Add Schemas/theme-settings.json with analytics and custom_code groups - Add ThemeSettingsFormBuilder for Admin Panel UI - Add ThemeSettingsFieldMapper for AJAX field mapping - Add ThemeSettingsRenderer for injecting GA/CSS/JS - Add ThemeSettingsInjector for wp_head/wp_footer hooks - Register component in AdminDashboardRenderer::getComponents() - Register FieldMapper in FieldMapperProvider 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
@@ -0,0 +1,135 @@
|
||||
<?php
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace ROITheme\Public\ThemeSettings\Infrastructure\Services;
|
||||
|
||||
use ROITheme\Shared\Domain\Contracts\ComponentSettingsRepositoryInterface;
|
||||
use ROITheme\Public\ThemeSettings\Infrastructure\Ui\ThemeSettingsRenderer;
|
||||
|
||||
/**
|
||||
* ThemeSettingsInjector
|
||||
*
|
||||
* Servicio que inyecta las configuraciones globales del tema
|
||||
* en los hooks de WordPress (wp_head y wp_footer).
|
||||
*
|
||||
* Responsabilidades:
|
||||
* - Registrar hooks de WordPress
|
||||
* - Obtener configuracion de theme-settings desde BD
|
||||
* - Delegar renderizado a ThemeSettingsRenderer
|
||||
* - Inyectar contenido en los hooks correspondientes
|
||||
*
|
||||
* @package ROITheme\Public\ThemeSettings\Infrastructure\Services
|
||||
*/
|
||||
final class ThemeSettingsInjector
|
||||
{
|
||||
private const COMPONENT_NAME = 'theme-settings';
|
||||
|
||||
/**
|
||||
* @param ComponentSettingsRepositoryInterface $repository Repositorio para leer configuraciones
|
||||
* @param ThemeSettingsRenderer $renderer Renderer para generar contenido
|
||||
*/
|
||||
public function __construct(
|
||||
private readonly ComponentSettingsRepositoryInterface $repository,
|
||||
private readonly ThemeSettingsRenderer $renderer
|
||||
) {}
|
||||
|
||||
/**
|
||||
* Registra los hooks de WordPress para inyeccion
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function register(): void
|
||||
{
|
||||
// Inyectar en wp_head con prioridad alta para GA y CSS
|
||||
add_action('wp_head', [$this, 'injectHeadContent'], 5);
|
||||
|
||||
// Inyectar en wp_footer con prioridad baja (al final)
|
||||
add_action('wp_footer', [$this, 'injectFooterContent'], 99);
|
||||
}
|
||||
|
||||
/**
|
||||
* Inyecta contenido en wp_head
|
||||
*
|
||||
* Callback para el hook wp_head.
|
||||
* Genera y muestra: Google Analytics, Custom CSS, Custom JS Header
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function injectHeadContent(): void
|
||||
{
|
||||
try {
|
||||
$settings = $this->getSettings();
|
||||
|
||||
if (empty($settings)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$content = $this->renderer->renderHeadContent($settings);
|
||||
|
||||
if (!empty($content)) {
|
||||
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
echo $content;
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
$this->logError('Error injecting head content', $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Inyecta contenido en wp_footer
|
||||
*
|
||||
* Callback para el hook wp_footer.
|
||||
* Genera y muestra: Custom JS Footer
|
||||
*
|
||||
* @return void
|
||||
*/
|
||||
public function injectFooterContent(): void
|
||||
{
|
||||
try {
|
||||
$settings = $this->getSettings();
|
||||
|
||||
if (empty($settings)) {
|
||||
return;
|
||||
}
|
||||
|
||||
$content = $this->renderer->renderFooterContent($settings);
|
||||
|
||||
if (!empty($content)) {
|
||||
// phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
|
||||
echo $content;
|
||||
}
|
||||
} catch (\Throwable $e) {
|
||||
$this->logError('Error injecting footer content', $e);
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Obtiene las configuraciones del componente theme-settings
|
||||
*
|
||||
* @return array Configuraciones agrupadas o array vacio si no hay
|
||||
*/
|
||||
private function getSettings(): array
|
||||
{
|
||||
return $this->repository->getComponentSettings(self::COMPONENT_NAME);
|
||||
}
|
||||
|
||||
/**
|
||||
* Registra errores en el log de WordPress
|
||||
*
|
||||
* @param string $message Mensaje de error
|
||||
* @param \Throwable $e Excepcion
|
||||
* @return void
|
||||
*/
|
||||
private function logError(string $message, \Throwable $e): void
|
||||
{
|
||||
if (defined('WP_DEBUG') && WP_DEBUG) {
|
||||
error_log(sprintf(
|
||||
'ROI Theme - ThemeSettingsInjector: %s - %s in %s:%d',
|
||||
$message,
|
||||
$e->getMessage(),
|
||||
$e->getFile(),
|
||||
$e->getLine()
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user