<?php
/* Kentooz Framework widget for recent posts. */

// Do not load directly...
if ( ! defined( 'ABSPATH' ) ) { die( 'Direct access forbidden.' ); }

class ktz_recent_posts extends WP_Widget {
	function __construct() {
		$widget_ops = array( 'classname' => 'ktz_recent_post clearfix', 'description' => __( 'Recent posts with category selection.',ktz_admin_textdomain ) );
		parent::__construct('ktz-recent-posts', __( 'KTZ Recent Posts',ktz_admin_textdomain ), $widget_ops);
		$this->alt_option_name = 'ktz_recent';
		add_action( 'save_post', array(&$this, 'flush_widget_cache') );
		add_action( 'deleted_post', array(&$this, 'flush_widget_cache') );
		add_action( 'switch_theme', array(&$this, 'flush_widget_cache') );
	}
	function widget($args, $instance) {
		$cache = wp_cache_get('widget_recent_posts', 'widget');
		if ( !is_array($cache) )
			$cache = array();
		if ( isset($cache[$args['widget_id']]) ) {
			echo $cache[$args['widget_id']];
			return;
		}
		extract($args);
		$hide_thumb= isset($instance['hide_thumb']) ? $instance['hide_thumb'] : false;
		$itemexcerpt= isset($instance['itemexcerpt']) ? $instance['itemexcerpt'] : false;
		$cats = empty( $instance['cats'] ) ? '' : $instance['cats'];
		$title = apply_filters('widget_title', empty($instance['title']) ? __( 'Recent Posts',ktz_admin_textdomain) : $instance['title']);
		if ( !$number = (int) $instance['number'] )
			$number = 10;
		else if ( $number < 1 )
			$number = 1;
		else if ( $number > 15 )
			$number = 15;
		$ktzrecent = new WP_Query(array('showposts' => $number, 'nopaging' => 0, 'post_status' => 'publish', 'ignore_sticky_posts' => 1, 'cat' => $cats));
		if ($ktzrecent->have_posts()) : 
		echo $before_widget; 
        if ( $title ) echo $before_title . $title . $after_title;
			$count = 0; 
			while ($ktzrecent->have_posts()) : $ktzrecent->the_post();
					global $post;
					echo '<div class="module-2 clearfix ';
					$count++; if ( $count <= 1 ) { echo 'module-2first'; } else { echo 'module-2general'; }
					echo '">';
					if ( $hide_thumb == false) { 
						echo ktz_featured_img( 70, 70, 'thumbnail btn-box pull-left' ); // width, height, class
					} 
					echo ktz_posted_on();
					echo '<span class="borderview">/</span><span class="view">';
					echo ktz_getPostViews(get_the_ID());
					echo '</span>';
					echo '<div class="ktz-titlecust"><a href="' . get_permalink() . '" title="Go to ' . get_the_title() . '">' . get_the_title() . '</a></div>';
					if( $itemexcerpt == false ) { 
					echo ktz_get_excerpt(7); }
					echo '</div>'; 
				    endwhile; 
			wp_reset_query();  
			endif;
			echo $after_widget;

		$cache[$args['widget_id']] = ob_get_flush();
		wp_cache_add('widget_recent_posts', $cache, 'widget');
	}

	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['number'] = (int) $new_instance['number'];
		$instance['cats'] = (int) $new_instance['cats'];
		$instance['hide_thumb'] = isset($new_instance['hide_thumb']) ? true : false;
		$instance['itemexcerpt'] = isset($new_instance['itemexcerpt']) ? true : false;
		$this->flush_widget_cache();
		$alloptions = wp_cache_get( 'alloptions', 'options' );
		if ( isset($alloptions['ktz_recent']) )
		delete_option('ktz_recent');
		return $instance;
	}

	function flush_widget_cache() {
		wp_cache_delete('widget_recent_posts', 'widget');
	}

	function form( $instance ) {
		$instance = wp_parse_args( (array) $instance, array( 'title' => '', 'hide_thumb' => false,'itemexcerpt' => false, 'cats' => '') );
		$title = esc_attr( $instance['title'] );
		$cats = esc_attr( $instance['cats'] );
		if ( !isset($instance['number']) || !$number = (int) $instance['number'] )
			$number = 5; ?>
		<p><label for="<?php echo $this->get_field_id('title',ktz_admin_textdomain ); ?>"><?php _e( 'Title:',ktz_admin_textdomain ); ?></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 $title; ?>" />
        </p>
		<p><label for="<?php echo $this->get_field_id('number',ktz_admin_textdomain ); ?>"><?php _e( 'Number of posts to show:'); ?></label>
		<input id="<?php echo $this->get_field_id('number'); ?>" name="<?php echo $this->get_field_name('number'); ?>" type="text" value="<?php echo $number; ?>" size="3" /><br />
		<small><?php _e( '(at most 15)',ktz_admin_textdomain ); ?></small>
        </p>
		<p><label for="<?php echo $this->get_field_id('cats'); ?>"><?php _e( 'Fill with Category ID:',ktz_theme_textdomain); ?></label>
		<select id="<?php echo $this->get_field_id('cats'); ?>" name="<?php echo $this->get_field_name('cats'); ?>" class="widefat">
		<option value="" <?php selected( $instance['cats'], '' ); ?>><?php _e('All', ktz_theme_textdomain); ?></option>
		<?php $blog_categories = get_categories( array('orderby' => 'id') ); foreach( $blog_categories as $category ): ?>
		<option value="<?php echo $category->term_id; ?>" <?php selected( $instance['cats'], $category->term_id ); ?>><?php echo $category->name; ?></option>
		<?php endforeach; ?>
		</select>
		<br />
		<small><?php _e('Please select category for display in your widget', ktz_theme_textdomain); ?></small>
		</p>
		<p><label for="<?php echo $this->get_field_id( 'hide_thumb' ); ?>"><?php _e( 'Hide Thumbnails?',ktz_admin_textdomain); ?></label>
        <input class="checkbox" type="checkbox" <?php checked($instance['hide_thumb'], true) ?> id="<?php echo $this->get_field_id('hide_thumb'); ?>" name="<?php echo $this->get_field_name( 'hide_thumb' ); ?>" /><br />
        <small><?php _e( 'If unchecked, it will show post thumbnails.',ktz_admin_textdomain); ?></small>
        </p>
		<p><label for="<?php echo $this->get_field_id( 'itemexcerpt' ); ?>"><?php _e( 'Hide Excerpt Post?',ktz_admin_textdomain); ?></label>
        <input class="checkbox" type="checkbox" <?php checked($instance['itemexcerpt'], true) ?> id="<?php echo $this->get_field_id('itemexcerpt'); ?>" name="<?php echo $this->get_field_name( 'itemexcerpt' ); ?>" /><br />
        <small><?php _e( 'If unchecked, it will show post excerpt/desc.',ktz_admin_textdomain); ?></small>
        </p>
	<?php }
}