COMPLETADO: Fase 1 de la migración a Clean Architecture + POO ## Estructura de Carpetas - ✓ Estructura completa de 4 capas (Domain, Application, Infrastructure, Presentation) - ✓ Carpetas de Use Cases (SaveComponent, GetComponent, DeleteComponent, SyncSchema) - ✓ Estructura de tests (Unit, Integration, E2E) - ✓ Carpetas de schemas y templates ## Composer y Autoloading - ✓ PSR-4 autoloading configurado para ROITheme namespace - ✓ Autoloader optimizado regenerado ## DI Container - ✓ DIContainer implementado con patrón Singleton - ✓ Métodos set(), get(), has() para gestión de servicios - ✓ Getters específicos para ComponentRepository, ValidationService, CacheService - ✓ Placeholders que serán implementados en Fase 5 - ✓ Prevención de clonación y deserialización ## Interfaces - ✓ ComponentRepositoryInterface (Domain) - ✓ ValidationServiceInterface (Application) - ✓ CacheServiceInterface (Application) - ✓ Component entity placeholder (Domain) ## Bootstrap - ✓ functions.php actualizado con carga de Composer autoloader - ✓ Inicialización del DIContainer - ✓ Helper function roi_container() disponible globalmente ## Tests - ✓ 10 tests unitarios para DIContainer (100% cobertura) - ✓ Total: 13 tests unitarios, 28 assertions - ✓ Suite de tests pasando correctamente ## Validación - ✓ Script de validación automatizado (48/48 checks pasados) - ✓ 100% de validaciones exitosas La arquitectura base está lista para la Fase 2. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
272 lines
7.5 KiB
PHP
272 lines
7.5 KiB
PHP
<?php
|
|
/**
|
|
* Sanitizer Helper
|
|
*
|
|
* Métodos estáticos reutilizables para sanitización de datos
|
|
*
|
|
* @package ROI_Theme
|
|
* @subpackage Admin_Panel\Sanitizers
|
|
* @since 2.1.0
|
|
*/
|
|
|
|
if (!defined('ABSPATH')) {
|
|
exit;
|
|
}
|
|
|
|
/**
|
|
* Class ROI_Sanitizer_Helper
|
|
*
|
|
* Proporciona métodos estáticos para sanitización común,
|
|
* eliminando código duplicado en los sanitizadores de componentes
|
|
*/
|
|
class ROI_Sanitizer_Helper {
|
|
|
|
/**
|
|
* Sanitiza un valor booleano
|
|
*
|
|
* @param array $data Array de datos
|
|
* @param string $key Clave del dato
|
|
* @return bool Valor booleano sanitizado
|
|
*/
|
|
public static function sanitize_boolean($data, $key) {
|
|
return !empty($data[$key]);
|
|
}
|
|
|
|
/**
|
|
* Sanitiza múltiples valores booleanos
|
|
*
|
|
* @param array $data Array de datos
|
|
* @param array $keys Array de claves a sanitizar
|
|
* @return array Array asociativo con valores booleanos sanitizados
|
|
*/
|
|
public static function sanitize_booleans($data, $keys) {
|
|
$result = array();
|
|
foreach ($keys as $key) {
|
|
$result[$key] = self::sanitize_boolean($data, $key);
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Sanitiza un campo de texto con valor por defecto
|
|
*
|
|
* @param array $data Array de datos
|
|
* @param string $key Clave del dato
|
|
* @param string $default Valor por defecto (default: '')
|
|
* @return string Texto sanitizado
|
|
*/
|
|
public static function sanitize_text($data, $key, $default = '') {
|
|
return sanitize_text_field($data[$key] ?? $default);
|
|
}
|
|
|
|
/**
|
|
* Sanitiza múltiples campos de texto
|
|
*
|
|
* @param array $data Array de datos
|
|
* @param array $keys Array de claves a sanitizar
|
|
* @param string $default Valor por defecto para todos (default: '')
|
|
* @return array Array asociativo con textos sanitizados
|
|
*/
|
|
public static function sanitize_texts($data, $keys, $default = '') {
|
|
$result = array();
|
|
foreach ($keys as $key) {
|
|
$result[$key] = self::sanitize_text($data, $key, $default);
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Sanitiza un color hexadecimal con valor por defecto
|
|
*
|
|
* @param array $data Array de datos
|
|
* @param string $key Clave del dato
|
|
* @param string $default Valor por defecto (default: '')
|
|
* @return string Color hexadecimal sanitizado
|
|
*/
|
|
public static function sanitize_color($data, $key, $default = '') {
|
|
return sanitize_hex_color($data[$key] ?? $default);
|
|
}
|
|
|
|
/**
|
|
* Sanitiza múltiples colores hexadecimales
|
|
*
|
|
* @param array $data Array de datos
|
|
* @param array $keys Array de claves a sanitizar
|
|
* @param string $default Valor por defecto para todos (default: '')
|
|
* @return array Array asociativo con colores sanitizados
|
|
*/
|
|
public static function sanitize_colors($data, $keys, $default = '') {
|
|
$result = array();
|
|
foreach ($keys as $key) {
|
|
$result[$key] = self::sanitize_color($data, $key, $default);
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Sanitiza un valor con validación enum (in_array)
|
|
*
|
|
* @param array $data Array de datos
|
|
* @param string $key Clave del dato
|
|
* @param array $allowed_values Valores permitidos
|
|
* @param mixed $default Valor por defecto
|
|
* @return mixed Valor sanitizado
|
|
*/
|
|
public static function sanitize_enum($data, $key, $allowed_values, $default) {
|
|
return in_array($data[$key] ?? '', $allowed_values, true)
|
|
? $data[$key]
|
|
: $default;
|
|
}
|
|
|
|
/**
|
|
* Sanitiza múltiples valores enum
|
|
*
|
|
* @param array $data Array de datos
|
|
* @param array $config Array de configuración [key => ['allowed' => [...], 'default' => ...]]
|
|
* @return array Array asociativo con valores enum sanitizados
|
|
*/
|
|
public static function sanitize_enums($data, $config) {
|
|
$result = array();
|
|
foreach ($config as $key => $settings) {
|
|
$result[$key] = self::sanitize_enum(
|
|
$data,
|
|
$key,
|
|
$settings['allowed'],
|
|
$settings['default']
|
|
);
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Sanitiza un valor entero con valor por defecto
|
|
*
|
|
* @param array $data Array de datos
|
|
* @param string $key Clave del dato
|
|
* @param int $default Valor por defecto
|
|
* @return int Entero sanitizado
|
|
*/
|
|
public static function sanitize_int($data, $key, $default = 0) {
|
|
return isset($data[$key]) ? intval($data[$key]) : $default;
|
|
}
|
|
|
|
/**
|
|
* Sanitiza múltiples valores enteros
|
|
*
|
|
* @param array $data Array de datos
|
|
* @param array $config Array de configuración [key => default_value]
|
|
* @return array Array asociativo con enteros sanitizados
|
|
*/
|
|
public static function sanitize_ints($data, $config) {
|
|
$result = array();
|
|
foreach ($config as $key => $default) {
|
|
$result[$key] = self::sanitize_int($data, $key, $default);
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Sanitiza un valor float con valor por defecto
|
|
*
|
|
* @param array $data Array de datos
|
|
* @param string $key Clave del dato
|
|
* @param float $default Valor por defecto
|
|
* @return float Float sanitizado
|
|
*/
|
|
public static function sanitize_float($data, $key, $default = 0.0) {
|
|
return isset($data[$key]) ? floatval($data[$key]) : $default;
|
|
}
|
|
|
|
/**
|
|
* Sanitiza múltiples valores float
|
|
*
|
|
* @param array $data Array de datos
|
|
* @param array $config Array de configuración [key => default_value]
|
|
* @return array Array asociativo con floats sanitizados
|
|
*/
|
|
public static function sanitize_floats($data, $config) {
|
|
$result = array();
|
|
foreach ($config as $key => $default) {
|
|
$result[$key] = self::sanitize_float($data, $key, $default);
|
|
}
|
|
return $result;
|
|
}
|
|
|
|
/**
|
|
* Sanitiza una URL con valor por defecto
|
|
*
|
|
* @param array $data Array de datos
|
|
* @param string $key Clave del dato
|
|
* @param string $default Valor por defecto (default: '')
|
|
* @return string URL sanitizada
|
|
*/
|
|
public static function sanitize_url($data, $key, $default = '') {
|
|
return esc_url_raw($data[$key] ?? $default);
|
|
}
|
|
|
|
/**
|
|
* Sanitiza un array de strings
|
|
*
|
|
* @param array $data Array de datos
|
|
* @param string $key Clave del dato
|
|
* @param array $default Array por defecto
|
|
* @return array Array de strings sanitizados
|
|
*/
|
|
public static function sanitize_array_of_strings($data, $key, $default = array()) {
|
|
return isset($data[$key]) && is_array($data[$key])
|
|
? array_map('sanitize_text_field', $data[$key])
|
|
: $default;
|
|
}
|
|
|
|
/**
|
|
* Sanitiza un grupo de campos anidados (custom_styles, dropdown, etc.)
|
|
*
|
|
* @param array $data Array de datos completo
|
|
* @param string $group_key Clave del grupo (ej: 'custom_styles')
|
|
* @param array $sanitization_rules Reglas de sanitización por campo
|
|
* Formato: [
|
|
* 'campo' => ['type' => 'text|color|int|float|enum|bool', 'default' => valor, 'allowed' => array()]
|
|
* ]
|
|
* @return array Array con campos del grupo sanitizados
|
|
*/
|
|
public static function sanitize_nested_group($data, $group_key, $sanitization_rules) {
|
|
$result = array();
|
|
$group_data = $data[$group_key] ?? array();
|
|
|
|
foreach ($sanitization_rules as $field => $rule) {
|
|
$type = $rule['type'];
|
|
$default = $rule['default'] ?? null;
|
|
|
|
switch ($type) {
|
|
case 'text':
|
|
$result[$field] = self::sanitize_text($group_data, $field, $default ?? '');
|
|
break;
|
|
case 'color':
|
|
$result[$field] = self::sanitize_color($group_data, $field, $default ?? '');
|
|
break;
|
|
case 'int':
|
|
$result[$field] = self::sanitize_int($group_data, $field, $default ?? 0);
|
|
break;
|
|
case 'float':
|
|
$result[$field] = self::sanitize_float($group_data, $field, $default ?? 0.0);
|
|
break;
|
|
case 'enum':
|
|
$result[$field] = self::sanitize_enum(
|
|
$group_data,
|
|
$field,
|
|
$rule['allowed'] ?? array(),
|
|
$default
|
|
);
|
|
break;
|
|
case 'bool':
|
|
$result[$field] = self::sanitize_boolean($group_data, $field);
|
|
break;
|
|
default:
|
|
$result[$field] = $group_data[$field] ?? $default;
|
|
}
|
|
}
|
|
|
|
return $result;
|
|
}
|
|
}
|