This repository was archived by the owner on Dec 27, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathcpt-onomy.php
2485 lines (2038 loc) · 86.3 KB
/
cpt-onomy.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
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
<?php
// Instantiate the class.
global $cpt_onomy;
$cpt_onomy = new CPT_TAXONOMY();
/**
* Holds the functions needed for using a custom post type as a taxonomy.
*
* @since 1.0
*/
class CPT_TAXONOMY {
/**
* Adds WordPress hooks (actions and filters).
*
* @since 1.0
*/
public function __construct() {
// Function filters.
add_filter( 'get_terms', array( $this, 'get_terms' ), 1, 3 );
add_filter( 'get_object_terms', array( $this, 'get_object_terms' ), 1, 4 );
// Other filters.
add_filter( 'get_terms_args', array( $this, 'adjust_get_terms_args' ), 1, 2 );
add_filter( 'get_the_terms', array( $this, 'get_the_terms' ), 1, 3 );
}
/**
* This function takes an object's information and creates a term object.
*
* As of version 1.2, you can hook into the 'term_description' or
* '{$taxonomy}_description' filter to add a description to your terms.
*
* The variable type (object or array) for the returned $term will match the set type of the passed $object.
*
* @since 1.0
* @uses $cpt_onomies_manager
* @param array|object $object - the information for the object you are converting.
* @param boolean $get_count - whether to get the term count.
* @return array|object - the information for the term you have created.
*/
private function convert_object_to_cpt_onomy_term( $object, $get_count = true ) {
global $cpt_onomies_manager;
// If its empty, then there's no point.
if ( empty( $object ) ) {
return $object;
}
// Make sure the term is an object.
$term = (object) $object;
// Make sure its a CPT-onomy.
if ( ! $cpt_onomies_manager->is_registered_cpt_onomy( $term->post_type ) ) {
return $object;
}
/*
* sanitize_term_field() lets you apply the 'term_description'
* or '{$taxonomy}_description' filter to tweak the description,
* if desired. Maybe you want the description to be a custom field?
* or the post content. Just return that info in the filter!
*/
$term = array(
'term_id' => $term->ID,
'name' => apply_filters( 'the_title', $term->post_title, $term->ID ),
'slug' => $term->post_name,
'term_group' => $term->post_parent,
'term_taxonomy_id' => 0,
'taxonomy' => $term->post_type,
'description' => sanitize_term_field( 'description', '', $term->ID, $term->post_type, 'display' ),
'parent' => $term->post_parent,
);
if ( $get_count ) {
$term['count'] = $this->get_term_count( $term['term_id'], $term['taxonomy'] );
}
if ( is_object( $object ) ) {
return (object) $term;
}
return $term;
}
/**
* Since setting the argument 'fields' to 'count' will not work with CPT-onomies,
* this gets rid of that field and adds a custom count argument that's applied
* in our get_terms() filter function. This allows the WP function wp_count_terms()
* to work with CPT-onomies.
*
* This function is applied to the filter 'get_terms_args'.
* The filter 'get_terms_args' was not added to get_terms() until 3.1 so this
* function will not work before WordPress version 3.1.
*
* @since 1.0.2
* @uses $cpt_onomies_manager
* @param array $args - original get_terms() arguments.
* @param array $taxonomies - the taxonomies we're getting terms from.
* @return array - the filtered $args
*/
public function adjust_get_terms_args( $args, $taxonomies ) {
global $cpt_onomies_manager;
// This function only filters registered CPT-onomies.
$cpt_taxonomies = array();
foreach ( $taxonomies as $taxonomy ) {
if ( $cpt_onomies_manager->is_registered_cpt_onomy( $taxonomy ) ) {
$cpt_taxonomies[] = $taxonomy;
}
}
// This means there are no CPT-onomies so wrap things up.
if ( empty( $cpt_taxonomies ) ) {
return $args;
}
// Change 'fields' to 'ids' and add a custom count argument.
if ( isset( $args['fields'] ) && 'count' == $args['fields'] ) {
$args['fields'] = 'ids';
$args['cpt_onomy_get_count'] = true;
}
return $args;
}
/**
* Whenever get_the_terms() is called, we need to
* clear the cache that WordPress stores.
*
* @since 1.3.5
* @param array|WP_Error $terms List of attached terms, or WP_Error on failure.
* @param int $post_id Post ID.
* @param string $taxonomy Name of the taxonomy.
* @return array|WP_Error $terms List of attached terms, or WP_Error on failure.
*/
public function get_the_terms( $terms, $post_id, $taxonomy ) {
// Clear the cache that WordPress adds.
wp_cache_delete( $post_id, "{$taxonomy}_relationships" );
return $terms;
}
/**
* This function mimics the WordPress function get_term()
* because we cannot hook into the function without receiving errors.
*
* @since 1.0
* @uses $cpt_onomies_manager
* @param int|object $term If integer, will get from database. If object will apply filters and return $term.
* @param string $taxonomy Taxonomy name that $term is part of.
* @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N.
* @param string $filter Optional, default is raw or no WordPress defined filter will applied.
* @return mixed|null|WP_Error Term Row from database. Will return null if $term is empty. If taxonomy does not
* exist then WP_Error will be returned.
*/
public function get_term( $term, $taxonomy, $output = OBJECT, $filter = 'raw' ) {
global $cpt_onomies_manager;
$null = null;
if ( empty( $term ) ) {
$error = new WP_Error( 'invalid_term', __( 'Empty Term', 'cpt-onomies' ) );
return $error;
}
if ( ! taxonomy_exists( $taxonomy ) ) {
$error = new WP_Error( 'invalid_taxonomy', __( 'Invalid Taxonomy', 'cpt-onomies' ) );
return $error;
}
/*
* This function only processes registered CPT-onomies.
* If this is a normal taxonomy, then use the WordPress function.
*/
if ( ! $cpt_onomies_manager->is_registered_cpt_onomy( $taxonomy ) ) {
return get_term( $term, $taxonomy, $output, $filter );
}
if ( is_object( $term ) && empty( $term->filter ) ) {
wp_cache_add( $term->term_id, $term, $taxonomy );
$_term = $term;
} else {
if ( is_object( $term ) ) {
$term = $term->term_id;
}
if ( ! $term = (int) $term ) {
return $null;
}
if ( ! $_term = wp_cache_get( $term, $taxonomy ) ) {
$_term = $this->convert_object_to_cpt_onomy_term( get_post( $term ) );
if ( ! $_term ) {
return $null;
}
wp_cache_add( $term, $_term, $taxonomy );
}
}
$_term = apply_filters( 'get_term', $_term, $taxonomy );
$_term = apply_filters( "get_$taxonomy", $_term, $taxonomy );
$_term = sanitize_term( $_term, $taxonomy, $filter );
if ( OBJECT == $output ) {
return $_term;
} elseif ( ARRAY_A == $output ) {
$__term = get_object_vars( $_term );
return $__term;
} elseif ( ARRAY_N == $output ) {
$__term = array_values( get_object_vars( $_term ) );
return $__term;
} else {
return $_term;
}
}
/**
* This function mimics the WordPress function get_term_by()
* because we cannot hook into the function without receiving errors.
*
* @since 1.0
* @uses $wpdb, $cpt_onomies_manager
* @param string $field Either 'slug', 'name', or 'id'.
* @param string|int $value Search for this term value.
* @param string $taxonomy Taxonomy Name.
* @param string $output Constant OBJECT, ARRAY_A, or ARRAY_N.
* @param string $filter Optional, default is raw or no WordPress defined filter will applied.
* @param int $parent allows to get a term by its parent's term id.
* @return mixed Term Row from database. Will return false if $taxonomy does not exist or $term was not found.
*/
public function get_term_by( $field, $value, $taxonomy, $output = OBJECT, $filter = 'raw', $parent = 0 ) {
global $wpdb, $cpt_onomies_manager;
if ( ! taxonomy_exists( $taxonomy ) ) {
return false;
}
/*
* This function only processes registered CPT-onomies.
* If this is a normal taxonomy, then use the WordPress function.
*/
if ( ! $cpt_onomies_manager->is_registered_cpt_onomy( $taxonomy ) ) {
return get_term_by( $field, $value, $taxonomy, $output, $filter );
}
if ( $parent > 0 ) {
$parent = $wpdb->prepare( ' AND wpposts.post_parent = %d', $parent );
} else {
$parent = null;
}
if ( 'slug' == $field ) {
$value = sanitize_title( $value );
if ( empty( $value ) ) {
return false;
}
// Get eligible post types.
$eligible_post_types = ( $tax = get_taxonomy( $taxonomy ) ) && isset( $tax->object_type ) ? $tax->object_type : array();
// Find term and term count.
$query = $wpdb->prepare( "SELECT (SELECT COUNT(*) FROM {$wpdb->postmeta} wpcountmeta INNER JOIN {$wpdb->posts} wpcountposts ON wpcountposts.ID = wpcountmeta.post_id AND wpcountposts.post_status = 'publish' AND wpcountposts.post_type IN (%s) WHERE wpcountmeta.meta_key = %s AND wpcountmeta.meta_value = wpposts.ID) AS count, wpposts.* FROM {$wpdb->posts} wpposts WHERE wpposts.post_type = %s AND wpposts.post_name = %s and wpposts.post_status = 'publish'", implode( "','", $eligible_post_types, CPT_ONOMIES_POSTMETA_KEY, $taxonomy, $value ) );
} elseif ( 'name' == $field ) {
// Assume already escaped.
$value = stripslashes( $value );
// Get eligible post types.
$eligible_post_types = ( $tax = get_taxonomy( $taxonomy ) ) && isset( $tax->object_type ) ? $tax->object_type : array();
// Find term and term count.
$query = $wpdb->prepare( "SELECT (SELECT COUNT(*) FROM {$wpdb->postmeta} wpcountmeta INNER JOIN {$wpdb->posts} wpcountposts ON wpcountposts.ID = wpcountmeta.post_id AND wpcountposts.post_status = 'publish' AND wpcountposts.post_type IN (%s) WHERE wpcountmeta.meta_key = %s AND wpcountmeta.meta_value = wpposts.ID) AS count, wpposts.* FROM {$wpdb->posts} wpposts WHERE wpposts.post_type = %s AND wpposts.post_title = %s and wpposts.post_status = 'publish'", implode( "','", $eligible_post_types, CPT_ONOMIES_POSTMETA_KEY, $taxonomy, $value ) );
} else {
$term = $this->get_term( (int) $value, $taxonomy, $output, $filter );
if ( is_wp_error( $term ) ) {
return false;
}
return $term;
}
// Get the term.
$term = $wpdb->get_row( $query . $parent );
if ( ! $term ) {
return false;
}
// Save the term count and remove from $term before conversion.
$term_count = $term->count;
unset( $term->count );
// Dont get the count, we'll add from before.
$term = $this->convert_object_to_cpt_onomy_term( $term, false );
if ( ! $term ) {
return false;
}
// Add count.
$term->count = $term_count;
wp_cache_add( $term->term_id, $term, $taxonomy );
$term = apply_filters( 'get_term', $term, $taxonomy );
$term = apply_filters( "get_$taxonomy", $term, $taxonomy );
$term = sanitize_term( $term, $taxonomy, $filter );
if ( OBJECT == $output ) {
return $term;
} elseif ( ARRAY_A == $output ) {
return get_object_vars( $term );
} elseif ( ARRAY_N == $output ) {
return array_values( get_object_vars( $term ) );
}
return $term;
}
/**
* As of 1.0.2, wp_count_terms() works with CPT-onomies so this
* function is now deprecated and will send you to the WordPress function.
*
* As of version 1.0.3, the WordPress minimum version is 3.1 and the filter
* 'get_terms_args' that allows CPT-onomies to work with wp_count_terms() was
* added in 3.1 so everyone is sent to the WordPress function.
*
* @param string $taxonomy Taxonomy name.
* @param array|string $args Overwrite defaults. See get_terms().
* @return int How many terms are in $taxonomy
*/
public function wp_count_terms( $taxonomy, $args = array() ) {
return wp_count_terms( $taxonomy, $args );
}
/**
* Returns an array of term counts for a
* specific CPT-onomy indexed by the term ID.
*
* Stores the term counts in the
* WP cache to help with query load.
*
* @since 1.3.3
* @param csv|string - $taxonomies - CPT-onomy names.
* @param csv|array - $term_ids - you can pass specific term IDs instead of all terms (will not use cache though).
* @return array|false - array of term counts indexed by term ID or false if error
*/
private function get_terms_count( $taxonomies, $term_ids = null ) {
global $cpt_onomies_manager, $wpdb;
// Make sure the taxonomies is an array.
if ( ! empty( $taxonomies ) && ! is_array( $taxonomies ) ) {
$taxonomies = explode( ',', $taxonomies );
}
// Make sure they are valid CPT-onomies.
foreach ( $taxonomies as $index => $taxonomy ) {
// If it's not a valid CPT-onomy...
if ( ! $cpt_onomies_manager->is_registered_cpt_onomy( $taxonomy ) ) {
// Remove this taxonomy.
unset( $taxonomies[ $index ] );
}
}
// If we have no valid taxonomies then there's no point in continuing.
if ( ! $taxonomies ) {
return false;
}
// We're gonna store the term counts in an array indexed by term ID.
$terms_count = array();
// Make sure term IDs is an array.
if ( ! empty( $term_ids ) && ! is_array( $term_ids ) ) {
$term_ids = explode( ',', $term_ids );
}
// This will hold taxonomies that need to be queried.
$taxonomies_to_query = array();
// First, see if we can get from the cache.
foreach ( $taxonomies as $taxonomy ) {
// Checking the cache...
if ( ( $terms_count_from_cache = wp_cache_get( $taxonomy, 'cpt_onomies_terms_count' ) )
&& false !== $terms_count_from_cache
&& is_array( $terms_count_from_cache ) ) {
// If we only want specific term IDs...
if ( ! empty( $term_ids ) ) {
// Then only get specific term IDs from the cache.
foreach ( $terms_count_from_cache as $term_id => $term_id_count ) {
if ( ! in_array( $term_id, $term_ids ) ) {
unset( $terms_count_from_cache[ $term_id ] );
}
}
}
// Add the counts from the cache.
$terms_count += $terms_count_from_cache;
} else {
// We need to query this taxonomy.
$taxonomies_to_query[] = $taxonomy;
}
}
// These taxonomies weren't cached so we need to query the database.
if ( $taxonomies_to_query ) {
// Build an array of eligible post types for these CPT-onomies.
$eligible_post_types = array();
// Get eligible post types for each taxonomy.
foreach ( $taxonomies_to_query as $index => $taxonomy ) {
// Get eligible post types.
$tax_eligible_post_types = ( $tax = get_taxonomy( $taxonomy ) ) && isset( $tax->object_type ) ? $tax->object_type : null;
// If we have some, then merge with the array.
if ( $tax_eligible_post_types ) {
$eligible_post_types = array_merge( $eligible_post_types, $tax_eligible_post_types );
}
}
// Make sure the eligible post types are unique.
$eligible_post_types = array_unique( $eligible_post_types );
// If we have no eligible post types then there's no point in continuing.
if ( $eligible_post_types ) {
// Build the terms count query.
$terms_count_query = "SELECT meta.meta_value AS ID, terms.post_type AS taxonomy, COUNT(meta.meta_value) AS count
FROM {$wpdb->postmeta} meta
INNER JOIN {$wpdb->posts} objects
ON objects.ID = meta.post_id
AND objects.post_type IN ( '" . implode( "','", $eligible_post_types ) . "' )
AND objects.post_status = 'publish'";
/*
* If we have no specific term IDs then we have to
* join the posts table to match taxonomy/post type.
*/
if ( empty( $term_ids ) ) {
$terms_count_query .= " INNER JOIN {$wpdb->posts} terms
ON terms.ID = meta.meta_value
AND terms.post_type IN ( '" . implode( "','", $taxonomies_to_query ) . "' )
AND objects.post_status = 'publish'";
}
$terms_count_query .= ' WHERE meta.meta_key = %s';
// If we have term IDs.
if ( ! empty( $term_ids ) ) {
$terms_count_query .= " AND meta.meta_value IN ( '" . implode( "','", $term_ids ) . "' )";
}
$terms_count_query .= ' GROUP BY meta.meta_value';
// Get term count from the database.
if ( $terms_count_from_db = $wpdb->get_results( $wpdb->prepare( $terms_count_query, CPT_ONOMIES_POSTMETA_KEY ) ) ) {
// If no specific term IDs, separate count by taxonomy for the cache.
$terms_count_by_taxonomy = array();
// If we have a posts count, we need to rearrange the array.
foreach ( $terms_count_from_db as $terms_count_index => $terms_count_item ) {
// Get the term count.
$term_count = isset( $terms_count_item->count ) && $terms_count_item->count > 0 ? $terms_count_item->count : 0;
// Store count by term ID.
$terms_count[ $terms_count_item->ID ] = $term_count;
// Store count by taxonomy and term ID.
$terms_count_by_taxonomy[ $terms_count_item->taxonomy ][ $terms_count_item->ID ] = $term_count;
}
// If no specific term IDs, set the cache.
if ( empty( $term_ids ) && ! empty( $terms_count_by_taxonomy ) ) {
foreach ( $terms_count_by_taxonomy as $taxonomy => $taxonomy_terms_count ) {
// Store the terms count for this taxonomy.
wp_cache_set( $taxonomy, $taxonomy_terms_count, 'cpt_onomies_terms_count' );
}
}
}
}
}
return $terms_count;
}
/**
* This function determines how many times a term has been assigned to an object.
*
* @since 1.0
* @uses $wpdb, $cpt_onomies_manager
* @param int $term_id - the ID of the term you're counting.
* @param string $taxonomy - the taxonomy the term belongs to.
* @return int - the number of times a term has been assigned to an object
*/
public function get_term_count( $term_id, $taxonomy ) {
global $wpdb, $cpt_onomies_manager;
// Must have a term ID and be a CPT-onomy.
if ( is_numeric( $term_id ) && $cpt_onomies_manager->is_registered_cpt_onomy( $taxonomy ) ) {
// We're only counting the posts who are supposed to be associated with this taxonomy.
if ( ( $eligible_post_types = ( $tax = get_taxonomy( $taxonomy ) ) && isset( $tax->object_type ) ? $tax->object_type : array() )
&& ! empty( $eligible_post_types ) ) {
return $wpdb->get_var( $wpdb->prepare( "SELECT COUNT(*) FROM {$wpdb->postmeta} wpcountmeta INNER JOIN {$wpdb->posts} wpposts_object ON wpposts_object.ID = wpcountmeta.post_id AND wpposts_object.post_status = 'publish' AND wpposts_object.post_type IN ( '" . implode( "','", $eligible_post_types ) . "' ) INNER JOIN {$wpdb->posts} wpposts_term ON wpposts_term.ID = wpcountmeta.meta_value AND wpposts_term.post_status = 'publish' AND wpposts_term.post_type = %s WHERE wpcountmeta.meta_key = %s AND wpcountmeta.meta_value = %d", $taxonomy, CPT_ONOMIES_POSTMETA_KEY, $term_id ) );
}
}
return 0;
}
/**
* This function mimics the WordPress function get_term_children()
* because we cannot hook into the function without receiving errors.
*
* As of WordPress 3.3.2, CPT-onomies will work with get_term_children()
* but I'm not a fan of how WordPress stores the children ids in an option.
*
* @since 1.0
* @uses $wpdb, $cpt_onomies_manager
* @param string $term_id ID of Term to get children.
* @param string $taxonomy Taxonomy Name.
* @return array|WP_Error List of Term Objects. WP_Error returned if $taxonomy does not exist
*/
public function get_term_children( $term_id, $taxonomy ) {
global $wpdb, $cpt_onomies_manager;
if ( ! taxonomy_exists( $taxonomy ) ) {
return new WP_Error( 'invalid_taxonomy', __( 'Invalid Taxonomy', 'cpt-onomies' ) );
}
/*
* This function only processes registered CPT-onomies.
* If this is a normal taxonomy, then use the WordPress function.
*/
if ( ! $cpt_onomies_manager->is_registered_cpt_onomy( $taxonomy ) ) {
return get_term_children( $term_id, $taxonomy );
}
$term_id = intval( $term_id );
$children = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE post_parent = %d AND post_status = 'publish' AND post_type = %s", $term_id, $taxonomy ) );
if ( empty( $children ) ) {
return array();
}
foreach ( $children as $child_id ) {
$children = array_merge( $children, $this->get_term_children( $child_id, $taxonomy ) );
}
return $children;
}
/**
* Get an array of ancestor IDs for a given term.
*
* @since 1.1
* @uses $cpt_onomies_manager
* @param int $term_id - The ID of the term for which we'll be retrieving ancestors.
* @param string $taxonomy - the taxonomy name.
* @return array|WP_Error List of Term Objects. WP_Error returned if $taxonomy does not exist
*/
public function get_term_ancestors( $term_id = 0, $taxonomy = '' ) {
global $cpt_onomies_manager;
if ( ! taxonomy_exists( $taxonomy ) ) {
return new WP_Error( 'invalid_taxonomy', __( 'Invalid Taxonomy', 'cpt-onomies' ) );
}
/*
* This function only processes registered CPT-onomies.
* If this is a normal taxonomy, then use the WordPress function.
*/
if ( ! $cpt_onomies_manager->is_registered_cpt_onomy( $taxonomy ) ) {
return get_ancestors( $term_id, $taxonomy );
}
$term_id = (int) $term_id;
$ancestors = array();
if ( empty( $term_id ) ) {
return apply_filters( 'get_ancestors', $ancestors, $term_id, $taxonomy );
}
if ( is_taxonomy_hierarchical( $taxonomy ) ) {
$term = $this->get_term( $term_id, $taxonomy );
while ( ! is_wp_error( $term ) && ! empty( $term->parent ) && ! in_array( $term->parent, $ancestors ) ) {
$ancestors[] = (int) $term->parent;
$term = $this->get_term( $term->parent, $taxonomy );
}
}
return apply_filters( 'get_ancestors', $ancestors, $term_id, $taxonomy );
}
/**
* This function mimics the WordPress function term_exists()
* because we cannot hook into the function without receiving errors.
*
* @since 1.0
* @uses $wpdb, $cpt_onomies_manager
* @param int|string $term The term to check.
* @param string $taxonomy The taxonomy name to use.
* @param int $parent ID of parent term under which to confine the exists search.
* @return mixed Get the term id or Term Object, if exists.
*/
public function term_exists( $term, $taxonomy = '', $parent = 0 ) {
global $wpdb, $cpt_onomies_manager;
if ( is_int( $term ) ) {
if ( 0 == $term ) {
return 0;
}
/*
* This function only processes registered CPT-onomies.
* If this is a normal taxonomy, then use the WordPress function.
*/
if ( ! empty( $taxonomy ) && ! $cpt_onomies_manager->is_registered_cpt_onomy( $taxonomy ) ) {
return term_exists( $term, $taxonomy, $parent );
} elseif ( ! empty( $taxonomy ) ) {
return $this->get_term( $term, $taxonomy );
}
// Get term info and convert.
$term = $this->convert_object_to_cpt_onomy_term( $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->posts} WHERE ID = %d AND post_status = 'publish'", $term ) ) );
if ( ! $term ) {
return 0;
}
// Make sure this term belongs to a CPT-onomy.
if ( $cpt_onomies_manager->is_registered_cpt_onomy( $term->taxonomy ) ) {
return $term;
}
return 0;
}
$term = trim( stripslashes( $term ) );
if ( '' === ( $slug = sanitize_title( $term ) ) ) {
return 0;
}
/*
* This function only processes registered CPT-onomies.
* If this is a normal taxonomy, then use the WordPress function.
*/
if ( ! empty( $taxonomy ) && ! $cpt_onomies_manager->is_registered_cpt_onomy( $taxonomy ) ) {
return term_exists( $term, $taxonomy, $parent );
} elseif ( ! empty( $taxonomy ) ) {
// Check for parent.
$parent = (int) $parent;
if ( $parent > 0 ) {
$parent = ' AND wp_posts.post_parent = ' . $parent;
} else {
$parent = null;
}
// Get CPT-onomy's eligible post types.
$eligible_post_types = ( $tax = get_taxonomy( $taxonomy ) ) && isset( $tax->object_type ) ? $tax->object_type : array();
/*
* Check for name first.
*
* Get term info and term count.
*/
$result = $wpdb->get_row( $wpdb->prepare( "SELECT (SELECT COUNT(*) FROM {$wpdb->postmeta} wp_count_meta INNER JOIN {$wpdb->posts} wp_count_posts ON wp_count_posts.ID = wp_count_meta.post_id AND wp_count_posts.post_type IN ('" . implode( "','", $eligible_post_types ) . "') AND wp_count_posts.post_status = 'publish' WHERE wp_count_meta.meta_value = wp_posts.ID) AS count, wp_posts.* FROM {$wpdb->posts} wp_posts WHERE wp_posts.post_title = %s AND wp_posts.post_status = 'publish'" . $parent . " AND wp_posts.post_type = %s AND wp_posts.post_status = 'publish'", $term, $taxonomy ) );
/*
* Check for slug.
*
* Get term info and term count.
*/
if ( empty( $result ) ) {
$result = $wpdb->get_row( $wpdb->prepare( "SELECT (SELECT COUNT(*) FROM {$wpdb->postmeta} wp_count_meta INNER JOIN {$wpdb->posts} wp_count_posts ON wp_count_posts.ID = wp_count_meta.post_id AND wp_count_posts.post_type IN ('" . implode( "','", $eligible_post_types ) . "') AND wp_count_posts.post_status = 'publish' WHERE wp_count_meta.meta_value = wp_posts.ID) AS count, wp_posts.* FROM {$wpdb->posts} wp_posts WHERE wp_posts.post_name = %s" . $parent . " AND wp_posts.post_type = %s AND wp_posts.post_status = 'publish'", $term, $taxonomy ) );
}
if ( ! empty( $result ) && $cpt_onomies_manager->is_registered_cpt_onomy( $result->post_type ) ) {
// Save count and remove from term for conversion.
$term_count = $result->count;
unset( $result->count );
/*
* Convert term.
*
* Dont get count, we'll add it back.
*/
$term = $this->convert_object_to_cpt_onomy_term( $result, false );
if ( ! $term ) {
return 0;
}
// Add count.
$term->count = $term_count;
return $term;
}
} else {
// Check for parent.
$parent = (int) $parent;
if ( $parent > 0 ) {
$parent = ' AND post_parent = ' . $parent;
} else {
$parent = null;
}
// Check for name first.
$result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->posts} WHERE post_title = %s {$parent} AND post_status = 'publish'", $term ) );
// Check for slug.
if ( empty( $result ) ) {
$result = $wpdb->get_row( $wpdb->prepare( "SELECT * FROM {$wpdb->posts} WHERE post_name = %s {$parent} AND post_status = 'publish'", $term ) );
}
if ( ! empty( $result ) && $cpt_onomies_manager->is_registered_cpt_onomy( $result->post_type ) ) {
return $this->convert_object_to_cpt_onomy_term( $result );
}
}
return 0;
}
/**
* This function mimics the WordPress function get_term_link()
* because we cannot hook into the function without receiving errors
* and returning an incorrect link due to the rewrite war between
* custom post types and taxonomies AND because we create our own
* rewrite rules.
*
* @since 1.0
* @uses $cpt_onomies_manager
* @param object|int|string $term.
* @param string $taxonomy (optional if $term is object).
* @return string|WP_Error HTML link to taxonomy term archive on success, WP_Error if term does not exist.
*/
public function get_term_link( $term, $taxonomy ) {
global $cpt_onomies_manager;
/*
* This function only processes registered CPT-onomies.
* If this is a normal taxonomy, then use the WordPress function.
*/
if ( ! $cpt_onomies_manager->is_registered_cpt_onomy( $taxonomy ) ) {
return get_term_link( $term, $taxonomy );
}
if ( ! is_object( $term ) ) {
if ( is_int( $term ) ) {
$term = $this->get_term( $term, $taxonomy );
} else {
$term = $this->get_term_by( 'slug', $term, $taxonomy );
}
}
if ( ! is_object( $term ) ) {
$term = new WP_Error( 'invalid_term', __( 'Empty Term', 'cpt-onomies' ) );
}
if ( is_wp_error( $term ) ) {
return $term;
}
$taxonomy = $term->taxonomy;
$termlink = null;
$slug = $term->slug;
$t = get_taxonomy( $taxonomy );
/*
* Link to CPT-onomy archive page.
*
* If no archive page, link to CPT post.
*/
if ( isset( $t->cpt_onomy_archive_slug ) && ! empty( $t->cpt_onomy_archive_slug ) ) {
$termlink = $t->cpt_onomy_archive_slug;
if ( $t->hierarchical ) {
$hierarchical_slugs = array();
$ancestors = get_ancestors( $term->term_id, $taxonomy );
foreach ( (array) $ancestors as $ancestor ) {
$ancestor_term = $this->get_term( $ancestor, $taxonomy );
$hierarchical_slugs[] = $ancestor_term->slug;
}
$hierarchical_slugs = array_reverse( $hierarchical_slugs );
$hierarchical_slugs[] = $slug;
// Replace the variables ($post_type and $term).
$slug = implode( '/', $hierarchical_slugs );
}
// Replace the variables ($post_type and $term).
$termlink = str_replace( array( '$post_type', '$term_slug', '$term_id' ), array( $taxonomy, $slug, $term->term_id ), $termlink );
$termlink = home_url( user_trailingslashit( $termlink, 'category' ) );
} else {
$termlink = get_permalink( $term->term_id );
}
// Back Compat filters.
if ( 'post_tag' == $taxonomy ) {
$termlink = apply_filters( 'tag_link', $termlink, $term->term_id );
} elseif ( 'category' == $taxonomy ) {
$termlink = apply_filters( 'category_link', $termlink, $term->term_id );
}
return apply_filters( 'term_link', $termlink, $term, $taxonomy );
}
/**
* This function mimics the WordPress function get_edit_term_link()
* because we cannot hook into the function without receiving errors.
*
* @since 1.0
* @uses $cpt_onomies_manager
* @param int $term_id Term ID.
* @param string $taxonomy Taxonomy.
* @param string $object_type The object type.
* @return string
*/
public function get_edit_term_link( $term_id, $taxonomy, $object_type = '' ) {
global $cpt_onomies_manager;
/*
* This function only processes registered CPT-onomies.
* If this is a normal taxonomy, then use the WordPress function.
*/
if ( ! $cpt_onomies_manager->is_registered_cpt_onomy( $taxonomy ) ) {
return get_edit_term_link( $term_id, $taxonomy, $object_type );
}
$post_type = get_post_type_object( $taxonomy );
if ( ! current_user_can( $post_type->cap->edit_posts ) ) {
return;
}
$term = $this->get_term( $term_id, $taxonomy );
if ( ! $term ) {
return;
}
$args = array(
'post' => $term->term_id,
'action' => 'edit',
);
if ( $object_type ) {
$args['post_type'] = $object_type;
}
$location = add_query_arg( $args, admin_url( 'post.php' ) );
return apply_filters( 'get_edit_term_link', $location, $term_id, $taxonomy, $object_type );
}
/**
* This function mimics the WordPress function previous_post_link()
* because we cannot use that function properly.
*
* In the WordPress function, previous_post_link(), you are only allowed
* to use 'category' for your taxonomy but this function adds a new parameter that
* allows you to designate which CPT-onomy you would like to use.
*
* @since 1.0.2
* @uses $cpt_onomies_manager
* @param string $format Optional. Link anchor format.
* @param string $link Optional. Link permalink format.
* @param bool $in_same_cpt_onomy Optional. Whether link should be in a same CPT-onomy.
* @param array|string $excluded_term_ids Optional. Array or comma-separated list of excluded term IDs.
* @param string $cpt_onomy - name of the CPT-onomy for $in_same_cpt_onomy.
*/
function previous_post_link( $format = '« %link', $link = '%title', $in_same_cpt_onomy = false, $excluded_term_ids = '', $cpt_onomy = '' ) {
global $cpt_onomies_manager;
if ( empty( $format ) ) {
$format = '« %link';
}
if ( empty( $cpt_onomy ) || ! $cpt_onomies_manager->is_registered_cpt_onomy( $cpt_onomy ) ) {
previous_post_link( $format, $link, $in_same_cpt_onomy, $excluded_term_ids );
} else {
$this->adjacent_post_link( $format, $link, $in_same_cpt_onomy, $excluded_term_ids, true, $cpt_onomy );
}
}
/**
* This function mimics the WordPress function next_post_link()
* because we cannot use that function properly.
*
* In the WordPress function, next_post_link(), you are only allowed
* to use 'category' for your taxonomy but this function adds a new parameter that
* allows you to designate which CPT-onomy you would like to use.
*
* @since 1.0.2
* @uses $cpt_onomies_manager
* @param string $format Optional. Link anchor format.
* @param string $link Optional. Link permalink format.
* @param bool $in_same_cpt_onomy Optional. Whether link should be in a same CPT-onomy.
* @param array|string $excluded_term_ids Optional. Array or comma-separated list of excluded term IDs.
* @param string $cpt_onomy - name of the CPT-onomy for $in_same_cpt_onomy.
*/
function next_post_link( $format = '%link »', $link = '%title', $in_same_cpt_onomy = false, $excluded_term_ids = '', $cpt_onomy = '' ) {
global $cpt_onomies_manager;
// Make sure we have a format.
if ( empty( $format ) ) {
$format = '%link »';
}
// If it's empty or not a valid CPT-onomy, then run the default WordPress function.
if ( empty( $cpt_onomy ) || ! $cpt_onomies_manager->is_registered_cpt_onomy( $cpt_onomy ) ) {
next_post_link( $format, $link, $in_same_cpt_onomy, $excluded_term_ids );
} else {
$this->adjacent_post_link( $format, $link, $in_same_cpt_onomy, $excluded_term_ids, false, $cpt_onomy );
}
}
/**
* This function mimics the WordPress function adjacent_post_link()
* because we cannot use that function properly.
*
* In the WordPress function, adjacent_post_link(), you are only allowed
* to use 'category' for your taxonomy but this function adds a new parameter that
* allows you to designate which CPT-onomy you would like to use.
*
* @since 1.0.2
* @uses $cpt_onomies_manager
* @param string $format Link anchor format.
* @param string $link Link permalink format.
* @param bool $in_same_cpt_onomy Optional. Whether link should be in a same CPT-onomy.
* @param array|string $excluded_term_ids Optional. Array or comma-separated list of excluded term IDs.
* @param bool $previous Optional, default is true. Whether to display link to previous or next post.
* @param string $cpt_onomy - name of the CPT-onomy for $in_same_cpt_onomy.
*/
function adjacent_post_link( $format, $link, $in_same_cpt_onomy = false, $excluded_term_ids = '', $previous = true, $cpt_onomy = '' ) {
global $cpt_onomies_manager;
// If it's empty or not a valid CPT-onomy, then run the default WordPress function.
if ( empty( $cpt_onomy ) || ! $cpt_onomies_manager->is_registered_cpt_onomy( $cpt_onomy ) ) {
adjacent_post_link( $format, $link, $in_same_cpt_onomy, $excluded_term_ids, $previous );
} else {
if ( $previous && is_attachment() ) {