<?php error_reporting(E_ALL & ~E_NOTICE);
add_action( 'widgets_init','realestate_register_contact_widget'); 
   function realestate_register_contact_widget() { return   register_widget( 'RealEstate_Contact_Widget' ); }

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

	/**
	 * Register widget with WordPress.
	 */
	function __construct() {
		parent::__construct(
			'RealEstate_Contact_Widget', // Base ID
			__('Real Estate Contact Us Widget', 'is-realestate'), // Name
			array( 'description' => __( 'Show Your Contact Information On your Widget Area.', 'is-realestate' ), ) // Args
		);
	}
	
	public function widget( $args, $instance ) {
		$title = apply_filters( 'widget_title', $instance['title'] );
		$address = apply_filters( 'address', $instance['address'] );
		$contact = apply_filters( 'contact', $instance['contact'] );
		$email = apply_filters( 'email', $instance['email'] );

		$title = apply_filters( 'widget_title', $title, $instance, $this->id_base );
		$address = apply_filters( 'address', $address, $instance, $this->id_base );
		$contact = apply_filters( 'contact', $contact, $instance, $this->id_base );
		$email = apply_filters( 'email', $email, $instance, $this->id_base );


		// before and after widget arguments are defined by themes
		echo wp_kses_post($args['before_widget']);
		if ( ! empty( $title ) )
		echo wp_kses_post($args['before_title']) . wp_kses_post($title) . wp_kses_post($args['after_title']);

		if($address!=''){ ?>
		<p> <b> <?php _e('Location :','is-realestate'); ?></b> <?php echo esc_attr($address); ?></p>
		<?php } if($contact!=''){ ?>
		<p> <b> <?php _e('Phone :','is-realestate'); ?></b> <?php echo esc_attr($contact); ?></p>
		<?php } if($email!=''){ ?>
		<p> <b> <?php _e('Email :','is-realestate'); ?></b> <?php echo esc_attr($email); ?></p>
		<?php }	
		echo $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[ 'address' ] ) && isset( $instance[ 'contact' ] ) && isset( $instance[ 'email' ] )) {
			$title = $instance[ 'title' ];
			$address = $instance[ 'address' ];
			$contact = $instance[ 'contact' ];
			$email = $instance[ 'email' ];
		} 
		else {
		$title = __( 'Contact Us', 'is-realestate' );
		} ?>
		<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( 'address' ); ?>"><?php _e( 'Your Address:','is-realestate' ); ?></label> 
		<input class="widefat" id="<?php echo $this->get_field_id( 'address' ); ?>" name="<?php echo $this->get_field_name( 'address' ); ?>" type="text" value="<?php echo esc_attr( $address ); ?>" />
		</p>
		<p>
		<label for="<?php echo $this->get_field_id( 'contact' ); ?>"><?php _e( 'Add Your Contact Number:','is-realestate' ); ?></label> 
		<input class="widefat" id="<?php echo $this->get_field_id( 'contact' ); ?>" name="<?php echo $this->get_field_name( 'contact' ); ?>" type="text" value="<?php echo esc_attr( $contact ); ?>" />
		</p>
		<p>
		<label for="<?php echo $this->get_field_id( 'email' ); ?>"><?php _e( 'Add Your Email:','is-realestate' ); ?></label> 
		<input class="widefat" id="<?php echo $this->get_field_id( 'email' ); ?>" name="<?php echo $this->get_field_name( 'email' ); ?>" type="text" value="<?php echo esc_attr( $email ); ?>" />
		</p>		
		<?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'  => '',
                                'address' => '',
                                'contact'  => '',
                                'email' => '',
                                'text'   => '',
                                'filter' => false, // For back-compat.
                                'visual' => null, // Must be explicitly defined.
	                        )
	                );
	                $instance = $old_instance;
	                $instance['title'] = sanitize_text_field( $new_instance['title'] );
	                 $instance['address'] = sanitize_text_field( $new_instance['address'] );
	                 $instance['contact'] = sanitize_text_field( $new_instance['contact'] );
	                 $instance['email'] = sanitize_text_field( $new_instance['email'] );
	                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;
	}

}
?>