Files
roi-theme/functions.php
FrankZamora 42edfab50d Fase-00: Configuración inicial del proyecto
- Configuración de Composer con PSR-4 en _testing-suite/
- Configuración de PHPUnit en _testing-suite/phpunit.xml
- Configuración de PHPCS en _testing-suite/phpcs.xml
- WordPress Test Suite integrado en bootstrap-integration.php
- Scripts de backup automatizado (backup-database.php, backup-files.bat)
- Procedimientos de rollback documentados (RESTORE-PROCEDURE.md)
- Estrategia de Git branching documentada (GIT-BRANCHING-STRATEGY.md)
- .gitignore actualizado para excluir _testing-suite/ y _planeacion/
- Limpieza de estructura anterior de Clean Architecture
- Documentación completa en _planeacion/roi-theme/_MIGRACION-CLEAN-ARCHITECTURE/Fase-00/

Archivos de configuración movidos a _testing-suite/:
- composer.json (PSR-4 autoloading, dev dependencies)
- phpunit.xml (3 suites: Unit, Integration, E2E)
- phpcs.xml (WordPress Coding Standards)
- bootstrap-unit.php y bootstrap-integration.php

Sistema de backup implementado con 4 mejoras críticas:
- Password protegido con --defaults-file
- Verificación de espacio en disco
- Lock files para prevenir ejecuciones concurrentes
- Detección automática de rutas

Procedimientos de rollback documentados:
- Rollback de base de datos (5-10 min)
- Rollback de archivos (10-15 min)
- Rollback de Git (5 min)
- Rollback completo (20-30 min)

Preparación del entorno completa. Listo para comenzar Fase-1.
2025-11-18 23:26:28 -06:00

376 lines
12 KiB
PHP

<?php
/**
* ROI Theme Functions and Definitions
*
* @package ROI_Theme
* @since 1.0.0
*/
// Exit if accessed directly
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.
*/
// Load Composer autoloader
if (file_exists(__DIR__ . '/vendor/autoload.php')) {
require_once __DIR__ . '/vendor/autoload.php';
}
// Initialize DI Container
use ROITheme\Infrastructure\DI\DIContainer;
/**
* Get DI Container instance
*
* Helper function to access the DI Container throughout the theme.
*
* @return DIContainer
*/
function roi_container(): DIContainer {
return DIContainer::getInstance();
}
/**
* ========================================================================
* END BOOTSTRAP
* ========================================================================
*/
/**
* ========================================================================
* THEME DATABASE TABLES SETUP
* ========================================================================
*
* Crea las tablas del tema cuando se activa.
* Esto asegura que el tema sea portable y funcione en cualquier instalación WordPress.
*/
/**
* 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;
require_once(ABSPATH . 'wp-admin/includes/upgrade.php');
$charset_collate = $wpdb->get_charset_collate();
// Tabla de components
$table_components = $wpdb->prefix . 'roi_theme_components';
$sql_components = "CREATE TABLE {$table_components} (
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,
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)
) {$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);
// Log en modo debug
if (defined('WP_DEBUG') && WP_DEBUG) {
error_log('ROI Theme: Database tables created/updated');
}
});
/**
* 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';
}