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