<?php
/**
 * Kopa Widget Single Post
 *
 * @author 		Kopatheme
 * @category 	Widgets
 * @package 	KopaFramework/Widgets
 * @since    	1.0.0
 * @extends 	Kopa_Widget
 */

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

class Kopa_Widget_Single_Post extends Kopa_Widget {
	/**
	 * Constructor
	 */
	public function __construct() {

        //Tags Array
        $tags = get_tags();
        $arr_tags = array(
            '' => __('----SELECT----', 'kopa-framework')
            );
        foreach ($tags as $category) 
            $arr_tags[$category->term_id] = $category->name.' ('.$category->count.')';

        //Categories Array
        $categories = get_categories();
        $arr_cats = array(
            '' => __('----SELECT----', 'kopa-framework')
            );
        foreach ($categories as $category)
            $arr_cats[$category->term_id] = $category->name.' ('.$category->count.')';

		$this->widget_cssclass    = 'kopa-widget-single-post';
		$this->widget_description = __( 'Display single post.', 'newmoment' );
		$this->widget_id          = 'kopa-widget-single-post';
		$this->widget_name        = __( 'Kopa Widget Single Post', 'newmoment' );
		// $this->widget_width       = 400;
		$this->settings           = array(
			'title'  => array(
				'type'  => 'text',
				'std'   => __( 'Kopa Widget Single Post', 'newmoment' ),
				'label' => __( 'Title:', 'newmoment' )
			),
            'categories' => array(
                'type'    => 'multiselect',
                'std'     => null,
                'label'   => __( 'Categories:', 'newmoment' ),
                'options' => $arr_cats,
            ),
            'relation' => array(
                'type'    => 'select',
                'label'   => __( 'Relation:', 'newmoment' ),
                'options' => array(
                    'OR'  => __( 'OR', 'newmoment' ),
                    'AND' => __( 'AND', 'newmoment' ),
                )
            ),
            'tags' => array(
                'type'    => 'multiselect',
                'std'     => null,
                'label'   => __( 'Tags:', 'newmoment' ),
                'options' => $arr_tags,
            ),
            'orderby' => array(
                'type'    => 'select',
                'std'     => '',
                'label'   => __( 'Orderby:', 'newmoment' ),
                'options' => array(
                    'latest'       => __('Latest', 'newmoment'),
                    'popular'      => __('Popular by view count', 'newmoment'),
                    'most_comment' => __('Popular by comment count', 'newmoment'),
                    'random'       => __('Random', 'newmoment')
                )
            ),
            'timestamp' => array(
                'type'  => 'select',
                'std'   => '',
                'label' => __( 'Timestamp (ago):', 'newmoment' ),
                'options' => array(
                    ''          => __('-- Select --', 'newmoment'),
                    '-1 week'   => __('1 week', 'newmoment'),
                    '-2 week'   => __('2 weeks', 'newmoment'),
                    '-3 week'   => __('3 weeks', 'newmoment'),
                    '-1 month'  => __('1 months', 'newmoment'),
                    '-2 month'  => __('2 months', 'newmoment'),
                    '-3 month'  => __('3 months', 'newmoment'),
                    '-4 month'  => __('4 months', 'newmoment'),
                    '-5 month'  => __('5 months', 'newmoment'),
                    '-6 month'  => __('6 months', 'newmoment'),
                    '-7 month'  => __('7 months', 'newmoment'),
                    '-8 month'  => __('8 months', 'newmoment'),
                    '-9 month'  => __('9 months', 'newmoment'),
                    '-10 month' => __('10 months', 'newmoment'),
                    '-11 month' => __('11 months', 'newmoment'),
                    '-1 year'   => __('1 year', 'newmoment'),
                    '-2 year'   => __('2 years', 'newmoment'),
                    '-3 year'   => __('3 years', 'newmoment'),
                    '-4 year'   => __('4 years', 'newmoment'),
                    '-5 year'   => __('5 years', 'newmoment'),
                    '-6 year'   => __('6 years', 'newmoment'),
                    '-7 year'   => __('7 years', 'newmoment'),
                    '-8 year'   => __('8 years', 'newmoment'),
                    '-9 year'   => __('9 years', 'newmoment'),
                    '-10 year'  => __('10 years', 'newmoment'),
                )
            ),
			'posts_per_page' => array(
				'type'  => 'number',
				'step'  => 1,
				'min'   => 1,
				'max'   => '',
				'std'   => 5,
				'label' => __( 'Number of items:', 'newmoment' )
			),
            'limit' => array(
                'type'  => 'number',
                'step'  => 1,
                'min'   => 1,
                'max'   => '',
                'std'   => 5,
                'label' => __( 'Custom post excerpt length:', 'newmoment' )
            ),
		);
		parent::__construct();
	}

	/**
	 * widget function.
	 *
	 * @see WP_Widget
	 * @access public
	 * @param array $args
	 * @param array $instance
	 * @return void
	 */
	public function widget( $args, $instance ) {

		if ( $this->get_cached_widget( $args ) )
			return;

		ob_start();
		extract( $args );

		$title       = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );

        $query_args['categories'] = $instance['categories'];
        $query_args['relation'] = esc_attr($instance['relation']);
        $query_args['tags'] = $instance['tags'];
        $query_args['posts_per_page'] = (int) $instance['posts_per_page'];

        if (isset($instance['timestamp'])){
            $query_args['date_query'] = $instance['timestamp'];
        }

        $query_args['orderby'] = $instance['orderby'];
        $limit = $instance['limit'];
        $number_of_post = (int) $instance['posts_per_page'];

        $status = get_theme_mod( 'kopa_status_default_thumbnail', true );
        if($status == 'enable') {
            $status = true;
        }else {
            $status = false;
        }

        $posts = kopa_widget_posttype_build_query($query_args);
        ?>
            <div class="widget kopa-article-list-1-widget">
                <h3 class="widget-title">
                    <span class="text-title"><?php echo $title; ?></span>
                    <span class="left-corner"></span>
                    <span class="right-corner"></span>
                </h3>
                <?php $count = 0; $next = true; ?>
                <?php while($posts->have_posts()) : $posts->the_post(); ?>
                <?php if($count == 0) { ?>
                <article class="last-item">
                    <?php if(has_post_thumbnail() || $status == true) : ?>
                        <div class="entry-thumb">
                            <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('kopa-size-11'); ?></a>
                            <div class="meta-box">
                                <?php 
                                if(comments_open()){
                                    comments_popup_link( '<i class="fa fa-comments-o"></i><span>0</span>', '<i class="fa fa-comments-o"></i><span>1</span>', '<i class="fa fa-comments-o"></i><span>%</span>', 'entry-comments' ); 
                                }
                                ?>
                                <span class="entry-view"><i class="fa fa-eye"></i><span><?php echo kopa_get_view_count(get_the_id()); ?></span></span>
                            </div>
                        </div>
                    <?php endif; ?>
                    <div class="entry-content">
                        <h5 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>
                        <?php global $post; ?>
                        <p><?php echo kopa_get_the_excerpt_for_widget(get_the_excerpt(), get_the_content(), $limit); ?></p>
                        <footer>
                            <span class="entry-time"><i class="fa fa-clock-o"></i><span><?php echo get_post_time('g:i A') ;?></span></span>
                            <span class="entry-meta">|</span>
                            <span class="entry-date"><i class="fa fa-calendar"></i><span><?php echo get_the_date(); ?></span></span>
                        </footer>
                    </div>
                </article>
                <?php } else {
                    if($next) {
                        echo '<ul class="older-post">';
                        $next = false;
                    }
                ?>
                    <li>
                        <article class="entry-item clearfix">
                            <?php if(has_post_thumbnail() || $status == true) : ?>
                            <div class="entry-thumb">
                                <a href="<?php the_permalink(); ?>"><?php the_post_thumbnail('kopa-size-10'); ?></a>
                            </div>
                        <?php endif; ?>
                            <div class="entry-content">
                                <h5 class="entry-title"><a href="<?php the_permalink(); ?>"><?php the_title(); ?></a></h5>
                                <footer>
                                    <span class="entry-time"><i class="fa fa-clock-o"></i><span><?php echo get_post_time('g:i A') ;?></span></span>
                                    <span class="entry-meta">|</span>
                                    <span class="entry-date"><i class="fa fa-calendar"></i><span><?php echo get_the_date(); ?></span></span>
                                </footer>
                            </div>
                        </article>
                    </li>
                <?php if($count == $number_of_post) {
                    echo '</ul>';
                    }
                }
                $count++; 
                ?>
                <?php endwhile; ?>
            </div>
            <!-- kopa-article-list-1-widget -->
		<?php
		wp_reset_postdata();

        $content = ob_get_clean();

        echo $content;

        $this->cache_widget( $args, $content );
	}
}

register_widget( 'Kopa_Widget_Single_Post' );