<?php

namespace ShopyPress\Utils;
if (!defined('ABSPATH')) exit;


class Sanitizer {

    public static function sanitize_data(mixed $data) {
        if (is_array($data)) {
            // Recursively sanitize each element in the array.
            return array_map([__CLASS__, 'sanitize_data'], $data);
        }

        // Sanitize single value.
        return sanitize_text_field(wp_unslash($data));
    }

    static function sanitizeSvg(string $raw_svg) {
        $allowed_tags = [
            'svg' => [
                'xmlns' => true, 'xmlns:xlink' => true, 'width' => true, 'height' => true,
                'viewBox' => true, 'preserveAspectRatio' => true, 'version' => true, 'baseProfile' => true,
                'fill' => true, 'stroke' => true, 'stroke-width' => true, 'stroke-opacity' => true,
                'class' => true, 'id' => true, 'style' => true, 'transform' => true, 'opacity' => true
            ],
            'g' => ['fill' => true, 'stroke' => true, 'class' => true, 'id' => true, 'style' => true, 'transform' => true],
            'path' => [
                'd' => true, 'fill' => true, 'stroke' => true, 'stroke-width' => true,
                'stroke-opacity' => true, 'stroke-linecap' => true, 'stroke-linejoin' => true,
                'stroke-miterlimit' => true, 'stroke-dasharray' => true, 'stroke-dashoffset' => true,
                'fill-rule' => true, 'clip-rule' => true, 'marker-start' => true, 'marker-mid' => true, 'marker-end' => true
            ],
            'circle' => [
                'cx' => true, 'cy' => true, 'r' => true, 'fill' => true, 'stroke' => true,
                'stroke-width' => true, 'stroke-opacity' => true, 'class' => true, 'id' => true, 'style' => true
            ],
            'ellipse' => [
                'cx' => true, 'cy' => true, 'rx' => true, 'ry' => true, 'fill' => true, 'stroke' => true,
                'stroke-width' => true, 'stroke-opacity' => true, 'class' => true, 'id' => true, 'style' => true
            ],
            'rect' => [
                'x' => true, 'y' => true, 'width' => true, 'height' => true, 'rx' => true, 'ry' => true,
                'fill' => true, 'stroke' => true, 'stroke-width' => true, 'stroke-opacity' => true, 
                'class' => true, 'id' => true, 'style' => true
            ],
            'line' => [
                'x1' => true, 'y1' => true, 'x2' => true, 'y2' => true, 'stroke' => true,
                'stroke-width' => true, 'stroke-opacity' => true, 'stroke-linecap' => true,
                'class' => true, 'id' => true, 'style' => true
            ],
            'polygon' => [
                'points' => true, 'fill' => true, 'stroke' => true, 'stroke-width' => true,
                'stroke-opacity' => true, 'class' => true, 'id' => true, 'style' => true
            ],
            'polyline' => [
                'points' => true, 'fill' => true, 'stroke' => true, 'stroke-width' => true,
                'stroke-opacity' => true, 'class' => true, 'id' => true, 'style' => true
            ],
            'text' => [
                'x' => true, 'y' => true, 'dx' => true, 'dy' => true, 'text-anchor' => true,
                'font-family' => true, 'font-size' => true, 'font-weight' => true,
                'letter-spacing' => true, 'word-spacing' => true, 'fill' => true, 'stroke' => true,
                'class' => true, 'id' => true, 'style' => true
            ],
            'tspan' => [
                'x' => true, 'y' => true, 'dx' => true, 'dy' => true, 'fill' => true, 'stroke' => true,
                'font-family' => true, 'font-size' => true, 'letter-spacing' => true, 'word-spacing' => true,
                'class' => true, 'id' => true, 'style' => true
            ],
            'use' => ['xlink:href' => true, 'width' => true, 'height' => true, 'class' => true, 'id' => true],
            'defs' => [], // Allows <defs> for gradients, filters, etc.
            'clipPath' => ['id' => true],
            'mask' => ['id' => true],
            'filter' => ['id' => true],
            'linearGradient' => ['id' => true, 'gradientUnits' => true, 'x1' => true, 'y1' => true, 'x2' => true, 'y2' => true],
            'radialGradient' => ['id' => true, 'cx' => true, 'cy' => true, 'r' => true, 'fx' => true, 'fy' => true, 'gradientUnits' => true],
            'stop' => ['offset' => true, 'stop-color' => true, 'stop-opacity' => true],
            'title' => [], // Allows <title> for accessibility
        ];
    
        return wp_kses($raw_svg, $allowed_tags);
    }
}