forked from argoproject/Argo
-
-
Notifications
You must be signed in to change notification settings - Fork 83
/
Copy pathtaxonomies.php
471 lines (425 loc) · 13.6 KB
/
taxonomies.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
460
461
462
463
464
465
466
467
468
469
470
471
<?php
/**
* Check if the Series taxonomy is enabled
*
* @since 0.4
* @return bool Whether or not the Series taxonomy option is enabled in the Theme Options > Advanced
*/
function largo_is_series_enabled() {
$series_enabled = of_get_option('series_enabled');
return !empty($series_enabled);
}
/**
* Check if Series landing pages are enabled
*
* @since 0.5.2
* @return bool Whether or not the Series Landing Page option is enabled in the Theme Options > Advanced
*/
function largo_is_series_landing_enabled() {
$series_landing_enabled = of_get_option('custom_landing_enabled');
return !empty($series_landing_enabled);
}
/**
* Register the prominence and series custom taxonomies
* Insert the default terms
*
* @uses largo_is_series_enabled
* @since 1.0
*/
function largo_custom_taxonomies() {
if (!taxonomy_exists('prominence')) {
register_taxonomy(
'prominence',
'post',
array(
'hierarchical' => true,
'labels' => array(
'name' => _x( 'Post Prominence', 'taxonomy general name' ),
'singular_name' => _x( 'Post Prominence', 'taxonomy singular name' ),
'search_items' => __( 'Search Post Prominences' ),
'all_items' => __( 'All Post Prominences' ),
'parent_item' => __( 'Parent Post Prominence' ),
'parent_item_colon' => __( 'Parent Post Prominence:' ),
'edit_item' => __( 'Edit Post Prominence' ),
'view_item' => __( 'View Post Prominence' ),
'update_item' => __( 'Update Post Prominence' ),
'add_new_item' => __( 'Add New Post Prominence' ),
'new_item_name' => __( 'New Post Prominence Name' ),
'menu_name' => __( 'Post Prominence' ),
),
'query_var' => true,
'rewrite' => true,
)
);
}
$termsDefinitions = array(
array(
'name' => __('Sidebar Featured Widget', 'largo'),
'description' => __('If you are using the Featured Posts widget in a sidebar, add this label to posts to determine which to display in the widget.', 'largo'),
'slug' => 'sidebar-featured'
),
array(
'name' => __('Footer Featured Widget', 'largo'),
'description' => __('If you are using the Featured Posts widget in the footer, add this label to posts to determine which to display in the widget.', 'largo'),
'slug' => 'footer-featured'
),
array(
'name' => __('Featured in Category', 'largo'),
'description' => __('This will allow you to designate a story to appear more prominently on category archive pages.', 'largo'),
'slug' => 'category-featured'
),
array(
'name' => __('Homepage Featured', 'largo'),
'description' => __('Add this label to posts to display them in the featured area on the homepage.', 'largo'),
'slug' => 'homepage-featured'
)
);
if (largo_is_series_enabled()) {
$termsDefinitions[] = array(
'name' => __('Featured in Series', 'largo'),
'description' => __('Select this option to allow this post to float to the top of any/all series landing pages sorting by Featured first.', 'largo'),
'slug' => 'series-featured'
);
}
$largoProminenceTerms = apply_filters('largo_prominence_terms', $termsDefinitions);
$changed = false;
$terms = get_terms('prominence', array(
'hide_empty' => false,
'fields' => 'all'
));
$names = array_map(function($arg) { return $arg->name; }, $terms);
$term_ids = array();
foreach ($largoProminenceTerms as $term ) {
if (!in_array($term['name'], $names)) {
wp_insert_term(
$term['name'], 'prominence',
array(
'description' => $term['description'],
'slug' => $term['slug']
)
);
$changed = true;
}
}
if ($changed)
delete_option('prominence_children');
do_action('largo_after_create_prominence_taxonomy', $largoProminenceTerms);
if ( ! taxonomy_exists( 'series' ) ) {
register_taxonomy(
'series',
'post',
array(
'hierarchical' => true,
'labels' => array(
'name' => _x( 'Series', 'taxonomy general name' ),
'singular_name' => _x( 'Series', 'taxonomy singular name' ),
'search_items' => __( 'Search Series' ),
'all_items' => __( 'All Series' ),
'parent_item' => __( 'Parent Series' ),
'parent_item_colon' => __( 'Parent Series:' ),
'edit_item' => __( 'Edit Series' ),
'view_item' => __( 'View Series' ),
'update_item' => __( 'Update Series' ),
'add_new_item' => __( 'Add New Series' ),
'new_item_name' => __( 'New Series Name' ),
'menu_name' => __( 'Series' ),
),
'query_var' => true,
'rewrite' => true,
)
);
}
}
add_action( 'init', 'largo_custom_taxonomies' );
/**
* Determines whether a post is in a series
* Expects to be called from within The Loop.
*
* @uses global $post
* @uses largo_is_series_enabled
* @return bool
* @since 1.0
*/
function largo_post_in_series( $post_id = NULL ) {
if ( !largo_is_series_enabled() ) return false;
global $post;
$the_id = ($post_id) ? $post_id : $post->ID ;
$features = get_the_terms( $the_id, 'series' );
return ( $features ) ? true : false;
}
/**
* Outputs custom taxonomy terms attached to a post
*
* @return array of terms
* @since 1.0
*/
function largo_custom_taxonomy_terms( $post_id ) {
$taxonomies = apply_filters( 'largo_custom_taxonomies', array( 'series' ) );
$post_terms = array();
foreach ( $taxonomies as $tax ) {
if ( taxonomy_exists( $tax ) ) {
$terms = get_the_terms( $post_id, $tax );
if ( $terms )
$post_terms = array_merge( $post_terms, $terms );
}
}
return $post_terms;
}
/**
* Output format for the series custom taxonomy at the bottom of single posts
*
* @param $term array the term we want to output
* @since 1.0
*/
if ( ! function_exists( 'largo_term_to_label' ) ) {
function largo_term_to_label( $term ) {
return sprintf( '<div class="series-label"><h5><a href="%1$s">%2$s</a><a class="rss-link" href="%3$s"></a></h5><p>%4$s</p></div>',
get_term_link( $term, $term->taxonomy ),
esc_attr( $term->name ),
get_term_feed_link( $term->term_id, $term->taxonomy ),
esc_attr( strip_tags ( $term->description )
));
}
}
/**
* Helper function for getting posts in proper landing-page order for a series
*
* @uses largo_is_series_enabled
* @param integer series term id
* @param integer number of posts to fetch, defaults to all
*/
function largo_get_series_posts( $series_id, $number = -1 ) {
// If series are not enabled, then there are no posts in a series.
if ( !largo_is_series_enabled() ) return;
// get the cf-tax-landing
$args = array(
'post_type' => 'cftl-tax-landing',
'posts_per_page' => 1,
'tax_query' => array( array(
'taxonomy' => 'series',
'field' => 'id',
'terms' => $series_id
)),
);
$landing = new WP_Query( $args );
$series_args = array(
'post_type' => 'post',
'tax_query' => array(
array(
'taxonomy' => 'series',
'field' => 'id',
'terms' => $series_id
)
),
'order' => 'DESC',
'orderby' => 'date',
'posts_per_page' => $number
);
if ( $landing->found_posts ) {
$landing->next_post();
$order = get_post_meta( $landing->post->ID, 'post_order', TRUE );
switch ( $order ) {
case 'ASC':
$series_args['order'] = 'ASC';
break;
case 'custom':
$series_args['orderby'] = 'series_custom';
break;
case 'featured, DESC':
case 'featured, ASC':
$series_args['orderby'] = $order;
break;
}
}
$series_posts = new WP_Query( $series_args );
if ( $series_posts->found_posts ) return $series_posts;
return false;
}
/**
* Filter: post_type_link
*
* Filter post permalinks for the Landing Page custom post type.
* Replace direct post link with the link for the associated
* Series taxonomy term, using the most recently created term
* if multiple are set.
*
* This filter overrides the wp-taxonomy-landing filter,
* which attempts to use the link for ANY term from ANY taxonomy.
* Largo really only cares about the Series taxonomy.
*
* @since 0.5
* @return filtered $post_link, replacing a Landing Page link with its Series link as needed
*/
function largo_series_landing_link($post_link, $post) {
// Get configuration setting for Custom Landing Pages
$opt_custom_landing_enabled = of_get_option('custom_landing_enabled');
$custom_landing_enabled = !empty($opt_custom_landing_enabled);
// Only process Landing Page post type when Series Landing Pages are enabled
if ( "cftl-tax-landing" == $post->post_type && $custom_landing_enabled ) {
// Get all series taxonomy terms for this landing page
$series_terms = wp_get_object_terms(
$post->ID,
'series',
array('orderby' => 'term_id', 'order' => 'DESC', 'fields' => 'slugs')
);
// Only proceed if we successfully found at least 1 series term
if ( !is_wp_error($series_terms) && !empty($series_terms) ) {
// Get the link for the first series term
// (ordered by the highest ID in the case of multiple terms)
$term_link = get_term_link($series_terms[0], 'series');
// Only proceed if we successfully found the term link
if ( !is_wp_error($term_link) && strlen(trim($term_link)) ) {
$post_link = esc_url($term_link);
}
}
}
// Return the filtered link
return $post_link;
}
// wp-taxonomy-landing library filters at priority 10.
// We must filter AFTER that.
add_filter('post_type_link', 'largo_series_landing_link', 22, 2);
/**
* Helper to get the Series Landing Page for a given series.
*
* @param Object|id|string $series
* @return array An array of all WP_Post objects answering the description of this series. May be 0, 1 or conceivably many.
*/
function largo_get_series_landing_page_by_series($series) {
if ( !is_object($series) ) {
if ( is_int( $series ) ) {
$series = get_term( $series, $taxonomy );
} else {
$series = get_term_by( 'slug', $series, $taxonomy );
}
}
// get the cftl-tax-landing
$args = array(
'post_type' => 'cftl-tax-landing',
'posts_per_page' => 1,
'tax_query' => array( array(
'taxonomy' => 'series',
'field' => 'id',
'terms' => $series->term_id
)),
);
$landing = new WP_Query( $args );
return $landing->posts;
}
/**
* Helper for getting posts in a category archive, excluding featured posts.
*
* @param WP_Query $query
* @uses largo_get_featured_posts_in_category
*/
function largo_category_archive_posts( $query ) {
//don't muck with admin, non-categories, etc
if ( !$query->is_category() || !$query->is_main_query() || is_admin() ) return;
// If this has been disabled by an option, do nothing
if ( of_get_option('hide_category_featured') == true ) return;
// get the featured posts
$featured_posts = largo_get_featured_posts_in_category($query->get('category_name'));
// get the IDs from the featured posts
$featured_post_ids = array();
foreach ( $featured_posts as $fpost )
$featured_post_ids[] = $fpost->ID;
$query->set('post__not_in', $featured_post_ids);
}
add_action('pre_get_posts', 'largo_category_archive_posts', 15);
/**
* Get posts marked as "Featured in category" for a given category name.
*
* @param string $category_name the category to retrieve featured posts for.
* @param integer $number total number of posts to return, backfilling with regular posts as necessary.
* @since 0.5
*/
function largo_get_featured_posts_in_category($category_name, $number=5) {
$args = array(
'category_name' => $category_name,
'numberposts' => $number,
'post_status' => 'publish',
);
$tax_query = array(
'tax_query' => array(
array(
'taxonomy' => 'prominence',
'field' => 'slug',
'terms' => 'category-featured',
)
)
);
// Get the featured posts
$featured_posts = get_posts(array_merge($args, $tax_query));
// Backfill with regular posts if necessary
if (count( $featured_posts ) < (int) $number) {
$needed = (int) $number - count( $featured_posts );
$regular_posts = get_posts(array_merge($args, array(
'numberposts' => $needed,
'post__not_in' => array_map(function($x) { return $x->ID; }, $featured_posts)
)));
$featured_posts = array_merge($featured_posts, $regular_posts);
}
return $featured_posts;
}
/**
* Return the first featured image thumbnail found in a given array of WP_Posts
*
* Useful if you wint to create a thumbnail for a given taxonomy
*
* @param array An array of WP_Post objects to iterate over
* @return str|false The HTML for the image, or false if no images were found.
* @since 0.5.3
* @uses largo_has_featured_media
*/
function largo_featured_thumbnail_in_post_array($array) {
$thumb = '';
foreach ($array as $post) {
$thumb = get_the_post_thumbnail($post->ID);
if ($thumb != '') return $thumb;
}
return $thumb;
}
/**
* Return the first headline link for an array of WP_Posts
*
* Useful if you want to link to an example post in a series.
*
* @param array An array of WP_Post objects to iterate over
* @return str The HTML for the link
* @since 0.5.3
*/
function largo_first_headline_in_post_array($array) {
$headline = '';
foreach ($array as $post) {
$headline = sprintf('<a href="%s">%s</a>',
get_permalink($post->ID),
get_the_title($post->ID)
);
if ($headline != '') return $headline;
}
return $headline;
}
/**
* If the option in Advanced Options is unchecked, unregister the "Series" taxonomy
*
* @uses largo_is_series_enabled
* @since 0.4
*/
function unregister_series_taxonomy() {
if ( !largo_is_series_enabled() ) {
register_taxonomy( 'series', array(), array('show_in_nav_menus' => false) );
}
}
add_action( 'init', 'unregister_series_taxonomy', 999 );
/**
* If the option in Advanced Options is unchecked, unregister the "Post Types" taxonomy
*
* @uses of_get_option
* @since 0.4
*/
function unregister_post_types_taxonomy() {
if ( of_get_option('post_types_enabled') == 0 ) {
register_taxonomy( 'post-type', array(), array('show_in_nav_menus' => false) );
}
}
add_action( 'init', 'unregister_post_types_taxonomy', 999 );