<?php
if (!defined('ABSPATH')) {
    exit;
}
/**
 * Adds United_Blog_Author_Profile widget.
 */
class United_Blog_Author_Profile extends WP_Widget
{
    public $social_networks;
    /**
     * Sets up a new widget instance.
     *
     * @since 1.0.0
     */
    function __construct()
    {
        parent::__construct(
            'united_blog_author_profile_widget',
            esc_html__('United Blog: Author Profile', 'united-blog'),
            array('description' => esc_html__('Displays author short profile.', 'united-blog'))
        );
        $this->social_networks = apply_filters(
            'united_blog_author_widget_social_networks',
            array(
                'facebook',
                'twitter',
                'linkedin',
                'instagram',
                'pinterest',
                'youtube',
                'tiktok',
                'twitch',
                'vk',
                'whatsapp',
                'amazon',
                'codepen',
                'dropbox',
                'flickr',
                'vimeo',
                'spotify',
                'github',
                'reddit',
                'skype',
                'soundcloud',
            )
        );
        add_action('wp_enqueue_scripts', array($this, 'enqueue_assets'));
    }
    /**
     * Outputs the content for the current widget instance.
     *
     * @param array $args Display arguments.
     * @param array $instance Saved values from database.
     * @since 1.0.0
     *
     */
    public function widget($args, $instance)
    {
        echo $args['before_widget'];
        if ($title = apply_filters('widget_title', empty($instance['title']) ? '' : $instance['title'], $instance, $this->id_base)) {
            echo $args['before_title'] . $title . $args['after_title'];
        }
        $social_link_class = ' social-menu author-social-profile reset-list-style ';
        $social_link_color = isset($instance['social_links_color']) ? $instance['social_links_color'] : 'theme_color';
        $social_link_class .= $social_link_color;
        do_action('united_blog_before_author_profile');
        ?>
        <div class="author-profile-content">
            <?php
            if (isset($instance['author_img']) && $instance['author_img']) {
                ?>
                <div class="author-image">
                    <?php echo wp_get_attachment_image(absint($instance['author_img']), 'medium_large', '', array('class' => 'attachment-medium_large size-medium_large image-responsive')); ?>
                </div>
                <?php
            }
            ?>
            <div class="author-details">
                <?php
                if (isset($instance['author_name']) && $instance['author_name']) {
                    ?>
                    <h3 class="author-name"><?php echo esc_html($instance['author_name']); ?></h3>
                    <?php
                }
                ?>
                <?php
                $social_networks = $this->social_networks;
                if (!empty($social_networks) && is_array($social_networks)) {
                    ob_start();
                    foreach ($social_networks as $network) {
                        if (!empty($instance[$network])) {
                            $svg = United_Blog_SVG_Icons::get_social_link_svg($instance[$network]);
                            if ($svg) {
                                ?>
                                <li>
                                    <a href="<?php echo esc_url($instance[$network]); ?>" target="_blank">
                                        <?php echo $svg; ?>
                                    </a>
                                </li>
                                <?php
                            }
                        }
                    }
                    $social_networks_list = ob_get_clean();
                    if ($social_networks_list) {
                        ?>
                        <div class="author-social">
                            <ul class="<?php echo esc_attr($social_link_class); ?>">
                                <?php echo $social_networks_list; ?>
                            </ul>
                        </div>
                        <?php
                    }
                }
                ?>
                <div class="author-desc">
                    <?php
                    if (isset($instance['author_desc']) && $instance['author_desc']) {
                        echo wpautop(wp_kses_post($instance['author_desc']));
                    }
                    ?>
                </div>
            </div>
        </div>
        <?php
        do_action('united_blog_after_author_profile');
        echo $args['after_widget'];
    }
    /**
     * Back-end widget form.
     *
     * @param array $instance Previously saved values from database.
     *
     * @return void
     * @since 1.0.0
     *
     */
    public function form($instance)
    {
        $title = !empty($instance['title']) ? $instance['title'] : '';
        $author_name = !empty($instance['author_name']) ? $instance['author_name'] : '';
        $author_desc = !empty($instance['author_desc']) ? $instance['author_desc'] : '';
        $author_img = !empty($instance['author_img']) ? $instance['author_img'] : '';
        $social_links_color = !empty($instance['social_links_color']) ? $instance['social_links_color'] : 'theme_color';
        ?>
        <p>
            <label for="<?php echo esc_attr($this->get_field_id('title')); ?>">
                <?php esc_attr_e('Title:', 'united-blog'); ?>
            </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>
            <label for="<?php echo esc_attr($this->get_field_id('author_name')); ?>">
                <?php esc_attr_e('Author Name:', 'united-blog'); ?>
            </label>
            <input class="widefat" id="<?php echo esc_attr($this->get_field_id('author_name')); ?>" name="<?php echo esc_attr($this->get_field_name('author_name')); ?>" type="text" value="<?php echo esc_attr($author_name); ?>">
        </p>
        <p>
            <label for="<?php echo esc_attr($this->get_field_id('author_desc')); ?>">
                <?php esc_attr_e('Short Description:', 'united-blog'); ?>
            </label>
            <textarea class="widefat" id="<?php echo esc_attr($this->get_field_id('author_desc')); ?>" name="<?php echo esc_attr($this->get_field_name('author_desc')); ?>" rows="10"><?php echo esc_textarea($author_desc); ?></textarea>
        </p>
        <div>
            <label for="<?php echo esc_attr($this->get_field_id('author_img')); ?>">
                <?php esc_attr_e('Author Image:', 'united-blog'); ?>
            </label>
            <?php
            $remove_button_style = ($author_img) ? 'display:inline-block' : 'display:none;';
            ?>
            <div class="image-field">
                <div class="image-preview">
                    <?php
                    if (!empty($author_img)) {
                        $image_attributes = wp_get_attachment_image_src($author_img);
                        if ($image_attributes) {
                            ?>
                            <img src="<?php echo esc_url($image_attributes[0]); ?>"/>
                            <?php
                        }
                    }
                    ?>
                </div>
                <p>
                    <input type="hidden" class="img" name="<?php echo esc_attr($this->get_field_name('author_img')); ?>" id="<?php echo esc_attr($this->get_field_id('author_img')); ?>" value="<?php echo esc_attr($author_img); ?>"/>
                    <button type="button" class="upload_image_button button" data-uploader-title-txt="<?php esc_attr_e('Use Image', 'united-blog'); ?>" data-uploader-btn-txt="<?php esc_attr_e('Choose an Image', 'united-blog'); ?>">
                        <?php esc_html_e('Upload/Add image', 'united-blog'); ?>
                    </button>
                    <button type="button" class="remove_image_button button" style="<?php echo esc_attr($remove_button_style); ?>"><?php esc_html_e('Remove image', 'united-blog'); ?></button>
                </p>
            </div>
        </div>
        <div>
            <p>
                <label for="<?php echo esc_attr($this->get_field_id('social_links_color')); ?>">
                    <span class="field-label"><?php esc_html_e('Social Links Color', 'united-blog'); ?></span>
                </label>
                <select id="<?php echo esc_attr($this->get_field_id('social_links_color')); ?>" name="<?php echo esc_attr($this->get_field_name('social_links_color')); ?>" class="widefat">
                    <option value="theme_color" <?php selected('theme_color', $social_links_color); ?>><?php esc_html_e('Use Theme Color', 'united-blog'); ?></option>
                    <option value="brand_color" <?php selected('brand_color', $social_links_color); ?>><?php esc_html_e('Use Brand Color', 'united-blog'); ?></option>
                    <option value="brand_background_color" <?php selected('brand_background_color', $social_links_color); ?>><?php esc_html_e('Use Brand Background', 'united-blog'); ?></option>
                </select>
            </p>
        </div>
        <?php
        $social_networks = $this->social_networks;
        if (!empty($social_networks) && is_array($social_networks)) {
            foreach ($social_networks as $network) {
                $social_link = !empty($instance[$network]) ? $instance[$network] : '';
                ?>
                <p class="author-social-link">
                    <label for="<?php echo esc_attr($this->get_field_id($network)); ?>">
                        <?php echo esc_html(ucfirst($network)); ?>
                    </label>
                    <input class="widefat" id="<?php echo esc_attr($this->get_field_id($network)); ?>" name="<?php echo esc_attr($this->get_field_name($network)); ?>" type="text" value="<?php echo esc_url($social_link); ?>">
                </p>
                <?php
            }
        }
        ?>
        <?php
    }
    /**
     * Sanitize widget form values as they are saved.
     *
     * @param array $new_instance Values just sent to be saved.
     * @param array $old_instance Previously saved values from database.
     *
     * @return array Updated safe values to be saved.
     * @since 1.0.0
     *
     */
    public function update($new_instance, $old_instance)
    {
        $instance = array();
        $instance['title'] = (!empty($new_instance['title'])) ? sanitize_text_field($new_instance['title']) : '';
        $instance['author_name'] = (!empty($new_instance['author_name'])) ? sanitize_text_field($new_instance['author_name']) : '';
        $instance['author_desc'] = (!empty($new_instance['author_desc'])) ? wp_kses_post($new_instance['author_desc']) : '';
        $instance['author_img'] = (!empty($new_instance['author_img'])) ? absint($new_instance['author_img']) : '';
        $instance['style'] = (!empty($new_instance['style'])) ? sanitize_text_field($new_instance['style']) : 'style_1';
        $instance['social_links_color'] = (!empty($new_instance['social_links_color'])) ? sanitize_text_field($new_instance['social_links_color']) : 'theme_color';
        $social_networks = $this->social_networks;
        if (!empty($social_networks) && is_array($social_networks)) {
            foreach ($social_networks as $network) {
                $instance[$network] = (!empty($new_instance[$network])) ? esc_url_raw($new_instance[$network]) : '';
            }
        }
        return $instance;
    }
    public function enqueue_assets()
    {
        if (is_active_widget(false, false, $this->id_base)) {
            $file_prefix = is_rtl() ? '-rtl' : '';
            $css_file = get_template_directory() . '/inc/widgets/css/author-profile' . $file_prefix . '.css';
            if (file_exists($css_file)) {
                $styles = wp_strip_all_tags(file_get_contents($css_file));
                wp_add_inline_style('united-blog-style', $styles);
            }
        }
    }
}
