<?php
/**
 * @copyright	Copyright (c) 2016 AmyTheme (http://www.amytheme.com). All rights reserved.
 * @license		http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 */

defined( 'ABSPATH' ) or die;

class Amy_Movie_Widget_Comments extends AmyFramework_Widget {
	function __construct() {
		$widget_ops		= array(
			'classname'		=> 'amy-widget-comments',
			'description'	=> esc_html__( 'Widget Comments.', 'amy-movie' ),
		);

		$control_ops	= array(
			'width'			=> 400,
			'height'		=> 350,
		);

		parent::__construct( 'amy_movie_widget_comments', esc_html__( '+ Amy Movie Comments', 'amy-movie' ), $widget_ops, $control_ops );
	}

	function widget( $args, $instance ) {
		extract( $args );

		$title		= apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );
		$number		= empty( $instance['number'] ) ? '' : $instance['number'];
		$class 		= empty( $instance['class'] ) ? '' : $instance['class'];

		echo $before_widget;

		$comments_query = new WP_Comment_Query();
		$comments_query = $comments_query->query( array( 'number' => $number, 'status' => 'approve' ) );
		$comment		= '';

		if ( ! empty( $comments_query ) ) {
			foreach ( $comments_query as $comm ) {
				$cid 			= $comm->comment_ID;
				$postid			= $comm->comment_post_ID;
				$date_format	= 'd M';

				$comment .= '<div class="entry-item">';

				$comment .= '<div class="entry-thumb">';
				$comment .= '<a href="' . get_permalink( $postid ) . '#comment-' . $cid . '">';
				$comment .= get_avatar( $comm->comment_author_email, '48' );
				$comment .= '</a></div>';

				$comment .= '<div class="entry-content">';
				$comment .= '<h2>' . get_comment_author( $cid ) . '</h2>';
				$comment .= '<span class="entry-date">' . get_comment_date( $date_format, $cid ) . '</span>';
				$comment .= '<p>' . strip_tags( substr( apply_filters( 'get_comment_text', $comm->comment_content ), 0, 60 ) ) . '...</p>';
				$comment .= '<div class="entry-in"><span>' . esc_html__( 'In: ', 'amy-movie' ) . '</span>';
				$comment .= '<a href="' . get_permalink( $postid ) . '">' . get_the_title( $postid ) . '</a>';

				$comment .= '</div>';
				$comment .= '<div class="clearfix"></div>';
				$comment .= '</div>';

				$comment .= '</div>';
			}
		}

		$output = '<div class="amy-widget amy-widget-comment ' . $class . '">';

		if ( ! empty( $title ) ) {
			$output .= '<h4 class="amy-title amy-widget-title">' . esc_html( $title ) . '</h4>';
		}

		$output .= $comment;

		$output .= '</div>';

		echo $output;

		echo $after_widget;
	}

	function get_options() {
		return array(
			array(
				'id'		=> 'title',
				'type'		=> 'text',
				'title'		=> esc_html__( 'Title', 'amy-movie' ),
			),

			array(
				'id'		=> 'number',
				'type'		=> 'text',
				'title'		=> esc_html__( 'Number of comments to show', 'amy-movie' ),
				'default'	=> '5',
			),

			array(
				'id'		=> 'class',
				'type'		=> 'text',
				'title'		=> esc_html__( 'Extra Class', 'amy-movie' ),
			),
		);
	}
}

register_widget( 'Amy_Movie_Widget_Comments' );
