<?php

class Good_Widget_Recent_Comments extends WP_Widget {

	function __construct() {
		parent::__construct( 'good_recent_comments', __( 'Good - Recent Comments', 'good' ), array(
			'classname'   => 'widget_comments_wrap',
			'description' => __( 'Good - Recent Comments', 'good' )
		) );
	}

	public function widget( $args, $instance ) {
		$default      = array(
			'widget_title' => __( 'Latest Comments', 'good' ),
			'quantity'     => '5'
		);
		$instance     = wp_parse_args( $instance, $default );
		$widget_title = apply_filters( 'widget_title', $instance['widget_title'] );
		$quantity     = $instance['quantity'];

		echo $args['before_widget'];
		?>
		<?php if ( ! empty( $instance['widget_title'] ) ) {
			echo $args['before_title'] . $widget_title . $args['after_title'];
		} ?>
		<div class="small-thumb-view">
			<?php
			$recent_comments = get_comments( array(
				'number' => $quantity,
				'status' => 'approve'
			) );

			foreach ( $recent_comments as $comment ) { ?>
				<article class="post">
					<div class="meta"><?php printf( __( 'By %1$s', 'good' ), get_comment_author_link( $comment ) ); ?> </div>
					<h4 class="entry-title no-heading-style"><a
							href="<?php echo esc_url( get_comment_link( $comment ) ) ?>"><?php echo get_the_title( $comment->comment_post_ID ); ?></a>
					</h4>
				</article>
				<?php
			} ?>
		</div>
		<?php
		echo $args['after_widget'];
	}

	public function update( $new_instance, $old_instance ) {
		$instance = $old_instance;

		$instance['widget_title'] = strip_tags( $new_instance['widget_title'] );
		$instance['quantity']     = strip_tags( $new_instance['quantity'] );

		return $instance;
	}

	public function form( $instance ) {
		$default      = array(
			'widget_title' => __( 'Latest Comments', 'good' ),
			'quantity'     => '5'
		);
		$instance     = wp_parse_args( $instance, $default );
		$widget_title = $instance['widget_title'];
		$quantity     = $instance['quantity'];
		?>
		<p>
			<?php _e( 'Widget title:', 'good' ); ?>
			<input class="widefat" type="text" name="<?php echo esc_attr( $this->get_field_name( 'widget_title' ) ); ?>"
			       value="<?php echo esc_attr( $widget_title ); ?>"/>
		</p>
		<p>
			<?php _e( 'Posts', 'good' ); ?>
			<input class="widefat" type="text" name="<?php echo esc_attr( $this->get_field_name( 'quantity' ) ); ?>"
			       value="<?php echo esc_attr( $quantity ); ?>"/>
		</p>
		<?php
	}

}
