<?php get_header() ?>

  <?php the_post() ?>

  <?php
    $site_host = parse_url(get_site_url(), PHP_URL_HOST);
    $post_id = get_the_ID();
    $post_type = get_post_type($post_id);
    $post_meta = sandbox_get_post_meta($post_id);

    if ( !isset($post_meta['_hide']) || !filter_var($post_meta['_hide'], FILTER_VALIDATE_BOOLEAN) ) {
      $post_taxonomies = sandbox_get_the_taxonomies_and_terms($post_id);

      $content = '';
      $post_class = 'no-download';
      $content_meta_args = array('date');

      // post style attribute
      $post_style = '';

      $color_hex_pattern = '/#([a-f0-9]{3}){1,2}\b/i';
      $has_background_color = isset($post_meta['_background-color']) && preg_match($color_hex_pattern, $post_meta['_background-color']);
      $has_post_style = $has_background_color;

      if ( $has_post_style ) {
        $post_style = ' style="';
      }

      // background-color
      if ( $has_background_color ) {
        $post_style .= 'background-color:' . $post_meta['_background-color'] . ';';
        $post_class .= ' has-background-color';
      }
      if ( $has_post_style ) {
        $post_style .= '"';
      }

      // Dark or light color scheme
      if ( isset($post_meta['_color-scheme']) ) {
        $post_class .= ' color-scheme-' . $post_meta['_color-scheme'];
      }

      // Show image before or after the content
      if ( isset($post_meta['_image-position']) ) {
        $post_class .= ' image-position-' . $post_meta['_image-position'];
      }

      // Post type "page" fields
      $subheadline = isset($post_meta['_subheadline']) ? apply_filters('the_content', $post_meta['_subheadline']) : '';

      // Post type "page"
      if ( $post_type === 'page' ) {
        $content_meta_args = false;
      }

      // Post type "service"
      if ( $post_type === 'service' ) {
        $content_meta_args = false;
      }

      // Post type "product" fields
      $produkt_pdf = isset($post_meta['_pdf-download']) ? $post_meta['_pdf-download'] : '';
      $produkt_url = isset($post_meta['_product-url']) ? $post_meta['_product-url'] : '';

      if ( !empty($produkt_pdf) ) {
        $produkt_url = wp_get_attachment_url($produkt_pdf);
      }
      if ( $post_type === 'produkt' ) {
        $content_meta_args = false;
      }

      // Download button
      $download_url = !empty($produkt_pdf) ? $produkt_pdf : '';
      $download_target = '';

      if ( !empty($download_url) ) {
        if ( parse_url($download_url, PHP_URL_HOST) !== $site_host ) {
          $download_target = ' target="_blank" rel="noopener"';
        }
        $post_class = 'has-download';
      }

      if ( empty($content) ) {
        $content = apply_filters('the_content', get_the_content());
      }
  ?>
  <article id="content" <?php post_class($post_class) ?><?php echo $post_style; ?>>
    <header class="content-header">
      <div class="inner">
        <h1 class="title"><?php the_title(); ?>
      <?php if ( !empty($download_url) ) { ?>
        <a class="download" href="<?php echo $download_url; ?>"<?php echo $download_target; ?>><?php echo __('Download', 'sandbox'); ?></a>
      <?php } ?></h1>
      <?php if ( !empty($subheadline) ) { ?>
        <div class="subheadline content">
          <?php echo $subheadline; ?>
        </div>
      <?php } ?>
      <?php if ( has_excerpt() ) { ?>
        <div class="intro">
          <?php the_excerpt(); ?>
        </div>
      <?php } ?>
      <?php if ( $meta = sandbox_get_meta_data($content_meta_args) ) { ?>
        <ul class="meta">
          <?php echo $meta; ?>
        </ul>
      <?php } ?>
      </div>
    </header>
    <?php
      /*
      * @Sidebar Content before
      */
      get_sidebar('content-before'); ?>
    <div class="content inner">
    <?php if ( has_post_thumbnail() ) { ?>
      <span class="post-thumbnail" <?php echo $thumbnail_style; ?>><?php the_post_thumbnail('full'); ?></span>
    <?php } ?>
    <?php if ( !empty($content) ) { ?>
      <?php echo $content; ?>
    <?php } ?>
    </div>
    <?php
      /*
      * @Sidebar Content after
      */
      get_sidebar('content-after'); ?>
  <?php  // If comments are open or we have at least one comment, load up the comment template. ?>
  <?php  if ( comments_open() || get_comments_number() ) { ?>
    <?php comments_template('/comments.php'); ?>
  <?php  } ?>
  </article><!-- .post -->
  <?php  } ?>

<?php get_footer() ?>
