<?php
if ( ! class_exists( 'Minimalistic_Blog_Quote_Widget' ) ) {
	/**
	 * Adds Minimalistic_Blog_Quote_Widget Widget.
	 */
	class Minimalistic_Blog_Quote_Widget extends WP_Widget {

		/**
		 * Register widget with WordPress.
		 */
		public function __construct() {
			$minimalistic_blog_quote_widget_ops = array(
				'classname'   => 'blog-quote-widget',
				'description' => __( 'Retrive Quote Widget', 'minimalistic-blog' ),
			);
			parent::__construct(
				'minimalistic_blog_quote_widget',
				__( 'Ascendoor Quotation Widget', 'minimalistic-blog' ),
				$minimalistic_blog_quote_widget_ops
			);
		}

		/**
		 * 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 ) {
			if ( ! isset( $args['widget_id'] ) ) {
				$args['widget_id'] = $this->id;
			}
			$section_title = ! empty( $instance['title'] ) ? $instance['title'] : '';
			$section_title = apply_filters( 'widget_title', $section_title, $instance, $this->id_base );
			$quotation     = ! empty( $instance['quotation'] ) ? $instance['quotation'] : '';
			$author_name   = ! empty( $instance['name'] ) ? $instance['name'] : '';

			echo $args['before_widget'];
			if ( ! empty( $section_title ) ) {
				echo $args['before_title'] . esc_html( $section_title ) . $args['after_title'];
			}
			?>
			<div class="quote-widget-wrapper">
				<div class="quote-widget-container">
					<p class="quote">
						<?php echo wp_kses_post( $quotation ); ?>
					</p>
					<p class="author"><?php echo esc_html( $author_name ); ?></p>
				</div>
			</div>
			<?php
			echo $args['after_widget'];
		}

		/**
		 * Back-end widget form.

		 * @see WP_Widget::form()

		 * @param array $instance Previously saved values from database.
		 */
		public function form( $instance ) {
			$section_title = isset( $instance['title'] ) ? $instance['title'] : '';
			$quotation     = isset( $instance['quotation'] ) ? $instance['quotation'] : '';
			$author_name   = isset( $instance['name'] ) ? $instance['name'] : '';
			?>
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Section Title:', 'minimalistic-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( $section_title ); ?>" />
			</p>
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'quotation' ) ); ?>"><?php esc_html_e( 'Description :', 'minimalistic-blog' ); ?></label>
				<textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'quotation' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'quotation' ) ); ?>" value="<?php echo esc_attr( $quotation ); ?>"><?php echo wp_kses_post( $quotation ); ?></textarea>
			</p>
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'name' ) ); ?>"><?php esc_html_e( 'Author Name:', 'minimalistic-blog' ); ?></label>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'name' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'name' ) ); ?>" type="text" value="<?php echo esc_attr( $author_name ); ?>" />
			</p>
			<?php
		}

		/**
		 * 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;
			$instance['title']     = sanitize_text_field( $new_instance['title'] );
			$instance['quotation'] = wp_kses_post( $new_instance['quotation'] );
			$instance['name']      = sanitize_text_field( $new_instance['name'] );

			return $instance;
		}

	}
}
