class-wc-product-cat-list-walker.php 4.5 KB

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