This repository has been 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 pathmanager.php
1709 lines (1407 loc) · 62.2 KB
/
manager.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_onomies_manager;
$cpt_onomies_manager = new CPT_ONOMIES_MANAGER();
/**
* Bring this function outside the class
* for easier use.
*
* Just invokes root class inside function.
*
* @since 1.3.2
*/
function register_cpt_onomy( $taxonomy, $object_type, $args = array() ) {
global $cpt_onomies_manager;
$cpt_onomies_manager->register_cpt_onomy( $taxonomy, $object_type, $args );
}
/**
* Holds the functions needed for managing the custom post types and taxonomies.
*
* @since 1.0
*/
class CPT_ONOMIES_MANAGER {
public $user_settings = array(
'network_custom_post_types' => array(),
'custom_post_types' => array(),
'other_custom_post_types' => array(),
);
/**
* Retrieves the user's plugin options and defines $user_settings.
* Registers the custom post types and taxonomies.
*
* Adds WordPress hooks (actions and filters).
*
* @since 1.0
*/
public function __construct() {
/*
* Get network user settings (only if multisite AND plugin is network activated)
* Had to take code from is_plugin_active_for_network() because the function is not loaded in time
*/
if ( is_multisite()
&& ( $plugins = get_site_option( 'active_sitewide_plugins' ) )
&& isset( $plugins[ CPT_ONOMIES_PLUGIN_FILE ] ) ) {
// Store network custom post types
if ( $network_custom_post_types = get_site_option( 'custom_post_type_onomies_custom_post_types' ) ) {
$this->user_settings['network_custom_post_types'] = $network_custom_post_types;
}
}
// Get site user settings.
$this->user_settings['custom_post_types'] = ( $custom_post_types = get_option( 'custom_post_type_onomies_custom_post_types' ) ) ? $custom_post_types : array();
$this->user_settings['other_custom_post_types'] = ( $other_custom_post_types = get_option( 'custom_post_type_onomies_other_custom_post_types' ) ) ? $other_custom_post_types : array();
// Register custom query vars.
add_filter( 'query_vars', array( $this, 'register_custom_query_vars' ) );
// Revert the query vars.
add_action( 'parse_request', array( $this, 'revert_query_vars' ), 100 );
// Manage user capabilities.
add_filter( 'user_has_cap', array( $this, 'user_has_term_capabilities' ), 10, 3 );
// Tweak the query.
add_filter( 'request', array( $this, 'change_query_vars' ) );
add_action( 'pre_get_posts', array( $this, 'add_cpt_onomy_term_queried_object' ), 1 );
add_filter( 'posts_clauses', array( $this, 'posts_clauses' ), 100, 2 );
// Clean up the query.
add_action( 'pre_get_posts', array( $this, 'clean_get_posts_terms_query' ), 100 );
// Register custom post types and taxonomies.
add_action( 'init', array( $this, 'register_custom_post_types_and_taxonomies' ), 100 );
}
/**
* Adds the custom query variable 'cpt_onomy_archive' to WordPress's
* WP_Query class which allows the plugin to create custom rewrites and queries.
*
* This function is applied to the filter 'query_vars'.
*
* @since 1.0
* @param array $vars - the query variables already created by WordPress
* @return array - the filtered query variables
*/
public function register_custom_query_vars( $vars ) {
array_push( $vars, 'cpt_onomy_archive' );
return $vars;
}
/**
* As of version 1.0.3, this function cleans up queries for front and back end tax queries.
*
* For front-end CPT-onomy archive pages, it removes 'name' so WordPress does not think this
* is a single post AND it defines which post types to show, i.e. which post types are attached
* to the CPT-onomy.
*
* This function is also run on the admin "edit posts" screen so you can filter posts by a CPT-onomy.
* It removes 'name' so WordPress does not think we are looking for a post with that 'name'.
*
* This function is applied to the filter 'request'.
*
* @since 1.0
* @uses $cpt_onomy, $pagenow, $post_type
* @param array $query - the query variables already created by WordPress
* @return array - the filtered query variables
*/
public function change_query_vars( $query ) {
global $cpt_onomy, $pagenow, $post_type;
if ( isset( $query['cpt_onomy_archive'] ) && $query['cpt_onomy_archive'] ) {
// Make sure CPT-onomy AND term exists, otherwise, why bother.
$change_query_vars = false;
foreach ( get_taxonomies( array(), 'objects' ) as $taxonomy => $tax ) {
if ( isset( $query[ $taxonomy ] ) && ! empty( $query[ $taxonomy ] ) && $this->is_registered_cpt_onomy( $taxonomy ) ) {
// Make sure the term exists.
$cpt_onomy_term_var = explode( '/', $query[ $taxonomy ] );
// Get parent.
$parent_term_id = $parent_term = 0;
if ( count( $cpt_onomy_term_var ) > 1 ) {
foreach ( $cpt_onomy_term_var as $index => $term_var ) {
if ( $index > 0 ) {
$parent_term = $cpt_onomy->get_term_by( 'slug', $cpt_onomy_term_var[ $index - 1 ], $taxonomy, OBJECT, 'raw', ( isset( $parent_term ) && isset( $parent_term->term_id ) ) ? $parent_term->term_id : 0 );
if ( isset( $parent_term->term_id ) ) {
$parent_term_id = $parent_term->term_id;
}
}
}
}
// If term id, then we need to get the term by id
$get_term_by = 'slug';
if ( is_numeric( $cpt_onomy_term_var[ count( $cpt_onomy_term_var ) - 1 ] ) ) {
$get_term_by = 'id';
}
// If the term doesn't exist, we're not going to change the query vars
if ( $cpt_onomy_term = $cpt_onomy->get_term_by( $get_term_by, $cpt_onomy_term_var[ count( $cpt_onomy_term_var ) - 1 ], $taxonomy, null, null, $parent_term_id ) ) {
// We're going to want to change the query vars
$change_query_vars = true;
// To avoid confusion with other children of the same name, change term to term id
if ( $cpt_onomy_term->parent ) {
$query[ $taxonomy ] = $cpt_onomy_term->term_id;
}
} else {
$change_query_vars = false;
break;
}
}
}
if ( $change_query_vars ) {
// The 'name' variable makes WordPress think this is a single post with the assigned 'name'
unset( $query['name'] );
}
} elseif ( is_admin() && 'edit.php' == $pagenow && isset( $post_type ) ) {
foreach ( get_taxonomies( array(), 'objects' ) as $taxonomy => $tax ) {
if ( isset( $_REQUEST[ $taxonomy ] ) && ! empty( $_REQUEST[ $taxonomy ] ) && $this->is_registered_cpt_onomy( $taxonomy ) ) {
if ( is_numeric( $_REQUEST[ $taxonomy ] ) ) {
$cpt_onomy_term = $cpt_onomy->get_term( (int) $_REQUEST[ $taxonomy ], $taxonomy );
} else {
$cpt_onomy_term = $cpt_onomy->get_term_by( 'slug', $_REQUEST[ $taxonomy ], $taxonomy );
}
// The 'name' variable makes WordPress think we are looking for a post with that 'name'
if ( ! empty( $cpt_onomy_term ) ) {
unset( $query['name'] );
}
}
}
}
return $query;
}
/**
* This function is used to revert any query variables that WordPress
* might have changed, if we deem necessary.
*
* For example, if our custom query/rewrite designates a 'post_type'
* but WordPress changes the 'post_type' query variable because we're
* also querying a CPT-onomy with the same name as as post type, we
* want to revert the 'post_type' to our designated value.
*
* This function also allows us to designate that we only want to
* query the post types our CPT-onomes are "attached" to.
*
* This function is applied to the action 'parse_request'.
*
* @since 1.2
* @param array $query - the query variables already created by WordPress
*/
public function revert_query_vars( $query ) {
if ( isset( $query->query_vars['cpt_onomy_archive'] ) && $query->query_vars['cpt_onomy_archive'] && ( isset( $query->matched_query ) && ( $matched_query = $query->matched_query ) ) ) {
/*
* We want the post type objects that all queried
* CPT-onomies have in common we also want to store
* which CPT-onomies are actually being queried.
*/
$cpt_onomy_objects = $queried_cpt_onomies = array();
$cpt_onomy_index = 0;
foreach ( get_taxonomies( array(), 'objects' ) as $taxonomy => $tax ) {
// Only want the CPT-onomies that are being queried
if ( array_key_exists( $taxonomy, $query->query_vars ) && $this->is_registered_cpt_onomy( $taxonomy ) ) {
$queried_cpt_onomies[ $taxonomy ] = $tax;
/*
* Get array started.
*
* Otherwise, intersect the arrays.
*/
if ( 0 == $cpt_onomy_index ) {
$cpt_onomy_objects = array_merge( $cpt_onomy_objects, $tax->object_type );
} else {
$cpt_onomy_objects = array_intersect( $cpt_onomy_objects, $tax->object_type );
}
$cpt_onomy_index++;
}
}
if ( $cpt_onomy_index > 0 ) {
/*
* If only one CPT-onomy is being queried, then we want to use its post types.
*
* Otherwise, we'll use the ones our multiple CPT-onomies have in common.
*/
if ( count( $queried_cpt_onomies ) == 1 && isset( $queried_cpt_onomies[0]->object_type ) ) {
$custom_post_type = $queried_cpt_onomies[0]->object_type;
} else {
$custom_post_type = ! empty( $cpt_onomy_objects ) ? $cpt_onomy_objects : null;
}
// If our custom query/rewrite defines a 'post_type', it overwrites the rest
foreach ( explode( '&', $matched_query ) as $parameter ) {
$parameter = explode( '=', $parameter );
if ( isset( $parameter[0] ) && strtolower( $parameter[0] ) == 'post_type'
&& isset( $parameter[1] ) && ( $set_post_type = $parameter[1] ) ) {
// We want to use the post type in our custom query/rewrite
$custom_post_type = $set_post_type;
break;
}
}
/*
* Convert to array for testing
* want to remove any post types who are not publicy queryable
*/
if ( ! is_array( $custom_post_type ) ) {
$custom_post_type = array( $custom_post_type );
}
foreach ( $custom_post_type as $post_type_index => $post_type ) {
$post_type_exists = post_type_exists( $post_type );
if ( ! $post_type_exists || ( $post_type_exists && get_post_type_object( $post_type )->exclude_from_search ) ) {
unset( $custom_post_type[ $post_type_index ] );
}
}
// If just one custom post type, then convert to string
if ( is_array( $custom_post_type ) && count( $custom_post_type ) == 1 ) {
$custom_post_type = array_shift( $custom_post_type );
}
/*
* Re-assign the 'post_type' query variable.
*
* Otherwise, there are no post types that
* are searchable and attached so kill the query.
*/
if ( isset( $custom_post_type ) && ! empty( $custom_post_type ) ) {
$query->query_vars['post_type'] = $custom_post_type;
} else {
$query->query_vars['cpt_onomies_kill_query'] = true;
}
}
}
}
/**
* This function is used for CPT-onomy archive pages (on the front-end of the site)
* in order to trick WordPress into thinking this is a legit taxonomy archive page.
*
* This function was created because we cannot hook into WordPress get_term_by(), without receiving an error.
* get_term_by() is responsible for passing the term's information to the query, which tells
* WordPress this is a taxonomy archive page, so this function creates the term information and
* passes it to the query.
*
* The CPT-onomy archive page query works without the queried object, but it is still required for
* other aspects of the page that use the queried object information, i.e. the page title.
*
* This function is applied to the action 'pre_get_posts'.
*
* @since 1.0
* @uses $cpt_onomy
* @param array $query - the query variables already created by WordPress
*/
public function add_cpt_onomy_term_queried_object( $query ) {
global $cpt_onomy;
// For CPT-onomy archive page on front-end.
if ( isset( $query->query['cpt_onomy_archive'] ) && ! empty( $query->query['cpt_onomy_archive'] ) ) {
// Make sure CPT-onomy AND term exists, otherwise, why bother
foreach ( get_taxonomies( array(), 'objects' ) as $taxonomy => $tax ) {
if ( isset( $query->query[ $taxonomy ] ) && ! empty( $query->query[ $taxonomy ] ) && $this->is_registered_cpt_onomy( $taxonomy ) ) {
// Make sure the term exists
if ( is_numeric( $query->query[ $taxonomy ] ) ) {
$cpt_onomy_term = $cpt_onomy->get_term( $query->query[ $taxonomy ], $taxonomy );
} else {
$cpt_onomy_term = $cpt_onomy->get_term_by( 'slug', $query->query[ $taxonomy ], $taxonomy );
}
if ( ! empty( $cpt_onomy_term ) ) {
// Make sure WordPress knows this is not a post type archive
$query->is_post_type_archive = false;
// Add queried object and queried object ID
$query->queried_object = $cpt_onomy_term;
$query->queried_object_id = $cpt_onomy_term->term_id;
break;
}
}
}
}
}
/**
* As of version 1.0.3, this function detects tax queries in the front and back end and
* adjusts the posts query accordingly.
*
* This function is invoked by the filter 'posts_clauses'.
*
* @since 1.0.3
* @uses $wpdb, $cpt_onomy
* @param array $clauses - the clauses variables already created by WordPress
* @param WP_Query $query - all of the query info
* @return array - the clauses info after it has been filtered
*/
public function posts_clauses( $clauses, $query ) {
global $wpdb, $cpt_onomy;
// Then kill the query and return the clauses.
if ( isset( $query->query['cpt_onomies_kill_query'] ) && $query->query['cpt_onomies_kill_query'] ) {
$clauses['where'] .= ' AND 0=1';
return $clauses;
}
// If ordering by a CPT-onomy.
if ( ( $taxonomies = $query->get( 'orderby' ) )
&& ( $post_type = $query->get( 'post_type' ) ) ) {
/*
* First, validate the CPT-onomy - could be multiple as array.
* Make sure everyone is an array.
*/
if ( ! is_array( $taxonomies ) ) {
$taxonomies = explode( ',', $taxonomies );
}
// Holds valid CPT-onomies.
$valid_cpt_onomies = array();
// Validate each taxonomy.
foreach ( $taxonomies as $taxonomy ) {
// If just one post type...
if ( ! is_array( $post_type ) ) {
// Check against post type.
if ( $this->is_registered_cpt_onomy( $taxonomy, $post_type ) ) {
$valid_cpt_onomies[] = $taxonomy;
continue;
}
} else {
/*
* Check against each post type.
* Let it through if it passes at least one test.
*/
foreach ( $post_type as $pt ) {
// Check against post type.
if ( $this->is_registered_cpt_onomy( $taxonomy, $pt ) ) {
$valid_cpt_onomies[] = $taxonomy;
break;
}
}
}
}
// If we have valid CPT-onomies, then go for it.
if ( $valid_cpt_onomies ) {
// Build our new orderby.
$new_orderby = null;
// Loop through each taxonomy.
foreach ( $valid_cpt_onomies as $tax_index => $taxonomy ) {
// Join each CPT-onomy's meta.
$clauses['join'] .= " LEFT OUTER JOIN {$wpdb->postmeta} cpt_onomy_order_{$tax_index}_pm ON cpt_onomy_order_{$tax_index}_pm.post_id = {$wpdb->posts}.ID
AND cpt_onomy_order_{$tax_index}_pm.meta_key = '" . CPT_ONOMIES_POSTMETA_KEY . "'
LEFT OUTER JOIN {$wpdb->posts} cpt_onomy_order_{$tax_index}_posts ON cpt_onomy_order_{$tax_index}_posts.ID = cpt_onomy_order_{$tax_index}_pm.meta_value
AND cpt_onomy_order_{$tax_index}_posts.post_type = '{$taxonomy}'";
// Order by each CPT-onomy.
if ( $new_orderby ) {
$new_orderby .= ', ';
}
$new_orderby .= "GROUP_CONCAT( cpt_onomy_order_{$tax_index}_posts.post_title ORDER BY cpt_onomy_order_{$tax_index}_posts.post_title ASC )" . ( ( isset( $query->query['order'] ) && strcasecmp( $query->query['order'], 'desc' ) == 0 ) ? ' DESC' : ' ASC' ) . ( ! empty( $clauses['orderby'] ) ? ', ' : ' ' ) . $clauses['orderby'];
}
// If defined, set the orderby.
if ( $new_orderby ) {
$clauses['orderby'] = $new_orderby;
}
// Group by the post's ID.
$clauses['groupby'] = "{$wpdb->posts}.ID";
}
}
// If running a tax query.
if ( isset( $query->tax_query ) ) {
$is_registered_cpt_onomy = false;
$taxonomies = array( 'join' => '', 'where' => array() );
$new_where = array();
$c = $t = 1;
// Get tax queries count for replacing 0 = 1 - do not include 'NOT IN' relationships.
$tax_queries_count = 0;
foreach ( $query->tax_query->queries as $this_query_key => $this_query ) {
// Get the taxonomy.
$taxonomy = isset( $this_query['taxonomy'] ) ? $this_query['taxonomy'] : null;
// Make sure the taxonomy exists.
if ( ! $taxonomy || ! taxonomy_exists( $taxonomy ) ) {
continue;
}
/*
* @TODO This used to skip for non-CPT-onomies but that caused a bug
*
* Now we let them through. Does this need to be fixed?
*/
/*if ( ! ( $is_registered_cpt_onomy = $this->is_registered_cpt_onomy( $taxonomy ) ) ) {
continue;
}*/
$is_registered_cpt_onomy = $this->is_registered_cpt_onomy( $taxonomy );
$this_query['terms'] = array_unique( (array) $this_query['terms'] );
if ( empty( $this_query['terms'] ) ) {
continue;
}
// If terms are ID, change field.
foreach ( $this_query['terms'] as $term ) {
if ( is_numeric( $term ) ) {
$this_query['field'] = 'id';
break;
}
}
/*
* First, CPT-onomies.
*
* Then, normal taxonomies.
*/
if ( $is_registered_cpt_onomy ) {
switch ( $this_query['field'] ) {
case 'slug':
case 'name':
$terms = "'" . implode( "','", array_map( 'sanitize_title_for_query', $this_query['terms'] ) ) . "'";
$terms = $wpdb->get_col( $wpdb->prepare( "SELECT ID FROM {$wpdb->posts} WHERE " . ( ( 'slug' == strtolower( $this_query['field'] ) ) ? 'post_name' : 'post_title' ) . " IN ({$terms}) AND post_type = %s", $this_query['taxonomy'] ) );
break;
default:
$terms = array_map( 'intval', $this_query['terms'] );
break;
}
} else {
switch ( $this_query['field'] ) {
case 'slug':
case 'name':
$terms = "'" . implode( "','", array_map( 'sanitize_title_for_query', $this_query['terms'] ) ) . "'";
$terms = $wpdb->get_col( $wpdb->prepare( "SELECT term_tax.term_taxonomy_id FROM {$wpdb->term_taxonomy} term_tax INNER JOIN {$wpdb->terms} terms USING (term_id) WHERE term_tax.taxonomy = %s AND terms.{$this_query['field']} IN ({$terms})", $taxonomy ) );
break;
default:
$terms = implode( ',', array_map( 'intval', $this_query['terms'] ) );
$terms = $wpdb->get_col( $wpdb->prepare( "SELECT term_taxonomy_id FROM {$wpdb->term_taxonomy} WHERE taxonomy = %s AND term_id IN ({$terms})", $taxonomy ) );
}
}
if ( 'AND' == $this_query['operator'] && count( $terms ) < count( $this_query['terms'] ) ) {
return;
}
$this_query['terms'] = $terms;
if ( is_taxonomy_hierarchical( $taxonomy ) && $this_query['include_children'] ) {
$children = array();
foreach ( $this_query['terms'] as $term ) {
/*
* First, hierarchical CPT-onomies.
*
* Then, normal hierarchical taxonomies.
*/
if ( $is_registered_cpt_onomy ) {
$children = array_merge( $children, $cpt_onomy->get_term_children( $term, $taxonomy ) );
} else {
$children = array_merge( $children, get_term_children( $term, $this_query['taxonomy'] ) );
}
$children[] = $term;
}
$this_query['terms'] = $children;
}
extract( $this_query );
$primary_table = $wpdb->posts;
$primary_id_column = 'ID';
sort( $terms );
if ( 'IN' == $operator ) {
if ( empty( $terms ) ) {
continue;
}
$terms = implode( ',', $terms );
/*
* First, CPT-onomies.
*
* Then, normal taxonomies.
*/
if ( $is_registered_cpt_onomy ) {
$alias = $c ? 'cpt_onomy_pm' . $c : $wpdb->postmeta;
$clauses['join'] .= " INNER JOIN $wpdb->postmeta";
$clauses['join'] .= $c ? " AS $alias" : '';
$clauses['join'] .= " ON ($wpdb->posts.ID = $alias.post_id AND $alias.meta_key = '" . CPT_ONOMIES_POSTMETA_KEY . "')";
$new_where[] = "{$alias}.meta_value {$operator} ({$terms})";
$c++;
} else {
$alias = $t ? 'cpt_onomy_tt' . $t : $wpdb->term_relationships;
$taxonomies['join'] .= " INNER JOIN $wpdb->term_relationships";
$taxonomies['join'] .= $t ? " AS $alias" : '';
$taxonomies['join'] .= " ON ($primary_table.$primary_id_column = $alias.object_id)";
$new_where[] = $taxonomies['where'][] = "{$alias}.term_taxonomy_id {$operator} ({$terms})";
$t++;
}
// Add to tax queries count for replacing 0 = 1 - do not include 'NOT IN' relationships.
$tax_queries_count++;
} elseif ( 'NOT IN' == $operator ) {
if ( empty( $terms ) ) {
continue;
}
$terms = implode( ',', $terms );
/*
* First, CPT-onomies.
*
* Then, normal taxonomies.
*/
if ( $is_registered_cpt_onomy ) {
$new_where[] = "{$wpdb->posts}.ID NOT IN (
SELECT post_id
FROM {$wpdb->postmeta}
WHERE meta_key = '" . CPT_ONOMIES_POSTMETA_KEY . "'
AND meta_value IN ({$terms})
)";
} else {
$new_where[] = $taxonomies['where'][] = "{$primary_table}.{$primary_id_column} NOT IN (
SELECT object_id
FROM {$wpdb->term_relationships}
WHERE term_taxonomy_id IN ({$terms})
)";
}
} elseif ( 'AND' == $operator ) {
if ( empty( $terms ) ) {
continue;
}
$num_terms = count( $terms );
$terms = implode( ',', $terms );
/*
* First, CPT-onomies.
*
* Then, normal taxonomies.
*/
if ( $is_registered_cpt_onomy ) {
$new_where[] = "(
SELECT COUNT(1)
FROM {$wpdb->postmeta}
WHERE meta_key = '" . CPT_ONOMIES_POSTMETA_KEY . "'
AND meta_value IN ({$terms})
AND post_id = {$wpdb->posts}.ID
) = $num_terms";
} else {
$new_where[] = $taxonomies['where'][] = "(
SELECT COUNT(1)
FROM {$wpdb->term_relationships}
WHERE term_taxonomy_id IN ({$terms})
AND object_id = {$primary_table}.{$primary_id_column}
) = {$num_terms}";
}
// Add to tax queries count for replacing 0 = 1 - do not include 'NOT IN' relationships
$tax_queries_count++;
}
}
// Only add taxonomies 'join' if it doesn't already exist
if ( $clauses['join'] && $taxonomies['join'] && strpos( $clauses['join'], $taxonomies['join'] ) === false ) {
$clauses['join'] .= $taxonomies['join'];
}
// Remove old taxonomies 'where' so we can add new 'where'.
if ( $taxonomies['where'] ) {
$tax_where = ' AND ( ';
foreach ( $taxonomies['where'] as $where_index => $add_where ) {
if ( $where_index > 0 ) {
$tax_where .= ' ' . $query->tax_query->relation . ' ';
}
$tax_where .= $add_where;
}
$tax_where .= ' )';
$clauses['where'] = str_replace( $tax_where, '', $clauses['where'] );
}
if ( ! empty( $new_where ) ) {
// Remove the post_name (WP adds this if the post type is hierarhical. I'm not sure why)
$clauses['where'] = preg_replace( '/wp\_posts\.post\_name\s=\s\'([^\']*)\'\sAND\s/i', '', $clauses['where'] );
/*
* Remove 0 = 1.
* Build the replace string.
*/
$preg_replace_str = null;
// We have to set it up for each tax query.
for ( $p = 0; $p < $tax_queries_count; $p++ ) {
// Add relation separator.
if ( $p > 0 ) {
$preg_replace_str .= '[\s]+' . $query->tax_query->relation;
}
$preg_replace_str .= '[\s]+0[\s]+\=[\s]+1';
}
// Wrap them all in an AND.
$preg_replace_str = 'AND[\s]+\(' . $preg_replace_str . '[\s]+\)';
// Replace the 0 = 1 in the 'where' clause.
$clauses['where'] = preg_replace( '/' . $preg_replace_str . '/i', '', $clauses['where'] );
// Find singular 0 = 1 to replace.
$clauses['where'] = preg_replace( '/AND[\s]+0[\s]+\=[\s]+1/i', '', $clauses['where'] );
// Make sure it removes the AND at the end as well.
$clauses['where'] = preg_replace( '/0[\s]+\=[\s]+1[\s]+AND/i', '', $clauses['where'] );
$clauses['where'] .= ' AND ( ';
foreach ( $new_where as $where_index => $add_where ) {
if ( $where_index > 0 ) {
$clauses['where'] .= ' ' . $query->tax_query->relation . ' ';
}
$clauses['where'] .= $add_where;
}
$clauses['where'] .= ' )';
}
}
return $clauses;
}
/**
* Because retrieving CPT-onomy terms involves get_posts(), we have to set some
* measures in place to remove any filters or queries that might affect retrieving
* the CPT-onomy terms.
*
* It detects the query variable 'get_cpt_onomy_terms' before editing the query.
*
* This function is applied to the action 'pre_get_posts'.
*
* @since 1.0.3
* @param array $query - the query variables already created by WordPress
*/
public function clean_get_posts_terms_query( $query ) {
if ( isset( $query->query_vars['get_cpt_onomy_terms'] ) ) {
// Remove all tax queries.
$query->set( 'taxonomy', null );
$query->set( 'term', null );
if ( isset( $query->tax_query ) ) {
$query->tax_query = null;
}
if ( isset( $query->query['taxonomy'] ) ) {
$query->query_vars['taxonomy'] = null;
}
if ( isset( $query->query['term'] ) ) {
$query->query['term'] = null;
}
// Remove all meta queries.
$query->set( 'meta_key', null );
$query->set( 'meta_value', null );
if ( isset( $query->meta_query ) ) {
$query->meta_query = null;
}
if ( isset( $query->query['meta_key'] ) ) {
$query->query_vars['meta_key'] = null;
}
if ( isset( $query->query['meta_value'] ) ) {
$query->query['meta_value'] = null;
}
}
}
/**
* This function hooks into WordPress current_user_can() whenever WordPress
* is checking that the user can 'assign_cpt_onomy_$taxonomy_terms', 'manage_cpt_onomy_$taxonomy_terms',
* 'edit_cpt_onomy_$taxonomy_terms' or 'delete_cpt_onomy_$taxonomy_terms'.
*
* If assign, it checks user settings to see if user role has permission to assign.
* If 'manage', 'edit' or 'delete, it tells WordPress NO!
*
* This function is applied to the filter 'user_has_cap'.
*
* @since 1.0
* @param array $allcaps - all of the user's preset capabilities
* @param array $caps - the capabilities we're testing
* @param array $args - additional arguments passed to the function
* @return array - the filtered $allcaps
*/
public function user_has_term_capabilities( $allcaps, $caps, $args ) {
// No one can manage, edit, or delete CPT-onomy terms
foreach ( $caps as $this_cap ) {
/*
* If user has capability manually assigned, then allow.
*
* Otherwise, check user settings.
* NO ONE is allowed to manage, edit or delete.
*/
if ( preg_match( '/assign\_cpt\_onomy\_([a-z\_]+)\_terms/i', $this_cap ) && ! isset( $allcaps[ $this_cap ] ) ) {
// Get taxonomy
$taxonomy = preg_replace( '/assign\_cpt\_onomy\_([a-z\_]+)\_terms/i', '\1', $this_cap );
// If registered CPT-onomy.
if ( taxonomy_exists( $taxonomy ) && $this->is_registered_cpt_onomy( $taxonomy ) ) {
// Get taxonomy info
$tax = get_taxonomy( $taxonomy );
// Default
$allow = false;
/*
* No capabilities are assigned so everyone has permission.
*
* Otherwise, the capability is restricted to specific roles.
*/
if ( ! isset( $tax->restrict_user_capabilities ) || empty( $tax->restrict_user_capabilities ) ) {
$allow = true;
} else {
/*
* Get user roles to see if user has capability to assign taxonomy.
* $args contains the user id.
*/
$user = new WP_User( $args[1] );
foreach ( $user->roles as $role ) {
// Test to see if role is selected.
if ( in_array( $role, $tax->restrict_user_capabilities ) ) {
$allow = true;
break;
}
}
}
// Assign the required capability
if ( $allow ) {
$allcaps[ $this_cap ] = 1;
} else {
unset( $allcaps[ $this_cap ] );
}
}
} elseif ( preg_match( '/(manage|edit|delete)\_cpt\_onomy\_([a-z\_]+)\_terms/i', $this_cap ) ) {
// Get taxonomy
$taxonomy = preg_replace( '/(manage|edit|delete)\_cpt\_onomy\_([a-z\_]+)\_terms/i', '\2', $this_cap );
// If registered CPT-onomy
if ( taxonomy_exists( $taxonomy ) && $this->is_registered_cpt_onomy( $taxonomy ) ) {
unset( $allcaps[ $this_cap ] );
}
}
}
return $allcaps;
}
/**
* Detects if a custom post type is overwriting a
* network-registered post type registered by this plugin.
*
* @since 1.3
* @uses $blog_id
* @param string $cpt_key - the key, or alias, for the custom post type you are checking
* @return boolean - whether this custom post type is overwriting a network-registered post type registered by this plugin
*/
public function overwrote_network_cpt( $cpt_key ) {
global $blog_id;
if ( isset( $this->user_settings['network_custom_post_types'] ) ) {
$network_cpts = $this->user_settings['network_custom_post_types'];
if ( isset( $network_cpts[ $cpt_key ] )
&& ( ( ! isset( $network_cpts[ $cpt_key ]['site_registration'] ) || ( isset( $network_cpts[ $cpt_key ]['site_registration'] ) && empty( $network_cpts[ $cpt_key ]['site_registration'] ) ) )
|| ( isset( $network_cpts[ $cpt_key ]['site_registration'] ) && in_array( $blog_id, $network_cpts[ $cpt_key ]['site_registration'] ) ) ) ) {
if ( $this->is_registered_cpt( $cpt_key ) && ! $this->is_registered_network_cpt( $cpt_key ) ) {
return true;
}
}
}
return false;
}
/**
* This functions checks to see if a custom post type is a network-registered
* custom post type registered by this plugin. When this plugin registers
* a network-registered custom post type, it adds the argument 'cpt_onomies_network_cpt'
* and 'created_by_cpt_onomies' for testing purposes.
*
* @since 1.3
* @param string $cpt_key - the key, or alias, for the custom post type you are checking
* @return boolean - whether this custom post type is a network-registered post type registered by this plugin
*/
public function is_registered_network_cpt( $cpt_key ) {
if ( ! empty( $cpt_key ) && post_type_exists( $cpt_key ) && ( $post_type = get_post_type_object( $cpt_key ) )
&& isset( $post_type->cpt_onomies_network_cpt ) && $post_type->cpt_onomies_network_cpt
&& isset( $post_type->created_by_cpt_onomies ) && $post_type->created_by_cpt_onomies ) {
return true;
}
return false;
}
/**
* This functions checks to see if a custom post type is a custom post type
* registered by this plugin. When this plugin registers a custom post type,
* it adds the argument 'created_by_cpt_onomies' for testing purposes.
*
* @since 1.0
* @param string $cpt_key - the key, or alias, for the custom post type you are checking
* @return boolean - whether this custom post type is a post type registered by this plugin
*/
public function is_registered_cpt( $cpt_key ) {
if ( ! empty( $cpt_key ) && post_type_exists( $cpt_key ) && ( $post_type = get_post_type_object( $cpt_key ) )
&& isset( $post_type->created_by_cpt_onomies ) && $post_type->created_by_cpt_onomies ) {
return true;
}
return false;
}
/**
* This functions checks to see if a taxonomy is a CPT-onomy
* registered by this plugin. When this plugin registers a CPT-onomy,
* it adds the argument 'cpt_onomy' for testing purposes.
*
* As of version 1.3.2, the function also allows you to test
* if the CPT-onomy is registered to a specific post type.
*
* @since 1.0
* @param string $tax - the key, or alias, for the taxonomy you are checking
* @param string $post_type - if set, checks to see is CPT-onomy AND is registered to set post type
* @return boolean - whether this taxonomy is a CPT-onomy registered by this plugin (and, if post type set, registered to a specific post type)
*/
public function is_registered_cpt_onomy( $taxonomy, $post_type = null ) {
if ( taxonomy_exists( $taxonomy ) ) {
// Get the taxonomy object
$tax = get_taxonomy( $taxonomy );
/*
* If post type is set, then checks to see if it's a
* CPT-onomy AND is registered to a specific post type.
* Otherwise, simply checks if it's a CPT-onomy.
*/
if ( ( empty( $post_type ) || ( ! empty( $post_type ) && post_type_exists( $post_type ) && in_array( $taxonomy, get_object_taxonomies( $post_type, 'names' ) ) ) )
&& isset( $tax->cpt_onomy ) && true == $tax->cpt_onomy ) {
return true;
}
}
return false;
}
/**
*
* Registers the user's custom post type as a CPT-onomy.
* The custom post type must already be registered in order
* to register the CPT-onomy.
*
* Because custom post types and taxonomies with the same name share
* the same $wp_rewrite permastruct, we cannot define the taxonomy's
* rewrite property (custom post types must win the rewrite battle).
* Instead, we add our own rewrite rule to display the CPT-onomy archive page.
*
* As of 1.1, users can define their own CPT-onomy archive page slug.
*
* @since 1.1
* @author Rachel Carden (@bamadesigner)
* @author Travis Smith (@wp_smith) - Thanks for your help Travis!!