-
Notifications
You must be signed in to change notification settings - Fork 27
/
core.php
286 lines (212 loc) · 5.75 KB
/
core.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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
<?php
class QMT_Hooks {
// Transform ?qmt[category][]=5&qmt[category][]=6 into something usable
static function request( $request ) {
if ( !isset( $_GET['qmt'] ) )
return $request;
foreach ( $_GET['qmt'] as $taxonomy => $terms ) {
$request['tax_query'][] = array(
'taxonomy' => $taxonomy,
'terms' => $terms,
'field' => 'term_id',
'operator' => 'IN'
);
}
return $request;
}
// Add the selected terms to the title
static function wp_title( $title, $sep, $seplocation ) {
$tax_title = QMT_Template::get_title();
if ( empty( $tax_title ) )
return $title;
if ( 'right' == $seplocation )
$title = "$tax_title $sep ";
else
$title = " $sep $tax_title";
return $title;
}
static function wp_head() {
?>
<style type="text/css">
.taxonomy-drilldown-lists p,
.taxonomy-drilldown-checkboxes p,
.taxonomy-drilldown-dropdowns p {
margin-top: 1em;
}
.taxonomy-drilldown-checkboxes li,
.taxonomy-drilldown-dropdowns li {
list-style: none;
}
.taxonomy-drilldown-dropdowns select {
display: block;
}
</style>
<?php
}
}
scbHooks::add( 'QMT_Hooks' );
class QMT_Terms {
private static $filtered_ids;
// Get a list of all the terms attached to all the posts in the current query
public static function get( $tax ) {
self::set_filtered_ids();
if ( empty( self::$filtered_ids ) )
return array();
$raw_terms = wp_get_object_terms( self::$filtered_ids, $tax );
// distinct terms
$terms = array();
foreach ( $raw_terms as $term )
$terms[ $term->term_id ] = $term;
return $terms;
}
private static function set_filtered_ids() {
global $wp_query;
if ( isset( self::$filtered_ids ) )
return;
$args = array_merge( $wp_query->query, array(
'fields' => 'ids',
'nopaging' => true,
'no_found_rows' => true,
'ignore_sticky_post' => true,
'cache_results' => false,
) );
$query = new WP_Query;
self::$filtered_ids = $query->query( $args );
}
}
class QMT_Count {
// Count posts, without getting them
public static function get( $query_vars ) {
$query_vars = array_merge( $query_vars, array(
'fields' => 'ids',
'qmt_count' => true,
'nopaging' => true
) );
$query = new WP_Query( $query_vars );
if ( !empty( $query->posts ) )
return $query->posts[0];
return 0;
}
public static function posts_clauses( $bits, $wp_query ) {
if ( $wp_query->get( 'qmt_count' ) ) {
if ( empty( $bits['groupby'] ) ) {
$what = '*';
} else {
$what = 'DISTINCT ' . $bits['groupby'];
$bits['groupby'] = '';
}
$bits['fields'] = "COUNT($what)";
}
return $bits;
}
}
add_filter( 'posts_clauses', array( 'QMT_Count', 'posts_clauses' ), 10, 2 );
class QMT_URL {
public static function for_tax( $taxonomy, $value ) {
$query = qmt_get_query();
if ( empty( $value ) )
unset( $query[ $taxonomy ] );
else
$query[ $taxonomy ] = trim( implode( '+', $value ), '+' );
return self::get( $query );
}
public static function get( $query = array() ) {
$url = self::get_base();
if ( empty($query) )
return apply_filters( 'qmt_reset_url', $url );
ksort( $query );
foreach ( $query as $taxonomy => $value )
$url = add_query_arg( get_taxonomy( $taxonomy )->query_var, $value, $url );
return apply_filters( 'qmt_url', $url, $query );
}
public static function get_base() {
static $base_url;
if ( empty( $base_url ) )
$base_url = apply_filters( 'qmt_base_url', get_bloginfo( 'url' ) );
return trailingslashit( $base_url );
}
}
class QMT_Template {
public static function get_title() {
$title = array();
foreach ( qmt_get_query() as $tax => $value ) {
$terms = preg_split( '/[+,]+/', $value );
$out = array();
foreach ( $terms as $slug ) {
$term_obj = get_term_by( 'slug', $slug, $tax );
if ( $term_obj )
$out[] = $term_obj->name;
}
$tax_obj = get_taxonomy( $tax );
if ( count( $out ) == 1 )
$key = $tax_obj->labels->singular_name;
else
$key = $tax_obj->labels->name;
$title[] .= $key . ': ' . implode( ' + ', $out );
}
return implode( '; ', $title );
}
}
/**
* Wether multiple taxonomies are queried
* @param array $taxonomies A list of taxonomies to check for (AND).
*
* @return bool
*/
function is_multitax( $taxonomies = array() ) {
$queried = array_keys( qmt_get_query() );
$count = count( $taxonomies );
if ( !$count )
return count( $queried ) > 1;
return count( array_intersect( $queried, $taxonomies) ) == $count;
}
/**
* Get the list of selected terms
*
* @param string $taxname a certain taxonomy name
*
* @return array( taxonomy => query )
*/
function qmt_get_query( $taxname = '' ) {
global $wp_query;
$qmt_query = array();
if ( !is_null( $wp_query->tax_query ) ) {
foreach ( $wp_query->tax_query->queries as &$tax_query ) {
$terms = _qmt_get_term_slugs( $tax_query );
if ( 'AND' == $tax_query['operator'] )
$qmt_query[ $tax_query['taxonomy'] ] = $terms;
if ( 'IN' == $tax_query['operator'] )
$qmt_query[ $tax_query['taxonomy'] ][] = implode( ',', $terms );
}
foreach ( $qmt_query as &$value )
$value = implode( '+', $value );
}
if ( $taxname ) {
if ( isset( $qmt_query[ $taxname ] ) )
return $qmt_query[ $taxname ];
return false;
}
return $qmt_query;
}
// https://core.trac.wordpress.org/ticket/21684
function _qmt_get_term_slugs( &$tax_query ) {
if ( 'slug' == $tax_query['field'] )
return $tax_query['terms'];
$terms = array();
foreach ( $tax_query['terms'] as $field ) {
$term = get_term_by( $tax_query['field'], $field, $tax_query['taxonomy'] );
if ( $term )
$terms[] = $term->slug;
}
$tax_query['field'] = 'slug';
$tax_query['terms'] = $terms;
return $terms;
}
// Deprecated
function qmt_get_terms( $tax ) {
_deprecated_function( __FUNCTION__, '1.4' );
if ( is_archive() )
return QMT_Terms::get( $tax );
else
return get_terms( $tax );
}