PluginProbe
Filter Everything — WordPress & WooCommerce Filters / 1.6.1
Filter Everything — WordPress & WooCommerce Filters v1.6.1
1.9.7 1.9.6 1.9.5 1.9.4 1.9.3 1.9.2.2 1.9.2.1 trunk 1.2.1 1.2.3 1.2.4 1.2.5 1.3.0 1.3.1 1.3.2 1.4.1 1.4.4 1.4.5 1.4.8 1.4.9 1.5.0 1.5.1 1.6.0 1.6.1 1.6.2 All 52 releases
filter-everything / src / Walkers / WalkerCheckbox.php

WalkerCheckbox.php in Filter Everything — WordPress & WooCommerce Filters 1.6.1, at src/Walkers/WalkerCheckbox.php

289 lines 9.9 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 namespace FilterEverything\Filter;
4
5 if ( ! defined('WPINC') ) {
6 wp_die();
7 }
8
9
10 class WalkerCheckbox extends \Walker
11 {
12 /**
13 * What the class handles.
14 *
15 * @since 2.1.0
16 * @var string
17 *
18 * @see Walker::$tree_type
19 */
20 public $tree_type = 'Checkbox';
21
22 /**
23 * Database fields to use.
24 *
25 * @since 2.1.0
26 * @var array
27 *
28 * @see Walker::$db_fields
29 * @todo Decouple this
30 */
31 public $db_fields = array(
32 'parent' => 'parent',
33 'id' => 'term_id',
34 );
35
36 public $max_depth = 0;
37
38 public $has_not_empty_children = [];
39
40 public $parent_opened_elements = [];
41
42 /**
43 * Starts the list before the elements are added.
44 *
45 * @since 2.1.0
46 *
47 * @see Walker::start_lvl()
48 *
49 * @param string $output Used to append additional content. Passed by reference.
50 * @param int $depth Optional. Depth of category. Used for tab indentation. Default 0.
51 * @param array $args Optional. An array of arguments. Will only append content if style argument
52 * value is 'list'. See wp_list_categories(). Default empty array.
53 */
54 public function start_lvl( &$output, $depth = 0, $args = array() ) {
55 $indent = str_repeat( "\t", $depth );
56 $output .= "$indent<ul class='children'>\n";
57 }
58
59 /**
60 * Ends the list of after the elements are added.
61 *
62 * @since 2.1.0
63 *
64 * @see Walker::end_lvl()
65 *
66 * @param string $output Used to append additional content. Passed by reference.
67 * @param int $depth Optional. Depth of category. Used for tab indentation. Default 0.
68 * @param array $args Optional. An array of arguments. Will only append content if style argument
69 * value is 'list'. See wp_list_categories(). Default empty array.
70 */
71 public function end_lvl( &$output, $depth = 0, $args = array() ) {
72 $indent = str_repeat( "\t", $depth );
73 $output .= "$indent</ul>\n";
74 }
75
76 /**
77 * Starts the element output.
78 *
79 * @since 2.1.0
80 *
81 * @see Walker::start_el()
82 *
83 * @param string $output Used to append additional content (passed by reference).
84 * @param WP_Term $term Term data object.
85 * @param int $depth Optional. Depth of category in reference to parents. Default 0.
86 * @param array $args Optional. An array of arguments. See wp_list_categories(). Default empty array.
87 * @param int $id Optional. ID of the current category. Default 0.
88 */
89 public function start_el( &$output, $term, $depth = 0, $args = array(), $id = 0 ) {
90
91 $term_name = esc_attr( $term->name );
92 $url_manager = $args['url_manager'];
93 $filter = $args['filter'];
94 $checked = ( in_array( $term->slug, $filter['values'] ) ) ? 1 : 0;
95 $disabled = 0;
96
97 if( isset( $term->wp_queried ) && $term->wp_queried ){
98 $checked = 1;
99 $disabled = 1;
100 }
101
102 $id = $id ? $id : $term->term_id;
103 $toggleButton = '';
104 $visibility_class = '';
105
106 // Don't generate an element if the term name is empty.
107 if ( '' === $term_name ) {
108 return;
109 }
110
111 $atts = array();
112 //$set = isset( $args['set']['ID'] ) ? array( 0 => array('ID' => $args['set']['ID'] ) ) : [];
113 $atts['href'] = $url_manager->getTermUrl( $term->slug, $filter['e_name']/*, $set*/ );
114
115 $attributes = '';
116 foreach ( $atts as $attr => $value ) {
117 if ( is_scalar( $value ) && '' !== $value && false !== $value ) {
118 $value = ( 'href' === $attr ) ? esc_url( $value ) : esc_attr( $value );
119 $attributes .= ' ' . $attr . '="' . $value . '"';
120 }
121 }
122
123 $link = apply_filters( 'wpc_filters_checkbox_term_html', '<a'.$attributes.'>'.$term->name.'</a>', $attributes, $term, $filter );
124
125 if ( isset( $args['set']['show_count']['value'] ) && $args['set']['show_count']['value'] === 'yes' ) {
126 $link .= '&nbsp;'. flrt_get_count( $term );
127 }
128
129 $output .= "\t<li";
130 $cross_count = isset( $term->cross_count ) ? esc_attr( $term->cross_count ) : '';
131
132 $parent_opened_elements_flipped = array_flip( $this->parent_opened_elements );
133 if( isset( $parent_opened_elements_flipped[$id] ) ){
134 $visibility_class = ' wpc-opened';
135 }
136
137 if( $this->max_depth > 0) {
138 $visibility_class = ' '. flrt_get_status_css_class( $id, FLRT_HIERARCHY_LIST_COOKIE_NAME );
139 }
140
141 $css_classes = array(
142 0 => 'wpc-checkbox-item',
143 1 => 'wpc-term-item',
144 3 => 'wpc-term-count-' . $cross_count,
145 4 => 'wpc-term-id-'.$id
146 );
147
148 if( $checked ){
149 $css_classes[2] = 'wpc-term-selected';
150 }
151
152 if( $disabled ){
153 $css_classes[] = 'wpc-term-disabled';
154 }
155
156 if( $this->has_children ){
157 $css_classes[] = 'wpc-has-children';
158 $toggleButton = '<i class="wpc-toggle-children-list" data-tid="'.esc_attr($term->term_id).'"></i>';
159 }
160
161 $has_not_empty_children_flipped = array_flip( $this->has_not_empty_children );
162 // if( in_array( $id, $this->has_not_empty_children ) ){
163 if( isset( $has_not_empty_children_flipped[$id] ) ){
164 $css_classes[] = 'wpc-has-not-empty-children';
165 }
166
167 $css_classes = implode( ' ', $css_classes );
168 $css_classes .= $visibility_class;
169 $css_classes = $css_classes ? ' class="' . esc_attr( $css_classes ) . '"' : '';
170
171 $output .= $css_classes;
172 $output .= ' id="'.flrt_term_id('term', $filter, $id, false).'">';
173 $output .= '<div class="wpc-term-item-content-wrapper'.esc_attr( $visibility_class ).'"><input '.checked( 1, $checked, false ).' '.disabled( 1, $disabled, false ).' type="checkbox" data-wpc-link="'.esc_url($atts['href']).'"';
174 $output .= ' id="'.flrt_term_id('checkbox', $filter, $id, false).'" />'."\n";
175 $output .= '<label for="'.flrt_term_id('checkbox', $filter, $id, false).'">';
176 $output .= "$link\n";
177 $output .= "</label>\n";
178 $output .= $toggleButton;
179 $output .= "</div>\n";
180
181 }
182
183 /**
184 * Ends the element output, if needed.
185 *
186 * @since 2.1.0
187 *
188 * @see Walker::end_el()
189 *
190 * @param string $output Used to append additional content (passed by reference).
191 * @param object $page Not used.
192 * @param int $depth Optional. Depth of category. Not used.
193 * @param array $args Optional. An array of arguments. Only uses 'list' for whether should append
194 * to output. See wp_list_categories(). Default empty array.
195 */
196 public function end_el( &$output, $page, $depth = 0, $args = array() ) {
197 $output .= "</li>\n";
198 }
199
200 public function walk( $elements, $max_depth, ...$args ) {
201 $output = '';
202
203 $this->max_depth = $max_depth;
204
205 // Invalid parameter or nothing to walk.
206 if ( $max_depth < -1 || empty( $elements ) ) {
207 return $output;
208 }
209
210 $parent_field = $this->db_fields['parent'];
211
212 // Flat display.
213 if ( -1 == $max_depth ) {
214 $empty_array = array();
215 foreach ( $elements as $e ) {
216 $this->display_element( $e, $empty_array, 1, 0, $args, $output );
217 }
218 return $output;
219 }
220
221 /*
222 * Need to display in hierarchical order.
223 * Separate elements into two buckets: top level and children elements.
224 * Children_elements is two dimensional array, eg.
225 * Children_elements[10][] contains all sub-elements whose parent is 10.
226 */
227 $top_level_elements = array();
228 $children_elements = array();
229 $parent_opened_elements = array();
230
231 foreach ( $elements as $e ) {
232 if ( empty( $e->$parent_field ) ) {
233 $top_level_elements[] = $e;
234 } else {
235 $children_elements[ $e->$parent_field ][] = $e;
236 }
237
238 if( ! empty( $args[0]['filter']['values'] ) ){
239 if( in_array( $e->slug, $args[0]['filter']['values'] ) ){
240 $parent_opened_elements[] = $e->$parent_field;
241 flrt_get_all_parents( $elements, $e->$parent_field, $parent_opened_elements );
242 }
243 }
244
245 }
246
247 $this->parent_opened_elements = array_unique($parent_opened_elements);
248 $this->has_not_empty_children = flrt_get_parents_with_not_empty_children($elements);
249
250 /*
251 * When none of the elements is top level.
252 * Assume the first one must be root of the sub elements.
253 */
254 if ( empty( $top_level_elements ) ) {
255
256 $first = array_slice( $elements, 0, 1 );
257 $root = $first[0];
258
259 $top_level_elements = array();
260 $children_elements = array();
261 foreach ( $elements as $e ) {
262 if ( $root->$parent_field == $e->$parent_field ) {
263 $top_level_elements[] = $e;
264 } else {
265 $children_elements[ $e->$parent_field ][] = $e;
266 }
267 }
268 }
269
270 foreach ( $top_level_elements as $e ) {
271 $this->display_element( $e, $children_elements, $max_depth, 0, $args, $output );
272 }
273
274 /*
275 * If we are displaying all levels, and remaining children_elements is not empty,
276 * then we got orphans, which should be displayed regardless.
277 */
278 if ( ( 0 == $max_depth ) && count( $children_elements ) > 0 ) {
279 $empty_array = array();
280 foreach ( $children_elements as $orphans ) {
281 foreach ( $orphans as $op ) {
282 $this->display_element( $op, $empty_array, 1, 0, $args, $output );
283 }
284 }
285 }
286
287 return $output;
288 }
289 }