<?php
/*
 *
 * @file: tax-categories-widget.php
 *
 *


Plugin Name: Tax Categories Widget Test
Plugin URI: http://core.trac.wordpress.org/ticket/17276
Description: This is just a plugin for testing - it symbolizes the hope and enthusiasm that in the near future relevant aspects of custom post types and custom taxonomies get integrated in with existing default WordPress features.
Author: squeeky
Version: 0.2
Author URI: http://mywebwizards.com/



	______________________________________________________
	******************************************************
	Tax / Categories widget 
	for relavant Taxonomies								*/

class Tax_Categories_Widget extends WP_Widget {

	function Tax_Categories_Widget() {
		$widget_ops = array( 'classname' => 'widget_categories', 'description' => __( "A list or dropdown of categories / and optiionally custom taxonomy categories" ) );
		$this->WP_Widget('tax_categories', __('* Tax / Categories'), $widget_ops);
	}

	function widget( $args, $instance ) {
		extract( $args );

		$title = apply_filters('widget_title', empty( $instance['title'] ) ? __( 'Custom Categories' ) : $instance['title'], $instance, $this->id_base);
		$taxcats = $instance['taxcats']; // *** $taxcats $instance 
		$c = $instance['count'] ? '1' : '0';
		$h = $instance['hierarchical'] ? '1' : '0';
		$d = $instance['dropdown'] ? '1' : '0';

		echo $before_widget;
		if ( $title )
			echo $before_title . $title . $after_title;


		// In case a custom taxonomy that this widget was set to use got deactivated, 
		// then SET it to use the widget's default / 'category' ( for Posts )
		if ( !taxonomy_exists($taxcats) )
			$taxcats = 'category';

		$cat_args = array('taxonomy' => $taxcats, 'orderby' => 'name', 'show_count' => $c, 'hierarchical' => $h);

		if ( $d ) {
			$cat_args['show_option_none'] = __('Select Category');
			wp_dropdown_categories(apply_filters('widget_categories_dropdown_args', $cat_args));
?>

<script type='text/javascript'>
/* <![CDATA[ */
	var dropdown = document.getElementById("cat");
	function onCatChange() {
		if ( dropdown.options[dropdown.selectedIndex].value > 0 ) {
			location.href = "<?php echo home_url(); ?>/?cat="+dropdown.options[dropdown.selectedIndex].value;
		}
	}
	dropdown.onchange = onCatChange;
/* ]]> */
</script>

<?php
		} else {
?>
		<ul>
<?php
		$cat_args['title_li'] = '';
		wp_list_categories(apply_filters('widget_categories_args', $cat_args));
?>
		</ul>
<?php
		}

		echo $after_widget;
	}

	function update( $new_instance, $old_instance ) {
		$instance = $old_instance;
		$instance['title'] = strip_tags($new_instance['title']);
		$instance['taxcats'] = $new_instance['taxcats']; // *** $taxcats $instance 
		$instance['count'] = !empty($new_instance['count']) ? 1 : 0;
		$instance['hierarchical'] = !empty($new_instance['hierarchical']) ? 1 : 0;
		$instance['dropdown'] = !empty($new_instance['dropdown']) ? 1 : 0;

		return $instance;
	}

	function form( $instance ) {

		//Defaults
		$instance = wp_parse_args( (array) $instance, array( 'title' => '') );
		$title = esc_attr( $instance['title'] );
		$taxcats = esc_attr( $instance['taxcats'] ); // *** $taxcats $instance 
		$count = isset($instance['count']) ? (bool) $instance['count'] :false;
		$hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
		$dropdown = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false;

?>

		<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:' ); ?></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>

<?php 


		/*	______________________________________________________
			******************************************************
			If custom hierarchical taxonomies exist...
			("custom categories") gather them in an array
			and provide an option to use them for this widget	*/


		// set arguments for custom hierarchical taxonomies
		$args = array(
			'hierarchical' => true,
			'public' => true,
			'_builtin' => false,
		);

		// IF custom hierarchical taxonomies exist 
		if( get_taxonomies( $args ) ) {

			// array to collect custom hierarchical taxonomies
			$custom_cats = (array) $custom_cats;

			// collect each custom hierarchical taxonomy
			foreach( get_taxonomies( $args ) as $custom_tax ) 
				$custom_cats[] = $custom_tax;

			$current_taxcats = $instance['taxcats']; // *** $taxcats $instance 

			// provide option to use the custom hierarchical taxonomies for this widget
?>

		<p><label for="<?php echo $this->get_field_id('taxcats'); ?>"><?php _e('Select taxonomy:') ?></label>
		<select class="widefat" id="<?php echo $this->get_field_id('taxcats'); ?>" name="<?php echo $this->get_field_name('taxcats'); ?>">
			<option value="category" <?php selected('category', $current_taxcats) ?>><?php _e('Categories ( default for posts )') ?></option>
<?php foreach ( $custom_cats as $taxcats ) : ?>
			<option value="<?php echo esc_attr($taxcats) ?>" <?php selected($taxcats, $current_taxcats) ?>><?php echo get_taxonomy($taxcats)->labels->name; ?></option>
<?php endforeach; ?>
		</select></p>

<?php

		}

?>

		<p><input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('dropdown'); ?>" name="<?php echo $this->get_field_name('dropdown'); ?>"<?php checked( $dropdown ); ?> />
		<label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e( 'Display as dropdown' ); ?></label><br />

		<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('count'); ?>" name="<?php echo $this->get_field_name('count'); ?>"<?php checked( $count ); ?> />
		<label for="<?php echo $this->get_field_id('count'); ?>"><?php _e( 'Show post counts' ); ?></label><br />

		<input type="checkbox" class="checkbox" id="<?php echo $this->get_field_id('hierarchical'); ?>" name="<?php echo $this->get_field_name('hierarchical'); ?>"<?php checked( $hierarchical ); ?> />
		<label for="<?php echo $this->get_field_id('hierarchical'); ?>"><?php _e( 'Show hierarchy' ); ?></label></p>

<?php

	}

}


/**
 * Register widget - Then call 'widgets_init' action 
 * after the widget has been registered.
 */
function taxcat_widget_init() {
	if ( !is_blog_installed() )
		return;

	register_widget('Tax_Categories_Widget');

	do_action('widgets_init');
}

add_action('init', 'taxcat_widget_init', 1);

?>
