
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
<?php

add_action( 'widgets_init', 'presso_widgets_init_posts' );
if ( ! function_exists( 'presso_widgets_init_posts' ) ) {
	function presso_widgets_init_posts() {
		register_widget( 'Presso_widget_posts' );
	}
}

if ( ! class_exists( 'Presso_widget_posts' ) ) {
	class Presso_widget_posts extends WP_Widget {
		private $default = array(
			'supertitle' => '',
			'title' => '',
			'subtitle' => '',
			'cat_ids' => array( 0 ),
			'lead_layout' => 'none',
			'lead_layout_count' => '0',
			'layout' => 'small-1',
			'count' => '5',
			'order' => 'latest',
			'avoid_duplicated_posts' => 0,
		);

		public function __construct() {
			// widget actual processes
			parent::__construct(
		 		'vw_widget_posts', // Base ID
				PRESSO_THEME_NAME.': Posts', // Name
				array( 'description' => esc_html__( 'Display posts', 'presso' ) ) // Args
			);
		}

		function widget( $args, $instance ) {
			extract($args);

			$instance['supertitle'] = apply_filters( 'wpml_translate_single_string', $instance['supertitle'], 'Widgets', $this->id.'_supertitle' );
			$instance['title'] = apply_filters( 'widget_title', $instance['title'], $instance, $this->id_base );
			$instance['subtitle'] = apply_filters( 'wpml_translate_single_string', $instance['subtitle'], 'Widgets', $this->id.'_subtitle' );

			$supertitle_html = '';
			if ( ! empty( $instance['supertitle'] ) ) {
				$supertitle_html = '<span class="vw-super-title">'.wp_kses_post( $instance['supertitle'] ).'</span>';
			}

			$title_html = '';
			if ( ! empty( $instance['title'] ) ) {
				$title_html = wp_kses_post( $instance['title'] );
			}

			$subtitle_html = '';
			if ( ! empty( $instance['subtitle'] ) ) {
				$subtitle_html = '<span class="vwspc-section-subtitle">'.wp_kses_post( $instance['subtitle'] ).'</span>';
			}

			$avoid_duplicated_posts = ! empty( $instance['avoid_duplicated_posts'] ) ? '1' : '0';

			/**
			 * Begin render widget
			 */
			
			echo wp_kses_post( $before_widget );
			if ( $instance['title'] ) echo wp_kses_post( $supertitle_html . $before_title . $title_html . $after_title . $subtitle_html );

			global $post;

			$query_args = array(
				'post_order' => $instance['order'],
				'posts_per_page' => $instance['count'],
				'lead_layout' => $instance['lead_layout'],
				'lead_layout_number' => $instance['lead_layout_count'],
				'layout' => $instance['layout'],
				'cat' => $instance['cat_ids'],
			);

			if ( $avoid_duplicated_posts ) { presso_enable_avoid_duplicate_post(); }
			presso_the_post_loop( apply_filters( 'presso_filter_widget_posts_query', $query_args, $args['id'] ) );
			if ( $avoid_duplicated_posts ) { presso_disable_avoid_duplicate_post(); }

			echo wp_kses_post( $after_widget );
		}

		function update( $new_instance, $old_instance ) {
			$instance = $old_instance;
			$instance['avoid_duplicated_posts'] = $new_instance['avoid_duplicated_posts'] ? 1 : 0; // don't change order of line, it's bug with default value replacement.

			$new_instance = wp_parse_args( (array) $new_instance, $this->default );
			$instance['supertitle'] = sanitize_text_field( $new_instance['supertitle'] );
			$instance['title'] = sanitize_text_field( $new_instance['title'] );
			$instance['subtitle'] = sanitize_text_field( $new_instance['subtitle'] );
			$instance['cat_ids'] = wp_kses_data( $new_instance['cat_ids'] );
			$instance['lead_layout'] = sanitize_text_field( $new_instance['lead_layout'] );
			$instance['lead_layout_count'] = intval( $new_instance['lead_layout_count'] );
			$instance['layout'] = sanitize_text_field( $new_instance['layout'] );
			$instance['order'] = sanitize_text_field( $new_instance['order'] );
			$instance['count'] = intval( $new_instance['count'] );

			do_action( 'wpml_register_single_string', 'Widgets', $this->id.'_supertitle', $instance['supertitle'] );
			do_action( 'wpml_register_single_string', 'Widgets', $this->id.'_title', $instance['title'] );
			do_action( 'wpml_register_single_string', 'Widgets', $this->id.'_subtitle', $instance['subtitle'] );

			return $instance;
		}

		function form( $instance ) {
			$instance = wp_parse_args( (array) $instance, $this->default );

			$supertitle = $instance['supertitle'];
			$title = $instance['title'];
			$subtitle = $instance['subtitle'];
			$cat_ids = $instance['cat_ids'];
			$lead_layout = $instance['lead_layout'];
			$lead_layout_count = $instance['lead_layout_count'];
			$layout = $instance['layout'];
			$order = $instance['order'];
			$count = $instance['count'];
			$avoid_duplicated_posts = $instance['avoid_duplicated_posts'] ? 'checked="checked"' : '';
			?>

			<!-- supertitle -->
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('supertitle') ); ?>"><?php esc_html_e( 'Super-title:', 'presso' ); ?></label>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id('supertitle') ); ?>" name="<?php echo esc_attr( $this->get_field_name('supertitle') ); ?>" type="text" value="<?php echo esc_attr($supertitle); ?>" />
			</p>

			<!-- title -->
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('title') ); ?>"><?php esc_html_e( 'Title:', 'presso' ); ?></label>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id('title') ); ?>" name="<?php echo esc_attr( $this->get_field_name('title') ); ?>" type="text" value="<?php echo esc_attr($title); ?>" />
			</p>

			<!-- subtitle -->
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('subtitle') ); ?>"><?php esc_html_e( 'Subtitle:', 'presso' ); ?></label>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id('subtitle') ); ?>" name="<?php echo esc_attr( $this->get_field_name('subtitle') ); ?>" type="text" value="<?php echo esc_attr($subtitle); ?>" />
			</p>

			<!-- categories -->
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('cat_ids') ); ?>"><?php esc_html_e( 'Categories:', 'presso' ); ?></label>
				<select class="widefat" multiple="multiple" id="<?php echo esc_attr( $this->get_field_id( 'cat_ids' ) ); ?>[]" name="<?php echo esc_attr( $this->get_field_name( 'cat_ids' ) ); ?>[]">
					<option value="0" <?php if ( in_array( 0, $cat_ids ) ) { echo ' selected="selected"' ; } ?>>(All)</option>
					<?php $categories = get_categories();
					foreach ( $categories as $category ): ?>
					<option value="<?php echo esc_attr( $category->cat_ID ); ?>" <?php if ( in_array( $category->cat_ID , $cat_ids ) ) { echo ' selected="selected"' ; } ?>><?php echo esc_html( $category->cat_name ); ?></option>
					<?php endforeach; ?>
				</select>
			</p>

			<!-- lead layout -->
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('lead_layout') ); ?>"><?php esc_html_e( 'Lead Layout:', 'presso' ); ?></label>
				<select class="widefat" id="<?php echo esc_attr( $this->get_field_id('lead_layout') ); ?>" name="<?php echo esc_attr( $this->get_field_name('lead_layout') ); ?>">
					<option value="none" <?php selected( $lead_layout, 'none' ); ?>><?php esc_html_e( 'Disabled', 'presso' ); ?></option>
					<option value="block-a-1" <?php selected( $lead_layout, 'block-a-1' ); ?>><?php esc_html_e( 'Block A-1', 'presso' ); ?></option>
					<option value="block-a-2" <?php selected( $lead_layout, 'block-a-2' ); ?>><?php esc_html_e( 'Block A-2', 'presso' ); ?></option>
					<option value="block-a-3" <?php selected( $lead_layout, 'block-a-3' ); ?>><?php esc_html_e( 'Block A-3', 'presso' ); ?></option>
					<option value="block-b-1" <?php selected( $lead_layout, 'block-b-1' ); ?>><?php esc_html_e( 'Block B-1', 'presso' ); ?></option>
					<option value="block-b-2" <?php selected( $lead_layout, 'block-b-2' ); ?>><?php esc_html_e( 'Block B-2', 'presso' ); ?></option>
					<option value="block-b-3" <?php selected( $lead_layout, 'block-b-3' ); ?>><?php esc_html_e( 'Block B-3', 'presso' ); ?></option>
					<option value="block-c-1" <?php selected( $lead_layout, 'block-c-1' ); ?>><?php esc_html_e( 'Block C-1', 'presso' ); ?></option>
					<option value="block-c-2" <?php selected( $lead_layout, 'block-c-2' ); ?>><?php esc_html_e( 'Block C-2', 'presso' ); ?></option>
					<option value="block-c-3" <?php selected( $lead_layout, 'block-c-3' ); ?>><?php esc_html_e( 'Block C-3', 'presso' ); ?></option>
					<option value="block-c-4" <?php selected( $lead_layout, 'block-c-4' ); ?>><?php esc_html_e( 'Block C-4', 'presso' ); ?></option>
					<option value="block-d-1" <?php selected( $lead_layout, 'block-d-1' ); ?>><?php esc_html_e( 'Block D-1', 'presso' ); ?></option>
					<option value="block-d-2" <?php selected( $lead_layout, 'block-d-2' ); ?>><?php esc_html_e( 'Block D-2', 'presso' ); ?></option>
					<option value="block-d-3" <?php selected( $lead_layout, 'block-d-3' ); ?>><?php esc_html_e( 'Block D-3', 'presso' ); ?></option>
					<option value="block-d-4" <?php selected( $lead_layout, 'block-d-4' ); ?>><?php esc_html_e( 'Block D-4', 'presso' ); ?></option>
					<option value="block-e-1" <?php selected( $lead_layout, 'block-e-1' ); ?>><?php esc_html_e( 'Block E-1', 'presso' ); ?></option>
					<option value="block-e-2" <?php selected( $lead_layout, 'block-e-2' ); ?>><?php esc_html_e( 'Block E-2', 'presso' ); ?></option>
					<option value="block-e-3" <?php selected( $lead_layout, 'block-e-3' ); ?>><?php esc_html_e( 'Block E-3', 'presso' ); ?></option>
					<option value="block-f-1" <?php selected( $lead_layout, 'block-f-1' ); ?>><?php esc_html_e( 'Block F-1', 'presso' ); ?></option>
					<option value="block-f-2" <?php selected( $lead_layout, 'block-f-2' ); ?>><?php esc_html_e( 'Block F-2', 'presso' ); ?></option>
					<option value="block-f-3" <?php selected( $lead_layout, 'block-f-3' ); ?>><?php esc_html_e( 'Block F-3', 'presso' ); ?></option>
					<option value="block-g-1" <?php selected( $lead_layout, 'block-g-1' ); ?>><?php esc_html_e( 'Block G-1', 'presso' ); ?></option>
					<option value="block-g-2" <?php selected( $lead_layout, 'block-g-2' ); ?>><?php esc_html_e( 'Block G-2', 'presso' ); ?></option>
					<option value="block-g-3" <?php selected( $lead_layout, 'block-g-3' ); ?>><?php esc_html_e( 'Block G-3', 'presso' ); ?></option>
					<option value="block-g-4" <?php selected( $lead_layout, 'block-g-4' ); ?>><?php esc_html_e( 'Block G-5', 'presso' ); ?></option>
					<option value="block-h-1" <?php selected( $lead_layout, 'block-h-1' ); ?>><?php esc_html_e( 'Block H-1', 'presso' ); ?></option>
					<option value="block-h-2" <?php selected( $lead_layout, 'block-h-2' ); ?>><?php esc_html_e( 'Block H-2', 'presso' ); ?></option>
					<option value="block-i-1" <?php selected( $lead_layout, 'block-i-1' ); ?>><?php esc_html_e( 'Block I-1', 'presso' ); ?></option>
					<option value="block-i-2" <?php selected( $lead_layout, 'block-i-2' ); ?>><?php esc_html_e( 'Block I-2', 'presso' ); ?></option>
					<option value="block-i-3" <?php selected( $lead_layout, 'block-i-3' ); ?>><?php esc_html_e( 'Block I-3', 'presso' ); ?></option>
					<option value="block-i-4" <?php selected( $lead_layout, 'block-i-4' ); ?>><?php esc_html_e( 'Block I-4', 'presso' ); ?></option>
					<option value="custom-1" <?php selected( $lead_layout, 'custom-1' ); ?>><?php esc_html_e( 'Custom 1', 'presso' ); ?></option>
					<option value="custom-2" <?php selected( $lead_layout, 'custom-2' ); ?>><?php esc_html_e( 'Custom 2', 'presso' ); ?></option>
					<option value="custom-3" <?php selected( $lead_layout, 'custom-3' ); ?>><?php esc_html_e( 'Custom 3', 'presso' ); ?></option>
					<option value="custom-4" <?php selected( $lead_layout, 'custom-4' ); ?>><?php esc_html_e( 'Custom 4', 'presso' ); ?></option>
				</select>
			</p>

			<!-- lead layout count -->
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('lead_layout_count') ); ?>"><?php esc_html_e( 'Number of lead posts:', 'presso' ); ?></label>
				<input id="<?php echo esc_attr( $this->get_field_id('lead_layout_count') ); ?>" name="<?php echo esc_attr( $this->get_field_name('lead_layout_count') ); ?>" type="text" value="<?php echo esc_attr( $lead_layout_count ); ?>" size="3">
			</p>

			<!-- layout -->
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('layout') ); ?>"><?php esc_html_e( 'Layout:', 'presso' ); ?></label>
				<select class="widefat" id="<?php echo esc_attr( $this->get_field_id('layout') ); ?>" name="<?php echo esc_attr( $this->get_field_name('layout') ); ?>">
					<option value="block-a-1" <?php selected( $layout, 'block-a-1' ); ?>><?php esc_html_e( 'Block A-1', 'presso' ); ?></option>
					<option value="block-a-2" <?php selected( $layout, 'block-a-2' ); ?>><?php esc_html_e( 'Block A-2', 'presso' ); ?></option>
					<option value="block-a-3" <?php selected( $layout, 'block-a-3' ); ?>><?php esc_html_e( 'Block A-3', 'presso' ); ?></option>
					<option value="block-b-1" <?php selected( $layout, 'block-b-1' ); ?>><?php esc_html_e( 'Block B-1', 'presso' ); ?></option>
					<option value="block-b-2" <?php selected( $layout, 'block-b-2' ); ?>><?php esc_html_e( 'Block B-2', 'presso' ); ?></option>
					<option value="block-b-3" <?php selected( $layout, 'block-b-3' ); ?>><?php esc_html_e( 'Block B-3', 'presso' ); ?></option>
					<option value="block-c-1" <?php selected( $layout, 'block-c-1' ); ?>><?php esc_html_e( 'Block C-1', 'presso' ); ?></option>
					<option value="block-c-2" <?php selected( $layout, 'block-c-2' ); ?>><?php esc_html_e( 'Block C-2', 'presso' ); ?></option>
					<option value="block-c-3" <?php selected( $layout, 'block-c-3' ); ?>><?php esc_html_e( 'Block C-3', 'presso' ); ?></option>
					<option value="block-c-4" <?php selected( $layout, 'block-c-4' ); ?>><?php esc_html_e( 'Block C-4', 'presso' ); ?></option>
					<option value="block-d-1" <?php selected( $layout, 'block-d-1' ); ?>><?php esc_html_e( 'Block D-1', 'presso' ); ?></option>
					<option value="block-d-2" <?php selected( $layout, 'block-d-2' ); ?>><?php esc_html_e( 'Block D-2', 'presso' ); ?></option>
					<option value="block-d-3" <?php selected( $layout, 'block-d-3' ); ?>><?php esc_html_e( 'Block D-3', 'presso' ); ?></option>
					<option value="block-d-4" <?php selected( $layout, 'block-d-4' ); ?>><?php esc_html_e( 'Block D-4', 'presso' ); ?></option>
					<option value="block-e-1" <?php selected( $layout, 'block-e-1' ); ?>><?php esc_html_e( 'Block E-1', 'presso' ); ?></option>
					<option value="block-e-2" <?php selected( $layout, 'block-e-2' ); ?>><?php esc_html_e( 'Block E-2', 'presso' ); ?></option>
					<option value="block-e-3" <?php selected( $layout, 'block-e-3' ); ?>><?php esc_html_e( 'Block E-3', 'presso' ); ?></option>
					<option value="block-f-1" <?php selected( $layout, 'block-f-1' ); ?>><?php esc_html_e( 'Block F-1', 'presso' ); ?></option>
					<option value="block-f-2" <?php selected( $layout, 'block-f-2' ); ?>><?php esc_html_e( 'Block F-2', 'presso' ); ?></option>
					<option value="block-f-3" <?php selected( $layout, 'block-f-3' ); ?>><?php esc_html_e( 'Block F-3', 'presso' ); ?></option>
					<option value="block-g-1" <?php selected( $layout, 'block-g-1' ); ?>><?php esc_html_e( 'Block G-1', 'presso' ); ?></option>
					<option value="block-g-2" <?php selected( $layout, 'block-g-2' ); ?>><?php esc_html_e( 'Block G-2', 'presso' ); ?></option>
					<option value="block-g-3" <?php selected( $layout, 'block-g-3' ); ?>><?php esc_html_e( 'Block G-3', 'presso' ); ?></option>
					<option value="block-g-4" <?php selected( $layout, 'block-g-4' ); ?>><?php esc_html_e( 'Block G-4', 'presso' ); ?></option>
					<option value="block-g-5" <?php selected( $layout, 'block-g-4' ); ?>><?php esc_html_e( 'Block G-5', 'presso' ); ?></option>
					<option value="block-h-1" <?php selected( $layout, 'block-h-1' ); ?>><?php esc_html_e( 'Block H-1', 'presso' ); ?></option>
					<option value="block-h-2" <?php selected( $layout, 'block-h-2' ); ?>><?php esc_html_e( 'Block H-2', 'presso' ); ?></option>
					<option value="block-h-3" <?php selected( $layout, 'block-h-3' ); ?>><?php esc_html_e( 'Block H-3', 'presso' ); ?></option>
					<option value="block-h-4" <?php selected( $layout, 'block-h-4' ); ?>><?php esc_html_e( 'Block H-4', 'presso' ); ?></option>
					<option value="block-h-5" <?php selected( $layout, 'block-h-5' ); ?>><?php esc_html_e( 'Block H-5', 'presso' ); ?></option>
					<option value="block-i-1" <?php selected( $layout, 'block-i-1' ); ?>><?php esc_html_e( 'Block I-1', 'presso' ); ?></option>
					<option value="block-i-2" <?php selected( $layout, 'block-i-2' ); ?>><?php esc_html_e( 'Block I-2', 'presso' ); ?></option>
					<option value="block-i-3" <?php selected( $layout, 'block-i-3' ); ?>><?php esc_html_e( 'Block I-3', 'presso' ); ?></option>
					<option value="block-i-4" <?php selected( $layout, 'block-i-4' ); ?>><?php esc_html_e( 'Block I-4', 'presso' ); ?></option>
					<option value="custom-1" <?php selected( $layout, 'custom-1' ); ?>><?php esc_html_e( 'Custom 1', 'presso' ); ?></option>
					<option value="custom-2" <?php selected( $layout, 'custom-2' ); ?>><?php esc_html_e( 'Custom 2', 'presso' ); ?></option>
					<option value="custom-3" <?php selected( $layout, 'custom-3' ); ?>><?php esc_html_e( 'Custom 3', 'presso' ); ?></option>
					<option value="custom-4" <?php selected( $layout, 'custom-4' ); ?>><?php esc_html_e( 'Custom 4', 'presso' ); ?></option>
				</select>
			</p>

			<!-- count -->
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('count') ); ?>"><?php esc_html_e( 'Number of posts to show:', 'presso' ); ?></label>
				<input id="<?php echo esc_attr( $this->get_field_id('count') ); ?>" name="<?php echo esc_attr( $this->get_field_name('count') ); ?>" type="text" value="<?php echo esc_attr( $count ); ?>" size="3">
			</p>

			<!-- order -->
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('order') ); ?>"><?php esc_html_e( 'Post Order:', 'presso' ); ?></label>
				<select class="widefat" id="<?php echo esc_attr( $this->get_field_id('order') ); ?>" name="<?php echo esc_attr( $this->get_field_name('order') ); ?>">
					<option value="latest" <?php selected( $order, 'latest' ); ?>><?php esc_html_e( 'Latest Posts', 'presso' ); ?></option>
					<option value="latest_gallery" <?php selected( $order, 'latest_gallery' ); ?>><?php esc_html_e( 'Latest Gallery Posts', 'presso' ); ?></option>
					<option value="latest_video" <?php selected( $order, 'latest_video' ); ?>><?php esc_html_e( 'Latest Video Posts', 'presso' ); ?></option>
					<option value="latest_audio" <?php selected( $order, 'latest_audio' ); ?>><?php esc_html_e( 'Latest Audio Posts', 'presso' ); ?></option>
					<option value="featured" <?php selected( $order, 'featured' ); ?>><?php esc_html_e( 'Latest Featured Posts', 'presso' ); ?></option>
					<option value="latest_reviews" <?php selected( $order, 'latest_reviews' ); ?>><?php esc_html_e( 'Latest Reviews', 'presso' ); ?></option>
					<option value="most_review_score" <?php selected( $order, 'most_review_score' ); ?>><?php esc_html_e( 'Most Review Score', 'presso' ); ?></option>
					<option value="most_viewed" <?php selected( $order, 'most_viewed' ); ?>><?php esc_html_e( 'Most Viewed', 'presso' ); ?></option>
					<option value="most_commented" <?php selected( $order, 'most_commented' ); ?>><?php esc_html_e( 'Most Commented', 'presso' ); ?></option>
					<option value="daily_trending_viewed" <?php selected( $order, 'daily_trending_viewed' ); ?>><?php esc_html_e( 'Daily Trending Views', 'presso' ); ?></option>
					<option value="weekly_trending_viewed" <?php selected( $order, 'weekly_trending_viewed' ); ?>><?php esc_html_e( 'Weekly Trending Views', 'presso' ); ?></option>
					<option value="monthly_trending_viewed " <?php selected( $order, 'monthly_trending_viewed ' ); ?>><?php esc_html_e( 'Monthly Trending Views', 'presso' ); ?></option>
				</select>
			</p>

			<!-- Avoid Duplicated Posts -->
			<p>
				<input class="checkbox" type="checkbox" <?php echo $avoid_duplicated_posts; ?> id="<?php echo esc_attr($this->get_field_id('avoid_duplicated_posts')); ?>" name="<?php echo esc_attr( $this->get_field_name('avoid_duplicated_posts')); ?>" />
				<label for="<?php echo esc_attr( $this->get_field_id('avoid_duplicated_posts') ); ?>"><?php esc_html_e('Avoid Duplicated Posts','presso'); ?></label>
			</p>

			<?php
		}
	}
}