<?php
/**
 * Theme Author Widget
 * @package Feron
 * @since 1.0.0
 */

if (!defined('ABSPATH')) {
    exit(); //exit if access directly
}
// Control core classes for avoid errors
if (class_exists('CSF')) {


    // Create a About Widget
    CSF::createWidget('feron_author_widget', array(
        'title' => esc_html__('Feron: Author', 'feron-core'),
        'classname' => 'feron-widget-author',
        'description' => esc_html__('Display Author widget', 'feron-core'),
        'fields' => array(
            array(
                'id' => 'image',
                'type' => 'media',
                'title' => esc_html__('Image', 'Feron-core')
            ),
            array(
                'id' => 'name',
                'type' => 'text',
                'title' => esc_html__('Name', 'Feron-core'),
                'default' => esc_html__('Leslie Alexander', 'feron-core')
            ),
            array(
                'id' => 'phone',
                'type' => 'text',
                'title' => esc_html__('Phone', 'Feron-core'),
                'default' => esc_html__('(480) 555-0103', 'feron-core')
            ),

            array(
                'id' => 'feron-author-social-repeater',
                'type' => 'repeater',
                'title' => esc_html__('Author', 'feron-core'),
                'fields' => array(
                    array(
                        'id' => 'feron-author-social',
                        'type' => 'icon',
                        'title' => esc_html__('author social', 'feron-core'),
                    ),
                    array(
                        'id' => 'feron-author-social-url',
                        'type' => 'text',
                        'title' => esc_html__('author social', 'feron-core'),
                        'default' => esc_html__('#', 'feron-core')
                    ),

                ),
            ),
        )
    ));


    if (!function_exists('feron_author_widget')) {
        function feron_author_widget($args, $instance)
        {

            echo $args['before_widget'];
            $image = $instance['image'];
            $img_id = $image['id'] ?? '';
            $img_print = $img_id ? wp_get_attachment_image_src($img_id,'full')[0] : '';
            $alt_text = get_post_meta($img_id, '_wp_attachment_image_alt', true);
            $name = $instance['name'] ?? '';
            $phone = $instance['phone'] ?? '';
            $author = is_array($instance['feron-author-social-repeater']) && !empty($instance['feron-author-social-repeater']) ? $instance['feron-author-social-repeater'] : [];
            ?>

            <div class="widget_author text-center">  
                <?php
                    if (!empty($img_print)) {
                        printf('<div class="thumb"><img src="%1$s" alt="%2$s"/></div>', esc_url($img_print), esc_attr($alt_text));
                    }
                ?> 
                <div class="details">
                    <h5><?php echo esc_html($name); ?></h5>
                    <h6><?php echo esc_html($phone); ?></h6>
                    <ul class="social-media-list">
                        <?php
                            foreach ($author as $socials) {
                                echo '<li>
                                    <a href="'.$socials['feron-author-social-url'].'">
                                        <i class="' . $socials['feron-author-social'] . '"></i>
                                    </a>
                                </li>';
                            };
                        ?>
                    </ul>
                </div>
            </div>
            <?php

            echo $args['after_widget'];

        }
    }

}

?>