<?php

/**
 * Widget Recent Posts
 *
 * @package Dotspice
 * @version 1.3.0
 *
 * 1.0 - method __construct()
 * 2.0 - method widget()
 * 3.0 - method form()
 * 4.0 - method update()
 */


class Dotspice_Widget_Recent_Posts extends WP_Widget {

	/**
	 * Sets up the widgets name etc
	 */
	public function __construct() {
		parent::__construct( 'dotspice_widget_posts_recent',  esc_html__( 'Dotspice Recent Posts', 'dotspice' ), array(
			'classname'   => 'widget-posts widget-posts__recent',
			'description' => esc_html__( 'Displays the Recent posts.', 'dotspice' )
		) );
	}


	/**
	 * Outputs the content of the widget
	 *
	 * @param array $args
	 * @param array $instance
	 */
	public function widget( $args, $instance ) {

		extract( $args );

		$widget_title   = ! empty( $instance['widget_title'] )  ? apply_filters( 'widget_title', $instance['widget_title'] ) : '';
		$display_thumb  = ! empty( $instance['display_thumb'] ) ? (bool) $instance['display_thumb'] : false;
		$display_date   = ! empty( $instance['display_date'] )  ? (bool) $instance['display_date'] : false;
		$posts_per_page = ! empty( $instance['per_page'] )      ? absint( $instance['per_page'] ) : 4;
		$categories     = ! empty( $instance['categories'] )    ? (array) $instance['categories'] : array( 'all' );

		if ( $posts_per_page < 1 ) {
			$posts_per_page = 1;
		}

		echo wp_kses_post( $before_widget );

		if ( $widget_title ) {
			echo wp_kses_post( $before_title . $widget_title . $after_title );
		}

		// Set Query
		$query_args = array(
			'posts_per_page'      => $posts_per_page,
			'post_status'         => 'publish',
			'ignore_sticky_posts' => 1,
			'orderby'             => 'date',
			'order'               => 'DESC',
		);

		if ( ! empty( $categories ) && ! in_array( 'all', $categories ) ) {
			$query_args['cat'] = implode( ',', $categories );
		}
		if( is_single() ){
			$query_args['post__not_in'] = array( get_queried_object()->ID );
		}

		$widget_query = new WP_Query( $query_args );

		// Start the Loop
		if ( $widget_query->have_posts() ) :
			?>
				<div class="widget-posts__list">
					<?php
						while ( $widget_query->have_posts() ) : $widget_query->the_post();
							?>
								<article id="post-<?php the_ID(); ?>" <?php post_class( 'widget-post' ); ?>>
									<?php
										if ( $display_thumb ) {
											?><a class="widget-post__img-link" href="<?php the_permalink(); ?>"><?php
												if ( has_post_thumbnail() ) {
													the_post_thumbnail( array( 70, 70, 'bfi_thumb' => true ), array( 'class' => 'widget-post__img' ) );
												} else {
													echo wp_get_attachment_image( 18, array( 70, 70, 'bfi_thumb' => true ) );
												}
											?></a><?php
										}
									?>
									<div class="widget-post__content">
										<?php if ( $display_date ) { ?>
											<div class="widget-post__date"><?php the_time( 'd.m.Y' ); ?></div>
										<?php } ?>

										<h2 class="widget-post__h"><a href="<?php echo get_the_permalink(); ?>"><?php the_title(); ?></a></h2>
									</div>
								</article>
							<?php
						endwhile;
					?>
				</div>
			<?php
		else:

			// If WP_Query Loop is Empty
			printf( '<p>' . wp_kses( __( 'Ready to publish your first post? <a href="%1$s">Get started here</a>.', 'dotspice' ), array( 'a' => array( 'href' => array() ) ) ) . '</p>',
				esc_url( admin_url( 'post-new.php' ) )
			);
		endif;

		wp_reset_postdata();

		echo wp_kses_post( $after_widget );
	}


	/**
	 * Outputs the options form on admin
	 *
	 * @param array $instance The widget options
	 */
	public function form( $instance ) {
		$widget_title   = isset( $instance['widget_title'] )  ? strip_tags( $instance['widget_title'] ) : esc_html__( 'Recent Posts', 'dotspice' );
		$display_thumb  = isset( $instance['display_thumb'] ) ? (bool) $instance['display_thumb'] : false;
		$display_date   = isset( $instance['display_date'] )  ? (bool) $instance['display_date'] : false;
		$posts_per_page = isset( $instance['per_page'] )      ? intval( $instance['per_page'] ) : 4;
		$categories     = isset( $instance['categories'] )    ? $instance['categories'] : array( 'all' );
		$all_categories = get_categories( 'orderby=name&hide_empty=0' );

		?>
			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'widget_title' ) ); ?>"><?php esc_html_e( 'Title:', 'dotspice' ); ?></label>
				<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'widget_title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'widget_title' ) ); ?>" type="text" value="<?php echo esc_attr( $widget_title ); ?>" />
			</p>

			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'display_thumb' ) ); ?>"><?php esc_html_e( 'Display Thumbnail:', 'dotspice' ); ?></label>&nbsp;
				<input id="<?php echo esc_attr( $this->get_field_id( 'display_thumb' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'display_thumb' ) ); ?>" type="checkbox" <?php checked( $display_thumb ); ?> />
			</p>

			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'display_date' ) ); ?>"><?php esc_html_e( 'Display Date:', 'dotspice' ); ?></label>&nbsp;
				<input id="<?php echo esc_attr( $this->get_field_id( 'display_date' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'display_date' ) ); ?>" type="checkbox" <?php checked( $display_date ); ?> />
			</p>

			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'per_page' ) ); ?>"><?php esc_html_e( 'Posts to show:', 'dotspice' ); ?></label>
				<input id="<?php echo esc_attr( $this->get_field_id( 'per_page' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'per_page' ) ); ?>" type="number" min="1" value="<?php echo absint( $posts_per_page ); ?>" size="4" />
			</p>

			<p>
				<label for="<?php echo esc_attr( $this->get_field_id( 'cat' ) ); ?>"><?php esc_html_e('Which Categories to show?', 'dotspice' ); ?></label>
				<select name="<?php echo esc_attr( $this->get_field_name( 'categories' ) ); ?>[]" id="<?php echo esc_attr( $this->get_field_id( 'categories' ) ); ?>" class="widefat" multiple="multiple">
					<option value="all" <?php echo in_array( 'all', $categories ) ? 'selected="selected"' : ''; ?>>- <?php esc_html_e( 'all', 'dotspice' ); ?> -</option>
					<?php foreach ( $all_categories as $category ): ?>
						<option value="<?php echo absint( $category->term_id ); ?>" <?php echo in_array( $category->term_id, $categories ) ? 'selected="selected"' : ''; ?>><?php echo esc_attr( $category->name ); ?></option>
					<?php endforeach; ?>
				</select>
			</p>
		<?php
	}


	/**
	 * Processing widget options on save
	 *
	 * @param array $new_instance The new options
	 * @param array $old_instance The previous options
	 *
	 * @return array
	 */
	public function update( $new_instance, $old_instance ) {
		$instance = $old_instance;

		$instance['widget_title']  = strip_tags( $new_instance['widget_title'] );
		$instance['display_thumb'] = (bool) $new_instance['display_thumb'];
		$instance['display_date']  = (bool) $new_instance['display_date'];
		$instance['per_page']      = intval( $new_instance['per_page'] );
		$instance['categories']    = $new_instance['categories'];

		return $instance;
	}
}


/**
 * Register the widget
 */
add_action( 'widgets_init', function(){
	register_widget( 'Dotspice_Widget_Recent_Posts' );
} );