forked from humanmade/Custom-Meta-Boxes
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathclasses.fields.php
842 lines (616 loc) · 19.4 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
<?php
/**
* Abstract class for all fields.
* Subclasses need only override html()
*
* @abstract
*/
abstract class CMB_Field {
public $value;
/**
* used for repeatable
*
*/
static $did_saves;
/**
* used for repeatable
*
*/
static $next_values;
public function __construct( $name, $title, array $values, $args = array() ) {
$this->id = $name;
$this->name = $name . '[]';
$this->title = $title;
$this->args = wp_parse_args( $args, array(
'repeatable' => false,
'std' => '',
'default' => '',
'show_label' => false,
'taxonomy' => '',
'hide_empty' => false,
'data_delegate' => null,
'options' => array(),
'cols' => '12'
)
);
if ( ! empty( $this->args['std'] ) && empty( $this->args['default'] ) ) {
$this->args['default'] = $this->args['std'];
_deprecated_argument( 'CMB_Field', "'std' is deprecated, use 'default instead'", '0.9' );
}
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'];
}
_deprecated_argument( 'CMB_Field', "'std' is deprecated, use 'default instead'", '0.9' );
$this->args['options'] = $re_format;
}
$this->values = $values;
$this->description = ! empty( $this->args['desc'] ) ? $this->args['desc'] : '';
}
/**
* Method responsible for enqueueing any extra scripts the field needs
*
* @uses wp_enqueue_script()
*/
public function enqueue_scripts() {}
/**
* Method responsible for enqueueing any extra styles the field needs
*
* @uses wp_enqueue_style()
*/
public function enqueue_styles() {}
/**
* 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 : $this->args['default'];
}
public function parse_save_values() {
// if it's repeatable take off the last one
if ( $this->args['repeatable'] ) {
end( $this->values );
unset( $this->values[key( $this->values )] );
reset( $this->values );
}
}
public function parse_save_value() {
}
public function save( $post_id ) {
$this->parse_save_values();
delete_post_meta( $post_id, $this->id );
foreach( $this->values as $v ) {
$this->value = $v;
$this->parse_save_value();
if ( $this->value )
add_post_meta( $post_id, $this->id, $this->value );
}
}
public function title() {
echo '<strong>' . $this->args['name'] . '</strong>';
}
public function display() {
// if there are no values and it's not repeateble, we want to do one with empty string
if ( empty( $this->values ) && ! $this->args['repeatable'] )
$this->values = array( '' );
$this->title();
foreach ( $this->values as $value ) {
$this->value = $value;
echo '<div class="field-item" style="position: relative">';
if ( $this->args['repeatable'] ) : ?>
<span class="cmb_element">
<span class="ui-state-default">
<a class="delete-field ui-icon-circle-close ui-icon" style="position: absolute; top: 5px; right: -10px">X</a>
</span>
</span>
<?php endif;
$this->html();
echo '</div>';
}
// insert a hidden one if it's repeatable
if ( $this->args['repeatable'] ) {
$this->value = '';
echo '<div class="field-item hidden" style="position: relative">';
if ( $this->args['repeatable'] ) : ?>
<span class="cmb_element">
<span class="ui-state-default">
<a class="delete-field ui-icon-circle-close ui-icon" style="position: absolute; top: 5px; right: -10px">X</a>
</span>
</span>
<?php endif;
$this->html();
echo '</div>';
?>
<p>
<a href="#" class="button repeat-field">Add New</a>
</p>
<?php
}
}
}
/**
* Standard text field.
*
*/
class CMB_Text_Field extends CMB_Field {
public function html() {
?>
<p>
<input type="text" name="<?php echo $this->name ?>" value="<?php echo $this->value ?>" />
</p>
<?php
}
}
class CMB_Text_Small_Field extends CMB_Field {
public function html() {
?>
<p>
<input class="cmb_text_small" type="text" name="<?php echo $this->name ?>" value="<?php echo $this->value ?>" /> <span class="cmb_metabox_description"><?php echo $this->description ?></span>
</p>
<?php
}
}
/**
* Field for image upload / file updoad.
*
* @todo ability to set image size (preview image) from caller
*/
class CMB_File_Field extends CMB_Field {
public function html() {
$field = array(
'id' => $this->name,
'allow' => '',
'desc' => $this->description,
'html_id' => str_replace( array( '[', ']' ), '_', $this->name . rand(1,100) )
);
// value can be URL of file or id of image attachment
if ( is_numeric( $this->value ) ) {
if ( wp_get_attachment_image_src( $this->value, 'width=100&height=100' ) )
$meta = reset( wp_get_attachment_image_src( $this->value, 'width=100&height=100' ) );
else
$meta = '';
} else {
$meta = $this->value;
}
$input_type_url = "hidden";
if ( 'url' == $field['allow'] || ( is_array( $field['allow'] ) && in_array( 'url', $field['allow'] ) ) )
$input_type_url="text";
echo '<input class="cmb_upload_file" type="' . $input_type_url . '" size="45" id="', $field['html_id'], '" value="', $meta, '" />';
echo '<input class="cmb_upload_button button" type="button" value="Upload File" />';
echo '<input class="cmb_upload_file_id" type="hidden" id="', $field['html_id'], '_id" name="', $field['id'], '" value="', $this->value, '" />';
echo '<p class="cmb_metabox_description">', $field['desc'], '</p>';
echo '<div id="', $field['html_id'], '_status" class="cmb_upload_status">';
if ( $meta != '' ) {
$check_image = preg_match( '/(^.*\.jpg|jpeg|png|gif|ico*)/i', $meta );
if ( $check_image ) {
echo '<div class="img_status">';
echo '<img src="', $meta, '" alt="" />';
echo '<a href="#" class="cmb_remove_file_button" rel="', $field['html_id'], '">Remove Image</a>';
echo '</div>';
} else {
?>
<div class="img_status">
<a href="<?php echo $meta ?>" target="_blank">View File</a>
<a href="#" class="cmb_remove_file_button" rel="<?php echo $field['html_id'] ?>">Remove</a>
</div>
<?php
}
}
echo '</div>';
}
}
/**
* Standard text meta box for a URL.
*
*/
class CMB_URL_Field extends CMB_Field {
public function html() {
?>
<p>
<input class="cmb_text_url" type="text" name="<?php echo $this->name ?>" value="<?php echo esc_url( $this->value ) ?>" /> <span class="cmb_metabox_description"><?php echo $this->description ?></span>
</p>
<?php
}
}
/**
* Date picker box.
*
*/
class CMB_Date_Field extends CMB_Field {
public function html() {
?>
<p>
<input class="cmb_text_small cmb_datepicker" type="text" name="<?php echo $this->name ?>" value="<?php echo $this->value ?>" /> <span class="cmb_metabox_description"><?php echo $this->description ?></span>
</p>
<?php
}
}
class CMB_Time_Field extends CMB_Field {
public function html() {
?>
<p>
<input class="cmb_text_small cmb_timepicker" type="text" name="<?php echo $this->name ?>" value="<?php echo $this->value;?>"/> <span class="cmb_metabox_description"><?php echo $this->description ?></span>
</p>
<?php
}
}
/**
* Date picker for date only (not time) box.
*
*/
class CMB_Date_Timestamp_Field extends CMB_Field {
public function html() {
?>
<p>
<input class="cmb_text_small cmb_datepicker" type="text" name="<?php echo $this->name ?>" value="<?php echo $this->value ? date( 'm\/d\/Y', $this->value ) : '' ?>" /> <span class="cmb_metabox_description"><?php echo $this->description ?></span>
</p>
<?php
}
public function parse_save_value() {
$this->value = strtotime( $this->value );
}
}
/**
* Date picker for date and time (seperate fields) box.
*
*/
class CMB_Datetime_Timestamp_Field extends CMB_Field {
public function html() {
?>
<p>
<input class="cmb_text_small cmb_datepicker" type="text" name="<?php echo $this->id ?>[date][]" value="<?php echo $this->value ? date( 'm\/d\/Y', $this->value ) : '' ?>" />
<input class="cmb_text_small cmb_timepicker" type="text" name="<?php echo $this->id ?>[time][]" value="<?php echo $this->value ? date( 'H:i A', $this->value ) : '' ?>" /> <span class="cmb_metabox_description"><?php echo $this->description ?></span>
</p>
<?php
}
public function parse_save_values() {
$r = array();
for ( $i = 0; $i < count( $this->values['date'] ); $i++ )
if( ! empty( $this->values['date'][$i] ) )
$r[$i] = strtotime( $this->values['date'][$i] . ' ' . $this->values['time'][$i] );
sort( $r );
$r[] = '';
$this->values = $r;
parent::parse_save_values();
}
}
/**
* Standard text meta box for a URL.
*
*/
class CMB_Oembed_Field extends CMB_Field {
public function html() { ?>
<style>
.cmb_oembed img, .cmb_oembed object, .cmb_oembed video, .cmb_oembed embed, .cmb_oembed iframe { max-width: 100%; height: auto; }
</style>
<p>
<?php if ( ! $this->value ) : ?>
<?php echo '<input class="cmb_oembed code" type="text" name="', $this->name, '" id="',$this->name, '" value="" /><span class="cmb_metabox_description">', $this->description, '</span>'; ?>
<?php else : ?>
<?php echo '<div class="hidden"><input disabled class="cmb_oembed code" type="text" name="', $this->name, '" id="',$this->name, '" value="" /><span class="cmb_metabox_description">', $this->description, '</span></div>'; ?>
<div style="position: relative">
<?php if ( is_array( $this->value ) ) :?>
<span class="cmb_oembed"><?php echo $this->value['object'] ?></span>
<input type="hidden" name="<?php echo $this->name ?>" value="<?php echo esc_attr( serialize( $this->value ) ); ?>" />
<?php else : ?>
<span class="cmb_oembed"><?php echo $this->value ?></span>
<input type="hidden" name="<?php echo $this->name ?>" value="<?php echo esc_attr( $this->value ); ?>" />
<?php endif; ?>
<a href="#" class="cmb_remove_file_button" onclick="jQuery( this ).closest('div').prev().removeClass('hidden').find('input').first().removeAttr('disabled')">Remove</a>
</div>
<?php endif; ?>
</p>
<?php }
public function parse_save_value() {
$args['cmb_oembed'] = true;
if ( ! empty( $this->args['height'] ) )
$args['height'] = $this->args['height'];
if ( strpos( $this->value, 'http' ) === 0 )
$this->value = wp_oembed_get( $this->value, $args );
}
}
function cmb_oembed_thumbnail( $return, $data, $url ) {
$backtrace = debug_backtrace();
if ( $data->type == 'video' && ! empty( $backtrace[5]['args'][1]['cmb_oembed'] ) )
return '<a href=""><img src="' . $data->thumbnail_url . '" /><span class="video_embed">' . $return . '</span></a>';
return $return;
}
add_filter( 'oembed_dataparse', 'cmb_oembed_thumbnail', 10, 3 );
/**
* Standard text field.
*
* Args:
* - int "rows" - number of rows in the <textarea>
*/
class CMB_Textarea_Field extends CMB_Field {
public function html() {
?>
<p>
<textarea rows="<?php echo !empty( $this->args['rows'] ) ? $this->args['rows'] : 4 ?>" name="<?php echo $this->name ?>"><?php echo $this->value ?></textarea>
</p>
<?php
}
}
/**
* Code style text field.
*
* Args:
* - int "rows" - number of rows in the <textarea>
*/
class CMB_Textarea_Field_Code extends CMB_Field {
public function html() {
?>
<p>
<textarea class="cmb_textarea_code" rows="<?php echo !empty( $this->args['rows'] ) ? $this->args['rows'] : 4 ?>" name="<?php echo $this->name ?>"><?php echo $this->value ?></textarea>
</p>
<?php
}
}
/**
* Colour picker
*
*/
class CMB_Color_Picker extends CMB_Field {
public function html() {
?>
<p>
<input class="cmb_colorpicker cmb_text_small" type="text" name="<?php echo $this->name; ?>" value="<?php echo $this->get_value() ?>" /><span class="cmb_metabox_description"><?php echo $this->description ?></span>
</p>
<?php
}
}
/**
* 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 enqueue_scripts() {
parent::enqueue_scripts();
wp_enqueue_script( 'select2', CMB_URL . '/js/select2/select2.js', array( 'jquery' ) );
}
public function enqueue_styles() {
parent::enqueue_styles();
wp_enqueue_style( 'select2', CMB_URL . '/js/select2/select2.css' );
}
public function html() {
if ( $this->has_data_delegate() )
$this->args['options'] = $this->get_delegate_data();
$id = 'select-' . rand( 0, 1000 );
?>
<p>
<select <?php echo ! empty( $this->args['multiple'] ) ? 'multiple' : '' ?> id="<?php echo $id ?>" name="<?php echo $this->name ?>"> >
<?php if ( ! empty( $this->args['allow_none'] ) ) : ?>
<option value="">None</option>
<?php endif; ?>
<?php foreach ( $this->args['options'] as $value => $name ): ?>
<option <?php selected( $this->value, $value ) ?> value="<?php echo $value; ?>"><?php echo $name; ?></option>
<?php endforeach; ?>
</select>
</p>
<script>
jQuery( document ).ready( function() {
jQuery( '#<?php echo $id ?>' ).select2();
} );
</script>
<?php
}
}
/**
* Standard radio field.
*
* Args:
* - bool "inline" - display the radio buttons inline
*/
class CMB_Radio_Field extends CMB_Field {
public function html() {
?>
<p>
<?php foreach ( $this->values as $key => $value ): ?>
<input type="radio" name="<?php echo $this->name ?>" value="<?php echo $value; ?>" <?php checked( $value, $this->get_value() ); ?> />
<?php endforeach; ?>
</p>
<?php
}
}
/**
* Standard checkbox field.
*
*/
class CMB_Checkbox extends CMB_Field {
public function parse_save_values() {
$name = str_replace( '[]', '', $this->name );
$values = _cmb_get_value_from_array_path( $_POST, 'checkbox_' . $name );
foreach ( $this->values as $key => $value )
$this->values[$key] = $values[$key];
$this->value = reset( $this->values );
}
public function title() {
}
public function html() {
?>
<p>
<label>
<input type="checkbox" name="checkbox_<?php echo $this->name ?>" value="1" <?php checked( $this->get_value() ); ?> />
<?php echo $this->args['name'] ?>
</label>
<span class="cmb_metabox_description"><?php echo $this->description ?></span>
<input type="hidden" name="<?php echo $this->name ?>" value="1" />
</p>
<?php
}
}
/**
* Standard title used as a splitter.
*
*/
class CMB_Title extends CMB_Field {
public function html() {
?>
<p>
<h5 class="cmb_metabox_title"><?php echo $this->title; ?></h5>
<span class="cmb_metabox_description"><?php echo $this->description ?></span>
</p>
<?php
}
}
/**
* wysiwyg field.
*
*/
class CMB_wysiwyg extends CMB_Field {
public function html() {
?>
<p>
<?php wp_editor( $this->get_value(), $this->name, $this->args['options'] );?><span class="cmb_metabox_description"><?php echo $this->description ?></span>
</p>
<?php
}
}
class CMB_Taxonomy extends CMB_Select {
public function __construct() {
$args = func_get_args();
call_user_func_array( array( 'parent', '__construct' ), $args );
$this->args['data_delegate'] = array( $this, 'get_delegate_data' );
}
public function get_delegate_data() {
$terms = $this->get_terms();
$term_options = array();
foreach ( $terms as $term )
$term_options[$term->term_id] = $term->name;
return $term_options;
}
private function get_terms() {
return get_terms( $this->args['taxonomy'], array( 'hide_empty' => $this->args['hide_empty'] ) );
}
}
/**
* Field to group child fieids
* pass $args[fields] array for child fields
* pass $args['repeatable'] for cloing all child fields (set)
*
* @todo remove global $post reference, somehow
*/
class CMB_Group_Field extends CMB_Field {
static $added_js;
public function display() {
global $post;
$meta = $this->values;
if ( ! $meta && ! $this->args['repeatable'] )
$meta = array( '' );
$field = $this->args;
foreach ( $meta as $value ) {
$this->value = $value;
echo '<div class="field-item">';
$this->html();
echo '</div>';
?>
<?php
}
if ( $this->args['repeatable'] ) {
$this->value = '';
echo '<div class="field-item hidden">';
$this->html();
echo '</div>';
?>
<p>
<a href="#" class="button repeat-field">Add New</a>
</p>
<?php
}
if ( ! self::$added_js ) : ?>
<script type="text/javascript">
</script>
<?php self::$added_js = true; endif;
}
public function html() {
$field = $this->args;
$value = $this->value;
$fields = array();
foreach ( $this->args['fields'] as $f ) {
$field_value = isset( $this->value[$f['id']] ) ? $this->value[$f['id']] : '';
$f['uid'] = $field['id'] . '[' . $f['id'] . ']';
// If it's cloneable , make it an array
if ( $field['repeatable'] == true )
$f['uid'] .= '[]';
$class = _cmb_field_class_for_type( $f['type'] );
$f['show_label'] = true;
// Todo support for repeatble fields in groups
$fields[] = new $class( $f['uid'], $f['name'], array( $field_value ), $f );
}
?>
<div class="group <?php echo !empty( $field['repeatable'] ) ? 'cloneable' : '' ?>" style="position: relative">
<?php if ( ! empty( $this->args['name'] ) ) : ?>
<h2 class="group-name"><?php echo $this->args['name'] ?></h2>
<?php endif; ?>
<?php if ( $this->args['repeatable'] ) : ?>
<a class="delete-field button" style="position: absolute; top: -3px; right: -3px">X</a>
<?php endif; ?>
<?php CMB_Meta_Box::layout_fields( $fields ); ?>
</div>
<?php
}
public function parse_save_values() {
$values = $this->values;
$this->values = array();
$first = reset( $values );
foreach ($first as $key => $field_val ) {
$meta = array();
foreach ( $this->args['fields'] as $construct_field ) {
$name = $this->args['id'] . '[' . $construct_field['id'] . ']';
// If it's cloneable , make it an array
if ( $this->args['repeatable'] == true )
$name .= '[]';
// create the fiel object so it can sanitize it's data etc
$class = _cmb_field_class_for_type( $construct_field['type'] );
$field = new $class( $name, $construct_field['name'], (array) $values[$construct_field['id']][$key], $construct_field );
$field->parse_save_values();
$field->parse_save_value();
$meta[$construct_field['id']] = $field->get_value();
}
if( $this->isNotEmptyArray( $meta ) )
$this->values[] = $meta;
}
parent::parse_save_values();
}
private function isNotEmptyArray( $array ) {
foreach ($array as &$value)
{
if (is_array($value))
{
$value = $this->isNotEmptyArray($value);
}
}
return array_filter($array);
}
}
function _cmb_get_value_from_array_path( $array, $path ) {
if ( strpos( $path , '[' ) === false )
return $array[$path];
$top_level = reset( explode( '[' , $path ) );
preg_match_all( '#\[([^\]]+)\]#', $path, $matches );
$value = $array[$top_level];
$keys = $matches[1];
foreach ( $keys as $key )
$value = $value[$key];
return $value;
}