<?php
if (!function_exists('register_ef3_widget')) return;     

add_action('widgets_init', 'haswell_categories_widget');
function haswell_categories_widget(){
    register_ef3_widget('Cms_Categories_Widget');
}
class Cms_Categories_Widget extends WP_widget{
	function __construct() {
		$widget_ops = array('classname' => 'cms_categories_widget widget_categories', 'description' => esc_html__('A widget that displays your categories with post counts.', 'haswell') );
		$control_ops = array( 'id_base' => 'cms_categories_widget' );
		parent::__construct('cms_categories_widget', esc_html__('CMS Categories', 'haswell'), $widget_ops, $control_ops);
	}
	
	function widget($args,$instance) {
		extract($args);

		$title = apply_filters('widget_title', $instance['title'] );
		$category = $instance['category'];

		/* Before widget (defined by themes). */
		echo wp_kses_post($before_widget);
		if ( $title ) { echo wp_kses_post($before_title . $title . $after_title); }
		?>
			
		<ul>
			<?php
				$args = array (
					'echo' => 0,
					'show_option_all'    => '',
					'orderby'            => 'name',
					'show_count' => 1,
					'title_li' => '',
					'exclude'  => '' . $category . '',
					'depth' => 1
				);
				$variable = wp_list_categories($args);
				$variable = str_replace ( "(" , "<small>", $variable );
				$variable = str_replace ( ")" , "</small>", $variable );
				echo haswell_html($variable);
			?>
		</ul>
	<?php
		echo wp_kses_post($after_widget);
	}
	
	function update($new_instance, $old_instance){
		$instance = $old_instance;
		$instance['title'] = strip_tags( $new_instance['title'] );
		$instance['category'] = $new_instance['category'];
		
		return $instance;
	}
	
	function form($instance){
		$defaults = array('title' => 'Categories', 'category' => '100, 101');
		$instance = wp_parse_args((array) $instance, $defaults); ?>
		<p>
			<label for="<?php echo esc_attr($this->get_field_id( 'title' )); ?>"><?php esc_html_e('Title:', 'haswell') ?></label>
			<input class="widefat" type="text" 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( 'category' )); ?>"><?php esc_html_e('Exclude Categories', 'haswell') ?></label>
			<input class="widefat" type="text" id="<?php echo esc_attr($this->get_field_id( 'category' )); ?>" name="<?php echo esc_attr($this->get_field_name('category')); ?>" value="<?php echo esc_attr($instance['category']); ?>" />
		</p>
		<?php
	}
}