<?php
/**
 * WidgetContactForm.
 *
 * Displays WPrequal widget in widget areas.
 *
 * @since 1.1
 */

namespace WPrequal;

use WP_Widget;

if ( ! defined( 'ABSPATH' ) ) {
    wp_die( 'Access denied!!' );
}

class WidgetContactForm extends WP_Widget {

    /** constructor */
    public function __construct() {
        parent::__construct(
            'wprequal_contact_form',
            __( 'WPrequal Contact Form', 'wprequal' ),
            [ 'description' => __( 'Add a contact form', 'wprequal' ), ]
        );
    }

    /** @see WP_Widget::widget */
    public function widget( $args, $instance ) {

        echo wp_kses_post( $args['before_widget'] );

        if ( ! empty( $instance['title'] ) ) {
            echo wp_kses_post( $args['before_title'] . apply_filters( 'widget_title', $instance['title'] ) . $args['after_title'] );
        }

        if ( ! empty( $instance['content'] ) ) {
            echo wp_kses_post( "<div>{$instance['content']}</div>" );
        }

        $post_id = isset( $instance['post_id'] ) ? $instance['post_id'] : false;

        $form_args = [
            'contact_form_id' => (int) $post_id,
            'type'            => 'contact'
        ];

        do_action( 'wprequal_contact_form', $form_args );

        echo wp_kses_post( $args['after_widget'] );

    }

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

        $post_id = isset( $instance['post_id'] ) ? $instance['post_id'] : '';

        view( 'contact/widget', 'contact-form', [
            'title'        => ! empty( $instance['title'] ) ? $instance['title'] : '',
            'content'      => ! empty( $instance['content'] ) ? $instance['content'] : '',
            'title_id'     => $this->get_field_id( 'title' ),
            'title_name'   => $this->get_field_name( 'title' ),
            'content_id'   => $this->get_field_id( 'content' ),
            'content_name' => $this->get_field_name( 'content' ),
            'args_name'    => $this->get_field_name( 'post_id' ),
            'post_id'      => $post_id
        ] );

    }

    /**
     * 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.
     * @see WP_Widget::update()
     *
     */
    public function update( $new_instance, $old_instance ) {
        $instance            = [];
        $instance['title']   = ( ! empty( $new_instance['title'] ) ) ? sanitize_text_field( $new_instance['title'] ) : '';
        $instance['content'] = ( ! empty( $new_instance['content'] ) ) ? wp_kses_post( $new_instance['content'] ) : '';
        $instance['post_id'] = intval( $new_instance['post_id'] );

        return $instance;
    }

}