<?php

class Solarva_Author_Info_Widget extends WP_Widget {
    function __construct() {
        parent::__construct(
        // Base ID of your widget
            'pxl_author_info_widget',
            esc_html__( 'About Author', 'solarva' ),
            [
                'description' => esc_html__( 'Display post author information', 'solarva' ),
            ]
        );
    }

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

        $title = $instance['title'];
        $title = apply_filters( 'widget_title', !empty($instance['title']) ? $instance['title'] : '' );
        echo wp_kses_post($args['before_widget']);

        echo wp_kses_post($args['before_title']) . wp_kses_post($title) . wp_kses_post($args['after_title']);

        $bg_box_id = !empty($instance['bg_box']) ? $instance['bg_box'] : '';
        $description = !empty($instance['description']) ? $instance['description'] : '';
        $facebook_link = !empty($instance['facebook']) ? $instance['facebook'] : '#';
        $twitter_link = !empty($instance['twitter']) ? $instance['twitter'] : '#';
        $instagram_link = !empty($instance['instagram']) ? $instance['instagram'] : '#';
        $pinterest_link = !empty($instance['pinterest']) ? $instance['pinterest'] : '#';
        $youtube_link = !empty($instance['youtube']) ? $instance['youtube'] : '#';
        $img  = pxl_get_image_by_size( array(
            'attach_id'  => $bg_box_id,
            'thumb_size' => '326x221',
        ) );
        $thumbnail_avatar= $img['thumbnail'];
        ?>
            <div class="pxl-wg-content">
                <?php if(is_single()) : ?>
                    <?php         
                        $user_description = get_user_meta(get_the_author_meta( 'ID' ), 'description', true);
                    ?>
                    <div class="pxl-author--avatar">
                        <?php echo get_avatar( get_the_author_meta( 'ID' ), 280 ); ?>
                    </div>
                    <div class="pxl-author--info">
                        <div class="pxl-author--desc">
                            <?php echo esc_attr($user_description); ?>
                        </div>
                        <?php solarva_get_user_social(); ?>
                    </div>
                    
                    <?php //return; ?>
                <?php else: ?>
                    <div class="pxl-author--avatar">
                        <?php echo wp_kses_post($thumbnail_avatar); ?>
                    </div>
                    <div class="pxl-author--info">
                        <?php if (!empty($description)): ?>
                            <div class="pxl-author--desc"><?php echo solarva_html(nl2br($description)); ?></div>
                        <?php endif; ?>
                        <div class="pxl-author-socials">
                            <a href="<?php echo esc_attr($facebook_link); ?>" class="btn pxl-item--social pxl-hover-scale-bg"><i class="caseicon-facebook"></i></a>
                            <a href="<?php echo esc_attr($twitter_link); ?>" class="btn pxl-item--social pxl-hover-scale-bg"><i class="caseicon-twitter"></i></a>
                            <a href="<?php echo esc_attr($instagram_link); ?>" class="btn pxl-item--social pxl-hover-scale-bg"><i class="caseicon-instagram"></i></a>
                            <a href="<?php echo esc_attr($pinterest_link); ?>" class="btn pxl-item--social pxl-hover-scale-bg"><i class="caseicon-pinterest"></i></a>
                            <a href="<?php echo esc_attr($youtube_link); ?>" class="btn pxl-item--social pxl-hover-scale-bg"><i class="caseicon-youtube"></i></a>
                        </div>
                    </div>
                    <?php endif; ?>
            </div>

        <?php
        echo wp_kses_post($args['after_widget']);
    }
 
    // Widget Settings Form
    public function form( $instance ) {
        $title = $instance['title'];
        $bg_box = isset($instance['bg_box']) ? esc_attr($instance['bg_box']) : '';
        $description = isset($instance['description']) ? esc_html($instance['description']) : '';
        $facebook_link = !empty($instance['facebook']) ? $instance['facebook'] : '#';
        $twitter_link = !empty($instance['twitter']) ? $instance['twitter'] : '#';
        $instagram_link = !empty($instance['instagram']) ? $instance['instagram'] : '#';
        $pinterest_link = !empty($instance['pinterest']) ? $instance['pinterest'] : '#';
        $youtube_link = !empty($instance['youtube']) ? $instance['youtube'] : '#';
        ?>
            <p>
                <label for="<?php echo  esc_attr($this->get_field_id( 'title' )); ?>">
                    <?php _e( 'Title:', 'solarva' ); ?>
                </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>
            <div class="author-image-wrap">
                <label for="<?php echo esc_url($this->get_field_id('bg_box')); ?>"><?php esc_html_e('Author Image', 'solarva'); ?></label>
                <input type="hidden" class="widefat hide-image-url"
                        id="<?php echo esc_attr($this->get_field_id('bg_box')); ?>"
                        name="<?php echo esc_attr($this->get_field_name('bg_box')); ?>"
                        value="<?php echo esc_attr($bg_box) ?>"/>
                <div class="pxl-show-image">
                    <?php
                    if ($bg_box != "") {
                        ?>
                            <img src="<?php echo wp_get_attachment_image_url($bg_box) ?>">
                        <?php
                    }
                    ?>
                </div>
                <?php
                if ($bg_box != "") {
                    ?>
                        <a href="#" class="pxl-select-image pxl-btn" style="display: none;"><?php esc_html_e('Select Image', 'solarva'); ?></a>
                        <a href="#" class="pxl-remove-image pxl-btn"><?php esc_html_e('Remove Image', 'solarva'); ?></a>
                    <?php
                } else {
                    ?>
                        <a href="#" class="pxl-select-image pxl-btn"><?php esc_html_e('Select Image', 'solarva'); ?></a>
                        <a href="#" class="pxl-remove-image pxl-btn" style="display: none;"><?php esc_html_e('Remove Image', 'solarva'); ?></a>
                    <?php
                }
                ?>
            </div>
            <p>
                <label for="<?php echo esc_url($this->get_field_id('description')); ?>"><?php esc_html_e('Description', 'solarva'); ?></label>
                <textarea class="widefat" rows="4" cols="20" id="<?php echo esc_attr($this->get_field_id('description')); ?>" name="<?php echo esc_attr($this->get_field_name('description')); ?>"><?php echo wp_kses_post($description); ?></textarea>
            </p>
            <p>
                <label for="<?php echo esc_attr($this->get_field_id('facebook')); ?>"><?php esc_html_e('Facebook Link:', 'solarva'); ?></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_url($facebook_link); ?>" />
            </p>
            <p>
                <label for="<?php echo esc_attr($this->get_field_id('twitter')); ?>"><?php esc_html_e('Twitter Link:', 'solarva'); ?></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_url($twitter_link); ?>" />
            </p>
            <p>
                <label for="<?php echo esc_attr($this->get_field_id('instagram')); ?>"><?php esc_html_e('Instagram Link:', 'solarva'); ?></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_url($instagram_link); ?>" />
            </p>
            <p>
                <label for="<?php echo esc_attr($this->get_field_id('pinterest')); ?>"><?php esc_html_e('Pinterest Link:', 'solarva'); ?></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_url($pinterest_link); ?>" />
            </p>
            <p>
                <label for="<?php echo esc_attr($this->get_field_id('youtube')); ?>"><?php esc_html_e('YouTube Link:', 'solarva'); ?></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_url($youtube_link); ?>" />
            </p>
        <?php
    }
 
    function update($new_instance, $old_instance)
    {
        $instance = $old_instance;
        $instance['title'] = sanitize_text_field( $new_instance['title'] );
        $instance['bg_box'] = strip_tags($new_instance['bg_box']);
        $instance['description'] = strip_tags($new_instance['description']);
        $instance['facebook'] = strip_tags($new_instance['facebook']);
        $instance['twitter'] = strip_tags($new_instance['twitter']);
        $instance['instagram'] = strip_tags($new_instance['instagram']);
        $instance['pinterest'] = strip_tags($new_instance['pinterest']);
        $instance['youtube'] = strip_tags($new_instance['youtube']);
        return $instance;
    }
 
}
 
 
add_action( 'widgets_init', 'solarva_register_author_info_widget' );
function solarva_register_author_info_widget(){
    if(function_exists('pxl_register_wp_widget')){
        pxl_register_wp_widget( 'Solarva_Author_Info_Widget' );
    }
}