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

class Forest_Jungle_Safari_About_Widget extends WP_Widget {
	function __construct() {
		parent::__construct(
			'Forest_Jungle_Safari_About_Widget',
			__('VW About us', 'forest-jungle-safari'),
			array( 'description' => __( 'Widget for about us section in sidebar', 'forest-jungle-safari' ), ) 
		);
	}
	
	public function widget( $forest_jungle_safari_args, $forest_jungle_safari_instance ) {
		?>
		<aside class="widget">
			<?php
			$forest_jungle_safari_title = isset( $forest_jungle_safari_instance['title'] ) ? $forest_jungle_safari_instance['title'] : '';
			$forest_jungle_safari_author = isset( $forest_jungle_safari_instance['author'] ) ? $forest_jungle_safari_instance['author'] : '';
			$forest_jungle_safari_designation = isset( $forest_jungle_safari_instance['designation'] ) ? $forest_jungle_safari_instance['designation'] : '';
			$forest_jungle_safari_description = isset( $forest_jungle_safari_instance['description'] ) ? $forest_jungle_safari_instance['description'] : '';
			$forest_jungle_safari_read_more_url = isset( $forest_jungle_safari_instance['read_more_url'] ) ? $forest_jungle_safari_instance['read_more_url'] : '';
			$forest_jungle_safari_read_more_text = isset( $forest_jungle_safari_instance['read_more_text'] ) ? $forest_jungle_safari_instance['read_more_text'] : '';
			$forest_jungle_safari_upload_image = isset( $forest_jungle_safari_instance['upload_image'] ) ? $forest_jungle_safari_instance['upload_image'] : '';

	        echo '<div class="custom-about-us">';
	        if(!empty($forest_jungle_safari_title) ){ ?><h3 class="custom_title"><?php echo esc_html($forest_jungle_safari_title); ?></h3><?php } ?>
		        <?php if($forest_jungle_safari_upload_image): ?>
	      			<img src="<?php echo esc_url($forest_jungle_safari_upload_image); ?>" alt="">
				<?php endif; ?>
				<?php if(!empty($forest_jungle_safari_author) ){ ?><p class="custom_author"><?php echo esc_html($forest_jungle_safari_author); ?></p><?php } ?>
				<?php if(!empty($forest_jungle_safari_designation) ){ ?><p class="custom_designation"><?php echo esc_html($forest_jungle_safari_designation); ?></p><?php } ?>
		        <?php if(!empty($forest_jungle_safari_description) ){ ?><p class="custom_desc"><?php echo esc_html($forest_jungle_safari_description); ?></p><?php } ?>
		        <?php if(!empty($forest_jungle_safari_read_more_url) ){ ?><div class="more-button"><a class="custom_read_more" href="<?php echo esc_url($forest_jungle_safari_read_more_url); ?>"><?php if(!empty($forest_jungle_safari_read_more_text) ){ ?><?php echo esc_html($forest_jungle_safari_read_more_text); ?><?php } ?></a></div><?php } ?>
	        <?php echo '</div>';
			?>
		</aside>
		<?php
	}
	
	// Widget Backend 
	public function form( $forest_jungle_safari_instance ) {	

		$forest_jungle_safari_title= ''; $forest_jungle_safari_author = ''; $forest_jungle_safari_designation = ''; $forest_jungle_safari_description= ''; $forest_jungle_safari_read_more_text = ''; $forest_jungle_safari_read_more_url = ''; $forest_jungle_safari_upload_image = '';

		$forest_jungle_safari_title = isset( $forest_jungle_safari_instance['title'] ) ? $forest_jungle_safari_instance['title'] : '';
		$forest_jungle_safari_author = isset( $forest_jungle_safari_instance['author'] ) ? $forest_jungle_safari_instance['author'] : '';
		$forest_jungle_safari_designation = isset( $forest_jungle_safari_instance['designation'] ) ? $forest_jungle_safari_instance['designation'] : '';
		$forest_jungle_safari_description = isset( $forest_jungle_safari_instance['description'] ) ? $forest_jungle_safari_instance['description'] : '';
		$forest_jungle_safari_read_more_url = isset( $forest_jungle_safari_instance['read_more_url'] ) ? $forest_jungle_safari_instance['read_more_url'] : '';
		$forest_jungle_safari_read_more_text = isset( $forest_jungle_safari_instance['read_more_text'] ) ? $forest_jungle_safari_instance['read_more_text'] : '';
		$forest_jungle_safari_upload_image = isset( $forest_jungle_safari_instance['upload_image'] ) ? $forest_jungle_safari_instance['upload_image'] : '';
	?>
		<p>
        <label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('Title:','forest-jungle-safari'); ?></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($forest_jungle_safari_title); ?>">
    	</p>
    	<p>
        <label for="<?php echo esc_attr($this->get_field_id('author')); ?>"><?php esc_html_e('Author Name:','forest-jungle-safari'); ?></label>
        <input class="widefat" id="<?php echo esc_attr($this->get_field_id('author')); ?>" name="<?php echo esc_attr($this->get_field_name('author')); ?>" type="text" value="<?php echo esc_attr($forest_jungle_safari_author); ?>">
    	</p>
    	<p>
        <label for="<?php echo esc_attr($this->get_field_id('designation')); ?>"><?php esc_html_e('Designation:','forest-jungle-safari'); ?></label>
        <input class="widefat" id="<?php echo esc_attr($this->get_field_id('designation')); ?>" name="<?php echo esc_attr($this->get_field_name('designation')); ?>" type="text" value="<?php echo esc_attr($forest_jungle_safari_designation); ?>">
    	</p>
    	<p>
        <label for="<?php echo esc_attr($this->get_field_id('description')); ?>"><?php esc_html_e('Description:','forest-jungle-safari'); ?></label>
        <input class="widefat" id="<?php echo esc_attr($this->get_field_id('description')); ?>" name="<?php echo esc_attr($this->get_field_name('description')); ?>" type="text" value="<?php echo esc_attr($forest_jungle_safari_description); ?>">
    	</p>
    	<p>
		<label for="<?php echo esc_attr($this->get_field_id('read_more_text')); ?>"><?php esc_html_e('Button Text:','forest-jungle-safari'); ?></label>
		<input class="widefat" id="<?php echo esc_attr($this->get_field_id('read_more_text')); ?>" name="<?php echo esc_attr($this->get_field_name('read_more_text')); ?>" type="text" value="<?php echo esc_attr($forest_jungle_safari_read_more_text); ?>">
		</p>
		<p>
		<label for="<?php echo esc_attr($this->get_field_id('read_more_url')); ?>"><?php esc_html_e('Button Url:','forest-jungle-safari'); ?></label>
		<input class="widefat" id="<?php echo esc_attr($this->get_field_id('read_more_url')); ?>" name="<?php echo esc_attr($this->get_field_name('read_more_url')); ?>" type="text" value="<?php echo esc_attr($forest_jungle_safari_read_more_url); ?>">
		</p>
		<p>
		<label for="<?php echo esc_attr($this->get_field_id( 'upload_image' )); ?>"><?php esc_html_e( 'Image Url:','forest-jungle-safari'); ?></label>
		<?php
			if ( $forest_jungle_safari_upload_image != '' ) :
			echo '<img class="custom_media_image" src="' . esc_url($forest_jungle_safari_upload_image) . '" style="margin:10px 0;padding:0;max-width:100%;float:left;display:inline-block" /><br />';
			endif;
		?>
		<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'upload_image' ) ); ?>" name="<?php echo esc_attr($this->get_field_name( 'upload_image' )); ?>" type="text" value="<?php echo esc_url( $forest_jungle_safari_upload_image ); ?>" />
	   	</p>
		<?php 
	}
	
	// Updating widget replacing old instances with new
	public function update( $forest_jungle_safari_new_instance, $forest_jungle_safari_old_instance ) {
		$forest_jungle_safari_instance = array();	
		$forest_jungle_safari_instance['title'] = (!empty($forest_jungle_safari_new_instance['title']) ) ? strip_tags($forest_jungle_safari_new_instance['title']) : '';
		$forest_jungle_safari_instance['author'] = ( ! empty( $forest_jungle_safari_new_instance['author'] ) ) ? strip_tags($forest_jungle_safari_new_instance['author']) : '';
		$forest_jungle_safari_instance['designation'] = ( ! empty( $forest_jungle_safari_new_instance['designation'] ) ) ? strip_tags($forest_jungle_safari_new_instance['designation']) : '';
		$forest_jungle_safari_instance['description'] = (!empty($forest_jungle_safari_new_instance['description']) ) ? strip_tags($forest_jungle_safari_new_instance['description']) : '';
        $forest_jungle_safari_instance['read_more_text'] = (!empty($forest_jungle_safari_new_instance['read_more_text']) ) ? strip_tags($forest_jungle_safari_new_instance['read_more_text']) : '';
        $forest_jungle_safari_instance['read_more_url'] = (!empty($forest_jungle_safari_new_instance['read_more_url']) ) ? esc_url_raw($forest_jungle_safari_new_instance['read_more_url']) : '';
        $forest_jungle_safari_instance['upload_image'] = ( ! empty( $forest_jungle_safari_new_instance['upload_image'] ) ) ? strip_tags($forest_jungle_safari_new_instance['upload_image']) : '';

		return $forest_jungle_safari_instance;
	}
}
// Register and load the widget
function forest_jungle_safari_about_custom_load_widget() {
	register_widget( 'Forest_Jungle_Safari_About_Widget' );
}
add_action( 'widgets_init', 'forest_jungle_safari_about_custom_load_widget' );