woocommerce/classes/widgets/class-wc-widget-product-cat...

209 lines
7.7 KiB
PHP
Raw Normal View History

2012-05-26 16:25:07 +00:00
<?php
/**
* Product Categories Widget
2012-08-14 17:37:50 +00:00
*
* @author WooThemes
* @category Widgets
* @package WooCommerce/Widgets
* @version 1.6.4
* @extends WP_Widget
2012-05-26 16:25:07 +00:00
*/
if ( ! defined( 'ABSPATH' ) ) exit; // Exit if accessed directly
class WC_Widget_Product_Categories extends WP_Widget {
2012-05-26 16:25:07 +00:00
var $woo_widget_cssclass;
var $woo_widget_description;
var $woo_widget_idbase;
var $woo_widget_name;
var $cat_ancestors;
var $current_cat;
2012-08-14 17:37:50 +00:00
/**
* constructor
*
* @access public
* @return void
*/
function WC_Widget_Product_Categories() {
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
/* Widget variable settings. */
2012-11-15 17:46:24 +00:00
$this->woo_widget_cssclass = 'woocommerce widget_product_categories';
2012-05-26 16:25:07 +00:00
$this->woo_widget_description = __( 'A list or dropdown of product categories.', 'woocommerce' );
$this->woo_widget_idbase = 'woocommerce_product_categories';
2012-10-16 09:45:33 +00:00
$this->woo_widget_name = __( 'WooCommerce Product Categories', 'woocommerce' );
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
/* Widget settings. */
$widget_ops = array( 'classname' => $this->woo_widget_cssclass, 'description' => $this->woo_widget_description );
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
/* Create the widget. */
$this->WP_Widget('product_categories', $this->woo_widget_name, $widget_ops);
}
2012-08-14 17:37:50 +00:00
/**
* widget function.
*
* @see WP_Widget
* @access public
* @param array $args
* @param array $instance
* @return void
*/
2012-05-26 16:25:07 +00:00
function widget( $args, $instance ) {
extract( $args );
$title = apply_filters('widget_title', empty( $instance['title'] ) ? __( 'Product Categories', 'woocommerce' ) : $instance['title'], $instance, $this->id_base);
$c = $instance['count'] ? '1' : '0';
$h = $instance['hierarchical'] ? true : false;
$s = (isset($instance['show_children_only']) && $instance['show_children_only']) ? '1' : '0';
$d = $instance['dropdown'] ? '1' : '0';
$o = isset($instance['orderby']) ? $instance['orderby'] : 'order';
echo $before_widget;
if ( $title ) echo $before_title . $title . $after_title;
$cat_args = array( 'show_count' => $c, 'hierarchical' => $h, 'taxonomy' => 'product_cat' );
$cat_args['menu_order'] = false;
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
if ( $o == 'order' ) {
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
$cat_args['menu_order'] = 'asc';
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
} else {
2012-08-14 17:37:50 +00:00
2012-06-10 08:11:33 +00:00
$cat_args['orderby'] = 'title';
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
}
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
if ( $d ) {
// Stuck with this until a fix for http://core.trac.wordpress.org/ticket/13258
woocommerce_product_dropdown_categories( $c, $h, 0, $o );
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
?>
<script type='text/javascript'>
/* <![CDATA[ */
2012-09-14 15:33:07 +00:00
var product_cat_dropdown = document.getElementById("dropdown_product_cat");
function onProductCatChange() {
if ( product_cat_dropdown.options[product_cat_dropdown.selectedIndex].value !=='' ) {
location.href = "<?php echo home_url(); ?>/?product_cat="+product_cat_dropdown.options[product_cat_dropdown.selectedIndex].value;
2012-05-26 16:25:07 +00:00
}
}
2012-09-14 15:33:07 +00:00
product_cat_dropdown.onchange = onProductCatChange;
2012-05-26 16:25:07 +00:00
/* ]]> */
</script>
<?php
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
} else {
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
global $wp_query, $post, $woocommerce;
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
$this->current_cat = false;
$this->cat_ancestors = array();
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
if ( is_tax('product_cat') ) :
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
$this->current_cat = $wp_query->queried_object;
$this->cat_ancestors = get_ancestors( $this->current_cat->term_id, 'product_cat' );
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
elseif ( is_singular('product') ) :
2012-08-14 17:37:50 +00:00
$product_category = wp_get_post_terms( $post->ID, 'product_cat' );
2012-05-26 16:25:07 +00:00
if ($product_category) :
$this->current_cat = end($product_category);
$this->cat_ancestors = get_ancestors( $this->current_cat->term_id, 'product_cat' );
endif;
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
endif;
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
include_once( $woocommerce->plugin_path() . '/classes/walkers/class-product-cat-list-walker.php' );
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
$cat_args['walker'] = new WC_Product_Cat_List_Walker;
$cat_args['title_li'] = '';
$cat_args['show_children_only'] = ( isset( $instance['show_children_only'] ) && $instance['show_children_only'] ) ? 1 : 0;
$cat_args['pad_counts'] = 1;
2012-10-16 09:45:33 +00:00
$cat_args['show_option_none'] = __('No product categories exist.', 'woocommerce' );
2012-05-26 16:25:07 +00:00
$cat_args['current_category'] = ( $this->current_cat ) ? $this->current_cat->term_id : '';
$cat_args['current_category_ancestors'] = $this->cat_ancestors;
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
echo '<ul class="product-categories">';
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
wp_list_categories( apply_filters( 'woocommerce_product_categories_widget_args', $cat_args ) );
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
echo '</ul>';
}
echo $after_widget;
}
2012-08-14 17:37:50 +00:00
/**
* update function.
*
* @see WP_Widget->update
* @access public
* @param array $new_instance
* @param array $old_instance
* @return array
*/
2012-05-26 16:25:07 +00:00
function update( $new_instance, $old_instance ) {
$instance = $old_instance;
$instance['title'] = strip_tags($new_instance['title']);
$instance['orderby'] = strip_tags($new_instance['orderby']);
$instance['count'] = !empty($new_instance['count']) ? 1 : 0;
$instance['hierarchical'] = !empty($new_instance['hierarchical']) ? true : false;
$instance['dropdown'] = !empty($new_instance['dropdown']) ? 1 : 0;
$instance['show_children_only'] = !empty($new_instance['show_children_only']) ? 1 : 0;
return $instance;
}
2012-08-14 17:37:50 +00:00
/**
* form function.
*
* @see WP_Widget->form
* @access public
* @param array $instance
* @return void
*/
2012-05-26 16:25:07 +00:00
function form( $instance ) {
//Defaults
$instance = wp_parse_args( (array) $instance, array( 'title' => '') );
$title = esc_attr( $instance['title'] );
$orderby = isset( $instance['orderby'] ) ? $instance['orderby'] : 'order';
$count = isset($instance['count']) ? (bool) $instance['count'] :false;
$hierarchical = isset( $instance['hierarchical'] ) ? (bool) $instance['hierarchical'] : false;
$dropdown = isset( $instance['dropdown'] ) ? (bool) $instance['dropdown'] : false;
$show_children_only = isset( $instance['show_children_only'] ) ? (bool) $instance['show_children_only'] : false;
?>
<p><label for="<?php echo $this->get_field_id('title'); ?>"><?php _e( 'Title:', 'woocommerce' ); ?></label>
<input class="widefat" id="<?php echo esc_attr( $this->get_field_id('title') ); ?>" name="<?php echo esc_attr( $this->get_field_name('title') ); ?>" type="text" value="<?php echo esc_attr( $title ); ?>" /></p>
2012-08-14 17:37:50 +00:00
2012-10-16 09:45:33 +00:00
<p><label for="<?php echo $this->get_field_id('orderby'); ?>"><?php _e( 'Order by:', 'woocommerce' ) ?></label>
2012-05-26 16:25:07 +00:00
<select id="<?php echo esc_attr( $this->get_field_id('orderby') ); ?>" name="<?php echo esc_attr( $this->get_field_name('orderby') ); ?>">
2012-10-16 09:45:33 +00:00
<option value="order" <?php selected($orderby, 'order'); ?>><?php _e( 'Category Order', 'woocommerce' ); ?></option>
<option value="name" <?php selected($orderby, 'name'); ?>><?php _e( 'Name', 'woocommerce' ); ?></option>
2012-05-26 16:25:07 +00:00
</select></p>
<p><input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id('dropdown') ); ?>" name="<?php echo esc_attr( $this->get_field_name('dropdown') ); ?>"<?php checked( $dropdown ); ?> />
<label for="<?php echo $this->get_field_id('dropdown'); ?>"><?php _e( 'Show as dropdown', 'woocommerce' ); ?></label><br />
<input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id('count') ); ?>" name="<?php echo esc_attr( $this->get_field_name('count') ); ?>"<?php checked( $count ); ?> />
<label for="<?php echo $this->get_field_id('count'); ?>"><?php _e( 'Show post counts', 'woocommerce' ); ?></label><br />
<input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id('hierarchical') ); ?>" name="<?php echo esc_attr( $this->get_field_name('hierarchical') ); ?>"<?php checked( $hierarchical ); ?> />
<label for="<?php echo $this->get_field_id('hierarchical'); ?>"><?php _e( 'Show hierarchy', 'woocommerce' ); ?></label><br/>
2012-08-14 17:37:50 +00:00
2012-05-26 16:25:07 +00:00
<input type="checkbox" class="checkbox" id="<?php echo esc_attr( $this->get_field_id('show_children_only') ); ?>" name="<?php echo esc_attr( $this->get_field_name('show_children_only') ); ?>"<?php checked( $show_children_only ); ?> />
2012-08-23 10:04:53 +00:00
<label for="<?php echo $this->get_field_id('show_children_only'); ?>"><?php _e( 'Only show children for the current category', 'woocommerce' ); ?></label></p>
2012-05-26 16:25:07 +00:00
<?php
}
}