<?php

if (!class_exists('crumina_recent_posts')){

	class crumina_recent_posts extends WP_Widget{

		function __construct(){
			parent::WP_Widget('crum_single_post', 'Crum: Recent Posts', array('description' => 'Recent posts'));
		}

		function widget($args, $instance){

			extract( $args );
			$title = apply_filters( 'widget_title', $instance['title'] );


			$posts_number = $instance['posts_number'];
			$show_thumb = $instance['show_thumb'];
			$show_add_info = $instance['show_meta'];
			$show_excerpt = $instance['show_excerpt'];

			if (isset($posts_number) && !($posts_number == '')){
				$per_page = $posts_number;
			}else{
				$per_page = '-1';
			}

			$posts_array = new WP_Query( array(
				'post_type'      => 'post',
				'posts_per_page' => $per_page,
				'post_status'    => 'publish'
			) );

			echo $before_widget;
			if ($title) {

				echo $before_title;
				echo $title;
				echo $after_title;

			}

			if ($posts_array->have_posts()){
				while ( $posts_array->have_posts() ) : $posts_array->the_post();?>

					<article <?php post_class('post'); ?>>

						<div class="post-media">

							<?php if (has_post_thumbnail() && $show_thumb){
								$thumb_url = get_post_thumbnail_id();
								$img_url = wp_get_attachment_url($thumb_url, 'full'); //get img URL
								$article_image = aq_resize($img_url, 1200, 500, true);
							?>

							<div class="entry-thumb">
								<a href="<?php echo get_the_permalink();?>" class="more-link">
									<img src="<?php echo $article_image ?>" style="margin:0 0;" alt="<?php echo get_the_title();?>" title="<?php echo get_the_title();?>">
								</a>
							</div>

							<?php }?>

						</div>

						<?php if ($show_add_info == 'true'):?>

							<time class="updated" datetime="<?php echo get_the_time('c'); ?>">
								<span class="day"><?php echo get_the_date('d'); ?></span>
								<span class="month"><?php echo get_the_date('M'); ?></span>
							</time>

						<?php endif;?>

						<div class="ovh">

							<header class="entry-title">
								<?php if ($show_add_info == 'true'):?>
									<?php

									get_template_part('templates/entry-meta');

									?>
								<?php endif;?>
								<h2><a href="<?php echo get_the_permalink(); ?>"><?php echo get_the_title(); ?></a></h2>
							</header>

							<div class="entry-content">
								<?php echo get_the_excerpt();?>
							</div>
						</div>

					</article>

				<?php endwhile;
			}

			wp_reset_query();

			echo $after_widget;

		}

		function update($old_instance, $new_instance){

			$instance = $old_instance;
			$instance['title'] = $new_instance['title'];
			$instance['posts_number'] = $new_instance['posts_number'];
			$instance['show_thumb'] = $new_instance['show_thumb'];
			$instance['show_meta'] = $new_instance['show_meta'];
			$instance['show_excerpt'] = $new_instance['show_excerpt'];

			return $instance;
		}

		function form($instance){

			$posts_number = $instance['posts_number'];
			$title = $instance['title'];

			?>

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

			<p>
				<label for="<?php echo $this->get_field_id('posts_number'); ?>"><?php _e('Number of posts', 'crum'); ?></label>
				<input class="widefat" id="<?php echo $this->get_field_id('posts_number'); ?>" name="<?php echo $this->get_field_name('posts_number'); ?>" type="text" value="<?php echo $posts_number; ?>"/>
			</p>

			<p>
				<label for="<?php echo $this->get_field_id( 'show_thumb' ); ?>"><?php _e("Show thumbnail","crum")?></label>
				<input id="<?php echo $this->get_field_id( 'show_thumb' ); ?>" name="<?php echo $this->get_field_name( 'show_thumb' ); ?>" value="true" <?php if( $instance['show_thumb'] ) echo 'checked="checked"'; ?> type="checkbox" />
			</p>

			<p>
				<label for="<?php echo $this->get_field_id( 'show_meta' ); ?>"><?php _e("Show additional info","crum")?></label>
				<input id="<?php echo $this->get_field_id( 'show_meta' ); ?>" name="<?php echo $this->get_field_name( 'show_meta' ); ?>" value="true" <?php if( $instance['show_meta'] ) echo 'checked="checked"'; ?> type="checkbox" />
			</p>

			<p>
				<label for="<?php echo $this->get_field_id( 'show_excerpt' ); ?>"><?php _e("Show excerpt","crum")?></label>
				<input id="<?php echo $this->get_field_id( 'show_excerpt' ); ?>" name="<?php echo $this->get_field_name( 'show_excerpt' ); ?>" value="true" <?php if( $instance['show_excerpt'] ) echo 'checked="checked"'; ?> type="checkbox" />
			</p>

			<?php
		}

	}

}

if ( class_exists('crumina_recent_posts') ) {
	add_action( 'widgets_init', create_function( '', 'register_widget("crumina_recent_posts");' ) );
}