<?php
?>
<div class="single-category-carousel single-category-carousel-layout-4">
    <?php if(empty($cat_id)):?>
        <div class="empty-cat">
            <h3>Please select a category first</h3>
        </div>
    <?php endif;?>
    
    <?php if($layout_title): ?>
    <div class="layout-header">
        <div class="layout-title">
            <h2>
                <?php echo esc_html($layout_title); ?>
            </h2>
        </div>
    </div>
    <?php endif; ?>
    <?php
    /**
     * Argument
     * for query post
     * ================
     */
    $count = 0;
    $args = array(
        'cat' => $cat_id,
        'post_type' =>  'post',
        'posts_per_page' => $post_num_to_show,
    );
    $query = new WP_Query($args);
    $total = $query->found_posts;
    ?>
    <div class="cat-post-list">

    <?php
    if($query->have_posts()):
        ?>
        <div class="cat-post-list-wrapper owl-carousel">
            <?php
            while($query->have_posts()):
                $query->the_post();

                $content = apply_filters( 'the_content', get_the_content() );
                $video = false;
                /**
                 * Only get video from the content if a playlist isn't present.
                 */
                if (false === strpos($content, 'wp-playlist-script')) {
                    $video = get_media_embedded_in_content($content, array('video', 'object', 'embed', 'iframe'));
                }
                if($video){
                    preg_match('/src="([^"]+)"/', $video[0], $match);
                    $url = $match[1];
                    $new_url = str_replace('embed/', 'watch?v=', $url);
                }else{
                    $new_url = '#';
                }

                ?>
                    <div class="regular-post">
                        <div class="regular-post-wrapper">
                            <?php if(has_post_thumbnail()): ?>
                                <div class="post-thumb">
                                    <a class="post-thumb-wrapper" href="<?php the_permalink(); ?>">
                                        <?php the_post_thumbnail($thumbnail_size ); ?>
                                    </a>
                                    <?php
                                    if('video' == get_post_format(get_the_ID())):?>
                                        <a class="<?php echo $video ? 'video-post-popup' : 'video-post-icon'; ?>"
                                           href="<?php echo $new_url ? $new_url : '#'; ?>">
                                            <img  src="<?php echo get_template_directory_uri(). '/dist/images/play-button-bold.png'; ?>" alt="<?php echo esc_attr('play-button', 'widgetkit-pro'); ?>">
                                        </a>
                                    <?php endif;?>
                                </div>
                            <?php else: ?>
                                <div class="post-thumb">
                                    <a class="post-thumb-wrapper" href="<?php the_permalink(); ?>">
                                        <img src="<?php echo esc_url(WKP_PLACEHOLDER_URL); ?>" alt="">
                                    </a>
                                </div>
                            <?php endif; ?>
                            <div class="post-data">
                                <div class="post-title">
                                    <a href="<?php the_permalink(); ?>" class="regular-post-title regular-title">
                                        <h3><?php the_title(); ?></h3>
                                    </a>
                                </div>
                                <div class="wkp-post-meta">
                                    <div class="post-author"><?php echo get_the_author(); ?></div>
                                    <div class="seperator"> <span><?php esc_html_e('|', 'widgetkit-pro'); ?></span></div>
                                    <div class="post-date"><?php echo get_the_date(); ?></div>
                                </div>
                                <?php if($show_excerpt): ?>
                                    <div class="post-excerpt"><?php echo wp_trim_words(get_the_excerpt(), $regular_excerpt_word_count, ''); ?></div>
                                <?php endif; ?>
                            </div>
                        </div>
                    </div>
                <?php
            endwhile;
            wp_reset_postdata();
            ?>
        </div>
        <?php
    endif;
    ?>
    </div>
    <script>
        jQuery(function($){
            $('.video-post-popup').magnificPopup({
                disableOn: 700,
                type: 'iframe',
                mainClass: 'mfp-fade',
                removalDelay: 160,
                preloader: false,
                fixedContentPos: false
            });
            $(".single-category-carousel-layout-4 .cat-post-list-wrapper").owlCarousel({
                pagination: true,
                dots:false,
                loop: <?php echo $total > 1 ? 'true' : 'false'; ?>,
                items:1,
                nav: true,
                navClass: ['owl-carousel-left','owl-carousel-right'],
                navText: ['<i class="fa fa-long-arrow-left fa-fw""></i>', '<i class="fa fa-long-arrow-right fa-fw"></i>'],
                margin:0,
                autoplay:false,
                autoHeight: true,
                autoHeightClass: 'owl-height',
                responsive:{
                    0:{
                        items:1
                    },
                    600:{
                        items:1
                    },
                    1000:{
                        items:1
                    }
                }
            });
        });

    </script>
    <?php //wp_reset_query(); ?>
</div>