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-dropdown-walker.php
103 lines
| 1 | <?php |
| 2 | /** |
| 3 | * WC_Product_Cat_Dropdown_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_Dropdown_Walker', false ) ) { |
| 12 | return; |
| 13 | } |
| 14 | |
| 15 | /** |
| 16 | * Product category dropdown walker class. |
| 17 | */ |
| 18 | class WC_Product_Cat_Dropdown_Walker extends Walker { |
| 19 | |
| 20 | /** |
| 21 | * What the class handles. |
| 22 | * |
| 23 | * @var string |
| 24 | */ |
| 25 | public $tree_type = 'category'; |
| 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_el() |
| 42 | * @since 2.1.0 |
| 43 | * |
| 44 | * @param string $output Passed by reference. Used to append additional content. |
| 45 | * @param object $cat Category. |
| 46 | * @param int $depth Depth of category in reference to parents. |
| 47 | * @param array $args Arguments. |
| 48 | * @param int $current_object_id Current object ID. |
| 49 | */ |
| 50 | public function start_el( &$output, $cat, $depth = 0, $args = array(), $current_object_id = 0 ) { |
| 51 | |
| 52 | if ( ! empty( $args['hierarchical'] ) ) { |
| 53 | $pad = str_repeat( ' ', $depth * 3 ); |
| 54 | } else { |
| 55 | $pad = ''; |
| 56 | } |
| 57 | |
| 58 | $cat_name = apply_filters( 'list_product_cats', $cat->name, $cat ); |
| 59 | $value = ( isset( $args['value'] ) && 'id' === $args['value'] ) ? $cat->term_id : $cat->slug; |
| 60 | $output .= "\t<option class=\"level-$depth\" value=\"" . esc_attr( $value ) . '"'; |
| 61 | |
| 62 | if ( $value === $args['selected'] || ( is_array( $args['selected'] ) && in_array( $value, $args['selected'], true ) ) ) { |
| 63 | $output .= ' selected="selected"'; |
| 64 | } |
| 65 | |
| 66 | $output .= '>'; |
| 67 | $output .= esc_html( $pad . $cat_name ); |
| 68 | |
| 69 | if ( ! empty( $args['show_count'] ) ) { |
| 70 | $output .= ' (' . absint( $cat->count ) . ')'; |
| 71 | } |
| 72 | |
| 73 | $output .= "</option>\n"; |
| 74 | } |
| 75 | |
| 76 | /** |
| 77 | * Traverse elements to create list from elements. |
| 78 | * |
| 79 | * Display one element if the element doesn't have any children otherwise, |
| 80 | * display the element and its children. Will only traverse up to the max. |
| 81 | * depth and no ignore elements under that depth. It is possible to set the. |
| 82 | * max depth to include all depths, see walk() method. |
| 83 | * |
| 84 | * This method shouldn't be called directly, use the walk() method instead. |
| 85 | * |
| 86 | * @since 2.5.0 |
| 87 | * |
| 88 | * @param object $element Data object. |
| 89 | * @param array $children_elements List of elements to continue traversing. |
| 90 | * @param int $max_depth Max depth to traverse. |
| 91 | * @param int $depth Depth of current element. |
| 92 | * @param array $args Arguments. |
| 93 | * @param string $output Passed by reference. Used to append additional content. |
| 94 | * @return null Null on failure with no changes to parameters. |
| 95 | */ |
| 96 | public function display_element( $element, &$children_elements, $max_depth, $depth, $args, &$output ) { |
| 97 | if ( ! $element || ( 0 === $element->count && ! empty( $args[0]['hide_empty'] ) ) ) { |
| 98 | return; |
| 99 | } |
| 100 | parent::display_element( $element, $children_elements, $max_depth, $depth, $args, $output ); |
| 101 | } |
| 102 | } |
| 103 |