<?php

if ( !defined( 'ABSPATH' ) ) {
	exit;
} // Exit if accessed directly

/**
 * Class Thim_Recent_Recipes
 */
class Thim_Recent_Recipes extends WP_Widget {

	function __construct() {
		parent::__construct(
			'thim_recent_recipes',
			esc_html__( 'Thim: Recent Recipes', 'cakeart' ),
			array(
				"description" => esc_html__( "Your site's most recent Recipes.", "cakeart" ),
			)
		);
	}

	public function form( $instance ) {
		$default = array(
			'title'          => esc_html__( 'Recent Recipes', 'cakeart' ),
			'number_recipes' => 3
		);

		$instance = wp_parse_args( (array) $instance, $default );

		$title          = $instance['title'];
		$number_recipes = $instance['number_recipes'];
		?>

		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'widget_title' ) ); ?>"><?php esc_html_e( 'Title', 'cakeart' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'widget_title' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'title' ) ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>">
		</p>
		<p>
			<label for="<?php echo esc_attr( $this->get_field_id( 'number_recipes' ) ); ?>"><?php esc_html_e( 'Number of recipes to show', 'cakeart' ); ?></label>
			<input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'number_recipes' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'number_recipes' ) ); ?>" type="number" min="1" value="<?php echo esc_attr( $number_recipes ); ?>">
		</p>

	<?php
	}

	public function update( $new_instance, $old_instance ) {
		$instance                   = $old_instance;
		$instance['title']          = strip_tags( $new_instance['title'] );
		$instance['number_recipes'] = !empty( $new_instance['number_recipes'] ) ? strip_tags( $new_instance['number_recipes'] ) : 1;
		return $instance;
	}

	public function widget( $args, $instance ) {

		extract( $args, EXTR_SKIP );

		$title          = apply_filters( 'widget-title', $instance['title'] );
		$number_recipes = esc_html( $instance['number_recipes'] );

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

		echo '<h3 class="widget-title">' . esc_html( $title ) . '</h3>';

		global $post;

		$query = array(
			'post_type'      => 'recipe',
			'posts_per_page' => $number_recipes,
			'order'          => 'date'
		);
		$loop  = new WP_Query( $query );
        $loop->in_the_loop = true;
		?>
		<div class="widget-recent-recipes">

		<?php
		while ( $loop->have_posts() ) : $loop->the_post();

			?>
			<div class="row recent-recipes">
				<div class="col-xs-6 recipe-thumbnail">
					<a href="<?php echo esc_url( get_the_permalink() ); ?>"><?php echo wp_get_attachment_image( get_post_thumbnail_id( $post->ID ) ); ?></a>
				</div>
				<div class="col-xs-6 recipe-info">
					<a href="<?php echo esc_url( get_the_permalink() ); ?>" class="recipe-name"><?php echo get_the_title(); ?></a>
					<br />

					<p class="time-posted">
						<?php echo esc_html__( 'On ', 'cakeart ' ) . get_the_time( 'M d, Y', get_the_ID() ); ?>
					</p>
				</div>
			</div>
		<?php

		endwhile;

		wp_reset_postdata();

		echo '</div>';

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

	}

}

add_action( 'widgets_init', 'thim_register_recent_recipes_widget' );

function thim_register_recent_recipes_widget() {
	register_widget( 'Thim_Recent_Recipes' );
}