<?php
add_action( 'widgets_init','realestate_register_posts_widget'); 
   function realestate_register_posts_widget() 
   { 
   	return register_widget( 'RealEstate_Posts_Widget' ); 
   }

/**
 * Adds widget for recent Post in footer.
 */
class RealEstate_Posts_Widget extends WP_Widget {

	/**
	 * Register widget with WordPress.
	 */
	function __construct() {
		parent::__construct(
			'RealEstate_Posts_Widget', // Base ID
			__('Real Estate Recent Posts Widget', 'is-realestate'), // Name
			array( 'description' => __( 'Show Your Recent Post On your Widget Area.', 'is-realestate' ), ) // Args
		);
	}

	public function widget( $args, $instance ) {
		$title = ! empty( $instance['title'] ) ? $instance['title'] : '';
		$post_num = ! empty( $instance['post_num'] ) ? $instance['post_num'] : '';
		if($post_num=='') { $post_num = 3; }

		$realestate_title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
		$post_num = apply_filters( 'post_num', $post_num, $instance, $this->id_base );
		
		// before and after widget arguments are defined by themes
		echo wp_kses_post($args['before_widget']);
		if ( ! empty( $realestate_title ) )
		echo wp_kses_post($args['before_title']) . wp_kses_post($realestate_title) . wp_kses_post($args['after_title']);

		$recent_args= array('post_type'=>'post', 'post__not_in' => get_option( 'sticky_posts' ), 'posts_per_page'=>$post_num);
		$recent_post = new WP_Query($recent_args); 
		if($recent_post->have_posts()){
			while($recent_post->have_posts()){
				$recent_post->the_post(); ?>
		<div class="media rs-sidebar-post">
		<?php if(has_post_thumbnail()){ ?>
			<div class="rs-sidebar-post-area">
				<a href="<?php the_permalink(); ?>">
					<?php $thumb_data= array('class' =>'blog-thumb img-responsive ');
					the_post_thumbnail('thumbnail', $thumb_data); ?>
				</a>
			</div>
		<?php } ?>
			<div class="media-body rs-post-right-text">
				<h4> <a href="<?php esc_url(the_permalink()); ?>"><span><?php the_title(); ?></span></a></h4>
				<h4> <small><?php get_the_date(); ?></small></h4>
				<p><?php echo wp_trim_words(get_the_excerpt(),15,''); ?> </p>
			</div>
			<hr>
		</div>
			<?php }
		} wp_reset_query();
		echo wp_kses_post($args['after_widget']); 
	}

	/**
	 * Back-end widget form.
	 *
	 * @see WP_Widget::form()
	 *
	 * @param array $instance Previously saved values from database.
	 */
	public function form( $instance ) {
		if ( isset( $instance[ 'title' ] )  && isset( $instance[ 'post_num' ] )) {
			$title = $instance[ 'title' ];
			$post_num = $instance[ 'post_num' ];
		} ?>
		<p>
		<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:','is-realestate' ); ?></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( 'post_num' ); ?>"><?php _e( 'Number of Posts:','is-realestate' ); ?></label> 
		<input class="widefat" id="<?php echo $this->get_field_id( 'post_num' ); ?>" name="<?php echo $this->get_field_name( 'post_num' ); ?>" type="number" min="1" max="5" value="<?php echo esc_attr( $post_num ); ?>" />	
		<?php 
	}

	/**
	 * Sanitize widget form values as they are saved.
	 *
	 * @see WP_Widget::update()
	 *
	 * @param array $new_instance Values just sent to be saved.
	 * @param array $old_instance Previously saved values from database.
	 *
	 * @return array Updated safe values to be saved.
	 */
		public function update( $new_instance, $old_instance ) {
		  $new_instance = wp_parse_args(
                       $new_instance, array(
                                'title'  => '',
                                'post_num' => '',
	                                'text'   => '',
	                                'filter' => false, // For back-compat.
	                                'visual' => null, // Must be explicitly defined.
	                        )
	                );
	                $instance = $old_instance;
	                $instance['title'] = sanitize_text_field( $new_instance['title'] );
	                 $instance['post_num'] = sanitize_text_field( $new_instance['post_num'] );
	                if ( current_user_can( 'unfiltered_html' ) ) {
                        $instance['text'] = $new_instance['text'];
	                } else {
                        $instance['text'] = wp_kses_post( $new_instance['text'] );
	                }

	                $instance['filter'] = ! empty( $new_instance['filter'] );
	
	                // Upgrade 4.8.0 format.
	                if ( isset( $old_instance['filter'] ) && 'content' === $old_instance['filter'] ) {
	                        $instance['visual'] = true;
	                }
	                if ( 'content' === $new_instance['filter'] ) {
	                        $instance['visual'] = true;
	                }
	
	                if ( isset( $new_instance['visual'] ) ) {
	                        $instance['visual'] = ! empty( $new_instance['visual'] );
	                }
	
	                // Filter is always true in visual mode.
	                if ( ! empty( $instance['visual'] ) ) {
	                        $instance['filter'] = true;
	                }
	                return $instance;
	}


}
?>