-
Notifications
You must be signed in to change notification settings - Fork 37
/
walker.taxonomy-single-term.php
165 lines (149 loc) · 4.84 KB
/
walker.taxonomy-single-term.php
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
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
<?php
if ( ! class_exists( 'Taxonomy_Single_Term_Walker' ) && class_exists( 'Walker' ) ) :
/**
* Walker to output an unordered list of taxonomy radio <input> elements.
*
* @see Walker
* @see wp_category_checklist()
* @see wp_terms_checklist()
* @since 0.1.2
*/
class Taxonomy_Single_Term_Walker extends Walker {
public $tree_type = 'category';
public $db_fields = array( 'parent' => 'parent', 'id' => 'term_id' ); //TODO: decouple this
public function __construct( $hierarchical, $input_element ) {
$this->hierarchical = $hierarchical;
$this->input_element = $input_element;
}
/**
* Starts the list before the elements are added.
*
* @see Walker:start_lvl()
*
* @since 0.1.2
*
* @param string $output Passed by reference. Used to append additional content.
* @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() ) {
if ( 'radio' == $this->input_element ) {
$indent = str_repeat("\t", $depth);
$output .= "$indent<ul class='children'>\n";
}
}
/**
* Ends the list of after the elements are added.
*
* @see Walker::end_lvl()
*
* @since 0.1.2
*
* @param string $output Passed by reference. Used to append additional content.
* @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() ) {
if ( 'radio' == $this->input_element ) {
$indent = str_repeat("\t", $depth);
$output .= "$indent</ul>\n";
}
}
/**
* Start the element output.
*
* @see Walker::start_el()
*
* @since 0.1.2
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $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 ) {
$taxonomy = empty( $args['taxonomy'] ) ? 'category' : $args['taxonomy'];
$name = $taxonomy == 'category' ? 'post_category' : 'tax_input['.$taxonomy.']';
// input name
$name = $this->hierarchical ? $name .'[]' : $name;
// input value
$value = $this->hierarchical ? $term->term_id : $term->slug;
$selected_cats = empty( $args['selected_cats'] ) ? array() : $args['selected_cats'];
$in_selected = in_array( $term->term_id, $selected_cats );
$args = array(
'id' => esc_attr( $taxonomy .'-'. $term->term_id ),
'name' => esc_attr( $name ),
'value' => esc_attr( $value ),
'checked' => checked( $in_selected, true, false ),
'selected' => selected( $in_selected, true, false ),
'disabled' => disabled( empty( $args['disabled'] ), false, false ),
'label' => esc_html( apply_filters('the_category', $term->name ) ),
'depth' => $depth,
);
$output .= 'radio' == $this->input_element
? $this->start_el_radio( $args )
: $this->start_el_select( $args );
}
/**
* Creates the opening markup for the radio input
*
* @since 0.2.0
*
* @param array $args Array of arguments for creating the element
*
* @return string Opening li element and radio input
*/
public function start_el_radio( $args ) {
return "\n".sprintf(
'<li id="%s"><label class="selectit"><input value="%s" type="radio" name="%s" id="in-%s" %s %s/>%s</label>',
$args['id'],
$args['value'],
$args['name'],
$args['id'],
$args['checked'],
$args['disabled'],
$args['label']
);
}
/**
* Creates the opening markup for the select input
*
* @since 0.2.0
*
* @param array $args Array of arguments for creating the element
*
* @return string Opening option element and option text
*/
public function start_el_select( $args ) {
$pad = str_repeat(' ', $args['depth'] * 3);
return "\n".sprintf(
'<option %s %s id="%s" value="%s" class="class-single-term">%s',
$args['selected'],
$args['disabled'],
$args['id'],
$args['value'],
$pad . $args['label']
);
}
/**
* Ends the element output, if needed.
*
* @see Walker::end_el()
*
* @since 0.1.2
*
* @param string $output Passed by reference. Used to append additional content.
* @param object $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()
*/
public function end_el( &$output, $term, $depth = 0, $args = array() ) {
if ( 'radio' == $this->input_element ) {
$output .= "</li>\n";
} else {
$output .= "</option>\n";
}
}
}
endif; // class_exists check