forked from misterbisson/scriblio
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathclass-facet-taxonomy.php
More file actions
257 lines (204 loc) · 7.51 KB
/
Copy pathclass-facet-taxonomy.php
File metadata and controls
257 lines (204 loc) · 7.51 KB
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
<?php
class Facet_Taxonomy implements Facet
{
public $version = 1;
public $ttl = 18013; // a little longer than 5 hours
function __construct( $name , $args , $facets_object )
{
$this->name = $name;
$this->args = $args;
$this->facets = $facets_object;
$this->taxonomy = $args['taxonomy'] ? $args['taxonomy'] : $this->name;
$this->facets->_tax_to_facet[ $this->taxonomy ] = $this->name;
$this->facets->_facet_to_tax[ $this->name ] = $this->taxonomy;
$taxonomy = get_taxonomy( $this->taxonomy );
$this->label = $taxonomy->label;
$this->labels = $taxonomy->labels;
if( $taxonomy->query_var )
$this->query_var = $taxonomy->query_var;
}
function register_query_var()
{
global $wp;
if ( TRUE === $this->query_var )
$this->query_var = $this->name;
// @ TODO: check to see if the query var is registered before adding it again
$this->query_var = sanitize_title_with_dashes( $this->query_var );
$wp->add_query_var( $this->query_var );
return $this->query_var;
}
function parse_query( $query_terms , $wp_query )
{
// identify the terms in this query
foreach( array_filter( array_map( 'trim' , (array) preg_split( '/[,\+\|\/]/' , $query_terms ))) as $val )
{
if ( $term = get_term_by( 'slug' , $val , $this->taxonomy ) )
{
$this->selected_terms[ $term->slug ] = $term;
}
}
return $this->selected_terms;
}
function get_terms_in_corpus()
{
if( isset( $this->terms_in_corpus ))
return $this->terms_in_corpus;
scriblio()->timer( 'facet_taxonomy::get_terms_in_corpus' );
$timer_notes = 'from cache';
if( ! $this->terms_in_corpus = wp_cache_get( 'terms-in-corpus-'. $this->taxonomy , 'scrib-facet-taxonomy' ))
{
$timer_notes = 'from query';
$terms = get_terms( $this->taxonomy , array( 'number' => 1000 , 'orderby' => 'count' , 'order' => 'DESC' ));
$terms = apply_filters( 'scriblio_facet_taxonomy_terms', $terms );
$this->terms_in_corpus = array();
foreach( $terms as $term )
{
$this->terms_in_corpus[] = (object) array(
'facet' => $this->facets->_tax_to_facet[ $term->taxonomy ],
'slug' => $term->slug,
'name' => $term->name,
'description' => $term->description,
'term_id' => $term->term_id,
'term_taxonomy_id' => $term->term_taxonomy_id,
'count' => $term->count,
);
}
wp_cache_set( 'terms-in-corpus-'. $this->taxonomy , $this->terms_in_corpus, 'scrib-facet-taxonomy', $this->ttl );
}
scriblio()->timer( 'facet_taxonomy::get_terms_in_corpus', $timer_notes );
return $this->terms_in_corpus;
}
function get_terms_in_found_set()
{
if( isset( $this->facets->_matching_tax_facets[ $this->name ] ) && is_array( $this->facets->_matching_tax_facets[ $this->name ] ) )
{
return $this->facets->_matching_tax_facets[ $this->name ];
}
$matching_post_ids = $this->facets->get_matching_post_ids();
// if there aren't any matching post ids, we don't need to query
if ( ! $matching_post_ids )
{
return array();
}//end if
scriblio()->timer( 'facet_taxonomy::get_terms_in_found_set' );
$timer_notes = 'from cache';
$cache_key = md5( serialize( $matching_post_ids ) ) . $this->version;
if( ! $this->facets->_matching_tax_facets = wp_cache_get( $cache_key . ( scriblio()->cachebuster ? 'CACHEBUSTER' : '' ), 'scrib-facet-taxonomy' ))
{
$timer_notes = 'from query';
global $wpdb;
$facets_query = "SELECT b.term_id, c.term_taxonomy_id, b.slug, b.name, a.taxonomy, a.description, COUNT(c.term_taxonomy_id) AS `count`
FROM $wpdb->term_relationships c
INNER JOIN $wpdb->term_taxonomy a ON a.term_taxonomy_id = c.term_taxonomy_id
INNER JOIN $wpdb->terms b ON a.term_id = b.term_id
WHERE c.object_id IN (". implode( ',' , $matching_post_ids ) .")
GROUP BY c.term_taxonomy_id ORDER BY count DESC LIMIT 2000
/* generated in Facet_Taxonomy::get_terms_in_found_set() */";
$terms = $wpdb->get_results( $facets_query );
scriblio()->timer( 'facet_taxonomy::get_terms_in_found_set::scriblio_facet_taxonomy_terms' );
$terms = apply_filters( 'scriblio_facet_taxonomy_terms', $terms );
scriblio()->timer( 'facet_taxonomy::get_terms_in_found_set::scriblio_facet_taxonomy_terms', count( $terms ) . ' terms' );
$this->facets->_matching_tax_facets = array();
foreach( $terms as $term )
{
$this->facets->_matching_tax_facets[ $this->facets->_tax_to_facet[ $term->taxonomy ]][] = (object) array(
'facet' => $this->facets->_tax_to_facet[ $term->taxonomy ],
'slug' => $term->slug,
'name' => $term->name,
'count' => $term->count,
'description' => $term->description,
'term_id' => $term->term_id,
'term_taxonomy_id' => $term->term_taxonomy_id,
);
}
wp_cache_set( $cache_key, $this->facets->_matching_tax_facets , 'scrib-facet-taxonomy', $this->ttl );
}
scriblio()->timer( 'facet_taxonomy::get_terms_in_found_set', $timer_notes );
if( ! isset( $this->facets->_matching_tax_facets[ $this->name ] ) || ! is_array( $this->facets->_matching_tax_facets[ $this->name ] ) )
{
return FALSE;
}
else
{
return $this->facets->_matching_tax_facets[ $this->name ];
}
}
function get_terms_in_post( $post_id = FALSE )
{
if( ! $post_id )
$post_id = get_the_ID();
if( ! $post_id )
return FALSE;
scriblio()->timer( 'facet_taxonomy::get_terms_in_post' );
$terms = wp_get_object_terms( $post_id , $this->taxonomy );
$terms = apply_filters( 'scriblio_facet_taxonomy_terms', $terms );
$terms_in_post = array();
foreach( $terms as $term )
{
$terms_in_post[] = (object) array(
'facet' => $this->facets->_tax_to_facet[ $term->taxonomy ],
'slug' => $term->slug,
'name' => $term->name,
'description' => $term->description,
'term_id' => $term->term_id,
'term_taxonomy_id' => $term->term_taxonomy_id,
'count' => $term->count,
);
}
scriblio()->timer( 'facet_taxonomy::get_terms_in_post' );
return $terms_in_post;
}
function selected( $term )
{
return( isset( $this->selected_terms[ ( is_object( $term ) ? $term->slug : $term ) ] ));
}
function queryterm_add( $term , $current )
{
$current[ $term->slug ] = $term;
return $current;
}
function queryterm_remove( $term , $current )
{
unset( $current[ $term->slug ] );
return $current;
}
function permalink( $terms )
{
if ( 1 === count( $terms ) )
{
$termlink = get_term_link( (int) current( $terms )->term_id , $this->taxonomy );
}
else
{
// much of this section comes from get_term_link() in /wp-includes/taxonomy.php,
// but that code can't handle multiple terms in a single taxonomy
global $wp_rewrite;
$termlink = $wp_rewrite->get_extra_permastruct( $this->taxonomy );
if ( empty($termlink) ) // dang, we're not using pretty permalinks
{
$t = get_taxonomy( $this->taxonomy );
$termlink = "?$t->query_var=" . implode( '+' , array_keys( $terms ) );
}
else
{
$termlink = str_replace( "%$this->taxonomy%" , implode( '+' , array_keys( $terms )) , $termlink );
}
$termlink = home_url( user_trailingslashit( $termlink , 'category' ));
}// end else
$termlink = apply_filters( 'scriblio_facet_taxonomy_permalink', $termlink, $terms, $this->taxonomy );
return $termlink;
}
// WP sometimes fails to update this count during regular operations, so this fixes that
// it's not actually called anywhere, though
function _update_term_counts()
{
$wpdb->get_results('
UPDATE '. $wpdb->term_taxonomy .' tt
SET tt.count = (
SELECT COUNT(*)
FROM '. $wpdb->term_relationships .' tr
WHERE tr.term_taxonomy_id = tt.term_taxonomy_id
) /* generated in Facet_Taxonomy::_update_term_counts() */'
);
}
}