| | |
| | | public static function sanitize(mixed $value, array $field_config): mixed |
| | | { |
| | | $callback = static::getCallback($field_config); |
| | | |
| | | // $caller = debug_backtrace(DEBUG_BACKTRACE_IGNORE_ARGS, 3); |
| | | // error_log('Called from: '.print_r($caller, true)); |
| | | // $name = $field_config['name']??''; |
| | | // |
| | | // error_log('Sanitizing '.$name. ' with value: '.print_r($value, true)); |
| | | |
| | | if (is_array($callback)) { |
| | | return call_user_func([static::class, $callback[1]], $value, $field_config); |
| | | } |
| | | if (method_exists(static::class, $callback)) { |
| | | } elseif (method_exists(static::class, $callback)) { |
| | | return static::$callback($value, $field_config); |
| | | } |
| | | |
| | | return call_user_func($callback, $value); |
| | | } |
| | | |
| | |
| | | return implode(',', array_intersect($value, array_keys($field_config['options']))); |
| | | } |
| | | |
| | | protected static function sanitizeDate(string $value, array $field_config):string |
| | | { |
| | | $timestamp = strtotime($value); |
| | | return $timestamp ? date('Y-m-d', $timestamp) : ''; |
| | | } |
| | | |
| | | protected static function sanitizeDateTime(string $value, array $field_config): string |
| | | protected static function sanitizeDate(?string $value, array $field_config): string |
| | | { |
| | | if (empty($value)) { |
| | | return ''; |
| | | } |
| | | |
| | | $timestamp = strtotime($value); |
| | | if (!$timestamp) { |
| | | try { |
| | | $date = new \DateTime($value, wp_timezone()); |
| | | } catch (\Exception $e) { |
| | | return ''; |
| | | } |
| | | return $date->format('Y-m-d'); |
| | | } |
| | | |
| | | // Return in MySQL datetime format |
| | | return date('Y-m-d H:i:s', $timestamp); |
| | | protected static function sanitizeDateTime(?string $value, array $field_config): string |
| | | { |
| | | if (empty($value)) { |
| | | return ''; |
| | | } |
| | | try { |
| | | $date = new \DateTime($value, wp_timezone()); |
| | | } catch (\Exception $e) { |
| | | return ''; |
| | | } |
| | | return $date->format('Y-m-d H:i:s'); |
| | | } |
| | | |
| | | protected static function sanitizeTime(string $value, array $field_config):string |