<?php
/**
 * Author Widget Class.
 *
 * @package Glaze_Blog_Lite
 */

if ( ! class_exists( 'Glaze_Blog_Lite_Author_Widget' ) ) {
	/**
	 * Widget class - Glaze_Blog_Lite_Author_Widget.
	 *
	 * @since 1.0.0
	 *
	 * @package Glaze_Blog_Lite
	 */
	class Glaze_Blog_Lite_Author_Widget extends WP_Widget {

		/**
		 * Define id, name and description of the widget.
		 *
		 * @since 1.0.0
		 */
		public function __construct() {

			parent::__construct(
				'glaze-blog-lite-author-widget',
				esc_html__( 'GBL: Author Widget', 'glaze-blog-lite' ),
				array(
					'description' => esc_html__( 'Displays brief author detail.', 'glaze-blog-lite' ),
				)
			);
		}

		/**
		 * Renders widget at the frontend.
		 *
		 * @since 1.0.0
		 *
		 * @param array $args Provides the HTML you can use to display the widget title class and widget content class.
		 * @param array $instance The settings for the instance of the widget..
		 */
		public function widget( $args, $instance ) {

			$title = apply_filters( 'widget_title', empty( $instance['title'] ) ? '' : $instance['title'], $instance, $this->id_base );

			$author_page       = ! empty( $instance['author_page'] ) ? $instance['author_page'] : '';
			$author_link_title = ! empty( $instance['author_link_title'] ) ? $instance['author_link_title'] : '';

			$author_args = array(
				'post_type'      => 'page',
				'posts_per_page' => 1,
			);

			if ( $author_page > 0 ) {
				$author_args['page_id'] = absint( $author_page );
			}

			$author = new WP_Query( $author_args );

			if ( $author->have_posts() ) {

				while ( $author->have_posts() ) {

					$author->the_post();
					?>
					<div class="widget gb-author-widget">
						<?php
						if ( ! empty( $title ) ) {
							?>
							<div class="widget_title">
								<h3><?php echo esc_html( $title ); ?></h3>
							</div><!-- .widget_title -->
							<?php
						}
						?>
						<div class="widget-container">
							<?php
							if ( has_post_thumbnail() ) {
								?>
								<div class="author-thumb">
									<a href="<?php the_permalink(); ?>">
										<?php
										the_post_thumbnail(
											'glaze-blog-lite-thumbnail-two',
											array(
												'alt' => the_title_attribute(
													array(
														'echo' => false,
													)
												),
											)
										);
										?>
									</a>
								</div><!-- .author-thumb -->
								<?php
							}
							?>
							<div class="author-bio">
								<?php the_excerpt(); ?>
							</div><!-- .author-bio -->
							<?php
							if ( ! empty( $author_link_title ) ) {
								?>
								<div class="author-permalink">
									<a href="<?php the_permalink(); ?>"><i class="fa fa-angle-right" aria-hidden="true"></i><span><?php echo esc_html( $author_link_title ); ?></span><i class="fa fa-angle-left" aria-hidden="true"></i></a>
								</div><!-- .auhtor-permalink -->
								<?php
							}
							?>
						</div><!-- .widget-container -->
					</div><!-- .widget.gb-author-widget -->
					<?php
				}
				wp_reset_postdata();
			}
		}

		/**
		 * Adds setting fields to the widget and renders them in the form.
		 *
		 * @since 1.0.0
		 *
		 * @param array $instance The settings for the instance of the widget..
		 */
		public function form( $instance ) {

			$defaults = array(
				'title'             => '',
				'author_page'       => '',
				'author_link_title' => '',
			);

			$instance = wp_parse_args( (array) $instance, $defaults );
			?>
			<p>
				<label for="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>">
					<strong><?php esc_html_e( 'Title', 'glaze-blog-lite' ); ?></strong>
				</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( $instance['title'] ); ?>"
				/>   
			</p>

			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'author_page' ) ); ?>"><strong><?php esc_html_e( 'Author Page', 'glaze-blog-lite' ); ?></strong></label>
				<?php
				wp_dropdown_pages(
					array(
						'id'               => esc_attr( $this->get_field_id( 'author_page' ) ),
						'class'            => 'widefat',
						'name'             => esc_attr( $this->get_field_name( 'author_page' ) ),
						'selected'         => esc_attr( $instance['author_page'] ),
						'show_option_none' => esc_html__( '&mdash; Select Page &mdash;', 'glaze-blog-lite' ),
					)
				);
				?>
			</p>

			<p>
				<label for="<?php echo esc_attr( $this->get_field_name( 'author_link_title' ) ); ?>">
					<strong><?php esc_html_e( 'Author Page Link Title', 'glaze-blog-lite' ); ?></strong>
				</label>
				<input
					class="widefat"
					id="<?php echo esc_attr( $this->get_field_id( 'author_link_title' ) ); ?>"
					name="<?php echo esc_attr( $this->get_field_name( 'author_link_title' ) ); ?>"
					type="text"
					value="<?php echo esc_attr( $instance['author_link_title'] ); ?>"
				/>   
			</p>
			<?php
		}

		/**
		 * Sanitizes and saves the instance of the widget.
		 *
		 * @since 1.0.0
		 *
		 * @param array $new_instance The settings for the new instance of the widget.
		 * @param array $old_instance The settings for the old instance of the widget.
		 * @return array Sanitized instance of the widget.
		 */
		public function update( $new_instance, $old_instance ) {

			$instance = $old_instance;

			$instance['title']             = isset( $new_instance['title'] ) ? sanitize_text_field( $new_instance['title'] ) : '';
			$instance['author_page']       = isset( $new_instance['author_page'] ) ? absint( $new_instance['author_page'] ) : '';
			$instance['author_link_title'] = isset( $new_instance['author_link_title'] ) ? sanitize_text_field( $new_instance['author_link_title'] ) : '';

			return $instance;
		}
	}
}
