<?php
/*
 * Element Tz View Post
 * */

function tzinteriart_recent_post($atts)
{
    $tz_type = $tz_number_posts = $tz_number_columns = $tz_image_height_option = $tz_image_height = $tz_css_animation = $tz_align = '';
    $tz_hide_author = $tz_hide_date = $hide_comment = $hide_description = $hide_readmore = $hide_total_view = '';
    extract(shortcode_atts(array(
        'tz_type' => '1',
        'tz_number_posts' => '',
        'tz_number_columns' => '2',
        'tz_image_height_option' => 'auto',
        'tz_image_height' => '',
        'tz_css_animation' => '',
        'tz_align' => '1',
        'tz_hide_author' => 'show',
        'tz_hide_date' => 'show',
        'hide_comment' => 'show',
        'hide_description' => 'show',
        'hide_readmore' => 'show',
        'hide_readmore' => 'show',
        'hide_total_view' => 'show',
    ), $atts));
    ob_start();

    if ($tz_type == '2' || $tz_image_height_option == 'fix') {
        wp_enqueue_script('tz-recent-post');
    }

    if ($tz_align == '1') {
        $tz_align_value = 'tz_align_left';
    } elseif ($tz_align == '2') {
        $tz_align_value = 'tz_align_center';
    } elseif ($tz_align == '3') {
        $tz_align_value = 'tz_align_right';
    }

    $interiart_class = '';
    if ($tz_type == '1') {
        $interiart_class = 'tzRecent-post-type-1';
    } elseif ($tz_type == '2') {
        $interiart_class = 'tzRecent-post-type-2';
    } elseif ($tz_type == '3') {
        $interiart_class = 'tzRecent-post-type-3';
    } elseif ($tz_type == '4') {
        $interiart_class = 'tzRecent-post-type-4';
    } elseif ($tz_type == '5') {
        $interiart_class = 'tzRecent-post-type-5';
    }
    $interiart_column = '';
    $interiart_class_column = '';
    if ($tz_type == '2') {
        $interiart_column = 2;
        $interiart_class_column = 'col-md-6';
    } elseif ($tz_type == '1' || $tz_type == '3' || $tz_type == '4' || $tz_type == '5') {
        $interiart_column = $tz_number_columns;
        if ($tz_number_columns == '2') {
            $interiart_class_column = 'col-md-6';
        } elseif ($tz_number_columns == '3') {
            $interiart_class_column = 'col-md-4';
        } elseif ($tz_number_columns == '4') {
            $interiart_class_column = 'col-md-3';
        }
    }

    $interiart_image_class = '';
    if ($tz_type == '2' && $tz_image_height_option == 'fix') {
        $interiart_image_class = 'tz-image-fix-height';
    } elseif ($tz_type == '2' && $tz_image_height_option == 'auto') {
        $interiart_image_class = 'tz-image-auto-height';
    }

    $interiart_args = array(
        'numberposts' => $tz_number_posts,
        'offset' => 0,
        'category' => 0,
        'orderby' => 'post_date',
        'order' => 'DESC',
        'post_type' => 'post',
        'post_status' => 'publish',
        'suppress_filters' => true);

    ?>
    <div class="tzElement-recent-post <?php echo esc_attr($interiart_class); ?> <?php echo esc_attr($tz_align_value); ?> ">

        <?php
        $interiart_count = 1;
        $recent_posts = wp_get_recent_posts($interiart_args);
        $recent_total = count($recent_posts);

        foreach ($recent_posts as $recent) {
            global $post;
            $interiart_date_format = get_option('date_format');
            $interiart_comment_count = wp_count_comments($recent["ID"]);
            $interiart_total_comments = $interiart_comment_count->total_comments;
            $interiart_total_views = interiart_getPostViews($recent["ID"]);

            ?>
            <?php
            if ($interiart_count % $interiart_column == 1) {
                ?>
                <div class="row">
                <?php
            }
            ?>
            <div class="<?php echo esc_attr($interiart_class_column); ?>">
                <div class="tz-recent-inner">
                    <?php
                    if ($tz_type == '1' || $tz_type == '3' || $tz_type == '4' || $tz_type == '5' ) {
                        ?>
                        <?php echo get_the_post_thumbnail($recent["ID"], 'large'); ?>

                        <?php if ($tz_type == '1'): ?>
                            <div class="tz-recent-meta">
                                <?php if ($tz_hide_date == 'show') : ?>
                                    <span>
                                            <i class="fa fa-calendar"></i>
                                            <?php echo get_the_date($interiart_date_format, $recent["ID"]); ?>
                                        </span>
                                <?php endif; ?>
                                <?php if ($hide_comment == 'show') : ?>
                                    <span>
                                            <small>/</small>
                                            <i class="fa fa-comment-o" aria-hidden="true"></i>
                                            <?php
                                            if ($interiart_total_comments > 1) {
                                                echo esc_html($interiart_total_comments) . esc_html__(' Comments', 'tz-interiart');
                                            } else {
                                                echo esc_html($interiart_total_comments) . esc_html__(' Comment', 'tz-interiart');
                                            }
                                            ?>
                                        </span>
                                <?php endif; ?>
                                <?php if ($hide_total_view == 'show') : ?>
                                    <span>
                                            <small>/</small>
                                            <i class="fa fa-heart-o"></i>
                                            <?php
                                            if ($interiart_total_views > 1) {
                                                echo esc_html($interiart_total_views) . esc_html__(' Views', 'tz-interiart');
                                            } else {
                                                echo esc_html($interiart_total_views) . esc_html__(' View', 'tz-interiart');
                                            }
                                            ?>
                                        </span>
                                <?php endif; ?>
                            </div>
                        <?php endif; ?>
                        <?php if ($tz_type == '5'): ?>
                            <div class="tz_box_info">
                        <?php endif; ?>
                        <h3 class="tz-recent-title">
                            <a href="<?php echo get_permalink($recent["ID"]); ?>"><?php echo $recent["post_title"]; ?></a>
                        </h3>

                        <?php if ($tz_type == '3'): ?>
                            <div class="tz-recent-meta">
                                <?php
                                if ($tz_hide_date == 'show') :?>

                                    <span>
                                            <i class="fa fa-calendar"></i>
                                            <?php
                                            if ($tz_align == 2) {
                                                echo get_the_date($interiart_date_format, $recent["ID"]);
                                            } elseif ($tz_align == 1 || $tz_align == 3) {
                                                echo get_the_date('Y-m-d', $recent['ID']);
                                            }
                                            ?>
                                 </span>
                                <?php endif; ?>
                                <?php if ($tz_hide_author == 'show') : ?>
                                    <span>
                                    <i class="fas fa-user-circle"></i>
                                     <?php the_author(); ?>
                                    </span>
                                <?php endif; ?>
                                <?php if ($hide_comment == 'show') : ?>
                                    <span>
                                            <i class="fa fa-commenting" aria-hidden="true"></i>
                                            <?php
                                            echo esc_html($interiart_total_comments);
                                            ?>
                                 </span>
                                <?php endif; ?>
                                <?php if ($hide_total_view == 'show') : ?>
                                    <span>
                                            <i class="fa fa-heart" aria-hidden="true"></i>
                                            <?php
                                            echo esc_html($interiart_total_views);
                                            ?>
                                </span>
                                <?php endif; ?>
                            </div>
                        <?php endif; ?>

                        <?php if ($tz_type == '1'): ?>
                            <?php if ($hide_description == 'show') : ?>
                                <p class="tz-recent-description">
                                    <?php echo get_the_excerpt($recent["ID"]); ?>
                                </p>
                            <?php endif; ?>
                            <?php if ($hide_readmore == 'show') : ?>
                                <a class="tz-recent-readmore"
                                   href="<?php echo get_permalink($recent["ID"]); ?>"><?php echo esc_html__('readmore', 'tz-interiart'); ?></a>
                            <?php endif; ?>
                        <?php endif; ?>

                        <?php if ($tz_type == '4'): ?>
                            <div class="tz-recent-meta">
                                <?php if ($tz_hide_date == 'show') : ?>
                                    <span>
                                    <i class="fa fa-calendar"></i>
                                   <?php echo get_the_date('Y-m-d', $recent['ID']); ?>
                                </span>
                                <?php endif; ?>

                                <?php if ($tz_hide_author == 'show') : ?>
                                    <span>
                                    <i class="fas fa-user-circle"></i>
                                     <?php the_author(); ?>
                                    </span>
                                <?php endif; ?>
                                <?php if ($hide_comment == 'show') : ?>
                                    <span>
                                            <i class="fa fa-commenting" aria-hidden="true"></i>
                                            <?php
                                            echo esc_html($interiart_total_comments);
                                            ?>
                                </span>
                                <?php endif; ?>
                            </div>
                            <?php if ($hide_description == 'show') : ?>
                                <p class="tz-recent-description">
                                    <?php echo get_the_excerpt($recent["ID"]); ?>
                                </p>
                            <?php endif; ?>
                            <?php if ($hide_readmore == 'show') : ?>
                                <a class="tz-recent-readmore"
                                   href="<?php echo get_permalink($recent["ID"]); ?>"><?php echo esc_html__('read more >', 'tz-interiart'); ?></a>
                            <?php endif; ?>
                        <?php endif; ?>

                        <?php if ($tz_type == '5'): ?>

                            <div class="tz-recent-meta">
                                <?php if ($tz_hide_date == 'show') : ?>
                                    <span>
                                   <?php echo get_the_date('F j, Y', $recent['ID']); ?>
                                </span>
                                <?php endif; ?>

                                <?php if ($tz_hide_author == 'show') : ?>
                                    <span>By
                                     <?php the_author(); ?>
                                    </span>
                                <?php endif; ?>
                                <?php if ($hide_comment == 'show') : ?>
                                    <span>
                                            <?php
                                            echo esc_html($interiart_total_comments);
                                            ?>
                                </span>
                                <?php endif; ?>
                            </div>
                            <?php if ($hide_description == 'show') : ?>
                                <p class="tz-recent-description">
                                    <?php echo get_the_excerpt($recent["ID"]); ?>
                                </p>
                            <?php endif; ?>
                            <?php if ($hide_readmore == 'show') : ?>
                                <a class="tz-recent-readmore"
                                   href="<?php echo get_permalink($recent["ID"]); ?>"><?php echo esc_html__('read more', 'tz-interiart'); ?></a>
                            <?php endif; ?>
                        </div>
                        <?php endif; ?>
                        <?php
                    } elseif ($tz_type == '2') {
                        ?>
                        <div class="row">
                            <div class="col-md-6">
                                <div class="tz-recent-image <?php echo esc_attr($interiart_image_class); ?>" <?php
                                if ($tz_image_height_option == 'fix' && $tz_image_height != '') {
                                    echo 'style="height:' . $tz_image_height . 'px"';
                                }
                                ?>>
                                    <?php echo get_the_post_thumbnail($recent["ID"], 'large'); ?>
                                </div>

                            </div>
                            <div class="col-md-6">
                                <h3 class="tz-recent-title"><a
                                            href="<?php echo get_permalink($recent["ID"]); ?>"><?php echo $recent["post_title"]; ?></a>
                                </h3>
                                <?php if ($hide_description == 'show') : ?>
                                    <p class="tz-recent-description">
                                        <?php echo get_the_excerpt($recent["ID"]); ?>
                                    </p>
                                <?php endif; ?>
                                <div class="tz-recent-meta">
                                    <?php if ($tz_hide_date == 'show') : ?>
                                        <span>
                                            <i class="fa fa-calendar"></i>
                                            <?php echo get_the_date($interiart_date_format, $recent["ID"]); ?>
                                        </span>
                                    <?php endif; ?>
                                    <?php if ($hide_comment == 'show') : ?>
                                        <span>
                                            <small>/</small>
                                            <i class="fa fa-comment-o" aria-hidden="true"></i>
                                            <?php
                                            if ($interiart_total_comments > 1) {
                                                echo esc_html($interiart_total_comments) . esc_html__(' Comments', 'tz-interiart');
                                            } else {
                                                echo esc_html($interiart_total_comments) . esc_html__(' Comment', 'tz-interiart');
                                            }
                                            ?>
                                        </span>
                                    <?php endif; ?>

                                </div>

                                <a class="tz-recent-readmore"
                                   href="<?php echo get_permalink($recent["ID"]); ?>"></a>

                            </div>
                        </div>
                        <?php
                    }
                    ?>
                </div>
            </div>
            <?php
            if ($interiart_count % $interiart_column == 0 || $interiart_count == $recent_total) {
                ?>
                </div>
                <?php
            }
            ?>

            <?php
            $interiart_count++;
        }
        ?>
    </div>
    <?php
    return ob_get_clean();
}

add_shortcode('tz-recent-posts', 'tzinteriart_recent_post');
?>