<?php   
/* 
Plugin Name: Blog Posts  1
Description: The blog posts widget displayed in blogroll. 
Version: 1.0 
Author: Stefan Naumovski 
*/    

add_action( 'widgets_init', 'blog_category_widget' );

function blog_category_widget() {register_widget( 'blog_category_t1t' );}

class blog_category_t1t extends WP_Widget {
	
	/* Register widget with WordPress. */
	
	function __construct() {
		parent::__construct(
			'blog_category_t1t', // Widget ID
			'Blogroll 1', // Name
			array( 'description' =>'A widget, that displays your posts in a blog roll style. You can select how many posts you want to be displayed 
and of course, what category should they be. Also you can choose to hide/show the author of those posts, and add an alternative look to the 2/3 form of the widget. This alternative look makes the posts look in a classical blog style. This widget also has three different forms. It can be 1 column of posts(1/3), two columns(2/3), and three columns(full width).', ) // Args
			);}
		
		/* Front-end display of widget. */
		
		public function widget( $args, $instance ) {
			
		/* Default widget settings. */
			
			$defaults = array( 'title' => 'Blogroll1', 'number' => 4, 'offset' => 0, 'author' => 'on', 'date' => 'on', 'widget_size' => 'one-part', 'categories' => '0', 'excerptnumber'=>'30');
			$instance = wp_parse_args( (array) $instance, $defaults );
			
		/* Widget settings. */
		
			$title = $instance['title'];
			$categories = $instance['categories'];
			$number = $instance['number'];
			$offset = $instance['offset'];
			$excerptnumber = $instance['excerptnumber'];
			$author = $instance['author'];
			$date = $instance['date'];
			$widget_size = $instance['widget_size'];

			
			$args['before_widget'] = str_replace('class="home-widget', 'class="home-widget '. $widget_size , $args['before_widget']);			
			echo $args['before_widget'];
			if ( ! empty( $title ) ){
				echo $args['before_title'];		
					if($categories != 0){echo '<a href='.get_category_link( $categories ).'>';}		
				echo $title; 			
					if($categories != 0){echo '</a>';}
				echo $args['after_title'];}
			?>

<div class="blog-category">
	<ul>
		<?php $t1t_posts = new WP_Query(array( 'cat' => $categories, 'posts_per_page' => $number, 'offset' => $offset, 'ignore_sticky_posts' => 1 )); while ( $t1t_posts->have_posts()) : $t1t_posts->the_post(); ?>
		<li>		
			<div class="blog-post-image">
				<?php if (  (function_exists('has_post_thumbnail')) && (has_post_thumbnail())  ) { ?>
				<a href="<?php the_permalink(); ?>" title="<?php the_title(); ?>">
				<?php the_post_thumbnail('small-blog'); ?>
				</a>
				<?php } ?>
			</div>
			<!--blog-post-image-->
			<div class="blog-post-title-box">
				<div class="blog-post-title">
					<h2>
						<a href="<?php the_permalink() ?>">
						<?php the_title(); ?>
						</a>
					</h2>
				</div>
				<!--blog-post-title-->
			</div>
			<!--blog-post-title-box-->
			<?php if($author || $date ) { ?>
			<div class="blog-post-date-author">
				<?php if($author) { ?>
				<div class="blog-post-author">
					<?php the_author_posts_link(); ?>
				</div>
				<!--blog-post-author-->
				<?php } ?>
				<?php if($date) { ?>
				<div class="blog-post-date">
					<?php echo get_the_date(); ?>
				</div>
				<!--blog-post-date-->
				<?php } ?>
			</div>
			<!--blog-post-date-author-->
			<?php } ?>
			<div class="blog-post-content">
				<?php echo nl2br(excerpt($excerptnumber)); ?>
			</div>
			<!--blog-post-content-->
		</li>
		<?php endwhile; ?>
	</ul>
</div>
<!--blog-category-->
<?php

	/* After widget. */
	
	echo $args['after_widget'];
	}


	/* Widget settings. */


	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		
	/* Strip tags. */
		
		$instance['title'] = $new_instance['title'] ;
		$instance['number'] = $new_instance['number'];
		$instance['offset'] = $new_instance['offset'];
		$instance['excerptnumber'] = $new_instance['excerptnumber'];
		$instance['categories'] = $new_instance['categories'];
		$instance['author'] = $new_instance['author'];
		$instance['date'] = $new_instance['date'];
		$instance['widget_size'] = $new_instance['widget_size'];

		
		return $instance;
	}
	
	
	function form( $instance ) {
		
				/* Default widget settings. */
			$defaults = array( 'title' => 'Blogroll1', 'number' => 4, 'offset' => 0, 'author' => 'on', 'date' => 'on', 'widget_size' => 'one-part', 'categories' => '0', 'excerptnumber'=>'30');
			$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>

<!-- widget_size -->
<p>
	<label for="<?php echo $this->get_field_id( 'widget_size' ); ?>">
		Widget size:
	</label>
	</br>
	<input type="radio" name="<?php echo $this->get_field_name( 'widget_size' ); ?>" value="one-part" <?php checked('one-part', $instance['widget_size']); ?> class="one-part"/>
	<input type="radio" name="<?php echo $this->get_field_name( 'widget_size' ); ?>" value="two-parts" <?php checked('two-parts', $instance['widget_size']); ?> class="two-parts" />
	<input type="radio" name="<?php echo $this->get_field_name( 'widget_size' ); ?>" value="three-parts" <?php checked('three-parts', $instance['widget_size']); ?> class="three-parts"/>
	<input type="radio" name="<?php echo $this->get_field_name( 'widget_size' ); ?>" value="four-parts" <?php checked('four-parts', $instance['widget_size']); ?> class="four-parts"/>
</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>

<!-- 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>

<!-- Offset posts -->
<p>
	<label for="<?php echo $this->get_field_id( 'offset' ); ?>">
		Forward Posts(offset):
	</label>
	<input id="<?php echo $this->get_field_id( 'offset' ); ?>" name="<?php echo $this->get_field_name( 'offset' ); ?>" value="<?php echo $instance['offset']; ?>" size="3" />
</p>

<!-- Number of words -->
<p>
	<label for="<?php echo $this->get_field_id( 'excerptnumber' ); ?>">
		Number of words(max: 120):
	</label>
	<input id="<?php echo $this->get_field_id( 'excerptnumber' ); ?>" name="<?php echo $this->get_field_name( 'excerptnumber' ); ?>" value="<?php echo $instance['excerptnumber']; ?>" size="3" />
</p>

<!-- Author -->
<p>
	<label for="<?php echo $this->get_field_id( 'author' ); ?>">
		Show Author:
	</label>
	<input type="checkbox" id="<?php echo $this->get_field_id( 'author' ); ?>" name="<?php echo $this->get_field_name( 'author' ); ?>" <?php checked( (bool) $instance['author'], true ); ?> />
</p>

<!-- Date -->
<p>
	<label for="<?php echo $this->get_field_id( 'date' ); ?>">
		Show Date:
	</label>
	<input type="checkbox" id="<?php echo $this->get_field_id( 'date' ); ?>" name="<?php echo $this->get_field_name( 'date' ); ?>" <?php checked( (bool) $instance['date'], true ); ?> />
</p>

<?php }} ?>