<?php   
/* 
Plugin Name: Multiple category
Description: Multiple category posts from 4 diferenet categories.
Version: 1.0 
Author: Stefan Naumovski 
*/    
add_action( 'widgets_init', 'featured_category_sqare_widget' );

function featured_category_sqare_widget() {register_widget( 'featured_category_sqare' );}

class featured_category_sqare extends WP_Widget {
	
	/* Register widget with WordPress. */
	
	function __construct() {
		parent::__construct(
			'featured_sqare_posts_tn', //Widget ID
			'Multiple categories', //Name
			array( 'description' => 'Multiple category - Latest posts from selected categories.', ) //Args
		);}
		
		/* Front-end display of widget. */
		
		public function widget( $args, $instance ) {
			$title = apply_filters( 'widget_title', $instance['title'] );
			$category_one = $instance['category_one'];
			$category_two = $instance['category_two'];
			$category_three = $instance['category_three'];
			$category_four = $instance['category_four'];			
			echo $args['before_widget'];
			if ( ! empty( $title ) )
				echo $args['before_title'] . $title . $args['after_title'];
				?>

<div class="category-square">
	<ul class="featured-square">
		<?php $tn_posts = new WP_Query(array( 'cat' => $category_one, 'posts_per_page' => 1, 'ignore_sticky_posts' => 1 )); while ( $tn_posts->have_posts()) : $tn_posts->the_post(); ?>
		<li>
			<div class="multiple-cat-image">
				<?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'); ?>
				</a>
				<?php } ?>
				<div class="multiple-cat-text">
					<div class="multiple-cat-title">
						<a href="<?php the_permalink() ?>">
						<?php $tit = the_title('','',FALSE); echo substr($tit, 0, 40); if (strlen($tit) > 40) echo " ..."; ?>
						</a>
					</div><!--multiple-cat-title-->
				</div><!--multiple-cat-text-->
			</div><!--multiple-cat-image-->
		</li>
		<?php endwhile; ?>
	</ul>
	<ul class="featured-category-sqare-links">
		<?php $tn_posts = new WP_Query(array( 'cat' => $category_one, 'posts_per_page' => 4, 'offset' => '1' )); while ( $tn_posts->have_posts()) : $tn_posts->the_post(); ?>
		<li>
			<h2>
				<a href="<?php the_permalink() ?>" class="small-featured-posts-title">
				<?php $tit = the_title('','',FALSE); echo substr($tit, 0, 45); if (strlen($tit) > 45) echo " ..."; ?>
				</a>
			</h2>
		</li>
		<?php endwhile; ?>
	</ul>
</div><!--category-square-->

<div class="category-square">
	<ul class="featured-square">
		<?php $tn_posts = new WP_Query(array( 'cat' => $category_two, 'posts_per_page' => 1, 'ignore_sticky_posts' => 1 )); while ( $tn_posts->have_posts()) : $tn_posts->the_post(); ?>
		<li>
			<div class="multiple-cat-image">
				<?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'); ?>
				</a>
				<?php } ?>
				<div class="multiple-cat-text">
					<div class="multiple-cat-title">
						<a href="<?php the_permalink() ?>">
						<?php $tit = the_title('','',FALSE); echo substr($tit, 0, 40); if (strlen($tit) > 40) echo " ..."; ?>
						</a>
					</div><!--multiple-cat-title-->
				</div><!--multiple-cat-text-->
			</div><!--multiple-cat-image-->
		</li>
		<?php endwhile; ?>
	</ul>
	<ul class="featured-category-sqare-links">
		<?php $tn_posts = new WP_Query(array( 'cat' => $category_two, 'posts_per_page' => 4, 'offset' => '1' )); while ( $tn_posts->have_posts()) : $tn_posts->the_post(); ?>
		<li>
			<h2>
				<a href="<?php the_permalink() ?>" class="small-featured-posts-title">
				<?php $tit = the_title('','',FALSE); echo substr($tit, 0, 45); if (strlen($tit) > 45) echo " ..."; ?>
				</a>
			</h2>
		</li>
		<?php endwhile; ?>
	</ul>
</div><!--category-square-->

<div class="category-square">
	<ul class="featured-square">
		<?php $tn_posts = new WP_Query(array( 'cat' => $category_three, 'posts_per_page' => 1, 'ignore_sticky_posts' => 1 )); while ( $tn_posts->have_posts()) : $tn_posts->the_post(); ?>
		<li>
			<div class="multiple-cat-image">
				<?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'); ?>
				</a>
				<?php } ?>
				<div class="multiple-cat-text">
					<div class="multiple-cat-title">
						<a href="<?php the_permalink() ?>">
						<?php $tit = the_title('','',FALSE); echo substr($tit, 0, 40); if (strlen($tit) > 40) echo " ..."; ?>
						</a>
					</div><!--multiple-cat-title-->
				</div><!--multiple-cat-text-->
			</div><!--multiple-cat-image-->
		</li>
		<?php endwhile; ?>
	</ul>
	<ul class="featured-category-sqare-links">
		<?php $tn_posts = new WP_Query(array( 'cat' => $category_three, 'posts_per_page' => 4, 'offset' => '1' )); while ( $tn_posts->have_posts()) : $tn_posts->the_post(); ?>
		<li>
			<h2>
				<a href="<?php the_permalink() ?>" class="small-featured-posts-title">
				<?php $tit = the_title('','',FALSE); echo substr($tit, 0, 45); if (strlen($tit) > 45) echo " ..."; ?>
				</a>
			</h2>
		</li>
		<?php endwhile; ?>
	</ul>
</div><!--category-square-->
<div class="category-square">
	<ul class="featured-square">
		<?php $tn_posts = new WP_Query(array( 'cat' => $category_four, 'posts_per_page' => 1, 'ignore_sticky_posts' => 1 )); while ( $tn_posts->have_posts()) : $tn_posts->the_post(); ?>
		<li>
			<div class="multiple-cat-image">
				<?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'); ?>
				</a>
				<?php } ?>
				<div class="multiple-cat-text">
					<div class="multiple-cat-title">
						<a href="<?php the_permalink() ?>">
						<?php $tit = the_title('','',FALSE); echo substr($tit, 0, 40); if (strlen($tit) > 40) echo " ..."; ?>
						</a>
					</div><!--multiple-cat-title-->
				</div><!--multiple-cat-text-->
			</div><!--multiple-cat-image-->
		</li>
		<?php endwhile; ?>
	</ul>
	<ul class="featured-category-sqare-links">
		<?php $tn_posts = new WP_Query(array( 'cat' => $category_four, 'posts_per_page' => 4, 'offset' => '1' )); while ( $tn_posts->have_posts()) : $tn_posts->the_post(); ?>
		<li>
			<h2>
				<a href="<?php the_permalink() ?>" class="small-featured-posts-title">
				<?php $tit = the_title('','',FALSE); echo substr($tit, 0, 45); if (strlen($tit) > 45) echo " ..."; ?>
				</a>
			</h2>
		</li>
		<?php endwhile; ?>
	</ul>
</div><!--category-square-->


<?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['category_one'] = $new_instance['category_one'];
		$instance['category_two'] = $new_instance['category_two'];
		$instance['category_three'] = $new_instance['category_three'];
		$instance['category_four'] = $new_instance['category_four'];		
		return $instance;
	}
	
	function form( $instance ) {
			
		/* Default widget settings. */
		
		$defaults = array( 'title' => 'Featured category');
		$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>
		
		<!-- cateogry-one -->
		<p>
			<label for="<?php echo $this->get_field_id('category_one'); ?>">Select Category: One</label>
			<select id="<?php echo $this->get_field_id('category_one'); ?>" name="<?php echo $this->get_field_name('category_one'); ?>" style="width:100%;">
				<option value='all' <?php if ('all' == (isset($instance['category_one']))) 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['category_one'])){ if ($category->term_id == $instance['category_one']) echo 'selected="selected"';}?>><?php echo $category->cat_name; ?></option>
				<?php } ?>
			</select>
		</p>
				
		<!-- cateogry-two -->
		<p>
			<label for="<?php echo $this->get_field_id('category_two'); ?>">Select Category: Two</label>
			<select id="<?php echo $this->get_field_id('category_two'); ?>" name="<?php echo $this->get_field_name('category_two'); ?>" style="width:100%;">
				<option value='all' <?php if ('all' == (isset($instance['category_two']))) 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['category_two'])){ if ($category->term_id == $instance['category_two']) echo 'selected="selected"';}?>><?php echo $category->cat_name; ?></option>
				<?php } ?>
			</select>
		</p>
		
		<!-- cateogry-three -->
		<p>
			<label for="<?php echo $this->get_field_id('category_three'); ?>">Select Category: Three</label>
			<select id="<?php echo $this->get_field_id('category_three'); ?>" name="<?php echo $this->get_field_name('category_three'); ?>" style="width:100%;">
				<option value='all' <?php if ('all' == (isset($instance['category_three']))) 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['category_three'])){ if ($category->term_id == $instance['category_three']) echo 'selected="selected"';}?>><?php echo $category->cat_name; ?></option>
				<?php } ?>
			</select>
		</p>
		
		<!-- cateogry-four -->
		<p>
			<label for="<?php echo $this->get_field_id('category_four'); ?>">Select Category: Four</label>
			<select id="<?php echo $this->get_field_id('category_four'); ?>" name="<?php echo $this->get_field_name('category_four'); ?>" style="width:100%;">
				<option value='all' <?php if ('all' == (isset($instance['category_four']))) 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['category_four'])){ if ($category->term_id == $instance['category_four']) echo 'selected="selected"';}?>><?php echo $category->cat_name; ?></option>
				<?php } ?>
			</select>
		</p>
<?php }}?>