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

		/**
		 * Register widget with WordPress.
		 */
		public function __construct() {
			$mindful_blog_author_info_widget_ops = array(
				'classname'   => 'ascendoor-widget author-info',
				'description' => __( 'Retrive Author Information Widget', 'mindful-blog' ),
			);
			parent::__construct(
				'mindful_blog_author_info_widget',
				__( 'Ascendoor Author Info Widget', 'mindful-blog' ),
				$mindful_blog_author_info_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;
			}
			$mindful_blog_section_title      = ! empty( $instance['title'] ) ? $instance['title'] : '';
			$mindful_blog_section_title      = apply_filters( 'widget_title', $mindful_blog_section_title, $instance, $this->id_base );
			$mindful_blog_author_name        = ! empty( $instance['name'] ) ? $instance['name'] : '';
			$mindful_blog_author_description = ! empty( $instance['description'] ) ? $instance['description'] : '';
			$mindful_blog_author_image_url   = ! empty( $instance['author_image_url'] ) ? $instance['author_image_url'] : '';

			echo $args['before_widget'];
			if ( ! empty( $mindful_blog_section_title ) ) {
				?>
				<div class="section-header">
					<?php echo $args['before_title'] . esc_html( $mindful_blog_section_title ) . $args['after_title']; ?>
				</div>
			<?php } ?>
			<div class="mindful-blog-section-body">
				<div class="author-img">
					<?php
					if ( ! empty( $mindful_blog_author_image_url ) ) {
						echo '<img src="' . esc_url( $mindful_blog_author_image_url ) . '" alt="' . esc_attr( $mindful_blog_author_name ) . '"  />';
					}
					?>
				</div>
				<div class="author-details">
					<h3 class="author-name"><?php echo esc_html( $mindful_blog_author_name ); ?></h3>
					<div class="author-social-contacts">
						<?php
						for ( $mindful_blog_i = 1; $mindful_blog_i <= 4; $mindful_blog_i++ ) {
							$mindful_blog_link = ( ! empty( $instance[ 'link' . '-' . $mindful_blog_i ] ) ) ? $instance[ 'link' . '-' . $mindful_blog_i ] : '';
							if ( ! empty( $mindful_blog_link ) ) :
								?>
								<a href="<?php echo esc_url( $mindful_blog_link ); ?>"></a>
								<?php
							endif;
						}
						?>
					</div>
					<p class="author-description"><?php echo wp_kses_post( $mindful_blog_author_description ); ?></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 ) {
			$mindful_blog_section_title      = isset( $instance['title'] ) ? $instance['title'] : '';
			$mindful_blog_author_name        = isset( $instance['name'] ) ? $instance['name'] : '';
			$mindful_blog_author_description = isset( $instance['description'] ) ? $instance['description'] : '';
			$mindful_blog_author_image_url   = isset( $instance['author_image_url'] ) ? $instance['author_image_url'] : '';
			?>
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>"><?php esc_html_e( 'Section Title:', 'mindful-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( $mindful_blog_section_title ); ?>" />
			</p>
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'name' ) ); ?>"><?php esc_html_e( 'Author Name:', 'mindful-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( $mindful_blog_author_name ); ?>" />
			</p>
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'description' ) ); ?>"><?php esc_html_e( 'Description :', 'mindful-blog' ); ?></label>
				<textarea class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'description' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'description' ) ); ?>" value="<?php echo esc_attr( $mindful_blog_author_description ); ?>"><?php echo wp_kses_post( $mindful_blog_author_description ); ?></textarea>
			</p>
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'author_image_url' ) ); ?>"><?php esc_html_e( 'Author Image URL', 'mindful-blog' ); ?></label>:<br />
				<input type="url" class="img widefat" name="<?php echo esc_attr( $this->get_field_name( 'author_image_url' ) ); ?>" id="<?php echo esc_attr( $this->get_field_id( 'author_image_url' ) ); ?>" value="<?php echo esc_url( $mindful_blog_author_image_url ); ?>" /><br />
				<input type="button" class="select-img" value="<?php esc_attr_e( 'Upload', 'mindful-blog' ); ?>" data-uploader_title="<?php esc_attr_e( 'Select Image', 'mindful-blog' ); ?>" data-uploader_button_text="<?php esc_attr_e( 'Choose Image', 'mindful-blog' ); ?>" style="margin-top:5px;" /><br/>
			</p>
			<?php
			for ( $mindful_blog_i = 1; $mindful_blog_i <= 4; $mindful_blog_i++ ) {
				$mindful_blog_link = isset( $instance[ 'link' . '-' . $mindful_blog_i ] ) ? $instance[ 'link' . '-' . $mindful_blog_i ] : '';
				?>
				<p>
					<label for="<?php echo esc_attr( $this->get_field_id( 'link' . '-' . $mindful_blog_i ) ); ?>"><?php sprintf( esc_html__( 'Link %s :', 'mindful-blog' ), $mindful_blog_i ); ?></label>
					<input type="url" class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'link' . '-' . $mindful_blog_i ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'link' . '-' . $mindful_blog_i ) ); ?>" value="<?php echo esc_url( $mindful_blog_link ); ?>"/>
				</p>
			<?php } ?>

			<?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['name']             = sanitize_text_field( $new_instance['name'] );
			$instance['description']      = wp_kses_post( $new_instance['description'] );
			$instance['author_image_url'] = esc_url_raw( $new_instance['author_image_url'] );
			for ( $mindful_blog_i = 1; $mindful_blog_i <= 4; $mindful_blog_i++ ) {
				$instance[ 'link' . '-' . $mindful_blog_i ] = esc_url_raw( $new_instance[ 'link' . '-' . $mindful_blog_i ] );
			}

			return $instance;
		}

	}
}

/**
 * Enqueue admin scripts for Image Widget
 *
 * @since Mindful Blog 1.0
 **/
function mindful_blog_author_info_widget_image_upload_enqueue( $hook ) {

	if ( 'widgets.php' !== $hook ) {
		return;
	}

	wp_enqueue_media();
	wp_enqueue_script( 'author-info-widget-image-upload-script', get_template_directory_uri() . '/assets/js/upload.min.js', array( 'jquery' ), MINDFUL_BLOG_VERSION, true );
}

add_action( 'admin_enqueue_scripts', 'mindful_blog_author_info_widget_image_upload_enqueue' );
