<?php
get_header();
$args            = array(
    'post_type' => 'portfolio',
    'p'         => get_the_ID(),
);
$portfolio_posts = new WP_Query($args);
while ($portfolio_posts->have_posts()) :
    $portfolio_posts->the_post();
    $id = get_the_ID();
    if (get_post_meta($id, 'gallery_images', true)) {
        $gallery_images = explode(',', get_post_meta($id, 'gallery_images', true));

        foreach ($gallery_images as $key => $item) {
            if ($item == '') {
                unset($gallery_images[ $key ]);
            }
        }

        if (get_option('portfolio_single', 'style-1') == 'style-1') {
            $header_media  = "<div class='gallery'>";
            $header_media .= "<div class='gallery-inner'>";
            $j             = 0;
            foreach ($gallery_images as $item) {
                $image_src     = wp_get_attachment_image_src($item, 'full');
                $image_title   = get_the_title($item);
                $header_media .= "<div class='item'>";
                $header_media .= "<a rel='lightbox' href='" . $image_src[0] . "'>";
                $header_media .= "<img alt='" . esc_attr($image_title) . "'  src='" . esc_url($image_src[0]) . "'>";
                $header_media .= '</a>';
                $header_media .= '</div>';
                $j++;
            }
            $header_media .= '</div>';
            $header_media .= '</div>';
        } else {
            $header_media = "<div id='carousel' class='carousel slide'>";
            if (count($gallery_images) > 1) {
                $header_media .= "<ol class='carousel-indicators'>";
                for ($i = 0; $i < count($gallery_images); $i++) {
                    $active_class = $i === 0 ? 'active' : '';
                    $header_media .= "<li data-target='#carousel' data-slide-to='" . $i . "' class='" . $active_class . "'></li>";
                }
                $header_media .= '</ol>';
            }
            $header_media .= "<div class='carousel-inner'>";
            $j             = 0;
            foreach ($gallery_images as $item) {
                $image_src   = wp_get_attachment_image_src($item, 'blog-full');
                $image_title = get_the_title($item);
                if ($j == 0) {
                    $active_class = ' active';
                } else {
                    $active_class = '';
                }
                $header_media .= "<div class='item$active_class'>";
                $header_media .= "<img alt='" . esc_attr($image_title) . "'  src='" . esc_url($image_src[0]) . "'>";
                $header_media .= '</div>';
                $j++;
            }
            $header_media .= '</div>';
            if (count($gallery_images) > 1) {
                $header_media .= "<a class='left carousel-control' href='#carousel' data-slide='prev'>
                    <div class='tp-leftarrow tparrows default round'></div>
                </a>
                <a class='right carousel-control' href='#carousel' data-slide='next'>
                    <div class='tp-rightarrow tparrows default round'></div>
                </a>";
            }
            $header_media .= '</div></div>';
        }
    } elseif (has_post_thumbnail($id)) {
        $header_media = get_the_post_thumbnail($id, 'full');
    } elseif (get_post_meta($id, $key = 'anps_featured_video', $single = true)) {
        $header_media = do_shortcode(get_post_meta($id, $key = 'anps_featured_video', $single = true));
    } else {
        $header_media = '';
    }
    ?>
<section class="container portfolio-single">
    <div class="row">
        <?php if (get_option('portfolio_single', 'style-1') == 'style-1') : ?>
            <?php
                global $row_inner;
                $row_inner = true;
            ?>
        <div class="col-md-8"><?php $allowed_tags = wp_kses_allowed_html('post');
        echo wp_kses($header_media, $allowed_tags); //PHPCS: XSS ok. ?></div>
        <div class="col-md-4">
            <?php the_content(); ?>
            <div class="row">
                <div class="col-md-12 buttons folionav">
                    <?php previous_post_link('%link', '<button class="btn btn-lg style-5"><i class="fas fa-angle-left"></i> &nbsp; ' . __('prev', 'accounting') . '</button>'); ?>
                    <?php next_post_link('%link', '<button class="btn btn-lg style-5">' . __('next', 'accounting') . ' &nbsp; <i class="fas fa-angle-right"></i></button>'); ?>
                </div>
            </div>
        </div>
        <?php else : ?>
        <div class="col-md-12">
            <?php
                    global $row_inner;
                    $row_inner = true;
            ?>
            <?php
                $hide_img = get_post_meta($post->ID, $key = 'anps_hide_portfolio_img', $single = true);
            if ($hide_img == 0) {
                $allowed_tags = wp_kses_allowed_html('post');
                echo wp_kses($header_media, $allowed_tags); //PHPCS: XSS ok.
            }
            ?>
            <?php the_content(); ?>
        </div>
        <?php endif; ?>
</section>

    <?php
endwhile;

wp_reset_postdata();
?>

<?php if (get_option('portfolio_single_footer', '') != '') : ?>
<p>&nbsp;</p>
<p>&nbsp;</p>
<?php endif; ?>

<?php echo do_shortcode(stripslashes(get_option('portfolio_single_footer', ''))); ?>

<?php get_footer();
