<?php
class STM_WP_Widget_Recent_Posts extends WP_Widget {

	public function __construct() {
		$widget_ops = array(
			'classname'   => 'stm_widget_recent_entries',
			'description' => __( "Your site&#8217;s most recent Posts.", 'motors' )
		);
		parent::__construct( 'stm-recent-posts', __( 'STM Last Post', 'motors' ), $widget_ops );
		$this->alt_option_name = 'stm_widget_recent_entries';

	}

	/**
	 * @param array $args
	 * @param array $instance
	 */
	public function widget( $args, $instance ) {
		$cache = array();
		if ( ! $this->is_preview() ) {
			$cache = wp_cache_get( 'stm_widget_recent_posts', 'widget' );
		}

		if ( ! is_array( $cache ) ) {
			$cache = array();
		}

		if ( ! isset( $args['widget_id'] ) ) {
			$args['widget_id'] = $this->id;
		}

		if ( isset( $cache[ $args['widget_id'] ] ) ) {
			echo $cache[ $args['widget_id'] ];

			return;
		}

		ob_start();

		$title = ( ! empty( $instance['title'] ) ) ? $instance['title'] : __( 'STM Recent Posts', 'motors' );

		$number_of_posts = ( ! empty( $instance['number_of_posts'] ) ) ? $instance['number_of_posts'] : __( '1', 'motors' );

		/** This filter is documented in wp-includes/default-widgets.php */
		$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );

		if(empty($number_of_posts)) {
			$number_of_posts = 1;
		}

		/**
		 * Filter the arguments for the Recent Posts widget.
		 *
		 * @since 3.4.0
		 *
		 * @see WP_Query::get_posts()
		 *
		 * @param array $args An array of arguments used to retrieve the recent posts.
		 */
		$r = new WP_Query( apply_filters( 'widget_posts_args', array(
			'posts_per_page'      => $number_of_posts,
			'no_found_rows'       => true,
			'post_status'         => 'publish',
			'ignore_sticky_posts' => true
		) ) );

		if ( $r->have_posts() ) :
			?>
			<?php echo $args['before_widget']; ?>
			<?php if ( $title ) {
			echo $args['before_title'] . $title . $args['after_title'];
		} ?>
				<?php while ( $r->have_posts() ) : $r->the_post(); ?>
					<div class="stm-last-post-widget">
						<?php echo wp_trim_words(get_the_excerpt(), 13, '...'); ?>
						<?php $com_num = get_comments_number(get_the_id()); ?>
						<?php if(!empty($com_num)){ ?>
							<div class="comments-number">
								<a href="<?php the_permalink() ?>"><i class="stm-icon-message"></i><?php echo esc_attr($com_num).' '.esc_html__('Comment', 'motors'); ?></a>
							</div>
						<?php } else{ ?>
							<div class="comments-number">
								<a href="<?php the_permalink() ?>"><i class="stm-icon-message"></i><?php esc_html_e('No comments', 'motors'); ?></a>
							</div>
						<?php }; ?>
					</div>
				<?php endwhile; ?>
			<?php echo $args['after_widget']; ?>
			<?php
			wp_reset_postdata();

		endif;
	}

	public function update( $new_instance, $old_instance ) {
		$instance              = $old_instance;
		$instance['title']     = strip_tags( $new_instance['title'] );
		$instance['number_of_posts']     = strip_tags( $new_instance['number_of_posts'] );

		return $instance;
	}

	public function form( $instance ) {
		$title     = isset( $instance['title'] ) ? esc_attr( $instance['title'] ) : '';
		$number_of_posts     = isset( $instance['number_of_posts'] ) ? esc_attr( $instance['number_of_posts'] ) : '';
		$show_date = isset( $instance['show_date'] ) ? (bool) $instance['show_date'] : false;
		?>
		<p><label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php esc_html_e( 'Title:', 'motors' ); ?></label>
			<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>"
			       name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo $title; ?>"/>
		</p>
		<p><label for="<?php echo $this->get_field_id( 'number_of_posts' ); ?>"><?php esc_html_e( 'Number of posts:', 'motors' ); ?></label>
			<input class="widefat" id="<?php echo $this->get_field_id( 'number_of_posts' ); ?>"
			       name="<?php echo $this->get_field_name( 'number_of_posts' ); ?>" type="number" value="<?php echo $number_of_posts; ?>"/>
		</p>
	<?php
	}
}

function register_stm_wp_widget_recent_posts() {
	register_widget( 'STM_WP_Widget_Recent_Posts' );
}
add_action( 'widgets_init', 'register_stm_wp_widget_recent_posts' );