-
Notifications
You must be signed in to change notification settings - Fork 0
/
CMB2_Types.php
900 lines (765 loc) · 29.2 KB
/
CMB2_Types.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
<?php
/**
* CMB field types
*
* @todo test taxonomy methods with non-post objects
* @todo test all methods with non-post objects
* @todo Date/Time fields should store date format as data attribute for JS
*
* @since 1.0.0
*/
class CMB2_Types {
/**
* An iterator value for repeatable fields
* @var integer
* @since 1.0.0
*/
public $iterator = 0;
/**
* Current CMB2_Field field object
* @var array
* @since 1.0.0
*/
public $field;
public function __construct( CMB2_Field $field ) {
$this->field = $field;
}
/**
* Default fallback. Allows rendering fields via "cmb2_render_$name" hook
* @since 1.0.0
* @param string $name Non-existent method name
* @param array $arguments All arguments passed to the method
*/
public function __call( $name, $arguments ) {
$this->field->peform_param_cb( 'before_field' );
/**
* Pass non-existent field types through an action
*
* The dynamic portion of the hook name, $name, refers to the field type.
*
* @param array $field The passed in `CMB2_Field` object
* @param mixed $escaped_value The value of this field escaped.
* It defaults to `sanitize_text_field`.
* If you need the unescaped value, you can access it
* via `$field->value()`
* @param int $object_id The ID of the current object
* @param string $object_type The type of object you are working with.
* Most commonly, `post` (this applies to all post-types),
* but could also be `comment`, `user` or `options-page`.
* @param object $field_type_object This `CMB2_Types` object
*/
do_action( "cmb2_render_$name", $this->field, $this->field->escaped_value(), $this->field->object_id, $this->field->object_type, $this );
$this->field->peform_param_cb( 'after_field' );
}
/**
* Render a field (and handle repeatable)
* @since 1.1.0
*/
public function render() {
if ( $this->field->args( 'repeatable' ) ) {
$this->render_repeatable_field();
} else {
$this->_render();
}
}
/**
* Render a field type
* @since 1.1.0
*/
protected function _render() {
$this->field->peform_param_cb( 'before_field' );
echo $this->{$this->field->type()}();
$this->field->peform_param_cb( 'after_field' );
}
/**
* Checks if we can get a post object, and if so, uses `get_the_terms` which utilizes caching
* @since 1.0.2
* @return mixed Array of terms on success
*/
public function get_object_terms() {
$object_id = $this->field->object_id;
$taxonomy = $this->field->args( 'taxonomy' );
if ( ! $post = get_post( $object_id ) ) {
$cache_key = 'cmb-cache-'. $taxonomy .'-'. $object_id;
// Check cache
$cached = $test = get_transient( $cache_key );
if ( $cached ) {
return $cached;
}
$cached = wp_get_object_terms( $object_id, $taxonomy );
// Do our own (minimal) caching. Long enough for a page-load.
$set = set_transient( $cache_key, $cached, 60 );
return $cached;
}
// WP caches internally so it's better to use
return get_the_terms( $post, $taxonomy );
}
/**
* Retrieve text parameter from field's options array (if it has one), or use fallback text
* @since 2.0.0
* @param string $option_key Key in field's options array
* @param string $fallback Fallback text
* @return string Text
*/
public function _text( $option_key, $fallback ) {
$options = (array) $this->field->args( 'options' );
return isset( $options[ $option_key ] ) ? $options[ $option_key ] : $fallback;
}
/**
* Determine a file's extension
* @since 1.0.0
* @param string $file File url
* @return string|false File extension or false
*/
public function get_file_ext( $file ) {
$parsed = @parse_url( $file, PHP_URL_PATH );
return $parsed ? strtolower( pathinfo( $parsed, PATHINFO_EXTENSION ) ) : false;
}
/**
* Determines if a file has a valid image extension
* @since 1.0.0
* @param string $file File url
* @return bool Whether file has a valid image extension
*/
public function is_valid_img_ext( $file ) {
$file_ext = $this->get_file_ext( $file );
$this->valid = empty( $this->valid )
? (array) apply_filters( 'cmb2_valid_img_types', array( 'jpg', 'jpeg', 'png', 'gif', 'ico', 'icon' ) )
: $this->valid;
return ( $file_ext && in_array( $file_ext, $this->valid ) );
}
/**
* Handles parsing and filtering attributes while preserving any passed in via field config.
* @since 1.1.0
* @param array $args Override arguments
* @param string $element Element for filter
* @param array $defaults Default arguments
* @return array Parsed and filtered arguments
*/
public function parse_args( $args, $element, $defaults ) {
return wp_parse_args( apply_filters( "cmb2_{$element}_attributes", $this->field->maybe_set_attributes( $args ), $defaults, $this->field, $this ), $defaults );
}
/**
* Combines attributes into a string for a form element
* @since 1.1.0
* @param array $attrs Attributes to concatenate
* @param array $attr_exclude Attributes that should NOT be concatenated
* @return string String of attributes for form element
*/
public function concat_attrs( $attrs, $attr_exclude = array() ) {
$attributes = '';
foreach ( $attrs as $attr => $val ) {
if ( ! in_array( $attr, (array) $attr_exclude, true ) ) {
$attributes .= sprintf( ' %s="%s"', $attr, $val );
}
}
return $attributes;
}
/**
* Generates html for an option element
* @since 1.1.0
* @param string $opt_label Option label
* @param string $opt_value Option value
* @param mixed $selected Selected attribute if option is selected
* @return string Generated option element html
*/
public function option( $opt_label, $opt_value, $selected ) {
return sprintf( "\t".'<option value="%s" %s>%s</option>', $opt_value, selected( $selected, true, false ), $opt_label )."\n";
}
/**
* Generates options html
* @since 1.1.0
* @param array $args Optional arguments
* @param string $method Method to generate individual option item
* @return string Concatenated html options
*/
public function concat_options( $args = array(), $method = 'list_input' ) {
$options = (array) $this->field->args( 'options' );
$saved_value = $this->field->escaped_value();
$value = $saved_value ? $saved_value : $this->field->args( 'default' );
$_options = ''; $i = 1;
foreach ( $options as $option_key => $option ) {
// Check for the "old" way
$opt_label = is_array( $option ) && array_key_exists( 'name', $option ) ? $option['name'] : $option;
$opt_value = is_array( $option ) && array_key_exists( 'value', $option ) ? $option['value'] : $option_key;
// Check if this option is the value of the input
$is_current = $value == $opt_value;
if ( ! empty( $args ) ) {
// Clone args & modify for just this item
$this_args = $args;
$this_args['value'] = $opt_value;
$this_args['label'] = $opt_label;
if ( $is_current ) {
$this_args['checked'] = 'checked';
}
$_options .= $this->$method( $this_args, $i );
} else {
$_options .= $this->option( $opt_label, $opt_value, $is_current );
}
$i++;
}
return $_options;
}
/**
* Generates html for list item with input
* @since 1.1.0
* @param array $args Override arguments
* @param int $i Iterator value
* @return string Gnerated list item html
*/
public function list_input( $args = array(), $i ) {
$args = $this->parse_args( $args, 'list_input', array(
'type' => 'radio',
'class' => 'cmb2-option',
'name' => $this->_name(),
'id' => $this->_id( $i ),
'value' => $this->field->escaped_value(),
'label' => '',
) );
return sprintf( "\t".'<li><input%s/> <label for="%s">%s</label></li>'."\n", $this->concat_attrs( $args, 'label' ), $args['id'], $args['label'] );
}
/**
* Generates html for list item with checkbox input
* @since 1.1.0
* @param array $args Override arguments
* @param int $i Iterator value
* @return string Gnerated list item html
*/
public function list_input_checkbox( $args, $i ) {
unset( $args['selected'] );
$saved_value = $this->field->escaped_value();
if ( is_array( $saved_value ) && in_array( $args['value'], $saved_value ) ) {
$args['checked'] = 'checked';
}
return $this->list_input( $args, $i );
}
/**
* Generates repeatable field table markup
* @since 1.0.0
*/
public function render_repeatable_field() {
$table_id = $this->field->id() .'_repeat';
$this->_desc( true, true, true );
?>
<div id="<?php echo $table_id; ?>" class="cmb-repeat-table cmb-nested">
<div class="cmb-tbody cmb-field-list">
<?php $this->repeatable_rows(); ?>
</div>
</div>
<p class="cmb-add-row">
<a data-selector="<?php echo $table_id; ?>" class="cmb-add-row-button button" href="#"><?php echo esc_html( $this->_text( 'add_row_text', __( 'Add Row', 'cmb2' ) ) ); ?></a>
</p>
<?php
// reset iterator
$this->iterator = 0;
}
/**
* Generates repeatable field rows
* @since 1.1.0
*/
public function repeatable_rows() {
$meta_value = array_filter( (array) $this->field->escaped_value() );
// check for default content
$default = $this->field->args( 'default' );
// check for saved data
if ( ! empty( $meta_value ) ) {
$meta_value = is_array( $meta_value ) ? array_filter( $meta_value ) : $meta_value;
$meta_value = ! empty( $meta_value ) ? $meta_value : $default;
} else {
$meta_value = $default;
}
// Loop value array and add a row
if ( ! empty( $meta_value ) ) {
$count = count( $meta_value );
foreach ( (array) $meta_value as $val ) {
$this->field->escaped_value = $val;
$this->repeat_row( $count < 2 );
$this->iterator++;
}
} else {
// Otherwise add one row
$this->repeat_row( true );
}
// Then add an empty row
$this->field->escaped_value = '';
$this->iterator = $this->iterator ? $this->iterator : 1;
$this->repeat_row( false, 'empty-row hidden' );
}
/**
* Generates a repeatable row's markup
* @since 1.1.0
* @param string $disable_remover Whether remove button should be disabled
* @param string $class Repeatable table row's class
*/
protected function repeat_row( $disable_remover = false, $class = 'cmb-repeat-row' ) {
$disabled = $disable_remover ? 'disabled="disabled"' : '';
?>
<div class="cmb-row <?php echo $class; ?>">
<div class="cmb-td">
<?php $this->_render(); ?>
</div>
<div class="cmb-td cmb-remove-row">
<a class="button cmb-remove-row-button" <?php echo $disabled; ?> href="#"><?php echo esc_html( $this->_text( 'remove_row_text', __( 'Remove', 'cmb2' ) ) ); ?></a>
</div>
</div>
<?php
}
/**
* Generates description markup
* @since 1.0.0
* @param boolean $paragraph Paragraph tag or span
* @param boolean $echo Whether to echo description or only return it
* @return string Field's description markup
*/
public function _desc( $paragraph = false, $echo = false, $repeat_group = false ) {
// Prevent description from printing multiple times for repeatable fields
if ( ! $repeat_group && ( $this->field->args( 'repeatable' ) || $this->iterator > 0 ) ) {
return '';
}
$desc = $this->field->args( 'description' );
if ( ! $desc ) {
return;
}
$tag = $paragraph ? 'p' : 'span';
$desc = "\n<$tag class=\"cmb2-metabox-description\">{$desc}</$tag>\n";
if ( $echo )
echo $desc;
return $desc;
}
/**
* Generate field name attribute
* @since 1.1.0
* @param string $suffix For multi-part fields
* @return string Name attribute
*/
public function _name( $suffix = '' ) {
return $this->field->args( '_name' ) . ( $this->field->args( 'repeatable' ) ? '['. $this->iterator .']' : '' ) . $suffix;
}
/**
* Generate field id attribute
* @since 1.1.0
* @param string $suffix For multi-part fields
* @return string Id attribute
*/
public function _id( $suffix = '' ) {
return $this->field->id() . $suffix . ( $this->field->args( 'repeatable' ) ? '_'. $this->iterator .'" data-iterator="'. $this->iterator : '' );
}
/**
* Handles outputting an 'input' element
* @since 1.1.0
* @param array $args Override arguments
* @return string Form input element
*/
public function input( $args = array() ) {
$args = $this->parse_args( $args, 'input', array(
'type' => 'text',
'class' => 'regular-text',
'name' => $this->_name(),
'id' => $this->_id(),
'value' => $this->field->escaped_value(),
'desc' => $this->_desc( true ),
) );
return sprintf( '<input%s/>%s', $this->concat_attrs( $args, 'desc' ), $args['desc'] );
}
/**
* Handles outputting an 'textarea' element
* @since 1.1.0
* @param array $args Override arguments
* @return string Form textarea element
*/
public function textarea( $args = array() ) {
$args = $this->parse_args( $args, 'textarea', array(
'class' => 'cmb2_textarea',
'name' => $this->_name(),
'id' => $this->_id(),
'cols' => 60,
'rows' => 10,
'value' => $this->field->escaped_value( 'esc_textarea' ),
'desc' => $this->_desc( true ),
) );
return sprintf( '<textarea%s>%s</textarea>%s', $this->concat_attrs( $args, array( 'desc', 'value' ) ), $args['value'], $args['desc'] );
}
/**
* Begin Field Types
*/
public function text() {
return $this->input();
}
public function hidden() {
return $this->input( array( 'type' => 'hidden', 'desc' => '', 'class' => '' ) );
}
public function text_small() {
return $this->input( array( 'class' => 'cmb2-text-small', 'desc' => $this->_desc() ) );
}
public function text_medium() {
return $this->input( array( 'class' => 'cmb2-text-medium', 'desc' => $this->_desc() ) );
}
public function text_email() {
return $this->input( array( 'class' => 'cmb2-text-email cmb2-text-medium', 'type' => 'email' ) );
}
public function text_url() {
return $this->input( array( 'class' => 'cmb2-text-url cmb2-text-medium regular-text', 'value' => $this->field->escaped_value( 'esc_url' ) ) );
}
public function text_date() {
return $this->input( array( 'class' => 'cmb2-text-small cmb2-datepicker', 'desc' => $this->_desc() ) );
}
public function text_time() {
return $this->input( array( 'class' => 'cmb2-timepicker text-time', 'desc' => $this->_desc() ) );
}
public function text_money() {
return ( ! $this->field->args( 'before_field' ) ? '$ ' : ' ' ) . $this->input( array( 'class' => 'cmb2-text-money', 'desc' => $this->_desc() ) );
}
public function textarea_small() {
return $this->textarea( array( 'class' => 'cmb2-textarea-small', 'rows' => 4 ) );
}
public function textarea_code() {
return sprintf( '<pre>%s', $this->textarea( array( 'class' => 'cmb2-textarea-code', 'desc' => '</pre>' . $this->_desc( true ) ) ) );
}
public function wysiwyg( $args = array() ) {
extract( $this->parse_args( $args, 'input', array(
'id' => $this->_id(),
'value' => $this->field->escaped_value( 'stripslashes' ),
'desc' => $this->_desc( true ),
'options' => $this->field->args( 'options' ),
) ) );
wp_editor( $value, $id, $options );
echo $desc;
}
public function text_date_timestamp() {
$meta_value = $this->field->escaped_value();
$value = ! empty( $meta_value ) ? date( $this->field->args( 'date_format' ), $meta_value ) : '';
return $this->input( array( 'class' => 'cmb2-text-small cmb2-datepicker', 'value' => $value ) );
}
public function text_datetime_timestamp( $meta_value = '' ) {
$desc = '';
if ( ! $meta_value ) {
$meta_value = $this->field->escaped_value();
// This will be used if there is a select_timezone set for this field
$tz_offset = $this->field->field_timezone_offset();
if ( ! empty( $tz_offset ) ) {
$meta_value -= $tz_offset;
}
$desc = $this->_desc();
}
$inputs = array(
$this->input( array(
'class' => 'cmb2-text-small cmb2-datepicker',
'name' => $this->_name( '[date]' ),
'id' => $this->_id( '_date' ),
'value' => ! empty( $meta_value ) && ! is_array( $meta_value ) ? date( $this->field->args( 'date_format' ), $meta_value ) : '',
'desc' => '',
) ),
$this->input( array(
'class' => 'cmb2-timepicker text-time',
'name' => $this->_name( '[time]' ),
'id' => $this->_id( '_time' ),
'value' => ! empty( $meta_value ) && ! is_array( $meta_value ) ? date( $this->field->args( 'time_format' ), $meta_value ) : '',
'desc' => $desc,
) )
);
return implode( "\n", $inputs );
}
public function text_datetime_timestamp_timezone() {
$meta_value = $this->field->escaped_value();
if ( is_array( $meta_value ) ) {
$meta_value = '';
}
$datetime = unserialize( $meta_value );
$meta_value = $tzstring = false;
if ( $datetime && $datetime instanceof DateTime ) {
$tz = $datetime->getTimezone();
$tzstring = $tz->getName();
$meta_value = $datetime->getTimestamp() + $tz->getOffset( new DateTime( 'NOW' ) );
}
$inputs = $this->text_datetime_timestamp( $meta_value );
$inputs .= '<select name="'. $this->_name( '[timezone]' ) .'" id="'. $this->_id( '_timezone' ) .'">';
$inputs .= wp_timezone_choice( $tzstring );
$inputs .= '</select>'. $this->_desc();
return $inputs;
}
public function select_timezone() {
$this->field->args['default'] = $this->field->args( 'default' )
? $this->field->args( 'default' )
: cmb2_utils()->timezone_string();
$meta_value = $this->field->escaped_value();
return '<select name="'. $this->_name() .'" id="'. $this->_id() .'">'. wp_timezone_choice( $meta_value ) .'</select>';
}
public function colorpicker() {
$meta_value = $this->field->escaped_value();
$hex_color = '(([a-fA-F0-9]){3}){1,2}$';
if ( preg_match( '/^' . $hex_color . '/i', $meta_value ) ) { // Value is just 123abc, so prepend #.
$meta_value = '#' . $meta_value;
}
elseif ( ! preg_match( '/^#' . $hex_color . '/i', $meta_value ) ) { // Value doesn't match #123abc, so sanitize to just #.
$meta_value = "#";
}
return $this->input( array( 'class' => 'cmb2-colorpicker cmb2-text-small', 'value' => $meta_value ) );
}
public function title() {
extract( $this->parse_args( array(), 'title', array(
'tag' => $this->field->object_type == 'post' ? 'h5' : 'h3',
'class' => 'cmb2-metabox-title',
'name' => $this->field->args( 'name' ),
'desc' => $this->_desc( true ),
) ) );
return sprintf( '<%1$s class="%2$s">%3$s</%1$s>%4$s', $tag, $class, $name, $desc );
}
public function select( $args = array() ) {
$args = $this->parse_args( $args, 'select', array(
'class' => 'cmb2_select',
'name' => $this->_name(),
'id' => $this->_id(),
'desc' => $this->_desc( true ),
'options' => $this->concat_options(),
) );
$attrs = $this->concat_attrs( $args, array( 'desc', 'options' ) );
return sprintf( '<select%s>%s</select>%s', $attrs, $args['options'], $args['desc'] );
}
public function taxonomy_select() {
$names = $this->get_object_terms();
$saved_term = is_wp_error( $names ) || empty( $names ) ? $this->field->args( 'default' ) : $names[0]->slug;
$terms = get_terms( $this->field->args( 'taxonomy' ), 'hide_empty=0' );
$options = '';
$option_none = $this->field->args( 'show_option_none' );
if( ! empty( $option_none ) ) {
$option_none_value = apply_filters( 'cmb2_taxonomy_select_default_value', '' );
$option_none_value = apply_filters( "cmb2_taxonomy_select_{$this->_id()}_default_value", $option_none_value );
$selected = $saved_term == $option_none_value;
$options .= $this->option( $option_none, $option_none_value, $selected );
}
foreach ( $terms as $term ) {
$selected = $saved_term == $term->slug;
$options .= $this->option( $term->name, $term->slug, $selected );
}
return $this->select( array( 'options' => $options ) );
}
public function radio( $args = array(), $type = 'radio' ) {
extract( $this->parse_args( $args, $type, array(
'class' => 'cmb2-radio-list cmb2-list',
'options' => $this->concat_options( array( 'label' => 'test' ) ),
'desc' => $this->_desc( true ),
) ) );
return sprintf( '<ul class="%s">%s</ul>%s', $class, $options, $desc );
}
public function radio_inline() {
return $this->radio( array(), 'radio_inline' );
}
public function multicheck( $type = 'checkbox' ) {
$classes = false === $this->field->args( 'select_all_button' )
? 'cmb2-checkbox-list no-select-all cmb2-list'
: 'cmb2-checkbox-list cmb2-list';
return $this->radio( array( 'class' => $classes, 'options' => $this->concat_options( array( 'type' => 'checkbox', 'name' => $this->_name() .'[]' ), 'list_input_checkbox' ) ), $type );
}
public function multicheck_inline() {
$this->multicheck( 'multicheck_inline' );
}
public function checkbox() {
$meta_value = $this->field->escaped_value();
$args = array( 'type' => 'checkbox', 'class' => 'cmb2-option cmb2-list', 'value' => 'on', 'desc' => '' );
if ( ! empty( $meta_value ) ) {
$args['checked'] = 'checked';
}
return sprintf( '%s <label for="%s">%s</label>', $this->input( $args ), $this->_id(), $this->_desc() );
}
public function taxonomy_radio() {
$names = $this->get_object_terms();
$saved_term = is_wp_error( $names ) || empty( $names ) ? $this->field->args( 'default' ) : $names[0]->slug;
$terms = get_terms( $this->field->args( 'taxonomy' ), 'hide_empty=0' );
$options = ''; $i = 1;
if ( ! $terms ) {
$options .= '<li><label>'. esc_html( $this->_text( 'no_terms_text', __( 'No terms', 'cmb2' ) ) ) .'</label></li>';
} else {
$option_none = $this->field->args( 'show_option_none' );
if( ! empty( $option_none ) ) {
$option_none_value = apply_filters( "cmb2_taxonomy_radio_{$this->_id()}_default_value", apply_filters( 'cmb2_taxonomy_radio_default_value', '' ) );
$args = array(
'value' => $option_none_value,
'label' => $option_none,
);
if( $saved_term == $option_none_value ) {
$args['checked'] = 'checked';
}
$options .= $this->list_input( $args, $i );
$i++;
}
foreach ( $terms as $term ) {
$args = array(
'value' => $term->slug,
'label' => $term->name,
);
if ( $saved_term == $term->slug ) {
$args['checked'] = 'checked';
}
$options .= $this->list_input( $args, $i );
$i++;
}
}
return $this->radio( array( 'options' => $options ), 'taxonomy_radio' );
}
public function taxonomy_radio_inline() {
$this->taxonomy_radio();
}
public function taxonomy_multicheck() {
$names = $this->get_object_terms();
$saved_terms = is_wp_error( $names ) || empty( $names )
? $this->field->args( 'default' )
: wp_list_pluck( $names, 'slug' );
$terms = get_terms( $this->field->args( 'taxonomy' ), 'hide_empty=0' );
$name = $this->_name() .'[]';
$options = ''; $i = 1;
if ( ! $terms ) {
$options .= '<li><label>'. esc_html( $this->_text( 'no_terms_text', __( 'No terms', 'cmb2' ) ) ) .'</label></li>';
} else {
foreach ( $terms as $term ) {
$args = array(
'value' => $term->slug,
'label' => $term->name,
'type' => 'checkbox',
'name' => $name,
);
if ( is_array( $saved_terms ) && in_array( $term->slug, $saved_terms ) ) {
$args['checked'] = 'checked';
}
$options .= $this->list_input( $args, $i );
$i++;
}
}
$classes = false === $this->field->args( 'select_all_button' )
? 'cmb2-checkbox-list no-select-all cmb2-list'
: 'cmb2-checkbox-list cmb2-list';
return $this->radio( array( 'class' => $classes, 'options' => $options ), 'taxonomy_multicheck' );
}
public function taxonomy_multicheck_inline() {
$this->taxonomy_multicheck();
}
public function file_list() {
$meta_value = $this->field->escaped_value();
$name = $this->_name();
$img_size = $this->field->args( 'preview_size' );
echo $this->input( array(
'type' => 'hidden',
'class' => 'cmb2-upload-file cmb2-upload-list',
'size' => 45, 'desc' => '', 'value' => '',
'data-previewsize' => is_array( $img_size ) ? '['. implode( ',', $img_size ) .']' : 50,
) ),
$this->input( array(
'type' => 'button',
'class' => 'cmb2-upload-button button cmb2-upload-list',
'value' => esc_html( $this->_text( 'add_upload_file_text', __( 'Add or Upload File', 'cmb2' ) ) ),
'name' => '', 'id' => '',
) );
echo '<ul id="', $this->_id( '-status' ) ,'" class="cmb2-media-status cmb-attach-list">';
if ( $meta_value && is_array( $meta_value ) ) {
foreach ( $meta_value as $id => $fullurl ) {
$id_input = $this->input( array(
'type' => 'hidden',
'value' => $fullurl,
'name' => $name .'['. $id .']',
'id' => 'filelist-'. $id,
'desc' => '', 'class' => '',
) );
if ( $this->is_valid_img_ext( $fullurl ) ) {
echo
'<li class="img-status">',
wp_get_attachment_image( $id, $img_size ),
'<p class="cmb2-remove-wrapper"><a href="#" class="cmb2-remove-file-button">'. esc_html( $this->_text( 'remove_image_text', __( 'Remove Image', 'cmb2' ) ) ) .'</a></p>
'. $id_input .'
</li>';
} else {
$parts = explode( '/', $fullurl );
for ( $i = 0; $i < count( $parts ); ++$i ) {
$title = $parts[$i];
}
echo
'<li>',
esc_html( $this->_text( 'file_text', __( 'File:', 'cmb2' ) ) ) ,' <strong>', $title ,'</strong> (<a href="', $fullurl ,'" target="_blank" rel="external">', esc_html( $this->_text( 'file-download-text', __( 'Download', 'cmb2' ) ) ) ,'</a> / <a href="#" class="cmb2-remove-file-button">', esc_html( $this->_text( 'remove_text', __( 'Remove', 'cmb2' ) ) ) ,'</a>)
', $id_input ,'
</li>';
}
}
}
echo '</ul>';
}
public function file() {
$meta_value = $this->field->escaped_value();
$options = (array) $this->field->args( 'options' );
$img_size = $this->field->args( 'preview_size' );
// if options array and 'url' => false, then hide the url field
$input_type = array_key_exists( 'url', $options ) && false === $options['url'] ? 'hidden' : 'text';
echo $this->input( array(
'type' => $input_type,
'class' => 'cmb2-upload-file',
'size' => 45,
'desc' => '',
'data-previewsize' => is_array( $img_size ) ? '['. implode( ',', $img_size ) .']' : 350,
) ),
'<input class="cmb2-upload-button button" type="button" value="'. esc_attr( $this->_text( 'add_upload_file_text', __( 'Add or Upload File', 'cmb2' ) ) ) .'" />',
$this->_desc( true );
$cached_id = $this->_id();
// Reset field args for attachment ID
$args = $this->field->args();
$args['id'] = $args['_id'] . '_id';
unset( $args['_id'], $args['_name'] );
// And get new field object
$this->field = new CMB2_Field( array(
'field_args' => $args,
'group_field' => $this->field->group,
'object_type' => $this->field->object_type(),
'object_id' => $this->field->object_id(),
) );
// Get ID value
$_id_value = $this->field->escaped_value( 'absint' );
// If there is no ID saved yet, try to get it from the url
if ( $meta_value && ! $_id_value ) {
$_id_value = cmb2_utils()->image_id_from_url( esc_url_raw( $meta_value ) );
}
echo $this->input( array(
'type' => 'hidden',
'class' => 'cmb2-upload-file-id',
'value' => $_id_value,
'desc' => '',
) ),
'<div id="', $this->_id( '-status' ) ,'" class="cmb2-media-status">';
if ( ! empty( $meta_value ) ) {
if ( $this->is_valid_img_ext( $meta_value ) ) {
echo '<div class="img-status">';
if ( $_id_value ) {
$image = wp_get_attachment_image( $_id_value, $img_size, null, array( 'class' => 'cmb-file-field-image' ) );
} else {
$size = is_array( $img_size ) ? $img_size[0] : 350;
$image = '<img style="max-width: '. absint( $size ) .'px; width: 100%; height: auto;" src="'. $meta_value .'" alt="" />';
}
echo $image;
echo '<p class="cmb2-remove-wrapper"><a href="#" class="cmb2-remove-file-button" rel="', $cached_id, '">'. esc_html( $this->_text( 'remove_image_text', __( 'Remove Image', 'cmb2' ) ) ) .'</a></p>';
echo '</div>';
} else {
// $file_ext = $this->get_file_ext( $meta_value );
$parts = explode( '/', $meta_value );
for ( $i = 0; $i < count( $parts ); ++$i ) {
$title = $parts[$i];
}
echo esc_html( $this->_text( 'file_text', __( 'File:', 'cmb2' ) ) ), ' <strong>', $title ,'</strong> (<a href="', $meta_value ,'" target="_blank" rel="external">', esc_html( $this->_text( 'file-download-text', __( 'Download', 'cmb2' ) ) ) ,'</a> / <a href="#" class="cmb2-remove-file-button" rel="', $cached_id, '">', esc_html( $this->_text( 'remove_text', __( 'Remove', 'cmb2' ) ) ) ,'</a>)';
}
}
echo '</div>';
}
public function oembed() {
echo $this->input( array(
'class' => 'cmb2-oembed regular-text',
'data-objectid' => $this->field->object_id,
'data-objecttype' => $this->field->object_type
) ),
'<p class="cmb-spinner spinner" style="display:none;"><img src="'. admin_url( '/images/wpspin_light.gif' ) .'" alt="spinner"/></p>',
'<div id="',$this->_id( '-status' ) ,'" class="cmb2-media-status ui-helper-clearfix embed_wrap">';
if ( $meta_value = $this->field->escaped_value() ) {
echo cmb2_get_oembed( array(
'url' => $meta_value,
'object_id' => $this->field->object_id,
'object_type' => $this->field->object_type,
'oembed_args' => array( 'width' => '640' ),
'field_id' => $this->_id(),
) );
}
echo '</div>';
}
}