<?php

/**
 * Fourth Wall Events
 * Single Post Template
 */
get_header();
global $post, $fwe_settings;
if (isset($post) && fwe_is_post($post)) {
    $categories = get_the_category($post->ID);
    if (count($categories) > 0) {
        $banner = get_term_meta($categories[0]->term_id, 'banner_image', true);
    } else {
        $banner = $fwe_settings['default_category_banner'];
    }
    $banner = $banner ? $banner : $fwe_settings['default_category_banner'];
    $banner = wp_get_attachment_image_src($banner, 'full');
}
?>

<?php 
if ($banner) {
    ?>
  <?php 
    $caption = get_term_meta($categories[0]->term_id, 'banner_caption', true);
    $caption = !empty($caption) ? $caption : $categories[0]->name;
    $banner_classes = 'post';
    include locate_template('partials/generic-banner.php');
}
?>
<?php

global $post;
$classes = array('banner');
if (fwe_is_post($post)) {
    $parent = fwe_get_root_parent($post);
    $classes[] = $parent->post_name;
}
$classes = 'class="' . implode(' ', $classes) . '"';
if ($banner && $banner['banner']) {
    ?>
  <?php 
    $banner_src = wp_get_attachment_image_src($banner['banner'], 'full');
    ?>
  <section <?php 
    echo $classes;
    ?>
>
    <figure id="page-banner">
      <img src="<?php 
    echo $banner_src[0];
    ?>
" alt="<?php 
    echo $banner['caption'];
    ?>
" width="100%">
      <figcaption><?php 
    echo $banner['caption'];
    ?>
</figcaption>
    </figure>
function fwe_get_root_parent($post)
{
    if (is_int($post)) {
        $post = get_post($post);
    }
    if (!fwe_is_post($post)) {
        return null;
    }
    if ($post->post_parent == 0) {
        return $post;
    }
    $parent = get_post($post->post_parent);
    return fwe_get_root_parent($parent);
}