forked from scribu/wp-query-multiple-taxonomies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwalkers.php
183 lines (130 loc) · 4.33 KB
/
walkers.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
<?php
class QMT_Data_Container {
private $taxonomy;
private $term;
private $data = array();
function __construct( $taxonomy, $term, $data ) {
$this->taxonomy = $taxonomy;
$this->term = $term;
$this->data = $data;
}
function __get( $key ) {
return $this->data[ $key ];
}
function __isset( $key ) {
return isset( $this->data[ $key ] );
}
function count() {
$old_query = qmt_get_query();
if ( $this->data['is-selected'] ) {
return $GLOBALS['wp_query']->post_count;
}
$query = array(
'tax_query' => array(
'relation' => 'AND'
)
);
$count_filter = array();
$count_filter[ $this->taxonomy ] = array( $this->term->slug );
// considering previous choices
foreach ( $old_query as $old_taxonomy => $old_terms ) {
$terms = explode( '+', $old_terms );
if ( !isset( $count_filter[$old_taxonomy] ) ) {
$count_filter[$old_taxonomy] = $terms;
} else {
$count_filter[$old_taxonomy] = array_merge( $count_filter[$old_taxonomy], $terms );
}
}
// now use all this for the query
foreach ( $count_filter as $tax => $terms ) {
$query['tax_query'][] = array (
'taxonomy' => $tax,
'field' => 'slug',
'terms' => $terms,
'include_children' => true,
'operator' => 'IN'
);
}
return QMT_Count::get( $query );
}
}
abstract class QMT_Walker extends Walker {
public $tree_type = 'term';
public $db_fields = array( 'parent' => 'parent', 'id' => 'term_id' );
protected $taxonomy;
protected $selected_terms = array();
protected $walker_type;
function __construct( $taxonomy, $walker_type ) {
$this->taxonomy = $taxonomy;
$this->walker_type = $walker_type;
$this->set_selected_terms();
}
protected function set_selected_terms() {
$this->selected_terms = explode( '+', qmt_get_query( $this->taxonomy ) );
}
// Make start_el() and end_el() unnecessary
function display_element( $element, &$children_elements, $max_depth, $depth=0, $args, &$output ) {
if ( !$element )
return;
$id_field = $this->db_fields['id'];
$id = $element->$id_field;
$child_output = '';
// descend only when the depth is right and there are childrens for this element
if ( ($max_depth == 0 || $max_depth > $depth+1 ) && isset( $children_elements[$id]) ) {
foreach ( $children_elements[ $id ] as $child ) {
$this->display_element( $child, $children_elements, $max_depth, $depth + 1, $args, $child_output );
}
unset( $children_elements[ $id ] );
}
$this->single_el( $output, $element, $depth, $child_output );
}
function single_el( &$output, $term, $depth, $child_output ) {
$data = $this->specific_data( $term, $depth );
$data = array_merge( $data, array(
'term-slug' => $term->slug,
'term-name' => $term->name,
'is-selected' => in_array( $term->slug, $this->selected_terms ) ? array(true) : false,
'depth' => $depth,
) );
if ( !empty( $child_output ) ) {
$data['children']['child-list'] = $child_output;
}
$full_data = new QMT_Data_Container( $this->taxonomy, $term, $data );
$output .= Taxonomy_Drill_Down_Widget::mustache_render( $this->walker_type . '-item.html', $full_data );
}
abstract function specific_data( $term, $depth );
}
class QMT_List_Walker extends QMT_Walker {
function specific_data( $term, $depth ) {
$tmp = $this->selected_terms;
$i = array_search( $term->slug, $tmp );
if ( false === $i ) {
$tmp[] = $term->slug;
$data['title'] = __( 'Add term', 'query-multiple-taxonomies' );
} else {
unset( $tmp[$i] );
$data['title'] = __( 'Remove term', 'query-multiple-taxonomies' );
}
$data['url'] = QMT_URL::for_tax( $this->taxonomy, $tmp );
return $data;
}
}
class QMT_Dropdown_Walker extends QMT_Walker {
function specific_data( $term, $depth ) {
return array(
'pad' => str_repeat(' ', $depth * 3),
'value' => $term->slug,
);
}
}
class QMT_Checkboxes_Walker extends QMT_Walker {
protected function set_selected_terms() {
$this->selected_terms = explode( ',', qmt_get_query( $this->taxonomy ) );
}
function specific_data( $term, $depth ) {
return array(
'name' => "qmt[$this->taxonomy][]",
'value' => $term->term_id,
);
}
}