<?php
/**
 * Plugin Name: Latest Posts Widget
 */

class rilee_recent_posts_widget extends WP_Widget {

	/**
	 * Widget setup.
	 */
	function __construct() {
		/* Widget settings. */
		$widget_ops = array( 'classname' => 'rilee_recent_posts_widget', 'description' => esc_html__('A widget that displays your latest posts.', 'rilee') );

		/* Widget control settings. */
		$control_ops = array( 'width' => 250, 'height' => 350, 'id_base' => 'rilee_recent_posts_widget' );

		//Create the Widget 
		parent::__construct( 'rilee_recent_posts_widget', esc_html__('Rilee: Recent Posts', 'rilee'), $widget_ops, $control_ops);
	}

	/**
	 * How to display the widget on the screen.
	 */
	function widget( $args, $instance ) {
		extract( $args );

		/* Our variables from the widget settings. */
		$the_title = isset($instance['title']) ? $instance['title'] : 'Recent Posts';
		$title = apply_filters('widget_title', $the_title );
		$categories = isset($instance['categories']) ? $instance['categories'] : '';
		$number = isset($instance['number']) ? $instance['number'] : 3;
		$query = array();
		
		if($categories == 'all') {
			$query = array('posts_per_page' => $number, 'nopaging' => 0, 'post_status' => 'publish', 'ignore_sticky_posts' => 1);
		} else {
			$query = array('posts_per_page' => $number, 'nopaging' => 0, 'post_status' => 'publish', 'ignore_sticky_posts' => 1, 'category_name' => $categories);	
		}

		$loop = new WP_Query($query);
		if ($loop->have_posts()) :
		
		/* Before widget (defined by themes). */
		echo wp_kses_post($before_widget); ?>

		<div class="rilee-recent-posts-widget">

			<?php if ($title) {
				echo wp_kses_post($before_title . $title . $after_title);
			} ?>


			<ul class="rilee-side-newsfeed">
			
			<?php  while ($loop->have_posts()) : $loop->the_post(); 

			$has_no_thumbnail = !has_post_thumbnail() ? ' rilee-side-no-thumbnail' : ''; ?>
			
				<li>
					<div class="rilee-side-item <?php echo esc_attr($has_no_thumbnail); ?>">
										
						<div class="rilee-side-image">
							<a href="<?php echo esc_url(get_permalink()); ?>" rel="bookmark"><?php the_post_thumbnail('medium_large'); ?></a>
						</div>
						
						<div class="rilee-side-item-text <?php echo esc_attr($has_no_thumbnail); ?>">
								<h4 class="rilee-side-title"><a href="<?php echo esc_url(get_permalink()); ?>" rel="bookmark"><?php the_title(); ?></a></h4>	
								<span class="rilee-side-item-date"><?php the_time( get_option('date_format') ); ?></span>	
						</div>
					</div>
				
				</li>
			
			<?php endwhile; ?>
			<?php wp_reset_postdata(); ?>
			<?php endif; ?>
			
			</ul>

			</div>	
			
		<?php

		/* After widget (defined by themes). */
		echo wp_kses_post($after_widget);
	}

	/**
	 * Update the widget settings.
	 */
	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;

		/* Strip tags for title and name to remove HTML (important for text inputs). */
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['categories'] = $new_instance['categories'];
		$instance['number'] = strip_tags( $new_instance['number'] );

		return $instance;
	}


	function form( $instance ) {

		/* Set up some default widget settings. */
		$defaults = array( 'title' => 'Recent Posts', 'number' => 3, 'categories' => '');
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>

		<div id="recent_posts_widget_form" class="rilee_widget">

			<!-- Widget Title: Text Input -->
			<p>
				<label for="<?php echo esc_attr($this->get_field_id( 'title' )); ?>">Widget Title:</label><br />
				<input type="text" class="widefat" id="<?php echo esc_attr($this->get_field_id( 'title' )); ?>" name="<?php echo esc_attr($this->get_field_name( 'title' )); ?>" value="<?php echo esc_attr($instance['title']); ?>"/>
			</p>

			
			<!-- Category -->
			<p>
			<label for="<?php echo esc_attr($this->get_field_id('categories')); ?>"><?php echo esc_html__('Filter by Category:', 'rilee'); ?></label> 
			<select id="<?php echo esc_attr($this->get_field_id('categories')); ?>" name="<?php echo esc_attr($this->get_field_name('categories')); ?>" class="widefat categories" style="width:100%;">
				<option value='all' <?php if ('all' == $instance['categories']) echo 'selected="selected"'; ?>><?php echo esc_html__('All Categories', 'rilee'); ?></option>
				<?php $categories = get_categories('hide_empty=1&depth=1&type=post'); ?>
				<?php foreach($categories as $category) { ?>
				<option value='<?php echo esc_attr($category->slug); ?>' <?php if ($category->slug == $instance['categories']) echo 'selected="selected"'; ?>><?php echo esc_html($category->cat_name); ?></option>
				<?php } ?>
			</select>
			</p>
			
			<!-- Number of posts -->
			<p>
				<label for="<?php echo esc_attr($this->get_field_id( 'number' )); ?>"><?php echo esc_html__('Number of posts to show:', 'rilee'); ?></label>
				<input type="text" class="widefat" id="<?php echo esc_attr($this->get_field_id( 'number' )); ?>" name="<?php echo esc_attr($this->get_field_name( 'number' )); ?>" value="<?php echo esc_attr($instance['number']); ?>" size="3" />
			</p>

		</div>	

	<?php
	}
}


add_action( 'widgets_init', 'rilee_recent_posts_load_widget' );

function rilee_recent_posts_load_widget() {
	register_widget( 'rilee_recent_posts_widget' );
} ?>