Passed
Push — trunk ( 467725...420cca )
by Justin
02:31
created

CMB2_Field::register_js_data()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 1
CRAP Score 2

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 6
ccs 1
cts 1
cp 1
rs 9.4285
cc 2
eloc 3
nc 2
nop 0
crap 2
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 138
	 */
124
	protected $hash_id = '';
125 138
126 4
	/**
127 4
	 * Constructs our field object
128 4
	 *
129 4
	 * @since 1.1.0
130 4
	 * @param array $args Field arguments
131 138
	 */
132 138
	public function __construct( $args ) {
133
134 138
		if ( ! empty( $args['group_field'] ) ) {
135 69
			$this->group       = $args['group_field'];
136 69
			$this->object_id   = $this->group->object_id;
137
			$this->object_type = $this->group->object_type;
138
			$this->cmb_id      = $this->group->cmb_id;
139 138
		} else {
140
			$this->object_id   = isset( $args['object_id'] ) && '_' !== $args['object_id'] ? $args['object_id'] : 0;
141 138
			$this->object_type = isset( $args['object_type'] ) ? $args['object_type'] : 'post';
142 123
143 123
			if ( isset( $args['cmb_id'] ) ) {
144 138
				$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 115
155 115
	/**
156
	 * Non-existent methods fallback to checking for field arguments of the same name
157
	 *
158
	 * @since  1.1.0
159 115
	 * @param  string $name     Method name
160 115
	 * @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 131
	}
171 131
172 131
	/**
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 134
184 134
	/**
185
	 * Get a field argument
186 134
	 *
187
	 * @since  1.1.0
188 1
	 * @param  string $key  Argument to check
189
	 * @param  string $_key Sub argument to check
190 134
	 * @return mixed        Argument value or false if non-existent
191
	 */
192
	public function args( $key = '', $_key = '' ) {
193
		$arg = $this->_data( 'args', $key );
194
195 134
		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 134
207 134
	/**
208 134
	 * Retrieve a portion of a field property
209 134
	 *
210
	 * @since  1.1.0
211 77
	 * @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 54
	}
222 54
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 127
234 127
	/**
235
	 * Retrieves metadata/option data
236 127
	 *
237
	 * @since  1.0.1
238
	 * @param  string $field_id Meta key/Option array key
239
	 * @param  array  $args     Override arguments
240 127
	 * @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 127
		 *     @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 127
		/**
279
		 * Filter and parameters are documented for 'cmb2_override_meta_value' filter (above).
280
		 *
281 127
		 * The dynamic portion of the hook, $field_id, refers to the current
282 127
		 * field id paramater. Returning a non 'cmb2_field_no_override_val' value
283 127
		 * will effectively short-circuit the value retrieval.
284 127
		 *
285 127
		 * @since 2.0.0
286
		 */
287 127
		$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 127
		}
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 23
	}
305 23
306 23
	/**
307 23
	 * Updates metadata/option data
308
	 *
309 23
	 * @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
	 */
313
	public function update_data( $new_value, $single = true ) {
314
		$a = $this->data_args( array(
315
			'single' => $single,
316
		) );
317
318
		$a['value'] = $a['repeat'] ? array_values( $new_value ) : $new_value;
319
320
		/**
321
		 * Filter whether to override saving of meta value.
322
		 * Returning a non-null value will effectively short-circuit the function.
323
		 *
324
		 * @since 2.0.0
325
		 *
326
		 * @param null|bool $check  Whether to allow updating metadata for the given type.
327
		 *
328
		 * @param array $args {
329
		 *     Array of data about current field including:
330
		 *
331
		 *     @type string $value    The value to set
332
		 *     @type string $type     The current object type
333
		 *     @type int    $id       The current object ID
334 23
		 *     @type string $field_id The ID of the field being updated
335
		 *     @type bool   $repeat   Whether current field is repeatable
336
		 *     @type bool   $single   Whether current field is a single database row
337
		 * }
338
		 *
339
		 * @param array $field_args All field arguments
340
		 *
341
		 * @param CMB2_Field object $field This field object
342
		 */
343
		$override = apply_filters( 'cmb2_override_meta_save', null, $a, $this->args(), $this );
344
345 23
		/**
346
		 * Filter and parameters are documented for 'cmb2_override_meta_save' filter (above).
347
		 *
348 23
		 * The dynamic portion of the hook, $a['field_id'], refers to the current
349 1
		 * field id paramater. Returning a non-null value
350
		 * will effectively short-circuit the function.
351
		 *
352
		 * @since 2.0.0
353 23
		 */
354 6
		$override = apply_filters( "cmb2_override_{$a['field_id']}_meta_save", $override, $a, $this->args(), $this );
355
356
		// If override, return that
357
		if ( null !== $override ) {
358 17
			return $override;
359 1
		}
360
361
		// Options page handling (or temp data store)
362
		if ( 'options-page' === $a['type'] || empty( $a['id'] ) ) {
363 16
			return cmb2_options( $a['id'] )->update( $a['field_id'], $a['value'], false, $a['single'] );
364
		}
365
366
		// Add metadata if not single
367
		if ( ! $a['single'] ) {
368 16
			return add_metadata( $a['type'], $a['id'], $a['field_id'], $a['value'], false );
369
		}
370
371
		// Delete meta if we have an empty array
372
		if ( is_array( $a['value'] ) && empty( $a['value'] ) ) {
373
			return delete_metadata( $a['type'], $a['id'], $a['field_id'], $this->value );
374
		}
375
376
		// Update metadata
377 5
		return update_metadata( $a['type'], $a['id'], $a['field_id'], $a['value'] );
378 5
	}
379 5
380 5
	/**
381
	 * Removes/updates metadata/option data
382
	 *
383
	 * @since  1.0.1
384
	 * @param  string $old Old value
385
	 */
386
	public function remove_data( $old = '' ) {
387
		$a = $this->data_args( array(
388
			'old' => $old,
389
		) );
390
391
		/**
392
		 * Filter whether to override removing of meta value.
393
		 * Returning a non-null value will effectively short-circuit the function.
394
		 *
395
		 * @since 2.0.0
396
		 *
397
		 * @param null|bool $delete Whether to allow metadata deletion of the given type.
398
		 * @param array $args       Array of data about current field including:
399 5
		 *                              'type'     : Current object type
400
		 *                              'id'       : Current object ID
401
		 *                              'field_id' : Current Field ID
402
		 *                              'repeat'   : Whether current field is repeatable
403
		 *                              'single'   : Whether to save as a
404
		 *                              					single meta value
405
		 * @param array $field_args All field arguments
406
		 * @param CMB2_Field object $field This field object
407
		 */
408
		$override = apply_filters( 'cmb2_override_meta_remove', null, $a, $this->args(), $this );
409
410
		/**
411
		 * Filter whether to override removing of meta value.
412
		 *
413
		 * The dynamic portion of the hook, $a['field_id'], refers to the current
414
		 * field id paramater. Returning a non-null value
415
		 * will effectively short-circuit the function.
416
		 *
417
		 * @since 2.0.0
418
		 *
419
		 * @param null|bool $delete Whether to allow metadata deletion of the given type.
420
		 * @param array $args       Array of data about current field including:
421 5
		 *                              'type'     : Current object type
422
		 *                              'id'       : Current object ID
423
		 *                              'field_id' : Current Field ID
424 5
		 *                              'repeat'   : Whether current field is repeatable
425
		 *                              'single'   : Whether to save as a
426
		 *                              					single meta value
427
		 * @param array $field_args All field arguments
428 5
		 * @param CMB2_Field object $field This field object
429 1
		 */
430
		$override = apply_filters( "cmb2_override_{$a['field_id']}_meta_remove", $override, $a, $this->args(), $this );
431
432
		// If no override, remove as usual
433 4
		if ( null !== $override ) {
434
			return $override;
435
		} // End if().
436
		// Option page handling.
437
		elseif ( 'options-page' === $a['type'] || empty( $a['id'] ) ) {
438
			return cmb2_options( $a['id'] )->remove( $a['field_id'] );
439
		}
440
441
		// Remove metadata
442
		return delete_metadata( $a['type'], $a['id'], $a['field_id'], $old );
443 127
	}
444 127
445 127
	/**
446 127
	 * Data variables for get/set data methods
447 127
	 *
448 127
	 * @since  1.1.0
449 127
	 * @param  array $args Override arguments
450 127
	 * @return array       Updated arguments
451 127
	 */
452
	public function data_args( $args = array() ) {
453
		$args = wp_parse_args( $args, array(
454
			'type'     => $this->object_type,
455
			'id'       => $this->object_id,
456
			'field_id' => $this->id( true ),
457
			'repeat'   => $this->args( 'repeatable' ),
458
			'single'   => ! $this->args( 'multiple' ),
459
		) );
460
		return $args;
461 25
	}
462
463 25
	/**
464
	 * Checks if field has a registered sanitization callback
465 2
	 *
466 2
	 * @since  1.0.1
467
	 * @param  mixed $meta_value Meta value
468
	 * @return mixed             Possibly sanitized meta value
469 25
	 */
470 25
	public function sanitization_cb( $meta_value ) {
471
472 2
		if ( $this->args( 'repeatable' ) && is_array( $meta_value ) ) {
473 24
			// Remove empties
474
			$meta_value = array_filter( $meta_value );
475
		}
476
477
		// Check if the field has a registered validation callback
478 24
		$cb = $this->maybe_callback( 'sanitization_cb' );
479 24
		if ( false === $cb ) {
480
			// If requesting NO validation, return meta value
481
			return $meta_value;
482
		} elseif ( $cb ) {
483
			// Ok, callback is good, let's run it.
484
			return call_user_func( $cb, $meta_value, $this->args(), $this );
485
		}
486
487
		$sanitizer = new CMB2_Sanitize( $this, $meta_value );
488
		$field_type = $this->type();
489
490
		/**
491
		 * Filter the value before it is saved.
492
		 *
493
		 * The dynamic portion of the hook name, $field_type, refers to the field type.
494
		 *
495
		 * Passing a non-null value to the filter will short-circuit saving
496 24
		 * the field value, saving the passed value instead.
497
		 *
498 24
		 * @param bool|mixed $override_value Sanitization/Validation override value to return.
499 1
		 *                                   Default: null. false to skip it.
500
		 * @param mixed      $value      The value to be saved to this field.
501
		 * @param int        $object_id  The ID of the object where the value will be saved
502
		 * @param array      $field_args The current field's arguments
503 23
		 * @param object     $sanitizer  This `CMB2_Sanitize` object
504
		 */
505
		$override_value = apply_filters( "cmb2_sanitize_{$field_type}", null, $sanitizer->value, $this->object_id, $this->args(), $sanitizer );
506
507
		if ( null !== $override_value ) {
508
			return $override_value;
509
		}
510
511
		// Sanitization via 'CMB2_Sanitize'
512
		return $sanitizer->{$field_type}();
513 4
	}
514 4
515
	/**
516 4
	 * Process $_POST data to save this field's value
517 4
	 *
518 4
	 * @since  2.0.3
519
	 * @param  array $data_to_save $_POST data to check
520 4
	 * @return array|int|bool                Result of save, false on failure
521
	 */
522
	public function save_field_from_data( array $data_to_save ) {
523
		$this->data_to_save = $data_to_save;
524
525
		$meta_value = isset( $this->data_to_save[ $this->id( true ) ] )
526
			? $this->data_to_save[ $this->id( true ) ]
527
			: null;
528
529
		return $this->save_field( $meta_value );
530 23
	}
531
532 23
	/**
533 23
	 * Sanitize/store a value to this field
534 23
	 *
535
	 * @since  2.0.0
536 23
	 * @param  array $meta_value Desired value to sanitize/store
537
	 * @return array|int|bool              Result of save. false on failure
538
	 */
539 1
	public function save_field( $meta_value ) {
540
541 23
		$updated   = false;
542
		$action    = '';
543 1
		$new_value = $this->sanitization_cb( $meta_value );
544 1
545
		if ( ! $this->args( 'save_field' ) ) {
546 1
547 1
			// Nothing to see here.
548 1
			$action = 'disabled';
549 1
550 1
		} elseif ( $this->args( 'multiple' ) && ! $this->args( 'repeatable' ) && ! $this->group ) {
551 1
552 1
			$this->remove_data();
553
			$count = 0;
554 1
555 1
			if ( ! empty( $new_value ) ) {
556
				foreach ( $new_value as $add_new ) {
557 22
					if ( $this->update_data( $add_new, false ) ) {
558 20
						$count++;
559 20
					}
560 21
				}
561 4
			}
562 4
563 4
			$updated = $count ? $count : false;
564
			$action  = 'repeatable';
565 23
566 22
		} elseif ( ! CMB2_Utils::isempty( $new_value ) && $new_value !== $this->get_data() ) {
567 22
			$updated = $this->update_data( $new_value );
568 22
			$action  = 'updated';
569
		} elseif ( CMB2_Utils::isempty( $new_value ) ) {
570 23
			$updated = $this->remove_data();
571
			$action  = 'removed';
572
		}
573
574
		if ( $updated ) {
575
			$this->value = $this->get_data();
576
			$this->escaped_value = null;
577
		}
578
579
		$field_id = $this->id( true );
580
581
		/**
582 23
		 * Hooks after save field action.
583
		 *
584
		 * @since 2.2.0
585
		 *
586
		 * @param string            $field_id the current field id paramater.
587
		 * @param bool              $updated  Whether the metadata update action occurred.
588
		 * @param string            $action   Action performed. Could be "repeatable", "updated", or "removed".
589
		 * @param CMB2_Field object $field    This field object
590
		 */
591
		do_action( 'cmb2_save_field', $field_id, $updated, $action, $this );
592
593
		/**
594
		 * Hooks after save field action.
595
		 *
596 22
		 * The dynamic portion of the hook, $field_id, refers to the
597
		 * current field id paramater.
598 21
		 *
599
		 * @since 2.2.0
600
		 *
601
		 * @param bool              $updated Whether the metadata update action occurred.
602
		 * @param string            $action  Action performed. Could be "repeatable", "updated", or "removed".
603
		 * @param CMB2_Field object $field   This field object
604
		 */
605
		do_action( "cmb2_save_field_{$field_id}", $updated, $action, $this );
606
607 54
		return $updated;
608
	}
609 54
610 54
	/**
611 54
	 * Determine if current type is exempt from escaping
612 54
	 *
613 54
	 * @since  1.1.0
614
	 * @return bool  True if exempt
615
	 */
616
	public function escaping_exception() {
617
		// These types cannot be escaped
618
		return in_array( $this->type(), array(
619
			'file_list',
620
			'multicheck',
621
			'text_datetime_timestamp_timezone',
622
		) );
623 6
	}
624
625
	/**
626
	 * Determine if current type cannot be repeatable
627 6
	 *
628 6
	 * @since  1.1.0
629 6
	 * @param  string $type Field type to check
630 6
	 * @return bool         True if type cannot be repeatable
631 6
	 */
632 6
	public function repeatable_exception( $type ) {
633 6
		// These types cannot be repeatable.
634 6
		$internal_fields = array(
635 6
			// Use file_list instead
636 6
			'file'                             => 1,
637 6
			'radio'                            => 1,
638 6
			'title'                            => 1,
639 6
			'wysiwyg'                          => 1,
640
			'checkbox'                         => 1,
641 6
			'radio_inline'                     => 1,
642
			'taxonomy_radio'                   => 1,
643
			'taxonomy_radio_inline'            => 1,
644
			'taxonomy_radio_hierarchical'      => 1,
645
			'taxonomy_select'                  => 1,
646
			'taxonomy_multicheck'              => 1,
647
			'taxonomy_multicheck_inline'       => 1,
648
			'taxonomy_multicheck_hierarchical' => 1,
649
650
		);
651
652
		/**
653
		 * Filter field types that are non-repeatable.
654 6
		 *
655 6
		 * Note that this does *not* allow overriding the default non-repeatable types.
656
		 *
657
		 * @since 2.1.1
658
		 *
659
		 * @param array $fields Array of fields designated as non-repeatable. Note that the field names are *keys*,
660
		 *                      and not values. The value can be anything, because it is meaningless. Example:
661
		 *                      array( 'my_custom_field' => 1 )
662
		 */
663
		$all_fields = array_merge( apply_filters( 'cmb2_non_repeatable_fields', array() ), $internal_fields );
664
		return isset( $all_fields[ $type ] );
665 138
	}
666
667
	/**
668
	 * Determine if current type has its own defaults field-arguments method.
669 138
	 *
670 138
	 * @since  2.2.6
671 138
	 * @param  string $type Field type to check
672
	 * @return bool         True if has own method.
673 138
	 */
674 6
	public function has_args_method( $type ) {
675
676
		// These types have their own arguments parser.
677 136
		$type_methods = array(
678 136
			'group'   => 'set_field_defaults_group',
679 136
			'wysiwyg' => 'set_field_defaults_wysiwyg',
680 136
		);
681 136
682 136
		if ( isset( $type_methods[ $type ] ) ) {
683 136
			return $type_methods[ $type ];
684 136
		}
685 136
686
		$all_or_nothing_types = array_flip( apply_filters( 'cmb2_all_or_nothing_types', array(
687 136
			'select',
688 18
			'radio',
689
			'radio_inline',
690
			'taxonomy_select',
691 120
			'taxonomy_radio',
692
			'taxonomy_radio_inline',
693
			'taxonomy_radio_hierarchical',
694
		), $this ) );
695
696
		if ( isset( $all_or_nothing_types[ $type ] ) ) {
697
			return 'set_field_defaults_all_or_nothing_types';
698
		}
699
700
		return false;
701
	}
702 54
703
	/**
704 54
	 * Escape the value before output. Defaults to 'esc_attr()'
705 31
	 *
706
	 * @since  1.0.1
707
	 * @param  callable $func       Escaping function (if not esc_attr())
708 54
	 * @param  mixed    $meta_value Meta value
709
	 * @return mixed                Final value
710
	 */
711 54
	public function escaped_value( $func = 'esc_attr', $meta_value = '' ) {
712
713
		if ( null !== $this->escaped_value ) {
714
			return $this->escaped_value;
715
		}
716 54
717
		$meta_value = $meta_value ? $meta_value : $this->value();
718
719
		// Check if the field has a registered escaping callback
720
		if ( $cb = $this->maybe_callback( 'escape_cb' ) ) {
721
			// Ok, callback is good, let's run it.
722
			return call_user_func( $cb, $meta_value, $this->args(), $this );
723
		}
724
725
		$field_type = $this->type();
726
727
		/**
728
		 * Filter the value for escaping before it is ouput.
729
		 *
730
		 * The dynamic portion of the hook name, $field_type, refers to the field type.
731
		 *
732 54
		 * Passing a non-null value to the filter will short-circuit the built-in
733 54
		 * escaping for this field.
734
		 *
735
		 * @param bool|mixed $override_value Escaping override value to return.
736
		 *                                   Default: null. false to skip it.
737 54
		 * @param mixed      $meta_value The value to be output.
738
		 * @param array      $field_args The current field's arguments.
739 5
		 * @param object     $field      This `CMB2_Field` object.
740
		 */
741
		$esc = apply_filters( "cmb2_types_esc_{$field_type}", null, $meta_value, $this->args(), $this );
742
		if ( null !== $esc ) {
743 49
			return $esc;
744 49
		}
745
746 49
		if ( false === $cb || $this->escaping_exception() ) {
747
			// If requesting NO escaping, return meta value
748
			return $this->val_or_default( $meta_value );
749
		}
750
751 49
		// escaping function passed in?
752
		$func       = $func ? $func : 'esc_attr';
753
		$meta_value = $this->val_or_default( $meta_value );
754 49
755 49
		if ( is_array( $meta_value ) ) {
756
			foreach ( $meta_value as $key => $value ) {
757
				$meta_value[ $key ] = call_user_func( $func, $value );
758
			}
759
		} else {
760
			$meta_value = call_user_func( $func, $meta_value );
761
		}
762
763
		$this->escaped_value = $meta_value;
764
		return $this->escaped_value;
765 54
	}
766 54
767
	/**
768
	 * Return non-empty value or field default if value IS empty
769
	 *
770
	 * @since  2.0.0
771
	 * @param  mixed $meta_value Field value
772
	 * @return mixed             Field value, or default value
773
	 */
774
	public function val_or_default( $meta_value ) {
775
		return ! CMB2_Utils::isempty( $meta_value ) ? $meta_value : $this->get_default();
776
	}
777
778
	/**
779
	 * Offset a time value based on timezone
780
	 *
781
	 * @since  1.0.0
782
	 * @return string Offset time string
783
	 */
784
	public function field_timezone_offset() {
785
		return CMB2_Utils::timezone_offset( $this->field_timezone() );
786
	}
787
788
	/**
789
	 * Return timezone string
790
	 *
791
	 * @since  1.0.0
792
	 * @return string Timezone string
793
	 */
794
	public function field_timezone() {
795
		$value = '';
796
797
		// Is timezone arg set?
798
		if ( $this->args( 'timezone' ) ) {
799
			$value = $this->args( 'timezone' );
800
		} // End if().
801
		// Is there another meta key with a timezone stored as its value we should use?
802
		elseif ( $this->args( 'timezone_meta_key' ) ) {
803
			$value = $this->get_data( $this->args( 'timezone_meta_key' ) );
804
		}
805
806
		return $value;
807
	}
808 10
809 10
	/**
810
	 * Format the timestamp field value based on the field date/time format arg
811
	 *
812
	 * @since  2.0.0
813
	 * @param  int    $meta_value Timestamp
814
	 * @param  string $format     Either date_format or time_format
815
	 * @return string             Formatted date
816
	 */
817
	public function format_timestamp( $meta_value, $format = 'date_format' ) {
818
		return date( stripslashes( $this->args( $format ) ), $meta_value );
819
	}
820 10
821 10
	/**
822 10
	 * Return a formatted timestamp for a field
823
	 *
824 10
	 * @since  2.0.0
825
	 * @param  string $format     Either date_format or time_format
826
	 * @param  string $meta_value Optional meta value to check
827
	 * @return string             Formatted date
828 10
	 */
829 10
	public function get_timestamp_format( $format = 'date_format', $meta_value = 0 ) {
830 10
		$meta_value = $meta_value ? $meta_value : $this->escaped_value();
831
		$meta_value = CMB2_Utils::make_valid_time_stamp( $meta_value );
832
833
		if ( empty( $meta_value ) ) {
834
			return '';
835
		}
836
837
		return is_array( $meta_value )
0 ignored issues
show
introduced by
The condition is_array($meta_value) is always false.
Loading history...
838
			? array_map( array( $this, 'format_timestamp' ), $meta_value, $format )
839
			: $this->format_timestamp( $meta_value, $format );
840
	}
841
842
	/**
843
	 * Get timestamp from text date
844
	 *
845
	 * @since  2.2.0
846
	 * @param  string $value Date value
847
	 * @return mixed         Unix timestamp representing the date.
848
	 */
849 10
	public function get_timestamp_from_value( $value ) {
850 10
		return CMB2_Utils::get_timestamp_from_value( $value, $this->args( 'date_format' ) );
851
	}
852 10
853
	/**
854
	 * Get field render callback and Render the field row
855 10
	 *
856
	 * @since 1.0.0
857
	 */
858
	public function render_field() {
859
		$this->render_context = 'edit';
860
861
		$this->peform_param_callback( 'render_row_cb' );
862
863 14
		// For chaining
864
		return $this;
865
	}
866 14
867
	/**
868
	 * Default field render callback
869
	 *
870
	 * @since 2.1.1
871 14
	 */
872
	public function render_field_callback() {
873
874
		// If field is requesting to not be shown on the front-end
875 14
		if ( ! is_admin() && ! $this->args( 'on_front' ) ) {
876
			return;
877 14
		}
878
879 14
		// If field is requesting to be conditionally shown
880
		if ( ! $this->should_show() ) {
881
			return;
882
		}
883
884
		$this->peform_param_callback( 'before_row' );
885
886 14
		printf(
887 14
			"<div class=\"cmb-row %s\" data-fieldtype=\"%s\" data-hash=\"%s\">\n",
888 14
			$this->row_classes(),
889
			$this->type(),
890 14
			$this->hash_id()
891
		);
892
893 14
		if ( ! $this->args( 'show_names' ) ) {
894
			echo "\n\t<div class=\"cmb-td\">\n";
895 14
896 14
			$this->peform_param_callback( 'label_cb' );
897
898 14
		} else {
899
900 14
			if ( $this->get_param_callback_result( 'label_cb' ) ) {
901
				echo '<div class="cmb-th">', $this->peform_param_callback( 'label_cb' ), '</div>';
902 14
			}
903
904
			echo "\n\t<div class=\"cmb-td\">\n";
905 14
		}
906
907
		$this->peform_param_callback( 'before' );
908
909
		$types = new CMB2_Types( $this );
910
		$types->render();
911
912
		$this->peform_param_callback( 'after' );
913
914 14
		echo "\n\t</div>\n</div>";
915 14
916
		$this->peform_param_callback( 'after_row' );
917
918
		// For chaining
919 14
		return $this;
920
	}
921 14
922
	/**
923
	 * The default label_cb callback (if not a title field)
924
	 *
925
	 * @since  2.1.1
926
	 * @return string Label html markup
927
	 */
928
	public function label() {
929
		if ( ! $this->args( 'name' ) ) {
930 50
			return '';
931
		}
932 50
933
		$style = ! $this->args( 'show_names' ) ? ' style="display:none;"' : '';
934
935
		return sprintf( "\n" . '<label%1$s for="%2$s">%3$s</label>' . "\n", $style, $this->id(), $this->args( 'name' ) );
936
	}
937
938
	/**
939
	 * Defines the classes for the current CMB2 field row
940
	 *
941 50
	 * @since  2.0.0
942 50
	 * @return string Space concatenated list of classes
943 50
	 */
944 50
	public function row_classes() {
945
946
		$classes = array();
947 50
948 50
		/**
949 50
		 * By default, 'text_url' and 'text' fields get table-like styling
950 50
		 *
951 50
		 * @since 2.0.0
952 50
		 *
953 50
		 * @param array $field_types The types of fields which should get the 'table-layout' class
954
		 */
955 50
		$repeat_table_rows_types = apply_filters( 'cmb2_repeat_table_row_types', array(
956 50
			'text_url',
957 50
			'text',
958 50
		) );
959 50
960
		$conditional_classes = array(
961 50
			'cmb-type-' . str_replace( '_', '-', sanitize_html_class( $this->type() ) ) => true,
962 1
			'cmb2-id-' . str_replace( '_', '-', sanitize_html_class( $this->id() ) )    => true,
963 50
			'cmb-repeat'             => $this->args( 'repeatable' ),
964 2
			'cmb-repeat-group-field' => $this->group,
965 2
			'cmb-inline'             => $this->args( 'inline' ),
966
			'table-layout'           => 'edit' === $this->render_context && in_array( $this->type(), $repeat_table_rows_types ),
967 50
		);
968 3
969 3
		foreach ( $conditional_classes as $class => $condition ) {
970
			if ( $condition ) {
971
				$classes[] = $class;
972
			}
973
		}
974
975
		if ( $added_classes = $this->args( 'classes' ) ) {
976
			$added_classes = is_array( $added_classes ) ? implode( ' ', $added_classes ) : (string) $added_classes;
977
		} elseif ( $added_classes = $this->get_param_callback_result( 'classes_cb' ) ) {
978
			$added_classes = is_array( $added_classes ) ? implode( ' ', $added_classes ) : (string) $added_classes;
979 50
		}
980
981
		if ( $added_classes ) {
982
			$classes[] = esc_attr( $added_classes );
983
		}
984
985
		/**
986
		 * Globally filter row classes
987
		 *
988
		 * @since 2.0.0
989 33
		 *
990 33
		 * @param string            $classes Space-separated list of row classes
991
		 * @param CMB2_Field object $field   This field object
992 33
		 */
993
		return apply_filters( 'cmb2_row_classes', implode( ' ', $classes ), $this );
994
	}
995 33
996
	/**
997
	 * Get field display callback and render the display value in the column.
998
	 *
999
	 * @since 2.2.2
1000
	 */
1001
	public function render_column() {
1002
		$this->render_context = 'display';
1003 33
1004
		$this->peform_param_callback( 'display_cb' );
1005 33
1006
		// For chaining
1007
		return $this;
1008
	}
1009 33
1010 33
	/**
1011
	 * Default callback to outputs field value in a display format.
1012
	 *
1013
	 * @since 2.2.2
1014
	 */
1015
	public function display_value_callback() {
1016
		// If field is requesting to be conditionally shown
1017
		if ( ! $this->should_show() ) {
1018
			return;
1019
		}
1020
1021
		$display = new CMB2_Field_Display( $this );
1022
		$field_type = $this->type();
1023 33
1024
		/**
1025 33
		 * A filter to bypass the default display.
1026
		 *
1027
		 * The dynamic portion of the hook name, $field_type, refers to the field type.
1028
		 *
1029
		 * Passing a non-null value to the filter will short-circuit the default display.
1030 33
		 *
1031
		 * @param bool|mixed         $pre_output Default null value.
1032 33
		 * @param CMB2_Field         $field      This field object.
1033
		 * @param CMB2_Field_Display $display    The `CMB2_Field_Display` object.
1034 33
		 */
1035
		$pre_output = apply_filters( "cmb2_pre_field_display_{$field_type}", null, $this, $display );
1036 33
1037
		if ( null !== $pre_output ) {
1038 33
			echo $pre_output;
1039
			return;
1040 33
		}
1041
1042 33
		$this->peform_param_callback( 'before_display_wrap' );
1043
1044
		printf( "<div class=\"cmb-column %s\" data-fieldtype=\"%s\">\n", $this->row_classes(), $field_type );
1045 33
1046
		$this->peform_param_callback( 'before_display' );
1047
1048
		CMB2_Field_Display::get( $this )->display();
1049
1050
		$this->peform_param_callback( 'after_display' );
1051
1052
		echo "\n</div>";
1053
1054
		$this->peform_param_callback( 'after_display_wrap' );
1055 2
1056
		// For chaining
1057 2
		return $this;
1058
	}
1059
1060
	/**
1061
	 * Replaces a hash key - {#} - with the repeatable index
1062
	 *
1063
	 * @since  1.2.0
1064
	 * @param  string $value Value to update
1065
	 * @return string        Updated value
1066
	 */
1067
	public function replace_hash( $value ) {
1068
		// Replace hash with 1 based count
1069 10
		return str_replace( '{#}', ( $this->index + 1 ), $value );
1070
	}
1071 10
1072 10
	/**
1073
	 * Retrieve text parameter from field's text array (if it has one), or use fallback text
1074 10
	 * For back-compatibility, falls back to checking the options array.
1075
	 *
1076
	 * @since  2.2.2
1077
	 * @param  string $text_key Key in field's text array
1078
	 * @param  string $fallback Fallback text
1079
	 * @return string            Text
1080
	 */
1081 10
	public function get_string( $text_key, $fallback ) {
1082
		// If null, populate with our field strings values.
1083
		if ( null === $this->strings ) {
1084 10
			$this->strings = (array) $this->args['text'];
1085 1
1086
			if ( is_callable( $this->args['text_cb'] ) ) {
1087
				$strings = call_user_func( $this->args['text_cb'], $this );
1088
1089 10
				if ( $strings && is_array( $strings ) ) {
1090
					$this->strings += $strings;
1091 10
				}
1092
			}
1093
		}
1094
1095
		// If we have that string value, send it back.
1096
		if ( isset( $this->strings[ $text_key ] ) ) {
1097
			return $this->strings[ $text_key ];
1098
		}
1099
1100
		// Check options for back-compat.
1101 43
		$string = $this->options( $text_key );
1102 43
1103 43
		return $string ? $string : $fallback;
1104 43
	}
1105
1106 43
	/**
1107 16
	 * Retrieve options args.
1108
	 *
1109
	 * @since  2.0.0
1110 32
	 * @param  string $key Specific option to retrieve
1111
	 * @return array|mixed Array of options or specific option.
1112
	 */
1113
	public function options( $key = '' ) {
1114
		if ( empty( $this->field_options ) ) {
1115
			$this->set_options();
1116
		}
1117
1118
		if ( $key ) {
1119
			return array_key_exists( $key, $this->field_options ) ? $this->field_options[ $key ] : false;
1120 43
		}
1121 43
1122
		return $this->field_options;
1123 43
	}
1124 1
1125
	/**
1126 1
	 * Generates/sets options args. Calls options_cb if it exists.
1127 1
	 *
1128 1
	 * @since  2.2.5
1129 1
	 *
1130
	 * @return array Array of options
1131 43
	 */
1132
	public function set_options() {
1133
		$this->field_options = (array) $this->args['options'];
1134
1135
		if ( is_callable( $this->args['options_cb'] ) ) {
1136
			$options = call_user_func( $this->args['options_cb'], $this );
1137
1138
			if ( $options && is_array( $options ) ) {
1139
				$this->field_options = $options + $this->field_options;
1140 14
			}
1141 14
		}
1142 14
1143 14
		return $this->field_options;
1144
	}
1145 14
1146 14
	/**
1147
	 * Store JS dependencies as part of the field args.
1148
	 *
1149
	 * @since 2.2.0
1150
	 * @param array $dependencies Dependies to register for this field.
1151
	 */
1152
	public function add_js_dependencies( $dependencies = array() ) {
1153
		foreach ( (array) $dependencies as $dependency ) {
1154
			$this->args['js_dependencies'][ $dependency ] = $dependency;
1155 41
		}
1156 41
1157
		CMB2_JS::add_dependencies( $dependencies );
1158 41
	}
1159 8
1160
	/**
1161
	 * Send field data to JS.
1162 40
	 *
1163 40
	 * @since 2.2.0
1164
	 */
1165
	public function register_js_data() {
1166 40
		if ( $this->group ) {
1167
			CMB2_JS::add_field_data( $this->group );
1168
		}
1169
1170
		return CMB2_JS::add_field_data( $this );
0 ignored issues
show
Bug introduced by
Are you sure the usage of CMB2_JS::add_field_data($this) targeting CMB2_JS::add_field_data() seems to always return null.

This check looks for function or method calls that always return null and whose return value is used.

class A
{
    function getObject()
    {
        return null;
    }

}

$a = new A();
if ($a->getObject()) {

The method getObject() can return nothing but null, so it makes no sense to use the return value.

The reason is most likely that a function or method is imcomplete or has been reduced for debug purposes.

Loading history...
1171
	}
1172
1173
	/**
1174
	 * Get an array of some of the field data to be used in the Javascript.
1175
	 *
1176
	 * @since  2.2.4
1177 138
	 *
1178
	 * @return array
1179
	 */
1180 138
	public function js_data() {
1181
		return array(
1182
			'label'     => $this->args( 'name' ),
1183
			'id'        => $this->id( true ),
1184
			'type'      => $this->type(),
1185
			'hash'      => $this->hash_id(),
1186
			'box'       => $this->cmb_id,
1187
			'id_attr'   => $this->id(),
1188
			'name_attr' => $this->args( '_name' ),
1189 138
			'default'   => $this->get_default(),
1190
			'group'     => $this->group_id(),
1191 138
			'index'     => $this->group ? $this->group->index : null,
1192 138
		);
1193 138
	}
1194 138
1195
	/**
1196 138
	 * Returns a unique hash representing this field.
1197 24
	 *
1198 24
	 * @since  2.2.4
1199
	 *
1200 138
	 * @return string
1201 4
	 */
1202 4
	public function hash_id() {
1203
		if ( '' === $this->hash_id ) {
1204 138
			$this->hash_id = CMB2_Utils::generate_hash( $this->cmb_id . '||' . $this->id() );
1205 138
		}
1206
1207
		return $this->hash_id;
1208 138
	}
1209
1210 138
	/**
1211 138
	 * Gets the id of the group field if this field is part of a group.
1212
	 *
1213 138
	 * @since  2.2.4
1214
	 *
1215 138
	 * @return string
1216
	 */
1217
	public function group_id() {
1218
		return $this->group ? $this->group->id( true ) : '';
1219
	}
1220
1221
	/**
1222
	 * Get CMB2_Field default value, either from default param or default_cb param.
1223
	 *
1224
	 * @since  0.2.2
1225
	 *
1226 5
	 * @return mixed  Default field value
1227 5
	 */
1228 5
	public function get_default() {
1229 5
		$default = $this->args['default'];
1230 5
1231
		if ( null !== $default ) {
1232 5
			return apply_filters( 'cmb2_default_filter', $default, $this );
1233
		}
1234
1235
		$param = is_callable( $this->args['default_cb'] ) ? 'default_cb' : 'default';
1236
		$default = $this->args['default'] = $this->get_param_callback_result( $param );
0 ignored issues
show
Unused Code introduced by
The assignment to $default is dead and can be removed.
Loading history...
1237
1238
		// Allow a filter override of the default value.
1239
		return apply_filters( 'cmb2_default_filter', $this->args['default'], $this );
1240
	}
1241
1242
	/**
1243 1
	 * Fills in empty field parameters with defaults
1244 1
	 *
1245 1
	 * @since 1.1.0
1246
	 *
1247 1
	 * @param  array  $args Field config array.
1248
	 * @return array        Modified field config array.
1249
	 */
1250
	public function _set_field_defaults( $args ) {
1251
1252
		// Set up blank or default values for empty ones
1253
		$args = wp_parse_args( $args, $this->get_default_field_args( $args ) );
1254
1255
		/*
1256
		 * Deprecated usage:
1257
		 *
1258 18
		 * 'std' -- use 'default' (no longer works)
1259 18
		 * 'row_classes' -- use 'class', or 'class_cb'
1260 18
		 * 'default' -- as callback (use default_cb)
1261
		 */
1262 18
		$args = $this->convert_deprecated_params( $args );
1263 17
1264 17
		$args['repeatable'] = $args['repeatable'] && ! $this->repeatable_exception( $args['type'] );
1265 17
		$args['inline']     = $args['inline'] || false !== stripos( $args['type'], '_inline' );
1266
		$args['_id']        = $args['id'];
1267 18
		$args['_name']      = $args['id'];
1268
1269
		if ( $method = $this->has_args_method( $args['type'] ) ) {
1270
			$args = $this->{$method}( $args );
1271
		}
1272
1273
		if ( $this->group ) {
1274
			$args = $this->set_group_sub_field_defaults( $args );
1275
		}
1276
1277
		$args['has_supporting_data'] = in_array(
1278 4
			$args['type'],
1279 4
			array(
1280 4
				// CMB2_Sanitize::_save_file_id_value()/CMB2_Sanitize::_get_group_file_value_array()
1281
				'file',
1282 4
				// See CMB2_Sanitize::_save_utc_value()
1283
				'text_datetime_timestamp_timezone',
1284
			),
1285
			true
1286
		);
1287
1288
		return $args;
1289
	}
1290
1291
	/**
1292
	 * Sets default arguments for the group field types.
1293 138
	 *
1294 138
	 * @since 2.2.6
1295
	 *
1296
	 * @param  array  $args Field config array.
1297 138
	 * @return array        Modified field config array.
1298 138
	 */
1299 138
	protected function set_field_defaults_group( $args ) {
1300 138
		$args['options'] = wp_parse_args( $args['options'], array(
1301 138
			'add_button'    => esc_html__( 'Add Group', 'cmb2' ),
1302 138
			'remove_button' => esc_html__( 'Remove Group', 'cmb2' ),
1303 138
		) );
1304 138
1305 138
		return $args;
1306 138
	}
1307 138
1308 138
	/**
1309 138
	 * Sets default arguments for the wysiwyg field types.
1310 138
	 *
1311 138
	 * @since 2.2.6
1312 138
	 *
1313 138
	 * @param  array  $args Field config array.
1314 138
	 * @return array        Modified field config array.
1315 138
	 */
1316 138
	protected function set_field_defaults_wysiwyg( $args ) {
1317 138
		$args['id'] = strtolower( str_ireplace( '-', '_', $args['id'] ) );
1318 138
		$args['options']['textarea_name'] = $args['_name'];
1319 138
1320 138
		return $args;
1321 138
	}
1322 138
1323 138
	/**
1324 138
	 * Sets default arguments for the all-or-nothing field types.
1325 138
	 *
1326 138
	 * @since 2.2.6
1327 138
	 *
1328 138
	 * @param  array  $args Field config array.
1329 138
	 * @return array        Modified field config array.
1330
	 */
1331
	protected function set_field_defaults_all_or_nothing_types( $args ) {
1332
		$args['show_option_none'] = isset( $args['show_option_none'] ) ? $args['show_option_none'] : null;
1333
		$args['show_option_none'] = true === $args['show_option_none'] ? esc_html__( 'None', 'cmb2' ) : $args['show_option_none'];
1334
1335
		if ( null === $args['show_option_none'] ) {
1336
			$off_by_default = in_array( $args['type'], array( 'select', 'radio', 'radio_inline' ), true );
1337
			$args['show_option_none'] = $off_by_default ? false : esc_html__( 'None', 'cmb2' );
1338
		}
1339
1340 8
		return $args;
1341 8
	}
1342
1343 8
	/**
1344
	 * Sets default arguments for group sub-fields.
1345
	 *
1346 8
	 * @since 2.2.6
1347
	 *
1348
	 * @param  array  $args Field config array.
1349 8
	 * @return array        Modified field config array.
1350
	 */
1351
	protected function set_group_sub_field_defaults( $args ) {
1352
		$args['id']    = $this->group->args( 'id' ) . '_' . $this->group->index . '_' . $args['id'];
1353
		$args['_name'] = $this->group->args( 'id' ) . '[' . $this->group->index . '][' . $args['_name'] . ']';
1354
1355
		return $args;
1356
	}
1357
1358
	/**
1359
	 * Gets the default arguments for all fields.
1360
	 *
1361
	 * @since 2.2.6
1362 8
	 *
1363 8
	 * @param  array  $args Field config array.
1364
	 * @return array        Field defaults.
1365
	 */
1366
	protected function get_default_field_args( $args ) {
1367
		$type = isset( $args['type'] ) ? $args['type'] : '';
1368
1369
		return array(
1370
			'type'              => $type,
1371
			'name'              => '',
1372
			'desc'              => '',
1373 2
			'before'            => '',
1374 2
			'after'             => '',
1375
			'options'           => array(),
1376
			'options_cb'        => '',
1377
			'text'              => array(),
1378 2
			'text_cb'           => '',
1379
			'attributes'        => array(),
1380
			'protocols'         => null,
1381
			'default'           => null,
1382
			'default_cb'        => '',
1383
			'classes'           => null,
1384
			'classes_cb'        => '',
1385
			'select_all_button' => true,
1386
			'multiple'          => false,
1387
			'repeatable'        => 'group' === $type,
1388 138
			'inline'            => false,
1389
			'on_front'          => true,
1390 138
			'show_names'        => true,
1391
			'save_field'        => true, // Will not save if false
1392
			'date_format'       => 'm\/d\/Y',
1393
			'time_format'       => 'h:i A',
1394
			'description'       => isset( $args['desc'] ) ? $args['desc'] : '',
1395 2
			'preview_size'      => 'file' === $type ? array( 350, 350 ) : array( 50, 50 ),
1396
			'render_row_cb'     => array( $this, 'render_field_callback' ),
1397 1
			'display_cb'        => array( $this, 'display_value_callback' ),
1398
			'label_cb'          => 'title' !== $type ? array( $this, 'label' ) : '',
1399 1
			'column'            => false,
1400 1
			'js_dependencies'   => array(),
1401 1
			'show_in_rest'      => null,
1402
		);
1403 1
	}
1404
1405
	/**
1406 2
	 * Get default field arguments specific to this CMB2 object.
1407 2
	 *
1408
	 * @since  2.2.0
1409
	 * @param  array      $field_args  Metabox field config array.
1410 138
	 * @param  CMB2_Field $field_group (optional) CMB2_Field object (group parent)
1411
	 * @return array                   Array of field arguments.
1412
	 */
1413
	protected function get_default_args( $field_args, $field_group = null ) {
1414
		$args = parent::get_default_args( array(), $this->group );
1415
1416
		if ( isset( $field_args['field_args'] ) ) {
1417
			$args = wp_parse_args( $field_args, $args );
1418
		} else {
1419 138
			$args['field_args'] = wp_parse_args( $field_args, $this->args );
1420
		}
1421
1422
		return $args;
1423
	}
1424
1425
	/**
1426
	 * Returns a cloned version of this field object, but with
1427 138
	 * modified/overridden field arguments.
1428
	 *
1429
	 * @since  2.2.2
1430
	 * @param  array $field_args Array of field arguments, or entire array of
1431
	 *                           arguments for CMB2_Field
1432
	 *
1433
	 * @return CMB2_Field         The new CMB2_Field instance.
1434
	 */
1435
	public function get_field_clone( $field_args ) {
1436
		return $this->get_new_field( $field_args );
1437
	}
1438
1439
	/**
1440
	 * Returns the CMB2 instance this field is registered to.
1441
	 *
1442
	 * @since  2.2.2
1443
	 *
1444
	 * @return CMB2|WP_Error If new CMB2_Field is called without cmb_id arg, returns error.
1445
	 */
1446
	public function get_cmb() {
1447
		if ( ! $this->cmb_id ) {
1448
			return new WP_Error( 'no_cmb_id', esc_html__( 'Sorry, this field does not have a cmb_id specified.', 'cmb2' ) );
1449
		}
1450
1451
		return cmb2_get_metabox( $this->cmb_id, $this->object_id, $this->object_type );
1452
	}
1453
1454
	/**
1455
	 * Converts deprecated field parameters to the current/proper parameter, and throws a deprecation notice.
1456
	 *
1457
	 * @since  2.2.3
1458
	 * @param  array $args Metabox field config array.
1459
	 * @return array       Modified field config array.
1460
	 */
1461
	protected function convert_deprecated_params( $args ) {
1462
1463
		if ( isset( $args['row_classes'] ) ) {
1464
1465
			// We'll let this one be.
1466
			// $this->deprecated_param( __CLASS__ . '::__construct()', '2.2.3', self::DEPRECATED_PARAM, 'row_classes', 'classes' );
1467
			// row_classes param could be a callback. This is definitely deprecated.
1468
			if ( is_callable( $args['row_classes'] ) ) {
1469
1470
				$this->deprecated_param( __CLASS__ . '::__construct()', '2.2.3', self::DEPRECATED_CB_PARAM, 'row_classes', 'classes_cb' );
1471
1472
				$args['classes_cb'] = $args['row_classes'];
1473
				$args['classes'] = null;
1474
			} else {
1475
1476
				$args['classes'] = $args['row_classes'];
1477
			}
1478
1479
			unset( $args['row_classes'] );
1480
		}
1481
1482
		// default param can be passed a callback as well
1483
		if ( is_callable( $args['default'] ) ) {
1484
1485
			$this->deprecated_param( __CLASS__ . '::__construct()', '2.2.3', self::DEPRECATED_CB_PARAM, 'default', 'default_cb' );
1486
1487
			$args['default_cb'] = $args['default'];
1488
			$args['default'] = null;
1489
		}
1490
1491
		// options param can be passed a callback as well
1492
		if ( is_callable( $args['options'] ) ) {
1493
1494
			$this->deprecated_param( __CLASS__ . '::__construct()', '2.2.3', self::DEPRECATED_CB_PARAM, 'options', 'options_cb' );
1495
1496
			$args['options_cb'] = $args['options'];
1497
			$args['options'] = array();
1498
		}
1499
1500
		return $args;
1501
	}
1502
1503
}
1504