This repository has been archived by the owner on Mar 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 3
/
custom-type.php
1858 lines (1548 loc) · 67.1 KB
/
custom-type.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
/*
Plugin Name: Easy Post Types
Plugin URI: http://www.wpeasyposttypes.com
Description: Handle multiple post types and related custom fields and templates
Author: New Signature
Version: 1.4.4
Author URI: http://newsignature.com/
*/
include "classes/db.php";
include "classes/javascript.php";
include "classes/custom-textfield/custom-textfield.php";
include "classes/custom-select/custom-select.php";
include "classes/custom-checkbox/custom-checkbox.php";
include "classes/custom-datefield/custom-datefield.php";
include "classes/custom-urlfield/custom-urlfield.php";
include "classes/custom-image/custom-image.php";
include "classes/custom-referencefield/custom-referencefield.php";
define( 'BOXES_TEMPLATES_DIR', dirname(__FILE__).'/boxes/' );
define( 'CONTEXFLOW_TEMPLATES_DIR', dirname(__FILE__).'/templates/' );
define( 'TEMPLATES_DIR', CONTEXFLOW_TEMPLATES_DIR );
define( 'PLUGIN_POST_KEY', md5('cct') );
define( 'PLUGIN_DIR', dirname(__FILE__).'/' );
define( 'EASYPOSTTYPES_PLUGIN_URL', plugins_url( '', __FILE__).'/');
define( 'EASYPOSTTYPES_ICONS_URL', EASYPOSTTYPES_PLUGIN_URL.'icons/');
class CustomFields {
private $version="1.4.4";
public $ajaxUrl;
public $types = array();
public $registeredPostTypes= array();
public $fields_info = array();
public $properties;
public $dbclass;
public $jsclass;
public $labels;
public $permissions;
public $httpRoot;
public $supports = array(
'title','editor','author',
'thumbnail','excerpt','trackbacks',
'custom_fields','comments','revisions',
'parent_child_relationships', 'page_attributes');
public function __toString()
{
return "Easy Post Types";
}
public function __construct() {
global $wpdb;
$this->ajaxUrl = admin_url('admin-ajax.php');
$this->httpRoot = EASYPOSTTYPES_PLUGIN_URL;
load_plugin_textdomain('cct', false, dirname( plugin_basename( __FILE__ ) ) );
$this->permissions = array(
'permission_edit_object',
'permission_edit_type',
'permission_edit_others_objects',
'permission_publish_objects',
'permission_read_object',
'permission_read_private_object',
'permission_delete_object'
);
$this->properties = array(
'fieldset1' => array('fieldset','group-1', 'open'),
'label' => array('textfield',__('Label','cct')),
'singular_label' => array('textfield',__('Singular Label','cct')),
'public' => array('checkbox',__('Public','cct')),
'has_archive' => array('checkbox',__('Has Archive URL','cct')),
'fieldset2' => array('fieldset', 'group-1', 'close'),
'show_ui' => array('checkbox',__('Show UI','cct')),
'rewrite' => array('checkbox',__('Rewrite','cct')),
'query_var' => array('checkbox',__('Query Var','cct')),
'hierarchical' => array('checkbox',__('Hierarchical','cct')),
'capability_type' => array('select', __('Capability Type','cct'), array('post','page')),
'supports' => array('multiselect', __('Supports','cct'), 0)
);
$this->labels = array(
'name' => array( _x('Posts', 'post type general name'), _x('Pages', 'post type general name'), __('Name','cct')),
'singular_name' => array( _x('Post', 'post type singular name'), _x('Page', 'post type singular name'),__('Singular Name','cct') ),
'add_new' => array( _x('Add New', 'post'), _x('Add New', 'page'),__('Add New','cct') ),
'add_new_item' => array( __('Add New Post','cct'), __('Add New Page','cct'),__('Add new Item','cct') ),
'edit_item' => array( __('Edit Post','cct'), __('Edit Page','cct'), __('Edit Item','cct') ),
'edit' => array( _x('Edit', 'post'), _x('Edit', 'page'), __('Edit','cct') ),
'new_item' => array( __('New Post','cct'), __('New Page','cct'), __('New Item','cct') ),
'view_item' => array( __('View Post','cct'), __('View Page','cct'), __('View Item','cct') ),
'search_items' => array( __('Search Posts','cct'), __('Search Pages','cct'),__('Search Items','cct') ),
'not_found' => array( __('No posts found','cct'), __('No pages found','cct'), __('Not Found','cct') ),
'not_found_in_trash' => array( __('No posts found in Trash','cct'), __('No pages found in Trash','cct'), __("Not Found in Trash",'cct') ),
'view' => array( __('View Post','cct'), __('View Page','cct'), __('View','cct') ),
'parent_item_colon' => array( null, __('Parent Page:','cct'), __('Parent Item','cct') )
);
add_action('init', array($this, 'init' ));
add_action('save_post', array($this, 'save_postdata'));
add_action('admin_menu', array($this, 'main_menu'));
add_action('admin_init', array($this, 'post_handler'));
add_action('admin_init', array($this, 'add_meta_boxes'));
add_action('restrict_manage_posts',array($this, 'restrict_listings_by_category'));
add_action('wp_ajax_update_labels', array($this,'ajax_update_labels'));
add_action('wp_ajax_update_permissions', array($this,'ajax_update_permissions'));
add_action('wp_ajax_update_admin', array($this,'ajax_update_admin'));
add_action('wp_ajax_update_visibility', array($this,'ajax_update_visibility'));
add_action('wp_ajax_load_quick_edit', array($this,'ajax_load_quick_edit'));
add_action('wp_ajax_export_content', array($this,'ajax_export_content'));
add_action('wp_ajax_import_content', array($this,'ajax_import_content'));
add_action('wp_ajax_delete_field', array($this,'ajax_delete_field'));
add_action('wp_ajax_edit_field', array($this,'ajax_edit_field'));
add_action('wp_ajax_update_field', array($this,'ajax_update_field'));
add_action('wp_ajax_edit_category', array($this,'ajax_edit_category'));
add_action('wp_ajax_update_category', array($this,'ajax_update_category'));
add_action('wp_ajax_save_categories_used_by_content_type', array($this,'ajax_save_categories_used_by_content_type'));
add_action('wp_ajax_add_category', array($this,'ajax_add_category'));
add_action('wp_ajax_reload_content', array($this,'ajax_reload_content'));
add_action('wp_ajax_reload_page_content', array($this,'ajax_reload_page_content'));
add_action('wp_ajax_reload_fieldtype', array($this,'ajax_reload_fieldtype'));
add_action('wp_ajax_save_field', array($this,'ajax_save_field'));
add_action('wp_ajax_delete_content', array($this,'ajax_delete_content'));
add_action('wp_ajax_move_content', array($this,'ajax_move_content'));
add_action('wp_ajax_update_field_type_settings', array($this, 'ajax_update_field_type_settings'));
add_action('wp_ajax_add_field', array($this, 'ajax_add_field'));
add_action('admin_footer-edit.php', array($this, 'post_page_edit_icon'));
add_action('admin_footer-post.php', array($this, 'post_page_edit_icon'));
add_action('contextual_help_list', array($this, 'admin_help'), 10, 2);
add_filter('get_user_option_closedpostboxes_content-type', array($this, 'default_closed_boxes_contenttype'), 10, 3 ); // default closed meta boxes
add_filter('parse_query',array($this, 'convert_taxonomy_term_in_query'));
$this->dbclass = new CustomFields_db();
}
public function prepare_labels() {
$output = "";
foreach($this->labels as $key=>$label) {
$output .= "data['post_label_".$key."']=jQuery(\"#post_label_".$key.'").val();';
$output .= "data['page_label_".$key."']=jQuery(\"#page_label_".$key.'").val();';
}
return $output;
}
public function prepare_permissions() {
$output = "";
foreach($this->permissions as $key=>$label) {
$output .= "data['".$label."']=jQuery('#".$label."').val();\n";
}
return $output;
}
/**
* Create the admin pages
*
* This register an admin menu and sub admin pages. It attaches a callback for each page.
* It also setups the scripts and styles to be included on the admin pages it registers.
*
*/
public function main_menu() {
$page1=add_menu_page('Page title', 'Post Types', 'administrator', 'ct_general', array($this, 'options'), EASYPOSTTYPES_PLUGIN_URL.'images/easy-post-type-small.png');
add_submenu_page( 'ct_general', 'Easy Post Types General Settings', __('General Settings','cct'), 'administrator', 'ct_general', array($this, 'noop'));
$page2=add_submenu_page( 'ct_general', 'Easy Post Types Edit Fields Settings', __('Edit Fields','cct'), 'administrator', 'ct_editcontent', array($this, 'edit_content'));
$page3=add_submenu_page( 'ct_general', 'Easy Post Types Utilities', __('Utilities','cct'), 'administrator', 'ct_utilities', array($this, 'utilities'));
add_action('admin_print_scripts-'.$page1, array($this, 'options_scripts' ));
add_action('admin_print_scripts-'.$page2, array($this, 'edit_scripts' ));
add_action('admin_print_scripts-'.$page3, array($this, 'options_scripts' ));
add_action('admin_print_styles-'.$page1, array($this, 'print_admin_styles' ));
add_action('admin_print_styles-'.$page2, array($this, 'print_admin_styles' ));
add_action('admin_print_styles-'.$page3, array($this, 'print_admin_styles' ));
}
public function noop(){}
public function admin_help($contextual_help, $screen) {
if($screen->id == 'toplevel_page_ct_general') {
$contextual_help[$screen->id] = __('Help for the custom post types settings page','cct');
} else {
$contextual_help[$screen->id] = '';
}
if($screen->parent_base == 'ct_general'){
$contextual_help[$screen->id] .= '<h5>' . __('General Help','cct') . '</h5>';
$contextual_help[$screen->id] .= __('General Help','cct');
}
return $contextual_help;
}
public function selectContentType() {
if (count($this->registeredPostTypes)>0) {
$choices='<option selected value="">'.__('Please Select','cct').'</option>';
foreach($this->registeredPostTypes as $key=>$obj){
$choices.='<option value="'.$obj.'">'.$obj.'</option>';
}
$changeAction="javascript:window.location='".get_bloginfo('wpurl')."/wp-admin/admin.php?page=ct_editcontent&type='+jQuery('#ct_name').val()";
echo '<select onChange="'.$changeAction.'" id="ct_name" name="ct_name">'.$choices.'</select>';
}
else {
_e('Please add a post type first...','cct');
}
}
public function convert_taxonomy_term_in_query($query) {
global $pagenow;
if ($pagenow=='edit.php') {
$list = $this->fields_info['categories'];
if (is_array($list)) {
$qv = &$query->query_vars;
foreach($list as $key=>$cat) {
if (isset($qv[$key]) && isset($this->fields_info['categories'][$key]['refinesearch'])) {
$term = get_term_by('id',$qv[$key],$key);
unset($qv[$key]);
$qv['term'] = $term->slug;
$qv['taxonomy'] = $key;
}
}
}
}
}
public function restrict_listings_by_category() {
global $typenow;
global $wp_query;
$list = $this->fields_info['categories'];
if ($list) {
foreach($list as $key => $cat) {
if (isset($this->fields_info['categories'][$key]['refinesearch'])) {
$taxonomy = get_taxonomy($key);
wp_dropdown_categories(array(
'show_option_all' => __("Show All {$taxonomy->label}"),
'taxonomy' => $key,
'name' => $taxonomy->name,
'orderby' => 'name',
'selected' => $wp_query->query['term'],
'hierarchical' => true,
'depth' => 3,
'show_count' => true, // Show # listings in parens
'hide_empty' => true, // Don't show businesses w/o listings
));
}
}
}
}
/**
* Render the edit field form
*
* @param $values
*/
public function edit_field_form($values=array(), $errors=array(), $is_new=true) {
include CONTEXFLOW_TEMPLATES_DIR.'field-form.php';
}
/**
* Render the settings for a form type in the edit field
*
* @param $field_type - the key for the type of field to render
* @param $content_type - the post type
*/
public function edit_field_form_type_fields($field_type, $content_type='', $values=array()){
$fieldType=$this->getFieldType($field_type);
include CONTEXFLOW_TEMPLATES_DIR.'field-form-field-settings.php';
}
/**
* Render the category form
*
* @param $category
* @param $errors
*/
public function edit_category_form($category, $errors=array()) {
include CONTEXFLOW_TEMPLATES_DIR.'add-category.php';
}
public function getFieldInfo($id, $type, $name) {
return array('extra' => $this->fields_info['fields'][$type][$name]['extra'],
'info' => $this->fields_info['fields'][$type][$name]);
}
public function saveField() {
if (empty($_POST['content_type']) ||
empty($_POST['ct_name']) ||
empty($_POST['field_name']) ||
empty($_POST['name'])) return false;
$type=$_POST['ct_name'];
$fieldType=$this->getFieldType($type);
$this->fields_info['fields'][$_POST['content_type']][$_POST['field_name']]=array(
'field_name' => $_POST['field_name'],
'name' => $_POST['name'],
'type' => $type,
'show_list' => $_POST['show_list'],
'extra' => $fieldType->extra($_POST));
$this->save_content_type();
return true;
}
public function saveCategory() {
if (empty($_POST['content_type']) ||
empty($_POST['internal_name'])||
empty($_POST['name'])){
return false;
}
$type=$_POST['content_type'];
$this->fields_info['categories'][$_POST['internal_name']]=array (
'internal_name' => $_POST['internal_name'],
'postpages' => $_POST['postpages']=='on',
'refinesearch' => $_POST['refinesearch']=='on',
'object_type' => array($type),
'filters' => array(
'hierarchical' => $_POST['hierarchical']=='on',
'query_var' => $_POST['query_var']=='on',
'rewrite' => $_POST['rewrite']=='on',
'public' => $_POST['public']=='on',
'label' => $_POST['name']
)
);
if ($_POST['postpages']=='on')
$postpages = array('post','page');
else
$postpages = array();
register_taxonomy($_POST['internal_name'], array_merge(array($type),$postpages), $this->fields_info['categories'][$_POST['internal_name']]['filters']);
$this->save_content_type();
return true;
}
/**
* Add/Update a field settings
*
* @param $content_type string - the key for the post type to add a field to
* @param $field_key string - the key for the field
* @param $field_type string - the name of the type of field
* @param $field_name string - the display name of the field
* @param $add_as_column boolean - flag to display the field as a column in the table of posts
* @param $extra array - settings for the field type
*/
public function update_field($content_type, $field_key, $field_type, $field_name=null, $add_as_column=null, $extra=null){
// $field_key, $content_type and $field_type are required to be non-empty
if(empty($field_key) || empty($content_type) || empty($field_type)){
return false;
}
// Get the definition for the field type
$field_type_def =$this->getFieldType($field_type);
if($field_type_def==null){
// return if not set
return false;
}
// Check if we are updating or adding
if(isset($this->fields_info['fields'][$content_type][$field_key])){
// update the field
$update = array();
if(!empty($field_name)){
$update['name'] = $field_name;
}
if(!empty($add_as_column)){
$update['show_list'] = $add_as_column;
}
if($extra!=null){
$update['extra'] = $field_type_def->extra($extra);
}
$this->fields_info['fields'][$content_type][$field_key] = array_merge( $this->fields_info['fields'][$content_type][$field_key], $update );
} else {
// new field
$this->fields_info['fields'][$content_type][$field_key] = array(
'name' => $field_name,
'field_name' => $field_key,
'type' => $field_type,
'show_list' => $add_as_column,
'extra' => $field_type_def->extra($extra),
);
}
$this->save_content_type();
return $this->fields_info['fields'][$content_type][$field_key];
}
/**
* Register a custom post type form handler
*/
public function register_post_type_form_handler() {
global $user_login;
// get the really basic information first
$post_type_key = $_POST['content_type'];
$name_plural = $_POST['label'];
$name_singular = $_POST['singular_label'];
// Will not save without a name for the post type
if(empty($name_plural)){
return;
}
// is this a new post type?
$is_new = empty($post_type_key) || !isset($this->fields_info['types'][$post_type_key]);
// Validate/create key
if(!$is_new && empty($post_type_key)){
return; // it must have a key for an existing post type
} else if(empty($post_type_key)){
// create a unique key for a new post type
$post_type_key = $this->sanitize_key($name_plural);
if(isset($this->fields_info[$post_type_key])){
$i = 1;
while(isset($this->fields_info[$post_type_key.'-'.$i])){
++$i;
}
$post_type_key = $post_type_key.'-'.$i;
}
}
// redundant check to stop if key is missing
if(empty($post_type_key)){
return;
}
// Convert a value of 'on' to true, a new content type to true, otherwise, false
$pretty_urls = (!empty($_POST['pretty_urls']) && $_POST['pretty_urls']=='on') || $is_new;
// 'label' = $name_plural
// 'content_type' = $post_type_key
// 'singular_label' = $name_singular
// 'pretty_urls' = $pretty_urls
// Save the names
$this->fields_info['types'][$post_type_key]['label']= $name_plural;
$this->fields_info['types'][$post_type_key]['singular_label']=(empty($name_singular) ? $name_plural : $name_singular);
// Save all the supported features
foreach($this->supports as $key) {
$this->fields_info['types'][$post_type_key][$key]= (!empty($_POST[$key]) && $_POST[$key]=='on');
}
// Save creation time and user
if (!isset($this->fields_info['types'][$post_type_key]['created'])) {
$this->fields_info['types'][$post_type_key]['created']=time();
$this->fields_info['types'][$post_type_key]['createdby']=$user_login;
}
// Save the pretty URLs setting
$this->fields_info['types'][$post_type_key]['rewrite']= $pretty_urls;
// CUSTOM REWRITE RULES
/*$this->fields_info['types'][$post_type_key]['rewrite']= $pretty_urls;
if($pretty_urls) {
global $wp_rewrite;
$post_type_slug = str_replace('_', '-', $post_type_key);
$permalink_structure = '/'.$post_type_slug.'/%year%/%monthnum%/%'.$post_type_slug.'%';
$wp_rewrite->add_rewrite_tag('%'.$post_type_slug.'%', '([^/]+)', $post_type_key.'=');
$wp_rewrite->add_permastruct($post_type_slug, $permalink_structure, false);
flush_rewrite_rules();
}*/
// Default settings for a new post type
// show_ui
if (!isset($this->fields_info['types'][$post_type_key]['show_ui'])) {
$this->fields_info['types'][$post_type_key]['show_ui']=true;
}
// public
if (!isset($this->fields_info['types'][$post_type_key]['public'])) {
$this->fields_info['types'][$post_type_key]['public']=true;
}
// Add the special columns for the admin screen with all the objects of the post type
$this->fields_info['fields'][$post_type_key]['admin_columns']=array('cb','title','author');
$this->save_content_type();
if ($is_new) flush_rewrite_rules(); // Make sure permalinks knows about our new post type.
}
public function utilities() {
include BOXES_TEMPLATES_DIR.'utility.php';
}
public function unset_categories($type) {
if (is_array($this->fields_info['categories'])) {
foreach($this->fields_info['categories'] as $key=>$category) {
if (in_array($type, $category['object_type'])) {
$this->fields_info['categories'][$key]['object_type']=array_diff($this->fields_info['categories'][$key]['object_type'], array($type));
}
}
}
}
public function post_handler() {
if (empty($_POST['plugin_post_key']) || $_POST['plugin_post_key']!=PLUGIN_POST_KEY) return;
if (!empty($_POST['update_content']) || !empty($_POST['save']) || !empty($_POST['add_content'])) {
$this->register_post_type_form_handler();
$this->registerContentTypes();
header("location:".admin_url('admin.php').'?'.$_SERVER['QUERY_STRING']);
return;
}
if ( ! empty( $_POST['save_field'] ) ) {
if (empty($_POST['content_type'])) return;
$this->saveField();
header("location:".admin_url('admin.php').'?'.$_SERVER['QUERY_STRING']);
}
if ( ! empty( $_POST['save_category'] ) ) {
if (empty($_POST['content_type'])) return;
//$this->saveCategory();
header("location:".admin_url('admin.php').'?'.$_SERVER['QUERY_STRING']);
}
if ( ! empty( $_POST['doaction'] ) ) {
$action=$_POST['action'];
if (isset($_POST['delete_content_type'])) {
$cts=$_POST['delete_content_type'];
foreach($cts as $key => $value) {
switch($action) {
case 'delete':
$this->dbclass->deleteContentType($value);
$this->unset_categories($value);
unset($this->fields_info['types'][$value]);
unset($this->fields_info['fields'][$value]);
$this->save_content_type();
break;
}
}
}
}
}
public function options() {
$contentTypes = $this->fields_info['types'];
$contentFields = $this->fields_info;
$this->jsclass = new CustomType_Javascript();
$this->jsclass->create($this);
include PLUGIN_DIR . "main.php";
}
/**
* Delete a field from a post type
*
* @param $content_type - the key for the post type to delete the field from.
* @param $field_name - the name of the field to delete
*/
public function remove_field_from_content_type($content_type, $field_name){
unset($this->fields_info['fields'][$content_type][$field_name]);
$this->save_content_type();
}
public function edit_content() {
$content="";
if (isset($_REQUEST['type']))
$type=$_REQUEST['type'];
else
$type="";
if (isset($this->fields_info['types'][$type])) {
$content=$this->fields_info['types'][$type];
$content['systemkey']=$type;
$content['fields'] = $this->fields_info['fields'][$type];
$content['categories'] = empty($this->fields_info['categories']) ? null : $this->fields_info['categories'];
}
$this->jsclass = new CustomType_Javascript();
$this->jsclass->create($this);
include PLUGIN_DIR . "editcontent.php";
}
public function include_template($plugin, $name, $template, $values, $options=array()) {
if (!empty($template)) {
$path=TEMPLATEPATH.'/'.$template;
if (file_exists($path)) {
include($path);
return;
}
}
$path=TEMPLATEPATH.'/theme-'.$plugin->getId().'-'.$name.'.php';
if (file_exists($path))
include($path);
else {
$path=TEMPLATEPATH.'/theme-'.$plugin->getId().'.php';
if (file_exists($path))
include($path);
else
include($plugin->getRoot(). 'theme-'.$plugin->getId().'.php');
}
}
public function theme($post, $name, $options, $template=null, $raw=false) {
$field=$this->getFieldType($this->fields_info['fields'][$post->post_type][$name]['type']);
if ($field)
return $field->theme($post, $name, $options);
else {
echo "<div class=\"error\">";
_e("Template missing for field : ",'cct');
echo $post->post_type.' => '.$name;
echo "</div>";
}
}
public function themeList($post, $name) {
$field=$this->getFieldType($this->fields_info['fields'][$post->post_type][$name]['type']);
if ($field)
return $field->themeList($post, $name);
else {
echo "<div class=\"error\">";
_e("Template missing for field: ",'cct');
echo $post->post_type.' => '.$name;
echo "</div>";
}
}
public function display_fields($type) {
if ($this->fields_info['fields'][$type]) {
echo '<div id="field-list-id" class="field-list">';
echo '<ul class="description">';
foreach($this->fields_info['fields'][$type] as $key =>$value) {
echo "<li>";
echo '<ul class="field_list">';
echo '<li><span class="label">'.__('Type:','cct').'</span><span class="value">'.$value['type'].'</span></li>';
echo '<li><span class="label">'.__('Label:','cct').'</span><span class="value">'.$value['name'].'</span></li>';
echo '<li><span class="label">'.__('Field Name:','cct').'</span><span class="value">'.$value['field_name'].'</span></li>';
echo '</ul>';
echo "</li>";
}
echo '</ul>';
echo '</div>';
}
}
/**
* Delete a field from a content-type via AJAX
*
*
*/
public function ajax_delete_field() {
$retval = array( 'status' => 'success' );
// Check that the content_type is given
if(empty($_POST['content_type'])){
$retval['status'] = 'error';
$retval['message'] = __('No post type was given.', 'cct');
} else {
// Get values from the POST
$content_type = $_POST['content_type'];
$field_name = $_POST['field_name'];
// Delete the field
$this->remove_field_from_content_type($content_type, $field_name);
// Rerender the list of fields
$content=$this->fields_info['types'][$content_type];
$content['systemkey'] = $content_type;
$content['fields'] = $this->fields_info['fields'][$content_type];
ob_start();
$this->list_fields($content);
$fields = ob_get_contents();
ob_end_clean();
$retval['contents'] = $fields;
}
echo $this->json_encode($retval);
exit();
}
// Ajax Call to update labels.
public function ajax_update_labels() {
$type=$_POST['content_type'];
$this->fields_info['types'][$type]['labels']=array();
foreach($this->labels as $key=>$label) {
$this->fields_info['types'][$type]['labels'][$key]=array();
if ($this->fields_info['types'][$type]['parent_child_relationships']==0)
$this->fields_info['types'][$type]['labels'][$key]=$_POST['post_label_'.$key];
else
$this->fields_info['types'][$type]['labels'][$key]=$_POST['page_label_'.$key];
}
$this->save_content_type();
exit();
}
public function ajax_update_permissions() {
$type=$_POST['content_type'];
$this->fields_info['types'][$type]['capabilities']=array();
foreach($this->permissions as $key=>$label) {
$this->fields_info['types'][$type]['capabilities'][]=$_POST[$label];
}
$this->save_content_type();
exit();
}
public function ajax_update_admin() {
$type=$_POST['content_type'];
$fields=split(',',$_POST['fields_to_show_in_table']);
$this->fields_info['types'][$type]['public']=($_POST['public']=='true' || $_POST['public']=='checked')?true:false;
$this->fields_info['types'][$type]['show_ui']=($_POST['show_ui']=='true' || $_POST['show_ui']=='checked')?true:false;
unset($this->fields_info['fields'][$type]['admin_columns']);
foreach ($this->fields_info['fields'][$type] as $key=>$value) {
$this->fields_info['fields'][$type][$key]['show_list']='off';
}
if (is_array($fields)) {
foreach($fields as $field) {
if (isset($this->fields_info['fields'][$type][$field])) {
$this->fields_info['fields'][$type][$field]['show_list']='on';
}
if (strpos($field,'%<')!==false) {
$this->fields_info['fields'][$type]['admin_columns'][]=strtolower(substr($field,2,strlen($field)-4));
}
}
}
echo $_POST['admin_menu_icon'];
$this->fields_info['types'][$type]['menu_position']= (int) $_POST['admin_menu_position'];
$this->fields_info['types'][$type]['menu_icon']=empty($_POST['admin_menu_icon'])? null: $_POST['admin_menu_icon'];
$this->save_content_type();
exit();
}
public function ajax_update_visibility() {
$type=$_POST['content_type'];
$this->fields_info['types'][$type]['query_var']=$_POST['query_var']=='true'?true:false;
$this->fields_info['types'][$type]['exclude_from_search']=$_POST['exclude_from_search']=='true'?false:true;
$this->save_content_type();
exit();
}
public function list_fields($content) {
include BOXES_TEMPLATES_DIR.'field-rows.php';
}
public function ajax_edit_field() {
$name=empty($_POST['field_name']) ? '' : $_POST['field_name'] ;
$type=empty($_POST['content_type']) ? '' : $_POST['content_type'];
$is_new=!empty($_POST['new_field']) && $_POST['new_field']=='true';
$values= empty($this->fields_info['fields'][$type][$name]) ? array() : $this->fields_info['fields'][$type][$name];
$this->edit_field_form($values, array(), $is_new);
exit();
}
public function ajax_update_field() {
$sorted=array();
$order=$_POST['order'];
$type=$_POST['content_type'];
$_REQUEST['type']=$_POST['content_type'];
$keys=split(',',$order);
$index=0;
foreach($keys as $key) {
if ($key=='%fieldset%') {
$sorted['fieldset'.$index++]=array('type'=>'_fieldset');
}
elseif(array_key_exists($key, $this->fields_info['fields'][$type])) {
$sorted[$key]=$this->fields_info['fields'][$type][$key];
}
}
$this->fields_info['fields'][$type]=$sorted;
$this->save_content_type();
if (isset($this->fields_info['types'][$type])) {
$content=$this->fields_info['types'][$type];
$content['systemkey']=$type;
$content['fields'] = $this->fields_info['fields'][$type];
$content['categories'] = $this->fields_info['categories'];
}
$this->box_fields($content);
exit();
}
/**
* Update/Add a category
*
* @param $key - the string that is a key for the category
* @param $options - an array of options
* $options.object_type
* $options.hierarchical
* $options.query_var
* $options.rewrite
* $options.public
* $options.label
*
* @return boolean for status, true if successful, otherwise, false
*/
public function update_category($key, $options=array()) {
if(empty($key)){
return false;
}
if(isset($this->fields_info['categories'][$key])){
$options = array_merge($this->fields_info['categories'][$key]['filters'], array( 'object_type' => $this->fields_info['categories'][$key]['object_type']), $options);
} else {
$options = array_merge(array(
'object_type' => array(),
'hierarchical'=> true,
'query_var' => true,
'rewrite' => true,
'public' => true,
'label' => $key,
), $options);
}
$this->fields_info['categories'][$key]=array (
'internal_name' => $key,
'postpages' => $options['postpages'],
'refinesearch' => $options['refinesearch'],
'object_type' => $options['object_type'],
'filters' => array(
'hierarchical' => $options['hierarchical'],
'query_var' => $options['query_var'],
'rewrite' => $options['rewrite'],
'public' => $options['public'],
'label' => $options['label']
)
);
if ($options['postpages'])
$postpages = array('post','page');
else
$postpages = array();
register_taxonomy($key, array_merge($options['object_type'],$postpages), $this->fields_info['categories'][$key]['filters']);
$this->save_content_type();
return true;
}
/**
* Returns the form for a category
*/
public function ajax_edit_category() {
if (isset($_POST['category'])) {
$category=$this->fields_info['categories'][$_POST['category']];
}
else
$category=array();
$this->edit_category_form($category);
exit();
}
/**
* Add a category via AJAX
*
* This is the callback for an AJAX response to add a new category.
* It creates a new category and attaches it to the post type that created it.
*/
public function ajax_add_category() {
return $this->_ajax_save_category(true);
}
/**
* Renders the category list for a post type
*
*/
public function _rerender_categories_via_ajax($content_type) {
$content=$this->fields_info['types'][$content_type];
$content['systemkey']=$content_type;
$content['fields'] = $this->fields_info['fields'][$content_type];
$content['categories'] = $this->fields_info['categories'];
ob_start();
$this->list_categories($content);
$cats = ob_get_contents();
ob_end_clean();
return $cats;
}
public function _ajax_save_category($is_new){
$retval = array(
'status' => 'success',
);
// Abort if content_type is not defined for a new category
if ($is_new && empty($_POST['content_type'])){
$retval = array(
'status' => 'error',
'message' => __('Missing post type to associate the category with.', 'cct')
);
// Return the form back with error since the Name is missing
} elseif (empty($_POST['name'])) {
ob_start();
$this->edit_category_form($_POST, array('name'));
$form = ob_get_contents();
ob_end_clean();
$retval = array(
'status' => 'error',
'message' => __('You need to give a name for the category.', 'cct'),
'form' => $form,
);
// Abort if updating a category and the internal_name is missing
} elseif(!$is_new && empty($_POST['internal_name'])){
$retval = array(
'status' => 'error',
'message' => __('The system name is missing.', 'cct'),
);
// Save the category
} else {
// The options for the category
$cat_options = array(
'hierarchical' => $_POST['hierarchical']=='on',
'query_var' => $_POST['query_var']=='on',
'rewrite' => $_POST['rewrite']=='on',
'public' => $_POST['public']=='on',
'label' => $_POST['name']
);
$cat_options['postpages'] = $_POST['postpages']=='on';
$cat_options['refinesearch'] = $_POST['refinesearch']=='on';
// If new, then create an internal_name if not provided and set the object_type to the current post type.
if($is_new){
// Create an internal name is not provided, and sanitize the internal name
$_POST['internal_name'] = empty($_POST['internal_name']) ? $this->sanitize_key($_POST['name']) : $this->sanitize_key($_POST['internal_name']);
$cat_options['object_type'] = array($_POST['content_type']);
}
$this->update_category($_POST['internal_name'], $cat_options);
// Rerender the list of categories and return it to update the list
$retval['categories'] = $this->_rerender_categories_via_ajax($_POST['content_type']);
}
echo $this->json_encode($retval);
exit();
}
/**
* Update the settings for an existing category via AJAX
*/
public function ajax_update_category() {
return $this->_ajax_save_category(false);
}
/**
* Saves the categories that are used by a post type via AJAX
*
*/