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
/
admin.php
1394 lines (1145 loc) · 55.4 KB
/
admin.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_admin;
$cpt_onomies_admin = new CPT_ONOMIES_ADMIN();
/**
* Holds the functions needed for the admin.
*
* @since 1.0
*/
class CPT_ONOMIES_ADMIN {
/**
* Stores data received from the
* 'custom_post_type_onomies_assigning_cpt_onomy_terms_include_term_ids'
* and 'custom_post_type_onomies_assigning_cpt_onomy_terms_exclude_term_ids'
* filters that are invoked when printing the CPT-onomy meta boxes.
*/
public $assigning_terms_include_term_ids = array();
public $assigning_terms_exclude_term_ids = array();
/**
* Adds WordPress hooks (actions and filters).
*
* This function is only run in the admin.
*
* @since 1.0
*/
public function __construct() {
// We're only running these suckers in the admin.
if ( is_admin() ) {
// If the user visits edit-tags.php to manage the terms, we set them straight.
add_action( 'admin_init', array( $this, 'deny_edit_tags' ) );
// Register all of the admin scripts.
add_action( 'admin_enqueue_scripts', array( $this, 'admin_register_styles_and_scripts' ), 10 );
// Returns wp_object_terms().
add_action( 'wp_ajax_custom_post_type_onomy_get_wp_object_terms', array( $this, 'ajax_get_wp_object_terms' ) );
// Checks to see if term exists.
add_action( 'wp_ajax_custom_post_type_onomy_check_if_term_exists', array( $this, 'ajax_check_if_term_exists' ) );
// Add CPT-onomy "edit" meta boxes.
add_action( 'add_meta_boxes', array( $this, 'add_cpt_onomy_meta_boxes' ), 10, 2 );
// Takes care of autocomplete meta boxes.
add_action( 'wp_ajax_custom_post_type_onomy_meta_box_autocomplete_callback', array( $this, 'ajax_meta_box_autocomplete_callback' ) );
// Runs when any post is saved.
add_action( 'save_post', array( $this, 'save_post' ), 10, 2 );
// Runs when any post is deleted.
add_action( 'delete_post', array( $this, 'delete_post' ) );
// Bulk/quick edit.
add_action( 'bulk_edit_custom_box', array( $this, 'bulk_quick_edit_custom_box' ), 100, 2 );
add_action( 'quick_edit_custom_box', array( $this, 'bulk_quick_edit_custom_box' ), 100, 2 );
add_action( 'wp_ajax_custom_post_type_onomy_get_cpt_onomy_terms_include_term_ids', array( $this, 'ajax_get_cpt_onomy_terms_include_term_ids' ) );
add_action( 'wp_ajax_custom_post_type_onomy_get_cpt_onomy_terms_exclude_term_ids', array( $this, 'ajax_get_cpt_onomy_terms_exclude_term_ids' ) );
add_action( 'wp_ajax_custom_post_type_onomy_populate_bulk_quick_edit', array( $this, 'ajax_get_wp_object_terms' ) );
add_action( 'wp_ajax_custom_post_type_onomy_save_bulk_edit', array( $this, 'ajax_save_bulk_edit' ) );
add_action( 'wp_ajax_custom_post_type_onomy_quick_edit_populate_custom_columns', array( $this, 'ajax_quick_edit_populate_custom_columns' ) );
// Add column filters.
add_action( 'restrict_manage_posts', array( $this, 'restrict_manage_posts' ) );
// Add custom admin columns.
add_filter( 'manage_pages_columns', array( $this, 'add_cpt_onomy_admin_column' ), 100, 1 );
add_filter( 'manage_posts_columns', array( $this, 'add_cpt_onomy_admin_column' ), 100, 2 );
// Define sortable columns.
add_action( 'load-edit.php', array( $this, 'add_cpt_onomy_admin_sortable_columns_filter' ) );
}
}
/**
* The usual admin page for managing terms is edit-tags.php but we do not
* want users to access this page. $cpt_onomies_manager->user_has_term_capabilities()
* removes the ability access this page and throws up a 'Cheatin' uh?' message
* but this function replaces that message with some helpful text on where to go
* to edit the terms.
*
* @since 1.0
* @uses $cpt_onomies_manager, $pagenow
*/
public function deny_edit_tags() {
global $cpt_onomies_manager, $pagenow;
// Only on the edit tags page.
if ( 'edit-tags.php' == $pagenow ) {
// Make sure we have a taxonomy.
if ( ! empty( $_REQUEST['taxonomy'] )
&& $cpt_onomies_manager->is_registered_cpt_onomy( $_REQUEST['taxonomy'] ) ) {
// Get the taxonomy and post type info.
$taxonomy = $_REQUEST['taxonomy'];
$tax = get_taxonomy( $taxonomy );
$custom_post_type = get_post_type_object( $taxonomy );
/*
* If the user is capable of editing the post to begin with.
*
* Otherwise, don't get their hopes up.
*/
if ( current_user_can( $custom_post_type->cap->edit_posts ) ) {
wp_die( sprintf( __( 'Since "%1$s" is a registered %2$s, you manage it\'s "terms" by managing the posts created under the custom post type "%3$s". So go ahead... %4$smanage the posts%5$s.', 'cpt-onomies' ), $tax->labels->name, 'CPT-onomy', $tax->labels->name, '<a href="' . add_query_arg( array( 'post_type' => $taxonomy ), admin_url( 'edit.php' ) ) . '">', '</a>' ) );
} else {
wp_die( sprintf( __( 'Since "%1$s" is a registered %2$s, you manage it\'s "terms" by managing the posts created under the custom post type "%3$s". Unfortunately, you don\'t have permission to edit these posts. Sorry. If this is a mistake, contact your administrator.', 'cpt-onomies' ), $tax->labels->name, 'CPT-onomy', $tax->labels->name ) . ' <a href="' . admin_url() . '">' . __( 'Go to the dashboard', 'cpt-onomies' ) . '</a>' );
}
}
}
}
/**
* Bringing all of the styles and scripts into one function helps to
* keep everything more organized and allows for easier sharing.
*
* @since 1.1
* @uses $current_screen
* @param string $page - the current page
*/
public function admin_register_styles_and_scripts( $page ) {
global $current_screen;
// Several pages in the admin need this script.
wp_register_script( 'jquery-form-validation', plugins_url( 'assets/js/jquery.validate.min.js', __FILE__ ), array( 'jquery' ), null, true );
// Enqueue scripts depending on page.
switch ( $page ) {
case 'edit.php':
wp_enqueue_script( 'custom-post-type-onomies-admin-edit', plugins_url( 'assets/js/admin-edit.min.js', __FILE__ ), array( 'jquery', 'inline-edit-post' ), null, true );
break;
case 'post.php':
case 'post-new.php':
wp_enqueue_style( 'custom-post-type-onomies-admin-post', plugins_url( 'assets/css/admin-post.min.css', __FILE__ ), false, null );
wp_enqueue_script( 'custom-post-type-onomies-admin-post', plugins_url( 'assets/js/admin-post.min.js', __FILE__ ), array( 'jquery', 'post', 'jquery-ui-autocomplete' ), null, true );
// Our localized info.
$cpt_onomies_admin_post_data = array();
$cpt_onomies_admin_post_translation = array(
'term_does_not_exist' => sprintf( __( 'The term you are trying to add does not exist. %s terms, a.k.a posts, must already exist to be available for selection.', 'cpt-onomies' ), 'CPT-onomy' ),
'add_a_term' => __( 'Add a term', 'cpt-onomies' ),
'add_the_term' => __( 'Add the term', 'cpt-onomies' ),
'no_self_relationship' => __( 'Kind of silly to create a relationship between a post and itself, eh?', 'cpt-onomies' ),
'relationship_already_exists' => __( 'This relationship already exists.', 'cpt-onomies' ),
'close' => __( 'Close', 'cpt-onomies' ),
);
/*
* We need to know if the user has permission to edit specific
* taxonomies AND we'll get the label name while we're at it.
*/
foreach ( get_object_taxonomies( $current_screen->post_type, 'objects' ) as $taxonomy => $tax ) {
// Get the permission.
$cpt_onomies_admin_post_data['can_assign_terms'][ $taxonomy ] = current_user_can( $tax->cap->assign_terms );
// Get the label name.
$cpt_onomies_admin_post_translation['no_terms_selected'][ $taxonomy ] = sprintf( __( 'There are no %s selected.', 'cpt-onomies' ), strtolower( $tax->labels->name ) );
}
// Add our info to the scripts.
wp_localize_script( 'custom-post-type-onomies-admin-post', 'cpt_onomies_admin_post_data', $cpt_onomies_admin_post_data );
wp_localize_script( 'custom-post-type-onomies-admin-post', 'cpt_onomies_admin_post_L10n', $cpt_onomies_admin_post_translation );
break;
}
}
/**
* Allows ajax to invoke the get_cpt_onomy_terms_include_term_ids() function.
*
* Prints an array of term ids.
*
* @since 1.3
*/
public function ajax_get_cpt_onomy_terms_include_term_ids() {
// Get the taxonomy and post type info.
$taxonomy = ( isset( $_POST['custom_post_type_onomies_taxonomy'] ) && ! empty( $_POST['custom_post_type_onomies_taxonomy'] ) ) ? $_POST['custom_post_type_onomies_taxonomy'] : array();
$post_type = ( isset( $_POST['custom_post_type_onomies_post_type'] ) && ! empty( $_POST['custom_post_type_onomies_post_type'] ) ) ? $_POST['custom_post_type_onomies_post_type'] : null;
$post_id = ( isset( $_POST['custom_post_type_onomies_post_id'] ) && ! empty( $_POST['custom_post_type_onomies_post_id'] ) ) ? $_POST['custom_post_type_onomies_post_id'] : 0;
$include_term_ids = array();
// If we have a taxonomy...
if ( ! empty( $taxonomy ) ) {
// Get the include IDs.
$taxonomy_include_term_ids = $this->get_cpt_onomy_terms_include_term_ids( $taxonomy, $post_type, $post_id );
if ( ! empty( $taxonomy_include_term_ids ) ) {
$include_term_ids = array_merge( $include_term_ids, $taxonomy_include_term_ids );
}
}
// Print terms.
echo json_encode( $include_term_ids );
die();
}
/**
* The 'custom_post_type_onomies_assigning_cpt_onomy_terms_include_term_ids' filter
* allows you to designate that you only want to include/print specific terms, and therefore
* only want those specific terms to be able to be assigned, in the admin by returning their
* term IDs. This function invokes that filter when needed, cleans up the data, stores the
* data in a global class variable and returns the data.
*
* The data returned to the filter can be an array, space-separated or comma separated string.
* The filter passes three parameters: the $taxonomy, the $post_type and the $post_id.
*
* @since 1.3
* @param string $taxonomy - the name of the CPT-onomy
* @param string $post_type - the name of the post type the CPT-onomy is being assigned to
* @param int $post_id - the ID for the post the CPT-onomy is being assigned to
* @return array - the ids for the included cpt_onomy terms
* @filters 'custom_post_type_onomies_assigning_cpt_onomy_terms_include_term_ids' - $taxonomy, $post_type, $post_id
*/
public function get_cpt_onomy_terms_include_term_ids( $taxonomy = null, $post_type = null, $post_id = 0 ) {
$include_term_ids = apply_filters( 'custom_post_type_onomies_assigning_cpt_onomy_terms_include_term_ids', array(), $taxonomy, $post_type, $post_id );
// Make sure its an array.
if ( ! is_array( $include_term_ids ) ) {
$include_term_ids = str_replace( ' ', ',', str_replace( ', ', ',', $include_term_ids ) );
$include_term_ids = explode( ',', $include_term_ids );
}
// Make sure the 'include' does not include the current post ID.
if ( in_array( $post_id, $include_term_ids ) ) {
foreach ( $include_term_ids as $term_id_index => $term_id ) {
if ( $post_id == $term_id ) {
unset( $include_term_ids[ $term_id_index ] );
}
}
}
// Store and return the include term data.
return $this->assigning_terms_include_term_ids[ $taxonomy ][ $post_type ][ $post_id ] = array_unique( $include_term_ids );
}
/**
* Allows ajax to invoke the get_cpt_onomy_terms_exclude_term_ids() function.
*
* Prints an array of term ids.
*
* @since 1.2.1
*/
public function ajax_get_cpt_onomy_terms_exclude_term_ids() {
// Get taxonomy and post type info
$taxonomies = ( isset( $_POST['custom_post_type_onomies_taxonomies'] ) && ! empty( $_POST['custom_post_type_onomies_taxonomies'] ) ) ? $_POST['custom_post_type_onomies_taxonomies'] : array();
$post_type = ( isset( $_POST['custom_post_type_onomies_post_type'] ) && ! empty( $_POST['custom_post_type_onomies_post_type'] ) ) ? $_POST['custom_post_type_onomies_post_type'] : null;
$post_id = ( isset( $_POST['custom_post_type_onomies_post_id'] ) && ! empty( $_POST['custom_post_type_onomies_post_id'] ) ) ? $_POST['custom_post_type_onomies_post_id'] : 0;
$exclude_term_ids = array();
foreach ( $taxonomies as $taxonomy ) {
$taxonomy_exclude_term_ids = $this->get_cpt_onomy_terms_exclude_term_ids( $taxonomy, $post_type, $post_id );
if ( ! empty( $taxonomy_exclude_term_ids ) ) {
$exclude_term_ids = array_merge( $exclude_term_ids, $taxonomy_exclude_term_ids );
}
}
// Print terms
echo json_encode( $exclude_term_ids );
die();
}
/**
* The 'custom_post_type_onomies_assigning_cpt_onomy_terms_exclude_term_ids' filter
* allows you to exclude specific terms from being printed, and therefore assigned,
* in the admin by returning their term IDs. This function invokes that filter when
* needed, cleans up the data, stores the data in a global class variable and returns the data.
*
* The data returned to the filter can be an array, space-separated or comma separated string.
* The filter passes three parameters: the $taxonomy, the $post_type and the $post_id.
*
* @since 1.2.1
* @param string $taxonomy - the name of the CPT-onomy
* @param string $post_type - the name of the post type the CPT-onomy is being assigned to
* @param int $post_id - the ID for the post the CPT-onomy is being assigned to
* @return array - the ids for the excluded cpt_onomy terms
* @filters 'custom_post_type_onomies_assigning_cpt_onomy_terms_exclude_term_ids' - $taxonomy, $post_type, $post_id
*/
public function get_cpt_onomy_terms_exclude_term_ids( $taxonomy = null, $post_type = null, $post_id = 0 ) {
$exclude_term_ids = apply_filters( 'custom_post_type_onomies_assigning_cpt_onomy_terms_exclude_term_ids', array(), $taxonomy, $post_type, $post_id );
// Make sure its an array.
if ( ! is_array( $exclude_term_ids ) ) {
$exclude_term_ids = str_replace( ' ', ',', str_replace( ', ', ',', $exclude_term_ids ) );
$exclude_term_ids = explode( ',', $exclude_term_ids );
}
// Make sure the 'excludes' includes the current post ID.
if ( ! in_array( $post_id, $exclude_term_ids ) ) {
$exclude_term_ids[] = $post_id;
}
// Store and return the excluded term data.
return $this->assigning_terms_exclude_term_ids[ $taxonomy ][ $post_type ][ $post_id ] = array_unique( $exclude_term_ids );
}
/**
* Used in collaboration with the CPT-onomy autocomplete term selection,
* if the CPT-onomy is hierarchical, in order to display a term's parents
* in the autocomplete dropdown and in the selected terms' checklist.
*
* @since 1.1
* @param int $term_parent - the term's parent term id
* @return string the complete parent title
*/
public function build_term_parent_title_with_csv( $term_parent ) {
if ( $term_parent > 0 ) {
$post_parent_id = $term_parent;
$term_parent = '';
do {
$post_parent = get_post( $post_parent_id );
$post_parent_id = $post_parent->post_parent;
if ( $term_parent ) {
$term_parent .= ',';
}
$term_parent .= $post_parent->post_title;
} while ( $post_parent_id > 0 );
return $term_parent;
} else {
return null;
}
}
/**
* Returns an object's term info to an AJAX call.
*
* Allows you to designate if you want the parent titles
* instead of term id. This comes in handy with the autcomplete
* term selection for hierarchical CPT-onomies so we can show
* the parent title along with the term name.
*
* This function replaced populate_bulk_quick_edit().
*
* @since 1.1
*/
public function ajax_get_wp_object_terms() {
$post_ids = ( isset( $_POST['custom_post_type_onomies_post_ids'] ) && ! empty( $_POST['custom_post_type_onomies_post_ids'] ) ) ? $_POST['custom_post_type_onomies_post_ids'] : array();
$taxonomies = ( isset( $_POST['custom_post_type_onomies_taxonomies'] ) && ! empty( $_POST['custom_post_type_onomies_taxonomies'] ) ) ? $_POST['custom_post_type_onomies_taxonomies'] : array();
$get_parent_title = ( isset( $_POST['custom_post_type_onomies_get_parent_title'] ) && ! empty( $_POST['custom_post_type_onomies_get_parent_title'] ) ) ? true : false;
$terms_fields = ( isset( $_POST['custom_post_type_onomies_wp_get_object_terms_fields'] ) && ! empty( $_POST['custom_post_type_onomies_wp_get_object_terms_fields'] ) && in_array( $_POST['custom_post_type_onomies_wp_get_object_terms_fields'], array( 'ids' ) ) ) ? $_POST['custom_post_type_onomies_wp_get_object_terms_fields'] : null;
if ( ! empty( $post_ids ) && ! empty( $taxonomies ) ) {
if ( ! is_array( $post_ids ) ) {
$post_ids = array( $post_ids );
}
if ( ! is_array( $taxonomies ) ) {
$taxonomies = array( $taxonomies );
}
// Set any arguments.
$args = array();
// If we want specific fields.
if ( $terms_fields ) {
$args = array( 'fields' => $terms_fields );
}
// Get terms.
$terms = wp_get_object_terms( $post_ids, $taxonomies, $args );
// Get parent title, if desired AND if post type is hierarchical.
if ( $get_parent_title ) {
foreach ( $terms as $term_index => $term ) {
$terms[ $term_index ]->parent = ( is_post_type_hierarchical( $term->taxonomy ) ) ? $this->build_term_parent_title_with_csv( $term->parent ) : '';
}
}
// Print terms.
echo json_encode( $terms );
}
die();
}
/**
* Allows us to check if a term exists via AJAX.
* Returns an object of term info.
*
* Also allows you to designate that you want the parent's
* name instead of the parent's term id.
*
* @since 1.1
* @uses $cpt_onomy
*/
public function ajax_check_if_term_exists() {
global $cpt_onomy;
$term = ( isset( $_POST['custom_post_type_onomies_term'] ) && ! empty( $_POST['custom_post_type_onomies_term'] ) ) ? $_POST['custom_post_type_onomies_term'] : '';
$term_id = ( isset( $_POST['custom_post_type_onomies_term_id'] ) && ! empty( $_POST['custom_post_type_onomies_term_id'] ) && (int) $_POST['custom_post_type_onomies_term_id'] > 0 ) ? (int) $_POST['custom_post_type_onomies_term_id'] : 0;
$taxonomy = ( isset( $_POST['custom_post_type_onomies_taxonomy'] ) && ! empty( $_POST['custom_post_type_onomies_taxonomy'] ) ) ? $_POST['custom_post_type_onomies_taxonomy'] : '';
$get_parent_title = ( isset( $_POST['custom_post_type_onomies_get_parent_title'] ) && ! empty( $_POST['custom_post_type_onomies_get_parent_title'] ) ) ? true : false;
if ( ( $term || $term_id > 0 ) && $taxonomy ) {
$term_exists = false;
if ( $term_id > 0 ) {
$term_exists = $cpt_onomy->term_exists( $term_id, $taxonomy );
}
if ( ! $term_exists && $term ) {
$term_exists = $cpt_onomy->term_exists( $term, $taxonomy );
}
if ( ! $term_exists ) {
echo json_encode( array() );
} elseif ( is_numeric( $term_exists ) ) {
echo json_encode( (object) array( 'term_id' => $term_exists ) );
} elseif ( is_object( $term_exists ) || is_array( $term_exists ) ) {
// Get parent title, if desired.
if ( $get_parent_title ) {
$term_exists->parent = $this->build_term_parent_title_with_csv( $term_exists->parent );
}
echo json_encode( $term_exists );
} else {
echo json_encode( array() );
}
} else {
echo json_encode( array() );
}
die();
}
/**
* The jQuery field autocomplete callback
*
* This function returns results for the CPT-onomy autocomplete term selection.
*
* You can designate that you only want to include specific terms in the results by returning
* their term IDs using the 'custom_post_type_onomies_assigning_cpt_onomy_terms_include_term_ids'
* filter which passes three parameters: the $taxonomy, the $post_type and the $post_id.
* The "include" filter overwrites the "exclude" filter.
*
* You can disable specific terms from being listed in the results by returning their
* term IDs using the 'custom_post_type_onomies_assigning_cpt_onomy_terms_exclude_term_ids'
* filter which passes three parameters: the $taxonomy, the $post_type and the $post_id.
* While the "include" filter overwrites the "exclude" filter, if exclude terms are in the
* include terms, they will be removed.
*
* @since 1.1
* @uses $wpdb
*/
public function ajax_meta_box_autocomplete_callback() {
global $wpdb;
// Get the taxonomy and post type info.
$taxonomy = ( isset( $_POST['custom_post_type_onomies_taxonomy'] ) && ! empty( $_POST['custom_post_type_onomies_taxonomy'] ) ) ? $_POST['custom_post_type_onomies_taxonomy'] : null;
$term = ( isset( $_POST['custom_post_type_onomies_term'] ) && ! empty( $_POST['custom_post_type_onomies_term'] ) ) ? $_POST['custom_post_type_onomies_term'] : null;
$post_type = ( isset( $_POST['custom_post_type_onomies_post_type'] ) && ! empty( $_POST['custom_post_type_onomies_post_type'] ) ) ? $_POST['custom_post_type_onomies_post_type'] : 0;
$post_id = ( isset( $_POST['custom_post_type_onomies_post_id'] ) && ! empty( $_POST['custom_post_type_onomies_post_id'] ) ) ? $_POST['custom_post_type_onomies_post_id'] : 0;
if ( $taxonomy && $term ) {
// Get available terms.
$available_terms = $wpdb->get_results( $wpdb->prepare( "SELECT ID, post_title AS label, post_parent AS parent FROM {$wpdb->posts} WHERE post_type = %s AND post_status = 'publish' ORDER BY post_title ASC", $taxonomy ) );
if ( $available_terms ) {
/*
* Allows you to use the
* 'custom_post_type_onomies_assigning_cpt_onomy_terms_include_term_ids'
* filter to designate that you only want specific terms to be
* printed and therefore assigned. The term ids are stored
* in an array that is used to customize each printed format.
* 'Include' overwrites 'exclude'.
*/
$include_term_ids = $this->get_cpt_onomy_terms_include_term_ids( $taxonomy, $post_type, $post_id );
/*
* Allows you to use the
* 'custom_post_type_onomies_assigning_cpt_onomy_terms_exclude_term_ids'
* filter to exclude specific terms from being printed
* and therefore assigned. The term ids are stored in an
* array that is used to customize each printed format.
* While 'include' overwrites 'exclude', if exclude terms
* are in the include array, they will be removed.
*/
$exclude_term_ids = $this->get_cpt_onomy_terms_exclude_term_ids( $taxonomy, $post_type, $post_id );
$results = array();
foreach ( $available_terms as $this_term ) {
// Whether or not we want the element displayed.
$add_term_to_results = true;
// Test against 'include' and 'exclude'.
if ( $include_term_ids && ! in_array( $this_term->ID, $include_term_ids ) ) {
$add_term_to_results = false;
}
if ( $exclude_term_ids && in_array( $this_term->ID, $exclude_term_ids ) ) {
$add_term_to_results = false;
}
// We don't want to display children of terms we filtered out.
if ( $this_term->parent ) {
foreach ( get_post_ancestors( $this_term->ID ) as $ancestor ) {
if ( in_array( $ancestor, $exclude_term_ids ) ) {
$add_term_to_results = false;
break;
}
}
}
if ( $add_term_to_results ) {
// Go ahead and apply the filter before it's "searched".
$this_term->label = apply_filters( 'the_title', $this_term->label, $this_term->ID );
/*
* We don't want to display the current post.
* If a match was found, add it to the suggestions.
*/
if ( stripos( $this_term->label, $term ) !== false ) {
$results[] = array(
'value' => $this_term->ID,
'label' => $this_term->label,
'parent' => ( is_post_type_hierarchical( $taxonomy ) ) ? $this->build_term_parent_title_with_csv( $this_term->parent ) : '',
);
}
}
}
// Print terms.
echo json_encode( $results );
}
}
die();
}
/**
* If a CPT-onomy is attached to a post type, the plugin adds a meta box to
* the post edit screen so the user can assign/manage the taxonomy's terms.
*
* You can remove the box by returning false to the
* 'custom_post_type_onomies_add_cpt_onomy_admin_meta_box' filter, which passes
* two parameters: the $taxonomy and the $post_type.
*
* This function is invoked by the action 'add_meta_boxes'.
*
* @since 1.0
* @uses $cpt_onomies_manager
* @param string $post_type - the current post's post type
* @param object $post - the current post's information
* @filters 'custom_post_type_onomies_add_cpt_onomy_admin_meta_box' - $taxonomy, $post_type
*/
public function add_cpt_onomy_meta_boxes( $post_type, $post ) {
global $cpt_onomies_manager;
// Loop through all the taxonomies tied to this post type.
foreach ( get_object_taxonomies( $post_type, 'objects' ) as $taxonomy => $tax ) {
// Make sure its a registered CPT-onomy.
if ( $cpt_onomies_manager->is_registered_cpt_onomy( $taxonomy ) ) {
/*
* This filter allows you to remove the meta box by returning false.
*
* If 'show_ui' is false, do not add meta box.
*/
if ( apply_filters( 'custom_post_type_onomies_add_cpt_onomy_admin_meta_box', ( post_type_exists( $taxonomy ) ? get_post_type_object( $taxonomy )->show_ui : true ), $taxonomy, $post_type ) ) {
// What's the meta box title? - default is taxonomy label.
$meta_box_title = isset( $tax->meta_box_title ) && ! empty( $tax->meta_box_title ) ? $tax->meta_box_title : $tax->label;
// Add the meta box.
add_meta_box( 'custom-post-type-onomies-' . $taxonomy, apply_filters( 'custom_post_type_onomies_meta_box_title', $meta_box_title, $taxonomy, $post_type ), array( $this, 'print_cpt_onomy_meta_box' ), $post_type, 'side', 'core', array( 'taxonomy' => $taxonomy ) );
}
}
}
}
/**
* This function is invoked when a CPT-onomy meta box is attached to a post type's edit post screen.
* This 'callback' function prints the html for the meta box.
*
* The meta box consists of a checklist that allows the user to assign/manage the taxonomy's terms.
* This function mimics a meta box for an ordinary custom taxonomy.
*
* Version 1.1 brought support for 'autocomplete' and 'dropdown' selection format,
* on top of the already existent 'checklist'.
*
* CPT-onomies follows default WordPress behavior, providing a checklist for hierarchical
* CPT-onomies and the autocomplete box for non-hierarchical CPT-onomies. You can change the
* format by hooking into the 'custom_post_type_onomies_meta_box_format' filter, which passes
* two parameters: the $taxonomy and the $post_type.
*
* You can designate that you only want specific terms listed in the results by returning their
* term IDs using the 'custom_post_type_onomies_assigning_cpt_onomy_terms_include_term_ids'
* filter which passes three parameters: the $taxonomy, the $post_type and the $post_id.
*
* You can disable specific terms from being listed in the results by returning their
* term IDs using the 'custom_post_type_onomies_assigning_cpt_onomy_terms_exclude_term_ids'
* filter which passes three parameters: the $taxonomy, the $post_type and the $post_id.
*
* This code mimics the WordPress function post_categories_meta_box().
*
* This function is invoked by the action 'add_meta_boxes'.
*
* @since 1.0
* @param object $post - the current post's information
* @param array $box - information about the metabox
* @filters 'custom_post_type_onomies_meta_box_format' - $taxonomy, $post_type
*/
public function print_cpt_onomy_meta_box( $post, $metabox ) {
// Add nonce.
wp_nonce_field( 'assigning_custom_post_type_onomies_taxonomy_relationships', 'custom_post_type_onomies_nonce' );
// Define variables.
$post_type = ( isset( $post->post_type ) && ! empty( $post->post_type ) && post_type_exists( $post->post_type ) ) ? $post->post_type : null;
$taxonomy = ( isset( $metabox['args']['taxonomy'] ) && ! empty( $metabox['args']['taxonomy'] ) && taxonomy_exists( $metabox['args']['taxonomy'] ) ) ? $metabox['args']['taxonomy'] : null;
if ( $post_type && $taxonomy ) {
// Get taxonomy info
$tax = get_taxonomy( $taxonomy );
// If 'meta_box_format' is not defined, use default WordPress setting
if ( ! ( $format = ( isset( $tax->meta_box_format ) && ! empty( $tax->meta_box_format ) ) ? $tax->meta_box_format : null ) ) {
$format = is_post_type_hierarchical( $taxonomy ) ? 'checklist' : 'autocomplete';
}
// Allow the user to change the format - 'autocomplete', 'dropdown', 'checklist' - default
$format = apply_filters( 'custom_post_type_onomies_meta_box_format', $format, $taxonomy, $post_type );
// Does the user have permission to assign terms?
$disabled = ! current_user_can( $tax->cap->assign_terms ) ? ' disabled="disabled"' : '';
/*
* Allows you to use the
* 'custom_post_type_onomies_assigning_cpt_onomy_terms_include_term_ids'
* filter to designate that you only want specific terms
* to be printed and therefore assigned. The term ids are
* stored in an array that is used to customize each printed
* format. 'Include' overwrites 'exclude'.
*/
$include_term_ids = $this->get_cpt_onomy_terms_include_term_ids( $taxonomy, $post_type, $post->ID );
/*
* Allows you to use the
* 'custom_post_type_onomies_assigning_cpt_onomy_terms_exclude_term_ids'
* filter to exclude specific terms from being printed
* and therefore assigned. The term ids are stored in an
* array that is used to customize each printed format.
* While 'include' overwrites 'exclude', if exclude terms
* are in the include array, they will be removed.
*/
$exclude_term_ids = $this->get_cpt_onomy_terms_exclude_term_ids( $taxonomy, $post_type, $post->ID );
// Add field for testing "editability" when we save the information.
?>
<input type="hidden" name="assign_cpt_onomies_<?php echo $taxonomy; ?>_rel" value="1" />
<?php
switch ( $format ) {
case 'autocomplete':
?>
<div id="taxonomy-<?php echo $taxonomy; ?>" class="cpt_onomies_tags_div">
<div class="jaxtag">
<div class="nojs-tags hide-if-js">
<p><?php echo $tax->labels->add_or_remove_items; ?></p>
<textarea name="<?php echo CPT_ONOMIES_POSTMETA_KEY; ?>[<?php echo $taxonomy; ?>]" rows="3" cols="20" class="the-tags" id="tax-input-<?php echo $taxonomy; ?>"<?php echo $disabled; ?>><?php echo get_terms_to_edit( $post->ID, $taxonomy ); ?></textarea>
</div>
<?php
if ( current_user_can( $tax->cap->assign_terms ) ) :
?>
<div class="ajaxtag hide-if-no-js">
<label class="screen-reader-text" for="new-tag-<?php echo $taxonomy; ?>"><?php echo $metabox['title']; ?></label>
<div class="taghint"><?php echo $tax->labels->add_new_item; ?></div>
<p>
<input type="text" id="new-tag-<?php echo $taxonomy; ?>" name="cpt_onomies_new_tag[<?php echo $taxonomy; ?>]" class="cpt_onomies_new_tag form-input-tip" size="16" autocomplete="off" value="" />
<input type="button" class="button cpt_onomies_tag_add" value="<?php esc_attr_e( 'Add', 'cpt-onomies' ); ?>" tabindex="3" />
</p>
</div>
<?php
endif;
?>
</div>
<div class="cpt_onomies_tag_checklist<?php if ( ! current_user_can( $tax->cap->assign_terms ) ) { echo ' alone'; } ?>"></div>
</div>
<?php
if ( current_user_can( $tax->cap->assign_terms ) ) :
?>
<p class="hide-if-no-js"><a href="#titlediv" class="cpt_onomies_tag_cloud" id="link-<?php echo $taxonomy; ?>"><?php echo $tax->labels->choose_from_most_used; ?></a></p>
<?php
endif;
break;
case 'dropdown':
// Get ALL info and then extract IDs because of ID conflict with regular taxonomies.
$selected_terms = wp_get_object_terms( $post->ID, $taxonomy );
// We only need the first term for a dropdown.
$selected_term = $selected_terms ? array_shift( $selected_terms )->term_id : 0;
/*
* Because the dropdown function only has 'exclude', if 'include' is set,
* we have to get all of the terms and exclude everything but what's in 'include'
*/
$dropdown_exclude_term_ids = array();
if ( $include_term_ids ) {
// Get all terms for this taxonomy that are not in 'include'.
foreach ( get_terms( $taxonomy, array( 'hide_empty' => false, 'fields' => 'ids' ) ) as $term_id ) {
if ( ! in_array( $term_id, $include_term_ids ) ) {
$dropdown_exclude_term_ids[] = $term_id;
}
}
}
// Make sure 'exclude' term ids are included.
if ( $exclude_term_ids ) {
$dropdown_exclude_term_ids = array_unique( array_merge( $dropdown_exclude_term_ids, $exclude_term_ids ) );
}
$dropdown = wp_dropdown_categories( array(
'show_option_none' => sprintf( __( 'No %s are selected', 'cpt-onomies' ), $tax->labels->all_items ),
'orderby' => 'name',
'order' => 'ASC',
'show_count' => false,
'hide_empty' => false,
'exclude' => $dropdown_exclude_term_ids,
'echo' => false,
'selected' => $selected_term,
'hierarchical' => is_post_type_hierarchical( $taxonomy ),
'name' => CPT_ONOMIES_POSTMETA_KEY . '[' . $taxonomy . '][]',
'id' => 'taxonomy-' . $taxonomy,
'class' => 'category cpt_onomies',
'taxonomy' => $taxonomy,
'hide_if_empty' => false,
));
/*
* Need to add disabled to select element
* as a backup, this attribute is also checked in admin-post.js
*/
if ( $disabled ) {
$dropdown = preg_replace( '/^\<select/', '<select' . $disabled, $dropdown );
}
// Print dropdown.
echo $dropdown;
break;
case 'checklist':
default:
?>
<div id="taxonomy-<?php echo $taxonomy; ?>" class="categorydiv cpt_onomies">
<ul id="<?php echo $taxonomy; ?>-tabs" class="category-tabs">
<li class="tabs"><a href="#<?php echo $taxonomy; ?>-all" tabindex="3"><?php echo $tax->labels->all_items; ?></a></li>
<li class="hide-if-no-js"><a href="#<?php echo $taxonomy; ?>-pop" tabindex="3"><?php _e( 'Most Used', 'cpt-onomies' ); ?></a></li>
</ul>
<div id="<?php echo $taxonomy; ?>-pop" class="tabs-panel" style="display:none;">
<ul id="<?php echo $taxonomy; ?>checklist-pop" class="categorychecklist form-no-clear" >
<?php $popular_ids = wp_popular_terms_checklist( $taxonomy ); ?>
</ul>
</div>
<div id="<?php echo $taxonomy; ?>-all" class="tabs-panel">
<ul id="<?php echo $taxonomy; ?>checklist" class="list:<?php echo $taxonomy?> categorychecklist form-no-clear">
<?php wp_terms_checklist( $post->ID, array( 'taxonomy' => $taxonomy, 'popular_cats' => $popular_ids, 'walker' => new CPTonomy_Walker_Terms_Checklist() ) ); ?>
</ul>
</div>
</div>
<?php
break;
}
}
}
/**
* This function is run when any post is saved by the action 'save_post'.
*
* @since 1.0
* @uses $cpt_onomies_manager, $cpt_onomy
* @param int $post_id - the ID of the current post
* @param object $post - the current post's information
* @return nada
*/
public function save_post( $post_id, $post ) {
global $cpt_onomies_manager, $cpt_onomy;
// Pointless if $_POST is empty (this happens on bulk edit).
if ( empty( $_POST ) ) {
return;
}
// Verify nonce.
if ( ! ( isset( $_POST['is_bulk_quick_edit'] ) || ( isset( $_POST['_wpnonce'] ) && wp_verify_nonce( $_POST['_wpnonce'], 'update-' . $post->post_type . '_' . $post_id ) ) || ( isset( $_POST['custom_post_type_onomies_nonce'] ) && wp_verify_nonce( $_POST['custom_post_type_onomies_nonce'], 'assigning_custom_post_type_onomies_taxonomy_relationships' ) ) ) ) {
return;
}
// Check autosave.
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) {
return;
}
// Don't save for revisions.
if ( isset( $post->post_type ) && 'revision' == $post->post_type ) {
return;
}
// Check CPT-onomies.
foreach ( get_object_taxonomies( $post->post_type, 'objects' ) as $taxonomy => $tax ) {
/*
* Make sure cpt-onomy was visible, otherwise we might be
* deleting relationships for taxonomies that weren't even "editable".
*/
$assign_key = "assign_cpt_onomies_{$taxonomy}_rel";
if ( ! isset( $_POST[ $assign_key ] ) ) {
continue;
}
// Make sure its a valid CPT-onomy.
if ( ! $cpt_onomies_manager->is_registered_cpt_onomy( $taxonomy ) ) {
continue;
}
// Check permissions.
if ( ! current_user_can( $tax->cap->assign_terms ) ) {
continue;
}
/*
* Set object terms.
*
* Otherwise, delete taxonomy relationships.
*/
if ( isset( $_POST[ CPT_ONOMIES_POSTMETA_KEY ][ $taxonomy ] ) ) {
// Need to make sure its an array.
if ( ! is_array( $_POST[ CPT_ONOMIES_POSTMETA_KEY ][ $taxonomy ] ) ) {
$_POST[ CPT_ONOMIES_POSTMETA_KEY ][ $taxonomy ] = explode( ',', $_POST[ CPT_ONOMIES_POSTMETA_KEY ][ $taxonomy ] );
}
// Set the terms.
$cpt_onomy->wp_set_object_terms( $post_id, $_POST[ CPT_ONOMIES_POSTMETA_KEY ][ $taxonomy ], $taxonomy );
} else {
$cpt_onomy->wp_delete_object_term_relationships( $post_id, $taxonomy );
}
}
}
/**
* This function is run when any post is deleted.
*
* This function is invoked by the action 'delete_post'.
*
* @since 1.0.1
* @uses $wpdb
* @param int $post_id - the ID of the post being deleted
*/
public function delete_post( $post_id ) {
global $wpdb;
// Delete all relationships tied to this term.
$wpdb->query( $wpdb->prepare( "DELETE FROM {$wpdb->postmeta} WHERE meta_key = %s AND meta_value = %d", CPT_ONOMIES_POSTMETA_KEY, $post_id ) );
}
/**
* Adds CPT-onomies to the "Bulk Edit" and "Quick Edit" screens. The function is called
* for each custom column who's name tells us which CPT-onomy checklist to print.
*
* This function is invoked by the actions 'bulk_edit_custom_box' and 'quick_edit_custom_box'.
*
* @since 1.0.3
* @uses $cpt_onomies_manager
* @param string $column_name - the name of the column (which tells us which taxonomy to show)
* @param string $post_type - the current post's post type
*/
public function bulk_quick_edit_custom_box( $column_name, $post_type ) {
global $cpt_onomies_manager;
// Get the taxonomy name from the column name.
$taxonomy = strtolower( str_replace( 'taxonomy-', '', $column_name ) );
// If the taxonomy is a CPT-onomy.
if ( $taxonomy && taxonomy_exists( $taxonomy )
&& $cpt_onomies_manager->is_registered_cpt_onomy( $taxonomy ) ) :
// Get the taxonomy object.
$tax = get_taxonomy( $taxonomy );
?>
<fieldset class="inline-edit-col-center inline-edit-<?php echo $taxonomy; ?>">
<div class="inline-edit-col">
<span class="title inline-edit-<?php echo $taxonomy; ?>-label"><?php echo esc_html( $tax->labels->name ) ?>
<span class="catshow">[<?php _e( 'more', 'cpt-onomies' ); ?>]</span>
<span class="cathide" style="display:none;">[<?php _e( 'less', 'cpt-onomies' ); ?>]</span>
</span>
<ul class="cat-checklist cpt-onomy-checklist cpt-onomy-<?php echo esc_attr( $taxonomy ); ?>">
<?php
// Print the checklist.
wp_terms_checklist( null, array(
'taxonomy' => $taxonomy,
'walker' => new CPTonomy_Walker_Terms_Checklist(),
));
?>
</ul>
<input type="hidden" name="is_bulk_quick_edit" value="true" />
<input type="hidden" name="<?php echo "assign_cpt_onomies_{$taxonomy}_rel"; ?>" value="true" />
</div>
</fieldset>
<?php
endif;
}
/**
* This ajax function is run on the "Edit Posts" screen when a "Bulk Edit" is saved.
* It retrieves the "checked" CPT-onomy information and saves the object/term relationships.
*
* Bulk edits do not delete relationships if they are not checked. It only "appends" term relationships,
* i.e. assigning checked relationships if they do not already exist.
*
* @since 1.0.3
* @uses $cpt_onomy
*/
public function ajax_save_bulk_edit() {
global $cpt_onomy;
// Get taxonomy and post type info.
$post_ids = ( isset( $_POST['custom_post_type_onomies_post_ids'] ) && ! empty( $_POST['custom_post_type_onomies_post_ids'] ) ) ? $_POST['custom_post_type_onomies_post_ids'] : array();
$taxonomy = ( isset( $_POST['custom_post_type_onomies_taxonomy'] ) && ! empty( $_POST['custom_post_type_onomies_taxonomy'] ) ) ? $_POST['custom_post_type_onomies_taxonomy'] : null;
$checked_ids = ( isset( $_POST['custom_post_type_onomies_checked_ids'] ) && ! empty( $_POST['custom_post_type_onomies_checked_ids'] ) ) ? $_POST['custom_post_type_onomies_checked_ids'] : array();
if ( ! empty( $post_ids ) && ! empty( $taxonomy ) ) {
$tax = get_taxonomy( $taxonomy );
// Check permissions.
if ( current_user_can( $tax->cap->assign_terms ) ) {
foreach ( $post_ids as $post_id ) {
/*
* Set object terms.
*
* "append" is set to true so it doesn't
* delete relationships, only creates.
*/
if ( ! empty( $checked_ids ) ) {
$cpt_onomy->wp_set_object_terms( $post_id, $checked_ids, $taxonomy, true );
}
}