Jake Vanderwerf
4 days ago 0afb2c0046b55c123eafb4ab9ee77efa68d12463
inc/meta/Sanitizer.php
@@ -14,7 +14,6 @@
   public static function sanitize(mixed $value, array $field_config): mixed
   {
      $callback = static::getCallback($field_config);
      if (is_array($callback)) {
         return call_user_func([static::class, $callback[1]], $value, $field_config);
      }
@@ -31,11 +30,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 +44,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 +172,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)) {
@@ -311,4 +327,22 @@
         }
         return 0.0;
   }
   public static function sanitizePhone(string|int $value, array $config = []):string
   {
      $digits = preg_replace('/\D/', '', (string) $value);
      $length = strlen($digits);
      if ($length < 10 || $length > 13) { // 13 = 3-digit country code + 10
         return '';
      }
      $countryCode = $length > 10 ? substr($digits, 0, $length - 10) : null;
      $number = substr($digits, -10);
      $formatted = preg_replace('/(\d{3})(\d{3})(\d{4})/', '$1-$2-$3', $number);
      return $countryCode ? '+' . $countryCode . '-' . $formatted : $formatted;
   }
}