<?php

/**
 * Big button 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.
 *          This is either the post ID currently being displayed inside a query loop,
 *          or the post ID of the post hosting this block.
 * @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-big-button wiggle';
$wrapper_attributes = get_block_wrapper_attributes(
  [
    'class' => $class_name
  ]
);
$link = get_field('link');
$subtitle = get_field('subtitle');

// TODO: validate this block
?>
<?php if ($link && !is_admin()): ?>
  <a href="<?php echo $link['url'] ?>" <?php echo $anchor ?><?php echo $wrapper_attributes; ?>>
    <?php if ($link && $link['title']): ?>
      <h3><?php echo $link['title'] ?></h3>
    <?php endif; ?>
    <?php if ($subtitle): ?>
      <p><?php echo $subtitle ?></p>
    <?php endif; ?>
  </a>
<?php else: ?>
  <div <?php echo $anchor ?><?php echo $wrapper_attributes; ?>>
    <?php if ($link && $link['title']): ?>
      <h3><?php echo $link['title'] ?></h3>
    <?php endif; ?>
    <?php if ($subtitle): ?>
      <p><?php echo $subtitle ?></p>
    <?php endif; ?>
  </div>
<?php endif; ?>