class-product-cat-dropdown-walker.php
5 years ago
class-product-cat-list-walker.php
5 years ago
class-wc-product-cat-dropdown-walker.php
5 years ago
class-wc-product-cat-list-walker.php
5 years ago
class-wc-product-cat-list-walker.php
154 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WC_Product_Cat_List_Walker class |
| 4 | * |
| 5 | * @package WooCommerce\Classes\Walkers |
| 6 | * @version 3.4.0 |
| 7 | */ |
| 8 | |
| 9 | defined( 'ABSPATH' ) || exit; |
| 10 | |
| 11 | if ( class_exists( 'WC_Product_Cat_List_Walker', false ) ) { |
| 12 | return; |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Product cat list walker class. |
| 17 | */ |
| 18 | class WC_Product_Cat_List_Walker extends Walker { |
| 19 | |
| 20 | /** |
| 21 | * What the class handles. |
| 22 | * |
| 23 | * @var string |
| 24 | */ |
| 25 | public $tree_type = 'product_cat'; |
| 26 | |
| 27 | /** |
| 28 | * DB fields to use. |
| 29 | * |
| 30 | * @var array |
| 31 | */ |
| 32 | public $db_fields = array( |
| 33 | 'parent' => 'parent', |
| 34 | 'id' => 'term_id', |
| 35 | 'slug' => 'slug', |
| 36 | ); |
| 37 | |
| 38 | /** |
| 39 | * Starts the list before the elements are added. |
| 40 | * |
| 41 | * @see Walker::start_lvl() |
| 42 | * @since 2.1.0 |
| 43 | * |
| 44 | * @param string $output Passed by reference. Used to append additional content. |
| 45 | * @param int $depth Depth of category. Used for tab indentation. |
| 46 | * @param array $args Will only append content if style argument value is 'list'. |
| 47 | */ |
| 48 | public function start_lvl( &$output, $depth = 0, $args = array() ) { |
| 49 | if ( 'list' !== $args['style'] ) { |
| 50 | return; |
| 51 | } |
| 52 | |
| 53 | $indent = str_repeat( "\t", $depth ); |
| 54 | $output .= "$indent<ul class='children'>\n"; |
| 55 | } |
| 56 | |
| 57 | /** |
| 58 | * Ends the list of after the elements are added. |
| 59 | * |
| 60 | * @see Walker::end_lvl() |
| 61 | * @since 2.1.0 |
| 62 | * |
| 63 | * @param string $output Passed by reference. Used to append additional content. |
| 64 | * @param int $depth Depth of category. Used for tab indentation. |
| 65 | * @param array $args Will only append content if style argument value is 'list'. |
| 66 | */ |
| 67 | public function end_lvl( &$output, $depth = 0, $args = array() ) { |
| 68 | if ( 'list' !== $args['style'] ) { |
| 69 | return; |
| 70 | } |
| 71 | |
| 72 | $indent = str_repeat( "\t", $depth ); |
| 73 | $output .= "$indent</ul>\n"; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Start the element output. |
| 78 | * |
| 79 | * @see Walker::start_el() |
| 80 | * @since 2.1.0 |
| 81 | * |
| 82 | * @param string $output Passed by reference. Used to append additional content. |
| 83 | * @param object $cat Category. |
| 84 | * @param int $depth Depth of category in reference to parents. |
| 85 | * @param array $args Arguments. |
| 86 | * @param integer $current_object_id Current object ID. |
| 87 | */ |
| 88 | public function start_el( &$output, $cat, $depth = 0, $args = array(), $current_object_id = 0 ) { |
| 89 | $cat_id = intval( $cat->term_id ); |
| 90 | |
| 91 | $output .= '<li class="cat-item cat-item-' . $cat_id; |
| 92 | |
| 93 | if ( $args['current_category'] === $cat_id ) { |
| 94 | $output .= ' current-cat'; |
| 95 | } |
| 96 | |
| 97 | if ( $args['has_children'] && $args['hierarchical'] && ( empty( $args['max_depth'] ) || $args['max_depth'] > $depth + 1 ) ) { |
| 98 | $output .= ' cat-parent'; |
| 99 | } |
| 100 | |
| 101 | if ( $args['current_category_ancestors'] && $args['current_category'] && in_array( $cat_id, $args['current_category_ancestors'], true ) ) { |
| 102 | $output .= ' current-cat-parent'; |
| 103 | } |
| 104 | |
| 105 | $output .= '"><a href="' . get_term_link( $cat_id, $this->tree_type ) . '">' . apply_filters( 'list_product_cats', $cat->name, $cat ) . '</a>'; |
| 106 | |
| 107 | if ( $args['show_count'] ) { |
| 108 | $output .= ' <span class="count">(' . $cat->count . ')</span>'; |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Ends the element output, if needed. |
| 114 | * |
| 115 | * @see Walker::end_el() |
| 116 | * @since 2.1.0 |
| 117 | * |
| 118 | * @param string $output Passed by reference. Used to append additional content. |
| 119 | * @param object $cat Category. |
| 120 | * @param int $depth Depth of category. Not used. |
| 121 | * @param array $args Only uses 'list' for whether should append to output. |
| 122 | */ |
| 123 | public function end_el( &$output, $cat, $depth = 0, $args = array() ) { |
| 124 | $output .= "</li>\n"; |
| 125 | } |
| 126 | |
| 127 | /** |
| 128 | * Traverse elements to create list from elements. |
| 129 | * |
| 130 | * Display one element if the element doesn't have any children otherwise, |
| 131 | * display the element and its children. Will only traverse up to the max. |
| 132 | * depth and no ignore elements under that depth. It is possible to set the. |
| 133 | * max depth to include all depths, see walk() method. |
| 134 | * |
| 135 | * This method shouldn't be called directly, use the walk() method instead. |
| 136 | * |
| 137 | * @since 2.5.0 |
| 138 | * |
| 139 | * @param object $element Data object. |
| 140 | * @param array $children_elements List of elements to continue traversing. |
| 141 | * @param int $max_depth Max depth to traverse. |
| 142 | * @param int $depth Depth of current element. |
| 143 | * @param array $args Arguments. |
| 144 | * @param string $output Passed by reference. Used to append additional content. |
| 145 | * @return null Null on failure with no changes to parameters. |
| 146 | */ |
| 147 | public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) { |
| 148 | if ( ! $element || ( 0 === $element->count && ! empty( $args[0]['hide_empty'] ) ) ) { |
| 149 | return; |
| 150 | } |
| 151 | parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output ); |
| 152 | } |
| 153 | } |
| 154 |