<?php
// Register and load the widget
function social_icon_load_widget() {
	register_widget( 'classic_social_icon_widget' );
}
add_action( 'widgets_init', 'social_icon_load_widget' );

// Creating the widget 
class Classic_social_icon_widget extends WP_Widget {

	function __construct() {
		parent::__construct(

		// Base ID of your widget
			'social_media', 

		// Widget name will appear in UI
			__('Social Media Icons', 'megapress'), 

		// Widget description
			array( 'description' => __( 'Add social icons', 'megapress' ), ) 
		);
	}

	// Creating widget front-end

	public function widget( $args, $instance ) {
		$title = apply_filters( 'widget_title', isset($instance['title'])?$instance['title']:'' );

		// print_r($instance);exit;

		// before and after widget arguments are defined by themes
		echo $args['before_widget'];
		if ( ! empty( $title ) )
			echo $args['before_title'] . $title . $args['after_title'];

		// This is where you run the code and display the output
		$html_output = '<ul>';

		if(!empty($instance['facebook_url'])) {
			$html_output .= '<li><a href="'. esc_url($instance['facebook_url']) .'" target="_blank"><i class="fa fa-facebook"></i></a></li>';
		}
		if(!empty($instance['google_plus_url'])) {
			$html_output .= '<li><a href="'. esc_url($instance['google_plus_url']) .'" target="_blank"><i class="fa fa-google-plus"></i></a></li>';
		}
		if(!empty($instance['twitter_url'])) {
			$html_output .= '<li><a href="'. esc_url($instance['twitter_url']) .'" target="_blank"><i class="fa fa-twitter"></i></a></li>';
		}
		if(!empty($instance['linkedin_url'])) {
			$html_output .= '<li><a href="'. esc_url($instance['linkedin_url']) .'" target="_blank"><i class="fa fa-linkedin"></i></a></li>';
		}
		$html_output .= '</ul>';

		echo $html_output;
		echo $args['after_widget'];
	}

	// Widget Backend 
	public function form( $instance ) {

		$title =  isset( $instance[ 'title' ] ) ? $instance[ 'title' ] : '';
		$facebook_url =  isset( $instance[ 'facebook_url' ] ) ? $instance[ 'facebook_url' ] : '';
		$google_plus_url =  isset( $instance[ 'google_plus_url' ] ) ? $instance[ 'google_plus_url' ] : '';
		$twitter_url =  isset( $instance[ 'twitter_url' ] ) ? $instance[ 'twitter_url' ] : '';
		$linkedin_url =  isset( $instance[ 'linkedin_url' ] ) ? $instance[ 'linkedin_url' ] : '';
	// Widget admin form
		?>
		<p>
			<label for="<?php echo $this->get_field_id( 'title' ); ?>"><?php _e( 'Title:', 'megapress' ); ?></label> 
			<input class="widefat" id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" />
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'facebook_url' ); ?>"><?php _e( 'Facebook URL:', 'megapress' ); ?></label> 
			<input class="widefat" id="<?php echo $this->get_field_id( 'facebook_url' ); ?>" name="<?php echo $this->get_field_name( 'facebook_url' ); ?>" type="text" value="<?php echo esc_attr( $facebook_url ); ?>" />
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'google_plus_url' ); ?>"><?php _e( 'Google+ URL:', 'megapress' ); ?></label> 
			<input class="widefat" id="<?php echo $this->get_field_id( 'google_plus_url' ); ?>" name="<?php echo $this->get_field_name( 'google_plus_url' ); ?>" type="text" value="<?php echo esc_attr( $google_plus_url ); ?>" />
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'twitter_url' ); ?>"><?php _e( 'Twitter URL:', 'megapress' ); ?></label> 
			<input class="widefat" id="<?php echo $this->get_field_id( 'twitter_url' ); ?>" name="<?php echo $this->get_field_name( 'twitter_url' ); ?>" type="text" value="<?php echo esc_attr( $twitter_url ); ?>" />
		</p>

		<p>
			<label for="<?php echo $this->get_field_id( 'linkedin_url' ); ?>"><?php _e( 'LinkedIn URL:', 'megapress' ); ?></label> 
			<input class="widefat" id="<?php echo $this->get_field_id( 'linkedin_url' ); ?>" name="<?php echo $this->get_field_name( 'linkedin_url' ); ?>" type="text" value="<?php echo esc_attr( $linkedin_url ); ?>" />
		</p>
		<?php 
	}

	// Updating widget replacing old instances with new
	public function update( $new_instance, $old_instance ) {
		$instance = array();
		$instance['title'] = ( ! empty( $new_instance['title'] ) ) ? strip_tags( $new_instance['title'] ) : '';
		$instance['facebook_url'] = ( ! empty( $new_instance['facebook_url'] ) ) ? strip_tags( $new_instance['facebook_url'] ) : '';
		$instance['google_plus_url'] = ( ! empty( $new_instance['google_plus_url'] ) ) ? strip_tags( $new_instance['google_plus_url'] ) : '';
		$instance['twitter_url'] = ( ! empty( $new_instance['twitter_url'] ) ) ? strip_tags( $new_instance['twitter_url'] ) : '';
		$instance['linkedin_url'] = ( ! empty( $new_instance['linkedin_url'] ) ) ? strip_tags( $new_instance['linkedin_url'] ) : '';
		return $instance;
	}
}