-
Notifications
You must be signed in to change notification settings - Fork 46
/
Copy pathcustom_metadata.php
1416 lines (1122 loc) · 56.8 KB
/
custom_metadata.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: Custom Metadata Manager
Plugin URI: http://wordpress.org/extend/plugins/custom-metadata/
Description: An easy way to add custom fields to your object types (post, pages, custom post types, users)
Author: Automattic, Stresslimit & Contributors
Version: 0.8-dev
Author URI: https://github.com/Automattic/custom-metadata/
Copyright 2010-2013 The Contributors
GNU General Public License, Free Software Foundation <http://creativecommons.org/licenses/GPL/2.0/>
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
*/
/**
* set this to true in your wp-config.php file to enable debug/test mode
*/
if ( ! defined( 'CUSTOM_METADATA_MANAGER_DEBUG' ) )
define( 'CUSTOM_METADATA_MANAGER_DEBUG', false );
if ( CUSTOM_METADATA_MANAGER_DEBUG )
include_once 'custom_metadata_examples.php';
class custom_metadata_manager {
var $errors = array();
var $metadata = array();
var $_non_post_types = array( 'user', 'comment' );
// Object types that come "built-in" with WordPress
var $_builtin_object_types = array( 'post', 'page', 'user', 'comment' );
// Column filter names
var $_column_types = array( 'posts', 'pages', 'users', 'comments' );
// field types
var $_field_types = array( 'text', 'textarea', 'password', 'number', 'email', 'telephone', 'checkbox', 'radio', 'select', 'multi_select', 'upload', 'wysiwyg', 'datepicker', 'datetimepicker', 'timepicker', 'colorpicker', 'taxonomy_select', 'taxonomy_radio', 'taxonomy_checkbox', 'link' );
// field types that are cloneable
var $_cloneable_field_types = array( 'text', 'textarea', 'upload', 'password', 'number', 'email', 'tel' );
// field types that support a default value
var $_field_types_that_support_default_value = array( 'text', 'textarea', 'password', 'number', 'email', 'telephone', 'upload', 'wysiwyg', 'datepicker', 'datetimepicker', 'timepicker', 'link' );
// field types that support the placeholder attribute
var $_field_types_that_support_placeholder = array( 'text', 'textarea', 'password', 'number', 'email', 'tel', 'upload', 'datepicker', 'datetimepicker', 'timepicker', 'link' );
// field types that are read only by default
var $_field_types_that_are_read_only = array( 'upload', 'link', 'datepicker', 'datetimepicker', 'timepicker' );
// field types that support being part of a multifield group
// @todo: workarounds needed for other field types
var $_field_types_that_support_multifield = array( 'text', 'textarea', 'password', 'number', 'email', 'tel', 'select' );
// taxonomy types
var $_taxonomy_fields = array( 'taxonomy_select', 'taxonomy_radio', 'taxonomy_checkbox', 'taxonomy_multi_select' );
// filed types that are saved as multiples but not cloneable
var $_multiple_not_cloneable = array( 'taxonomy_checkbox' );
// fields that always save as an array
var $_always_multiple_fields = array( 'taxonomy_checkbox', 'multi_select', 'taxonomy_multi_select' );
// Object types whose columns are generated through apply_filters instead of do_action
var $_column_filter_object_types = array( 'user' );
// Whitelisted pages that get stylesheets and scripts
var $_pages_whitelist = array( 'edit.php', 'post.php', 'post-new.php', 'users.php', 'profile.php', 'user-edit.php', 'edit-comments.php', 'comment.php' );
// the default args used for the wp_editor function
var $default_editor_args = array();
// singleton instance
private static $instance;
public static function instance() {
if ( isset( self::$instance ) )
return self::$instance;
self::$instance = new custom_metadata_manager;
self::$instance->run_initial_hooks();
return self::$instance;
}
// do nothing on construct
function __construct() {}
function run_initial_hooks() {
add_action( 'admin_init', array( $this, 'admin_init' ), 1000, 0 );
}
function admin_init() {
global $pagenow;
// filter our vars
$this->_non_post_types = apply_filters( 'custom_metadata_manager_non_post_types', $this->_non_post_types );
$this->_builtin_object_types = apply_filters( 'custom_metadata_manager_builtin_object_types', $this->_builtin_object_types );
$this->_column_types = apply_filters( 'custom_metadata_manager_column_types', $this->_column_types );
$this->_field_types = apply_filters( 'custom_metadata_manager_field_types', $this->_field_types );
$this->_cloneable_field_types = apply_filters( 'custom_metadata_manager_cloneable_field_types', $this->_cloneable_field_types );
$this->_field_types_that_support_default_value = apply_filters( 'custom_metadata_manager_field_types_that_support_default_value', $this->_field_types_that_support_default_value );
$this->_field_types_that_support_placeholder = apply_filters( 'custom_metadata_manager_field_types_that_support_placeholder', $this->_field_types_that_support_placeholder );
$this->_field_types_that_are_read_only = apply_filters( 'custom_metadata_manager_field_types_that_are_read_only', $this->_field_types_that_are_read_only );
$this->_field_types_that_support_multifield = apply_filters( 'custom_metadata_manager_field_types_that_support_multifield', $this->_field_types_that_support_multifield );
$this->_taxonomy_fields = apply_filters( 'custom_metadata_manager_cloneable_field_types', $this->_taxonomy_fields );
$this->_column_filter_object_types = apply_filters( 'custom_metadata_manager_column_filter_object_types', $this->_column_filter_object_types );
$this->_pages_whitelist = apply_filters( 'custom_metadata_manager_pages_whitelist', $this->_pages_whitelist );
$this->default_editor_args = apply_filters( 'custom_metadata_manager_default_editor_args', $this->default_editor_args );
define( 'CUSTOM_METADATA_MANAGER_SELECT2_VERSION', '3.2' ); // version for included select2.js
define( 'CUSTOM_METADATA_MANAGER_TIMEPICKER_VERSION', '1.2' ); // version for included timepicker
define( 'CUSTOM_METADATA_MANAGER_VERSION', '0.8-dev' );
define( 'CUSTOM_METADATA_MANAGER_URL' , apply_filters( 'custom_metadata_manager_url', trailingslashit( plugins_url( '', __FILE__ ) ) ) );
$this->init_object_types();
// Hook into load to initialize custom columns
if ( in_array( $pagenow, $this->_pages_whitelist ) ) {
add_action( 'load-' . $pagenow, array( $this, 'init_metadata' ) );
}
// Hook into admin_notices to show errors
if ( current_user_can( 'manage_options' ) )
add_action( 'admin_notices', array( $this, '_display_registration_errors' ) );
do_action( 'custom_metadata_manager_init' );
do_action( 'custom_metadata_manager_admin_init' );
}
function init_object_types() {
foreach ( array_merge( get_post_types(), $this->_builtin_object_types ) as $object_type )
$this->metadata[$object_type] = array();
}
function init_metadata() {
$object_type = $this->_get_object_type_context();
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_scripts' ) );
add_action( 'admin_enqueue_scripts', array( $this, 'enqueue_styles' ) );
$this->init_columns();
// Handle actions related to users
if ( $object_type == 'user' ) {
global $user_id;
if ( empty( $user_id ) )
$user_id = get_current_user_id();
// Editing another user's profile
add_action( 'edit_user_profile', array( $this, 'add_user_metadata_groups' ) );
add_action( 'edit_user_profile_update', array( $this, 'save_user_metadata' ) );
// Allow user-editable fields on "Your Profile"
add_action( 'show_user_profile', array( $this, 'add_user_metadata_groups' ) );
add_action( 'personal_options_update', array( $this, 'save_user_metadata' ) );
} else {
// Hook in to metaboxes
add_action( 'add_meta_boxes', array( $this, "add_post_metadata_groups" ) );
// Hook in to save
add_action( 'save_post', array( $this, 'save_post_metadata' ) );
add_action( 'edit_comment', array( $this, 'save_comment_metadata' ) );
}
do_action( 'custom_metadata_manager_init_metadata', $object_type );
add_action( 'admin_footer', array( $this, '_display_wp_link_dialog' ) );
}
function init_columns() {
$object_type = $this->_get_object_type_context();
// This is not really that clean, but it works. Damn inconsistencies!
if ( post_type_exists( $object_type ) ) {
$column_header_name = sprintf( '%s_posts', $object_type );
$column_content_name = ( 'page' != $object_type ) ? 'posts' : 'pages';
} elseif ( $object_type == 'comment' ) {
$column_header_name = 'edit-comments';
$column_content_name = 'comments';
} else {
// users
$column_header_name = $column_content_name = $object_type . 's';
}
// Hook into Column Headers
add_filter( "manage_{$column_header_name}_columns", array( $this, 'add_metadata_column_headers' ) );
// User and Posts have different functions
$custom_column_content_function = array( $this, "add_{$object_type}_metadata_column_content" );
if ( ! is_callable( $custom_column_content_function ) )
$custom_column_content_function = array( $this, 'add_metadata_column_content' );
// Hook into Column Content. Users get filtered, others get actioned.
if ( ! in_array( $object_type, $this->_column_filter_object_types ) )
add_action( "manage_{$column_content_name}_custom_column", $custom_column_content_function, 10, 3 );
else
add_filter( "manage_{$column_content_name}_custom_column", $custom_column_content_function, 10, 3 );
}
function enqueue_scripts() {
wp_enqueue_media();
wp_enqueue_script( 'wplink' );
wp_enqueue_script( 'wpdialogs-popup' );
wp_enqueue_style( 'wp-jquery-ui-dialog' );
wp_enqueue_script( 'select2', apply_filters( 'custom_metadata_manager_select2_js', CUSTOM_METADATA_MANAGER_URL .'js/select2.min.js' ), array( 'jquery' ), CUSTOM_METADATA_MANAGER_SELECT2_VERSION, true );
wp_enqueue_script( 'timepicker', apply_filters( 'custom_metadata_manager_timepicker_js', CUSTOM_METADATA_MANAGER_URL .'js/jquery-ui-timepicker.min.js' ), array( 'jquery', 'jquery-ui-datepicker' ), CUSTOM_METADATA_MANAGER_TIMEPICKER_VERSION, true );
wp_enqueue_script( 'custom-metadata-manager-js', apply_filters( 'custom_metadata_manager_default_js', CUSTOM_METADATA_MANAGER_URL .'js/custom-metadata-manager.js' ), array( 'jquery', 'jquery-ui-datepicker', 'select2' ), CUSTOM_METADATA_MANAGER_VERSION, true );
wp_enqueue_script( 'wp-color-picker' );
}
function enqueue_styles() {
wp_enqueue_style( 'wp-jquery-ui-dialog' );
wp_enqueue_style( 'editor-buttons' );
wp_enqueue_style( 'custom-metadata-manager-css', apply_filters( 'custom_metadata_manager_default_css', CUSTOM_METADATA_MANAGER_URL .'css/custom-metadata-manager.css' ), array(), CUSTOM_METADATA_MANAGER_VERSION );
wp_enqueue_style( 'jquery-ui-datepicker', apply_filters( 'custom_metadata_manager_jquery_ui_css', CUSTOM_METADATA_MANAGER_URL .'css/jquery-ui-smoothness.css' ), array(), CUSTOM_METADATA_MANAGER_VERSION );
wp_enqueue_style( 'select2', apply_filters( 'custom_metadata_manager_select2_css', CUSTOM_METADATA_MANAGER_URL .'css/select2.css' ), array(), CUSTOM_METADATA_MANAGER_SELECT2_VERSION );
wp_enqueue_style( 'wp-color-picker' );
}
function add_metadata_column_headers( $columns ) {
$object_type = $this->_get_object_type_context();
if ( $object_type ) {
$fields = $this->get_fields_in_object_type( $object_type );
foreach ( $fields as $field_slug => $field ) {
if ( $this->is_field_addable_to_columns( $field_slug, $field ) ) {
$columns[$field_slug] = is_string( $field->display_column ) ? $field->display_column : $field->label;
}
}
}
return $columns;
}
function add_user_metadata_column_content( $param, $name, $object_id ) {
return $this->add_metadata_column_content( $name, $object_id, $param );
}
function add_metadata_column_content( $name, $object_id, $column_content = '' ) {
$object_type = $this->_get_object_type_context();
$field_slug = $name;
if ( $this->is_registered_object_type( $object_type ) && $this->is_registered_field( $field_slug, null, $object_type ) ) {
$field = $this->get_field( $field_slug, null, $object_type );
$column_content = $this->_metadata_column_content( $field_slug, $field, $object_type, $object_id );
}
if ( $column_content && ! in_array( $object_type, $this->_column_filter_object_types ) )
echo $column_content;
else
return $column_content;
}
function add_metadata_field( $field_slug, $object_types = array( 'post' ), $args = array() ) {
static $localized_strings;
if ( ! $localized_strings ) {
$localized_strings = (object) array(
'upload_modal_title' => __( 'Choose a file', 'custom-metadata' ), // upload modal title (for upload field only)
'upload_modal_button_text' => __( 'Select this file', 'custom-metadata' ), // upload modal button text (for upload field only)
'upload_clear_button_text' => __( 'Clear', 'custom-metadata' ), // upload clear field text (for upload field only)
'link_modal_button_text' => __( 'Select', 'custom-metadata' ), // link field button text
);
}
$defaults = array(
'group' => '', // To which meta_box the field should be added
'multifield' => false, // which multifield does this field belong to, if any
'field_type' => 'text', // The type of field; possibly values: text, checkbox, radio, select, image
'label' => $field_slug, // Label for the field
'slug' => $field_slug, // Slug for the field
'description' => '', // Description of the field, displayed below the input
'values' => array(), // values for select, checkbox, radio buttons
'default_value' => '', // default value
'placeholder' => '',
'display_callback' => '', // function to custom render the input
'sanitize_callback' => '',
'display_column' => false, // Add the field to the columns when viewing all posts
'display_column_callback' => '',
'add_to_quick_edit' => false, // (post only) Add the field to Quick edit
'required_cap' => false, // the cap required to view and edit the field
'multiple' => false, // can the field be duplicated with a click of a button
'readonly' => false, // makes the field be readonly
'select2' => false, // applies select2.js (work on select and multi select field types)
'min' => false, // a minimum value (for number field only)
'max' => false, // a maximum value (for number field only)
'upload_modal_title' => $localized_strings->upload_modal_title,
'upload_modal_button_text' => $localized_strings->upload_modal_button_text,
'upload_clear_button_text' => $localized_strings->upload_clear_button_text,
'link_modal_button_text' => $localized_strings->link_modal_button_text,
);
// upload field is readonly by default (can be set explicitly to false though)
if ( ! empty( $args['field_type'] ) && in_array( $args['field_type'], $this->_field_types_that_are_read_only ) )
$defaults['readonly'] = true;
// `chosen` arg is the same as `select2` arg
if ( isset( $args['chosen'] ) ) {
$args['select2'] = $args['chosen'];
unset( $args['chosen'] );
}
// Merge defaults with args
$field = wp_parse_args( $args, $defaults );
$field = (object) $field;
// Sanitize slug
$field_slug = sanitize_key( $field_slug );
$group_slug = sanitize_key( $field->group );
// Check to see if the user should see this field
if ( ! empty( $field->required_cap ) && ! current_user_can( $field->required_cap ) )
return;
$field = apply_filters( 'custom_metadata_manager_add_metadata_field', $field, $field_slug, $group_slug, $object_types );
if ( ! $this->_validate_metadata_field( $field_slug, $field, $group_slug, $object_types ) )
return;
// $object_types = (array) $object_type;
// if ( $field->multifield && $this->_multifield_exists_for_group_object( $field->multifield, $group_slug, array_shift( $object_types ) ) ) {
// $this->add_field_to_multifield( $field_slug, $field, $group_slug, $object_types );
// } else {
// add to group
$this->add_field_to_group( $field_slug, $field, $group_slug, $object_types );
// }
}
function add_multifield( $slug, $object_types = array( 'post' ), $args = array() ) {
$defaults = array(
'group' => '', // To which meta_box the multifield should be added
'label' => $slug, // Label for the multifield
'description' => '', // Description of the multifield, displayed below all the fields
'required_cap' => false, // the cap required to view and edit the multifield
);
// Merge defaults with args
$multifield = wp_parse_args( $args, $defaults );
$multifield['multifield'] = true; // force it
$multifield = (object) $multifield;
// Sanitize slug
$slug = sanitize_key( $slug );
$group_slug = sanitize_key( $multifield->group );
// Check to see if the user should see this field
if ( ! empty( $multifield->required_cap ) && ! current_user_can( $multifield->required_cap ) )
return;
$multifield = apply_filters( 'custom_metadata_manager_add_multifield', $multifield, $slug, $group_slug, $object_types );
if ( ! $this->_validate_metadata_field( $slug, $multifield, $group_slug, $object_types ) )
return;
// Add to group
$this->add_multifield_to_group( $slug, $multifield, $group_slug, $object_types );
}
function add_metadata_group( $group_slug, $object_types, $args = array() ) {
$defaults = array(
'label' => $group_slug, // Label for the group
'description' => '', // Description of the group
'context' => 'normal', // (post only)
'priority' => 'default', // (post only)
'autosave' => false, // (post only) Should the group be saved in autosave?
'required_cap' => false, // the cap required to view and edit the group
);
// Merge defaults with args
$group = wp_parse_args( $args, $defaults );
$group = (object) $group;
// Sanitize slug
$group_slug = sanitize_key( $group_slug );
$group = apply_filters( 'custom_metadata_manager_add_metadata_group', $group, $group_slug, $object_types );
// Check to see if the user has caps to view/edit this group
if ( ! empty( $group->required_cap ) && ! current_user_can( $group->required_cap ) )
return;
if ( !$this->_validate_metadata_group( $group_slug, $group, $object_types ) )
return;
$this->add_group_to_object_type( $group_slug, $group, $object_types );
}
function add_field_to_group( $field_slug, $field, $group_slug, $object_types ) {
$object_types = (array) $object_types;
foreach ( $object_types as $object_type ) {
if ( ! $group_slug ) {
$group_slug = sprintf( 'single-group-%1$s-%2$s', $object_type, $field_slug );
}
// If group doesn't exist, create group
if ( ! $this->is_registered_group( $group_slug, $object_type ) ) {
$this->add_metadata_group( $group_slug, $object_type, array( 'label' => ( ! empty( $field->label ) ) ? $field->label : $field_slug ) );
$field->group = $group_slug;
}
$this->_push_field( $field_slug, $field, $group_slug, $object_type );
}
}
function add_multifield_to_group( $slug, $multifield, $group_slug, $object_types ) {
$object_types = (array) $object_types;
foreach ( $object_types as $object_type ) {
if ( ! $group_slug ) {
$group_slug = sprintf( 'single-group-%1$s-%2$s', $object_type, $slug );
}
// If group doesn't exist, create group
if ( ! $this->is_registered_group( $group_slug, $object_type ) ) {
$this->add_metadata_group( $group_slug, $object_type, array( 'label' => ( ! empty( $multifield->label ) ) ? $multifield->label : $slug ) );
$multifield->group = $group_slug;
}
$this->_push_multifield( $slug, $multifield, $group_slug, $object_type );
}
}
function add_group_to_object_type( $group_slug, $group, $object_types ) {
$object_types = (array) $object_types;
foreach ( $object_types as $object_type ) {
if ( ( $this->is_registered_object_type( $object_type ) && ! $this->is_group_in_object_type( $group_slug, $object_type ) ) ) {
$group->fields = array();
$this->_push_group( $group_slug, $group, $object_type );
}
}
}
function _validate_metadata_group( $group_slug, $group, $object_type ) {
$valid = true;
// TODO: only validate when DEBUG is on?
/*
if( ! $group_slug ) {
} elseif ( $this->is_registered_group( $group_slug, $object_type ) ) {
// TODO: check that it hasn't been registered already
} elseif ( $this->is_restricted_group( $group_slug, $object_type ) ) {
// TODO: check that it isn't restricted
}
*/
return $valid;
}
function _validate_metadata_field( $field_slug, $field, $group_slug, $object_types ) {
// TODO: only validate when DEBUG is on?
$valid = true;
/*
if( !$field_slug ) {
// Check that
$this->_add_registration_error( $field_slug, __( 'You entered an empty slug name for this field!', 'custom-metadata-manager' ) );
$valid = false;
} else if( $this->is_registered_field( $field_slug, $group_slug, $object_type ) ) {
// does field name already exists
$this->_add_registration_error( $field_slug, __( 'This field already exists. Check to see that you\'re not registering the field twice, or use a different slug.', 'custom-metadata-manager' ) );
$valid = false;
} else if( $this->is_restricted_field( $field_slug, $object_type ) ) {
// is field restricted
$this->_add_registration_error( $field_slug, __( 'This field is restricted. Please use a different slug.', 'custom-metadata-manager' ) );
$valid = false;
}
// if display_callback not defined
// show admin_notices error
// show as text field (?)
*/
// TODO: valid object_type?
return $valid;
}
function _add_registration_error( $field_slug, $error_message ) {
$this->errors[] = sprintf( __( '<strong>%1$s:</strong> %2$s', 'custom-metadata-manager' ), $field_slug, $error_message );
}
function add_post_metadata_groups() {
global $post, $comment;
$object_id = 0;
if ( isset( $post ) ) {
$object_id = $post->ID;
} elseif ( isset( $comment ) ) {
$object_id = $comment->comment_ID;
}
$object_type = $this->_get_object_type_context();
$groups = $this->get_groups_in_object_type( $object_type );
if ( $object_id && !empty( $groups ) ) {
foreach ( $groups as $group_slug => $group ) {
$this->add_post_metadata_group( $group_slug, $group, $object_type, $object_id );
}
}
}
function add_post_metadata_group( $group_slug, $group, $object_type, $object_id ) {
$fields = $this->get_fields_in_group( $group_slug, $object_type );
if ( ! empty( $fields ) && $this->is_thing_added_to_object( $group_slug, $group, $object_type, $object_id ) ) {
add_meta_box( $group_slug, $group->label, array( $this, '_display_post_metadata_box' ), $object_type, $group->context, $group->priority, array( 'group' => $group, 'fields' => $fields ) );
}
}
function add_user_metadata_groups() {
global $user_id;
if ( !$user_id ) return;
$object_type = 'user';
$groups = $this->get_groups_in_object_type( $object_type );
if ( !empty( $groups ) ) {
foreach ( $groups as $group_slug => $group ) {
$this->add_user_metadata_group( $group_slug, $group, $object_type, $user_id );
}
}
}
function add_user_metadata_group( $group_slug, $group, $object_type, $user_id ) {
$fields = $this->get_fields_in_group( $group_slug, $object_type );
if ( ! empty( $fields ) && $this->is_thing_added_to_object( $group_slug, $group, $object_type, $user_id ) )
$this->_display_user_metadata_box( $group_slug, $group, $object_type, $fields );
}
function _display_user_metadata_box( $group_slug, $group, $object_type, $fields ) {
global $user_id;
?>
<h3><?php echo $group->label; ?></h3>
<table class="form-table user-metadata-group">
<?php foreach ( $fields as $field_slug => $field ) : ?>
<?php if ( $this->is_thing_added_to_object( $field_slug, $field, $object_type, $user_id ) ) : ?>
<tr valign="top">
<td scope="row">
<?php $this->_display_metadata_field( $field_slug, $field, $object_type, $user_id ); ?>
</td>
</tr>
<?php endif; ?>
<?php endforeach; ?>
</table>
<?php
$this->_display_group_nonce( $group_slug, $object_type );
}
function _display_post_metadata_box( $object, $meta_box ) {
$group_slug = $meta_box['id'];
$group = $meta_box['args']['group'];
$fields = $meta_box['args']['fields'];
$object_type = $this->_get_object_type_context();
// I really don't like using variable variables, but this is the path of least resistence.
if ( isset( $object->{$object_type . '_ID'} ) ) {
$object_id = $object->{$object_type . '_ID'};
} elseif ( isset( $object->ID ) ) {
$object_id = $object->ID;
} else {
_e( 'Uh oh, something went wrong!', 'custom-metadata-manager' );
return;
}
$this->_display_group_description( $group );
foreach ( $fields as $field_slug => $field ) {
if ( $this->is_thing_added_to_object( $field_slug, $field, $object_type, $object_id ) ) {
if ( $this->_is_multifield( $field_slug ) ) {
$this->_display_metadata_multifield( $field_slug, $field, $object_type, $object_id );
} elseif ( empty( $field->multifield ) ) {
$this->_display_metadata_field( $field_slug, $field, $object_type, $object_id );
}
}
}
// Each group gets its own nonce
$this->_display_group_nonce( $group_slug, $object_type );
}
function _display_group_description( $group ) {
if ( ! empty( $group->description ) )
printf( '<div class="custom-metadata-group-description description">%s</div>', $group->description );
}
function _display_group_nonce( $group_slug, $object_type ) {
$nonce_key = $this->build_nonce_key( $group_slug, $object_type );
wp_nonce_field( 'save-metadata', $nonce_key, false );
}
function verify_group_nonce( $group_slug, $object_type ) {
$nonce_key = $this->build_nonce_key( $group_slug, $object_type );
if ( isset( $_POST[$nonce_key] ) )
return wp_verify_nonce( $_POST[$nonce_key], 'save-metadata' );
else
return false;
}
function build_nonce_key( $group_slug, $object_type ) {
return sprintf( 'metadata-%1$s-%2$s', $object_type, $group_slug );
}
function save_user_metadata( $user_id ) {
$object_type = 'user';
$groups = $this->get_groups_in_object_type( $object_type );
foreach ( $groups as $group_slug => $group ) {
$this->save_metadata_group( $group_slug, $group, $object_type, $user_id );
}
}
function save_post_metadata( $post_id ) {
if ( wp_is_post_revision( $post_id ) ) {
return;
}
$post_type = $this->_get_object_type_context();
$groups = $this->get_groups_in_object_type( $post_type );
foreach ( $groups as $group_slug => $group ) {
// TODO: Allow hook into autosave
if ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE && !$group->autosave )
return $post_id;
$this->save_metadata_group( $group_slug, $group, $post_type, $post_id );
}
}
function save_comment_metadata( $comment_id ) {
$object_type = 'comment';
$groups = $this->get_groups_in_object_type( $object_type );
foreach ( $groups as $group_slug => $group ) {
$this->save_metadata_group( $group_slug, $group, $object_type, $comment_id );
}
}
function save_metadata_group( $group_slug, $group, $object_type, $object_id ) {
if ( !$this->verify_group_nonce( $group_slug, $object_type ) ) {
return $object_id;
}
$fields = $this->get_fields_in_group( $group_slug, $object_type );
foreach ( $fields as $field_slug => $field ) {
if ( true === $field->multifield ) {
$this->save_metadata_multifield( $field_slug, $field, $object_type, $object_id );
} elseif ( ! $field->multifield ) {
$this->save_metadata_field( $field_slug, $field, $object_type, $object_id );
}
}
}
function save_metadata_multifield( $slug, $multifield, $object_type, $object_id ) {
if ( isset( $_POST[$slug] ) ) {
$multifield_value = array();
$groupings = $_POST[$slug];
$fields = $this->get_fields_in_multifield( $multifield->group, $slug, $object_type );
foreach ( $groupings as $grouping ) {
$grouping_values = array();
foreach ( $fields as $field_slug => $field ) {
if ( ! empty( $grouping[$field_slug] ) ) {
$grouping_values[$field_slug] = $this->_sanitize_field_value( $field_slug, $field, $object_type, $object_id, $grouping[$field_slug] );
} else {
$grouping_values[$field_slug] = '';
}
}
$multifield_value[] = $grouping_values;
}
$slug = sanitize_key( $slug );
if ( ! in_array( $object_type, $this->_non_post_types ) )
$object_type = 'post';
update_metadata( $object_type, $object_id, $slug, $multifield_value );
} else {
$slug = sanitize_key( $slug );
if ( ! in_array( $object_type, $this->_non_post_types ) )
$object_type = 'post';
delete_metadata( $object_type, $object_id, $slug );
}
}
function save_metadata_field( $field_slug, $field, $object_type, $object_id ) {
if ( isset( $_POST[$field_slug] ) ) {
$value = $this->_sanitize_field_value( $field_slug, $field, $object_type, $object_id, $_POST[$field_slug] );
$this->_save_field_value( $field_slug, $field, $object_type, $object_id, $value );
// save the attachment ID of the upload field as well
if ( $field->field_type == 'upload' && isset( $_POST[$field_slug . '_attachment_id'] ) )
$this->_save_field_value( $field_slug . '_attachment_id', $field, $object_type, $object_id, absint( $_POST[$field_slug . '_attachment_id'] ) );
} else {
$this->_delete_field_value( $field_slug, $field, $object_type, $object_id );
// delete the attachment ID of the upload field as well
if ( $field->field_type == 'upload' && isset( $_POST[$field_slug . '_attachment_id'] ) )
$this->_delete_field_value( $field_slug . '_attachment_id', $field, $object_type, $object_id );
}
}
function get_metadata_mulitifield_value( $slug, $multifield, $object_type, $object_id ) {
return $this->_get_field_value( $slug, $multifield, $object_type, $object_id, true );
}
function get_metadata_field_value( $field_slug, $field, $object_type, $object_id ) {
return $this->_get_field_value( $field_slug, $field, $object_type, $object_id );
}
function is_registered_object_type( $object_type ) {
return array_key_exists( $object_type, $this->metadata ) /*&& is_array( $this->metadata[$object_type] )*/;
}
function is_registered_group( $group_slug, $object_type ) {
return $this->is_registered_object_type( $object_type ) && array_key_exists( $group_slug, $this->get_groups_in_object_type( $object_type ) );
}
function is_registered_field( $field_slug, $group_slug = '', $object_type ) {
if ( $group_slug )
return $this->is_registered_group( $group_slug, $object_type ) && array_key_exists( $field_slug, $this->get_fields_in_group( $group_slug, $object_type ) );
else
return array_key_exists( $field_slug, $this->get_fields_in_object_type( $object_type ) );
}
function is_field_in_group( $field_slug, $group_slug, $object_type ) {
return in_array( $field_slug, $this->get_fields_in_group( $group_slug, $object_type ) );
}
function is_group_in_object_type( $group_slug, $object_type ) {
return array_key_exists( $group_slug, $this->get_groups_in_object_type( $object_type ) );
}
function is_field_addable_to_columns( $field_slug, $field ) {
return is_string( $field->display_column ) || ( is_bool( $field->display_column ) && $field->display_column );
}
function get_field( $field_slug, $group_slug, $object_type ) {
if ( $this->is_registered_field( $field_slug, $group_slug, $object_type ) ) {
if ( $group_slug ) {
return $this->get_single_field_in_group( $field_slug, $group_slug, $object_type );
} else {
return $this->get_single_field_in_object_type( $field_slug, $object_type );
}
}
return null;
}
function get_group( $group_slug, $object_type ) {
if ( $this->is_registered_group( $group_slug, $object_type ) ) {
$groups = $this->get_groups_in_object_type( $object_type );
$group = $groups[$group_slug];
return $group;
}
return null;
}
function get_object_types() {
return array_keys( $this->metadata );
}
function get_groups_in_object_type( $object_type ) {
if ( $this->is_registered_object_type( $object_type ) )
return $this->metadata[$object_type];
return array();
}
function get_single_field_in_group( $field_slug, $group_slug, $object_type ) {
$fields = $this->get_fields_in_group( $group_slug, $object_type );
return isset( $fields[$field_slug] ) ? $fields[$field_slug] : null;
}
function get_fields_in_group( $group_slug, $object_type ) {
$group = $this->get_group( $group_slug, $object_type );
if ( $group ) return (array) $group->fields;
return array();
}
function get_fields_in_multifield( $group_slug, $multifield_slug, $object_type ) {
$group = $this->get_group( $group_slug, $object_type );
$fields_in_multifield = array();
if ( empty( $group ) || empty( $group->fields ) || empty( $group->fields[$multifield_slug] ) )
return $fields_in_multifield;
$_multifields = wp_list_pluck( $group->fields, 'multifield' );
foreach ( $_multifields as $_field_key => $_multifield ) {
if ( empty( $_multifield ) || true === $_multifield )
continue;
if ( $multifield_slug == $_multifield || $multifield_slug == '_x_multifield_' . $_multifield )
$fields_in_multifield[$_field_key] = $group->fields[$_field_key];
}
return $fields_in_multifield;
}
function get_single_field_in_object_type( $field_slug, $object_type ) {
$fields = $this->get_fields_in_object_type( $object_type );
return isset( $fields[$field_slug] ) ? $fields[$field_slug] : null;
}
function get_fields_in_object_type( $object_type ) {
$fields = array();
foreach ( $this->get_groups_in_object_type( $object_type ) as $group_slug => $group ) {
$fields = array_merge( $fields, $this->get_fields_in_group( $group_slug, $object_type ) );
}
return $fields;
}
function _push_group( $group_slug, $group, $object_type ) {
$this->metadata[$object_type][$group_slug] = $group;
}
function _push_field( $field_slug, $field, $group_slug, $object_type ) {
$this->metadata[$object_type][$group_slug]->fields[$field_slug] = $field;
}
function _push_multifield( $slug, $multifield, $group_slug, $object_type ) {
$this->metadata[$object_type][$group_slug]->fields['_x_multifield_' . $slug] = $multifield;
}
function _multifield_exists_for_group_object( $slug, $group_slug, $object_type ) {
$slug = '_x_multifield_' . $slug;
return (
! empty( $this->metadata[$object_type] ) &&
! empty( $this->metadata[$object_type][$group_slug] ) &&
! empty( $this->metadata[$object_type][$group_slug]->fields ) &&
array_key_exists( $slug, $this->metadata[$object_type][$group_slug]->fields )
);
}
function _is_multifield( $slug ) {
return ( 0 === strpos( $slug, '_x_multifield' ) );
}
function is_thing_added_to_object( $thing_slug, $thing, $object_type, $object_id, $object_slug = '' ) {
if ( isset( $thing->exclude ) ) {
if ( is_callable( $thing->exclude ) )
return ! (bool) call_user_func( $thing->exclude, $thing_slug, $thing, $object_type, $object_id, $object_slug );
return ! $this->does_id_array_match_object( $thing->exclude, $object_type, $object_id, $object_slug );
}
if ( isset( $thing->include ) ) {
if ( is_callable( $thing->include ) )
return (bool) call_user_func( $thing->include, $thing_slug, $thing, $object_type, $object_id, $object_slug );
return $this->does_id_array_match_object( $thing->include, $object_type, $object_id, $object_slug );
}
return true;
}
function does_id_array_match_object( $id_array, $object_type, $object_id, $object_slug = '' ) {
if ( is_array( $id_array ) ) {
if ( isset( $id_array[$object_type] ) ) {
if ( is_array( $id_array[$object_type] ) ) {
// array( 'user' => array( 123, 'postname' ) )
return $this->does_id_array_match_object( $id_array[$object_type], $object_type, $object_id, $object_slug );
} else {
// array( 'post' => 123 )
return $this->does_id_match_object( $id_array[$object_type], $object_id, $object_slug );
}
} else {
// array( 123, 456, 'postname' )
$match = false;
foreach ( $id_array as $id ) {
if ( $this->does_id_match_object( $id, $object_id, $object_slug ) ) {
$match = true;
break;
}
}
return $match;
}
} else {
// 123 || 'postname' || 'username' || 'comment-name'(?)
return $this->does_id_match_object( $id_array, $object_id, $object_slug );
}
}
function does_id_match_object( $id, $object_id, $object_slug = '' ) {
if ( is_int( $id ) ) {
// 123
return $id == $object_id;
} elseif ( is_string( $id ) ) {
// 'postname' || 'username' || 'comment-name' ??
return $id == $object_slug;
}
return false;
}
function is_restricted_field( $field_slug, $object_type ) {
// TODO: Build this out
$post_restricted = array( 'post_title', 'post_author' );
$page_restricted = array( );
$user_restricted = array( );
switch ( $object_type ) {
case 'user':
return in_array( $field_slug, $user_restricted );
case 'page':
return in_array( $field_slug, $page_restricted ) || in_array( $field_slug, $post_restricted );
case 'post':
default:
return in_array( $field_slug, $post_restricted );
}
return false;
}
function is_restricted_group( $group_slug, $object_type ) {
// TODO: Build this out
// Built-in metaboxes: title, custom-fields, revisions, author, etc.
return false;
}
function _get_object_type_context() {
global $current_screen, $pagenow;
$object_type = '';
if ( $pagenow == 'profile.php' || $pagenow == 'user-edit.php' || $pagenow == 'users.php' ) {
return 'user';
}
if ( isset( $current_screen->post_type ) ) {
$object_type = $current_screen->post_type;
} elseif ( isset( $current_screen->base ) ) {
foreach ( $this->_builtin_object_types as $builtin_type ) {
if ( strpos( $current_screen->base, $builtin_type ) !== false ) {
$object_type = $builtin_type;
break;
}
}
}
return $object_type;
}
function _get_value_callback( $field, $object_type ) {
$callback = isset( $field->value_callback ) ? $field->value_callback : '';
if ( ! ( $callback && is_callable( $callback ) ) )
$callback = '';
return apply_filters( 'custom_metadata_manager_get_value_callback', $callback, $field, $object_type );
}
function _get_save_callback( $field, $object_type ) {
$callback = isset( $field->save_callback ) ? $field->save_callback : '';
if ( ! ( $callback && is_callable( $callback ) ) )
$callback = '';
return apply_filters( 'custom_metadata_manager_get_save_callback', $callback, $field, $object_type );
}
function get_sanitize_callback( $field, $object_type ) {
$callback = $field->sanitize_callback;