Jake Vanderwerf
2026-09-13 1aa7c404dae63c9518468fe9b579fad819f8d871
inc/meta/Sanitizer.php
@@ -15,13 +15,17 @@
   {
      $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);
   }
@@ -31,11 +35,9 @@
           MetaTypeManager::getSanitizeCallback($field_config['type']);
    }
    protected static function sanitizeTaxonomy(array|string $values, array $field_config):string
    protected static function sanitizeTaxonomy(string $values, array $field_config):string
    {
        if (!is_array($values)) {
            $values = explode(',', $values);
        }
      $values = array_map('absint', explode(',', $values));
        // Ensure taxonomy starts with BASE
        $taxonomy = (str_starts_with($field_config['taxonomy'], BASE))
@@ -47,17 +49,21 @@
        return implode(',', $values);
    }
    protected static function sanitizeUser(array|string $values, array $field_config):string
    protected static function sanitizeUser(string $values, array $field_config):string
    {
        if (!is_array($values)) {
            $values = explode(',', $values);
        }
      $values = array_map('absint', explode(',', $values));
        $values = array_filter($values, fn($value) => (bool)get_userdata((int)$value));
        return implode(',', $values);
    }
   protected static function sanitizePost(string $values, array $config):string
   {
      $values = array_map('absint', explode(',', $values));
      return implode(',', array_filter($values, fn($value) => (bool)get_post((int)$value)));
   }
   protected static function sanitizeTagList(array $values, array $field_config): array
   {
      if (empty(array_filter($values, fn($value) => !empty($value)))) {
@@ -171,6 +177,21 @@
      return $sanitized;
   }
   protected static function sanitizeSelector(string|array $value, array $config):string
   {
      if (is_array($value)) {
         $value = implode(',', $value);
      }
      if (array_key_exists('subtype', $config)) {
         return match ($config['subtype']) {
            'user' => self::sanitizeUser($value, $config),
            'taxonomy'=> self::sanitizeTaxonomy($value, $config),
            'post'   => self::sanitizePost($value, $config),
         };
      }
      return implode(',',array_map('absint', explode(',',$value)));
   }
    protected static function sanitizeUpload(array|string $value):string
    {
        if (empty($value)) {
@@ -189,9 +210,12 @@
        return implode(',', $valid_ids);
    }
    protected static 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));
       if (empty($value['address']??'')) {
          return MetaTypeManager::getType('location')['default'];
       }
        return [
            'address' => sanitize_text_field($value['address'] ?? ''),
            'lat' => (float)($value['lat'] ?? 0),
@@ -217,25 +241,30 @@
        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