<?php

/*******************************************************
*
*	Custom News Widget
*	By: Andre Gagnon
*	http://www.designcirc.us
*
*******************************************************/

// Initialize widget
add_action( 'widgets_init', 'ag_news_widgets' );

// Register widget
function ag_news_widgets() {
	register_widget( 'ag_news_widget' );
}

// Widget class
class ag_news_widget extends WP_Widget {

/*----------------------------------------------------------*/
/*	Set up the Widget
/*----------------------------------------------------------*/
	
	function __construct() {
	
		/* General widget settings */
		$widget_ops = array( 'classname' => 'ag_news_widget', 'description' => __('A widget that displays the latest post(s) from a category.', 'themewich') );

		/* Widget control settings */
		$control_ops = array( 'width' => 400, 'height' => 350, 'id_base' => 'ag_news_widget' );

		/* Create widget */
		parent::__construct( 'ag_news_widget', __('Custom News Widget', 'themewich'), $widget_ops, $control_ops );
	}

/*----------------------------------------------------------*/
/*	Display The Widget 
/*----------------------------------------------------------*/
	
	function widget( $args, $instance ) {
		extract( $args );
		
		$title = apply_filters('widget_title', $instance['title'] );

		/* Variables from settings. */
		$postnum = $instance['postnum'] ;
		$category = $instance['category'] ;
		
		/* Before widget (defined in functions.php). */
		echo wp_kses_post( $before_widget );

		/* Display The Widget */
		?>
		<?php 
			// First loop to display only my single, most recent sticky post
			// Display the widget title if one was input
			if ( $title )
				echo wp_kses_post( $before_title . $title . $after_title );

			$the_query = new WP_Query('cat='.$category.'&showposts='.$postnum); //or category_name=
			while ($the_query->have_posts()) : $the_query->the_post();
				$tw_categories = ag_get_cats_array(3);   // get all post categories
		?>
		            <div class="sidepost <?php if ( isset($tw_categories[0]->term_id) ) { echo 'highlight_category_' . $tw_categories[0]->term_id; } ?>">
		                <div class="articleinner">
		                    <h3><a href="<?php the_permalink(); ?>" title="<?php printf(__('Permanent Link to %s', 'themewich'), get_the_title()); ?>"><?php the_title(); ?></a></h3>
		                    <div class="tiny-details">
								<span class="author">
									<?php _e( 'By', 'themewich' );?> <span><?php tw_author_link('posts'); ?></span>
								</span>
								<span class="tw-bullet">&nbsp;&bull;&nbsp;</span>
								<?php echo ag_get_cats(3, true); ?>
							</div>
								<?php /* if the post has a WP 2.9+ Thumbnail */
		                        if ( (function_exists('has_post_thumbnail')) && (has_post_thumbnail()) ) : ?>
		                        <div class="thumbnailarea">
		                         	<a class="thumblink" title="<?php printf(__('Permanent Link to %s', 'themewich'), get_the_title()); ?>" href="<?php the_permalink(); ?>">
										<?php the_post_thumbnail('regular', array('class' => 'scale-with-grid')); /* post thumbnail settings configured in functions.php */ ?>
		                            </a>
		                        </div>
		                        <?php endif; ?>
		                     <?php global $more; $more=0; 
							// echo get_the_content_unformatted_more('<span class="more-link">' . __('Read More', 'themewich') . '</span>');
							the_excerpt(); ?>
		                     <div class="clear"></div>
		                </div>
		            </div>
		        
		    <?php endwhile;  wp_reset_query(); ?>

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

/*----------------------------------------------------------*/
/*	Update the Widget
/*----------------------------------------------------------*/
	
	function update( $new_instance, $old_instance ) {
		
		$instance = $old_instance;
		
		/* Remove HTML: */
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['postnum'] = strip_tags( $new_instance['postnum'] );
		$instance['category'] = strip_tags( $new_instance['category'] );
	
		return $instance;
	}
	

/*----------------------------------------------------------*/
/*	Widget Settings
/*----------------------------------------------------------*/
	 
	function form( $instance ) {

		/* Default widget settings */
		$defaults = array(
		'title' => '',
		'postnum' => '',
		'category' => '',
		
		);
		$instance = wp_parse_args( (array) $instance, $defaults ); ?>

<p>
  <label for="<?php echo esc_attr( $this->get_field_id( 'title' ) ); ?>">
        <?php _e('News Title (Optional):', 'themewich') ?>
    </label>
    <input 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>
<p>
    <label for="<?php echo esc_attr( $this->get_field_id( 'postnum' ) ); ?>">
        <?php _e('Number of Posts:', 'themewich') ?>
    </label>
    <input class="widefat" id="<?php echo esc_attr( $this->get_field_id( 'postnum' ) ); ?>" name="<?php echo esc_attr( $this->get_field_name( 'postnum' ) ); ?>" value="<?php echo esc_attr( $instance['postnum'] ); ?>" />
</p>
<?php if ($displaycatname = $instance['category']) { echo '<p>Current Category: '.get_cat_name( $displaycatname ).'</p>'; }?> 
<p>
    <label for="<?php echo esc_attr( $this->get_field_id( 'category' ) ); ?>">
        <?php _e('Choose a Category:', 'themewich'); ?>
    </label>
   <?php wp_dropdown_categories( 'name='.$this->get_field_name( 'category' ).'&id='.$this->get_field_id( 'category' ).'&show_count=1');?>                             
</p>
<hr>
<?php
	}
}
?>