<?php
defined( 'ABSPATH' ) || exit;

/**
 * Enqueue animated SVG icon CSS.
 */
function mwpl_enqueue_animated_icons_css() {
	wp_enqueue_style(
		'mwpl-animated-icons',
		plugin_dir_url( __FILE__ ) . '../assets/animated-icons.css',
		array(),
		'1.0.0'
	);
}
add_action( 'wp_enqueue_scripts', 'mwpl_enqueue_animated_icons_css' );

/**
 * Output the SVG sprite once in the footer.
 * The sprite is shipped with the plugin, but we still sanitize it before output.
 */
function mwpl_output_animated_icons_sprite() {
	$file = plugin_dir_path( __FILE__ ) . '../assets/svg/animated-icons.svg';
	if ( ! file_exists( $file ) ) {
		return;
	}

	$mwpl_svg_raw = file_get_contents( $file ); // phpcs:ignore WordPress.WP.AlternativeFunctions.file_get_contents_file_get_contents
	if ( false === $mwpl_svg_raw || '' === $mwpl_svg_raw ) {
		return;
	}

	$allowed = array(
		'svg' => array(
			'xmlns' => true,
			'viewBox' => true,
			'aria-hidden' => true,
			'style' => true,
			'width' => true,
			'height' => true,
			'class' => true,
			'fill' => true,
		),
		'symbol' => array(
			'id' => true,
			'viewBox' => true,
		),
		'defs' => array(),
		'g' => array(
			'id' => true,
			'class' => true,
			'fill' => true,
			'stroke' => true,
			'stroke-width' => true,
			'transform' => true,
			'opacity' => true,
		),
		'path' => array(
			'd' => true,
			'class' => true,
			'fill' => true,
			'stroke' => true,
			'stroke-width' => true,
			'stroke-linecap' => true,
			'stroke-linejoin' => true,
			'transform' => true,
			'opacity' => true,
		),
		'circle' => array(
			'cx' => true,
			'cy' => true,
			'r' => true,
			'class' => true,
			'fill' => true,
			'stroke' => true,
			'stroke-width' => true,
			'opacity' => true,
			'transform' => true,
		),
		'rect' => array(
			'x' => true,
			'y' => true,
			'width' => true,
			'height' => true,
			'rx' => true,
			'ry' => true,
			'class' => true,
			'fill' => true,
			'stroke' => true,
			'stroke-width' => true,
			'opacity' => true,
			'transform' => true,
		),
		'line' => array(
			'x1' => true,
			'y1' => true,
			'x2' => true,
			'y2' => true,
			'class' => true,
			'stroke' => true,
			'stroke-width' => true,
			'opacity' => true,
			'transform' => true,
		),
		'polyline' => array(
			'points' => true,
			'class' => true,
			'fill' => true,
			'stroke' => true,
			'stroke-width' => true,
			'opacity' => true,
			'transform' => true,
		),
		'polygon' => array(
			'points' => true,
			'class' => true,
			'fill' => true,
			'stroke' => true,
			'stroke-width' => true,
			'opacity' => true,
			'transform' => true,
		),
		'animate' => array(
			'attributeName' => true,
			'values' => true,
			'dur' => true,
			'repeatCount' => true,
			'begin' => true,
			'keyTimes' => true,
			'keySplines' => true,
			'calcMode' => true,
			'from' => true,
			'to' => true,
		),
		'animateTransform' => array(
			'attributeName' => true,
			'attributeType' => true,
			't' => true, /* some exports use a short attribute name */
			'type' => true,
			'from' => true,
			'to' => true,
			'dur' => true,
			'repeatCount' => true,
			'begin' => true,
		),
		'title' => array(),
		'desc' => array(),
	);

	$mwpl_svg_safe = wp_kses( $mwpl_svg_raw, $allowed );

	echo '<div aria-hidden="true" style="position:absolute;width:0;height:0;overflow:hidden">'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
	echo $mwpl_svg_safe; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
	echo '</div>'; // phpcs:ignore WordPress.Security.EscapeOutput.OutputNotEscaped
}
add_action( 'wp_footer', 'mwpl_output_animated_icons_sprite', 5 );
