<?php
?>

<div class="single-catgory-layout">
    <div class="layout-5">
        <?php if($layout_header_enable): ?>
        <div class="layout-header">
            <div class="layout-title">
                <h4>
                    <?php echo esc_html($layout_title); ?>
                </h4>
            </div>
            <?php if($layout_view_all_enable): ?>
            <div class="view-all">
                <a href="<?php echo get_category_link($cat_id); ?>">
                    <?php esc_html_e('View All', 'widgetkit-pro'); ?>
                    <i class="fa fa-long-arrow-right"></i>
                </a>
            </div>
            <?php endif; ?>
        </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);
        ?>
        <div class="cat-post-list post-grid-<?php echo $regular_post_grid ? $regular_post_grid : 'no-grid'; ?> <?php echo 'highlight-post-style-'.$highlight_layout_style; ?>">
        <?php
        if($query->have_posts()):
            ?>
            <div class="cat-post-list-wrapper">
                <?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 = '#';
                    }


                    ?>
                        <?php if($count == 0 && ($highlight_first_post == 'yes')): ?>
                        <div class="featured-post">
                            <?php if(has_post_thumbnail()): ?>
                                <div class="post-thumb">
                                    <a href="<?php the_permalink() ?>">
                                        <?php the_post_thumbnail($featured_thumbnail_size); ?>
                                    </a>
                                    <?php
                                    if('video' == get_post_format(get_the_ID())):?>
                                        <a class="<?php echo $video[0] ? '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 endif; ?>
                            <div class="post-data">
                                <div class="post-title">
                                    <div class="highlight-post-cat">
                                        <?php magmax_limited_cateory_with_seperator_for_elements(get_the_ID()); ?>
                                    </div>
                                    <a href="<?php the_permalink(); ?>" class="highlight-post-title highlight-title">
                                        <h3><?php the_title(); ?></h3>
                                    </a>
                                </div>
                                <?php if($show_excerpt): ?>
                                    <div class="post-excerpt">
                                        <?php echo wp_trim_words(get_the_excerpt(), $highlight_excerpt_word_count, ''); ?>
                                    </div>
                                <?php endif; ?>
                            </div>
                        </div>
                        <?php else: ?>
                        <div class="regular-post">
                            <div class="regular-post-wrapper row <?php echo esc_html($regular_post_style); ?>">
                                <?php if(has_post_thumbnail()): ?>
                                    <div class="post-thumb <?php echo ($highlight_first_post == 'yes') ? 'col-md-4': 'col-md-4';?>">
                                        <a href="<?php the_permalink(); ?>">
                                            <?php the_post_thumbnail($regular_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 endif; ?>
                                <div class="post-data <?php echo ($highlight_first_post == 'yes') ? 'col-md-8': 'col-md-8';?>">
                                    <div class="post-title">
                                        <?php if($show_cat_tag): ?>
                                            <div class="post-cat">
                                                <?php magmax_limited_cateory_with_seperator_for_elements(get_the_ID()); ?>
                                            </div>
                                        <?php endif; ?>
                                        <a href="<?php the_permalink(); ?>" class="regular-post-title regular-title">
                                            <h3><?php the_title(); ?></h3>
                                        </a>
                                    </div>
                                </div>
                            </div>
                        </div>
                        <?php endif; ?>
                    <?php
                    $count++;
                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
                });
            });
        </script>
        <?php //wp_reset_query(); ?>
    </div>
</div>