<?php

if ( !defined( 'ABSPATH' ) ) {
	exit;
} // Exit if accessed directly

/**
 * Class Thim_Social_Links
 */
class Thim_Social_Links_Widget extends WP_Widget {

	function __construct() {
		parent::__construct(
			'social_links_widget',
			esc_html__( 'Thim: Social Links', 'cakeart' ),
			array(
				'description' => esc_html__( 'Display a list of social links.', 'cakeart' ),
			)
		);
	}

	public function form( $instance ) {
		$default = array(
			'facebook'  => '#',
			'twitter'   => '#',
			'pinterest' => '#',
			'google'    => '#',
			'youtube'   => '#',
            'instagram'   => '#',

		);

		$instance = wp_parse_args( (array) $instance, $default );

		$facebook  = $instance['facebook'];
		$twitter   = $instance['twitter'];
		$pinterest = $instance['pinterest'];
		$google    = $instance['google'];
		$youtube   = $instance['youtube'];
        $instagram   = $instance['instagram'];

		?>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'facebook' ) ); ?>"><?php esc_html_e( 'Facebook URL', 'cakeart' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'facebook' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'facebook' ) ); ?>" type="text" value="<?php echo esc_attr( $facebook ); ?>" />
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'twitter' ) ); ?>"><?php esc_html_e( 'Twitter URL', 'cakeart' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'twitter' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'twitter' ) ); ?>" type="text" value="<?php echo esc_attr( $twitter ); ?>" />
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'pinterest' ) ); ?>"><?php esc_html_e( 'Pinterest URL', 'cakeart' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'pinterest' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'pinterest' ) ); ?>" type="text" value="<?php echo esc_attr( $pinterest ); ?>" />
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'google' ) ); ?>"><?php esc_html_e( 'Google plus URL', 'cakeart' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'google' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'google' ) ); ?>" type="text" value="<?php echo esc_attr( $google ); ?>" />
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'youtube' ) ); ?>"><?php esc_html_e( 'Youtube URL', 'cakeart' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'youtube' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'youtube' ) ); ?>" type="text" value="<?php echo esc_attr( $youtube ); ?>" />
		</p>
        <p>
            <label for="<?php echo esc_attr( $this->get_field_id( 'instagram' ) ); ?>"><?php esc_html_e( 'Instagram URL', 'cakeart' ); ?></label>
            <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'instagram' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'instagram' ) ); ?>" type="text" value="<?php echo esc_attr( $instagram ); ?>" />
        </p>
		<p><?php echo esc_html__( 'You can disable one of them by set empty in form input of it.', 'cakeart' ) ?></p>
	<?php
	}

	public function update( $new_instance, $old_instance ) {
		$instance              = $old_instance;
		$instance['facebook']  = ( !empty( $new_instance['facebook'] ) ) ? strip_tags( $new_instance['facebook'] ) : '';
		$instance['twitter']   = ( !empty( $new_instance['twitter'] ) ) ? strip_tags( $new_instance['twitter'] ) : '';
		$instance['pinterest'] = ( !empty( $new_instance['pinterest'] ) ) ? strip_tags( $new_instance['pinterest'] ) : '';
		$instance['google']    = ( !empty( $new_instance['google'] ) ) ? strip_tags( $new_instance['google'] ) : '';
		$instance['youtube']   = ( !empty( $new_instance['youtube'] ) ) ? strip_tags( $new_instance['youtube'] ) : '';
        $instance['instagram']   = ( !empty( $new_instance['instagram'] ) ) ? strip_tags( $new_instance['instagram'] ) : '';
		return $instance;
	}

	public function widget( $args, $instance ) {
		extract( $args, EXTR_SKIP );

		$facebook  = $instance['facebook'];
		$twitter   = $instance['twitter'];
		$pinterest = $instance['pinterest'];
		$google    = $instance['google'];
		$youtube   = $instance['youtube'];
        $instagram   = $instance['instagram'];


		echo ent2ncr( $args['before_widget'] );
		?>

		<div class="social-link">
			<ul>
				<?php if ( $facebook ) { ?>
					<li>
						<div class="facebook">
							<a href="<?php echo esc_url( $facebook ); ?>" target="_blank"><i class="fa fa-facebook"></i></a>
						</div>
					</li>
				<?php } ?>
				<?php if ( $twitter ) { ?>
					<li>
						<div class="twitter">
							<a href="<?php echo esc_url( $twitter ); ?>" target="_blank"><i class="fa fa-twitter"></i></a>
						</div>
					</li>
				<?php } ?>
				<?php if ( $pinterest ) { ?>
					<li>
						<div class="pinterest">
							<a href="<?php echo esc_url( $pinterest ); ?>" target="_blank"><i class="fa fa-pinterest"></i></a>
						</div>
					</li>
				<?php } ?>
				<?php if ( $google ) { ?>
					<li>
						<div class="google">
							<a href="<?php echo esc_url( $google ); ?>" target="_blank"><i class="fa fa-google-plus"></i></a>
						</div>
					</li>
				<?php } ?>
				<?php if ( $youtube ) { ?>
					<li>
						<div class="youtube">
							<a href="<?php echo esc_url( $youtube ); ?>" target="_blank"><i class="fa fa-youtube"></i></a>
						</div>
					</li>
				<?php } ?>

                <?php if ( $instagram ) { ?>
                    <li>
                        <div class="google">
                            <a href="<?php echo esc_url( $instagram ); ?>" target="_blank"><i class="fa fa-instagram"></i></a>
                        </div>
                    </li>
                <?php } ?>

			</ul>
		</div>

		<?php
		echo ent2ncr( $args['after_widget'] );
	}

}

add_action( 'widgets_init', 'thim_register_social_links_widget' );

function thim_register_social_links_widget() {
	register_widget( 'Thim_Social_Links_Widget' );
}

