
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
<?php

add_action( 'widgets_init', 'presso_widgets_init_categories' );
if ( ! function_exists( 'presso_widgets_init_categories' ) ) {
	function presso_widgets_init_categories() {
		register_widget( 'Presso__widget_categories' );
	}
}

if ( ! class_exists( 'Presso__widget_categories' ) ) {
	class Presso__widget_categories extends WP_Widget {
		private $default = array(
			'supertitle' => '',
			'title' => '',
			'subtitle' => '',
			'show_count' => 1,
			'show_posts' => 1,
			'selected_cats' => array(),
		);

		public function __construct() {
			// widget actual processes
			parent::__construct(
		 		'vw_widget_categories', // Base ID
				PRESSO_THEME_NAME.': Categories', // Name
				array( 'description' => esc_html__( "Display a category listing in theme's style", '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>';
			}

			echo wp_kses_post( $before_widget );
			if ( $instance['title'] ) echo wp_kses_post( $supertitle_html . $before_title . $title_html . $after_title . $subtitle_html );

			$show_count = ! empty( $instance['show_count'] ) ? '1' : '0';
			$show_posts = ! empty( $instance['show_posts'] ) ? '1' : '0';

			$cats_args = array(
				'pad_counts' => true,
			);

			if ( isset( $instance['selected_cats'] ) ) {
				$cats_args['include'] = $instance['selected_cats'];
			}

			$categories = get_categories( $cats_args );

			$category_classes = '';
			if ( $show_count ) {
				$category_classes .= ' vw-category-list__item--show-count';
			}

			if ( empty( $title_html ) ) {
				$category_classes .= ' vw-category-list__item--no-title';
			}

			echo '<ul class="vw-category-list">';
			foreach ( $categories as $category ) :
			?>
				<li class="vw-category-list__item <?php echo esc_attr( $category_classes ); ?> clearfix">
					<?php if ( $show_count ) : ?>
					<div class="vw-category-list__count"><?php echo sprintf( "%02s", $category->count ); ?></div>
					<?php endif; ?>

					<h3 class="vw-category-list__title">
						<a href="<?php echo get_category_link( $category->cat_ID ); ?>" title="<?php printf( esc_attr__('Permalink to %s', 'presso'), single_cat_title( '', false ) ); ?>" rel="bookmark">
							<?php echo apply_filters( 'list_cats', $category->name, $category ); ?>
						</a>
					</h3>

					<?php 
					if ( $show_posts ) :
						$query_args = presso_build_query( array(
							'cat' => $category->cat_ID,
							'posts_per_page' => 3,
							'has_post_thumbnail' => 1,
						) );

						$presso_secondary_query = new WP_Query( apply_filters( 'presso_filter_widget_categories_query', $query_args ) );

						if ( $presso_secondary_query->have_posts() ) : ?>
							<div class="vw-category-posts">
							<?php while ( $presso_secondary_query->have_posts() ) : $presso_secondary_query->the_post(); ?>
								<a href="<?php the_permalink(); ?>" title="<?php printf( esc_attr__('Permalink to %s', 'presso'), the_title_attribute('echo=0') ); ?>" rel="bookmark" style="background-image: url( <?php echo esc_url( presso_get_featured_image_url( apply_filters( 'presso_filter_widget_categories_thumb', 'presso_thumbnail_small_2' ) ) ); ?> );">
								</a>
							<?php endwhile; ?>
							</div>
						<?php
						endif;
						wp_reset_postdata();
					endif; ?>
				</li>
			<?php endforeach;
			echo '</ul>';
			echo wp_kses_post( $after_widget );

		}

		function update( $new_instance, $old_instance ) {
			$instance = $old_instance;
			$instance['show_count'] = $new_instance['show_count'] ? 1 : 0; // don't change order of line, it's bug with default value replacement.
			$instance['show_posts'] = $new_instance['show_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['selected_cats'] = wp_kses_data( $new_instance['selected_cats'] );

			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'];
			$show_count = $instance['show_count'] ? 'checked="checked"' : '';
			$show_posts = $instance['show_posts'] ? 'checked="checked"' : '';
			$selected_cats = $instance['selected_cats'];
			?>

			<!-- 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>

			<!-- show count -->
			<p>
				<input class="checkbox" type="checkbox" <?php echo $show_count; ?> id="<?php echo esc_attr($this->get_field_id('show_count')); ?>" name="<?php echo esc_attr( $this->get_field_name('show_count')); ?>" />
				<label for="<?php echo esc_attr( $this->get_field_id('show_count') ); ?>"><?php esc_html_e('Show post counts','presso'); ?></label>
			</p>

			<!-- show latest posts -->
			<p>
				<input class="checkbox" type="checkbox" <?php echo $show_posts; ?> id="<?php echo esc_attr($this->get_field_id('show_posts')); ?>" name="<?php echo esc_attr($this->get_field_name('show_posts')); ?>" />
				<label for="<?php echo esc_attr($this->get_field_id('show_posts')); ?>"><?php esc_html_e( 'Show latest posts (Only post contains featured image)', 'presso' ); ?></label>
			</p>

			<!-- selected cats -->
			<p>
				<label for="<?php echo esc_attr($this->get_field_id('selected_cats')); ?>"><?php esc_html_e('Select Categories (No select for all):','presso'); ?></label>
				<ul class="vw-category-checklist tabs-panel" id="<?php echo esc_attr($this->get_field_id('selected_cats')); ?>" name="<?php echo esc_attr($this->get_field_name('selected_cats')); ?>">
					<?php
					$walker = new Presso_Walker_Category_Checklist();
					$walker->set_field_name( $this->get_field_name('selected_cats') );

					wp_category_checklist( 0, 0, $selected_cats, false, $walker );
					?>
				</ul>
				
			</p>

			<style type="text/css">
			.vw-category-checklist {
				border: 1px solid #ddd;
				background-color: #fdfdfd;
				padding: 15px;
				max-height: 300px;
				overflow-y: scroll;
			}
			.vw-category-checklist .children {
				margin-top: 5px;
				margin-left: 1em;
			}
			</style>

			

			<?php
		}
	}
}