<?php
/**
 * Custom Contact us Widget
 */

class Book_Publisher_Agency_Contact_Widget extends WP_Widget {
	function __construct() {
		parent::__construct(
			'Book_Publisher_Agency_Contact_Widget', 
			__('VW Contact us', 'book-publisher-agency'),
			array( 'description' => __( 'Widget for contact us section in sidebar', 'book-publisher-agency' ), ) 
		);
	}
	
	public function widget( $book_publisher_agency_args, $book_publisher_agency_instance ) {
		?>
		<aside class="widget">
			<?php
			$book_publisher_agency_title = isset( $book_publisher_agency_instance['title'] ) ? $book_publisher_agency_instance['title'] : '';
			$book_publisher_agency_phone = isset( $book_publisher_agency_instance['phone'] ) ? $book_publisher_agency_instance['phone'] : '';
			$book_publisher_agency_email = isset( $book_publisher_agency_instance['email'] ) ? $book_publisher_agency_instance['email'] : '';
			$book_publisher_agency_address = isset( $book_publisher_agency_instance['address'] ) ? $book_publisher_agency_instance['address'] : '';
			$book_publisher_agency_timing = isset( $book_publisher_agency_instance['timing'] ) ? $book_publisher_agency_instance['timing'] : '';
			$book_publisher_agency_longitude = isset( $book_publisher_agency_instance['longitude'] ) ? $book_publisher_agency_instance['longitude'] : '';
			$book_publisher_agency_latitude = isset( $book_publisher_agency_instance['latitude'] ) ? $book_publisher_agency_instance['latitude'] : '';
			$book_publisher_agency_contact_form = isset( $book_publisher_agency_instance['contact_form'] ) ? $book_publisher_agency_instance['contact_form'] : '';

	        echo '<div class="custom-contact-us">';
	        if(!empty($book_publisher_agency_title) ){ ?><h3 class="custom_title"><?php echo esc_html($book_publisher_agency_title); ?></h3><?php } ?>
		        <?php if(!empty($book_publisher_agency_phone) ){ ?><p><span class="custom_details"><?php esc_html_e('Phone Number: ','book-publisher-agency'); ?></span><span class="custom_desc"><?php echo esc_html($book_publisher_agency_phone); ?></span></p><?php } ?>
		        <?php if(!empty($book_publisher_agency_email) ){ ?><p><span class="custom_details"><?php esc_html_e('Email ID: ','book-publisher-agency'); ?></span><span class="custom_desc"><?php echo esc_html($book_publisher_agency_email); ?></span></p><?php } ?>
		        <?php if(!empty($book_publisher_agency_address) ){ ?><p><span class="custom_details"><?php esc_html_e('Address: ','book-publisher-agency'); ?></span><span class="custom_desc"><?php echo esc_html($book_publisher_agency_address); ?></span></p><?php } ?> 
		        <?php if(!empty($book_publisher_agency_timing) ){ ?><p><span class="custom_details"><?php esc_html_e('Opening Time: ','book-publisher-agency'); ?></span><span class="custom_desc"><?php echo esc_html($book_publisher_agency_timing); ?></span></p><?php } ?>
		        <?php if(!empty($book_publisher_agency_longitude) ){ ?><embed width="100%" height="200px" frameborder="0" scrolling="no" marginheight="0" marginwidth="0" src="https://maps.google.com/maps?q=<?php echo esc_html($book_publisher_agency_longitude); ?>,<?php echo esc_html($book_publisher_agency_latitude); ?>&hl=es;z=14&amp;output=embed"></embed><?php } ?>
		        <?php if(!empty($book_publisher_agency_contact_form) ){ ?><?php echo do_shortcode($book_publisher_agency_contact_form); ?><?php } ?>
	        <?php echo '</div>';
			?>
		</aside>
		<?php
	}
	
	// Widget Backend 
	public function form( $book_publisher_agency_instance ) {

		$book_publisher_agency_title= ''; $book_publisher_agency_phone= ''; $book_publisher_agency_email = ''; $book_publisher_agency_address = ''; $book_publisher_agency_timing = ''; $book_publisher_agency_longitude = ''; $book_publisher_agency_latitude = ''; $book_publisher_agency_contact_form = ''; 
		
		$book_publisher_agency_title = isset( $book_publisher_agency_instance['title'] ) ? $book_publisher_agency_instance['title'] : '';
		$book_publisher_agency_phone = isset( $book_publisher_agency_instance['phone'] ) ? $book_publisher_agency_instance['phone'] : '';
		$book_publisher_agency_email = isset( $book_publisher_agency_instance['email'] ) ? $book_publisher_agency_instance['email'] : '';
		$book_publisher_agency_address = isset( $book_publisher_agency_instance['address'] ) ? $book_publisher_agency_instance['address'] : '';
		$book_publisher_agency_timing = isset( $book_publisher_agency_instance['timing'] ) ? $book_publisher_agency_instance['timing'] : '';
		$book_publisher_agency_longitude = isset( $book_publisher_agency_instance['longitude'] ) ? $book_publisher_agency_instance['longitude'] : '';
		$book_publisher_agency_latitude = isset( $book_publisher_agency_instance['latitude'] ) ? $book_publisher_agency_instance['latitude'] : '';
		$book_publisher_agency_contact_form = isset( $book_publisher_agency_instance['contact_form'] ) ? $book_publisher_agency_instance['contact_form'] : '';
		
		?>

		<p>
        	<label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('Title:','book-publisher-agency'); ?></label>
        	<input class="widefat" id="<?php echo esc_attr($this->get_field_id('title')); ?>" name="<?php echo esc_attr($this->get_field_name('title')); ?>" type="text" value="<?php echo esc_attr($book_publisher_agency_title); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('phone')); ?>"><?php esc_html_e('Phone Number:','book-publisher-agency'); ?></label>
        	<input class="widefat" id="<?php echo esc_attr($this->get_field_id('phone')); ?>" name="<?php echo esc_attr($this->get_field_name('phone')); ?>" type="text" value="<?php echo esc_attr($book_publisher_agency_phone); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('email')); ?>"><?php esc_html_e('Email id:','book-publisher-agency'); ?></label>
        	<input class="widefat" id="<?php echo esc_attr($this->get_field_id('email')); ?>" name="<?php echo esc_attr($this->get_field_name('email')); ?>" type="text" value="<?php echo esc_attr($book_publisher_agency_email); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('address')); ?>"><?php esc_html_e('Address:','book-publisher-agency'); ?></label>
        	<input class="widefat" id="<?php echo esc_attr($this->get_field_id('address')); ?>" name="<?php echo esc_attr($this->get_field_name('address')); ?>" type="text" value="<?php echo esc_attr($book_publisher_agency_address); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('timing')); ?>"><?php esc_html_e('Opening Time:','book-publisher-agency'); ?></label>
        	<input class="widefat" id="<?php echo esc_attr($this->get_field_id('timing')); ?>" name="<?php echo esc_attr($this->get_field_name('timing')); ?>" type="text" value="<?php echo esc_attr($book_publisher_agency_timing); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('longitude')); ?>"><?php esc_html_e('Longitude:','book-publisher-agency'); ?></label>
        	<input class="widefat" id="<?php echo esc_attr($this->get_field_id('longitude')); ?>" name="<?php echo esc_attr($this->get_field_name('longitude')); ?>" type="text" value="<?php echo esc_attr($book_publisher_agency_longitude); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('latitude')); ?>"><?php esc_html_e('Latitude:','book-publisher-agency'); ?></label>
        	<input class="widefat" id="<?php echo esc_attr($this->get_field_id('latitude')); ?>" name="<?php echo esc_attr($this->get_field_name('latitude')); ?>" type="text" value="<?php echo esc_attr($book_publisher_agency_latitude); ?>">
    	</p>
    	<p>
        	<label for="<?php echo esc_attr($this->get_field_id('contact_form')); ?>"><?php esc_html_e('Contact Form Shortcode:','book-publisher-agency'); ?></label>
        	<input class="widefat" id="<?php echo esc_attr($this->get_field_id('contact_form')); ?>" name="<?php echo esc_attr($this->get_field_name('contact_form')); ?>" type="text" value="<?php echo esc_attr($book_publisher_agency_contact_form); ?>">
    	</p>
		
		<?php 
	}
	
	// Updating widget replacing old instances with new
	public function update( $book_publisher_agency_new_instance, $book_publisher_agency_old_instance ) {
		$book_publisher_agency_instance = array();	
		$book_publisher_agency_instance['title'] = (!empty($book_publisher_agency_new_instance['title']) ) ? strip_tags($book_publisher_agency_new_instance['title']) : '';
		$book_publisher_agency_instance['phone'] = (!empty($book_publisher_agency_new_instance['phone']) ) ? book_publisher_agency_sanitize_phone_number($book_publisher_agency_new_instance['phone']) : '';
		$book_publisher_agency_instance['email'] = (!empty($book_publisher_agency_new_instance['email']) ) ? sanitize_email($book_publisher_agency_new_instance['email']) : '';
		$book_publisher_agency_instance['address'] = (!empty($book_publisher_agency_new_instance['address']) ) ? strip_tags($book_publisher_agency_new_instance['address']) : '';
		$book_publisher_agency_instance['timing'] = (!empty($book_publisher_agency_new_instance['timing']) ) ? strip_tags($book_publisher_agency_new_instance['timing']) : '';
		$book_publisher_agency_instance['longitude'] = (!empty($book_publisher_agency_new_instance['longitude']) ) ? strip_tags($book_publisher_agency_new_instance['longitude']) : '';
		$book_publisher_agency_instance['latitude'] = (!empty($book_publisher_agency_new_instance['latitude']) ) ? strip_tags($book_publisher_agency_new_instance['latitude']) : '';
		$book_publisher_agency_instance['contact_form'] = (!empty($book_publisher_agency_new_instance['contact_form']) ) ? strip_tags($book_publisher_agency_new_instance['contact_form']) : '';
        
		return $book_publisher_agency_instance;
	}
}
// Register and load the widget
function book_publisher_agency_contact_custom_load_widget() {
	register_widget( 'Book_Publisher_Agency_Contact_Widget' );
}
add_action( 'widgets_init', 'book_publisher_agency_contact_custom_load_widget' );