<?php

/**
 * Icon List Block Template.
 *
 * @param array $block The block settings and attributes.
 * @param string $content The block inner HTML (empty).
 * @param bool $is_preview True during backend preview render.
 * @param int $post_id The post ID the block is rendering content against.
 * @param array $context The context provided to the block by the post or it's parent block.
 */

// Support custom "anchor" values.
$anchor = '';
if (!empty($block['anchor'])) {
  $anchor = 'id="' . esc_attr($block['anchor']) . '" ';
}

// Create class attribute allowing for custom "className" and "align" values.
$class_name = 'olympus-icon-list';
if (!empty($block['className'])) {
  $class_name .= ' ' . $block['className'];
}
if (!empty($block['align'])) {
  $class_name .= ' align' . $block['align'];
}

// Load values and assign defaults.
$icons = get_field('icons') ?: [];
$icon_justify = get_field('icon_justify') ?: 'center';

$class_name .= ' ' . $icon_justify;

$wrapper_attributes = get_block_wrapper_attributes([
  'class' => $class_name
]);
?>

<div <?php echo $anchor; ?><?php echo $wrapper_attributes; ?>>
  <?php if ($icons) : ?>
    <?php foreach ($icons as $icon) : ?>
      <?php
      if ($icon['icon']) {
        $svg = file_get_contents(get_template_directory() . '/admin/img/acf/' . $icon['icon'] . '.svg');
      }
      ?>
      <div class="olympus-icon-list-item">
        <?php if ($icon['link']) : ?>
          <a href="<?php if (is_admin()) echo '#';
                    else echo esc_url($icon['link']['url']); ?>" <?php echo $icon['link']['target']; ?>>
            <?php echo $svg; ?>
          </a>
        <?php else : ?>
          <?php echo $svg; ?>
        <?php endif; ?>
      </div>
    <?php endforeach; ?>
  <?php endif; ?>
</div>