<?php
/**
 * SVG - Template
 * 
 * @package OrokuBlocks
 * @author OrokuBlocks (https://orokublocks.com/)
 * 
 * @since 1.0.0
 * 
 * @param array $args {
 *   @type string $icon                             Name of the SVG icon.
 *   @type string $additional_wrapper_classes       Classes to add to the wrapper.
 *   @type array  $linear_gradient {
 *     @type string $id           Id of the linear gradient.
 *     @type array  $stops {
 *       @type int    $offset        Stop offset. Between 0 and 100.
 *       @type string $color         Stop color.
 *     }
 *   }             
 * }
 */

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
  exit;
}

$defaults = [
  'additional_wrapper_classes'  => false,
  'linear_gradient'             => false
];

$options = array_merge( $defaults, $args );

$wrapper_classes = [
  'orokublocks-icon',
  'orokublocks-icon-'. $options['icon']
];

if ( $options['additional_wrapper_classes'] ) {
  $wrapper_classes[] = $options['additional_wrapper_classes'];
}

$svg_style_attributes = [];

if ( $options['linear_gradient'] ) {
  $svg_style_attributes['fill'] = 'url(#' . $options['linear_gradient']['id'] . ')';
}

$svg_style_attributes_string = '';

foreach ( $svg_style_attributes as $property => $value ) {
  $svg_style_attributes_string .= $property . ': ' . $value . ';';
}

?>
<svg class="<?php echo esc_attr( implode( ' ', $wrapper_classes ) ); ?>" style="<?php echo esc_attr( $svg_style_attributes_string ); ?>">
<?php if ( $options['linear_gradient'] ) : ?>
  <defs>
    <linearGradient id="<?php echo esc_attr( $options['linear_gradient']['id'] ); ?>">
    <?php foreach ( $options['linear_gradient']['stops'] as $stop ) : ?>
      <stop offset="<?php echo esc_attr( $stop['offset'] ); ?>%" stop-color="<?php echo esc_attr( $stop['color'] ); ?>" />
    <?php endforeach; ?>
    </linearGradient>
  </defs>
<?php endif; ?>

  <use href="#orokublocks-widget-svg-<?php echo esc_attr( $options['icon'] ); ?>"></use>
</svg>