<?php
if (!defined('ABSPATH')) {
    exit;
}
class United_Blog_Social_Menu extends United_Blog_Widget_Base
{
    /**
     * Constructor.
     */
    public function __construct()
    {
        $this->widget_cssclass = 'widget_united_blog_social_menu';
        $this->widget_description = __('Displays social menu if you have set it.', 'united-blog');
        $this->widget_id = 'united_blog_social_menu';
        $this->widget_name = __('United Blog: Social Menu', 'united-blog');
        $this->settings = array(
            'title' => array(
                'type' => 'text',
                'label' => __('Title', 'united-blog'),
            ),
            'color' => array(
                'type' => 'select',
                'label' => __('Social Links Color', 'united-blog'),
                'options' => array(
                    'theme_color' => __('Use Theme Color', 'united-blog'),
                    'brand_color' => __('Use Brand Color', 'united-blog'),
                    'brand_background_color' => __('Use Background Color', 'united-blog'),
                ),
                'std' => 'theme_color',
            ),
            'style' => array(
                'type' => 'select',
                'label' => __('Style', 'united-blog'),
                'options' => united_blog_get_social_display_styles(),
                'std' => 'style_1',
            ),
            'show_label' => array(
                'type' => 'checkbox',
                'label' => __('Show Label', 'united-blog'),
                'std' => false,
            ),
        );
        parent::__construct();
        add_action('wp_enqueue_scripts', array($this, 'enqueue_assets'));
    }
    /**
     * Output widget.
     *
     * @param array $args
     * @param array $instance
     * @see WP_Widget
     *
     */
    public function widget($args, $instance)
    {
        ob_start();
        $this->widget_start($args, $instance);
        do_action('united_blog_before_social_menu');
        ?>
        <div class="social-menu-widget">
            <?php
            if (has_nav_menu('social')) {
                $social_link_class = 'reset-list-style united-social-widget ';
                $social_link_style = isset($instance['style']) ? $instance['style'] : $this->settings['style']['std'];
                $social_link_style .= united_blog_the_theme_svg($social_link_style);
                $social_link_color = isset($instance['color']) ? $instance['color'] : $this->settings['color']['std'];
                $social_link_class .= $social_link_style . ' ' . $social_link_color;
                $label_class = 'screen-reader-text';
                $show_label = isset($instance['show_label']) ? $instance['show_label'] : $this->settings['show_label']['std'];
                if ($show_label) {
                    $label_class = 'united-social-label';
                    $social_link_class .= ' has-social-label';
                }
                wp_nav_menu(
                    array(
                        'theme_location' => 'social',
                        'container' => false,
                        'fallback_cb' => false,
                        'depth' => 1,
                        'menu_class' => $social_link_class,
                        'link_before' => '<span class="' . $label_class . '">',
                        'link_after' => '</span>',
                    )
                );
            } else {
                esc_html_e('Social menu is not set. You need to create menu and assign it to Social Menu on Menu Settings.', 'united-blog');
            }
            ?>
        </div>
        <?php
        do_action('united_blog_after_social_menu');
        $this->widget_end($args, $instance);
        echo ob_get_clean();
    }
    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/social-menu' . $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);
            }
        }
    }
}
