<?php
if ( ! class_exists( 'Kayla_Social_Media_Links_Widget' ) ) {
    class Kayla_Social_Media_Links_Widget extends WP_Widget {
        public function __construct() {
            $widget_ops = array(
                'classname'   => 'kayla-social-media-links',
                'description' => __('Show your list of social networks', 'kayla')
            );
            
            parent::__construct( 'kayla-social-media-links', __( 'Kayla: Social Media Links', 'kayla' ), $widget_ops );
        }
    
        public function widget($args, $instance) {
    
            $instance = wp_parse_args( $instance, $args );
            extract( $instance );
    
            echo wp_kses_post($before_widget);
    
            if ( $title ) {
                echo wp_kses_post( $before_title . $title . $after_title );
            }
    
            $facebook   = get_theme_mod('kayla_facebook_url');
            $twitter    = get_theme_mod('kayla_twitter_url');
            $instagram  = get_theme_mod('kayla_instagram_url');
            $pinterest  = get_theme_mod('kayla_pinterest_url');
            $youtube    = get_theme_mod('kayla_youtube_url');
			$tiktok     = get_theme_mod('kayla_tiktok_url');
			$linkedin   = get_theme_mod('kayla_linkedin_url');
            $soundcloud = get_theme_mod('kayla_soundcloud_url');
            $vimeo      = get_theme_mod('kayla_vimeo_url');
    
            if ( $fb || $tw || $in || $pi || $yt || $ti || $li || $sc || $vm ) {
                ?>
                <div class="social-links">
                    <?php if( $fb ) { ?><a href="<?php echo esc_url($facebook); ?>" ><i class="fa-brands fa-facebook"></i></i></a><?php } ?>
                    <?php if( $tw ) { ?><a href="<?php echo esc_url($twitter); ?>" ><i class="fa-brands fa-x-twitter"></i></a><?php } ?>
                	<?php if( $in ) { ?><a href="<?php echo esc_url($instagram); ?>" ><i class="fa-brands fa-instagram"></i></a><?php } ?>
                	<?php if( $pi ) { ?><a href="<?php echo esc_url($pinterest); ?>" ><i class="fa-brands fa-pinterest"></i></a><?php } ?>
                    <?php if( $yt ) { ?><a href="<?php echo esc_url($youtube); ?>" ><i class="fa-brands fa-youtube"></i></a><?php } ?>
                    <?php if( $ti ) { ?><a href="<?php echo esc_url($tiktok); ?>" ><i class="fa-brands fa-tiktok"></i></a><?php } ?>
					<?php if( $li ) { ?><a href="<?php echo esc_url($linkedin); ?>" ><i class="fa-brands fa-linkedin"></i></a><?php } ?>
                	<?php if( $sc ) { ?><a href="<?php echo esc_url($soundcloud); ?>" ><i class="fa-brands fa-soundcloud"></i></a><?php } ?>
                	<?php if( $vm ) { ?><a href="<?php echo esc_url($vimeo); ?>" ><i class="fa-brands fa-vimeo"></i></a><?php } ?>
                </div>
                <?php
            }
    
            echo wp_kses_post($after_widget);
        }
        
        public function update( $new_instance, $old_instance ) {
            $instance = $old_instance;
            $instance['title'] = sanitize_text_field( $new_instance['title'] );
            $instance['fb'] = $new_instance['fb'];
            $instance['tw'] = $new_instance['tw'];
            $instance['in'] = $new_instance['in'];
            $instance['pi'] = $new_instance['pi'];
            $instance['yt'] = $new_instance['yt'];
            $instance['ti'] = $new_instance['ti'];
            $instance['bl'] = $new_instance['bl'];
            $instance['dr'] = $new_instance['dr'];
            $instance['sc'] = $new_instance['sc'];
            $instance['vm'] = $new_instance['vm'];
            $instance['li'] = $new_instance['li'];
            return $instance;
        }
    
        public function form( $instance ) {        
            $defaults = array(
                'title' => 'Social Media Links',
                'fb' => 'off',
                'tw' => 'off',
                'in' => 'off',
                'pi' => 'off',
                'yt' => 'off',
                'ti' => 'off',
                'fb' => 'off',
                'bl' => 'off',
                'dr' => 'off',
                'sc' => 'off',
                'vm' => 'off',
                'li' => 'off'
            );
            
            $instance = wp_parse_args( ( array ) $instance, $defaults );
            extract( $instance );
            ?>
            <p>
                <label for="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>"><?php esc_html_e( 'Title:', 'kayla' ); ?></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( $title ); ?>" />
            </p>
            <p><i>Note: Set your social links in the <a href="<?php echo esc_url( admin_url( '/customize.php?autofocus[section]=kayla_section_social_media' ) ); ?>">Customize</a></i></p>
            <p>
        		<label for="<?php echo esc_attr( $this->get_field_id( 'fb' ) ); ?>">Facebook</label>
    			<input type="checkbox" <?php checked( $fb, 'on' ); ?> name="<?php echo esc_attr($this->get_field_name( 'fb' )); ?>" />
    		</p>
            <p>
        		<label for="<?php echo esc_attr( $this->get_field_id( 'tw' ) ); ?>">Twitter</label>
    			<input type="checkbox" <?php checked( $tw, 'on' ); ?> name="<?php echo esc_attr($this->get_field_name( 'tw' )); ?>" />
    		</p>
            <p>
        		<label for="<?php echo esc_attr( $this->get_field_id( 'in' ) ); ?>">Instagram</label>
    			<input type="checkbox" <?php checked( $in, 'on' ); ?> name="<?php echo esc_attr($this->get_field_name( 'in' )); ?>" />
    		</p>
            <p>
        		<label for="<?php echo esc_attr( $this->get_field_id( 'pi' ) ); ?>">Pinterest</label>
    			<input type="checkbox" <?php checked( $pi, 'on' ); ?> name="<?php echo esc_attr($this->get_field_name( 'pi' )); ?>" />
    		</p>
            <p>
        		<label for="<?php echo esc_attr( $this->get_field_id( 'yt' ) ); ?>">Youtube</label>
    			<input type="checkbox" <?php checked( $yt, 'on' ); ?> name="<?php echo esc_attr($this->get_field_name( 'yt' )); ?>" />
    		</p>
            <p>
        		<label for="<?php echo esc_attr( $this->get_field_id( 'ti' ) ); ?>">Tiktok</label>
    			<input type="checkbox" <?php checked( $ti, 'on' ); ?> name="<?php echo esc_attr($this->get_field_name( 'ti' )); ?>" />
    		</p>
            <p>
        		<label for="<?php echo esc_attr( $this->get_field_id( 'li' ) ); ?>">LinkedIn</label>
    			<input type="checkbox" <?php checked( $li, 'on' ); ?> name="<?php echo esc_attr($this->get_field_name( 'li' )); ?>" />
    		</p>
            <p>
        		<label for="<?php echo esc_attr( $this->get_field_id( 'sc' ) ); ?>">SoudCloud</label>
    			<input type="checkbox" <?php checked( $sc, 'on' ); ?> name="<?php echo esc_attr($this->get_field_name( 'sc' )); ?>" />
    		</p>
            <p>
        		<label for="<?php echo esc_attr( $this->get_field_id( 'vm' ) ); ?>">Vimeo</label>
    			<input type="checkbox" <?php checked( $vm, 'on' ); ?> name="<?php echo esc_attr($this->get_field_name( 'vm' )); ?>" />
    		</p>
            <?php
        }
    }

    if ( ! function_exists( 'kayla_social_media_links_widget_init' ) ) {
        function kayla_social_media_links_widget_init() {
            register_widget( 'Kayla_Social_Media_Links_Widget' );
        }
        
        add_action( 'widgets_init', 'kayla_social_media_links_widget_init' );
    }
}
