<?php
/**
 * Frontend output for the AUX Button Slab component
 * AUX documentation: https://aggieux.tamu.edu/?path=/docs/components-button-slab-about-button-slabs--docs
 *
 * Outline:
 * 1. Set variables to null
 * 2. Get fields from the post, if they have values, wrap in html
 * 3. Build the component html
 * 4. Sanitize and output
 *
 * @author Hailey Sanders
 * @since 1.0.0
 */

use WHOOP\Entities\Elements\A;
use WHOOP\Entities\Elements\Div;
use WP_AUX\Services\AuxCF\BlocksService;

$buttonItems = aux_get_field('button');
$buttonEmphasis = aux_get_field('button_emphasis_switch');

$buttonGroup = '';
foreach ($buttonItems as $buttonIndex => $buttonItem) {
  if (empty($buttonItem['button_link'])) {
    continue;
  }

  if (empty($buttonGroup)) {
    $buttonGroup = Div::create(null, ['class' => 'button-group button-group--primary']);
  }

  // Setting the button class depending on emphasis
  $buttonClass = 'btn btn--primary-alt';

  $button = A::create($buttonItem['button_link']['title'], [
    'href' => $buttonItem['button_link']['url'],
    'class' => $buttonClass
  ]);

  $buttonGroup->appendElement($button);
}

if ($buttonEmphasis) {
  $button->addClass('btn--primary')->removeClass('btn--primary-alt');
}

$slab = BlocksService::getSlab('button-slab');
$slab->setContent($buttonGroup);

echo $slab;
