| File was renamed from inc/meta/MetaSanitizer.php |
| | |
| | | <?php |
| | | namespace JVBase\meta; |
| | | |
| | | use JVBase\meta\MetaTypeManager; |
| | | |
| | | if (!defined('ABSPATH')) { |
| | | exit; // Exit if accessed directly |
| | | } |
| | |
| | | /** |
| | | * Handles meta value sanitization |
| | | */ |
| | | class MetaSanitizer |
| | | class Sanitizer |
| | | { |
| | | protected MetaTypeManager $type_manager; |
| | | |
| | | public function __construct() |
| | | { |
| | | $this->type_manager = new MetaTypeManager(); |
| | | } |
| | | public static function sanitize(mixed $value, array $field_config): mixed |
| | | { |
| | | $callback = static::getCallback($field_config); |
| | | |
| | | public function sanitize(mixed $value, array $field_config):mixed |
| | | { |
| | | $callback = $this->getCallback($field_config); |
| | | if (is_array($callback)) { |
| | | return call_user_func([$this, $callback[1]], $value, $field_config); |
| | | } |
| | | if (method_exists($this, $callback)) { |
| | | return $this->$callback($value, $field_config); |
| | | } else { |
| | | return call_user_func($callback, $value); |
| | | } |
| | | } |
| | | if (is_array($callback)) { |
| | | return call_user_func([static::class, $callback[1]], $value, $field_config); |
| | | } |
| | | if (method_exists(static::class, $callback)) { |
| | | return static::$callback($value, $field_config); |
| | | } |
| | | |
| | | public function getCallback(array $field_config):mixed |
| | | return call_user_func($callback, $value); |
| | | } |
| | | |
| | | public static function getCallback(array $field_config):mixed |
| | | { |
| | | return $field_config['sanitize'] ?? |
| | | $this->type_manager->getSanitizeCallback($field_config['type']); |
| | | MetaTypeManager::getSanitizeCallback($field_config['type']); |
| | | } |
| | | |
| | | protected function sanitizeTaxonomy(array|string $values, array $field_config):string |
| | | protected static function sanitizeTaxonomy(array|string $values, array $field_config):string |
| | | { |
| | | if (!is_array($values)) { |
| | | $values = explode(',', $values); |
| | |
| | | return implode(',', $values); |
| | | } |
| | | |
| | | protected function sanitizeUser(array|string $values, array $field_config):string |
| | | protected static function sanitizeUser(array|string $values, array $field_config):string |
| | | { |
| | | if (!is_array($values)) { |
| | | $values = explode(',', $values); |
| | |
| | | return implode(',', $values); |
| | | } |
| | | |
| | | protected function sanitizeTagList(array $values, array $field_config): array |
| | | protected static function sanitizeTagList(array $values, array $field_config): array |
| | | { |
| | | if (!is_array($values)) { |
| | | return []; |
| | | } |
| | | |
| | | if (empty(array_filter($values, fn($value) => !empty($value)))) { |
| | | return []; |
| | | } |
| | |
| | | } |
| | | |
| | | $subfield_config['name'] = $key; // For backwards compatibility |
| | | $clean_row[$key] = $this->sanitize($row[$key], $subfield_config); |
| | | $clean_row[$key] = static::sanitize($row[$key], $subfield_config); |
| | | } |
| | | |
| | | // Only add row if it has at least one non-empty value |
| | |
| | | return $sanitized; |
| | | } |
| | | |
| | | protected function sanitizeRepeater(array $values, array $field_config):array |
| | | protected static function sanitizeRepeater(array $values, array $field_config):array |
| | | { |
| | | if (!is_array($values)) { |
| | | return []; |
| | | } |
| | | if (empty(array_filter($values, fn($value) => !empty($value)))) { |
| | | return []; |
| | | } |
| | |
| | | continue; |
| | | } |
| | | $subfield_config['name'] = $key;//For backwards compatability |
| | | $clean_row[$key] = $this->sanitize($row[$key], $subfield_config); |
| | | $clean_row[$key] = static::sanitize($row[$key], $subfield_config); |
| | | } |
| | | $sanitized[] = $clean_row; |
| | | } |
| | |
| | | return $sanitized; |
| | | } |
| | | |
| | | protected function sanitizeGroup(array|string $values, array $field_config):array |
| | | protected static function sanitizeGroup(array|string $values, array $field_config):array |
| | | { |
| | | if (!is_array($values)) { |
| | | return []; |
| | |
| | | foreach ($field_config['fields'] as $key => $subfield_config) { |
| | | if (!array_key_exists($key, $clean_values)) { |
| | | // Use default value if not provided |
| | | $default = $this->type_manager->getType($subfield_config['type'])['default'] ?? ''; |
| | | $default = MetaTypeManager::getType($subfield_config['type'])['default'] ?? ''; |
| | | $sanitized[$key] = $default; |
| | | continue; |
| | | } |
| | | |
| | | $subfield_config['name'] = $key; // For backwards compatibility |
| | | $sanitized[$key] = $this->sanitize($clean_values[$key], $subfield_config); |
| | | $sanitized[$key] = static::sanitize($clean_values[$key], $subfield_config); |
| | | } |
| | | |
| | | return $sanitized; |
| | | } |
| | | |
| | | protected function sanitizeUpload(array|string $value):string |
| | | protected static function sanitizeUpload(array|string $value):string |
| | | { |
| | | if (empty($value)) { |
| | | return ''; |
| | |
| | | return implode(',', $valid_ids); |
| | | } |
| | | |
| | | protected function sanitizeLocation(array $value, array $field_config):array |
| | | protected static function sanitizeLocation(array $value, array $field_config):array |
| | | { |
| | | error_log('Location field to sanitize: '.print_r($value, true)); |
| | | return [ |
| | |
| | | ]; |
| | | } |
| | | |
| | | protected function sanitizeOptions(array|string $value, array $field_config):string |
| | | protected static function sanitizeOptions(array|string $value, array $field_config):string |
| | | { |
| | | error_log('Sanitizing options: '.print_r($value, true)); |
| | | if (!isset($field_config['options'])) { |
| | |
| | | return implode(',', array_intersect($value, array_keys($field_config['options']))); |
| | | } |
| | | |
| | | protected function sanitizeDate(string $value, array $field_config):string |
| | | protected static function sanitizeDate(string $value, array $field_config):string |
| | | { |
| | | $timestamp = strtotime($value); |
| | | return $timestamp ? date('Y-m-d', $timestamp) : ''; |
| | | } |
| | | |
| | | protected function sanitizeDateTime(string $value, array $field_config): string |
| | | protected static function sanitizeDateTime(string $value, array $field_config): string |
| | | { |
| | | if (empty($value)) { |
| | | return ''; |
| | |
| | | return date('Y-m-d H:i:s', $timestamp); |
| | | } |
| | | |
| | | protected function sanitizeTime(string $value, array $field_config):string |
| | | protected static function sanitizeTime(string $value, array $field_config):string |
| | | { |
| | | // Remove any whitespace |
| | | $value = trim($value); |
| | |
| | | return ''; |
| | | } |
| | | |
| | | public function sanitizeFloat(string $value, array $config):float |
| | | public static function sanitizeFloat(string $value, array $config):float |
| | | { |
| | | if (is_numeric($value)) { |
| | | return (float) $value; |