<?php

/*********************************************************************************************************
* Widget Recent Post
**********************************************************************************************************/			
	
class Recent_Post_Widget extends WP_Widget {

	/**
	 * Register widget with WordPress.
	 */
	function __construct() {
		parent::__construct(
			'recent_post_widget', // Base ID
			__( 'Mina Recent Post', 'mina' ), // Name
			array( 'description' => __( 'Mina Recent Post Widget', 'mina' ), ) // Args
		);
	}

/**************************************************/

	public function widget( $args, $instance ) {
		echo $args['before_widget'];
		if ( ! empty( $instance['title'] ) ) {
			echo $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ). $args['after_title'];
		} ?>
		
<?php
       

	$args1 = array( 'numberposts' => $instance['number'], 'tax_query' => array(
			array(
				'taxonomy' => 'post_format',
				'field' => 'slug',
				'terms' => 'post-format-aside',
				'operator' => 'NOT IN',
				'orderby' => 'post_date'
			), 
			array(
				'taxonomy' => 'post_format',
				'field' => 'slug',
				'terms' => 'post-format-image',
 				'operator' => 'NOT IN',
				'orderby' => 'post_date'
			)
	) );
	$recent_posts = wp_get_recent_posts( $args1 );
	foreach( $recent_posts as $recent ){
		echo '<ul class="news-widget">';
		echo '<li class="thumb-news"><a href="' . get_permalink($recent["ID"]) . '">' . get_the_post_thumbnail($recent["ID"]) . '</a>  </li> ';
		echo '<li class="news-widget-title"><a href="' . get_permalink($recent["ID"]) . '">' . ($recent["post_title"]).'</a> </li> ';
		echo '<li class="news-widget-date"><i class="fa fa-calendar"></i> '. get_the_date( "Y-m-d", $recent["ID"] ).' </li> ';
		echo '</ul>';		
	}
?>
	
		
		
	<?php	
		echo $args['after_widget'];
		
	}

/**************************************************/

	public function form( $instance ) {

		$title = ! empty( $instance['title'] ) ? $instance['title'] : __( 'New title', 'mina' );
		$number = ! empty( $instance['number'] ) ? $instance['number'] : __( '3', 'mina' );
		?>
		<p>
		<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'mina' ); ?></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 esc_attr( $title ); ?>">
		</p>

		
		<p>
		<label for="<?php echo $this->get_field_id( 'number' ); ?>"><?php _e( 'Number Of post:', 'mina' ); ?></label> 
		<input class="widefat1" id="<?php echo $this->get_field_id( 'number' ); ?>" cols="5" name="<?php echo $this->get_field_name( 'number' ); ?>" type="text" value="<?php echo esc_attr( $number ); ?>">
		</p>
		<?php 
	}

/**************************************************/

	public function update( $new_instance, $old_instance ) {
		$instance = array();
		$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
		$instance['number'] = ( ! empty( $new_instance['number'] ) ) ? strip_tags( $new_instance['number'] ) : '';

		return $instance;
	}

}

/**************************************************/

function register_recent_post_widget() {
    register_widget( 'Recent_Post_Widget' );
}
add_action( 'widgets_init', 'register_recent_post_widget' );
	
	