Migración completa a Clean Architecture con componentes funcionales

- Reorganización de estructura: Admin/, Public/, Shared/, Schemas/
- 12 componentes migrados: TopNotificationBar, Navbar, CtaLetsTalk, Hero,
  FeaturedImage, TableOfContents, CtaBoxSidebar, SocialShare, CtaPost,
  RelatedPost, ContactForm, Footer
- Panel de administración con tabs Bootstrap 5 funcionales
- Schemas JSON para configuración de componentes
- Renderers dinámicos con CSSGeneratorService (cero CSS hardcodeado)
- FormBuilders para UI admin con Design System consistente
- Fix: Bootstrap JS cargado en header para tabs funcionales
- Fix: buildTextInput maneja valores mixed (bool/string)
- Eliminación de estructura legacy (src/, admin/, assets/css/componente-*)

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

Co-Authored-By: Claude <noreply@anthropic.com>
This commit is contained in:
FrankZamora
2025-11-25 21:20:06 -06:00
parent 90de6df77c
commit 0846a3bf03
224 changed files with 21670 additions and 17816 deletions

View File

@@ -1,66 +1,240 @@
<?php
declare(strict_types=1);
/**
* ROI Theme Functions and Definitions
* ROI Theme - Clean Architecture Bootstrap
*
* @package ROI_Theme
* @since 1.0.0
* Este archivo es el punto de entrada del tema.
* Solo contiene bootstrap (carga del autoloader e inicialización del DI Container).
* TODO el código de lógica de negocio está en admin/, public/, shared/
*
* @package ROITheme
* @version 1.0.0
*/
// Exit if accessed directly
// Prevenir acceso directo
if (!defined('ABSPATH')) {
exit;
}
/**
* ========================================================================
* BOOTSTRAP CLEAN ARCHITECTURE (Fase 1)
* ========================================================================
*
* Carga el autoloader de Composer y el DI Container.
* Esta sección inicializa la arquitectura limpia del tema.
*/
// Definir constante de versión del tema
define('ROI_VERSION', '1.0.19');
// Load Composer autoloader
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
// =============================================================================
// 1. CARGAR AUTOLOADER MANUAL
// =============================================================================
// IMPORTANTE: Cargar functions-addon.php PRIMERO para registrar el autoloader
// que permite cargar clases de ROITheme automáticamente
require_once __DIR__ . '/functions-addon.php';
// =============================================================================
// 2. CARGAR ARCHIVOS DE INC/ (ANTES DE LA CONFIGURACIÓN)
// =============================================================================
// IMPORTANTE: Cargar archivos inc/ ANTES del DI Container
// para que los estilos y scripts se registren correctamente
require_once get_template_directory() . '/Inc/sanitize-functions.php';
require_once get_template_directory() . '/Inc/theme-options-helpers.php';
require_once get_template_directory() . '/Inc/nav-walker.php';
require_once get_template_directory() . '/Inc/enqueue-scripts.php';
require_once get_template_directory() . '/Inc/customizer-fonts.php';
require_once get_template_directory() . '/Inc/seo.php';
require_once get_template_directory() . '/Inc/performance.php';
require_once get_template_directory() . '/Inc/critical-css.php';
require_once get_template_directory() . '/Inc/image-optimization.php';
require_once get_template_directory() . '/Inc/template-functions.php';
require_once get_template_directory() . '/Inc/template-tags.php';
require_once get_template_directory() . '/Inc/featured-image.php';
require_once get_template_directory() . '/Inc/category-badge.php';
require_once get_template_directory() . '/Inc/adsense-delay.php';
require_once get_template_directory() . '/Inc/related-posts.php';
require_once get_template_directory() . '/Inc/toc.php';
require_once get_template_directory() . '/Inc/apu-tables.php';
require_once get_template_directory() . '/Inc/search-disable.php';
require_once get_template_directory() . '/Inc/comments-disable.php';
require_once get_template_directory() . '/Inc/social-share.php';
require_once get_template_directory() . '/Inc/cta-ab-testing.php';
require_once get_template_directory() . '/Inc/customizer-cta.php';
// =============================================================================
// 3. INICIALIZAR DI CONTAINER (Clean Architecture)
// =============================================================================
use ROITheme\Shared\Infrastructure\DI\DIContainer;
try {
global $wpdb;
// Path a los schemas
$schemas_path = get_template_directory() . '/schemas';
// Crear instancia del DI Container con dependencias
$container = new DIContainer($wpdb, $schemas_path);
// TODO: Inicializar controladores cuando estén implementados (Fase 5+)
// $ajaxController = $container->getAjaxController();
} catch (\Throwable $e) {
// Manejar errores de inicialización (no crítico, solo log)
if (defined('WP_DEBUG') && WP_DEBUG) {
error_log('ROI Theme: Failed to initialize DI Container: ' . $e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine());
}
// NO hacer return - permitir que el tema continúe funcionando
$container = null;
}
// Initialize DI Container
use ROITheme\Infrastructure\DI\DIContainer;
// =============================================================================
// 3.1. INICIALIZAR PANEL DE ADMINISTRACIÓN (Clean Architecture)
// =============================================================================
/**
* Get DI Container instance
*
* Helper function to access the DI Container throughout the theme.
*
* @return DIContainer
*/
function roi_container(): DIContainer {
return DIContainer::getInstance();
use ROITheme\Admin\Domain\ValueObjects\MenuItem;
use ROITheme\Admin\Application\UseCases\RenderDashboardUseCase;
use ROITheme\Admin\Infrastructure\Ui\AdminDashboardRenderer;
use ROITheme\Admin\Infrastructure\API\WordPress\AdminMenuRegistrar;
use ROITheme\Admin\Infrastructure\Services\AdminAssetEnqueuer;
try {
// Obtener Use Case para cargar configuraciones
$getComponentSettingsUseCase = $container?->getGetComponentSettingsUseCase();
// Crear MenuItem con configuración del panel
$menuItem = new MenuItem(
pageTitle: 'ROI Theme - Panel de Administración',
menuTitle: 'ROI Theme',
capability: 'manage_options',
menuSlug: 'roi-theme-admin',
icon: 'dashicons-admin-settings',
position: 60
);
// Crear renderer del dashboard con inyección del Use Case
$dashboardRenderer = new AdminDashboardRenderer($getComponentSettingsUseCase);
// Crear caso de uso para renderizar
$renderDashboardUseCase = new RenderDashboardUseCase($dashboardRenderer);
// Crear y registrar el menú de administración
$adminMenuRegistrar = new AdminMenuRegistrar($menuItem, $renderDashboardUseCase);
$adminMenuRegistrar->register();
// Crear y registrar el enqueuer de assets
$adminAssetEnqueuer = new AdminAssetEnqueuer(get_template_directory_uri());
$adminAssetEnqueuer->register();
// Obtener Use Case para guardar configuraciones
$saveComponentSettingsUseCase = $container?->getSaveComponentSettingsUseCase();
// Crear y registrar el handler AJAX con inyección del Use Case
$adminAjaxHandler = new \ROITheme\Admin\Infrastructure\API\WordPress\AdminAjaxHandler($saveComponentSettingsUseCase);
$adminAjaxHandler->register();
// Crear y registrar el handler AJAX para el Contact Form (público)
$contactFormAjaxHandler = new \ROITheme\Public\ContactForm\Infrastructure\Api\WordPress\ContactFormAjaxHandler(
$container->getComponentSettingsRepository()
);
$contactFormAjaxHandler->register();
// Crear y registrar el handler AJAX para Newsletter (público)
$newsletterAjaxHandler = new \ROITheme\Public\Footer\Infrastructure\Api\WordPress\NewsletterAjaxHandler(
$container->getComponentSettingsRepository()
);
$newsletterAjaxHandler->register();
// Log en modo debug
if (defined('WP_DEBUG') && WP_DEBUG) {
error_log('ROI Theme: Admin Panel initialized successfully');
}
} catch (\Throwable $e) {
// Manejar errores de inicialización del panel
if (defined('WP_DEBUG') && WP_DEBUG) {
error_log('ROI Theme: Failed to initialize Admin Panel: ' . $e->getMessage() . ' in ' . $e->getFile() . ':' . $e->getLine());
}
}
/**
* ========================================================================
* END BOOTSTRAP
* ========================================================================
*/
// =============================================================================
// 4. CONFIGURACIÓN DEL TEMA
// =============================================================================
/**
* ========================================================================
* THEME DATABASE TABLES SETUP
* ========================================================================
* Setup del tema
*
* Crea las tablas del tema cuando se activa.
* Esto asegura que el tema sea portable y funcione en cualquier instalación WordPress.
* Configuraciones básicas de WordPress theme support
*/
add_action('after_setup_theme', function() {
// Soporte para título del documento
add_theme_support('title-tag');
// Soporte para imágenes destacadas
add_theme_support('post-thumbnails');
// Soporte para HTML5
add_theme_support('html5', [
'search-form',
'comment-form',
'comment-list',
'gallery',
'caption',
'style',
'script'
]);
// Soporte para feeds automáticos
add_theme_support('automatic-feed-links');
// Registro de ubicaciones de menús
register_nav_menus([
'primary' => __('Primary Menu', 'roi-theme'),
'footer' => __('Footer Menu', 'roi-theme'),
'footer_menu_1' => __('Footer Menu 1 (Widget 1)', 'roi-theme'),
'footer_menu_2' => __('Footer Menu 2 (Widget 2)', 'roi-theme'),
'footer_menu_3' => __('Footer Menu 3 (Widget 3)', 'roi-theme'),
]);
// TODO: Agregar más configuraciones según sea necesario
});
// =============================================================================
// 5. HOOKS DE INICIALIZACIÓN (Para fases posteriores)
// =============================================================================
/**
* Hook para sincronización de schemas
* TODO: Implementar en Fase 6
*/
// add_action('admin_init', function() use ($container) {
// $syncService = $container->getSchemaSyncService();
// // Verificar si hay schemas desactualizados
// });
/**
* Hook para detección de schemas desactualizados
* TODO: Implementar en Fase 6
*/
// add_action('admin_notices', function() use ($container) {
// // Mostrar aviso si hay schemas desactualizados
// });
// =============================================================================
// 5. INFORMACIÓN DE DEBUG (Solo en desarrollo)
// =============================================================================
if (defined('WP_DEBUG') && WP_DEBUG) {
// Registrar que el tema se inicializó correctamente
error_log('ROI Theme: Bootstrap completed successfully');
}
// =============================================================================
// 6. INSTALACIÓN DE TABLAS DEL TEMA
// =============================================================================
/**
* Crear tablas del tema en la activación
*
* Este hook se ejecuta cuando el tema se activa en WordPress.
* Crea las tablas necesarias si no existen.
*
* @since 1.0.19
*/
add_action('after_switch_theme', function() {
global $wpdb;
@@ -68,39 +242,25 @@ add_action('after_switch_theme', function() {
$charset_collate = $wpdb->get_charset_collate();
// Tabla de components
$table_components = $wpdb->prefix . 'roi_theme_components';
$sql_components = "CREATE TABLE {$table_components} (
// Tabla de configuración de componentes (normalizada)
$table_settings = $wpdb->prefix . 'roi_theme_component_settings';
$sql_settings = "CREATE TABLE {$table_settings} (
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
component_name VARCHAR(50) NOT NULL,
configuration LONGTEXT NOT NULL,
content LONGTEXT,
visibility TEXT NOT NULL,
is_enabled TINYINT(1) NOT NULL DEFAULT 1,
schema_version VARCHAR(20) NOT NULL,
component_name VARCHAR(100) NOT NULL,
group_name VARCHAR(100) NOT NULL,
attribute_name VARCHAR(100) NOT NULL,
attribute_value LONGTEXT NOT NULL,
is_editable TINYINT(1) NOT NULL DEFAULT 1,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE KEY component_name (component_name),
INDEX idx_enabled (is_enabled),
INDEX idx_schema_version (schema_version)
UNIQUE KEY unique_setting (component_name, group_name, attribute_name),
INDEX idx_component (component_name),
INDEX idx_editable (is_editable)
) {$charset_collate};";
// Tabla de defaults/schemas
$table_defaults = $wpdb->prefix . 'roi_theme_defaults';
$sql_defaults = "CREATE TABLE {$table_defaults} (
id BIGINT(20) UNSIGNED NOT NULL AUTO_INCREMENT,
component_name VARCHAR(50) NOT NULL,
default_schema LONGTEXT NOT NULL,
created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
PRIMARY KEY (id),
UNIQUE KEY component_name (component_name)
) {$charset_collate};";
// Crear/actualizar tablas
dbDelta($sql_components);
dbDelta($sql_defaults);
// Crear/actualizar tabla
dbDelta($sql_settings);
// Log en modo debug
if (defined('WP_DEBUG') && WP_DEBUG) {
@@ -108,268 +268,11 @@ add_action('after_switch_theme', function() {
}
});
/**
* Theme Version
*/
define('ROI_VERSION', '1.0.19');
/**
* Theme Setup
*/
function roi_theme_setup() {
// Make theme available for translation
load_theme_textdomain('roi-theme', get_template_directory() . '/languages');
// Let WordPress manage the document title
add_theme_support('title-tag');
// Enable support for Post Thumbnails
add_theme_support('post-thumbnails');
// Add image sizes
add_image_size('roi-thumbnail', 400, 300, true);
add_image_size('roi-medium', 800, 600, true);
add_image_size('roi-large', 1200, 900, true);
add_image_size('roi-featured-large', 1200, 600, true);
add_image_size('roi-featured-medium', 800, 400, true);
// Switch default core markup to output valid HTML5
add_theme_support('html5', array(
'gallery',
'caption',
'style',
'script',
));
// Set content width
if (!isset($content_width)) {
$content_width = 1200;
}
// Register navigation menus
register_nav_menus(array(
'primary' => __('Primary Menu', 'roi-theme'),
'footer' => __('Footer Menu', 'roi-theme'),
));
}
add_action('after_setup_theme', 'roi_theme_setup');
/**
* Set the content width in pixels
*/
function roi_content_width() {
$GLOBALS['content_width'] = apply_filters('roi_content_width', 1200);
}
add_action('after_setup_theme', 'roi_content_width', 0);
/**
* ELIMINADO: roi_enqueue_scripts()
*
* Esta función estaba duplicando la carga de CSS.
* El sistema modular en inc/enqueue-scripts.php ya carga style.css como 'roi-main-style' (prioridad 5).
* Esta función duplicada lo cargaba otra vez como 'roi-theme-style' (prioridad 10).
*
* Fecha eliminación: 2025-01-08
* Issue: #128 - Footer Contact Form
*/
/**
* Register Widget Areas
*/
function roi_register_widget_areas() {
// Primary Sidebar
register_sidebar(array(
'name' => __('Primary Sidebar', 'roi-theme'),
'id' => 'sidebar-1',
'description' => __('Main sidebar widget area', 'roi-theme'),
'before_widget' => '<section id="%1$s" class="widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<h2 class="widget-title">',
'after_title' => '</h2>',
));
// Footer Contact Form (Issue #37) - ARRIBA de los 4 widgets
register_sidebar(array(
'name' => __('Footer Contact Form', 'roi-theme'),
'id' => 'footer-contact',
'description' => __('Área de contacto arriba de los 4 widgets del footer', 'roi-theme'),
'before_widget' => '<section id="%1$s" class="footer-contact-widget %2$s">',
'after_widget' => '</section>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
));
// Footer Widget Areas
for ($i = 1; $i <= 4; $i++) {
register_sidebar(array(
'name' => sprintf(__('Footer Column %d', 'roi-theme'), $i),
'id' => 'footer-' . $i,
'description' => sprintf(__('Footer widget area %d', 'roi-theme'), $i),
'before_widget' => '<div id="%1$s" class="widget %2$s">',
'after_widget' => '</div>',
'before_title' => '<h3 class="widget-title">',
'after_title' => '</h3>',
));
}
}
add_action('widgets_init', 'roi_register_widget_areas');
/**
* Configure locale and date format
*/
function roi_configure_locale() {
// Set locale to es_MX
add_filter('locale', function($locale) {
return 'es_MX';
});
}
add_action('after_setup_theme', 'roi_configure_locale');
/**
* Custom date format
*/
function roi_custom_date_format($format) {
return 'd/m/Y'; // Format: day/month/year
}
add_filter('date_format', 'roi_custom_date_format');
/**
* Include modular files
*/
// Sanitize Functions (load first to avoid redeclaration errors)
if (file_exists(get_template_directory() . '/inc/sanitize-functions.php')) {
require_once get_template_directory() . '/inc/sanitize-functions.php';
}
// Theme Options Helpers (load first as other files may depend on it)
if (file_exists(get_template_directory() . '/inc/theme-options-helpers.php')) {
require_once get_template_directory() . '/inc/theme-options-helpers.php';
}
// Admin Options API (Theme Options)
// Cargar solo options-api.php para funciones auxiliares como roi_get_default_options()
// theme-options.php está desactivado porque el menú se registra en admin/includes/class-admin-menu.php
if (is_admin()) {
if (file_exists(get_template_directory() . '/admin/theme-options/options-api.php')) {
require_once get_template_directory() . '/admin/theme-options/options-api.php';
}
}
// Bootstrap Nav Walker
if (file_exists(get_template_directory() . '/inc/nav-walker.php')) {
require_once get_template_directory() . '/inc/nav-walker.php';
}
// Bootstrap and Script Enqueuing
if (file_exists(get_template_directory() . '/inc/enqueue-scripts.php')) {
require_once get_template_directory() . '/inc/enqueue-scripts.php';
}
// Font customizer options
if (file_exists(get_template_directory() . '/inc/customizer-fonts.php')) {
require_once get_template_directory() . '/inc/customizer-fonts.php';
}
// SEO optimizations and Rank Math compatibility
if (file_exists(get_template_directory() . '/inc/seo.php')) {
require_once get_template_directory() . '/inc/seo.php';
}
// Performance optimizations
if (file_exists(get_template_directory() . '/inc/performance.php')) {
require_once get_template_directory() . '/inc/performance.php';
}
// Critical CSS (optional, disabled by default)
if (file_exists(get_template_directory() . '/inc/critical-css.php')) {
require_once get_template_directory() . '/inc/critical-css.php';
}
// Image optimization
if (file_exists(get_template_directory() . '/inc/image-optimization.php')) {
require_once get_template_directory() . '/inc/image-optimization.php';
}
// Template functions
if (file_exists(get_template_directory() . '/inc/template-functions.php')) {
require_once get_template_directory() . '/inc/template-functions.php';
}
// Template tags
if (file_exists(get_template_directory() . '/inc/template-tags.php')) {
require_once get_template_directory() . '/inc/template-tags.php';
}
// Featured image functions
if (file_exists(get_template_directory() . '/inc/featured-image.php')) {
require_once get_template_directory() . '/inc/featured-image.php';
}
// Category badge functions
if (file_exists(get_template_directory() . '/inc/category-badge.php')) {
require_once get_template_directory() . '/inc/category-badge.php';
}
// AdSense delay loading
if (file_exists(get_template_directory() . '/inc/adsense-delay.php')) {
require_once get_template_directory() . '/inc/adsense-delay.php';
}
// Related posts functionality
if (file_exists(get_template_directory() . '/inc/related-posts.php')) {
require_once get_template_directory() . '/inc/related-posts.php';
}
// Related posts configuration options (admin helpers)
if (file_exists(get_template_directory() . '/admin/theme-options/related-posts-options.php')) {
require_once get_template_directory() . '/admin/theme-options/related-posts-options.php';
}
// Table of Contents
if (file_exists(get_template_directory() . '/inc/toc.php')) {
require_once get_template_directory() . '/inc/toc.php';
}
// APU Tables - Funciones para tablas de Análisis de Precios Unitarios (Issue #30)
if (file_exists(get_template_directory() . '/inc/apu-tables.php')) {
require_once get_template_directory() . '/inc/apu-tables.php';
}
// Desactivar búsqueda nativa (Issue #3)
if (file_exists(get_template_directory() . '/inc/search-disable.php')) {
require_once get_template_directory() . '/inc/search-disable.php';
}
// Desactivar comentarios (Issue #4)
if (file_exists(get_template_directory() . '/inc/comments-disable.php')) {
require_once get_template_directory() . '/inc/comments-disable.php';
}
// Social share buttons (Issue #31)
if (file_exists(get_template_directory() . '/inc/social-share.php')) {
require_once get_template_directory() . '/inc/social-share.php';
}
// CTA A/B Testing system (Issue #32)
if (file_exists(get_template_directory() . '/inc/cta-ab-testing.php')) {
require_once get_template_directory() . '/inc/cta-ab-testing.php';
}
// CTA Customizer options (Issue #32)
if (file_exists(get_template_directory() . '/inc/customizer-cta.php')) {
require_once get_template_directory() . '/inc/customizer-cta.php';
}
// Admin Panel Module (Phase 1-2: Base Structure)
if (file_exists(get_template_directory() . '/admin/init.php')) {
require_once get_template_directory() . '/admin/init.php';
}
// =============================================================================
// REGISTRO DE COMANDOS WP-CLI
// =============================================================================
if (defined('WP_CLI') && WP_CLI) {
require_once get_template_directory() . '/src/Infrastructure/API/WordPress/MigrationCommand.php';
require_once get_template_directory() . '/Shared/Infrastructure/Api/WordPress/MigrationCommand.php';
}