<?php
/**
 * SVG Upload Support
 * 
 * @package Watson
 */

class WS_SVG_Support {
    /**
     * Instance of the class
     */
    private static $instance = null;

    /**
     * Get instance of the class
     */
    public static function get_instance() {
        if (null === self::$instance) {
            self::$instance = new self();
        }
        return self::$instance;
    }

    /**
     * Constructor
     */
    private function __construct() {
        // Add SVG support
        add_filter('upload_mimes', [$this, 'add_svg_mime_type']);
        add_filter('wp_check_filetype_and_ext', [$this, 'check_svg_filetype'], 10, 4);
        add_filter('wp_prepare_attachment_for_js', [$this, 'prepare_svg_for_media_library']);
    }

    /**
     * Add SVG mime type
     */
    public function add_svg_mime_type($mimes) {
        $mimes['svg'] = 'image/svg+xml';
        $mimes['svgz'] = 'image/svg+xml';
        return $mimes;
    }

    /**
     * Check SVG filetype
     */
    public function check_svg_filetype($data, $file, $filename, $mimes) {
        if (!$data['type']) {
            $wp_filetype = wp_check_filetype($filename, $mimes);
            $ext = $wp_filetype['ext'];
            $type = $wp_filetype['type'];
            $proper_filename = $filename;

            if ($ext === 'svg') {
                // Validate SVG content
                $file_content = file_get_contents($file);
                if ($this->validate_svg($file_content)) {
                    $data['ext'] = $ext;
                    $data['type'] = $type;
                    $data['proper_filename'] = $proper_filename;
                }
            }
        }
        return $data;
    }

    /**
     * Validate SVG content
     */
    private function validate_svg($content) {
        // Basic security: only allow SVG tags and safe attributes
        $allowed_tags = [
            'svg' => [
                'xmlns' => true,
                'viewbox' => true,
                'width' => true,
                'height' => true,
                'fill' => true,
                'class' => true,
                'style' => true,
                'preserveaspectratio' => true,
            ],
            'path' => [
                'd' => true,
                'fill' => true,
                'style' => true,
            ],
            'circle' => [
                'cx' => true,
                'cy' => true,
                'r' => true,
                'fill' => true,
                'style' => true,
            ],
            'rect' => [
                'x' => true,
                'y' => true,
                'width' => true,
                'height' => true,
                'fill' => true,
                'style' => true,
            ],
            'g' => [
                'fill' => true,
                'style' => true,
                'transform' => true,
            ],
            'title' => true,
            'desc' => true,
        ];

        // Remove any potentially malicious content
        $content = wp_kses($content, $allowed_tags);

        // Check if content contains svg tag
        return strpos($content, '<svg') !== false;
    }

    /**
     * Prepare SVG for media library
     */
    public function prepare_svg_for_media_library($response) {
        if ($response['mime'] === 'image/svg+xml') {
            // Set dimensions for preview
            if (empty($response['sizes'])) {
                $response['sizes'] = [
                    'full' => [
                        'url' => $response['url'],
                        'width' => 150,
                        'height' => 150,
                    ],
                ];
            }

            // Add icon preview
            $response['image'] = [
                'src' => $response['url'],
            ];
        }
        return $response;
    }
}
