<?php
/**
 * CV: Author Info
 *
 * A widget that displays the author's information, including details like their name,
 * bio, and avatar, to be shown in a sidebar or other widgetized areas.
 *
 * @package Yaatra
 */

// Exit if accessed directly
if ( ! defined( 'ABSPATH' ) ) {
    exit;
}

class Yaatra_Author_Info extends WP_widget {

    /**
     * Register widget with WordPress.
     */
    public function __construct() {
        $widget_ops = array( 
            'classname'         => 'yaatra_author_info',
            'description'       => __( 'Select the user to display the author info.', 'yaatra' ),
            'customize_selective_refresh' => true,
        );
        parent::__construct( 'yaatra_author_info', __( 'CV: Author Info', 'yaatra' ), $widget_ops );
    }

    /**
     * Helper function that holds widget fields.
     * Array is used in update and form functions.
     *
     * @return array
     */
    private function widget_fields() {
        $fields = array(
            'widget_title' => array(
                'yaatra_widgets_name'       => 'widget_title',
                'yaatra_widgets_title'      => __( 'Widget title', 'yaatra' ),
                'yaatra_widgets_field_type' => 'text'
            ),
            'user_id' => array(
                'yaatra_widgets_name'       => 'user_id',
                'yaatra_widgets_title'      => __( 'Select Author', 'yaatra' ),
                'yaatra_widgets_default'    => '',
                'yaatra_widgets_field_type' => 'user_dropdown'
            ),
        );
        return $fields;
    }

    /**
     * Front-end display of widget.
     *
     * @see WP_Widget::widget()
     *
     * @param array $args Widget arguments.
     * @param array $instance Saved values from database.
     */
    public function widget( $args, $instance ) {
        extract( $args );

        if ( empty( $instance ) ) {
            return;
        }

        $widget_title   = !empty( $instance['widget_title'] ) ? $instance['widget_title'] : '';
        $user_id        = !empty( $instance['user_id'] ) ? $instance['user_id'] : '';

        echo $before_widget;
?>
        <div class="cv-author-info-wrapper">
            <?php if ( !empty( $widget_title ) ) {
                echo $before_title . esc_html( $widget_title ) . $after_title;
            } ?>
            <div class="author-bio-wrap">
                <div class="author-avatar"><?php echo get_avatar( $user_id, '132' ); ?></div>
                <div class="author-description"><?php echo wp_kses_post( wpautop( get_the_author_meta( 'description', $user_id ) ) ); ?></div>
                <div class="author-social">
                    <?php get_template_part( 'template-parts/partials/social', 'icons' ); ?>
                </div><!-- .author-social -->
            </div><!-- .author-bio-wrap -->
        </div><!-- .cv-author-info-wrapper -->
<?php
        echo $after_widget;
    }

    /**
     * Sanitize widget form values as they are saved.
     *
     * @see WP_Widget::update()
     *
     * @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.
     */
    public function update( $new_instance, $old_instance ) {
        $instance = $old_instance;
        $widget_fields = $this->widget_fields();

        foreach ( $widget_fields as $widget_field ) {
            $field_name = $widget_field['yaatra_widgets_name'];
            $instance[$field_name] = yaatra_widgets_updated_field_value( $widget_field, $new_instance[$field_name] );
        }

        return $instance;
    }

    /**
     * Back-end widget form.
     *
     * @see WP_Widget::form()
     *
     * @param array $instance Previously saved values from database.
     */
    public function form( $instance ) {
        $widget_fields = $this->widget_fields();

        foreach ( $widget_fields as $widget_field ) {
            $field_name = $widget_field['yaatra_widgets_name'];
            $field_value = !empty( $instance[$field_name] ) ? wp_kses_post( $instance[$field_name] ) : '';
            yaatra_widgets_show_widget_field( $this, $widget_field, $field_value );
        }
    }
}