<?php
if (!defined('ABSPATH'))
    exit; // Exit if accessed directly
/**
 * Template Post
 * 
 * Access original fields: $mod_settings
 * @author Themify
 */
$fields_default = array(
    'mod_title_post' => '',
    'layout_post' => '',
    'post_type_post' => 'post',
    'type_query_post' => 'category',
    'category_post' => '',
    'query_slug_post' => '',
    'post_per_page_post' => '',
    'offset_post' => '',
    'order_post' => 'desc',
    'orderby_post' => 'date',
    'display_post' => 'content',
    'hide_feat_img_post' => 'no',
    'image_size_post' => '',
    'img_width_post' => '',
    'img_height_post' => '',
    'unlink_feat_img_post' => 'no',
    'hide_post_title_post' => 'no',
    'unlink_post_title_post' => 'no',
    'hide_post_date_post' => 'no',
    'hide_post_meta_post' => 'no',
    'hide_page_nav_post' => 'yes',
    'animation_effect' => '',
    'css_post' => ''
);

if (isset($mod_settings['category_post']))
    $mod_settings['category_post'] = $this->get_param_value($mod_settings['category_post']);

$fields_args = wp_parse_args($mod_settings, $fields_default);
extract($fields_args, EXTR_SKIP);
$animation_effect = $this->parse_animation_effect($animation_effect, $fields_args);

$container_class = implode(' ', apply_filters('themify_builder_module_classes', array(
    'module', 'module-' . $mod_name, $module_ID, $css_post
                ), $mod_name, $module_ID, $fields_args)
);
$container_props = apply_filters( 'themify_builder_module_container_props', array(
    'id' => $module_ID,
    'class' => $container_class
), $fields_args, $mod_name, $module_ID );

$this->add_post_class($animation_effect);
$this->in_the_loop = true;
global $paged, $wp;
$paged = $this->get_paged_query();
?>
<?php if ($orderby_post == 'rand' || TFCache::start_cache('post', self::$post_id, array('page' => $paged, 'ID' => $module_ID))):
add_filter( 'themify_after_post_title_parse_args', 'themify_builder_post_title_args' );
?>
    <!-- module post -->
    <div<?php echo $this->get_element_attributes( $container_props ); ?>>
        <?php if ($mod_title_post != ''): ?>
            <?php echo $mod_settings['before_title'] . wp_kses_post(apply_filters('themify_builder_module_title', $mod_title_post, $fields_args)) . $mod_settings['after_title']; ?>
        <?php endif; ?>

        <?php
        do_action('themify_builder_before_template_content_render');
        // The Query

        $order = $order_post;
        $orderby = $orderby_post;
        $limit = $post_per_page_post;
        $terms = isset($fields_args["{$type_query_post}_post"]) ? $fields_args["{$type_query_post}_post"] : $category_post;
        // deal with how category fields are saved
        $terms = preg_replace('/\|[multiple|single]*$/', '', $terms);

        $temp_terms = explode(',', $terms);
        $new_terms = $new_exclude_terms = array();
        $is_string = false;
        foreach ($temp_terms as $t) {
            if (!is_numeric($t))
                $is_string = true;
            if ('' != $t) {
                $result_array = ( ( is_numeric( $t ) && 0 <= $t ) || ! is_numeric( $t ) ) ? 'new_terms' : 'new_exclude_terms';
                array_push( $$result_array, is_numeric( $t ) ? abs( trim( $t ) ) : trim( $t ) );
            }
        }
        $tax_field = ( $is_string ) ? 'slug' : 'id';

        $args = array(
            'post_status' => 'publish',
            'posts_per_page' => $limit,
            'order' => $order,
            'orderby' => $orderby,
            'suppress_filters' => false,
            'paged' => $paged,
            'post_type' => $post_type_post
        );

        if (count($new_terms) > 0 && !in_array('0', $new_terms) && 'post_slug' !== $type_query_post) {
            $args['tax_query'] = array(
                array(
                    'taxonomy'  => $type_query_post,
                    'field'     => $tax_field,
                    'terms'     => $new_terms,
                ),
            );
        }
        if (count($new_exclude_terms) > 0 && 'post_slug' !== $type_query_post) {
            $args['tax_query'][] = array(
                'taxonomy'  => $type_query_post,
                'field'     => $tax_field,
                'terms'     => $new_exclude_terms,
                'operator'  => "NOT IN",
            );
        }

        if (!empty($query_slug_post) && 'post_slug' == $type_query_post) {
            $args['post__in'] = $this->parse_slug_to_ids($query_slug_post, $post_type_post);
        }

        // add offset posts
        if ($offset_post != '') {
            if (empty($limit))
                $limit = get_option('posts_per_page');

            $args['offset'] = ( ( $paged - 1 ) * $limit ) + $offset_post;
        }

        $the_query = new WP_Query();
        $args = apply_filters("themify_builder_module_{$mod_name}_query_args", $args, $fields_args);
        $posts = $the_query->query($args);
        ?>
        <div class="builder-posts-wrap clearfix loops-wrapper <?php echo apply_filters('themify_builder_module_loops_wrapper', $layout_post) ?>">
            <?php
            // if the active theme is using Themify framework use theme template loop (includes/loop.php file)
            if (themify_is_themify_theme()) {
                // save a copy
                global $themify;
                $themify_save = clone $themify;

                // override $themify object
                $themify->hide_image = $hide_feat_img_post;
                $themify->unlink_image = $unlink_feat_img_post;
                $themify->hide_title = $hide_post_title_post;
                $themify->width = $img_width_post;
                $themify->height = $img_height_post;
                $themify->image_setting = 'ignore=true&';
                $themify->is_builder_loop = true;
                if ($this->is_img_php_disabled())
                    $themify->image_setting .= $image_size_post != '' ? 'image_size=' . $image_size_post . '&' : '';
                $themify->unlink_title = $unlink_post_title_post;
                $themify->display_content = $display_post;
                $themify->hide_date = $hide_post_date_post;
                $themify->hide_meta = $hide_post_meta_post;
                $themify->post_layout = $layout_post;

                // hooks action
                do_action_ref_array('themify_builder_override_loop_themify_vars', array( $themify, $mod_name, $fields_args ) );

                $out = '';
                if ($posts) {
                    $out .= themify_get_shortcode_template($posts);
                } else {
                    if( current_user_can( 'publish_posts' ) ) {
                        printf( __('<a href="%s" target="_blank">Click here</a> to add post and assign it in %s', 'themify' )
                            , $post_type_post != 'post' ? admin_url( 'post-new.php?post_type=' . $post_type_post )
                                : admin_url( 'post-new.php' )
                            , $category_post ? $category_post : $type_query_post  );
                    }
                }
                // revert to original $themify state
                $themify = clone $themify_save;
                echo!empty($out) ? $out : '';
            } else {
                // use builder template
                global $post;
                $temp_post = $post;
                foreach ($posts as $post): setup_postdata($post);
                    ?>

                    <?php themify_post_before(); // hook   ?>

                    <article id="post-<?php echo esc_attr($post->ID); ?>" <?php post_class("post clearfix"); ?>>

                        <?php themify_post_start(); // hook   ?>

                        <?php
                        if ($hide_feat_img_post != 'yes') {
                            $width = $img_width_post;
                            $height = $img_height_post;
                            $param_image = 'w=' . $width . '&h=' . $height . '&ignore=true';
                            if ($this->is_img_php_disabled())
                                $param_image .= $image_size_post != '' ? '&image_size=' . $image_size_post : '';

                            //check if there is a video url in the custom field
                            if (themify_get('video_url') != '') {
                                global $wp_embed;

                                themify_before_post_image(); // Hook

                                echo $wp_embed->run_shortcode('[embed]' . esc_url(themify_get('video_url')) . '[/embed]');

                                themify_after_post_image(); // Hook
                            } elseif ($post_image = themify_get_image($param_image)) {

                                themify_before_post_image(); // Hook 
                                ?>

                                <figure class="post-image">
                                    <?php if ($unlink_feat_img_post == 'yes'): ?>
                                        <?php echo wp_kses_post($post_image); ?>
                                    <?php else: ?>
                                        <a href="<?php echo themify_get_featured_image_link(); ?>"><?php echo wp_kses_post($post_image); ?></a>
                                    <?php endif; ?>
                                </figure>

                                <?php
                                themify_after_post_image(); // Hook
                            }
                        }
                        ?>

                        <div class="post-content">

                            <?php if ($hide_post_date_post != 'yes'): ?>
                                <time datetime="<?php the_time('o-m-d') ?>" class="post-date" pubdate><?php the_date(apply_filters('themify_loop_date', '')) ?></time>
                            <?php endif; //post date   ?>

                            <?php if ($hide_post_title_post != 'yes'): ?>
                                <?php themify_before_post_title(); // Hook ?>
                                <?php if ($unlink_post_title_post == 'yes'): ?>
                                    <h2 class="post-title"><?php the_title(); ?></h2>
                                <?php else: ?>
                                    <h2 class="post-title"><a href="<?php echo themify_get_featured_image_link(); ?>"><?php the_title(); ?></a></h2>
                                <?php endif; //unlink post title    ?>
                                <?php themify_after_post_title(); // Hook ?> 
                            <?php endif; //post title  ?>    

                            <?php if ($hide_post_meta_post != 'yes'): ?>
                                <p class="post-meta"> 
                                    <span class="post-author"><?php the_author_posts_link() ?></span>
                                    <span class="post-category"><?php the_category(', ') ?></span>
                                    <?php the_tags(' <span class="post-tag">', ', ', '</span>'); ?>
                                    <?php if (!themify_get('setting-comments_posts') && comments_open()) : ?>
                                        <span class="post-comment"><?php comments_popup_link(__('0 Comments', 'themify'), __('1 Comment', 'themify'), __('% Comments', 'themify')); ?></span>
                                    <?php endif; //post comment    ?>
                                </p>
                            <?php endif; //post meta   ?>    

                            <?php
                            // fix the issue more link doesn't output
                            global $more;
                            $more = 0;
                            ?>

                            <?php if ($display_post == 'excerpt'): ?>
                                <?php the_excerpt(); ?>
                            <?php elseif ($display_post != 'none'): ?>
                                <?php the_content(themify_check('setting-default_more_text') ? themify_get('setting-default_more_text') : __('More &rarr;', 'themify')); ?>
                            <?php endif; //display content  ?>

                            <?php edit_post_link(__('Edit', 'themify'), '[', ']'); ?>

                        </div>
                        <!-- /.post-content -->
                        <?php themify_post_end(); // hook   ?>

                    </article>
                    <?php themify_post_after(); // hook    ?>

                    <?php
                endforeach;

                if( !$posts && current_user_can( 'publish_posts' ) ) {
                    printf( __( '<a href="%s" target="_blank">Click here</a> to add post and assign it in %s', 'themify' )
                        , $post_type_post != 'post' ? admin_url( 'post-new.php?post_type=' . $post_type_post )
                            : admin_url( 'post-new.php' )
                        , $category_post ? $category_post : $type_query_post  );
                }
                wp_reset_postdata();
                $post = $temp_post;
            } // end $is_theme_template
            ?>
        </div><!-- .builder-posts-wrap -->
        <?php if ('yes' != $hide_page_nav_post): ?>
           <?php echo $this->get_pagenav( '', '', $the_query, $offset_post ) ?>
        <?php endif; ?>
        <?php
        do_action('themify_builder_after_template_content_render');
        $this->remove_post_class($animation_effect);
        ?>
    </div>
    <!-- /module post -->
<?php endif; ?>
<?php remove_filter( 'themify_after_post_title_parse_args', 'themify_builder_post_title_args' ); ?>
<?php if ($orderby_post != 'rand'): ?>
    <?php TFCache::end_cache(); ?>
<?php endif; ?>
<?php $this->in_the_loop = false; ?>