forked from scribu/wp-query-multiple-taxonomies
-
Notifications
You must be signed in to change notification settings - Fork 0
/
widget.php
248 lines (196 loc) · 6.38 KB
/
widget.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
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
<?php
class Taxonomy_Drill_Down_Widget extends scbWidget {
protected $defaults = array(
'title' => '',
'mode' => 'lists',
'taxonomies' => array(),
);
static function init( $file ) {
parent::init( __CLASS__, $file, 'taxonomy-drill-down' );
if ( !class_exists( 'Mustache' ) )
require dirname(__FILE__) . '/mustache/Mustache.php';
add_action( 'load-widgets.php', array( __CLASS__, '_init' ) );
}
static function _init() {
add_action( 'admin_print_styles', array( __CLASS__, 'add_style' ), 11 );
add_action( 'admin_footer', array( __CLASS__, 'add_script' ), 11 );
}
function __construct() {
parent::__construct(
'taxonomy-drill-down',
__( 'Taxonomy Drill-Down', 'query-multiple-taxonomies' ),
array(
'description' => __( 'Display a drill-down navigation based on custom taxonomies', 'query-multiple-taxonomies' )
)
);
}
function add_style() {
?>
<style type="text/css">
.qmt-taxonomies {
margin: -.5em 0 0 .5em;
cursor: move;
}
</style>
<?php
}
function add_script() {
?>
<script type="text/javascript">
jQuery(function($){
$(document).delegate('.qmt-taxonomies', 'mouseenter', function(ev) {
$(this).sortable();
$(this).disableSelection();
});
});
</script>
<?php
}
function form( $instance ) {
if ( empty( $instance ) )
$instance = $this->defaults;
$data = array(
'title-input' => $this->input( array(
'name' => 'title',
'type' => 'text',
'desc' => __( 'Title:', 'query-multiple-taxonomies' ),
'extra' => array( 'class' => 'widefat' )
), $instance ),
'mode-input' => $this->input( array(
'type' => 'select',
'name' => 'mode',
'values' => array(
'lists' => __( 'lists', 'query-multiple-taxonomies' ),
'checkboxes' => __( 'checkboxes', 'query-multiple-taxonomies' ),
'dropdowns' => __( 'dropdowns', 'query-multiple-taxonomies' ),
),
'text' => false,
'desc' => __( 'Mode:', 'query-multiple-taxonomies' ),
'extra' => array( 'class' => 'widefat' )
), $instance ),
'taxonomies-label' => __( 'Taxonomies:', 'query-multiple-taxonomies' )
);
$selected_taxonomies = $instance['taxonomies'];
// Start with the selected taxonomies
$tax_list = $selected_taxonomies;
// Append the other taxonomies
foreach ( get_taxonomies() as $tax_name ) {
if ( !in_array( $tax_name, $selected_taxonomies ) )
$tax_list[] = $tax_name;
}
foreach ( $tax_list as $tax_name ) {
$tax_obj = self::test_tax( $tax_name );
if ( !$tax_obj )
continue;
$data['taxonomies'][] = array(
'title' => sprintf( _n( 'Post type: %s', 'Post types: %s', count( $tax_obj->object_type ), 'query-multiple-taxonomies' ), implode( ', ', $tax_obj->object_type ) ),
'input' => $this->input( array(
'type' => 'checkbox',
'name' => 'taxonomies[]',
'value' => $tax_name,
'checked'=> in_array( $tax_name, $selected_taxonomies ),
'desc' => $tax_obj->label,
) )
);
}
$m = new Mustache;
echo $m->render( file_get_contents( dirname(__FILE__) . '/widget.html' ), $data );
}
function content( $instance ) {
extract( $instance );
$taxonomies = array_filter( $taxonomies, array( __CLASS__, 'test_tax' ) );
$query = qmt_get_query();
$common = array_intersect( $taxonomies, array_keys( $query ) );
$this->all_terms = empty( $common );
if ( empty( $taxonomies ) ) {
echo
html( 'p', __( 'No taxonomies selected!', 'query-multiple-taxonomies' ) );
} else {
echo call_user_func( array( __CLASS__, "generate_$mode" ), $taxonomies, array(
'reset-text' => __( 'Reset', 'query-multiple-taxonomies' ),
'reset-url' => QMT_URL::get(),
) );
}
}
private function get_terms( $tax ) {
if ( is_taxonomy_hierarchical( $tax ) || $this->all_terms )
return get_terms( $tax );
else
return QMT_Terms::get( $tax );
}
private function generate_lists( $taxonomies, $data ) {
$query = qmt_get_query();
foreach ( $taxonomies as $taxonomy ) {
$terms = $this->get_terms( $taxonomy );
if ( empty( $terms ) )
continue;
$walker = new QMT_List_Walker( $taxonomy, 'list' );
$data_tax = array(
'taxonomy' => $taxonomy,
'title' => get_taxonomy( $taxonomy )->label,
'term-list' => $walker->walk( $terms, 0 ),
'any-text' => __( 'any', 'query-multiple-taxonomies' )
);
if ( isset( $query[$taxonomy] ) ) {
$data_tax['clear'] = array(
'url' => QMT_URL::for_tax( $taxonomy, '' ),
'title' => __( 'Remove selected terms in group', 'query-multiple-taxonomies' )
);
}
$data['taxonomy'][] = $data_tax;
}
return self::mustache_render( 'lists.html', $data );
}
private function generate_dropdowns( $taxonomies, $data ) {
$data = array_merge( $data, array(
'base-url' => QMT_URL::get_base(),
'submit-text' => __( 'Submit', 'query-multiple-taxonomies' ),
'any-text' => '— ' . __( 'any', 'query-multiple-taxonomies' ) . ' —',
) );
foreach ( $taxonomies as $taxonomy ) {
$terms = get_terms( $taxonomy );
if ( empty( $terms ) )
continue;
$walker = new qmt_dropdown_walker( $taxonomy, 'dropdown' );
$data['taxonomy'][] = array(
'name' => get_taxonomy( $taxonomy )->query_var,
'title' => get_taxonomy( $taxonomy )->label,
'term-list' => $walker->walk( $terms, 0 )
);
}
if ( empty( $data['taxonomy'] ) )
return '';
return self::mustache_render( 'dropdowns.html', $data );
}
private function generate_checkboxes( $taxonomies, $data ) {
$data = array_merge( $data, array(
'base-url' => QMT_URL::get_base(),
'submit-text' => __( 'Submit', 'query-multiple-taxonomies' ),
) );
foreach ( $taxonomies as $taxonomy ) {
$terms = $this->get_terms( $taxonomy );
if ( empty( $terms ) )
continue;
$walker = new QMT_Checkboxes_Walker( $taxonomy, 'checkbox' );
$data['taxonomy'][] = array(
'taxonomy' => $taxonomy,
'title' => get_taxonomy( $taxonomy )->label,
'term-list' => $walker->walk( $terms, 0 )
);
}
return self::mustache_render( 'checkboxes.html', $data );
}
static function test_tax( $tax_name ) {
$tax_obj = get_taxonomy( $tax_name );
if ( $tax_obj && $tax_obj->public && $tax_obj->query_var )
return $tax_obj;
return false;
}
static function mustache_render( $file, $data ) {
$template_path = locate_template( 'qmt-templates/' . $file );
if ( !$template_path )
$template_path = dirname(__FILE__) . '/templates/' . $file;
$m = new Mustache;
return $m->render( file_get_contents( $template_path ), $data );
}
}