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

class rilee_about_widget extends WP_Widget {

	/**
	 * Widget setup.
	 */
	function __construct() {

		// Widget settings.
		$widget_ops = array( 'classname' => 'rilee_about_widget', 'description' => esc_html__('Author image and description.', 'rilee') );

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

		//Create the Widget 
		parent::__construct( 'rilee_about_widget', esc_html__('Rilee: About Me', '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'] );
		$image = $instance['image'];
		$description = $instance['description'];

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

			<div class="rilee-about-widget">
				<?php if($image) { ?>
					<div class="rilee-about-img" style="background-image:url(<?php echo esc_url($image); ?>)"></div>

				<?php }
				if($title || $description) { ?>

					<div class="rilee-about-widget-caption">

						<?php if($title && $title != '') { ?>
							<h4><?php echo wp_kses_post($title); ?></h4>
							<?php
						}
						if($description && $description != '') { ?>
							<p><?php echo wp_kses_post($description); ?></p>
							<?php	
						} ?>
						
					</div>
					<?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['image'] = strip_tags( $new_instance['image'] );
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['description'] = $new_instance['description'];

		return $instance;
	}


	function form( $instance ) {

		/* Set up some default widget settings. */
		$defaults = array( 'title' => 'About Me', 'image' => '', 'title' => '', 'description' => '');
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>

		<div class="rilee_widget">

			<!-- image url -->
			<p>
				<label for="<?php echo esc_attr($this->get_field_id( 'image' )); ?>">Image URL:</label>
				<input type="text" class="widefat" id="<?php echo esc_attr($this->get_field_id( 'image' )); ?>" name="<?php echo esc_attr($this->get_field_name( 'image' )); ?>" value="<?php echo esc_url($instance['image']); ?>" /><br />
			</p>

			<!-- 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>
			
			<!-- description -->
			<p>
				<label for="<?php echo esc_attr($this->get_field_id( 'description' )); ?>">About me text:</label>
				<textarea type="text" class="widefat" id="<?php echo esc_attr($this->get_field_id( 'description' )); ?>" name="<?php echo esc_attr($this->get_field_name( 'description' )); ?>" style="width:95%;" rows="6"><?php echo esc_textarea($instance['description']); ?></textarea>
			</p>

		</div>	

	<?php
	}
}

add_action( 'widgets_init', 'rilee_register_about_widget' );

function rilee_register_about_widget() {
	
	register_widget( 'rilee_about_widget' );
} ?>