class-wc-product-cat-dropdown-walker.php 3.0 KB

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