107 lines
3.3 KiB
PHP
107 lines
3.3 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 (acepta URLs completas y relativas que empiecen con /)
|
|
if (!empty($top_bar['link_url'])) {
|
|
$url = $top_bar['link_url'];
|
|
$is_valid_url = filter_var($url, FILTER_VALIDATE_URL) !== false;
|
|
$is_relative_url = preg_match('/^\//', $url);
|
|
|
|
if (!$is_valid_url && !$is_relative_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;
|
|
}
|
|
}
|