<?php
/*-----------------------------------------------------------------------------------

	Plugin Name: Themient: Recent Posts
	Description: A widget that displays your recent posts.
	Version: 1.0.0

-----------------------------------------------------------------------------------*/

if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly

class elif_recent_posts_widget extends WP_Widget {

	public function __construct() {
		parent::__construct(
	 		'elif_recent_posts_widget',
			__( 'Themient: Recent Posts', 'elif-lite' ),
			array( 'description' => __( 'Display the most recent posts from all categories.', 'elif-lite' ) )
		);
	}

 	public function form( $instance ) {
		$defaults = array(
			'comments' => 1,
			'date' => 1,
			'show_thumb' => 1,
            'display_type'  => 'horizontal',
		);
		$instance = wp_parse_args((array) $instance, $defaults);
		$title = isset( $instance[ 'title' ] ) ? $instance[ 'title' ] : __( 'Recent Posts', 'elif-lite' );
		$qty = isset( $instance[ 'qty' ] ) ? intval( $instance[ 'qty' ] ) : 5;
		$comments = isset( $instance[ 'comments' ] ) ? esc_attr( $instance[ 'comments' ] ) : 1;
		$date = isset( $instance[ 'date' ] ) ? esc_attr( $instance[ 'date' ] ) : 1;
		$show_thumb = isset( $instance[ 'show_thumb' ] ) ? esc_attr( $instance[ 'show_thumb' ] ) : 1;
        $display_type = isset( $instance[ 'display_type' ] ) ? strip_tags( $instance[ 'display_type' ] ) : 'horizontal'; ?>

		<p>
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'elif-lite' ); ?></label> 
			<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
		</p>
		
		<p>
			<label for="<?php echo $this->get_field_id( 'qty' ); ?>"><?php _e( 'Number of Posts to show', 'elif-lite' ); ?></label> 
			<input id="<?php echo $this->get_field_id( 'qty' ); ?>" name="<?php echo $this->get_field_name( 'qty' ); ?>" type="number" min="1" max="10" step="1" value="<?php echo $qty; ?>" />
		</p>

		<p>
			<label for="<?php echo $this->get_field_id("show_thumb"); ?>">
				<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("show_thumb"); ?>" name="<?php echo $this->get_field_name("show_thumb"); ?>" value="1" <?php if (isset($instance['show_thumb'])) { checked( 1, $instance['show_thumb'], true ); } ?> />
				<?php _e( 'Show Thumbnails', 'elif-lite' ); ?>
			</label>
		</p>
		
		<p>
			<label for="<?php echo $this->get_field_id("date"); ?>">
				<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("date"); ?>" name="<?php echo $this->get_field_name("date"); ?>" value="1" <?php checked( 1, $instance['date'], true ); ?> />
				<?php _e( 'Show post date', 'elif-lite' ); ?>
			</label>
		</p>

		<p>
			<label for="<?php echo $this->get_field_id("comments"); ?>">
				<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id("comments"); ?>" name="<?php echo $this->get_field_name("comments"); ?>" value="1" <?php checked( 1, $instance['comments'], true ); ?> />
				<?php _e( 'Show comments number', 'elif-lite' ); ?>
			</label>
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'display_type' ); ?>"><?php _e( 'Display Type', 'elif-lite' ); ?>
                <select class="widefat" id="<?php echo $this->get_field_id( 'display_type' ); ?>" name="<?php echo $this->get_field_name( 'display_type' ); ?>">
                    <option value="horizontal" <?php if ( $instance[ 'display_type' ] == 'horizontal' ) echo 'selected="selected"'; ?>><?php _e( 'Horizontal', 'elif-lite' ); ?></option>
                    <option value="vertical" <?php if ( $instance[ 'display_type' ] == 'vertical' ) echo 'selected="selected"'; ?>><?php _e( 'Vertical', 'elif-lite' ); ?></option>
                </select>
            </label>
		</p>
		<?php 
	}

	public function update( $new_instance, $old_instance ) {
		$instance = array();
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['qty'] = intval( $new_instance['qty'] );
		$instance['comments'] = intval( $new_instance['comments'] );
		$instance['date'] = intval( $new_instance['date'] );
		$instance['show_thumb'] = intval( $new_instance['show_thumb'] );
        $instance['display_type'] = strip_tags( $new_instance[ 'display_type' ] );
		return $instance;
	}

	public function widget( $args, $instance ) {
		extract( $args );
        $title = null; $comments = null; $date = null; $qty = null; $show_thumb = null; $display_type = null;

        if (! empty( $instance['title'] ) ) { $title = apply_filters( 'widget_title', $instance['title'] ); }
        if (! empty( $instance['comments'] ) ) { $comments = $instance['comments']; }
        if (! empty( $instance['date'] ) ) { $date = $instance['date']; }
        if (! empty( $instance['qty'] ) ) { $qty = $instance['qty']; }
        if (! empty( $instance['show_thumb'] ) ) { $show_thumb = $instance['show_thumb']; }
        if (! empty( $instance['display_type'] ) ) { $display_type = $instance['display_type']; }

		$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'Recent Posts', 'elif-lite' );
		
		echo $before_widget;
		if ( ! empty( $title ) ) echo $before_title . $title . $after_title;
		echo self::get_cat_posts( $qty, $comments, $date, $show_thumb, $display_type );
		echo $after_widget;
	}

	public function get_cat_posts( $qty, $comments, $date, $show_thumb, $display_type ) {

		global $post;
		$posts = new WP_Query(
			"orderby=date&order=DESC&posts_per_page=". ($qty)
		);

        if ( $display_type == 'horizontal' ) {
            $class = 'horizontal';
            $thumb_size = 'tiny';
            $thumb_width = '250';
            $thumb_height = '100';
        } else {
            $class = 'vertical';
            $thumb_size = 'square';
            $thumb_width = '100';
            $thumb_height = '100';
        }
        
        if ( $display_type == 'vertical' && $show_thumb == 1 ) {
            $css = 'style="width:66%;"';
        } else {
            $css = 'style="width:100%;"';
        }

		echo '<div class="widget-container recent-posts widget-posts-wrap"><ul>';
		
		while ( $posts->have_posts() ) { $posts->the_post(); ?>
            <li class="post-box <?php echo $class; ?>-container">
				<?php if ( $show_thumb == 1 ) { ?>
                    <div class="post-img">
                        <a href="<?php the_permalink(); ?>" title="<?php the_title_attribute(); ?>">
                            <img width="<?php echo $thumb_width; ?>" height="<?php echo $thumb_height; ?>" src="<?php echo elif_get_thumbnail( $post->ID, $thumb_size, false ); ?>" class="widget-featured wp-post-image" alt="<?php the_title_attribute(); ?>">
                        </a>
                    </div>
				<?php } ?>
				<div class="post-data" <?php echo $css; ?>>
                    <h4 class="title"><a rel="nofollow" href="<?php the_permalink()?>" title="<?php the_title(); ?>"><?php the_title(); ?></a></h4>
                    <?php if ( $date == 1 || $comments == 1 ) { ?>
                        <div class="post-meta">
                            <?php if ( $date == 1 ) { ?>
                                <span class="post-date"><i class="fa fa-clock-o" aria-hidden="true"></i><?php the_time( 'F j, Y' ); ?></span>
                            <?php } ?>
                            <?php if ( $comments == 1 ) { ?>
                                <span class="post-comments"><i class="fa fa-comments" aria-hidden="true"></i><?php comments_number( '0', '1', '%' ); ?></span>
                            <?php } ?>
                        </div>
                    <?php } ?>
				</div>
            </li>
		<?php }

		wp_reset_postdata();
		echo '</ul></div>'."\r\n";
	}
}
add_action( 'widgets_init', create_function( '', 'register_widget( "elif_recent_posts_widget" );' ) );