-
Notifications
You must be signed in to change notification settings - Fork 0
/
custom-fields.php
1663 lines (1295 loc) · 40.7 KB
/
custom-fields.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: ClassicPress Custom Fields
* Description: Feature-as-a-plugin offering Forms & Fields for ClassicPress, initially forms for post admin edit but later for users, comments, taxonomy terms, options, etc.
*/
require( dirname( __FILE__ ) . '/functions.php' );
require( dirname( __FILE__ ) . '/functions/post.php' );
require( dirname( __FILE__ ) . '/functions/user.php' );
require( dirname( __FILE__ ) . '/functions/comment.php' );
require( dirname( __FILE__ ) . '/functions/option.php' );
/**
* Class Custom_Fields
*/
class Custom_Fields {
/**
* @var string
*/
static $prefix = false;
/**
* @var array
*/
private static $_form_args = array();
/**
* @var array
*/
private static $_field_args = array();
/**
* @var array
*/
private static $_object_type_fields = array();
/**
* @var array
*/
private static $_object_type_forms = array();
/**
* @var array
*/
private static $_element_attributes = array();
/**
* @var array
*/
private static $_views = array();
/**
* @var WP_Annotated_Property[]
*/
private static $_class_annotations;
/**
* @var WP_Registry[]
*/
private static $_registries = array(
'storage_types' => null,
'field_types' => null,
'field_feature_types' => null,
);
/**
* @var array
*/
private static $_autoload_classes = array(
'Custom_Fields_Base' => 'base/class-custom-field-base.php',
'WP_Object_Type' => 'core/class-object-type.php',
'WP_Html_Element' => 'core/class-html-element.php',
'WP_Registry' => 'core/class-registry.php',
'WP_Annotated_Property' => 'core/class-annotated-property.php',
'WP_Storage_Base' => 'base/class-storage-base.php',
'WP_Field_Base' => 'base/class-field-base.php',
'WP_Field_Feature_Base' => 'base/class-field-feature-base.php',
'WP_View_Base' => 'base/class-view-base.php',
'WP_Form_View_Base' => 'base/class-form-view-base.php',
'WP_Field_View_Base' => 'base/class-field-view-base.php',
'WP_Core_Storage' => 'storage/class-core-storage.php',
'WP_Meta_Storage' => 'storage/class-meta-storage.php',
'WP_Option_Storage' => 'storage/class-option-storage.php',
'WP_Memory_Storage' => 'storage/class-memory-storage.php',
'WP_Form' => 'forms/class-form.php',
'WP_Text_Field' => 'fields/class-text-field.php',
'WP_Textarea_Field' => 'fields/class-textarea-field.php',
'WP_Url_Field' => 'fields/class-url-field.php',
'WP_Date_Field' => 'fields/class-date-field.php',
'WP_Hidden_Field' => 'fields/class-hidden-field.php',
'WP_Field_Input_Feature' => 'features/class-field-input-feature.php',
'WP_Field_Label_Feature' => 'features/class-field-label-feature.php',
'WP_Field_Help_Feature' => 'features/class-field-help-feature.php',
'WP_Field_Message_Feature' => 'features/class-field-message-feature.php',
'WP_Field_Infobox_Feature' => 'features/class-field-infobox-feature.php',
'WP_Form_View' => 'views/class-form-view.php',
'WP_Text_Field_View' => 'views/class-text-field-view.php',
'WP_Textarea_Field_View' => 'views/class-textarea-field-view.php',
'WP_Select_Field_View' => 'views/class-select-view.php',
'WP_Hidden_Field_View' => 'views/class-hidden-field-view.php',
);
/**
*
*/
static function on_load() {
spl_autoload_register( array( __CLASS__, '_autoloader' ) );
self::initialize_registries();
self::register_default_annotations( 'WP_Html_Element', array(
'html_tag' => 'div'
) );
/*
* Register field classes
*/
//self::$_field_type_registry = new WP_Registry();
self::register_field_type( 'text', 'WP_Text_Field' );
self::register_field_type( 'textarea', 'WP_Textarea_Field' );
self::register_field_type( 'url', 'WP_Url_Field' );
self::register_field_type( 'date', 'WP_Date_Field' );
self::register_field_type( 'hidden', 'WP_Hidden_Field' );
self::register_field_view( 'text', 'WP_Text_Field_View' );
self::register_field_view( 'textarea', 'WP_Textarea_Field_View' );
self::register_field_view( 'select', 'WP_Select_Field_View' );
self::register_field_view( 'hidden', 'WP_Hidden_Field_View' );
self::register_feature_type( 'input', 'WP_Field_Input_Feature' );
self::register_feature_type( 'label', 'WP_Field_Label_Feature' );
self::register_feature_type( 'message', 'WP_Field_Message_Feature' );
self::register_feature_type( 'help', 'WP_Field_Help_Feature' );
self::register_feature_type( 'infobox', 'WP_Field_Infobox_Feature' );
/*
* Register "storage" classes
*/
self::register_storage_type( 'meta', 'WP_Meta_Storage' );
self::register_storage_type( 'core', 'WP_Core_Storage' );
self::register_storage_type( 'option', 'WP_Option_Storage' );
self::register_storage_type( 'taxonomy', 'WP_Taxonomy_Storage' );
self::register_storage_type( 'memory', 'WP_Memory_Storage' );
// /**
// * Hook a different hook differently based on how the page is loaded to initialize the fields.
// */
// if ( defined( 'DOING_AJAX' ) ) {
// add_action( 'admin_init', array( __CLASS__, '_wp_loaded' ) );
// } else if ( is_admin() ) {
// add_action( 'admin_menu', array( __CLASS__, '_wp_loaded' ) );
// } else {
// add_action( 'wp_loaded', array( __CLASS__, '_wp_loaded' ) );
// }
add_action( 'registered_post_type', array( __CLASS__, '_registered_post_type' ), 10, 2 );
if ( is_admin() ) {
add_action( 'admin_init', array( __CLASS__, '_admin_init' ) );
}
}
/**
*
* @param string $class_name
* @param array $default_values
*/
static function register_default_annotations( $class_name, $default_values ) {
WP_Annotated_Property::register_default_annotations( $class_name, $default_values );
}
/**
* @param string $type_name - Name of type
* @param string|array $type_def - Classname, or array of $args
*
* @return bool Whether the object type $type_name was registered
*/
static function register_field_type( $type_name, $type_def = array() ) {
if ( ! isset( self::$_registries['field_types']->$type_name ) ) {
self::$_registries['field_types']->$type_name = $type_def;
return true;
}
return false;
}
/**
* Register a class or array of View Type args.
*
* @example
*
* Custom_Fields::register_field_view( 'text', 'WP_Text_Field_View' );
* Custom_Fields::register_field_view( 'hidden', 'WP_Hidden_Field_View' );
*
* @param string $field_view
* @param string|array $field_view_args
*/
static function register_field_view( $field_view, $field_view_args ) {
self::register_view( 'field_views', $field_view, $field_view_args );
}
/**
* Register a class to be used as a view for the current class.
*
* @example
*
* Custom_Fields::register_view( 'field', 'text', 'WP_Text_Field_View' );
* Custom_Fields::register_view( 'field', 'hidden', 'WP_Hidden_Field_View' );
*
* @param string $view_group Grouping of Views
* @param string $view_type The name of the view that is unique for this class.
* @param string $class_name The class name for the View object.
*/
static function register_view( $view_group, $view_type, $class_name ) {
if ( ! self::view_exists( $view_type, $view_group ) ) {
self::$_views[ $view_group ][ $view_type ] = $class_name;
}
}
/**
* Does the named field view exist
*
* @param string $view_type The name of the view that is unique for this class.
* @param string $view_group Grouping of Views
*
* @return bool
*/
static function view_exists( $view_group, $view_type ) {
return isset( self::$_views[ $view_group ][ $view_type ] );
}
/**
* @param string $feature_type Name of Feature
* @param string $feature_class Classname
*/
static function register_feature_type( $feature_type, $feature_class ) {
self::$_registries['field_feature_types']->register_entry( $feature_type, $feature_class );
}
/**
* @param string $storage_type_name - Name of storage
* @param bool|string $storage_type_class - Classname
*/
static function register_storage_type( $storage_type_name, $storage_type_class = false ) {
self::$_registries['storage_types']->register_entry( $storage_type_name, $storage_type_class );
}
/**
* @param string $class_name
* @param string $class_filepath
*
* @return bool Return true if it was registered, false if not.
*/
static function register_autoload_class( $class_name, $class_filepath ) {
if ( ! isset( self::$_autoload_classes[ $class_name ] ) ) {
self::$_autoload_classes[ $class_name ] = $class_filepath;
return true;
}
return false;
}
/**
* @param string $class_name
*/
static function _autoloader( $class_name ) {
if ( isset( self::$_autoload_classes[ $class_name ] ) ) {
$filepath = self::$_autoload_classes[ $class_name ];
/**
* @todo This needs to be made to work for Windows...
*/
if ( '/' == $filepath[0] ) {
require_once( $filepath );
} else {
require_once( dirname( __FILE__ ) . "/{$filepath}" );
}
}
}
/**
*
*/
static function _admin_init() {
if ( Custom_Fields::is_post_edit_screen() ) {
add_action( 'edit_form_top', array( __CLASS__, '_edit_post_form' ) );
add_action( 'edit_form_after_title', array( __CLASS__, '_edit_post_form' ) );
add_action( 'edit_form_after_editor', array( __CLASS__, '_edit_post_form' ) );
add_action( 'edit_form_advanced', array( __CLASS__, '_edit_post_form' ) );
// add_action( 'add_meta_boxes', array( __CLASS__, 'add_meta_boxes' ) );
add_action( 'save_post_' . self::get_current_screen()->post_type, array( __CLASS__, '_save_post' ), 10, 3 );
// Add global styles for custom fields api.
add_action( 'admin_enqueue_scripts', array( __CLASS__, '_enqueue_admin_styles' ) );
}
}
/**
* @return bool
*
* @todo For Core dev review. Better way?
*/
static function is_post_edit_screen() {
global $pagenow;
return 'post.php' == $pagenow || 'post-new.php' == $pagenow;
}
/**
* Grabs the current or a new WP_screen object.
*
* Tries to get the current one but if it's not available then it hacks it's way to recreate one
* because WordPress does not consistently set it, and it's not our place to change it's state.
* We just want what we want.
*
* @return WP_Screen
*
* @todo For Core dev review. Better way?
*/
static function get_current_screen() {
$screen = get_current_screen();
if ( empty( $screen ) ) {
global $hook_suffix, $page_hook, $plugin_page, $pagenow, $current_screen;
if ( empty( $hook_suffix ) ) {
$save_hook_suffix = $hook_suffix;
$save_current_screen = $current_screen;
if ( isset( $page_hook ) ) {
$hook_suffix = $page_hook;
} else if ( isset( $plugin_page ) ) {
$hook_suffix = $plugin_page;
} else if ( isset( $pagenow ) ) {
$hook_suffix = $pagenow;
}
set_current_screen();
$screen = get_current_screen();
$hook_suffix = $save_hook_suffix;
$current_screen = $save_current_screen;
}
}
return $screen;
}
/**
* Load css required for the custom fields api.
*
*/
static function _enqueue_admin_styles( $hook ) {
wp_enqueue_style( 'custom-fields', plugin_dir_url( __FILE__ ) . 'css/custom-fields.css', array() );
}
/**
* @param string|WP_Object_Type $object_type
* @param bool|array $form_names
*
* @return array
*/
static function get_forms( $object_type, $form_names = false ) {
$forms = array();
if ( isset( self::$_object_type_forms[ $object_type ] ) ) {
$forms = self::$_object_type_forms[ $object_type ];
}
if ( $form_names ) {
if ( is_array( $form_names ) ) {
$form_names = array_flip( $form_names );
} else {
$form_names = array( $form_names => 0 );
}
$forms = array_intersect_key( $forms, $form_names );
}
foreach ( $forms as $form_name => $form_args ) {
$forms[ $form_name ] = self::make_form( $form_name, $object_type, $form_args );
}
return $forms;
}
/**
* @param string $form_name
* @param string|WP_Object_Type $object_type
* @param array $form_args
*
* @return WP_Form
*/
static function make_form( $form_name, $object_type, $form_args = array() ) {
return WP_Form::make_new( $form_name, $object_type, $form_args );
}
/**
* @param int $post_id
* @param WP_Post $post
* @param bool $update
*/
static function _save_post( $post_id, $post, $update ) {
if ( count( $forms = self::get_forms_from_POST( $post->post_type ) ) ) {
/**
* @var WP_Form $form
*/
foreach ( $forms as $form_name => $form ) {
$form->set_object( $post );
$form->update_values();
}
}
}
/**
* @param string $post_type
*
* @return array
*/
static function get_forms_from_POST( $post_type ) {
$forms = array();
if ( ! isset( $_POST[ 'cp_custom_fields_forms' ] ) || ! is_array( $_POST[ 'cp_custom_fields_forms' ] ) ) {
$forms = array();
} else {
$forms = $_POST[ 'cp_custom_fields_forms' ];
foreach ( $forms as $form_name => $form_data ) {
$form = self::make_form( $form_name, Custom_Fields::get_post_object_type_literal( $post_type ), array( 'view' => false ) );
/**
* @var WP_Field_Base $field
*/
foreach ( $form->fields as $field_name => $field ) {
if ( isset( $form_data[ $field_name ] ) ) {
$field->set_value( $form_data[ $field_name ] );
}
}
$forms[ $form_name ] = $form;
}
}
return $forms;
}
/**
* @param string $post_type
* @param array $args
*/
static function _registered_post_type( $post_type, $args ) {
global $wp_post_types;
if ( empty( $wp_post_types[ $post_type ] ) ) {
return;
}
$wp_post_types[ $post_type ]->default_form = ! empty( $args->default_form ) ? $args->default_form : 'after_title';
}
/**
* Hook handler for 'edit_form_top', 'edit_form_after_title'. 'edit_form_after_editor' and 'edit_form_advanced'.
*
* Displayed the post_type's default form based on the value of post_type_object->default_form that can be set
* as an argument to register_post_type. Valid values for default form include:
*
* 'top', 'after_title', 'after_editor', 'advanced', or 'custom_fields'
*
* @todo Explain how to handle custom metaboxes once we figure out how we'll handle them.
*
* @param WP_Post $post
*
* @internal
*
*/
static function _edit_post_form( $post ) {
$post_type = $post->post_type;
$object_type = Custom_Fields::get_post_object_type_literal( $post_type );
$current_form = preg_replace( '#^edit_form_(.*)$#', '$1', current_action() );
if ( $current_form == get_post_type_object( $post_type )->default_form ) {
if ( ! self::form_registered( $current_form, $object_type ) ) {
self::register_form( $current_form, $object_type );
}
$form = self::get_form( $current_form, $object_type );
$form->set_storage_object( $post );
$form->the_form();
}
}
/**
* @param string $form_name
* @param string|WP_Object_Type $object_type
*
* @return bool
*/
static function form_registered( $form_name, $object_type ) {
return false !== self::get_form_index( $form_name, $object_type );
}
/**
* Retrieve a form
*
* @param string $form_name
* @param string|WP_Object_Type $object_type
*
* @return int
*/
static function get_form_index( $form_name, $object_type ) {
return isset( self::$_object_type_forms[ $object_type ][ $form_name ] )
? self::$_object_type_forms[ $object_type ][ $form_name ]
: false;
}
/**
* @param string $form_name
* @param string|WP_Object_Type $object_type
* @param array $form_args
*
* @return int Form Index
*/
static function register_form( $form_name, $object_type, $form_args = array() ) {
$form_args['form_name'] = $form_name;
$form_args['object_type'] = $object_type;
$form_args['form_index'] = count( self::$_form_args );
self::$_object_type_forms[ $object_type ][ $form_name ] = $form_args['form_index'];
self::$_form_args[] = $form_args;
return $form_args['form_index'];
}
/**
* Retrieve a form
*
* @param string $form_name
* @param string|WP_Object_Type $object_type
* @param array $form_args
*
* @return WP_Form
*/
static function get_form( $form_name, $object_type, $form_args = array() ) {
$form_index = self::get_form_index( $form_name, $object_type );
$form_args = wp_parse_args( $form_args, self::get_form_args( $form_index ) );
$form = self::make_form( $form_name, $object_type, $form_args );
return $form;
}
/**
* @param int $form_index
*
* @return bool|array
*/
static function get_form_args( $form_index ) {
return isset( self::$_form_args[ $form_index ] ) ? self::$_form_args[ $form_index ] : false;
}
/**
* @param string|WP_Object_Type $object_type
*
* @return array
*/
static function get_field_names( $object_type ) {
$object_type = (string) $object_type;
return isset( self::$_object_type_fields[ $object_type ] )
? array_keys( self::$_object_type_fields[ $object_type ] )
: array();
}
/**
* Retrieve a field
*
* @param string $field_name
* @param string|WP_Object_Type $object_type
* @param array $field_args
*
* @return WP_Field_Base
*/
static function get_field( $field_name, $object_type, $field_args = array() ) {
$field_index = self::get_field_index( $field_name, $object_type );
$field_args = wp_parse_args( $field_args, self::get_field_args( $field_index ) );
$field = self::make_field( $field_name, $object_type, $field_args );
return $field;
}
//
// /**
// * @param string $tag_name
// * @param array $attributes
// * @param mixed $value
// *
// * @return WP_Html_Element
// */
// static function get_element_html( $tag_name, $attributes, $value ) {
//
// $html_element = self::get_html_element( $tag_name, $attributes, $value, true );
//
// return $html_element->get_element_html();
//
// }
/**
* Retrieve a field
*
* @param string $field_name
* @param string|WP_Object_Type $object_type
*
* @return int
*/
static function get_field_index( $field_name, $object_type ) {
$object_type = (string) $object_type;
return isset( self::$_object_type_fields[ $object_type ][ $field_name ] ) ? self::$_object_type_fields[ $object_type ][ $field_name ] : false;
}
/**
* @param int $field_index
*
* @return bool|array
*/
static function get_field_args( $field_index ) {
return isset( self::$_field_args[ $field_index ] ) ? self::$_field_args[ $field_index ] : false;
}
/**
* Make a New Field object
*
* @param string $field_name
* @param string|WP_Object_Type $object_type
* @param array $field_args
*
* @return WP_Field_Base
*
*/
static function make_field( $field_name, $object_type, $field_args = array() ) {
return WP_Field_Base::make_new( $field_name, $object_type, $field_args );
}
/**
* @param string $field_name
* @param string|WP_Object_Type $object_type
* @param array $field_args
*
* @return int Field Index
*/
static function register_field( $field_name, $object_type, $field_args = array() ) {
$field_args['field_name'] = $field_name;
$field_args['object_type'] = $object_type;
$field_args['field_index'] = count( self::$_field_args );
self::$_object_type_fields[ $object_type ][ $field_name ] = $field_args['field_index'];
self::$_field_args[] = $field_args;
return $field_args['field_index'];
}
/**
* @param string $field_type
*
* @return string|array|object
*/
static function get_field_type( $field_type ) {
return self::$_registries['field_types']->$field_type;
}
/**
*
*/
/**
* Retrieve the $args for a named Field Type.
*
* Could be either an array of $args to create a field with make_new(), or a class name to instantiate it.
*
* @param string $field_type The name of the Field
*
* @return array
*/
static function get_field_type_args( $field_type ) {
$field_type_args = self::$_registries['field_types']->$field_type;
return $field_type_args;
}
/**
* @param string $tag_name
* @param array $attributes
* @param null ,mixed $value
* @param bool $reuse
*
* @return WP_Html_Element
*/
static function get_html_element( $tag_name, $attributes = array(), $value = null, $reuse = false ) {
if ( ! $reuse ) {
$element = new WP_Html_Element( $tag_name, $attributes, $value );
} else {
/**
* @var WP_Html_Element $reusable_element
*/
static $reusable_element = false;
if ( ! $reusable_element ) {
$reusable_element = new WP_Html_Element( $tag_name, $attributes, $value );
} else {
$reusable_element->assign( $tag_name, $attributes, $value );
}
$element = $reusable_element;
}
return $element;
}
/**
* Retrieve the class name for a named View.
*
* @param string $view_group Grouping of Views
* @param string $view_type The name of the view that is unique for this class.
*
* @return string
*/
static function get_view_type_args( $view_group, $view_type ) {
return self::view_exists( $view_group, $view_type ) ? self::$_views[ $view_group ][ $view_type ] : false;
}
/*********************************************/
/*** Field Feature Type Registry Methods ***/
/*********************************************/
/**
* Retrieve the class name for a named field view.
*
* @param string $field_view_type The name of the field_view that is unique for this class.
*
* @return string
*/
static function get_field_view_type_args( $field_view_type ) {
return self::field_view_exists( $field_view_type )
? self::$_views['field_views'][ $field_view_type ]
: false;
}
/**
* Does the named field view exist?
*
* @param string $view_type The name of the view that is unique for this class.
*
* @return bool
*/
static function field_view_exists( $view_type ) {
return self::view_exists( 'field_views', $view_type );
}
/**
* Does the named feature type exist?
*
* @param string $feature_type_name The name of the view that is unique for this class.
*
* @return bool
*/
static function feature_type_exists( $feature_type_name ) {
return self::$_registries['field_feature_types']->entry_exists( $feature_type_name );
}
/*********************************************/
/*** Field Storage Type Registry Methods ***/
/*********************************************/
/**
* @param string $storage_type
*
* @return string
*/
static function get_storage_type_class( $storage_type ) {
return self::$_registries['storage_types']->get_entry( $storage_type );
}
/**
* Does the named storage type exist?
*
* @param string $storage_type_name The name of the view that is unique for this class.
*
* @return bool
*/
static function storage_type_exists( $storage_type_name ) {
return self::$_registries['storage_types']->entry_exists( $storage_type_name );
}
/**
* @param string $registry_type - Name of Registry
* @param string $item_name - Name of item in registry
* @param null|mixed $item_value - Value of item in registry
*/
static function register_registry_item( $registry_type, $item_name, $item_value = null ) {
self::$_registries[ $registry_type ]->register_entry( $item_name, $item_value );
}
/*********************************************/
/*** Generic Registry Item Methods ***/
/*********************************************/
/**
* Initialize the generic registries.
*
*/
static function initialize_registries() {
foreach ( array_keys( self::$_registries ) as $registry_type ) {
self::register_registry( $registry_type );
}
}
/**
* Registers a new type of Registry.
*
* @param string $registry_type - Name of Registry
*/
static function register_registry( $registry_type ) {
self::$_registries[ $registry_type ] = new WP_Registry( $registry_type );
}
/**
* @param string $registry_type - Name of Registry
* @param string $item_name - Name of item in registry
*
* @return null|mixed
*/
static function get_registry_item( $registry_type, $item_name ) {
return self::$_registries[ $registry_type ]->get_entry( $item_name );
}
/**
* Does the named registry item exist?
*
* @param string $registry_type - Name of Registry
* @param string $item_name - Name of item in registry
*
* @return bool
*/
static function registry_item_exists( $registry_type, $item_name ) {
return self::$_registries[ $registry_type ]->entry_exists( $item_name );
}
/**
* Does the named registry exist?
*
* @param string $registry_type - Name of Registry
*
* @return bool
*/
static function registry_exists( $registry_type ) {
return isset( self::$_registries[ $registry_type ] );
}
/**
* Collect $args from list of keys and values into a tree of keys and values.
*
* Look for $args based on their prefixes (i.e. 'view:').
* If found capture the non-prefixed key and value into $collected_args for return.
* (Stripping the prefix allows for nested values, i.e. 'view:features[label]:wrapper:class')
*
* @note ONLY collect the first level, so $args['label:html:class'] would become $args['label']['html:class'].
* Subnames will get split later thanks to calls that drill down recursively.
*
* @param array $prefixed_args
* @param array $prefixes
*
* @return array
*/
static function collect_args( $prefixed_args, $prefixes ) {
$collected_args = array();
$arg_count = count( $prefixed_args ) - 1;
foreach ( array_keys( $prefixed_args ) as $key ) {
/*
* Move the complex keys (ones with colons ) to after the simple keys
*/
if ( false !== strpos( $key, ':' ) ) {
$value = $prefixed_args[ $key ];
unset( $prefixed_args[ $key ] );
$prefixed_args[ $key ] = $value;
}
}
foreach ( $prefixed_args as $arg_name => $arg_value ) {
if ( false === strpos( $arg_name, ':' ) ) {
$collected_args[ $arg_name ] = $arg_value;
} else {
$index = false;
list( $new_name, $sub_name ) = preg_split( '#:#', $arg_name, 2 );
if ( preg_match( '#^(.+)\[([^]]+)\]$#', $new_name, $matches ) ) {
list( $null, $new_name, $index ) = $matches;
if ( ! isset( $collected_args[ $new_name ] ) || ! is_array( $collected_args[ $new_name ] ) ) {
$collected_args[ $new_name ] = array();
}
}
if ( isset( $prefixes[ $new_name ] ) ) {
$collected_args['_collected_args'][ $arg_name ] = $arg_value;
if ( false === $index ) {
$collected_args[ $new_name ][ $sub_name ] = $arg_value;
} else {
$collected_args[ $new_name ][ $index ][ $sub_name ] = $arg_value;
}
unset( $collected_args[ $arg_name ] );
}
}
}