<?php
/**
 * Plugin Name: Map Widget
 */

class rilee_map_widget extends WP_Widget {

	/**
	 * Widget setup.
	 */
	function __construct() {
		/* Widget settings. */
		$widget_ops = array( 'classname' => 'rilee_map_widget', 'description' => esc_html__('A widget that displays your current location.', 'rilee') );

		/* Widget control settings. */
		$control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => 'rilee_map_widget' );

		//Create the Widget 
		parent::__construct( 'rilee_map_widget', esc_html__('Rilee: Current Location', 'rilee'), $widget_ops, $control_ops);
	}

	/**
	 * How to display the widget on the screen.
	 */
	function widget( $args, $instance ) {
		extract( $args );

		/* Our variables from the widget settings. */
		$title = apply_filters('widget_title', $instance['title'] );
		$current_loc = $instance['current_location'];
		$current_loc_url = $instance['current_location_url'];
		$map_image = get_template_directory_uri() . '/images/world_map_crp_sidebar.png';

		/* Before widget (defined by themes). */
		echo wp_kses_post($before_widget); ?>

			<div class="rilee-map-widget">
				<img class="rilee-world-map" src="<?php echo esc_url($map_image); ?>" alt="<?php echo esc_attr($title); ?>" />
				<span><i class="fas fa-map-marker-alt"></i></span>
				<?php 
				//Widget Title
				if($title) { ?>
					<h4><?php echo wp_kses_post($title); ?></h4>
					<?php
				}
	
				//Current Location
				if($current_loc && $current_loc != '') { ?>
					<?php if($current_loc_url && $current_loc_url != '') { ?>
						<a href="<?php echo esc_url($current_loc_url); ?>">
							<h6><?php echo wp_kses_post($current_loc); ?></h6>
						</a>	
					<?php } else { ?>
						<h6><?php echo wp_kses_post($current_loc); ?></h6>
					<?php }	
				} ?>
			</div>	

		<?php 

		/* After widget (defined by themes). */
		echo wp_kses_post($after_widget);
	}

	/**
	 * Update the widget settings.
	 */
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;

		/* Strip tags for title and name to remove HTML (important for text inputs). */
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['current_location'] = strip_tags( $new_instance['current_location'] );
		$instance['current_location_url'] = strip_tags( $new_instance['current_location_url'] );

		return $instance;
	}


	function form( $instance ) {

		/* Set up some default widget settings. */
		$defaults = array( 'title' => 'Current Location', 'current_location' => '', 'current_location_url' => '');
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>

		<!-- Widget Title: Text Input -->
		<p>
			<label for="<?php echo esc_attr($this->get_field_id( 'title' )); ?>">Title</label>
			<input  type="text" class="widefat" id="<?php echo esc_attr($this->get_field_id( 'title' )); ?>" name="<?php echo esc_attr($this->get_field_name( 'title' )); ?>" value="<?php echo esc_attr($instance['title']); ?>"  />
		</p>
		
		<!-- Current Location -->
		<p>
			<label for="<?php echo esc_attr($this->get_field_id( 'current_location' )); ?>">Current Location:</label>
			<input  type="text" class="widefat" id="<?php echo esc_attr($this->get_field_id( 'current_location' )); ?>" name="<?php echo esc_attr($this->get_field_name( 'current_location' )); ?>" value="<?php echo esc_attr($instance['current_location']); ?>" size="3" />
		</p>

		<!-- Current Location URL -->
		<p>
			<label for="<?php echo esc_attr($this->get_field_id( 'current_location_url' )); ?>">Current Location URL:</label>
			<input  type="text" class="widefat" id="<?php echo esc_attr($this->get_field_id( 'current_location_url' )); ?>" name="<?php echo esc_attr($this->get_field_name( 'current_location_url' )); ?>" value="<?php echo esc_attr($instance['current_location_url']); ?>" size="3" />
		</p>

	<?php
	}
}


add_action( 'widgets_init', 'rilee_map_load_widget' );

function rilee_map_load_widget() {
	register_widget( 'rilee_map_widget' );
} ?>