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

class Repair_Shop_About_Widget extends WP_Widget {
	function __construct() {
		parent::__construct(
			'Repair_Shop_About_Widget',
			__('VW About us', 'repair-shop'),
			array( 'description' => __( 'Widget for about us section in sidebar', 'repair-shop' ), ) 
		);
	}
	
	public function widget( $repair_shop_args, $repair_shop_instance ) {
		?>
		<aside class="widget">
			<?php
			$repair_shop_title = isset( $repair_shop_instance['title'] ) ? $repair_shop_instance['title'] : '';
			$repair_shop_author = isset( $repair_shop_instance['author'] ) ? $repair_shop_instance['author'] : '';
			$repair_shop_designation = isset( $repair_shop_instance['designation'] ) ? $repair_shop_instance['designation'] : '';
			$repair_shop_description = isset( $repair_shop_instance['description'] ) ? $repair_shop_instance['description'] : '';
			$repair_shop_read_more_url = isset( $repair_shop_instance['read_more_url'] ) ? $repair_shop_instance['read_more_url'] : '';
			$repair_shop_read_more_text = isset( $repair_shop_instance['read_more_text'] ) ? $repair_shop_instance['read_more_text'] : '';
			$repair_shop_upload_image = isset( $repair_shop_instance['upload_image'] ) ? $repair_shop_instance['upload_image'] : '';

	        echo '<div class="custom-about-us">';
	        if(!empty($repair_shop_title) ){ ?><h3 class="custom_title"><?php echo esc_html($repair_shop_title); ?></h3><?php } ?>
		        <?php if($repair_shop_upload_image): ?>
	      			<img src="<?php echo esc_url($repair_shop_upload_image); ?>" alt="">
				<?php endif; ?>
				<?php if(!empty($repair_shop_author) ){ ?><p class="custom_author"><?php echo esc_html($repair_shop_author); ?></p><?php } ?>
				<?php if(!empty($repair_shop_designation) ){ ?><p class="custom_designation"><?php echo esc_html($repair_shop_designation); ?></p><?php } ?>
		        <?php if(!empty($repair_shop_description) ){ ?><p class="custom_desc"><?php echo esc_html($repair_shop_description); ?></p><?php } ?>
		        <?php if(!empty($repair_shop_read_more_url) ){ ?><div class="more-button"><a class="custom_read_more" href="<?php echo esc_url($repair_shop_read_more_url); ?>"><?php if(!empty($repair_shop_read_more_text) ){ ?><?php echo esc_html($repair_shop_read_more_text); ?><?php } ?></a></div><?php } ?>
	        <?php echo '</div>';
			?>
		</aside>
		<?php
	}
	
	// Widget Backend 
	public function form( $repair_shop_instance ) {	

		$repair_shop_title= ''; $repair_shop_author = ''; $repair_shop_designation = ''; $repair_shop_description= ''; $repair_shop_read_more_text = ''; $repair_shop_read_more_url = ''; $repair_shop_upload_image = '';

		$repair_shop_title = isset( $repair_shop_instance['title'] ) ? $repair_shop_instance['title'] : '';
		$repair_shop_author = isset( $repair_shop_instance['author'] ) ? $repair_shop_instance['author'] : '';
		$repair_shop_designation = isset( $repair_shop_instance['designation'] ) ? $repair_shop_instance['designation'] : '';
		$repair_shop_description = isset( $repair_shop_instance['description'] ) ? $repair_shop_instance['description'] : '';
		$repair_shop_read_more_url = isset( $repair_shop_instance['read_more_url'] ) ? $repair_shop_instance['read_more_url'] : '';
		$repair_shop_read_more_text = isset( $repair_shop_instance['read_more_text'] ) ? $repair_shop_instance['read_more_text'] : '';
		$repair_shop_upload_image = isset( $repair_shop_instance['upload_image'] ) ? $repair_shop_instance['upload_image'] : '';
	?>
		<p>
        <label for="<?php echo esc_attr($this->get_field_id('title')); ?>"><?php esc_html_e('Title:','repair-shop'); ?></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($repair_shop_title); ?>">
    	</p>
    	<p>
        <label for="<?php echo esc_attr($this->get_field_id('author')); ?>"><?php esc_html_e('Author Name:','repair-shop'); ?></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($repair_shop_author); ?>">
    	</p>
    	<p>
        <label for="<?php echo esc_attr($this->get_field_id('designation')); ?>"><?php esc_html_e('Designation:','repair-shop'); ?></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($repair_shop_designation); ?>">
    	</p>
    	<p>
        <label for="<?php echo esc_attr($this->get_field_id('description')); ?>"><?php esc_html_e('Description:','repair-shop'); ?></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($repair_shop_description); ?>">
    	</p>
    	<p>
		<label for="<?php echo esc_attr($this->get_field_id('read_more_text')); ?>"><?php esc_html_e('Button Text:','repair-shop'); ?></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($repair_shop_read_more_text); ?>">
		</p>
		<p>
		<label for="<?php echo esc_attr($this->get_field_id('read_more_url')); ?>"><?php esc_html_e('Button Url:','repair-shop'); ?></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($repair_shop_read_more_url); ?>">
		</p>
		<p>
		<label for="<?php echo esc_attr($this->get_field_id( 'upload_image' )); ?>"><?php esc_html_e( 'Image Url:','repair-shop'); ?></label>
		<?php
			if ( $repair_shop_upload_image != '' ) :
			echo '<img class="custom_media_image" src="' . esc_url($repair_shop_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( $repair_shop_upload_image ); ?>" />
	   	</p>
		<?php 
	}
	
	// Updating widget replacing old instances with new
	public function update( $repair_shop_new_instance, $repair_shop_old_instance ) {
		$repair_shop_instance = array();	
		$repair_shop_instance['title'] = (!empty($repair_shop_new_instance['title']) ) ? strip_tags($repair_shop_new_instance['title']) : '';
		$repair_shop_instance['author'] = ( ! empty( $repair_shop_new_instance['author'] ) ) ? strip_tags($repair_shop_new_instance['author']) : '';
		$repair_shop_instance['designation'] = ( ! empty( $repair_shop_new_instance['designation'] ) ) ? strip_tags($repair_shop_new_instance['designation']) : '';
		$repair_shop_instance['description'] = (!empty($repair_shop_new_instance['description']) ) ? strip_tags($repair_shop_new_instance['description']) : '';
        $repair_shop_instance['read_more_text'] = (!empty($repair_shop_new_instance['read_more_text']) ) ? strip_tags($repair_shop_new_instance['read_more_text']) : '';
        $repair_shop_instance['read_more_url'] = (!empty($repair_shop_new_instance['read_more_url']) ) ? esc_url_raw($repair_shop_new_instance['read_more_url']) : '';
        $repair_shop_instance['upload_image'] = ( ! empty( $repair_shop_new_instance['upload_image'] ) ) ? strip_tags($repair_shop_new_instance['upload_image']) : '';

		return $repair_shop_instance;
	}
}
// Register and load the widget
function repair_shop_about_custom_load_widget() {
	register_widget( 'Repair_Shop_About_Widget' );
}
add_action( 'widgets_init', 'repair_shop_about_custom_load_widget' );