<?php 

if( !class_exists( 'newsweek_news_widget' ) ) {

	class newsweek_news_widget extends WP_Widget {
		
		public $order_options;
		public $orderby_options;
		public $truncate_titles_options;

		/**
		* Constructor
		*/

		public function __construct() {
			
			$this->order_options = array(
				'asc'  => esc_html__( 'Ascending order', 'newsweek'),
				'desc'  => esc_html__( 'Descending order', 'newsweek'),
			);

			$this->orderby_options = array(
				'date'  => esc_html__( 'Post date', 'newsweek'),
				'title'  => esc_html__( 'Post title', 'newsweek'),
				'rand'  => esc_html__( 'Random order', 'newsweek'),
				'comment_count'  => esc_html__( 'Number of comments', 'newsweek'),
				'id'  => esc_html__( 'Post ID', 'newsweek'),
			);

			$this->truncate_titles_options = array(
				'no'  => esc_html__( 'No', 'newsweek'),
				'yes'  => esc_html__( 'Yes', 'newsweek'),
			);

			parent::__construct(
				'newsweek_news_widget',
				esc_html__( 'Newsweek News Widget', 'newsweek'),
				array(
					'description' => esc_html__( 'Newsweek News Widget', 'newsweek')
				)
			);
		}

		/**
		* Select validation
		*/

		public function select_validation($current, $choises, $default) {
				
			if ( array_key_exists( $current, $choises ) ) {
			
				return $current;
			
			} else {
			
				return $default;
			
			}
			
		}

		/**
		* Form
		*/

		public function form( $instance ) {

			$defaults = array( 
				'title' => esc_html__( 'Recent posts', 'newsweek'),
				'items' => 4,
				'order' => 'asc',
				'orderby' => 'date',
				'category' => false,
				'truncate_titles' => 'no',
			);

			$order = $this->order_options;
			$orderby = $this->orderby_options;
			$truncate_titles = $this->truncate_titles_options;

			$instance = wp_parse_args( (array) $instance, $defaults ); 
		
		?>
			
			<p>
				<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('Title','newsweek'); ?></label>
				<input class="widefat" type="text" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title'));?>" value="<?php echo esc_attr($instance['title']); ?>" />
			</p>  

			<p>

				<label for="<?php echo esc_attr($this->get_field_id( 'truncate_titles' )); ?>"><?php esc_html_e('Truncate post titles','newsweek'); ?></label>
				<select class="widefat" id="<?php echo esc_attr($this->get_field_id('truncate_titles'));?>" name="<?php echo esc_attr($this->get_field_name('truncate_titles')); ?>">
					<?php foreach ($truncate_titles as $k => $v) { ?>
						<option value="<?php echo esc_attr( $k ); ?>" <?php selected( $instance['truncate_titles'], $k ); ?>><?php echo esc_html($v); ?></option>
					<?php } ?>
				</select>

			</p>

			<p>
				
				<label for="<?php echo esc_attr($this->get_field_id('items')); ?>"> <?php esc_html_e('Num of items (Set -1 to show all items)','newsweek'); ?>: </label>
				<input class="widefat" type="number" id="<?php echo esc_attr($this->get_field_id('items')); ?>" name="<?php echo esc_attr($this->get_field_name('items'));?>" step="1" min="-1" value="<?php echo esc_attr($instance['items']); ?>" />
				
			</p> 

			<p>
				
				<label for="<?php echo esc_attr($this->get_field_id( 'category' )); ?>"> <?php esc_html_e('Category','newsweek'); ?></label>
				<select class="widefat" id="<?php echo esc_attr($this->get_field_id('category'));?>" name="<?php echo esc_attr($this->get_field_name('category')); ?>">
					<option><?php esc_html_e('All categories','newsweek'); ?></option>
					<?php foreach (get_terms('category') as $category) { ?>
						<option value="<?php echo absint( $category->term_id ); ?>" <?php selected($instance['category'], $category->term_id ); ?>><?php echo esc_html($category->name); ?></option>
					<?php } ?>
				</select>

			</p>

			<p>
			
				<label for="<?php echo esc_attr($this->get_field_id( 'order' )); ?>"><?php esc_html_e('Order','newsweek'); ?></label>
				<select class="widefat" id="<?php echo esc_attr($this->get_field_id('order'));?>" name="<?php echo esc_attr($this->get_field_name('order')); ?>">
					<?php foreach ($order as $k => $v) { ?>
						<option value="<?php echo esc_attr($k); ?>" <?php selected( $instance['order'], $k ); ?>><?php echo esc_html($v); ?></option>
					<?php } ?>
				</select>

			</p>

			<p>

				<label for="<?php echo esc_attr($this->get_field_id( 'orderby' )); ?>"><?php esc_html_e('Order by','newsweek'); ?></label>
				<select class="widefat" id="<?php echo esc_attr($this->get_field_id('orderby'));?>" name="<?php echo esc_attr($this->get_field_name('orderby')); ?>">
					<?php foreach ($orderby as $k => $v) { ?>
						<option value="<?php echo esc_attr( $k ); ?>" <?php selected( $instance['orderby'], $k ); ?>><?php echo esc_html($v); ?></option>
					<?php } ?>
				</select>

			</p>

			<?php
			
		}

		/**
		* Update
		*/

		public function update( $new_instance, $old_instance ) {
			
			$instance = $old_instance;
			
			$instance['title'] = sanitize_text_field( $new_instance['title'] );
			$instance['items'] = ( !empty( $new_instance['items'] ) ) ? (int) $new_instance['items'] : 4;
			$instance['category'] = (int) $new_instance['category'];
			$instance['order'] =  $this->select_validation($new_instance['order'], $this->order_options, 'asc');
			$instance['orderby'] =  $this->select_validation($new_instance['orderby'], $this->orderby_options, 'date');
			$instance['truncate_titles'] =  $this->select_validation($new_instance['truncate_titles'], $this->truncate_titles_options, 'no');

			return $instance;
			
		}   

		/**
		* Widget
		*/

		public function widget( $args, $instance ) {
			
			extract($args);
			
			$title = apply_filters('widget_title', $instance['title'] );
			$ppp = (!empty( $instance['items'] ) ) ? absint($instance['items']) : 4;

			echo $args['before_widget'];

			$news_args = array(
				'post_type' => 'post',
				'posts_per_page' => $ppp,
				'order' => $this->select_validation($instance['order'], $this->order_options, 'asc'),
				'orderby' => $this->select_validation($instance['orderby'], $this->orderby_options, 'date'),
			);
		
			if ( is_numeric($instance['category']) ) :
				$news_args['cat'] = $instance['category'];
			endif;

			$query = new WP_Query($news_args);
			
			if ( !empty($title) ) {
				echo $args['before_title'] . esc_html($title) . $args['after_title'];
			}

			$widget_classes = '';

			if ( isset($instance['truncate_titles']) && $instance['truncate_titles'] === 'yes' ) {
				$widget_classes = ' newsweek-news-widget-truncate-post-titles';
			}
		
			$widget_class_attr = !empty($widget_classes) ? 'class="' . esc_attr($widget_classes) . '"' : '';
		
		?>

			<div id="<?php echo esc_attr($args['widget_id']); ?>" <?php echo $widget_class_attr; ?>>

				<?php
			
					while ( $query->have_posts() ) : $query->the_post(); 
			
						global $post;
			
						if ( has_post_thumbnail($post->ID) ) :
							
							$thumbnail = get_the_post_thumbnail($post->ID, 'newsweek_post_blocks_small');
										
						else:
										
							$thumbnail_img = get_template_directory_uri() . '/assets/images/placeholders/post-blocks/placeholder-120x100.jpg';
							$thumbnail = '<img src="' . esc_url($thumbnail_img) . '" alt="' . esc_attr(get_the_title($post->ID)) . '">';
															
						endif;

				?>
				
						
							<div class="small_article">

								<div class="article_featured_img">

									<a class="standard-post-permalink" href="<?php echo esc_url(get_permalink()); ?>">

										<?php echo $thumbnail; ?>

									</a>

								</div>
								
								<div class="article_details" >
									
									<a href="<?php echo esc_url(get_permalink($post->ID)); ?>">
										<h3><?php echo esc_html(get_the_title()); ?></h3>
									</a>
									
									<div class="meta-info">
										<?php echo esc_html(get_the_date(false, $post->ID));?>	
									</div>

								</div>

								<div class="clear"></div>
								
							</div>
							
				
				<?php

					endwhile; 
					wp_reset_postdata();

				?>

				
			</div>
							
			<?php

			echo $args['after_widget'];
			
		}
		
	}

}

?>