PluginProbe
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI / 6.0.3
AutomatorWP – No-Code Workflow Automation, Integration & Webhooks Plugin, now with AI v6.0.3
6.0.3 6.0.2 6.0.1 6.0.0 5.8.6 5.8.5 5.8.4 5.8.3 5.8.2 5.8.1 5.8.0 5.7.9.2 5.7.9.1 5.7.8 5.7.9 5.7.6 5.7.7 5.7.5 5.7.4 5.7.3 5.7.2 5.7.1 trunk 5.6.0 5.6.1 All 34 releases
← All changes | libraries/cmb2/includes/CMB2_Field.php +1647 -0 5.8.4 → 6.0.3 View file →
@@ -1,0 +1,1647 @@
1 +<?php
2 +/**
3 + * CMB2 field objects
4 + *
5 + * @since 1.1.0
6 + *
7 + * @category WordPress_Plugin
8 + * @package CMB2
9 + * @author CMB2 team
10 + * @license GPL-2.0+
11 + * @link https://cmb2.io
12 + *
13 + * @method string _id()
14 + * @method string type()
15 + * @method mixed fields()
16 + */
17 +class CMB2_Field extends CMB2_Base {
18 +
19 + /**
20 + * The object properties name.
21 + *
22 + * @var string
23 + * @since 2.2.3
24 + */
25 + protected $properties_name = 'args';
26 +
27 + /**
28 + * Field arguments
29 + *
30 + * @var mixed
31 + * @since 1.1.0
32 + */
33 + public $args = array();
34 +
35 + /**
36 + * Field group object or false (if no group)
37 + *
38 + * @var mixed
39 + * @since 1.1.0
40 + */
41 + public $group = false;
42 +
43 + /**
44 + * Field meta value
45 + *
46 + * @var mixed
47 + * @since 1.1.0
48 + */
49 + public $value = null;
50 +
51 + /**
52 + * Field meta value
53 + *
54 + * @var mixed
55 + * @since 1.1.0
56 + */
57 + public $escaped_value = null;
58 +
59 + /**
60 + * Grouped Field's current numeric index during the save process
61 + *
62 + * @var mixed
63 + * @since 2.0.0
64 + */
65 + public $index = 0;
66 +
67 + /**
68 + * Array of field options
69 + *
70 + * @var array
71 + * @since 2.0.0
72 + */
73 + protected $field_options = array();
74 +
75 + /**
76 + * Array of provided field text strings
77 + *
78 + * @var array
79 + * @since 2.0.0
80 + */
81 + protected $strings;
82 +
83 + /**
84 + * The field's render context. In most cases, 'edit', but can be 'display'.
85 + *
86 + * @var string
87 + * @since 2.2.2
88 + */
89 + public $render_context = 'edit';
90 +
91 + /**
92 + * All CMB2_Field callable field arguments.
93 + * Can be used to determine if a field argument is callable.
94 + *
95 + * @var array
96 + */
97 + public static $callable_fields = array(
98 + 'default_cb',
99 + 'classes_cb',
100 + 'options_cb',
101 + 'text_cb',
102 + 'label_cb',
103 + 'render_row_cb',
104 + 'display_cb',
105 + 'before_group',
106 + 'before_group_row',
107 + 'before_row',
108 + 'before',
109 + 'before_field',
110 + 'after_field',
111 + 'after',
112 + 'after_row',
113 + 'after_group_row',
114 + 'after_group',
115 + );
116 +
117 + /**
118 + * Represents a unique hash representing this field.
119 + *
120 + * @since 2.2.4
121 + *
122 + * @var string
123 + */
124 + protected $hash_id = '';
125 +
126 + /**
127 + * Constructs our field object
128 + *
129 + * @since 1.1.0
130 + * @param array $args Field arguments.
131 + */
132 + public function __construct( $args ) {
133 +
134 + if ( ! empty( $args['group_field'] ) ) {
135 + $this->group = $args['group_field'];
136 + $this->object_id = $this->group->object_id;
137 + $this->object_type = $this->group->object_type;
138 + $this->cmb_id = $this->group->cmb_id;
139 + } else {
140 + $this->object_id = isset( $args['object_id'] ) && '_' !== $args['object_id'] ? $args['object_id'] : 0;
141 + $this->object_type = isset( $args['object_type'] ) ? $args['object_type'] : 'post';
142 +
143 + if ( isset( $args['cmb_id'] ) ) {
144 + $this->cmb_id = $args['cmb_id'];
145 + }
146 + }
147 +
148 + $this->args = $this->_set_field_defaults( $args['field_args'] );
149 +
150 + if ( $this->object_id ) {
151 + $this->value = $this->get_data();
152 + }
153 + }
154 +
155 + /**
156 + * Non-existent methods fallback to checking for field arguments of the same name
157 + *
158 + * @since 1.1.0
159 + * @param string $name Method name.
160 + * @param array $arguments Array of passed-in arguments.
161 + * @return mixed Value of field argument
162 + */
163 + public function __call( $name, $arguments ) {
164 + if ( 'string' === $name ) {
165 + return call_user_func_array( array( $this, 'get_string' ), $arguments );
166 + }
167 +
168 + $key = isset( $arguments[0] ) ? $arguments[0] : '';
169 + return $this->args( $name, $key );
170 + }
171 +
172 + /**
173 + * Retrieves the field id
174 + *
175 + * @since 1.1.0
176 + * @param boolean $raw Whether to retrieve pre-modidifed id.
177 + * @return string Field id
178 + */
179 + public function id( $raw = false ) {
180 + $id = $raw ? '_id' : 'id';
181 + return $this->args( $id );
182 + }
183 +
184 + /**
185 + * Get a field argument
186 + *
187 + * @since 1.1.0
188 + * @param string $key Argument to check.
189 + * @param string $_key Sub argument to check.
190 + * @return mixed Argument value or false if non-existent
191 + */
192 + public function args( $key = '', $_key = '' ) {
193 + $arg = $this->_data( 'args', $key );
194 +
195 + if ( in_array( $key, array( 'default', 'default_cb' ), true ) ) {
196 +
197 + $arg = $this->get_default();
198 +
199 + } elseif ( $_key ) {
200 +
201 + $arg = isset( $arg[ $_key ] ) ? $arg[ $_key ] : false;
202 + }
203 +
204 + return $arg;
205 + }
206 +
207 + /**
208 + * Retrieve a portion of a field property
209 + *
210 + * @since 1.1.0
211 + * @param string $var Field property to check.
212 + * @param string $key Field property array key to check.
213 + * @return mixed Queried property value or false
214 + */
215 + public function _data( $var, $key = '' ) {
216 + $vars = $this->{$var};
217 + if ( $key ) {
218 + return array_key_exists( $key, $vars ) ? $vars[ $key ] : false;
219 + }
220 + return $vars;
221 + }
222 +
223 + /**
224 + * Get Field's value
225 + *
226 + * @since 1.1.0
227 + * @param string $key If value is an array, is used to get array key->value.
228 + * @return mixed Field value or false if non-existent
229 + */
230 + public function value( $key = '' ) {
231 + return $this->_data( 'value', $key );
232 + }
233 +
234 + /**
235 + * Retrieves metadata/option data
236 + *
237 + * @since 1.0.1
238 + * @param string $field_id Meta key/Option array key.
239 + * @param array $args Override arguments.
240 + * @return mixed Meta/Option value
241 + */
242 + public function get_data( $field_id = '', $args = array() ) {
243 + if ( $field_id ) {
244 + $args['field_id'] = $field_id;
245 + } elseif ( $this->group ) {
246 + $args['field_id'] = $this->group->id();
247 + }
248 +
249 + $a = $this->data_args( $args );
250 +
251 + /**
252 + * Filter whether to override getting of meta value.
253 + * Returning a non 'cmb2_field_no_override_val' value
254 + * will effectively short-circuit the value retrieval.
255 + *
256 + * @since 2.0.0
257 + *
258 + * @param mixed $value The value get_metadata() should
259 + * return - a single metadata value,
260 + * or an array of values.
261 + *
262 + * @param int $object_id Object ID.
263 + *
264 + * @param array $args {
265 + * An array of arguments for retrieving data
266 + *
267 + * @type string $type The current object type
268 + * @type int $id The current object ID
269 + * @type string $field_id The ID of the field being requested
270 + * @type bool $repeat Whether current field is repeatable
271 + * @type bool $single Whether current field is a single database row
272 + * }
273 + *
274 + * @param CMB2_Field object $field This field object
275 + */
276 + $data = apply_filters( 'cmb2_override_meta_value', 'cmb2_field_no_override_val', $this->object_id, $a, $this );
277 +
278 + /**
279 + * Filter and parameters are documented for 'cmb2_override_meta_value' filter (above).
280 + *
281 + * The dynamic portion of the hook, $field_id, refers to the current
282 + * field id paramater. Returning a non 'cmb2_field_no_override_val' value
283 + * will effectively short-circuit the value retrieval.
284 + *
285 + * @since 2.0.0
286 + */
287 + $data = apply_filters( "cmb2_override_{$a['field_id']}_meta_value", $data, $this->object_id, $a, $this );
288 +
289 + // If no override, get value normally.
290 + if ( 'cmb2_field_no_override_val' === $data ) {
291 + $data = 'options-page' === $a['type']
292 + ? cmb2_options( $a['id'] )->get( $a['field_id'] )
293 + : get_metadata( $a['type'], $a['id'], $a['field_id'], ( $a['single'] || $a['repeat'] ) );
294 + }
295 +
296 + if ( $this->group ) {
297 +
298 + $data = is_array( $data ) && isset( $data[ $this->group->index ][ $this->args( '_id' ) ] )
299 + ? $data[ $this->group->index ][ $this->args( '_id' ) ]
300 + : false;
301 + }
302 +
303 + return $data;
304 + }
305 +
306 + /**
307 + * Updates metadata/option data.
308 + *
309 + * @since 1.0.1
310 + * @param mixed $new_value Value to update data with.
311 + * @param bool $single Whether data is an array (add_metadata).
312 + * @return mixed
313 + */
314 + public function update_data( $new_value, $single = true ) {
315 + $a = $this->data_args( array(
316 + 'single' => $single,
317 + ) );
318 +
319 + $a['value'] = $a['repeat'] ? array_values( $new_value ) : $new_value;
320 +
321 + /**
322 + * Filter whether to override saving of meta value.
323 + * Returning a non-null value will effectively short-circuit the function.
324 + *
325 + * @since 2.0.0
326 + *
327 + * @param null|bool $check Whether to allow updating metadata for the given type.
328 + *
329 + * @param array $args {
330 + * Array of data about current field including:
331 + *
332 + * @type string $value The value to set
333 + * @type string $type The current object type
334 + * @type int $id The current object ID
335 + * @type string $field_id The ID of the field being updated
336 + * @type bool $repeat Whether current field is repeatable
337 + * @type bool $single Whether current field is a single database row
338 + * }
339 + *
340 + * @param array $field_args All field arguments
341 + *
342 + * @param CMB2_Field object $field This field object
343 + */
344 + $override = apply_filters( 'cmb2_override_meta_save', null, $a, $this->args(), $this );
345 +
346 + /**
347 + * Filter and parameters are documented for 'cmb2_override_meta_save' filter (above).
348 + *
349 + * The dynamic portion of the hook, $a['field_id'], refers to the current
350 + * field id paramater. Returning a non-null value
351 + * will effectively short-circuit the function.
352 + *
353 + * @since 2.0.0
354 + */
355 + $override = apply_filters( "cmb2_override_{$a['field_id']}_meta_save", $override, $a, $this->args(), $this );
356 +
357 + // If override, return that.
358 + if ( null !== $override ) {
359 + return $override;
360 + }
361 +
362 + // Options page handling (or temp data store).
363 + if ( 'options-page' === $a['type'] || empty( $a['id'] ) ) {
364 + return cmb2_options( $a['id'] )->update( $a['field_id'], $a['value'], false, $a['single'] );
365 + }
366 +
367 + // Add metadata if not single.
368 + if ( ! $a['single'] ) {
369 + return add_metadata( $a['type'], $a['id'], $a['field_id'], $a['value'], false );
370 + }
371 +
372 + // Delete meta if we have an empty array.
373 + if ( is_array( $a['value'] ) && empty( $a['value'] ) ) {
374 + return delete_metadata( $a['type'], $a['id'], $a['field_id'], $this->value );
375 + }
376 +
377 + // Update metadata.
378 + return update_metadata( $a['type'], $a['id'], $a['field_id'], $a['value'] );
379 + }
380 +
381 + /**
382 + * Removes/updates metadata/option data.
383 + *
384 + * @since 1.0.1
385 + * @param string $old Old value.
386 + * @return mixed
387 + */
388 + public function remove_data( $old = '' ) {
389 + $a = $this->data_args( array(
390 + 'old' => $old,
391 + ) );
392 +
393 + /**
394 + * Filter whether to override removing of meta value.
395 + * Returning a non-null value will effectively short-circuit the function.
396 + *
397 + * @since 2.0.0
398 + *
399 + * @param null|bool $delete Whether to allow metadata deletion of the given type.
400 + * @param array $args Array of data about current field including:
401 + * 'type' : Current object type
402 + * 'id' : Current object ID
403 + * 'field_id' : Current Field ID
404 + * 'repeat' : Whether current field is repeatable
405 + * 'single' : Whether to save as a
406 + * single meta value
407 + * @param array $field_args All field arguments
408 + * @param CMB2_Field object $field This field object
409 + */
410 + $override = apply_filters( 'cmb2_override_meta_remove', null, $a, $this->args(), $this );
411 +
412 + /**
413 + * Filter whether to override removing of meta value.
414 + *
415 + * The dynamic portion of the hook, $a['field_id'], refers to the current
416 + * field id paramater. Returning a non-null value
417 + * will effectively short-circuit the function.
418 + *
419 + * @since 2.0.0
420 + *
421 + * @param null|bool $delete Whether to allow metadata deletion of the given type.
422 + * @param array $args Array of data about current field including:
423 + * 'type' : Current object type
424 + * 'id' : Current object ID
425 + * 'field_id' : Current Field ID
426 + * 'repeat' : Whether current field is repeatable
427 + * 'single' : Whether to save as a
428 + * single meta value
429 + * @param array $field_args All field arguments
430 + * @param CMB2_Field object $field This field object
431 + */
432 + $override = apply_filters( "cmb2_override_{$a['field_id']}_meta_remove", $override, $a, $this->args(), $this );
433 +
434 + // If no override, remove as usual.
435 + if ( null !== $override ) {
436 + return $override;
437 + } elseif ( 'options-page' === $a['type'] || empty( $a['id'] ) ) {
438 + // Option page handling.
439 + return cmb2_options( $a['id'] )->remove( $a['field_id'] );
440 + }
441 +
442 + // Remove metadata.
443 + return delete_metadata( $a['type'], $a['id'], $a['field_id'], $old );
444 + }
445 +
446 + /**
447 + * Data variables for get/set data methods
448 + *
449 + * @since 1.1.0
450 + * @param array $args Override arguments.
451 + * @return array Updated arguments
452 + */
453 + public function data_args( $args = array() ) {
454 + $args = wp_parse_args( $args, array(
455 + 'type' => $this->object_type,
456 + 'id' => $this->object_id,
457 + 'field_id' => $this->id( true ),
458 + 'repeat' => $this->args( 'repeatable' ),
459 + 'single' => ! $this->args( 'multiple' ),
460 + ) );
461 + return $args;
462 + }
463 +
464 + /**
465 + * Checks if field has a registered sanitization callback
466 + *
467 + * @since 1.0.1
468 + * @param mixed $meta_value Meta value.
469 + * @return mixed Possibly sanitized meta value
470 + */
471 + public function sanitization_cb( $meta_value ) {
472 +
473 + if ( $this->args( 'repeatable' ) && is_array( $meta_value ) ) {
474 + // Remove empties.
475 + $meta_value = array_filter( $meta_value );
476 + }
477 +
478 + // Check if the field has a registered validation callback.
479 + $cb = $this->maybe_callback( 'sanitization_cb' );
480 + if ( false === $cb ) {
481 + // If requesting NO validation, return meta value.
482 + return $meta_value;
483 + } elseif ( $cb ) {
484 + // Ok, callback is good, let's run it.
485 + return call_user_func( $cb, $meta_value, $this->args(), $this );
486 + }
487 +
488 + $sanitizer = new CMB2_Sanitize( $this, $meta_value );
489 + $field_type = $this->type();
490 +
491 + /**
492 + * Filter the value before it is saved.
493 + *
494 + * The dynamic portion of the hook name, $field_type, refers to the field type.
495 + *
496 + * Passing a non-null value to the filter will short-circuit saving
497 + * the field value, saving the passed value instead.
498 + *
499 + * @param bool|mixed $override_value Sanitization/Validation override value to return.
500 + * Default: null. false to skip it.
501 + * @param mixed $value The value to be saved to this field.
502 + * @param int $object_id The ID of the object where the value will be saved
503 + * @param array $field_args The current field's arguments
504 + * @param object $sanitizer This `CMB2_Sanitize` object
505 + */
506 + $override_value = apply_filters( "cmb2_sanitize_{$field_type}", null, $sanitizer->value, $this->object_id, $this->args(), $sanitizer );
507 +
508 + if ( null !== $override_value ) {
509 + return $override_value;
510 + }
511 +
512 + // Sanitization via 'CMB2_Sanitize'.
513 + return $sanitizer->{$field_type}();
514 + }
515 +
516 + /**
517 + * Process $_POST data to save this field's value
518 + *
519 + * @since 2.0.3
520 + * @param array $data_to_save $_POST data to check.
521 + * @return array|int|bool Result of save, false on failure
522 + */
523 + public function save_field_from_data( array $data_to_save ) {
524 + $this->data_to_save = $data_to_save;
525 +
526 + $meta_value = isset( $this->data_to_save[ $this->id( true ) ] )
527 + ? $this->data_to_save[ $this->id( true ) ]
528 + : null;
529 +
530 + return $this->save_field( $meta_value );
531 + }
532 +
533 + /**
534 + * Sanitize/store a value to this field
535 + *
536 + * @since 2.0.0
537 + * @param array $meta_value Desired value to sanitize/store.
538 + * @return array|int|bool Result of save. false on failure
539 + */
540 + public function save_field( $meta_value ) {
541 +
542 + $updated = false;
543 + $action = '';
544 + $new_value = $this->sanitization_cb( $meta_value );
545 +
546 + if ( ! $this->args( 'save_field' ) ) {
547 +
548 + // Nothing to see here.
549 + $action = 'disabled';
550 +
551 + } elseif ( $this->args( 'multiple' ) && ! $this->args( 'repeatable' ) && ! $this->group ) {
552 +
553 + $this->remove_data();
554 + $count = 0;
555 +
556 + if ( ! empty( $new_value ) ) {
557 + foreach ( $new_value as $add_new ) {
558 + if ( $this->update_data( $add_new, false ) ) {
559 + $count++;
560 + }
561 + }
562 + }
563 +
564 + $updated = $count ? $count : false;
565 + $action = 'repeatable';
566 +
567 + } elseif ( ! CMB2_Utils::isempty( $new_value ) && $new_value !== $this->get_data() ) {
568 + $updated = $this->update_data( $new_value );
569 + $action = 'updated';
570 + } elseif ( CMB2_Utils::isempty( $new_value ) ) {
571 + $updated = $this->remove_data();
572 + $action = 'removed';
573 + }
574 +
575 + if ( $updated ) {
576 + $this->value = $this->get_data();
577 + $this->escaped_value = null;
578 + }
579 +
580 + $field_id = $this->id( true );
581 +
582 + /**
583 + * Hooks after save field action.
584 + *
585 + * @since 2.2.0
586 + *
587 + * @param string $field_id the current field id paramater.
588 + * @param bool $updated Whether the metadata update action occurred.
589 + * @param string $action Action performed. Could be "repeatable", "updated", or "removed".
590 + * @param CMB2_Field object $field This field object
591 + */
592 + do_action( 'cmb2_save_field', $field_id, $updated, $action, $this );
593 +
594 + /**
595 + * Hooks after save field action.
596 + *
597 + * The dynamic portion of the hook, $field_id, refers to the
598 + * current field id paramater.
599 + *
600 + * @since 2.2.0
601 + *
602 + * @param bool $updated Whether the metadata update action occurred.
603 + * @param string $action Action performed. Could be "repeatable", "updated", or "removed".
604 + * @param CMB2_Field object $field This field object
605 + */
606 + do_action( "cmb2_save_field_{$field_id}", $updated, $action, $this );
607 +
608 + return $updated;
609 + }
610 +
611 + /**
612 + * Determine if current type is exempt from escaping
613 + *
614 + * @since 1.1.0
615 + * @return bool True if exempt
616 + */
617 + public function escaping_exception() {
618 + // These types cannot be escaped.
619 + return in_array( $this->type(), array(
620 + 'file_list',
621 + 'multicheck',
622 + 'text_datetime_timestamp_timezone',
623 + ) );
624 + }
625 +
626 + /**
627 + * Determine if current type cannot be repeatable
628 + *
629 + * @since 1.1.0
630 + * @param string $type Field type to check.
631 + * @return bool True if type cannot be repeatable
632 + */
633 + public function repeatable_exception( $type ) {
634 + // These types cannot be repeatable.
635 + $internal_fields = array(
636 + // Use file_list instead.
637 + 'file' => 1,
638 + 'radio' => 1,
639 + 'title' => 1,
640 + 'wysiwyg' => 1,
641 + 'checkbox' => 1,
642 + 'radio_inline' => 1,
643 + 'taxonomy_radio' => 1,
644 + 'taxonomy_radio_inline' => 1,
645 + 'taxonomy_radio_hierarchical' => 1,
646 + 'taxonomy_select' => 1,
647 + 'taxonomy_select_hierarchical' => 1,
648 + 'taxonomy_multicheck' => 1,
649 + 'taxonomy_multicheck_inline' => 1,
650 + 'taxonomy_multicheck_hierarchical' => 1,
651 + );
652 +
653 + /**
654 + * Filter field types that are non-repeatable.
655 + *
656 + * Note that this does *not* allow overriding the default non-repeatable types.
657 + *
658 + * @since 2.1.1
659 + *
660 + * @param array $fields Array of fields designated as non-repeatable. Note that the field names are *keys*,
661 + * and not values. The value can be anything, because it is meaningless. Example:
662 + * array( 'my_custom_field' => 1 )
663 + */
664 + $all_fields = array_merge( apply_filters( 'cmb2_non_repeatable_fields', array() ), $internal_fields );
665 + return isset( $all_fields[ $type ] );
666 + }
667 +
668 + /**
669 + * Determine if current type has its own defaults field-arguments method.
670 + *
671 + * @since 2.2.6
672 + * @param string $type Field type to check.
673 + * @return bool True if has own method.
674 + */
675 + public function has_args_method( $type ) {
676 +
677 + // These types have their own arguments parser.
678 + $type_methods = array(
679 + 'group' => 'set_field_defaults_group',
680 + 'wysiwyg' => 'set_field_defaults_wysiwyg',
681 + );
682 +
683 + if ( isset( $type_methods[ $type ] ) ) {
684 + return $type_methods[ $type ];
685 + }
686 +
687 + $all_or_nothing_types = array_flip( apply_filters( 'cmb2_all_or_nothing_types', array(
688 + 'select',
689 + 'radio',
690 + 'radio_inline',
691 + 'taxonomy_select',
692 + 'taxonomy_select_hierarchical',
693 + 'taxonomy_radio',
694 + 'taxonomy_radio_inline',
695 + 'taxonomy_radio_hierarchical',
696 + ), $this ) );
697 +
698 + if ( isset( $all_or_nothing_types[ $type ] ) ) {
699 + return 'set_field_defaults_all_or_nothing_types';
700 + }
701 +
702 + return false;
703 + }
704 +
705 + /**
706 + * Escape the value before output. Defaults to 'esc_attr()'
707 + *
708 + * @since 1.0.1
709 + * @param callable|string $func Escaping function (if not esc_attr()).
710 + * @param mixed $meta_value Meta value.
711 + * @return mixed Final value.
712 + */
713 + public function escaped_value( $func = 'esc_attr', $meta_value = '' ) {
714 +
715 + if ( null !== $this->escaped_value ) {
716 + return $this->escaped_value;
717 + }
718 +
719 + $meta_value = $meta_value ? $meta_value : $this->value();
720 +
721 + // Check if the field has a registered escaping callback.
722 + if ( $cb = $this->maybe_callback( 'escape_cb' ) ) {
723 + // Ok, callback is good, let's run it.
724 + return call_user_func( $cb, $meta_value, $this->args(), $this );
725 + }
726 +
727 + $field_type = $this->type();
728 +
729 + /**
730 + * Filter the value for escaping before it is ouput.
731 + *
732 + * The dynamic portion of the hook name, $field_type, refers to the field type.
733 + *
734 + * Passing a non-null value to the filter will short-circuit the built-in
735 + * escaping for this field.
736 + *
737 + * @param bool|mixed $override_value Escaping override value to return.
738 + * Default: null. false to skip it.
739 + * @param mixed $meta_value The value to be output.
740 + * @param array $field_args The current field's arguments.
741 + * @param object $field This `CMB2_Field` object.
742 + */
743 + $esc = apply_filters( "cmb2_types_esc_{$field_type}", null, $meta_value, $this->args(), $this );
744 + if ( null !== $esc ) {
745 + return $esc;
746 + }
747 +
748 + if ( false === $cb || $this->escaping_exception() ) {
749 + // If requesting NO escaping, return meta value.
750 + return $this->val_or_default( $meta_value );
751 + }
752 +
753 + // escaping function passed in?
754 + $func = $func ? $func : 'esc_attr';
755 + $meta_value = $this->val_or_default( $meta_value );
756 +
757 + if ( is_array( $meta_value ) ) {
758 + foreach ( $meta_value as $key => $value ) {
759 + $meta_value[ $key ] = call_user_func( $func, $value );
760 + }
761 + } else {
762 + $meta_value = call_user_func( $func, $meta_value );
763 + }
764 +
765 + $this->escaped_value = $meta_value;
766 + return $this->escaped_value;
767 + }
768 +
769 + /**
770 + * Return non-empty value or field default if value IS empty
771 + *
772 + * @since 2.0.0
773 + * @param mixed $meta_value Field value.
774 + * @return mixed Field value, or default value
775 + */
776 + public function val_or_default( $meta_value ) {
777 + return ! CMB2_Utils::isempty( $meta_value ) ? $meta_value : $this->get_default();
778 + }
779 +
780 + /**
781 + * Offset a time value based on timezone
782 + *
783 + * @since 1.0.0
784 + * @return string Offset time string
785 + */
786 + public function field_timezone_offset() {
787 + return CMB2_Utils::timezone_offset( $this->field_timezone() );
788 + }
789 +
790 + /**
791 + * Return timezone string
792 + *
793 + * @since 1.0.0
794 + * @return string Timezone string
795 + */
796 + public function field_timezone() {
797 + $value = '';
798 +
799 + // Is timezone arg set?
800 + if ( $this->args( 'timezone' ) ) {
801 + $value = $this->args( 'timezone' );
802 + } elseif ( $this->args( 'timezone_meta_key' ) ) {
803 + // Is there another meta key with a timezone stored as its value we should use?
804 + $value = $this->get_data( $this->args( 'timezone_meta_key' ) );
805 + }
806 +
807 + return $value;
808 + }
809 +
810 + /**
811 + * Format the timestamp field value based on the field date/time format arg
812 + *
813 + * @since 2.0.0
814 + * @param int $meta_value Timestamp.
815 + * @param string $format Either date_format or time_format.
816 + * @return string Formatted date
817 + */
818 + public function format_timestamp( $meta_value, $format = 'date_format' ) {
819 + return date( stripslashes( $this->args( $format ) ), $meta_value );
820 + }
821 +
822 + /**
823 + * Return a formatted timestamp for a field
824 + *
825 + * @since 2.0.0
826 + * @param string $format Either date_format or time_format.
827 + * @param string|int $meta_value Optional meta value to check.
828 + * @return string Formatted date
829 + */
830 + public function get_timestamp_format( $format = 'date_format', $meta_value = 0 ) {
831 + $meta_value = $meta_value ? $meta_value : $this->escaped_value();
832 + if ( empty( $meta_value ) ) {
833 + $meta_value = $this->get_default();
834 + }
835 +
836 + $meta_value = CMB2_Utils::make_valid_time_stamp( $meta_value );
837 + if ( empty( $meta_value ) ) {
838 + return '';
839 + }
840 +
841 + return is_array( $meta_value )
842 + ? array_map( array( $this, 'format_timestamp' ), $meta_value, $format )
843 + : $this->format_timestamp( $meta_value, $format );
844 + }
845 +
846 + /**
847 + * Get timestamp from text date
848 + *
849 + * @since 2.2.0
850 + * @param string $value Date value.
851 + * @return mixed Unix timestamp representing the date.
852 + */
853 + public function get_timestamp_from_value( $value ) {
854 + $timestamp = CMB2_Utils::get_timestamp_from_value( $value, $this->args( 'date_format' ) );
855 + if ( empty( $timestamp ) && CMB2_Utils::is_valid_date( $value ) ) {
856 + $timestamp = CMB2_Utils::make_valid_time_stamp( $value );
857 + }
858 +
859 + return $timestamp;
860 + }
861 +
862 + /**
863 + * Get field render callback and Render the field row
864 + *
865 + * @since 1.0.0
866 + */
867 + public function render_field() {
868 + $this->render_context = 'edit';
869 +
870 + $this->peform_param_callback( 'render_row_cb' );
871 +
872 + // For chaining.
873 + return $this;
874 + }
875 +
876 + /**
877 + * Default field render callback
878 + *
879 + * @since 2.1.1
880 + */
881 + public function render_field_callback() {
882 +
883 + // If field is requesting to not be shown on the front-end.
884 + if ( ! is_admin() && ! $this->args( 'on_front' ) ) {
885 + return;
886 + }
887 +
888 + // If field is requesting to be conditionally shown.
889 + if ( ! $this->should_show() ) {
890 + return;
891 + }
892 +
893 + $field_type = $this->type();
894 +
895 + /**
896 + * Hook before field row begins.
897 + *
898 + * @param CMB2_Field $field The current field object.
899 + */
900 + do_action( 'cmb2_before_field_row', $this );
901 +
902 + /**
903 + * Hook before field row begins.
904 + *
905 + * The dynamic portion of the hook name, $field_type, refers to the field type.
906 + *
907 + * @param CMB2_Field $field The current field object.
908 + */
909 + do_action( "cmb2_before_{$field_type}_field_row", $this );
910 +
911 + $this->peform_param_callback( 'before_row' );
912 +
913 + printf( "<div class=\"cmb-row %s\" data-fieldtype=\"%s\">\n", $this->row_classes(), $field_type );
914 +
915 + if ( ! $this->args( 'show_names' ) ) {
916 + echo "\n\t<div class=\"cmb-td\">\n";
917 +
918 + $this->peform_param_callback( 'label_cb' );
919 +
920 + } else {
921 +
922 + if ( $this->get_param_callback_result( 'label_cb' ) ) {
923 + echo '<div class="cmb-th">', $this->peform_param_callback( 'label_cb' ), '</div>';
924 + }
925 +
926 + echo "\n\t<div class=\"cmb-td\">\n";
927 + }
928 +
929 + $this->peform_param_callback( 'before' );
930 +
931 + $types = new CMB2_Types( $this );
932 + $types->render();
933 +
934 + $this->peform_param_callback( 'after' );
935 +
936 + echo "\n\t</div>\n</div>";
937 +
938 + $this->peform_param_callback( 'after_row' );
939 +
940 + /**
941 + * Hook after field row ends.
942 + *
943 + * The dynamic portion of the hook name, $field_type, refers to the field type.
944 + *
945 + * @param CMB2_Field $field The current field object.
946 + */
947 + do_action( "cmb2_after_{$field_type}_field_row", $this );
948 +
949 + /**
950 + * Hook after field row ends.
951 + *
952 + * @param CMB2_Field $field The current field object.
953 + */
954 + do_action( 'cmb2_after_field_row', $this );
955 +
956 + // For chaining.
957 + return $this;
958 + }
959 +
960 + /**
961 + * The default label_cb callback (if not a title field)
962 + *
963 + * @since 2.1.1
964 + * @return string Label html markup.
965 + */
966 + public function label() {
967 + if ( ! $this->args( 'name' ) ) {
968 + return '';
969 + }
970 +
971 + $style = ! $this->args( 'show_names' ) ? ' style="display:none;"' : '';
972 +
973 + return sprintf( "\n" . '<label%1$s for="%2$s">%3$s</label>' . "\n", $style, $this->id(), $this->args( 'name' ) );
974 + }
975 +
976 + /**
977 + * Defines the classes for the current CMB2 field row
978 + *
979 + * @since 2.0.0
980 + * @return string Space concatenated list of classes
981 + */
982 + public function row_classes() {
983 +
984 + $classes = array();
985 +
986 + /**
987 + * By default, 'text_url' and 'text' fields get table-like styling
988 + *
989 + * @since 2.0.0
990 + *
991 + * @param array $field_types The types of fields which should get the 'table-layout' class
992 + */
993 + $repeat_table_rows_types = apply_filters( 'cmb2_repeat_table_row_types', array(
994 + 'text_url',
995 + 'text',
996 + ) );
997 +
998 + $conditional_classes = array(
999 + 'cmb-type-' . str_replace( '_', '-', sanitize_html_class( $this->type() ) ) => true,
1000 + 'cmb2-id-' . str_replace( '_', '-', sanitize_html_class( $this->id() ) ) => true,
1001 + 'cmb-repeat' => $this->args( 'repeatable' ),
1002 + 'cmb-repeat-group-field' => $this->group,
1003 + 'cmb-inline' => $this->args( 'inline' ),
1004 + 'table-layout' => 'edit' === $this->render_context && in_array( $this->type(), $repeat_table_rows_types ),
1005 + );
1006 +
1007 + foreach ( $conditional_classes as $class => $condition ) {
1008 + if ( $condition ) {
1009 + $classes[] = $class;
1010 + }
1011 + }
1012 +
1013 + if ( $added_classes = $this->args( 'classes' ) ) {
1014 + $added_classes = is_array( $added_classes ) ? implode( ' ', $added_classes ) : (string) $added_classes;
1015 + } elseif ( $added_classes = $this->get_param_callback_result( 'classes_cb' ) ) {
1016 + $added_classes = is_array( $added_classes ) ? implode( ' ', $added_classes ) : (string) $added_classes;
1017 + }
1018 +
1019 + if ( $added_classes ) {
1020 + $classes[] = esc_attr( $added_classes );
1021 + }
1022 +
1023 + /**
1024 + * Globally filter row classes
1025 + *
1026 + * @since 2.0.0
1027 + *
1028 + * @param string $classes Space-separated list of row classes
1029 + * @param CMB2_Field object $field This field object
1030 + */
1031 + return apply_filters( 'cmb2_row_classes', implode( ' ', $classes ), $this );
1032 + }
1033 +
1034 + /**
1035 + * Get field display callback and render the display value in the column.
1036 + *
1037 + * @since 2.2.2
1038 + */
1039 + public function render_column() {
1040 + $this->render_context = 'display';
1041 +
1042 + $this->peform_param_callback( 'display_cb' );
1043 +
1044 + // For chaining.
1045 + return $this;
1046 + }
1047 +
1048 + /**
1049 + * The method to fetch the value for this field for the REST API.
1050 + *
1051 + * @since 2.5.0
1052 + */
1053 + public function get_rest_value() {
1054 + $field_type = $this->type();
1055 + $field_id = $this->id( true );
1056 +
1057 + if ( $cb = $this->maybe_callback( 'rest_value_cb' ) ) {
1058 + add_filter( "cmb2_get_rest_value_for_{$field_id}", $cb, 99 );
1059 + }
1060 +
1061 + $value = $this->get_data();
1062 +
1063 + /**
1064 + * Filter the value before it is sent to the REST request.
1065 + *
1066 + * @since 2.5.0
1067 + *
1068 + * @param mixed $value The value from CMB2_Field::get_data()
1069 + * @param CMB2_Field $field This field object.
1070 + */
1071 + $value = apply_filters( 'cmb2_get_rest_value', $value, $this );
1072 +
1073 + /**
1074 + * Filter the value before it is sent to the REST request.
1075 + *
1076 + * The dynamic portion of the hook name, $field_type, refers to the field type.
1077 + *
1078 + * @since 2.5.0
1079 + *
1080 + * @param mixed $value The value from CMB2_Field::get_data()
1081 + * @param CMB2_Field $field This field object.
1082 + */
1083 + $value = apply_filters( "cmb2_get_rest_value_{$field_type}", $value, $this );
1084 +
1085 + /**
1086 + * Filter the value before it is sent to the REST request.
1087 + *
1088 + * The dynamic portion of the hook name, $field_id, refers to the field id.
1089 + *
1090 + * @since 2.5.0
1091 + *
1092 + * @param mixed $value The value from CMB2_Field::get_data()
1093 + * @param CMB2_Field $field This field object.
1094 + */
1095 + return apply_filters( "cmb2_get_rest_value_for_{$field_id}", $value, $this );
1096 + }
1097 +
1098 + /**
1099 + * Get a field object for a supporting field. (e.g. file field)
1100 + *
1101 + * @since 2.7.0
1102 + *
1103 + * @return CMB2_Field|bool Supporting field object, if supported.
1104 + */
1105 + public function get_supporting_field() {
1106 + $suffix = $this->args( 'has_supporting_data' );
1107 + if ( empty( $suffix ) ) {
1108 + return false;
1109 + }
1110 +
1111 + return $this->get_field_clone( array(
1112 + 'id' => $this->_id( '', false ) . $suffix,
1113 + 'sanitization_cb' => false,
1114 + ) );
1115 + }
1116 +
1117 + /**
1118 + * Default callback to outputs field value in a display format.
1119 + *
1120 + * @since 2.2.2
1121 + */
1122 + public function display_value_callback() {
1123 + // If field is requesting to be conditionally shown.
1124 + if ( ! $this->should_show() ) {
1125 + return;
1126 + }
1127 +
1128 + $display = new CMB2_Field_Display( $this );
1129 + $field_type = $this->type();
1130 +
1131 + /**
1132 + * A filter to bypass the default display.
1133 + *
1134 + * The dynamic portion of the hook name, $field_type, refers to the field type.
1135 + *
1136 + * Passing a non-null value to the filter will short-circuit the default display.
1137 + *
1138 + * @param bool|mixed $pre_output Default null value.
1139 + * @param CMB2_Field $field This field object.
1140 + * @param CMB2_Field_Display $display The `CMB2_Field_Display` object.
1141 + */
1142 + $pre_output = apply_filters( "cmb2_pre_field_display_{$field_type}", null, $this, $display );
1143 +
1144 + if ( null !== $pre_output ) {
1145 + echo $pre_output;
1146 + return;
1147 + }
1148 +
1149 + $this->peform_param_callback( 'before_display_wrap' );
1150 +
1151 + printf( "<div class=\"cmb-column %s\" data-fieldtype=\"%s\">\n", $this->row_classes(), $field_type );
1152 +
1153 + $this->peform_param_callback( 'before_display' );
1154 +
1155 + CMB2_Field_Display::get( $this )->display();
1156 +
1157 + $this->peform_param_callback( 'after_display' );
1158 +
1159 + echo "\n</div>";
1160 +
1161 + $this->peform_param_callback( 'after_display_wrap' );
1162 +
1163 + // For chaining.
1164 + return $this;
1165 + }
1166 +
1167 + /**
1168 + * Replaces a hash key - {#} - with the repeatable index
1169 + *
1170 + * @since 1.2.0
1171 + * @param string $value Value to update.
1172 + * @return string Updated value
1173 + */
1174 + public function replace_hash( $value ) {
1175 + // Replace hash with 1 based count.
1176 + return str_replace( '{#}', ( $this->index + 1 ), $value );
1177 + }
1178 +
1179 + /**
1180 + * Retrieve text parameter from field's text array (if it has one), or use fallback text
1181 + * For back-compatibility, falls back to checking the options array.
1182 + *
1183 + * @since 2.2.2
1184 + * @param string $text_key Key in field's text array.
1185 + * @param string $fallback Fallback text.
1186 + * @return string Text
1187 + */
1188 + public function get_string( $text_key, $fallback ) {
1189 + // If null, populate with our field strings values.
1190 + if ( null === $this->strings ) {
1191 + $this->strings = (array) $this->args['text'];
1192 +
1193 + if ( is_callable( $this->args['text_cb'] ) ) {
1194 + $strings = call_user_func( $this->args['text_cb'], $this );
1195 +
1196 + if ( $strings && is_array( $strings ) ) {
1197 + $this->strings += $strings;
1198 + }
1199 + }
1200 + }
1201 +
1202 + // If we have that string value, send it back.
1203 + if ( isset( $this->strings[ $text_key ] ) ) {
1204 + return $this->strings[ $text_key ];
1205 + }
1206 +
1207 + // Check options for back-compat.
1208 + $string = $this->options( $text_key );
1209 +
1210 + return $string ? $string : $fallback;
1211 + }
1212 +
1213 + /**
1214 + * Retrieve options args.
1215 + *
1216 + * @since 2.0.0
1217 + * @param string $key Specific option to retrieve.
1218 + * @return array|mixed Array of options or specific option.
1219 + */
1220 + public function options( $key = '' ) {
1221 + if ( empty( $this->field_options ) ) {
1222 + $this->set_options();
1223 + }
1224 +
1225 + if ( $key ) {
1226 + return array_key_exists( $key, $this->field_options ) ? $this->field_options[ $key ] : false;
1227 + }
1228 +
1229 + return $this->field_options;
1230 + }
1231 +
1232 + /**
1233 + * Generates/sets options args. Calls options_cb if it exists.
1234 + *
1235 + * @since 2.2.5
1236 + *
1237 + * @return array Array of options
1238 + */
1239 + public function set_options() {
1240 + $this->field_options = (array) $this->args['options'];
1241 +
1242 + if ( is_callable( $this->args['options_cb'] ) ) {
1243 + $options = call_user_func( $this->args['options_cb'], $this );
1244 +
1245 + if ( $options && is_array( $options ) ) {
1246 + $this->field_options = $options + $this->field_options;
1247 + }
1248 + }
1249 +
1250 + return $this->field_options;
1251 + }
1252 +
1253 + /**
1254 + * Store JS dependencies as part of the field args.
1255 + *
1256 + * @since 2.2.0
1257 + * @param array $dependencies Dependies to register for this field.
1258 + */
1259 + public function add_js_dependencies( $dependencies = array() ) {
1260 + foreach ( (array) $dependencies as $dependency ) {
1261 + $this->args['js_dependencies'][ $dependency ] = $dependency;
1262 + }
1263 +
1264 + CMB2_JS::add_dependencies( $dependencies );
1265 + }
1266 +
1267 + /**
1268 + * Send field data to JS.
1269 + *
1270 + * @since 2.2.0
1271 + */
1272 + public function register_js_data() {
1273 + if ( $this->group ) {
1274 + CMB2_JS::add_field_data( $this->group );
1275 + }
1276 +
1277 + return CMB2_JS::add_field_data( $this );
1278 + }
1279 +
1280 + /**
1281 + * Get an array of some of the field data to be used in the Javascript.
1282 + *
1283 + * @since 2.2.4
1284 + *
1285 + * @return array
1286 + */
1287 + public function js_data() {
1288 + return array(
1289 + 'label' => $this->args( 'name' ),
1290 + 'id' => $this->id( true ),
1291 + 'type' => $this->type(),
1292 + 'hash' => $this->hash_id(),
1293 + 'box' => $this->cmb_id,
1294 + 'id_attr' => $this->id(),
1295 + 'name_attr' => $this->args( '_name' ),
1296 + 'default' => $this->get_default(),
1297 + 'group' => $this->group_id(),
1298 + 'index' => $this->group ? $this->group->index : null,
1299 + );
1300 + }
1301 +
1302 + /**
1303 + * Returns a unique hash representing this field.
1304 + *
1305 + * @since 2.2.4
1306 + *
1307 + * @return string
1308 + */
1309 + public function hash_id() {
1310 + if ( '' === $this->hash_id ) {
1311 + $this->hash_id = CMB2_Utils::generate_hash( $this->cmb_id . '||' . $this->id() );
1312 + }
1313 +
1314 + return $this->hash_id;
1315 + }
1316 +
1317 + /**
1318 + * Gets the id of the group field if this field is part of a group.
1319 + *
1320 + * @since 2.2.4
1321 + *
1322 + * @return string
1323 + */
1324 + public function group_id() {
1325 + return $this->group ? $this->group->id( true ) : '';
1326 + }
1327 +
1328 + /**
1329 + * Get CMB2_Field default value, either from default param or default_cb param.
1330 + *
1331 + * @since 0.2.2
1332 + *
1333 + * @return mixed Default field value
1334 + */
1335 + public function get_default() {
1336 + $default = $this->args['default'];
1337 +
1338 + if ( null !== $default ) {
1339 + return apply_filters( 'cmb2_default_filter', $default, $this );
1340 + }
1341 +
1342 + $param = is_callable( $this->args['default_cb'] ) ? 'default_cb' : 'default';
1343 + $default = $this->args['default'] = $this->get_param_callback_result( $param );
1344 +
1345 + // Allow a filter override of the default value.
1346 + return apply_filters( 'cmb2_default_filter', $this->args['default'], $this );
1347 + }
1348 +
1349 + /**
1350 + * Fills in empty field parameters with defaults
1351 + *
1352 + * @since 1.1.0
1353 + *
1354 + * @param array $args Field config array.
1355 + * @return array Modified field config array.
1356 + */
1357 + public function _set_field_defaults( $args ) {
1358 + $defaults = $this->get_default_field_args( $args );
1359 +
1360 + /**
1361 + * Filter the CMB2 Field defaults.
1362 + *
1363 + * @since 2.6.0
1364 + * @param array $defaults Metabox field config array defaults.
1365 + * @param string $id Field id for the current field to allow for selective filtering.
1366 + * @param string $type Field type for the current field to allow for selective filtering.
1367 + * @param CMB2_Field object $field This field object.
1368 + */
1369 + $defaults = apply_filters( 'cmb2_field_defaults', $defaults, $args['id'], $args['type'], $this );
1370 +
1371 + // Set up blank or default values for empty ones.
1372 + $args = wp_parse_args( $args, $defaults );
1373 +
1374 + /**
1375 + * Filtering the CMB2 Field arguments once merged with the defaults, but before further processing.
1376 + *
1377 + * @since 2.6.0
1378 + * @param array $args Metabox field config array defaults.
1379 + * @param CMB2_Field object $field This field object.
1380 + */
1381 + $args = apply_filters( 'cmb2_field_arguments_raw', $args, $this );
1382 +
1383 + /*
1384 + * Deprecated usage:
1385 + *
1386 + * 'std' -- use 'default' (no longer works)
1387 + * 'row_classes' -- use 'class', or 'class_cb'
1388 + * 'default' -- as callback (use default_cb)
1389 + */
1390 + $args = $this->convert_deprecated_params( $args );
1391 +
1392 + $args['repeatable'] = $args['repeatable'] && ! $this->repeatable_exception( $args['type'] );
1393 + $args['inline'] = $args['inline'] || false !== stripos( $args['type'], '_inline' );
1394 + $args['_id'] = $args['id'];
1395 + $args['_name'] = $args['id'];
1396 +
1397 + if ( $method = $this->has_args_method( $args['type'] ) ) {
1398 + $args = $this->{$method}( $args );
1399 + }
1400 +
1401 + if ( $this->group ) {
1402 + $args = $this->set_group_sub_field_defaults( $args );
1403 + }
1404 +
1405 + $with_supporting = array(
1406 + // CMB2_Sanitize::_save_file_id_value()/CMB2_Sanitize::_get_group_file_value_array().
1407 + 'file' => '_id',
1408 + // See CMB2_Sanitize::_save_utc_value().
1409 + 'text_datetime_timestamp_timezone' => '_utc',
1410 + );
1411 +
1412 + $args['has_supporting_data'] = isset( $with_supporting[ $args['type'] ] )
1413 + ? $with_supporting[ $args['type'] ]
1414 + : false;
1415 +
1416 + // Repeatable fields require jQuery sortable library.
1417 + if ( ! empty( $args['repeatable'] ) ) {
1418 + CMB2_JS::add_dependencies( 'jquery-ui-sortable' );
1419 + }
1420 +
1421 + /**
1422 + * Filter the CMB2 Field arguments after processing.
1423 + *
1424 + * @since 2.6.0
1425 + * @param array $args Metabox field config array after processing.
1426 + * @param CMB2_Field object $field This field object.
1427 + */
1428 + return apply_filters( 'cmb2_field_arguments', $args, $this );
1429 + }
1430 +
1431 + /**
1432 + * Sets default arguments for the group field types.
1433 + *
1434 + * @since 2.2.6
1435 + *
1436 + * @param array $args Field config array.
1437 + * @return array Modified field config array.
1438 + */
1439 + protected function set_field_defaults_group( $args ) {
1440 + $args['options'] = wp_parse_args( $args['options'], array(
1441 + 'add_button' => esc_html__( 'Add Group', 'cmb2' ),
1442 + 'remove_button' => esc_html__( 'Remove Group', 'cmb2' ),
1443 + 'remove_confirm' => '',
1444 + ) );
1445 +
1446 + return $args;
1447 + }
1448 +
1449 + /**
1450 + * Sets default arguments for the wysiwyg field types.
1451 + *
1452 + * @since 2.2.6
1453 + *
1454 + * @param array $args Field config array.
1455 + * @return array Modified field config array.
1456 + */
1457 + protected function set_field_defaults_wysiwyg( $args ) {
1458 + $args['id'] = strtolower( str_ireplace( '-', '_', $args['id'] ) );
1459 + $args['options']['textarea_name'] = $args['_name'];
1460 +
1461 + return $args;
1462 + }
1463 +
1464 + /**
1465 + * Sets default arguments for the all-or-nothing field types.
1466 + *
1467 + * @since 2.2.6
1468 + *
1469 + * @param array $args Field config array.
1470 + * @return array Modified field config array.
1471 + */
1472 + protected function set_field_defaults_all_or_nothing_types( $args ) {
1473 + $args['show_option_none'] = isset( $args['show_option_none'] ) ? $args['show_option_none'] : null;
1474 + $args['show_option_none'] = true === $args['show_option_none'] ? esc_html__( 'None', 'cmb2' ) : $args['show_option_none'];
1475 +
1476 + if ( null === $args['show_option_none'] ) {
1477 + $off_by_default = in_array( $args['type'], array( 'select', 'radio', 'radio_inline' ), true );
1478 + $args['show_option_none'] = $off_by_default ? false : esc_html__( 'None', 'cmb2' );
1479 + }
1480 +
1481 + return $args;
1482 + }
1483 +
1484 + /**
1485 + * Sets default arguments for group sub-fields.
1486 + *
1487 + * @since 2.2.6
1488 + *
1489 + * @param array $args Field config array.
1490 + * @return array Modified field config array.
1491 + */
1492 + protected function set_group_sub_field_defaults( $args ) {
1493 + $args['id'] = $this->group->args( 'id' ) . '_' . $this->group->index . '_' . $args['id'];
1494 + $args['_name'] = $this->group->args( 'id' ) . '[' . $this->group->index . '][' . $args['_name'] . ']';
1495 +
1496 + return $args;
1497 + }
1498 +
1499 + /**
1500 + * Gets the default arguments for all fields.
1501 + *
1502 + * @since 2.2.6
1503 + *
1504 + * @param array $args Field config array.
1505 + * @return array Field defaults.
1506 + */
1507 + protected function get_default_field_args( $args ) {
1508 + $type = isset( $args['type'] ) ? $args['type'] : '';
1509 +
1510 + return array(
1511 + 'type' => $type,
1512 + 'name' => '',
1513 + 'desc' => '',
1514 + 'before' => '',
1515 + 'after' => '',
1516 + 'options' => array(),
1517 + 'options_cb' => '',
1518 + 'text' => array(),
1519 + 'text_cb' => '',
1520 + 'attributes' => array(),
1521 + 'protocols' => null,
1522 + 'default' => null,
1523 + 'default_cb' => '',
1524 + 'classes' => null,
1525 + 'classes_cb' => '',
1526 + 'select_all_button' => true,
1527 + 'multiple' => false,
1528 + 'repeatable' => 'group' === $type,
1529 + 'inline' => false,
1530 + 'on_front' => true,
1531 + 'show_names' => true,
1532 + 'save_field' => true, // Will not save if false.
1533 + 'date_format' => 'm\/d\/Y',
1534 + 'time_format' => 'h:i A',
1535 + 'description' => isset( $args['desc'] ) ? $args['desc'] : '',
1536 + 'preview_size' => 'file' === $type ? array( 350, 350 ) : array( 50, 50 ),
1537 + 'render_row_cb' => array( $this, 'render_field_callback' ),
1538 + 'display_cb' => array( $this, 'display_value_callback' ),
1539 + 'label_cb' => 'title' !== $type ? array( $this, 'label' ) : '',
1540 + 'column' => false,
1541 + 'js_dependencies' => array(),
1542 + 'show_in_rest' => null,
1543 + 'char_counter' => false,
1544 + 'char_max' => false,
1545 + 'char_max_enforce' => false,
1546 + );
1547 + }
1548 +
1549 + /**
1550 + * Get default field arguments specific to this CMB2 object.
1551 + *
1552 + * @since 2.2.0
1553 + * @param array $field_args Metabox field config array.
1554 + * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent).
1555 + * @return array Array of field arguments.
1556 + */
1557 + protected function get_default_args( $field_args, $field_group = null ) {
1558 + $args = parent::get_default_args( array(), $this->group );
1559 +
1560 + if ( isset( $field_args['field_args'] ) ) {
1561 + $args = wp_parse_args( $field_args, $args );
1562 + } else {
1563 + $args['field_args'] = wp_parse_args( $field_args, $this->args );
1564 + }
1565 +
1566 + return $args;
1567 + }
1568 +
1569 + /**
1570 + * Returns a cloned version of this field object, but with
1571 + * modified/overridden field arguments.
1572 + *
1573 + * @since 2.2.2
1574 + * @param array $field_args Array of field arguments, or entire array of
1575 + * arguments for CMB2_Field.
1576 + *
1577 + * @return CMB2_Field The new CMB2_Field instance.
1578 + */
1579 + public function get_field_clone( $field_args ) {
1580 + return $this->get_new_field( $field_args );
1581 + }
1582 +
1583 + /**
1584 + * Returns the CMB2 instance this field is registered to.
1585 + *
1586 + * @since 2.2.2
1587 + *
1588 + * @return CMB2|WP_Error If new CMB2_Field is called without cmb_id arg, returns error.
1589 + */
1590 + public function get_cmb() {
1591 + if ( ! $this->cmb_id ) {
1592 + return new WP_Error( 'no_cmb_id', esc_html__( 'Sorry, this field does not have a cmb_id specified.', 'cmb2' ) );
1593 + }
1594 +
1595 + return cmb2_get_metabox( $this->cmb_id, $this->object_id, $this->object_type );
1596 + }
1597 +
1598 + /**
1599 + * Converts deprecated field parameters to the current/proper parameter, and throws a deprecation notice.
1600 + *
1601 + * @since 2.2.3
1602 + * @param array $args Metabox field config array.
1603 + * @return array Modified field config array.
1604 + */
1605 + protected function convert_deprecated_params( $args ) {
1606 +
1607 + if ( isset( $args['row_classes'] ) ) {
1608 +
1609 + // We'll let this one be.
1610 + // $this->deprecated_param( __CLASS__ . '::__construct()', '2.2.3', self::DEPRECATED_PARAM, 'row_classes', 'classes' );
1611 + // row_classes param could be a callback. This is definitely deprecated.
1612 + if ( is_callable( $args['row_classes'] ) ) {
1613 +
1614 + $this->deprecated_param( __CLASS__ . '::__construct()', '2.2.3', self::DEPRECATED_CB_PARAM, 'row_classes', 'classes_cb' );
1615 +
1616 + $args['classes_cb'] = $args['row_classes'];
1617 + $args['classes'] = null;
1618 + } else {
1619 +
1620 + $args['classes'] = $args['row_classes'];
1621 + }
1622 +
1623 + unset( $args['row_classes'] );
1624 + }
1625 +
1626 + // default param can be passed a callback as well.
1627 + if ( is_callable( $args['default'] ) ) {
1628 +
1629 + $this->deprecated_param( __CLASS__ . '::__construct()', '2.2.3', self::DEPRECATED_CB_PARAM, 'default', 'default_cb' );
1630 +
1631 + $args['default_cb'] = $args['default'];
1632 + $args['default'] = null;
1633 + }
1634 +
1635 + // options param can be passed a callback as well.
1636 + if ( is_callable( $args['options'] ) ) {
1637 +
1638 + $this->deprecated_param( __CLASS__ . '::__construct()', '2.2.3', self::DEPRECATED_CB_PARAM, 'options', 'options_cb' );
1639 +
1640 + $args['options_cb'] = $args['options'];
1641 + $args['options'] = array();
1642 + }
1643 +
1644 + return $args;
1645 + }
1646 +
1647 +}