-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathtemplate.php
462 lines (375 loc) · 12.5 KB
/
template.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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
<?php
/**
* The main template function for outputting a Facetious search form.
*
* @param $args array Arguments for this form
* @return string The markup for the form
* @author John Blackbourn
**/
function facetious( $args = array() ) {
$args = wp_parse_args( $args, array(
'fields' => array(
's'
),
'submit' => __( 'Go', 'facetious' ),
'echo' => true,
'class' => 'facetious_form',
'id' => 'facetious_form',
) );
$out = '';
$out .= '<form action="' . home_url( '/' ) . '" class="' . esc_attr( $args['class'] ) . '"';
if ( $args['id'] )
$out .= ' id="' . esc_attr( $args['id'] ) . '"';
$out .= '>';
if ( isset( $args['post_type'] ) and( '-1' !== $args['post_type'] ) and ! isset( $args['post_type'][ 'pt' ] ) )
$out .= sprintf( '<input type="hidden" name="facetious_post_type" value="%s" />', esc_attr( reset( $args['post_type'] ) ) );
foreach ( $args['fields'] as $key => $val ) {
if ( is_numeric( $key ) ) {
$key = $val;
$val = true;
}
if ( !is_array( $val ) ) {
$val = array(
'label' => $val
);
}
# Switch depending on what we're asking for.
# Currently everything that's not 's' or 'm' is treated as a taxonomy.
switch ( $key ) {
# Keyword search input:
case 's':
if ( empty( $val['label'] ) or ( true === $val['label'] ) )
$val['label'] = __( 'Search by keyword', 'facetious' );
if ( !isset( $val['class'] ) )
$val['class'] = 'facetious_input facetious_input_search';
if ( !isset( $val['id'] ) )
$val['id'] = 'facetious_input_search';
$out .= sprintf( '<p class="%s">', 'facetious_search' );
$out .= sprintf( '<label for="%1$s">%2$s</label>',
esc_attr( $val['id'] ),
$val['label']
);
$out .= sprintf( '<input type="text" name="s" value="%1$s" class="%2$s" id="%3$s" />',
esc_attr( get_search_query( false ) ),
esc_attr( $val['class'] ),
esc_attr( $val['id'] )
);
$out .= '</p>';
break;
# Post type dropdown:
case 'pt':
$post_types = array();
if ( isset( $val[ 'options' ] ) ) {
if ( ! is_array( $val[ 'options' ] ) )
$val[ 'options' ] = (array) $val[ 'options' ];
foreach ( $val[ 'options' ] as $pt ) {
if ( is_object( $pto = get_post_type_object( $pt ) ) )
$post_types[ $pt ] = $pto;
}
} else {
$post_type_names = get_post_types( array( 'public' => true ) );
foreach ( $post_type_names as $pt ) {
if ( is_object( $pto = get_post_type_object( $pt ) ) )
$post_types[ $pt ] = $pto;
}
}
if ( empty( $post_types ) )
continue;
if ( empty( $val['label'] ) or ( true === $val['label'] ) )
$val['label'] = __( 'Filter by type', 'facetious' );
if ( !isset( $val['class'] ) )
$val['class'] = 'facetious_filter facetious_filter_post_type';
if ( !isset( $val['id'] ) )
$val['id'] = 'facetious_filter_post_type';
if ( !isset( $val['all'] ) )
$val['all'] = __( 'All types', 'facetious' );
$out .= sprintf( '<p class="%s">', 'facetious_post_type' );
$out .= sprintf( '<label for="%1$s">%2$s</label>',
esc_attr( $val['id'] ),
$val['label']
);
$out .= sprintf( '<select name="facetious_post_type" class="%1$s" id="%2$s">',
esc_attr( $val['class'] ),
esc_attr( $val['id'] )
);
$out .= sprintf( '<option value="">%s</option>',
esc_html( $val['all'] )
);
foreach ( $post_types as $pt => $pto ) {
$out .= sprintf( '<option value="%s"%s>%s</option>',
esc_attr( $pt ),
selected( $pt, get_query_var( 'post_type' ), false ),
esc_html( $pto->labels->name )
);
}
$out .= '</select>';
$out .= '</p>';
break;
# Month dropdown:
case 'm':
$months = facetious_get_available_months();
if ( empty( $months ) )
continue;
if ( empty( $val['label'] ) or ( true === $val['label'] ) )
$val['label'] = __( 'Month', 'facetious' );
if ( !isset( $val['class'] ) )
$val['class'] = 'facetious_filter facetious_filter_month';
if ( !isset( $val['id'] ) )
$val['id'] = 'facetious_filter_month';
if ( !isset( $val['all'] ) )
$val['all'] = __( 'Any Month', 'facetious' );
$out .= sprintf( '<p class="%s">', 'facetious_month' );
$out .= sprintf( '<label for="%1$s">%2$s</label>',
esc_attr( $val['id'] ),
$val['label']
);
$out .= sprintf( '<select name="m" class="%1$s" id="%2$s">',
esc_attr( $val['class'] ),
esc_attr( $val['id'] )
);
$out .= sprintf( '<option value="">%s</option>',
esc_html( $val['all'] )
);
foreach ( $months as $month_key => $month_val ) {
$out .= sprintf( '<option value="%s"%s>%s</option>',
esc_attr( $month_key ),
selected( $month_key, get_query_var( 'm' ), false ),
esc_html( $month_val )
);
}
$out .= '</select>';
$out .= '</p>';
break;
# Taxonomy dropdown:
case taxonomy_exists( $key ):
$tax = get_taxonomy( $key );
if ( empty( $tax ) )
continue;
if ( !isset( $val['options'] ) ) {
$terms = get_terms( $key, array(
'hide_empty' => false
) );
if ( empty( $terms ) )
continue;
foreach ( $terms as $term )
$val['options'][$term->slug] = $term->name;
}
if ( empty( $val['label'] ) or ( true === $val['label'] ) )
$val['label'] = $tax->labels->singular_name;
if ( !isset( $val['class'] ) )
$val['class'] = sprintf( 'facetious_filter facetious_filter_%s', $tax->name );
if ( !isset( $val['id'] ) )
$val['id'] = sprintf( 'facetious_filter_%s', $tax->name );
if ( !isset( $val['all'] ) )
$val['all'] = $tax->labels->all_items;
$out .= sprintf( '<p class="facetious_%s">', $tax->name );
$out .= sprintf( '<label for="%1$s">%2$s</label>',
esc_attr( $val['id'] ),
$val['label']
);
$out .= sprintf( '<select name="%1$s" class="%2$s" id="%3$s">',
$tax->query_var,
esc_attr( $val['class'] ),
esc_attr( $val['id'] )
);
$out .= sprintf( '<option value="">%s</option>',
esc_html( $val['all'] )
);
foreach ( $val['options'] as $value => $label ) {
$out .= sprintf( '<option value="%s"%s>%s</option>',
esc_attr( $value ),
selected( $value, get_query_var( $tax->query_var ), false ),
esc_html( $label )
);
}
$out .= '</select>';
$out .= '</p>';
break;
# custom fields
default:
if ( empty( $val['label'] ) or ( true === $val['label'] ) ) {
$val['label'] = esc_attr( $key );
}
if ( !isset( $val['class'] ) ) {
$val['class'] = sprintf( 'facetious_filter facetious_filter_%s', esc_attr( $key ) );
}
if ( !isset( $val['id'] ) ) {
$val['id'] = sprintf( 'facetious_filter_%s', esc_attr( $key ) );
}
if ( !isset( $val['all'] ) ) {
$val['all'] = __('All Items', 'facetious');
}
if ( empty( $val['options'] ) or !isset( $val['options'] ) ) {
continue;
}
$out .= sprintf( '<p class="facetious_%s">', esc_attr( $key ) );
$out .= sprintf( '<label for="%1$s">%2$s</label>',
esc_attr( $val['id'] ),
$val['label']
);
$out .= sprintf( '<select name="%1$s" class="%2$s" id="%3$s" />',
esc_attr( $key ),
esc_attr( $val['class'] ),
esc_attr( $val['id'] )
);
$out .= sprintf( '<option value="">%s</option>',
esc_html( $val['all'] )
);
foreach ( $val['options'] as $value => $label ) {
$out .= sprintf( '<option value="%s"%s>%s</option>',
esc_attr( $value ),
selected( $value, get_query_var( $key ), false ),
esc_html( $label )
);
}
$out .= '</select>';
$out .= '</p>';
break;
}
}
$out .= '<p class="facetious_submit">';
$out .= sprintf( '<input type="submit" value="%s" class="facetious_submit_button" />', esc_attr( $args['submit'] ) );
$out .= '</p>';
$out .= '</form>';
if ( $args['echo'] )
echo $out;
return $out;
}
/**
* Helper function for retrieving a list of populated months for a given post type
*
* @param $post_type string An optional post type to restrict the query to
* @return array The populated months
* @author John Blackbourn
**/
function facetious_get_available_months( $post_type = null ) {
global $wpdb, $wp_locale;
if ( $post_type )
$where = $wpdb->prepare( 'WHERE post_type = %s', $post_type );
else
$where = '';
$months = $wpdb->get_results( "
SELECT DISTINCT YEAR( post_date ) AS year, MONTH( post_date ) AS month
FROM {$wpdb->posts}
{$where}
ORDER BY post_date DESC
" );
$month_count = count( $months );
if ( !$month_count )
return array();
if ( ( 1 == $month_count ) and ( 0 == $months[0]->month ) )
return array();
$available_months = array();
foreach ( $months as $arc_row ) {
if ( 0 == $arc_row->year )
continue;
$month = zeroise( $arc_row->month, 2 );
# @TODO _x() this:
$available_months[$arc_row->year . $month] = sprintf( __( '%1$s %2$d', 'facetious' ),
$wp_locale->get_month( $month ),
$arc_row->year
);
}
return $available_months;
}
/**
* Is the existing query a Facetious query?
*
* @return bool True if true, natch
* @author
**/
function is_facetious() {
return isset( $GLOBALS['wp_query']->query_vars[ 'facetious' ] );
}
/**
* Returns a Facetious URL from a WP_Query::query_vars like array
* of parameters.
*
* Note that this function assumes the site is using pretty permalinks.
*
* @param array $query A WP_Query::query like array of parameters
* @return string A Facetious format URL
* @author Simon Wheatley
**/
function get_facetious_url( $query ) {
$facetious = Facetious::init();
if ( isset( $query[ 'post_type' ] ) )
$query[ 'facetious_post_type' ] = $query[ 'post_type' ];
return $facetious->construct_query_url( $query );
}
/**
* Echoes a Facetious URL from a WP_Query::query_vars like array
* of parameters.
*
* @param array $query A WP_Query::query like array of parameters
* @return void (echoes)
* @author Simon Wheatley
**/
function facetious_url( $query ) {
echo esc_url( get_facetious_url( $query ) );
}
/**
* Display the terms for a post as a list of Facetious links.
*
* Note that this function assumes the site is using pretty permalinks.
*
* @param int $id Post ID.
* @param string $taxonomy Taxonomy name.
* @param array $query_default A WP_Query::query like array of parameters
* @param string $before Optional. Before list.
* @param string $sep Optional. Separate items using this.
* @param string $after Optional. After list.
* @return void
* @author Simon Wheatley
**/
function the_facetious_terms( $id, $taxonomy, $query_default = array(), $before = '', $sep = '', $after = '' ) {
$term_list = get_the_facetious_term_list( $id, $taxonomy, $query_default, $before, $sep, $after );
if ( empty( $term_list ) || is_wp_error( $term_list ) )
return false;
echo apply_filters( 'facetious_the_terms', $term_list, $query_default, $taxonomy, $before, $sep, $after );
}
/**
* Retrieve a post's terms as a list of Facetious links with specified format.
*
* Note that this function assumes the site is using pretty permalinks.
*
* @param int $id Post ID.
* @param string $taxonomy Taxonomy name.
* @param array $query_default A WP_Query::query like array of parameters
* @param string $before Optional. Before list.
* @param string $sep Optional. Separate items using this.
* @param string $after Optional. After list.
* @return string|obj False on WordPress error. HTML on success, or an empty string if no terms available.
* @author Simon Wheatley
**/
function get_the_facetious_term_list( $id, $taxonomy, $query_default = array(), $before = '', $sep = '', $after = '' ) {
$terms = get_the_terms( $id, $taxonomy );
if ( is_wp_error( $terms ) )
return $terms;
if ( empty( $terms ) )
return '';
if ( ! is_array( $query_default ) )
$query_default = array();
foreach ( $terms as $term ) {
$query = array_merge( $query_default, array( $taxonomy => $term->slug ) );
$link = get_facetious_url( $query );
if ( is_wp_error( $link ) )
return $link;
$term_links[] = '<a href="' . esc_url( $link ) . '" rel="tag">' . $term->name . '</a>';
}
$term_links = apply_filters( "facetious_term_links-$taxonomy", $term_links, $query_default, $taxonomy, $before, $sep, $after );
return $before . join( $sep, $term_links ) . $after;
}
/**
* Returns any current Facetious query as a WP_Query::query_vars
* like array of parameters.
*
* @return array A WP_Query::query like array of parameters
* @author Simon Wheatley
**/
function get_current_facetious_query() {
if ( ! is_facetious() )
return array();
$facetious = Facetious::init();
return $facetious->parse_search( get_query_var( 'facetious' ) );
}