class-acf-walker-taxonomy-field.php 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. if ( ! defined( 'ABSPATH' ) ) {
  3. exit; // Exit if accessed directly
  4. }
  5. if ( ! class_exists( 'ACF_Taxonomy_Field_Walker' ) ) :
  6. class ACF_Taxonomy_Field_Walker extends Walker {
  7. /**
  8. * What the class handles.
  9. *
  10. * @since 2.1.0
  11. * @var string
  12. */
  13. public $tree_type = 'category';
  14. /**
  15. * DB fields to use.
  16. *
  17. * @since 2.1.0
  18. * @var array
  19. */
  20. public $db_fields = array(
  21. 'parent' => 'parent',
  22. 'id' => 'term_id',
  23. );
  24. /**
  25. * The field being rendered.
  26. *
  27. * @since 1.0.0
  28. * @var array
  29. */
  30. public $field;
  31. /**
  32. * Constructor
  33. *
  34. * @date 20/4/21
  35. * @since 1.0.0
  36. *
  37. * @param array $field The field being rendered.
  38. * @return void
  39. */
  40. function __construct( $field ) {
  41. $this->field = $field;
  42. }
  43. /**
  44. * Starts the list before the elements are added.
  45. *
  46. * @see Walker:start_lvl()
  47. *
  48. * @since 1.0.0
  49. *
  50. * @param string $output Used to append additional content (passed by reference).
  51. * @param int $depth Depth of category. Used for tab indentation.
  52. * @param array $args An array of arguments. @see wp_terms_checklist()
  53. */
  54. public function start_lvl( &$output, $depth = 0, $args = array() ) {
  55. $indent = str_repeat( "\t", $depth );
  56. $output .= "$indent<ul class='children acf-bl'>\n";
  57. }
  58. /**
  59. * Ends the list of after the elements are added.
  60. *
  61. * @see Walker::end_lvl()
  62. *
  63. * @since 1.0.0
  64. *
  65. * @param string $output Used to append additional content (passed by reference).
  66. * @param int $depth Depth of category. Used for tab indentation.
  67. * @param array $args An array of arguments. @see wp_terms_checklist()
  68. */
  69. public function end_lvl( &$output, $depth = 0, $args = array() ) {
  70. $indent = str_repeat( "\t", $depth );
  71. $output .= "$indent</ul>\n";
  72. }
  73. /**
  74. * Start the element output.
  75. *
  76. * @see Walker::start_el()
  77. *
  78. * @since 1.0.0
  79. *
  80. * @param string $output Used to append additional content (passed by reference).
  81. * @param WP_Term $term The current term object.
  82. * @param int $depth Depth of the term in reference to parents. Default 0.
  83. * @param array $args An array of arguments. @see wp_terms_checklist()
  84. * @param int $id ID of the current term.
  85. */
  86. public function start_el( &$output, $term, $depth = 0, $args = array(), $id = 0 ) {
  87. $is_selected = in_array( $term->term_id, $this->field['value'] );
  88. // Generate array of checkbox input attributes.
  89. $input_attrs = array(
  90. 'type' => $this->field['field_type'],
  91. 'name' => $this->field['name'],
  92. 'value' => $term->term_id,
  93. );
  94. if ( $is_selected ) {
  95. $input_attrs['checked'] = true;
  96. }
  97. $output .= "\n" . '<li data-id="' . esc_attr( $term->term_id ) . '">' .
  98. '<label' . ( $is_selected ? ' class="selected"' : '' ) . '>' .
  99. '<input ' . acf_esc_attrs( $input_attrs ) . '/> ' .
  100. '<span>' . acf_esc_html( $term->name ) . '</span>' .
  101. '</label>';
  102. }
  103. /**
  104. * Ends the element output, if needed.
  105. *
  106. * @see Walker::end_el()
  107. *
  108. * @since 1.0.0
  109. *
  110. * @param string $output Used to append additional content (passed by reference).
  111. * @param WP_Term $category The current term object.
  112. * @param int $depth Depth of the term in reference to parents. Default 0.
  113. * @param array $args An array of arguments. @see wp_terms_checklist()
  114. */
  115. public function end_el( &$output, $category, $depth = 0, $args = array() ) {
  116. $output .= "</li>\n";
  117. }
  118. }
  119. endif;