
­­­­­­­­­­­­­­­­­­
<!DOCTYPE html>
<html>
<?php

add_action( 'widgets_init', 'presso_widgets_init_latest_comments' );
if ( ! function_exists( 'presso_widgets_init_latest_comments' ) ) {
	function presso_widgets_init_latest_comments() {
		register_widget( 'Presso__widget_latest_comments' );
	}
}

if ( ! class_exists( 'Presso__widget_latest_comments' ) ) {
	class Presso__widget_latest_comments extends WP_Widget {
		private $default = array(
			'supertitle' => '',
			'title' => '',
			'subtitle' => '',
			'count' => '5',
			'excerpt_length' => '15',
		);

		public function __construct() {
			// widget actual processes
			parent::__construct(
		 		'vw_widget_latest_comments', // Base ID
				PRESSO_THEME_NAME.': Latest Comments', // Name
				array( 'description' => esc_html__( "Display latest comments", '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 );

			$comments = get_comments( array(
				'status' => 'approve',
				'number' => $instance['count'],
			) );

			echo '<ul class="vw-latest-comments">';
			foreach ( $comments as $comment ) : ?>
				<li class="clearfix">
					<a class="vw-latest-comments__avatar" href="<?php echo get_permalink( $comment->comment_post_ID ); ?>" title="<?php printf( esc_attr__('Permalink to %s', 'presso'), get_the_title( $comment->comment_post_ID ) ); ?>" rel="bookmark">
						<?php echo presso_get_avatar( $comment->comment_author_email, 30 ); ?>
					</a>

					<div class="vw-latest-comments__meta clearfix">
						<h5 class="vw-latest-comments__author">
							<?php echo $comment->comment_author ?>
						</h5>
						<div class="vw-latest-comments__post">
							<a href="<?php echo esc_url( get_permalink( $comment->comment_post_ID ) ); ?>" title="<?php printf( esc_attr__('Permalink to %s', 'presso'), get_the_title( $comment->comment_post_ID ) ); ?>" rel="bookmark">
								<?php printf( esc_html__( 'on %s', 'presso' ), get_the_title( $comment->comment_post_ID ) ); ?>
							</a>
						</div>
						<?php if ( $instance['excerpt_length'] > 0 ) : ?>
						<div class="vw-latest-comments__content"><?php echo wp_trim_words( $comment->comment_content, $instance['excerpt_length'] ); ?></div>
						<?php endif; ?>
					</div>
				</li>
			<?php endforeach;
			echo '</ul>';

			echo wp_kses_post( $after_widget );
		}

		function update( $new_instance, $old_instance ) {
			$instance = $old_instance;
			$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['count'] = intval( $new_instance['count'] );
			$instance['excerpt_length'] = intval( $new_instance['excerpt_length'] );

			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'];
			$count = $instance['count'];
			$excerpt_length = $instance['excerpt_length'];
			?>

			<!-- 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>

			<!-- count -->
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('count') ); ?>"><?php esc_html_e( 'Count:', 'presso' ); ?></label>
				<input class="" 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>

			<!-- Excerpt length -->
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id('excerpt_length') ); ?>"><?php esc_html_e( 'Excerpt Length:', 'presso' ); ?></label>
				<input class="" id="<?php echo esc_attr( $this->get_field_id('excerpt_length') ); ?>" name="<?php echo esc_attr( $this->get_field_name('excerpt_length') ); ?>" type="text" value="<?php echo esc_attr($excerpt_length); ?>" size="3" />
			</p>
			<?php
		}
	}
}