<?php   
/* 
Plugin Name: Blog Posts 
Description: The newest posts widget displayed in blogroll. 
Version: 1.0 
Author: Stefan Naumovski 
*/    
add_action ( 'widgets_init', 'Newest_posts_widget' );

function Newest_posts_widget() {register_widget ( 'Newest_posts' );}

class Newest_posts extends WP_Widget {
	
	/* Register widget with WordPress. */

	function __construct() {
		parent::__construct ( 
		'Newest_posts_tn',	//Widget ID
		'Blog posts',	// Name
		array( 'description' => 'Blog posts - your latest posts displayed in blogroll.', ) // Args
	);}
	
		/* Front-end display of widget. */
	
	public function widget($args, $instance) {
		$title = apply_filters ( 'widget_title', $instance ['title'] );	
		$number = $instance ['number'];
		$categories = $instance ['categories'];
		
		echo $args ['before_widget'];
			if (! empty( $title ))
				echo $args['before_title'] . $title . $args['after_title'];?>

<ul class="newest-posts-category">
	<?php $tn_posts = new WP_Query(array( 'cat' => $categories, 'posts_per_page' => $number )); while ( $tn_posts->have_posts()) : $tn_posts->the_post(); ?>
	<li <?php post_class(); ?>>
		<div class="newest-posts-thumb">
			<?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) { ?>
			<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
			<?php the_post_thumbnail('carousel-thumb'); if ( 'video' == get_post_format() ): echo '<span class="play-icon"></span>'; endif; ?>
			</a>
			<?php } ?>
		</div><!--newest-posts-thumb-->
		<div class="newest-posts-text">
		<span class="category-sidebar-news"><?php $category = get_the_category(); if($category[0]){echo '<a href="'.get_category_link($category[0]->term_id ).'">'.$category[0]->cat_name.'</a>';} ?></span>
			<h2><a href="<?php the_permalink() ?>" class="newest-posts-title">
				<?php the_title(); ?>
				</a>
			</h2>
            <div class="newest-posts-content">
                <?php echo excerpt(37); ?>
            </div><!--newest-posts-content-->
			<div class="newest-posts-date-posted">
				<span class="bypostauthor">
				<?php the_author_posts_link(); ?>
				</span>
				<span class="date">
				<?php echo get_the_date(); ?>
				</span>
          		<span class="views-count">
					<?php echo get_views(get_the_ID()); ?>
                </span>
				<span class="comments-count">
					<?php comments_number( '0', '1', '%' ); ?>
                </span>
			</div><!--newset-posts-date-posted-->
        </div><!--newest-posts-text-->
	</li>
	<?php endwhile; ?>
</ul>

<?php		
	/* After widget. */
		
	echo $args ['after_widget'];
	}
	
		/* Widget settings. */
			
	function update($new_instance, $old_instance) {
		$instance = $old_instance;
		
		/* Strip tags. */
		
		$instance ['title'] = strip_tags ( $new_instance ['title'] );
		$instance ['number'] = strip_tags ( $new_instance ['number'] );
		$instance ['categories'] = $new_instance ['categories'];
		
		return $instance;
	}
	function form($instance) {
		
		/* Default widget settings. */

		$defaults = array( 'title' => 'Blog Posts', 'number' => 5);
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>
		
		<!-- Widget Title-->
		<p>
		  <label for="<?php echo $this->get_field_id( 'title' ); ?>">Title:</label>
		  <input id="<?php echo $this->get_field_id( 'title' ); ?>" name="<?php echo $this->get_field_name( 'title' ); ?>" value="<?php echo $instance['title']; ?>" style="width: 90%;" />
		</p>
		
		<!-- Number of posts -->
		<p>
		  <label for="<?php echo $this->get_field_id( 'number' ); ?>">Number of posts to show:</label>
		  <input
				id="<?php echo $this->get_field_id( 'number' ); ?>" name="<?php echo $this->get_field_name( 'number' ); ?>"	value="<?php echo $instance['number']; ?>" size="3" />
		</p>

		<!--Category -->
		<p>
			<label for="<?php echo $this->get_field_id('categories'); ?>">(Optional)Select Category:</label>
			<select id="<?php echo $this->get_field_id('categories'); ?>" name="<?php echo $this->get_field_name('categories'); ?>" style="width:100%;">
				<option value='all' <?php if ('all' == (isset($instance['categories']))) echo 'selected="selected"'; ?>>All Categories</option>
				<?php $categories = get_categories('hide_empty=0&depth=1&type=post'); ?>
				<?php foreach($categories as $category) { ?>
				<option value='<?php echo $category->term_id; ?>' <?php if(isset($instance['categories'])){ if ($category->term_id == $instance['categories']) echo 'selected="selected"';}?>><?php echo $category->cat_name; ?></option>
				<?php } ?>
			</select>
		</p>
<?php }} ?>