<?php


function contact_us_init() {
	register_widget('contact_us_Widget');
}

add_action('widgets_init', 'contact_us_init');


class contact_us_Widget extends WP_Widget {

	function contact_us_Widget() {
		$widget_ops = array('classname' => 'contact_us_widget', 'description' => __( "Contact Us widget at the footer.",KP_THEME_NAME));
		$this->WP_Widget('contact-us', __('Slant Contact Us',KP_THEME_NAME), $widget_ops);
		$this->alt_option_name = 'contact_us_widget';

		add_action( 'save_post', array(&$this, 'flush_widget_cache') );
		add_action( 'deleted_post', array(&$this, 'flush_widget_cache') );
		add_action( 'switch_theme', array(&$this, 'flush_widget_cache') );
	}

	function widget($args, $instance) {
		$cache = wp_cache_get('contact_us_widget', 'widget');

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

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

		ob_start();
		extract($args);

		$title = apply_filters('widget_title', empty($instance['title']) ? __('Contact Us',KP_THEME_NAME) : $instance['title']);
		

		
?>
		<?php echo $before_widget; ?>
		<?php if ( $title ) echo $before_title . $title . $after_title; ?>
        <div class="contacts">
		
		<?php if($instance['address']): ?>
		<div class="col-md-12 col-sm-6 col-xs-12"><p><i class="fa fa-home"></i><?php echo $instance['address']; ?></p></div>
		<?php endif; ?>
		
   
        <?php if($instance['phone']): ?>
		<div class="col-md-12 col-sm-6 col-xs-12"> <p> <i class="fa fa-phone"></i> <?php echo $instance['phone']; ?></p></div>
		<?php endif; ?>
        
        <?php if($instance['mobile']): ?>
		<div class="col-md-12 col-sm-6 col-xs-12"> <p> <i class="fa fa-mobile-phone"></i> <?php echo $instance['mobile']; ?></p></div>
		<?php endif; ?>
		
		 <?php if($instance['fax']): ?>
		<div class="col-md-12 col-sm-6 col-xs-12"> <p> <i class="fa fa-fax"></i> <?php echo $instance['fax']; ?></p></div>
		<?php endif; ?>
		
		<?php if($instance['email']): ?>
		<div class="col-md-12 col-sm-6 col-xs-12"> <p> <i class="fa fa-envelope"></i><a href='mailto:<?php echo $instance['email']; ?>'><?php echo $instance['email']; ?></a></p></div>
		<?php endif; ?>
		
		</div>
		<div class="clear"></div>
        
        
		<?php echo $after_widget; ?>
<?php
			

		$cache[$args['widget_id']] = ob_get_flush();
		wp_cache_add('contact_us_widget', $cache, 'widget');
	}

	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['address'] = strip_tags($new_instance['address']);
		$instance['email'] = strip_tags($new_instance['email']);
		$instance['phone'] = strip_tags($new_instance['phone']);
		$instance['mobile'] = strip_tags($new_instance['mobile']);
		$instance['fax'] = strip_tags($new_instance['fax']);
		
		$this->flush_widget_cache();

		$alloptions = wp_cache_get( 'alloptions', 'options' );
		if ( isset($alloptions['contact_us_widget']) )
			delete_option('contact_us_widget');

		return $instance;
	}

	function flush_widget_cache() {
		wp_cache_delete('contact_us_widget', 'widget');
	}

	function form( $instance ) {
		$title = isset($instance['title']) ? esc_attr($instance['title']) : '';
		$address = isset($instance['address']) ? esc_attr($instance['address']) : '';
		$email = isset($instance['email']) ? esc_attr($instance['email']) : '';
		$phone = isset($instance['phone']) ? esc_attr($instance['phone']) : '';
		$mobile = isset($instance['mobile']) ? esc_attr($instance['mobile']) : '';
		$fax = isset($instance['fax']) ? esc_attr($instance['fax']) : '';
		
?>

<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e('Title:', KP_THEME_NAME); ?></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('address'); ?>"><?php _e('Address:', KP_THEME_NAME); ?></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 $address; ?>" /></p>
        
  
        <p><label for="<?php echo $this->get_field_id('phone'); ?>"><?php _e('Phone:', KP_THEME_NAME); ?></label>
		<input class="widefat" id="<?php echo $this->get_field_id('phone'); ?>" name="<?php echo $this->get_field_name('phone'); ?>" type="text" value="<?php echo $phone; ?>" /></p>
        
     
        <p><label for="<?php echo $this->get_field_id('mobile'); ?>"><?php _e('Mobile:', KP_THEME_NAME); ?></label>
		<input class="widefat" id="<?php echo $this->get_field_id('mobile'); ?>" name="<?php echo $this->get_field_name('mobile'); ?>" type="text" value="<?php echo $mobile; ?>" /></p>
		
		<p><label for="<?php echo $this->get_field_id('fax'); ?>"><?php _e('Fax:', KP_THEME_NAME); ?></label>
		<input class="widefat" id="<?php echo $this->get_field_id('fax'); ?>" name="<?php echo $this->get_field_name('fax'); ?>" type="text" value="<?php echo $fax; ?>" /></p>
		
		<p><label for="<?php echo $this->get_field_id('email'); ?>"><?php _e('E-mail:', KP_THEME_NAME); ?></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 $email; ?>" /></p>
        

		
<?php
	}
}


?>