From 087f2a7a7ce4eb145bea26024253763d38bcc3fa Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Tue, 28 Aug 2012 16:18:16 +0100 Subject: [PATCH] tidy up --- widgets/widget-layered_nav.php | 363 ++++++++++++++++----------------- 1 file changed, 180 insertions(+), 183 deletions(-) diff --git a/widgets/widget-layered_nav.php b/widgets/widget-layered_nav.php index e1426c11d3c..57e58b89510 100644 --- a/widgets/widget-layered_nav.php +++ b/widgets/widget-layered_nav.php @@ -24,16 +24,16 @@ class WooCommerce_Widget_Layered_Nav extends WP_Widget { function WooCommerce_Widget_Layered_Nav() { /* Widget variable settings. */ - $this->woo_widget_cssclass = 'widget_layered_nav'; - $this->woo_widget_description = __( 'Shows a custom attribute in a widget which lets you narrow down the list of products when viewing product categories.', 'woocommerce' ); - $this->woo_widget_idbase = 'woocommerce_layered_nav'; - $this->woo_widget_name = __('WooCommerce Layered Nav', 'woocommerce' ); + $this->woo_widget_cssclass = 'widget_layered_nav'; + $this->woo_widget_description = __( 'Shows a custom attribute in a widget which lets you narrow down the list of products when viewing product categories.', 'woocommerce' ); + $this->woo_widget_idbase = 'woocommerce_layered_nav'; + $this->woo_widget_name = __( 'WooCommerce Layered Nav', 'woocommerce' ); /* Widget settings. */ $widget_ops = array( 'classname' => $this->woo_widget_cssclass, 'description' => $this->woo_widget_description ); /* Create the widget. */ - $this->WP_Widget('woocommerce_layered_nav', $this->woo_widget_name, $widget_ops); + $this->WP_Widget( 'woocommerce_layered_nav', $this->woo_widget_name, $widget_ops ); } /** @@ -46,41 +46,40 @@ class WooCommerce_Widget_Layered_Nav extends WP_Widget { * @return void */ function widget( $args, $instance ) { - extract($args); - global $_chosen_attributes, $woocommerce, $_attributes_array; + + extract( $args ); - if ( !is_post_type_archive('product') && !is_tax( array_merge( $_attributes_array, array('product_cat', 'product_tag') ) ) ) return; + if ( ! is_post_type_archive( 'product' ) && ! is_tax( array_merge( $_attributes_array, array( 'product_cat', 'product_tag' ) ) ) ) + return; - $current_term = ($_attributes_array && is_tax($_attributes_array)) ? get_queried_object()->term_id : ''; - $current_tax = ($_attributes_array && is_tax($_attributes_array)) ? get_queried_object()->taxonomy : ''; + $current_term = $_attributes_array && is_tax( $_attributes_array ) ? get_queried_object()->term_id : ''; + $current_tax = $_attributes_array && is_tax( $_attributes_array ) ? get_queried_object()->taxonomy : ''; - $title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base); - $taxonomy = $woocommerce->attribute_taxonomy_name($instance['attribute']); - $query_type = (isset($instance['query_type'])) ? $instance['query_type'] : 'and'; - $display_type = (isset($instance['display_type'])) ? $instance['display_type'] : 'list'; + $title = apply_filters('widget_title', $instance['title'], $instance, $this->id_base); + $taxonomy = $woocommerce->attribute_taxonomy_name($instance['attribute']); + $query_type = isset( $instance['query_type'] ) ? $instance['query_type'] : 'and'; + $display_type = isset( $instance['display_type'] ) ? $instance['display_type'] : 'list'; - if (!taxonomy_exists($taxonomy)) return; + if ( ! taxonomy_exists( $taxonomy ) ) + return; - $args = array( - 'hide_empty' => '1' - ); - $terms = get_terms( $taxonomy, $args ); + $terms = get_terms( $taxonomy, array( 'hide_empty' => '1' ) ); - $count = count($terms); - - if($count > 0){ - - $found = false; + if ( count( $terms ) > 0 ) { + ob_start(); - + + $found = false; + echo $before_widget . $before_title . $title . $after_title; // Force found when option is selected - do not force found on taxonomy attributes - if ( !$_attributes_array || !is_tax($_attributes_array) ) - if (is_array($_chosen_attributes) && array_key_exists($taxonomy, $_chosen_attributes)) $found = true; + if ( ! $_attributes_array || ! is_tax( $_attributes_array ) ) + if ( is_array( $_chosen_attributes ) && array_key_exists( $taxonomy, $_chosen_attributes ) ) + $found = true; - if ($display_type=='dropdown') { + if ( $display_type == 'dropdown' ) { // skip when viewing the taxonomy if ( $current_tax && $taxonomy == $current_tax ) { @@ -89,21 +88,22 @@ class WooCommerce_Widget_Layered_Nav extends WP_Widget { } else { - $taxonomy_filter = str_replace('pa_', '', $taxonomy); + $taxonomy_filter = str_replace( 'pa_', '', $taxonomy ); $found = true; - echo ''; - echo ''; + echo ''; - foreach ($terms as $term) { + foreach ( $terms as $term ) { // If on a term page, skip that term in widget list - if( $term->term_id == $current_term ) continue; + if ( $term->term_id == $current_term ) + continue; // Get count based on current view - uses transients - $transient_name = 'wc_ln_count_' . md5( sanitize_key($taxonomy) . sanitize_key( $term->term_id ) ); + $transient_name = 'wc_ln_count_' . md5( sanitize_key( $taxonomy ) . sanitize_key( $term->term_id ) ); if ( false === ( $_products_in_term = get_transient( $transient_name ) ) ) { @@ -112,27 +112,30 @@ class WooCommerce_Widget_Layered_Nav extends WP_Widget { set_transient( $transient_name, $_products_in_term ); } - $option_is_set = (isset($_chosen_attributes[$taxonomy]) && in_array($term->term_id, $_chosen_attributes[$taxonomy]['terms'])); + $option_is_set = ( isset( $_chosen_attributes[ $taxonomy ] ) && in_array( $term->term_id, $_chosen_attributes[ $taxonomy ]['terms'] ) ); // If this is an AND query, only show options with count > 0 - if ($query_type=='and') { + if ( $query_type == 'and' ) { - $count = sizeof(array_intersect($_products_in_term, $woocommerce->query->filtered_product_ids)); + $count = sizeof( array_intersect( $_products_in_term, $woocommerce->query->filtered_product_ids ) ); - if ($count>0) $found = true; + if ( $count > 0 ) + $found = true; - if ($count==0 && !$option_is_set) continue; + if ( $count == 0 && ! $option_is_set ) + continue; // If this is an OR query, show all options so search can be expanded } else { - $count = sizeof(array_intersect($_products_in_term, $woocommerce->query->unfiltered_product_ids)); + $count = sizeof( array_intersect( $_products_in_term, $woocommerce->query->unfiltered_product_ids ) ); - if ($count>0) $found = true; + if ( $count > 0 ) + $found = true; } - echo ''; + echo ''; } echo ''; @@ -141,7 +144,7 @@ class WooCommerce_Widget_Layered_Nav extends WP_Widget { jQuery('#dropdown_layered_nav_$taxonomy_filter').change(function(){ - location.href = '".add_query_arg('filtering', '1', remove_query_arg('filter_' . $taxonomy_filter))."&filter_$taxonomy_filter=' + jQuery('#dropdown_layered_nav_$taxonomy_filter').val(); + location.href = '" . add_query_arg('filtering', '1', remove_query_arg('filter_' . $taxonomy_filter ) ) . "&filter_$taxonomy_filter=' + jQuery('#dropdown_layered_nav_$taxonomy_filter').val(); }); @@ -154,10 +157,10 @@ class WooCommerce_Widget_Layered_Nav extends WP_Widget { // List display echo "