<?php

namespace ThimPress\ThimBlocks\Helpers;

if ( ! defined( 'ABSPATH' ) ) {
	exit;
}

class SVGHelper {

	public static function render_svg( string $iconSVG ): string {
		$output = '';

		if ( ! empty( $iconSVG ) && preg_match( '/\.svg$/i', $iconSVG ) ) {

			$upload  = wp_upload_dir();
			$baseurl = trailingslashit( $upload['baseurl'] );
			$basedir = wp_normalize_path( $upload['basedir'] );

			if ( strpos( $iconSVG, $baseurl ) === 0 ) {

				$relative = ltrim( str_replace( $baseurl, '', $iconSVG ), '/' );
				$svg_path = wp_normalize_path( $basedir . '/' . $relative );

				$svg_path = realpath( $svg_path );

				if ( $svg_path && strpos( wp_normalize_path( $svg_path ), $basedir ) === 0 ) {

					if ( file_exists( $svg_path ) ) {

						$svg_body = file_get_contents( $svg_path );

						if ( strpos( $svg_body, '<svg' ) !== false ) {

							$output = wp_kses(
								$svg_body,
								self::allowed_tags()
							);
						}
					}
				}
			}
		}

		return $output;
	}

	public static function allowed_tags(): array {
		return array(
			'svg'   => array(
				'xmlns'       => true,
				'viewBox'     => true,
				'viewbox'     => true,
				'width'       => true,
				'height'      => true,
				'fill'        => true,
				'class'       => true,
				'id'          => true,
				'style'       => true,
				'aria-hidden' => true,
				'role'        => true,
				'focusable'   => true,
			),
			'path'  => array(
				'd'               => true,
				'fill'            => true,
				'fill-rule'       => true,
				'clip-rule'       => true,
				'stroke'          => true,
				'stroke-width'    => true,
				'stroke-linecap'  => true,
				'stroke-linejoin' => true,
				'opacity'         => true,
				'class'           => true,
			),
			'g'     => array(
				'fill'      => true,
				'transform' => true,
				'class'     => true,
				'id'        => true,
				'clip-path' => true,
				'opacity'   => true,
			),
			'title' => array(),
			'desc'  => array(),
			'defs'  => array(),
			'circle' => array(
				'cx'           => true,
				'cy'           => true,
				'r'            => true,
				'fill'         => true,
				'stroke'       => true,
				'stroke-width' => true,
				'class'        => true,
			),
			'rect' => array(
				'x'      => true,
				'y'      => true,
				'width'  => true,
				'height' => true,
				'rx'     => true,
				'ry'     => true,
				'fill'   => true,
				'stroke' => true,
				'class'  => true,
			),
			'line' => array(
				'x1'           => true,
				'y1'           => true,
				'x2'           => true,
				'y2'           => true,
				'stroke'       => true,
				'stroke-width' => true,
			),
			'polygon'  => array(
				'points' => true,
				'fill'   => true,
				'stroke' => true,
			),
			'polyline' => array(
				'points' => true,
				'fill'   => true,
				'stroke' => true,
			),
			'clipPath' => array(
				'id' => true,
			),
			'use' => array(
				'href'       => true,
				'xlink:href' => true,
				'x'          => true,
				'y'          => true,
				'width'      => true,
				'height'     => true,
			)
		);
	}

}
