Files
roi-theme/admin-panel/includes/class-validator.php
FrankZamora 465b879135 Feat: Implementar Top Bar configurable - Issue #143
Implementación completa del componente Top Bar con 15 campos configurables desde el admin panel, siguiendo el algoritmo universal v2.0.

## Cambios Realizados

### Backend (PHP)
- Agregados defaults del Top Bar a class-settings-manager.php
- Implementada validación completa en class-validator.php (15 campos)
- Implementada sanitización con sanitize_text_field, esc_url_raw y sanitize_hex_color
- Modificado header.php con código 100% configurable usando wp_parse_args()

### Frontend (Admin Panel)
- Creado tab HTML completo con 3 secciones: Activación, Contenido y Estilos
- Implementado JavaScript para renderizado y recolección de datos
- 15 campos configurables: enabled, visibility, icon, content, link, custom styles

### Infraestructura
- Creado admin-panel/init.php para carga del módulo
- Creada class-admin-menu.php con enqueue de Bootstrap 5 y assets
- Creada estructura base CSS y JavaScript del admin
- Ya cargado en functions.php línea 276

## Características
- Responsive: Control independiente mobile/desktop
- Estilos personalizables: 4 colores + tamaño de fuente
- Validación robusta: Límites de caracteres, URLs, colores hex
- Defaults inteligentes: Valores seguros si no hay configuración

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

Co-Authored-By: Claude <noreply@anthropic.com>
2025-11-09 15:13:55 -06:00

101 lines
3.1 KiB
PHP

<?php
/**
* Validator Class
*
* Validación de datos por componentes
*
* @package Apus_Theme
* @since 2.0.0
*/
if (!defined('ABSPATH')) {
exit;
}
class APUS_Validator {
/**
* Validar todas las configuraciones
*/
public function validate($data) {
$errors = array();
// Validar estructura base
if (!isset($data['components']) || !is_array($data['components'])) {
$errors[] = 'Estructura de datos inválida';
return array('valid' => false, 'errors' => $errors);
}
// Validar Top Bar
if (isset($data['components']['top_bar'])) {
$top_bar_errors = $this->validate_top_bar($data['components']['top_bar']);
$errors = array_merge($errors, $top_bar_errors);
}
return array(
'valid' => empty($errors),
'errors' => $errors
);
}
/**
* Validar Top Bar
*/
public function validate_top_bar($top_bar) {
$errors = array();
// Validar icon_class
if (!empty($top_bar['icon_class']) && strlen($top_bar['icon_class']) > 50) {
$errors[] = 'La clase del icono no puede exceder 50 caracteres';
}
// Validar highlight_text
if (!empty($top_bar['highlight_text']) && strlen($top_bar['highlight_text']) > 30) {
$errors[] = 'El texto destacado no puede exceder 30 caracteres';
}
// Validar message_text
if (empty($top_bar['message_text'])) {
$errors[] = 'El mensaje principal es obligatorio';
} elseif (strlen($top_bar['message_text']) > 250) {
$errors[] = 'El mensaje principal no puede exceder 250 caracteres';
}
// Validar link_text
if (!empty($top_bar['link_text']) && strlen($top_bar['link_text']) > 50) {
$errors[] = 'El texto del enlace no puede exceder 50 caracteres';
}
// Validar link_url
if (!empty($top_bar['link_url']) && !filter_var($top_bar['link_url'], FILTER_VALIDATE_URL)) {
$errors[] = 'La URL del enlace no es válida';
}
// Validar link_target
if (!in_array($top_bar['link_target'] ?? '', array('_self', '_blank'))) {
$errors[] = 'El target del enlace debe ser _self o _blank';
}
// Validar colores
if (!empty($top_bar['custom_styles']['background_color']) && !preg_match('/^#[a-f0-9]{6}$/i', $top_bar['custom_styles']['background_color'])) {
$errors[] = 'El color de fondo debe ser un color hexadecimal válido';
}
if (!empty($top_bar['custom_styles']['text_color']) && !preg_match('/^#[a-f0-9]{6}$/i', $top_bar['custom_styles']['text_color'])) {
$errors[] = 'El color de texto debe ser un color hexadecimal válido';
}
if (!empty($top_bar['custom_styles']['highlight_color']) && !preg_match('/^#[a-f0-9]{6}$/i', $top_bar['custom_styles']['highlight_color'])) {
$errors[] = 'El color del highlight debe ser un color hexadecimal válido';
}
if (!empty($top_bar['custom_styles']['link_hover_color']) && !preg_match('/^#[a-f0-9]{6}$/i', $top_bar['custom_styles']['link_hover_color'])) {
$errors[] = 'El color hover del enlace debe ser un color hexadecimal válido';
}
// Validar font_size
if (!in_array($top_bar['custom_styles']['font_size'] ?? '', array('small', 'normal', 'large'))) {
$errors[] = 'El tamaño de fuente debe ser small, normal o large';
}
return $errors;
}
}