<?php
/**
 * Frontend output for the AUX Standalone Blockquote component
 * AUX documentation: https://aggieux.tamu.edu/?path=/story/components-quotes-standalone-block-quote
 *
 * 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 Jeremy Tarpley
 * @since 1.0.0
 */

use WP_AUX\Entities\AuxFramework\Partials\ImageEntity;
use WP_AUX\Services\AuxFramework\ElementClassesService;
use WP_AUX\Services\HtmlService;

// Get fields from the post, if they have values, wrap in html
$background = get_field('standalone_blockquote_background');
$style = get_field('standalone_blockquote_style');
$quote = get_field('standalone_blockquote_quote');
$imageFile = get_field('standalone_blockquote_image');

// Build component styles
$sectionClasses = ElementClassesService::buildDynamicClasses('section-wrap', '', $background);
$componentClasses = ElementClassesService::buildClasses('standalone-blockquote', $style, $sectionClasses);

// Pulling in image
if ($imageFile) {
  $image = ImageEntity::create($imageFile);
  // Setting class of media feature if image present
  $mediaFeatureStyle = ElementClassesService::buildClasses('media-feature', $style, 'gray');
}

// Creating quote
$quote = HtmlService::formattedValue($quote, '<blockquote class="blockquote__content"><p>%s</p></blockquote>');

// Build the caption
$captionContent = HtmlService::formattedValue(get_field('standalone_blockquote_name'), '<span class="name">%s</span>');
if ($captionContent) {
  // Only get the title and org if name has a value
  $captionContent .= HtmlService::formattedValue(get_field('standalone_blockquote_title'), '<span class="title">%s</span>');
  $captionContent .= HtmlService::formattedValue(get_field('standalone_blockquote_org'), '<span class="org">%s</span>');
}
$caption = HtmlService::formattedValue($captionContent,
  '<figcaption class="blockquote__cite">
      %s
   </figcaption>');

$blockquote = <<<HTML
    <div class="$componentClasses">
      <figure class="blockquote blockquote--has-cite">
        $quote
        $caption
      </figure>
    </div>
HTML;


// Build the component html
// Markup changes if image option is selected
if ($imageFile) {
  $output = <<<HTML
<div class="$mediaFeatureStyle">
  <div class="media-feature__media">
    $image
  </div>
  <div class="media-feature__content">
    $blockquote
  </div>
</div>
HTML;
} else {
  $output = <<<HTML
  <section class="$sectionClasses">
    $blockquote
  </section>
HTML;
}

echo $output;
