forked from humanmade/Custom-Meta-Boxes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
classes.fields.php
1675 lines (1174 loc) · 41.1 KB
/
classes.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
/**
* Abstract class for all fields.
* Subclasses need only override html()
*
* @abstract
*/
abstract class CMB_Field {
public $value;
public $field_index = 0;
public function __construct( $name, $title, array $values, $args = array() ) {
$this->id = $name;
$this->name = $name . '[]';
$this->title = $title;
$this->args = wp_parse_args( $args, $this->get_default_args() );
// Deprecated argument: 'std'
if ( ! empty( $this->args['std'] ) && empty( $this->args['default'] ) ) {
$this->args['default'] = $this->args['std'];
_deprecated_argument( 'CMB_Field', '0.9', "field argument 'std' is deprecated, use 'default' instead" );
}
if ( ! empty( $this->args['options'] ) && is_array( reset( $this->args['options'] ) ) ) {
$re_format = array();
foreach ( $this->args['options'] as $option ) {
$re_format[$option['value']] = $option['name'];
}
$this->args['options'] = $re_format;
}
// If the field has a custom value populator callback
if ( ! empty( $args['values_callback'] ) )
$this->values = call_user_func( $args['values_callback'], get_the_id() );
else
$this->values = $values;
$this->value = reset( $this->values );
}
/**
* Get the default args for the abstract field.
* These args are available to all fields.
*
* @return array $args
*/
public function get_default_args() {
return apply_filters(
'cmb_field_default_args',
array(
'desc' => '',
'repeatable' => false,
'sortable' => false,
'repeatable_max' => null,
'show_label' => false,
'readonly' => false,
'disabled' => false,
'default' => '',
'cols' => '12',
'style' => '',
'class' => '',
'data_delegate' => null,
'save_callback' => null,
'string-repeat-field' => __( 'Add New', 'cmb' ),
'string-delete-field' => __( 'Remove Field', 'cmb' ),
),
get_class( $this )
);
}
/**
* Enqueue all scripts required by the field.
*
* @uses wp_enqueue_script()
*/
public function enqueue_scripts() {
if ( isset( $this->args['sortable'] ) && $this->args['sortable'] )
wp_enqueue_script( 'jquery-ui-sortable' );
}
/**
* Enqueue all styles required by the field.
*
* @uses wp_enqueue_style()
*/
public function enqueue_styles() {}
/**
* Output the field input ID attribute.
*
* If multiple inputs are required for a single field,
* use the append parameter to add unique identifier.
*
* @param string $append
* @return null
*/
public function id_attr( $append = null ) {
printf( 'id="%s"', esc_attr( $this->get_the_id_attr( $append ) ) );
}
/**
* Output the for attribute for the field.
*
*
*
* If multiple inputs are required for a single field,
* use the append parameter to add unique identifier.
*
* @param string $append
* @return null
*/
public function get_the_id_attr( $append = null ) {
$id = $this->id;
if ( isset( $this->parent ) ) {
$parent_id = preg_replace( '/cmb\-field\-(\d+|x)/', 'cmb-group-$1', $this->parent->get_the_id_attr() );
$id = $parent_id . '[' . $id . ']';
}
$id .= '-cmb-field-' . $this->field_index;
if ( ! is_null( $append ) )
$id .= '-' . $append;
$id = str_replace( array( '[', ']', '--' ), '-', $id );
return $id;
}
/**
* Return the field input ID attribute value.
*
* If multiple inputs are required for a single field,
* use the append parameter to add unique identifier.
*
* @param string $append
* @return string id attribute value.
*/
public function for_attr( $append = null ) {
printf( 'for="%s"', esc_attr( $this->get_the_id_attr( $append ) ) );
}
public function name_attr( $append = null ) {
printf( 'name="%s"', esc_attr( $this->get_the_name_attr( $append ) ) );
}
public function get_the_name_attr( $append = null ) {
$name = str_replace( '[]', '', $this->name );
if ( isset( $this->parent ) ) {
$parent_name = preg_replace( '/cmb\-field\-(\d+|x)/', 'cmb-group-$1', $this->parent->get_the_name_attr() );
$name = $parent_name . '[' . $name . ']';
}
$name .= "[cmb-field-$this->field_index]";
if ( ! is_null( $append ) )
$name .= $append;
return $name;
}
public function class_attr( $classes = '' ) {
if ( $classes = implode( ' ', array_map( 'sanitize_html_class', array_filter( array_unique( explode( ' ', $classes . ' ' . $this->args['class'] ) ) ) ) ) ) { ?>
class="<?php echo esc_attr( $classes ); ?>"
<?php }
}
/**
* Get JS Safe ID.
*
* For use as a unique field identifier in javascript.
*/
public function get_js_id() {
return str_replace( array( '-', '[', ']', '--' ),'_', $this->get_the_id_attr() ); // JS friendly ID
}
public function boolean_attr( $attrs = array() ) {
if ( $this->args['readonly'] )
$attrs[] = 'readonly';
if ( $this->args['disabled'] )
$attrs[] = 'disabled';
$attrs = array_filter( array_unique( $attrs ) );
foreach ( $attrs as $attr )
echo esc_html( $attr ) . '="' . esc_attr( $attr ) . '"';
}
/**
* Check if this field has a data delegate set
*
* @return boolean
*/
public function has_data_delegate() {
return (bool) $this->args['data_delegate'];
}
/**
* Get the array of data from the data delegate
*
* @return array mixed
*/
protected function get_delegate_data() {
if ( $this->args['data_delegate'] )
return call_user_func_array( $this->args['data_delegate'], array( $this ) );
return array();
}
public function get_value() {
return ( $this->value || $this->value === '0' ) ? $this->value : $this->args['default'];
}
public function &get_values() {
return $this->values;
}
public function set_values( array $values ) {
$this->values = $values;
unset( $this->value );
}
public function parse_save_values() {}
public function parse_save_value() {}
/**
* @todo this surely only works for posts
* @todo why do values need to be passed in, they can already be passed in on construct
*/
public function save( $post_id, $values ) {
// Don't save readonly values.
if ( $this->args['readonly'] )
return;
$this->values = $values;
$this->parse_save_values();
// Allow override from args
if ( ! empty( $this->args['save_callback'] ) ) {
call_user_func( $this->args['save_callback'], $this->values, $post_id );
return;
}
// If we are not on a post edit screen
if ( ! $post_id )
return;
delete_post_meta( $post_id, $this->id );
foreach( $this->values as $v ) {
$this->value = $v;
$this->parse_save_value();
if ( $this->value || $this->value === '0' )
add_post_meta( $post_id, $this->id, $this->value );
}
}
public function title() {
if ( $this->title ) { ?>
<div class="field-title">
<label <?php $this->for_attr(); ?>>
<?php echo esc_html( $this->title ); ?>
</label>
</div>
<?php }
}
public function description() {
if ( ! empty( $this->args['desc'] ) ) { ?>
<div class="cmb_metabox_description">
<?php echo wp_kses_post( $this->args['desc'] ); ?>
</div>
<?php }
}
public function display() {
// If there are no values and it's not repeateble, we want to do one with empty string
if ( ! $this->get_values() && ! $this->args['repeatable'] )
$values = array( '' );
else
$values = $this->get_values();
$this->title();
$this->description();
$i = 0;
foreach ( $values as $key => $value ) {
$this->field_index = $i;
$this->value = $value; ?>
<div class="field-item" data-class="<?php echo esc_attr( get_class( $this ) ); ?>" style="position: relative; <?php echo esc_attr( $this->args['style'] ); ?>">
<?php if ( $this->args['repeatable'] ) : ?>
<button class="cmb-delete-field" title="<?php echo esc_attr( $this->args['string-delete-field'] ); ?>">
<span class="cmb-delete-field-icon">×</span>
</button>
<?php endif; ?>
<?php $this->html(); ?>
</div>
<?php
$i++;
}
// Insert a hidden one if it's repeatable
if ( $this->args['repeatable'] ) {
$this->field_index = 'x'; // x used to distinguish hidden fields.
$this->value = ''; ?>
<div class="field-item hidden" data-class="<?php echo esc_attr( get_class( $this ) ); ?>" style="position: relative; <?php echo esc_attr( $this->args['style'] ); ?>">
<?php if ( $this->args['repeatable'] ) : ?>
<button class="cmb-delete-field" title="<?php echo esc_attr( $this->args['string-delete-field'] ); ?>">
<span class="cmb-delete-field-icon">×</span>
<?php echo esc_html( $this->args['string-delete-field'] ); ?>
</button>
<?php endif; ?>
<?php $this->html(); ?>
</div>
<button class="button repeat-field"><?php echo esc_html( $this->args['string-repeat-field'] ); ?></button>
<?php }
}
}
/**
* Standard text field.
*
* @extends CMB_Field
*/
class CMB_Text_Field extends CMB_Field {
public function html() { ?>
<input type="text" <?php $this->id_attr(); ?> <?php $this->boolean_attr(); ?> <?php $this->class_attr(); ?> <?php $this->name_attr(); ?> value="<?php echo esc_attr( $this->get_value() ); ?>" />
<?php }
}
class CMB_Text_Small_Field extends CMB_Text_Field {
public function html() {
$this->args['class'] .= ' cmb_text_small';
parent::html();
}
}
/**
* Field for image upload / file updoad.
*
* @todo ability to set image size (preview image) from caller
*/
class CMB_File_Field extends CMB_Field {
/**
* Return the default args for the File field.
*
* @return array $args
*/
public function get_default_args() {
return array_merge(
parent::get_default_args(),
array(
'library-type' => array( 'video', 'audio', 'text', 'application' )
)
);
}
function enqueue_scripts() {
global $post_ID;
$post_ID = isset($post_ID) ? (int) $post_ID : 0;
parent::enqueue_scripts();
wp_enqueue_media( array( 'post' => $post_ID ));
wp_enqueue_script( 'cmb-file-upload', trailingslashit( CMB_URL ) . 'js/file-upload.js', array( 'jquery', 'cmb-scripts' ) );
}
public function html() {
if ( $this->get_value() ) {
$src = wp_mime_type_icon( $this->get_value() );
$size = getimagesize( str_replace( site_url(), ABSPATH, $src ) );
$icon_img = '<img src="' . $src . '" ' . $size[3] . ' />';
}
$data_type = ( ! empty( $this->args['library-type'] ) ? implode( ',', $this->args['library-type'] ) : null );
?>
<div class="cmb-file-wrap" <?php echo 'data-type="' . esc_attr( $data_type ) . '"'; ?>>
<div class="cmb-file-wrap-placeholder"></div>
<button class="button cmb-file-upload <?php echo esc_attr( $this->get_value() ) ? 'hidden' : '' ?>">
<?php esc_html_e( 'Add File', 'cmb' ); ?>
</button>
<div class="cmb-file-holder type-file <?php echo $this->get_value() ? '' : 'hidden'; ?>">
<?php if ( $this->get_value() ) : ?>
<?php if ( isset( $icon_img ) ) echo $icon_img; ?>
<div class="cmb-file-name">
<strong><?php echo esc_html( basename( get_attached_file( $this->get_value() ) ) ); ?></strong>
</div>
<?php endif; ?>
</div>
<button class="cmb-remove-file button <?php echo $this->get_value() ? '' : 'hidden'; ?>">
<?php esc_html_e( 'Remove', 'cmb' ); ?>
</button>
<input type="hidden"
<?php $this->class_attr( 'cmb-file-upload-input' ); ?>
<?php $this->name_attr(); ?>
value="<?php echo esc_attr( $this->value ); ?>"
/>
</div>
<?php }
}
class CMB_Image_Field extends CMB_File_Field {
/**
* Return the default args for the Image field.
*
* @return array $args
*/
public function get_default_args() {
return array_merge(
parent::get_default_args(),
array(
'size' => 'thumbnail',
'library-type' => array( 'image' ),
'show_size' => false
)
);
}
public function html() {
if ( $this->get_value() )
$image = wp_get_attachment_image_src( $this->get_value(), $this->args['size'], true );
// Convert size arg to array of width, height, crop
$size = $this->parse_image_size( $this->args['size'] );
// Inline styles
$styles = sprintf( 'width: %1$dpx; height: %2$dpx; line-height: %2$dpx', intval( $size['width'] ), intval( $size['height'] ) );
$placeholder_styles = sprintf( 'width: %dpx; height: %dpx;', intval( $size['width'] ) - 8, intval( $size['height'] ) - 8 );
$data_type = ( ! empty( $this->args['library-type'] ) ? implode( ',', $this->args['library-type'] ) : null );
?>
<div class="cmb-file-wrap" style="<?php echo esc_attr( $styles ); ?>" data-type="<?php echo esc_attr( $data_type ); ?>">
<div class="cmb-file-wrap-placeholder" style="<?php echo esc_attr( $placeholder_styles ); ?>">
<?php if ( $this->args['show_size'] ) : ?>
<span class="dimensions">
<?php printf( '%dpx × %dpx', intval( $size['width'] ), intval( $size['height'] ) ); ?>
</span>
<?php endif; ?>
</div>
<button class="button cmb-file-upload <?php echo esc_attr( $this->get_value() ) ? 'hidden' : '' ?>" data-nonce="<?php echo wp_create_nonce( 'cmb-file-upload-nonce' ); ?>">
<?php esc_html_e( 'Add Image', 'cmb' ); ?>
</button>
<div class="cmb-file-holder type-img <?php echo $this->get_value() ? '' : 'hidden'; ?>" data-crop="<?php echo (bool) $size['crop']; ?>">
<?php if ( ! empty( $image ) ) : ?>
<img src="<?php echo esc_url( $image[0] ); ?>" width="<?php echo intval( $image[1] ); ?>" height="<?php echo intval( $image[2] ); ?>" />
<?php endif; ?>
</div>
<button class="cmb-remove-file button <?php echo $this->get_value() ? '' : 'hidden'; ?>">
<?php esc_html_e( 'Remove', 'cmb' ); ?>
</button>
<input type="hidden"
<?php $this->class_attr( 'cmb-file-upload-input' ); ?>
<?php $this->name_attr(); ?>
value="<?php echo esc_attr( $this->value ); ?>"
/>
</div>
<?php }
/**
* Parse the size argument to get pixel width, pixel height and crop information.
*
* @param string $size
* @return array width, height, crop
*/
private function parse_image_size( $size ) {
// Handle string for built-in image sizes
if ( is_string( $size ) && in_array( $size, array( 'thumbnail', 'medium', 'large' ) ) ) {
return array(
'width' => get_option( $size . '_size_w' ),
'height' => get_option( $size . '_size_h' ),
'crop' => get_option( $size . '_crop' )
);
}
// Handle string for additional image sizes
global $_wp_additional_image_sizes;
if ( is_string( $size ) && isset( $_wp_additional_image_sizes[$size] ) ) {
return array(
'width' => $_wp_additional_image_sizes[$size]['width'],
'height' => $_wp_additional_image_sizes[$size]['height'],
'crop' => $_wp_additional_image_sizes[$size]['crop']
);
}
// Handle default WP size format.
if ( is_array( $size ) && isset( $size[0] ) && isset( $size[1] ) )
$size = array( 'width' => $size[0], 'height' => $size[1] );
return wp_parse_args( $size, array(
'width' => get_option( 'thumbnail_size_w' ),
'height' => get_option( 'thumbnail_size_h' ),
'crop' => get_option( 'thumbnail_crop' )
) );
}
/**
* Ajax callback for outputing an image src based on post data.
*
* @return null
*/
static function request_image_ajax_callback() {
if ( ! ( isset( $_POST['nonce'] ) && wp_verify_nonce( $_POST['nonce'], 'cmb-file-upload-nonce' ) ) )
return;
$id = intval( $_POST['id'] );
$size = array(
intval( $_POST['width'] ),
intval( $_POST['height'] ),
'crop' => (bool) $_POST['crop']
);
$image = wp_get_attachment_image_src( $id, $size );
echo reset( $image );
die(); // this is required to return a proper result
}
}
add_action( 'wp_ajax_cmb_request_image', array( 'CMB_Image_Field', 'request_image_ajax_callback' ) );
/**
* Standard text meta box for a URL.
*
*/
class CMB_URL_Field extends CMB_Field {
public function html() { ?>
<input type="text" <?php $this->id_attr(); ?> <?php $this->boolean_attr(); ?> <?php $this->class_attr( 'cmb_text_url code' ); ?> <?php $this->name_attr(); ?> value="<?php echo esc_attr( esc_url( $this->value ) ); ?>" />
<?php }
}
/**
* Date picker box.
*
*/
class CMB_Date_Field extends CMB_Field {
public function enqueue_scripts() {
parent::enqueue_scripts();
wp_enqueue_style( 'cmb-jquery-ui', trailingslashit( CMB_URL ) . 'css/vendor/jquery-ui/jquery-ui.css', '1.10.3' );
wp_enqueue_script( 'cmb-datetime', trailingslashit( CMB_URL ) . 'js/field.datetime.js', array( 'jquery', 'jquery-ui-core', 'jquery-ui-datepicker', 'cmb-scripts' ) );
}
public function html() { ?>
<input <?php $this->id_attr(); ?> <?php $this->boolean_attr(); ?> <?php $this->class_attr( 'cmb_text_small cmb_datepicker' ); ?> type="text" <?php $this->name_attr(); ?> value="<?php echo esc_attr( $this->value ); ?>" />
<?php }
}
class CMB_Time_Field extends CMB_Field {
public function enqueue_scripts() {
parent::enqueue_scripts();
wp_enqueue_style( 'cmb-jquery-ui', trailingslashit( CMB_URL ) . 'css/vendor/jquery-ui/jquery-ui.css', '1.10.3' );
wp_enqueue_script( 'cmb-timepicker', trailingslashit( CMB_URL ) . 'js/jquery.timePicker.min.js', array( 'jquery', 'cmb-scripts' ) );
wp_enqueue_script( 'cmb-datetime', trailingslashit( CMB_URL ) . 'js/field.datetime.js', array( 'jquery', 'jquery-ui-core', 'jquery-ui-datepicker', 'cmb-scripts' ) );
}
public function html() { ?>
<input <?php $this->id_attr(); ?> <?php $this->boolean_attr(); ?> <?php $this->class_attr( 'cmb_text_small cmb_timepicker' ); ?> type="text" <?php $this->name_attr(); ?> value="<?php echo esc_attr( $this->value ); ?>"/>
<?php }
}
/**
* Date picker for date only (not time) box.
*
*/
class CMB_Date_Timestamp_Field extends CMB_Field {
public function enqueue_scripts() {
parent::enqueue_scripts();
wp_enqueue_style( 'cmb-jquery-ui', trailingslashit( CMB_URL ) . 'css/vendor/jquery-ui/jquery-ui.css', '1.10.3' );
wp_enqueue_script( 'cmb-timepicker', trailingslashit( CMB_URL ) . 'js/jquery.timePicker.min.js', array( 'jquery', 'cmb-scripts' ) );
wp_enqueue_script( 'cmb-datetime', trailingslashit( CMB_URL ) . 'js/field.datetime.js', array( 'jquery', 'jquery-ui-core', 'jquery-ui-datepicker', 'cmb-scripts' ) );
}
public function html() { ?>
<input <?php $this->id_attr(); ?> <?php $this->boolean_attr(); ?> <?php $this->class_attr( 'cmb_text_small cmb_datepicker' ); ?> type="text" <?php $this->name_attr(); ?> value="<?php echo $this->value ? esc_attr( date( 'm\/d\/Y', $this->value ) ) : '' ?>" />
<?php }
public function parse_save_values() {
foreach( $this->values as &$value )
$value = strtotime( $value );
sort( $this->values );
}
}
/**
* Date picker for date and time (seperate fields) box.
*
*/
class CMB_Datetime_Timestamp_Field extends CMB_Field {
public function enqueue_scripts() {
parent::enqueue_scripts();
wp_enqueue_style( 'cmb-jquery-ui', trailingslashit( CMB_URL ) . 'css/vendor/jquery-ui/jquery-ui.css', '1.10.3' );
wp_enqueue_script( 'cmb-timepicker', trailingslashit( CMB_URL ) . 'js/jquery.timePicker.min.js', array( 'jquery', 'cmb-scripts' ) );
wp_enqueue_script( 'cmb-datetime', trailingslashit( CMB_URL ) . 'js/field.datetime.js', array( 'jquery', 'jquery-ui-core', 'jquery-ui-datepicker', 'cmb-scripts' ) );
}
public function html() { ?>
<input <?php $this->id_attr('date'); ?> <?php $this->boolean_attr(); ?> <?php $this->class_attr( 'cmb_text_small cmb_datepicker' ); ?> type="text" <?php $this->name_attr( '[date]' ); ?> value="<?php echo $this->value ? esc_attr( date( 'm\/d\/Y', $this->value ) ) : '' ?>" />
<input <?php $this->id_attr('time'); ?> <?php $this->boolean_attr(); ?> <?php $this->class_attr( 'cmb_text_small cmb_timepicker' ); ?> type="text" <?php $this->name_attr( '[time]' ); ?> value="<?php echo $this->value ? esc_attr( date( 'h:i A', $this->value ) ) : '' ?>" />
<?php }
public function parse_save_values() {
// Convert all [date] and [time] values to a unix timestamp.
// If date is empty, assume delete. If time is empty, assume 00:00.
foreach( $this->values as $key => &$value ) {
if ( empty( $value['date'] ) )
unset( $this->values[$key] );
else
$value = strtotime( $value['date'] . ' ' . $value['time'] );
}
$this->values = array_filter( $this->values );
sort( $this->values );
parent::parse_save_values();
}
}
/**
* Standard text field.
*
* Args:
* - int "rows" - number of rows in the <textarea>
*/
class CMB_Textarea_Field extends CMB_Field {
public function html() { ?>
<textarea <?php $this->id_attr(); ?> <?php $this->boolean_attr(); ?> <?php $this->class_attr(); ?> rows="<?php echo ! empty( $this->args['rows'] ) ? esc_attr( $this->args['rows'] ) : 4; ?>" <?php $this->name_attr(); ?>><?php echo esc_html( $this->value ); ?></textarea>
<?php }
}
/**
* Code style text field.
*
* Args:
* - int "rows" - number of rows in the <textarea>
*/
class CMB_Textarea_Field_Code extends CMB_Textarea_Field {
public function html() {
$this->args['class'] .= ' code';
parent::html();
}
}
/**
* Colour picker
*
*/
class CMB_Color_Picker extends CMB_Field {
public function enqueue_scripts() {
parent::enqueue_scripts();
wp_enqueue_script( 'cmb-colorpicker', trailingslashit( CMB_URL ) . 'js/field.colorpicker.js', array( 'jquery', 'wp-color-picker', 'cmb-scripts' ) );
wp_enqueue_style( 'wp-color-picker' );
}
public function html() { ?>
<input <?php $this->id_attr(); ?> <?php $this->boolean_attr(); ?> <?php $this->class_attr( 'cmb_colorpicker cmb_text_small' ); ?> type="text" <?php $this->name_attr(); ?> value="<?php echo esc_attr( $this->get_value() ); ?>" />
<?php }
}
/**
* Standard radio field.
*
* Args:
* - bool "inline" - display the radio buttons inline
*/
class CMB_Radio_Field extends CMB_Field {
/**
* Return the default args for the Radio input field.
*
* @return array $args
*/
public function get_default_args() {
return array_merge(
parent::get_default_args(),
array(
'options' => array(),
)
);
}
public function html() {
if ( $this->has_data_delegate() )
$this->args['options'] = $this->get_delegate_data(); ?>
<?php foreach ( $this->args['options'] as $key => $value ): ?>
<input <?php $this->id_attr( 'item-' . $key ); ?> <?php $this->boolean_attr(); ?> <?php $this->class_attr(); ?> type="radio" <?php $this->name_attr(); ?> value="<?php echo esc_attr( $key ); ?>" <?php checked( $key, $this->get_value() ); ?> />
<label <?php $this->for_attr( 'item-' . $key ); ?> style="margin-right: 20px;">
<?php echo esc_html( $value ); ?>
</label>
<?php endforeach; ?>
<?php }
}
/**
* Standard checkbox field.
*
*/
class CMB_Checkbox extends CMB_Field {
public function title() {}
public function html() { ?>
<input <?php $this->id_attr(); ?> <?php $this->boolean_attr(); ?> <?php $this->class_attr(); ?> type="checkbox" <?php $this->name_attr(); ?> value="1" <?php checked( $this->get_value() ); ?> />
<label <?php $this->for_attr(); ?>><?php echo esc_html( $this->title ); ?></label>
<?php }
}
/**
* Standard title used as a splitter.
*
*/
class CMB_Title extends CMB_Field {
public function title() {
?>
<div class="field-title">
<h2 <?php $this->class_attr(); ?>>
<?php echo esc_html( $this->title ); ?>
</h2>
</div>
<?php
}
public function html() {}
}
/**
* wysiwyg field.
*
*/
class CMB_wysiwyg extends CMB_Field {
/**
* Return the default args for the WYSIWYG field.
*
* @return array $args
*/
public function get_default_args() {
return array_merge(
parent::get_default_args(),
array(
'options' => array(),
)
);
}
function enqueue_scripts() {
parent::enqueue_scripts();
wp_enqueue_script( 'cmb-wysiwyg', trailingslashit( CMB_URL ) . 'js/field-wysiwyg.js', array( 'jquery', 'cmb-scripts' ) );
}
public function html() {
$id = $this->get_the_id_attr();
$name = $this->get_the_name_attr();
$field_id = $this->get_js_id();
printf( '<div class="cmb-wysiwyg" data-id="%s" data-name="%s" data-field-id="%s">', $id, $name, $field_id );
if ( $this->is_placeholder() ) {
// For placeholder, output the markup for the editor in a JS var.
ob_start();
$this->args['options']['textarea_name'] = 'cmb-placeholder-name-' . $field_id;
wp_editor( '', 'cmb-placeholder-id-' . $field_id, $this->args['options'] );
$editor = ob_get_clean();
$editor = str_replace( array( "\n", "\r" ), "", $editor );
$editor = str_replace( array( "'" ), '"', $editor );
?>
<script>
if ( 'undefined' === typeof( cmb_wysiwyg_editors ) )
var cmb_wysiwyg_editors = {};
cmb_wysiwyg_editors.<?php echo $field_id; ?> = '<?php echo $editor; ?>';
</script>
<?php
} else {
$this->args['options']['textarea_name'] = $name;
echo wp_editor( $this->get_value(), $id, $this->args['options'] );
}
echo '</div>';
}
/**
* Check if this is a placeholder field.
* Either the field itself, or because it is part of a repeatable group.
*
* @return bool
*/
public function is_placeholder() {
if ( isset( $this->parent ) && ! is_int( $this->parent->field_index ) )
return true;
else return ! is_int( $this->field_index );
}
}
/**
* Standard select field.
*
* @supports "data_delegate"
* @args
* 'options' => array Array of options to show in the select, optionally use data_delegate instead
* 'allow_none' => bool Allow no option to be selected (will palce a "None" at the top of the select)
* 'multiple' => bool whether multiple can be selected
*/
class CMB_Select extends CMB_Field {
public function __construct() {
$args = func_get_args();
call_user_func_array( array( 'parent', '__construct' ), $args );