From c4b4086d8f969c1b1d74d7d7b13550e5cc4c3b23 Mon Sep 17 00:00:00 2001 From: Mike Jolley Date: Thu, 8 Dec 2011 00:22:38 +0000 Subject: [PATCH] Category walker should be more efficient. closes #340. --- woocommerce_taxonomy.php | 116 ++++++++++++++++----------------------- 1 file changed, 47 insertions(+), 69 deletions(-) diff --git a/woocommerce_taxonomy.php b/woocommerce_taxonomy.php index 4a486de95f8..c18966d3741 100644 --- a/woocommerce_taxonomy.php +++ b/woocommerce_taxonomy.php @@ -494,88 +494,66 @@ function get_woocommerce_term_meta($term_id, $key, $single = true){ return get_metadata('woocommerce_term', $term_id, $key, $single); } - /** * WooCommerce Dropdown categories * * Stuck with this until a fix for http://core.trac.wordpress.org/ticket/13258 + * We use a custom walker, just like WordPress does it */ function woocommerce_product_dropdown_categories( $show_counts = 1, $hierarchal = 1 ) { - $terms = get_terms('product_cat', 'pad_counts=1&hierarchal='.$hierarchal.'&hide_empty=1&child_of=0'); - if (!$terms) return; - $output = ""; + $output .= ''; + $output .= woocommerce_walk_category_dropdown_tree( $terms, 0, $r ); $output .=""; + echo $output; } -function woocommerce_get_product_category_depth( $id = '', $depth = '', $i = '', $show_counts = 1 ) { - global $wpdb, $term_taxonomy; - - if( $depth == '' ) : - - if( $id == '' ) $id = $term_taxonomy->term_id; - - $depth = $wpdb->get_var($wpdb->prepare("SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = '%s'", $id)); - return woocommerce_get_product_category_depth($id, $depth, $i); - - elseif( $depth == "0" ) : - - return $i; - - else : - - $depth = $wpdb->get_var($wpdb->prepare("SELECT parent FROM $wpdb->term_taxonomy WHERE term_id = '%s'", $depth)); - $i .='   '; - return woocommerce_get_product_category_depth($id, $depth, $i); - - endif; +/** + * Walk the Product Categories. + */ +function woocommerce_walk_category_dropdown_tree() { + $args = func_get_args(); + // the user's options are the third parameter + if ( empty($args[2]['walker']) || !is_a($args[2]['walker'], 'Walker') ) + $walker = new Woocommerce_Walker_CategoryDropdown; + else + $walker = $args[2]['walker']; + + return call_user_func_array(array( &$walker, 'walk' ), $args ); } -function woocommerce_get_product_category_children( $id = '', $show_counts = 1 ) { - global $wp_query; - - if (!$id) return; - - $output = ''; - - $terms = get_terms('product_cat', 'pad_counts=1&hierarchal=1&hide_empty=1&child_of='.esc_attr($id)); +/** + * Create HTML dropdown list of Product Categories. + */ +class Woocommerce_Walker_CategoryDropdown extends Walker { - foreach( $terms as $term ) : - if ($term->parent!=$id) continue; - - $depth = woocommerce_get_product_category_depth($term->term_id); - - if ( isset( $wp_query->query['product_cat'] ) ) : - $output .=""; - else : - $output .=""; - endif; - $output .= woocommerce_get_product_category_children($term->term_id); - - endforeach; + var $tree_type = 'category'; + var $db_fields = array ('parent' => 'parent', 'id' => 'term_id', 'slug' => 'slug' ); - return $output; + function start_el(&$output, $category, $depth, $args) { + $pad = str_repeat(' ', $depth * 3); + + $cat_name = apply_filters('list_product_cats', $category->name, $category); + $output .= "\t\n"; + } } \ No newline at end of file