Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like CMB2_Field often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use CMB2_Field, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class CMB2_Field extends CMB2_Base { |
||
|
|||
18 | |||
19 | /** |
||
20 | * The object properties name. |
||
21 | * @var string |
||
22 | * @since 2.2.3 |
||
23 | */ |
||
24 | protected $properties_name = 'args'; |
||
25 | |||
26 | /** |
||
27 | * Field arguments |
||
28 | * @var mixed |
||
29 | * @since 1.1.0 |
||
30 | */ |
||
31 | public $args = array(); |
||
32 | |||
33 | /** |
||
34 | * Field group object or false (if no group) |
||
35 | * @var mixed |
||
36 | * @since 1.1.0 |
||
37 | */ |
||
38 | public $group = false; |
||
39 | |||
40 | /** |
||
41 | * Field meta value |
||
42 | * @var mixed |
||
43 | * @since 1.1.0 |
||
44 | */ |
||
45 | public $value = null; |
||
46 | |||
47 | /** |
||
48 | * Field meta value |
||
49 | 46 | * @var mixed |
|
50 | * @since 1.1.0 |
||
51 | 46 | */ |
|
52 | public $escaped_value = null; |
||
53 | |||
54 | /** |
||
55 | * Grouped Field's current numeric index during the save process |
||
56 | 46 | * @var mixed |
|
57 | 46 | * @since 2.0.0 |
|
58 | 46 | */ |
|
59 | public $index = 0; |
||
60 | |||
61 | 46 | /** |
|
62 | * Array of field options |
||
63 | * @var array |
||
64 | * @since 2.0.0 |
||
65 | */ |
||
66 | protected $field_options = array(); |
||
67 | |||
68 | /** |
||
69 | * Array of provided field text strings |
||
70 | * @var array |
||
71 | * @since 2.0.0 |
||
72 | */ |
||
73 | protected $strings; |
||
74 | |||
75 | /** |
||
76 | * The field's render context. In most cases, 'edit', but can be 'display'. |
||
77 | * @var string |
||
78 | 46 | * @since 2.2.2 |
|
79 | */ |
||
80 | public $render_context = 'edit'; |
||
81 | |||
82 | /** |
||
83 | * All CMB2_Field callable field arguments. |
||
84 | * Can be used to determine if a field argument is callable. |
||
85 | * |
||
86 | * @var array |
||
87 | */ |
||
88 | public static $callable_fields = array( |
||
89 | 'default_cb', |
||
90 | 'classes_cb', |
||
91 | 'options_cb', |
||
92 | 'text_cb', |
||
93 | 'label_cb', |
||
94 | 'render_row_cb', |
||
95 | 'display_cb', |
||
96 | 'before_group', |
||
97 | 46 | 'before_group_row', |
|
98 | 'before_row', |
||
99 | 'before', |
||
100 | 46 | 'before_field', |
|
101 | 46 | 'after_field', |
|
102 | 46 | 'after', |
|
103 | 46 | 'after_row', |
|
104 | 'after_group_row', |
||
105 | 'after_group', |
||
106 | ); |
||
107 | |||
108 | /** |
||
109 | * Constructs our field object |
||
110 | * @since 1.1.0 |
||
111 | * @param array $args Field arguments |
||
112 | 38 | */ |
|
113 | 38 | public function __construct( $args ) { |
|
114 | 38 | ||
115 | if ( ! empty( $args['group_field'] ) ) { |
||
116 | $this->group = $args['group_field']; |
||
117 | $this->object_id = $this->group->object_id; |
||
118 | $this->object_type = $this->group->object_type; |
||
119 | $this->cmb_id = $this->group->cmb_id; |
||
120 | } else { |
||
121 | $this->object_id = isset( $args['object_id'] ) && '_' !== $args['object_id'] ? $args['object_id'] : 0; |
||
122 | $this->object_type = isset( $args['object_type'] ) ? $args['object_type'] : 'post'; |
||
123 | 46 | ||
124 | 46 | if ( isset( $args['cmb_id'] ) ) { |
|
125 | 46 | $this->cmb_id = $args['cmb_id']; |
|
126 | } |
||
127 | } |
||
128 | |||
129 | $this->args = $this->_set_field_defaults( $args['field_args'], $args ); |
||
130 | |||
131 | if ( $this->object_id ) { |
||
132 | $this->value = $this->get_data(); |
||
133 | } |
||
134 | } |
||
135 | 46 | ||
136 | 46 | /** |
|
137 | 46 | * Non-existent methods fallback to checking for field arguments of the same name |
|
138 | * @since 1.1.0 |
||
139 | * @param string $name Method name |
||
140 | 46 | * @param array $arguments Array of passed-in arguments |
|
141 | * @return mixed Value of field argument |
||
142 | */ |
||
143 | public function __call( $name, $arguments ) { |
||
144 | if ( 'string' === $name ) { |
||
145 | return call_user_func_array( array( $this, 'get_string' ), $arguments ); |
||
146 | } |
||
147 | |||
148 | $key = isset( $arguments[0] ) ? $arguments[0] : false; |
||
149 | 37 | return $this->args( $name, $key ); |
|
150 | 37 | } |
|
151 | |||
152 | /** |
||
153 | * Retrieves the field id |
||
154 | * @since 1.1.0 |
||
155 | * @param boolean $raw Whether to retrieve pre-modidifed id |
||
156 | * @return string Field id |
||
157 | */ |
||
158 | public function id( $raw = false ) { |
||
159 | $id = $raw ? '_id' : 'id'; |
||
160 | 46 | return $this->args( $id ); |
|
161 | 46 | } |
|
162 | 46 | ||
163 | 46 | /** |
|
164 | * Get a field argument |
||
165 | 46 | * @since 1.1.0 |
|
166 | * @param string $key Argument to check |
||
167 | * @param string $_key Sub argument to check |
||
168 | * @return mixed Argument value or false if non-existent |
||
169 | */ |
||
170 | public function args( $key = '', $_key = '' ) { |
||
184 | 46 | ||
185 | 46 | /** |
|
186 | * Retrieve a portion of a field property |
||
187 | 46 | * @since 1.1.0 |
|
188 | * @param string $var Field property to check |
||
189 | * @param string $key Field property array key to check |
||
190 | * @return mixed Queried property value or false |
||
191 | */ |
||
192 | 46 | public function _data( $var, $key = '' ) { |
|
193 | $vars = $this->{$var}; |
||
194 | if ( $key ) { |
||
195 | return array_key_exists( $key, $vars ) ? $vars[ $key ] : false; |
||
196 | } |
||
197 | return $vars; |
||
198 | } |
||
199 | |||
200 | /** |
||
201 | * Get Field's value |
||
202 | * @since 1.1.0 |
||
203 | * @param string $key If value is an array, is used to get array key->value |
||
204 | * @return mixed Field value or false if non-existent |
||
205 | */ |
||
206 | public function value( $key = '' ) { |
||
209 | |||
210 | /** |
||
211 | * Retrieves metadata/option data |
||
212 | * @since 1.0.1 |
||
213 | * @param string $field_id Meta key/Option array key |
||
214 | * @param array $args Override arguments |
||
215 | * @return mixed Meta/Option value |
||
216 | */ |
||
217 | public function get_data( $field_id = '', $args = array() ) { |
||
218 | if ( $field_id ) { |
||
219 | $args['field_id'] = $field_id; |
||
220 | } else if ( $this->group ) { |
||
221 | $args['field_id'] = $this->group->id(); |
||
222 | } |
||
223 | |||
224 | $a = $this->data_args( $args ); |
||
225 | |||
226 | /** |
||
227 | * Filter whether to override getting of meta value. |
||
228 | * Returning a non 'cmb2_field_no_override_val' value |
||
229 | * will effectively short-circuit the value retrieval. |
||
230 | * |
||
231 | * @since 2.0.0 |
||
232 | * |
||
233 | * @param mixed $value The value get_metadata() should |
||
234 | * return - a single metadata value, |
||
235 | * or an array of values. |
||
236 | * |
||
237 | * @param int $object_id Object ID. |
||
238 | * |
||
239 | * @param array $args { |
||
240 | * An array of arguments for retrieving data |
||
241 | * |
||
242 | * @type string $type The current object type |
||
243 | * @type int $id The current object ID |
||
244 | * @type string $field_id The ID of the field being requested |
||
245 | * @type bool $repeat Whether current field is repeatable |
||
246 | * @type bool $single Whether current field is a single database row |
||
247 | * } |
||
248 | * |
||
249 | * @param CMB2_Field object $field This field object |
||
250 | */ |
||
251 | $data = apply_filters( 'cmb2_override_meta_value', 'cmb2_field_no_override_val', $this->object_id, $a, $this ); |
||
252 | |||
253 | /** |
||
254 | * Filter and parameters are documented for 'cmb2_override_meta_value' filter (above). |
||
255 | * |
||
256 | * The dynamic portion of the hook, $field_id, refers to the current |
||
257 | * field id paramater. Returning a non 'cmb2_field_no_override_val' value |
||
258 | * will effectively short-circuit the value retrieval. |
||
259 | * |
||
260 | * @since 2.0.0 |
||
261 | */ |
||
262 | $data = apply_filters( "cmb2_override_{$a['field_id']}_meta_value", $data, $this->object_id, $a, $this ); |
||
263 | |||
264 | // If no override, get value normally |
||
265 | if ( 'cmb2_field_no_override_val' === $data ) { |
||
266 | $data = 'options-page' === $a['type'] |
||
267 | ? cmb2_options( $a['id'] )->get( $a['field_id'] ) |
||
268 | : get_metadata( $a['type'], $a['id'], $a['field_id'], ( $a['single'] || $a['repeat'] ) ); |
||
269 | } |
||
270 | |||
271 | if ( $this->group ) { |
||
272 | |||
273 | $data = is_array( $data ) && isset( $data[ $this->group->index ][ $this->args( '_id' ) ] ) |
||
274 | ? $data[ $this->group->index ][ $this->args( '_id' ) ] |
||
275 | : false; |
||
276 | } |
||
277 | |||
278 | return $data; |
||
279 | } |
||
280 | |||
281 | /** |
||
282 | * Updates metadata/option data |
||
283 | * @since 1.0.1 |
||
284 | * @param mixed $new_value Value to update data with |
||
285 | * @param bool $single Whether data is an array (add_metadata) |
||
286 | */ |
||
287 | public function update_data( $new_value, $single = true ) { |
||
351 | 46 | ||
352 | 46 | /** |
|
353 | 46 | * Removes/updates metadata/option data |
|
354 | 46 | * @since 1.0.1 |
|
355 | 46 | * @param string $old Old value |
|
356 | 46 | */ |
|
357 | 46 | public function remove_data( $old = '' ) { |
|
413 | |||
414 | /** |
||
415 | * Data variables for get/set data methods |
||
416 | * @since 1.1.0 |
||
417 | * @param array $args Override arguments |
||
418 | * @return array Updated arguments |
||
419 | */ |
||
420 | public function data_args( $args = array() ) { |
||
430 | 39 | ||
431 | 39 | /** |
|
432 | 38 | * Checks if field has a registered sanitization callback |
|
433 | * @since 1.0.1 |
||
434 | * @param mixed $meta_value Meta value |
||
435 | * @return mixed Possibly sanitized meta value |
||
436 | 7 | */ |
|
437 | public function sanitization_cb( $meta_value ) { |
||
480 | 1 | ||
481 | 1 | /** |
|
482 | 1 | * Process $_POST data to save this field's value |
|
483 | * @since 2.0.3 |
||
484 | * @param array $data_to_save $_POST data to check |
||
485 | * @return array|int|bool Result of save, false on failure |
||
486 | */ |
||
487 | public function save_field_from_data( array $data_to_save ) { |
||
496 | |||
497 | /** |
||
498 | 37 | * Sanitize/store a value to this field |
|
499 | * @since 2.0.0 |
||
500 | * @param array $meta_value Desired value to sanitize/store |
||
501 | 37 | * @return array|int|bool Result of save. false on failure |
|
502 | */ |
||
503 | public function save_field( $meta_value ) { |
||
504 | |||
505 | $updated = false; |
||
506 | $action = ''; |
||
507 | 37 | $new_value = $this->sanitization_cb( $meta_value ); |
|
508 | 37 | ||
509 | if ( ! $this->args( 'save_field' ) ) { |
||
510 | |||
511 | // Nothing to see here. |
||
512 | 37 | $action = 'disabled'; |
|
513 | |||
514 | 4 | } elseif ( $this->args( 'multiple' ) && ! $this->args( 'repeatable' ) && ! $this->group ) { |
|
515 | |||
516 | $this->remove_data(); |
||
517 | $count = 0; |
||
518 | 33 | ||
519 | 33 | if ( ! empty( $new_value ) ) { |
|
520 | foreach ( $new_value as $add_new ) { |
||
521 | 33 | if ( $this->update_data( $add_new, false ) ) { |
|
522 | $count++; |
||
523 | } |
||
524 | } |
||
525 | } |
||
526 | 33 | ||
527 | $updated = $count ? $count : false; |
||
528 | $action = 'repeatable'; |
||
529 | 33 | ||
530 | 33 | } elseif ( ! CMB2_Utils::isempty( $new_value ) && $new_value !== $this->get_data() ) { |
|
531 | $updated = $this->update_data( $new_value ); |
||
532 | $action = 'updated'; |
||
533 | } elseif ( CMB2_Utils::isempty( $new_value ) ) { |
||
534 | $updated = $this->remove_data(); |
||
535 | $action = 'removed'; |
||
536 | } |
||
537 | |||
538 | 1 | if ( $updated ) { |
|
539 | 1 | $this->value = $this->get_data(); |
|
540 | $this->escaped_value = null; |
||
541 | } |
||
542 | |||
543 | $field_id = $this->id( true ); |
||
544 | |||
545 | /** |
||
546 | * Hooks after save field action. |
||
547 | 1 | * |
|
548 | * @since 2.2.0 |
||
549 | * |
||
550 | 1 | * @param string $field_id the current field id paramater. |
|
551 | * @param bool $updated Whether the metadata update action occurred. |
||
552 | * @param string $action Action performed. Could be "repeatable", "updated", or "removed". |
||
553 | * @param CMB2_Field object $field This field object |
||
554 | 1 | */ |
|
555 | do_action( 'cmb2_save_field', $field_id, $updated, $action, $this ); |
||
556 | |||
557 | /** |
||
558 | 1 | * Hooks after save field action. |
|
559 | * |
||
560 | * The dynamic portion of the hook, $field_id, refers to the |
||
561 | * current field id paramater. |
||
562 | * |
||
563 | * @since 2.2.0 |
||
564 | * |
||
565 | * @param bool $updated Whether the metadata update action occurred. |
||
566 | * @param string $action Action performed. Could be "repeatable", "updated", or "removed". |
||
567 | * @param CMB2_Field object $field This field object |
||
568 | 5 | */ |
|
569 | 5 | do_action( "cmb2_save_field_{$field_id}", $updated, $action, $this ); |
|
570 | |||
571 | return $updated; |
||
572 | } |
||
573 | |||
574 | /** |
||
575 | * Determine if current type is exempt from escaping |
||
576 | * @since 1.1.0 |
||
577 | * @return bool True if exempt |
||
578 | 5 | */ |
|
579 | 5 | public function escaping_exception() { |
|
587 | 5 | ||
588 | 5 | /** |
|
589 | * Determine if current type cannot be repeatable |
||
590 | * @since 1.1.0 |
||
591 | * @param string $type Field type to check |
||
592 | * @return bool True if type cannot be repeatable |
||
593 | */ |
||
594 | public function repeatable_exception( $type ) { |
||
595 | 5 | // These types cannot be repeatable. |
|
596 | $internal_fields = array( |
||
597 | // Use file_list instead |
||
598 | 5 | 'file' => 1, |
|
599 | 'radio' => 1, |
||
600 | 'title' => 1, |
||
601 | 'wysiwyg' => 1, |
||
602 | 'checkbox' => 1, |
||
603 | 5 | 'radio_inline' => 1, |
|
604 | 'taxonomy_radio' => 1, |
||
605 | 'taxonomy_select' => 1, |
||
606 | 'taxonomy_multicheck' => 1, |
||
607 | 5 | ); |
|
608 | |||
609 | 5 | /** |
|
610 | * Filter field types that are non-repeatable. |
||
611 | 5 | * |
|
612 | * Note that this does *not* allow overriding the default non-repeatable types. |
||
613 | * |
||
614 | * @since 2.1.1 |
||
615 | * |
||
616 | * @param array $fields Array of fields designated as non-repeatable. Note that the field names are *keys*, |
||
617 | * and not values. The value can be anything, because it is meaningless. Example: |
||
618 | * array( 'my_custom_field' => 1 ) |
||
619 | */ |
||
620 | 5 | $all_fields = array_merge( apply_filters( 'cmb2_non_repeatable_fields', array() ), $internal_fields ); |
|
621 | 5 | return isset( $all_fields[ $type ] ); |
|
622 | 5 | } |
|
623 | |||
624 | 5 | /** |
|
625 | * Escape the value before output. Defaults to 'esc_attr()' |
||
626 | * @since 1.0.1 |
||
627 | 5 | * @param callable $func Escaping function (if not esc_attr()) |
|
628 | * @param mixed $meta_value Meta value |
||
629 | 5 | * @return mixed Final value |
|
630 | 5 | */ |
|
631 | public function escaped_value( $func = 'esc_attr', $meta_value = '' ) { |
||
632 | 5 | ||
633 | if ( null !== $this->escaped_value ) { |
||
634 | 5 | return $this->escaped_value; |
|
635 | } |
||
636 | 5 | ||
637 | 5 | $meta_value = $meta_value ? $meta_value : $this->value(); |
|
638 | |||
639 | // Check if the field has a registered escaping callback |
||
640 | if ( $cb = $this->maybe_callback( 'escape_cb' ) ) { |
||
641 | // Ok, callback is good, let's run it. |
||
642 | return call_user_func( $cb, $meta_value, $this->args(), $this ); |
||
643 | } |
||
644 | |||
645 | 5 | // Or custom escaping filter can be used |
|
646 | 5 | $esc = apply_filters( "cmb2_types_esc_{$this->type()}", null, $meta_value, $this->args(), $this ); |
|
647 | 5 | if ( null !== $esc ) { |
|
648 | 5 | return $esc; |
|
649 | 5 | } |
|
650 | |||
651 | 5 | if ( false === $cb || $this->escaping_exception() ) { |
|
652 | // If requesting NO escaping, return meta value |
||
653 | 5 | return $this->val_or_default( $meta_value ); |
|
654 | 5 | } |
|
655 | 5 | ||
656 | // escaping function passed in? |
||
657 | 5 | $func = $func ? $func : 'esc_attr'; |
|
658 | 3 | $meta_value = $this->val_or_default( $meta_value ); |
|
659 | 3 | ||
660 | if ( is_array( $meta_value ) ) { |
||
661 | 5 | foreach ( $meta_value as $key => $value ) { |
|
662 | $meta_value[ $key ] = call_user_func( $func, $value ); |
||
663 | } |
||
664 | } else { |
||
665 | $meta_value = call_user_func( $func, $meta_value ); |
||
666 | } |
||
667 | |||
668 | $this->escaped_value = $meta_value; |
||
669 | return $this->escaped_value; |
||
670 | } |
||
671 | 38 | ||
672 | 38 | /** |
|
673 | * Return non-empty value or field default if value IS empty |
||
674 | 2 | * @since 2.0.0 |
|
675 | 2 | * @param mixed $meta_value Field value |
|
676 | * @return mixed Field value, or default value |
||
677 | */ |
||
678 | public function val_or_default( $meta_value ) { |
||
681 | |||
682 | /** |
||
683 | * Offset a time value based on timezone |
||
684 | * @since 1.0.0 |
||
685 | * @return string Offset time string |
||
686 | */ |
||
687 | public function field_timezone_offset() { |
||
688 | return CMB2_Utils::timezone_offset( $this->field_timezone() ); |
||
689 | } |
||
690 | |||
691 | /** |
||
692 | * Return timezone string |
||
693 | * @since 1.0.0 |
||
694 | * @return string Timezone string |
||
695 | */ |
||
696 | public function field_timezone() { |
||
697 | $value = ''; |
||
698 | |||
699 | 14 | // Is timezone arg set? |
|
700 | 14 | if ( $this->args( 'timezone' ) ) { |
|
701 | 5 | $value = $this->args( 'timezone' ); |
|
702 | 4 | } |
|
703 | // Is there another meta key with a timezone stored as its value we should use? |
||
704 | else if ( $this->args( 'timezone_meta_key' ) ) { |
||
705 | 1 | $value = $this->get_data( $this->args( 'timezone_meta_key' ) ); |
|
706 | } |
||
707 | |||
708 | 14 | return $value; |
|
709 | } |
||
710 | 14 | ||
711 | 1 | /** |
|
712 | * Format the timestamp field value based on the field date/time format arg |
||
713 | 1 | * @since 2.0.0 |
|
714 | 1 | * @param int $meta_value Timestamp |
|
715 | 1 | * @param string $format Either date_format or time_format |
|
716 | 1 | * @return string Formatted date |
|
717 | */ |
||
718 | 14 | public function format_timestamp( $meta_value, $format = 'date_format' ) { |
|
721 | |||
722 | 11 | /** |
|
723 | * Return a formatted timestamp for a field |
||
724 | * @since 2.0.0 |
||
725 | * @param string $format Either date_format or time_format |
||
726 | * @param string $meta_value Optional meta value to check |
||
727 | * @return string Formatted date |
||
728 | */ |
||
729 | public function get_timestamp_format( $format = 'date_format', $meta_value = 0 ) { |
||
730 | 46 | $meta_value = $meta_value ? $meta_value : $this->escaped_value(); |
|
731 | $meta_value = CMB2_Utils::make_valid_time_stamp( $meta_value ); |
||
732 | |||
733 | 46 | if ( empty( $meta_value ) ) { |
|
734 | 46 | return ''; |
|
735 | 46 | } |
|
736 | 46 | ||
737 | 46 | return is_array( $meta_value ) |
|
738 | 46 | ? array_map( array( $this, 'format_timestamp' ), $meta_value, $format ) |
|
739 | 46 | : $this->format_timestamp( $meta_value, $format ); |
|
740 | 46 | } |
|
741 | 46 | ||
742 | 46 | /** |
|
743 | 46 | * Get timestamp from text date |
|
744 | 46 | * @since 2.2.0 |
|
745 | 46 | * @param string $value Date value |
|
746 | 46 | * @return mixed Unix timestamp representing the date. |
|
747 | 46 | */ |
|
748 | 46 | public function get_timestamp_from_value( $value ) { |
|
749 | 46 | return CMB2_Utils::get_timestamp_from_value( $value, $this->args( 'date_format' ) ); |
|
750 | 46 | } |
|
751 | 46 | ||
752 | 46 | /** |
|
753 | 46 | * Get field render callback and Render the field row |
|
754 | 46 | * @since 1.0.0 |
|
755 | */ |
||
756 | public function render_field() { |
||
757 | $this->render_context = 'edit'; |
||
758 | 46 | ||
759 | $this->peform_param_callback( 'render_row_cb' ); |
||
760 | 46 | ||
761 | 46 | // For chaining |
|
762 | return $this; |
||
763 | } |
||
764 | 46 | ||
765 | /** |
||
766 | * Default field render callback |
||
767 | * @since 2.1.1 |
||
768 | */ |
||
769 | 46 | public function render_field_callback() { |
|
770 | |||
771 | // If field is requesting to not be shown on the front-end |
||
772 | 46 | if ( ! is_admin() && ! $this->args( 'on_front' ) ) { |
|
773 | return; |
||
774 | 46 | } |
|
775 | 46 | ||
776 | // If field is requesting to be conditionally shown |
||
777 | 46 | if ( ! $this->should_show() ) { |
|
778 | return; |
||
779 | } |
||
780 | |||
781 | $this->peform_param_callback( 'before_row' ); |
||
782 | |||
783 | 46 | printf( "<div class=\"cmb-row %s\" data-fieldtype=\"%s\">\n", $this->row_classes(), $this->type() ); |
|
784 | 1 | ||
785 | 1 | if ( ! $this->args( 'show_names' ) ) { |
|
786 | 1 | echo "\n\t<div class=\"cmb-td\">\n"; |
|
787 | |||
788 | 46 | $this->peform_param_callback( 'label_cb' ); |
|
789 | |||
790 | 46 | } else { |
|
791 | |||
792 | 2 | if ( $this->get_param_callback_result( 'label_cb' ) ) { |
|
793 | 2 | echo '<div class="cmb-th">', $this->peform_param_callback( 'label_cb' ), '</div>'; |
|
794 | } |
||
795 | 2 | ||
796 | echo "\n\t<div class=\"cmb-td\">\n"; |
||
797 | 46 | } |
|
798 | |||
799 | $this->peform_param_callback( 'before' ); |
||
800 | |||
801 | $field_type = new CMB2_Types( $this ); |
||
802 | $field_type->render(); |
||
803 | |||
804 | $this->peform_param_callback( 'after' ); |
||
805 | 36 | ||
806 | 36 | echo "\n\t</div>\n</div>"; |
|
807 | |||
808 | $this->peform_param_callback( 'after_row' ); |
||
809 | |||
810 | 1 | // For chaining |
|
811 | return $this; |
||
812 | } |
||
813 | |||
814 | /** |
||
815 | * The default label_cb callback (if not a title field) |
||
816 | * |
||
817 | * @since 2.1.1 |
||
818 | * @return string Label html markup |
||
819 | */ |
||
820 | public function label() { |
||
821 | if ( ! $this->args( 'name' ) ) { |
||
822 | return ''; |
||
823 | } |
||
824 | |||
825 | $style = ! $this->args( 'show_names' ) ? ' style="display:none;"' : ''; |
||
826 | |||
827 | return sprintf( "\n" . '<label%1$s for="%2$s">%3$s</label>' . "\n", $style, $this->id(), $this->args( 'name' ) ); |
||
828 | } |
||
829 | |||
830 | /** |
||
831 | * Defines the classes for the current CMB2 field row |
||
832 | * |
||
833 | * @since 2.0.0 |
||
834 | * @return string Space concatenated list of classes |
||
835 | */ |
||
836 | public function row_classes() { |
||
837 | |||
838 | $classes = array(); |
||
839 | |||
840 | /** |
||
841 | * By default, 'text_url' and 'text' fields get table-like styling |
||
842 | * |
||
843 | * @since 2.0.0 |
||
844 | * |
||
845 | * @param array $field_types The types of fields which should get the 'table-layout' class |
||
846 | */ |
||
847 | $repeat_table_rows_types = apply_filters( 'cmb2_repeat_table_row_types', array( |
||
848 | 'text_url', 'text', |
||
849 | ) ); |
||
850 | |||
851 | $conditional_classes = array( |
||
852 | 'cmb-type-' . str_replace( '_', '-', sanitize_html_class( $this->type() ) ) => true, |
||
853 | 'cmb2-id-' . str_replace( '_', '-', sanitize_html_class( $this->id() ) ) => true, |
||
854 | 'cmb-repeat' => $this->args( 'repeatable' ), |
||
855 | 'cmb-repeat-group-field' => $this->group, |
||
856 | 'cmb-inline' => $this->args( 'inline' ), |
||
857 | 'table-layout' => 'edit' === $this->render_context && in_array( $this->type(), $repeat_table_rows_types ), |
||
858 | ); |
||
859 | |||
860 | foreach ( $conditional_classes as $class => $condition ) { |
||
861 | if ( $condition ) { |
||
862 | $classes[] = $class; |
||
863 | } |
||
864 | } |
||
865 | |||
866 | if ( $added_classes = $this->args( 'classes' ) ) { |
||
867 | $added_classes = is_array( $added_classes ) ? implode( ' ', $added_classes ) : (string) $added_classes; |
||
868 | } elseif ( $added_classes = $this->get_param_callback_result( 'classes_cb' ) ) { |
||
869 | $added_classes = is_array( $added_classes ) ? implode( ' ', $added_classes ) : (string) $added_classes; |
||
870 | } |
||
871 | |||
872 | if ( $added_classes ) { |
||
873 | $classes[] = esc_attr( $added_classes ); |
||
874 | } |
||
875 | |||
876 | /** |
||
877 | * Globally filter row classes |
||
878 | * |
||
879 | * @since 2.0.0 |
||
880 | * |
||
881 | * @param string $classes Space-separated list of row classes |
||
882 | * @param CMB2_Field object $field This field object |
||
883 | */ |
||
884 | return apply_filters( 'cmb2_row_classes', implode( ' ', $classes ), $this ); |
||
885 | } |
||
886 | |||
887 | |||
888 | |||
889 | /** |
||
890 | * Get field display callback and render the display value in the column. |
||
891 | * @since 2.2.2 |
||
892 | */ |
||
893 | public function render_column() { |
||
894 | $this->render_context = 'display'; |
||
895 | |||
896 | $this->peform_param_callback( 'display_cb' ); |
||
897 | |||
898 | // For chaining |
||
899 | return $this; |
||
900 | } |
||
901 | |||
902 | /** |
||
903 | * Default callback to outputs field value in a display format. |
||
904 | * @since 2.2.2 |
||
905 | */ |
||
906 | public function display_value_callback() { |
||
907 | // If field is requesting to be conditionally shown |
||
908 | if ( ! $this->should_show() ) { |
||
909 | return; |
||
910 | } |
||
911 | |||
912 | $display = new CMB2_Field_Display( $this ); |
||
913 | |||
914 | /** |
||
915 | * A filter to bypass the default display. |
||
916 | * |
||
917 | * The dynamic portion of the hook name, $this->type(), refers to the field type. |
||
918 | * |
||
919 | * Passing a non-null value to the filter will short-circuit the default display. |
||
920 | * |
||
921 | * @param bool|mixed $pre_output Default null value. |
||
922 | * @param CMB2_Field $field This field object. |
||
923 | * @param CMB2_Field_Display $display The `CMB2_Field_Display` object. |
||
924 | */ |
||
925 | $pre_output = apply_filters( "cmb2_pre_field_display_{$this->type()}", null, $this, $display ); |
||
926 | |||
927 | if ( null !== $pre_output ) { |
||
928 | echo $pre_output; |
||
929 | return; |
||
930 | } |
||
931 | |||
932 | $this->peform_param_callback( 'before_display_wrap' ); |
||
933 | |||
934 | printf( "<div class=\"cmb-column %s\" data-fieldtype=\"%s\">\n", $this->row_classes( 'display' ), $this->type() ); |
||
935 | |||
936 | $this->peform_param_callback( 'before_display' ); |
||
937 | |||
938 | CMB2_Field_Display::get( $this )->display(); |
||
939 | |||
940 | $this->peform_param_callback( 'after_display' ); |
||
941 | |||
942 | echo "\n</div>"; |
||
943 | |||
944 | $this->peform_param_callback( 'after_display_wrap' ); |
||
945 | |||
946 | // For chaining |
||
947 | return $this; |
||
948 | } |
||
949 | |||
950 | /** |
||
951 | * Replaces a hash key - {#} - with the repeatable index |
||
952 | * @since 1.2.0 |
||
953 | * @param string $value Value to update |
||
954 | * @return string Updated value |
||
955 | */ |
||
956 | public function replace_hash( $value ) { |
||
957 | // Replace hash with 1 based count |
||
958 | return str_replace( '{#}', ( $this->index + 1 ), $value ); |
||
959 | } |
||
960 | |||
961 | /** |
||
962 | * Retrieve text parameter from field's text array (if it has one), or use fallback text |
||
963 | * For back-compatibility, falls back to checking the options array. |
||
964 | * |
||
965 | * @since 2.2.2 |
||
966 | * @param string $text_key Key in field's text array |
||
967 | * @param string $fallback Fallback text |
||
968 | * @return string Text |
||
969 | */ |
||
970 | public function get_string( $text_key, $fallback ) { |
||
971 | // If null, populate with our field strings values. |
||
972 | if ( null === $this->strings ) { |
||
973 | $this->strings = (array) $this->args['text']; |
||
974 | |||
975 | View Code Duplication | if ( is_callable( $this->args['text_cb'] ) ) { |
|
976 | $strings = call_user_func( $this->args['text_cb'], $this ); |
||
977 | |||
978 | if ( $strings && is_array( $strings ) ) { |
||
979 | $this->strings += $strings; |
||
980 | } |
||
981 | } |
||
982 | } |
||
983 | |||
984 | // If we have that string value, send it back. |
||
985 | if ( isset( $this->strings[ $text_key ] ) ) { |
||
986 | return $this->strings[ $text_key ]; |
||
987 | } |
||
988 | |||
989 | // Check options for back-compat. |
||
990 | $string = $this->options( $text_key ); |
||
991 | |||
992 | return $string ? $string : $fallback; |
||
993 | } |
||
994 | |||
995 | /** |
||
996 | * Retrieve options args. Calls options_cb if it exists. |
||
997 | * @since 2.0.0 |
||
998 | * @param string $key Specific option to retrieve |
||
999 | * @return array Array of options |
||
1000 | */ |
||
1001 | public function options( $key = '' ) { |
||
1002 | if ( ! empty( $this->field_options ) ) { |
||
1003 | if ( $key ) { |
||
1004 | return array_key_exists( $key, $this->field_options ) ? $this->field_options[ $key ] : false; |
||
1005 | } |
||
1006 | |||
1007 | return $this->field_options; |
||
1008 | } |
||
1009 | |||
1010 | $this->field_options = (array) $this->args['options']; |
||
1011 | |||
1012 | View Code Duplication | if ( is_callable( $this->args['options_cb'] ) ) { |
|
1013 | $options = call_user_func( $this->args['options_cb'], $this ); |
||
1014 | |||
1015 | if ( $options && is_array( $options ) ) { |
||
1016 | $this->field_options = $options + $this->field_options; |
||
1017 | } |
||
1018 | } |
||
1019 | |||
1020 | if ( $key ) { |
||
1021 | return array_key_exists( $key, $this->field_options ) ? $this->field_options[ $key ] : false; |
||
1022 | } |
||
1023 | |||
1024 | return $this->field_options; |
||
1025 | } |
||
1026 | |||
1027 | /** |
||
1028 | * Store JS dependencies as part of the field args. |
||
1029 | * @since 2.2.0 |
||
1030 | * @param array $dependencies Dependies to register for this field. |
||
1031 | */ |
||
1032 | public function add_js_dependencies( $dependencies = array() ) { |
||
1033 | foreach ( (array) $dependencies as $dependency ) { |
||
1034 | $this->args['js_dependencies'][ $dependency ] = $dependency; |
||
1035 | } |
||
1036 | |||
1037 | CMB2_JS::add_dependencies( $dependencies ); |
||
1038 | } |
||
1039 | |||
1040 | /** |
||
1041 | * Get CMB2_Field default value, either from default param or default_cb param. |
||
1042 | * |
||
1043 | * @since 0.2.2 |
||
1044 | * |
||
1045 | * @return mixed Default field value |
||
1046 | */ |
||
1047 | public function get_default() { |
||
1048 | if ( null !== $this->args['default'] ) { |
||
1049 | return $this->args['default']; |
||
1050 | } |
||
1051 | |||
1052 | $param = is_callable( $this->args['default_cb'] ) ? 'default_cb' : 'default'; |
||
1053 | $default = $this->get_param_callback_result( $param ); |
||
1054 | |||
1055 | // Allow a filter override of the default value |
||
1056 | $this->args['default'] = apply_filters( 'cmb2_default_filter', $default, $this ); |
||
1057 | |||
1058 | return $this->args['default']; |
||
1059 | } |
||
1060 | |||
1061 | /** |
||
1062 | * Fills in empty field parameters with defaults |
||
1063 | * @since 1.1.0 |
||
1064 | * @param array $args Metabox field config array |
||
1065 | * @param array Modified field config array. |
||
1066 | */ |
||
1067 | public function _set_field_defaults( $args ) { |
||
1068 | |||
1069 | // Set up blank or default values for empty ones |
||
1070 | $args = wp_parse_args( $args, array( |
||
1071 | 'type' => '', |
||
1072 | 'name' => '', |
||
1073 | 'desc' => '', |
||
1074 | 'before' => '', |
||
1075 | 'after' => '', |
||
1076 | 'options' => array(), |
||
1077 | 'options_cb' => '', |
||
1078 | 'text' => array(), |
||
1079 | 'text_cb' => '', |
||
1080 | 'attributes' => array(), |
||
1081 | 'protocols' => null, |
||
1082 | 'default' => null, |
||
1083 | 'default_cb' => '', |
||
1084 | 'classes' => null, |
||
1085 | 'classes_cb' => '', |
||
1086 | 'select_all_button' => true, |
||
1087 | 'multiple' => false, |
||
1088 | 'repeatable' => isset( $args['type'] ) && 'group' == $args['type'], |
||
1089 | 'inline' => false, |
||
1090 | 'on_front' => true, |
||
1091 | 'show_names' => true, |
||
1092 | 'save_field' => true, // Will not save if false |
||
1093 | 'date_format' => 'm\/d\/Y', |
||
1094 | 'time_format' => 'h:i A', |
||
1095 | 'description' => isset( $args['desc'] ) ? $args['desc'] : '', |
||
1096 | 'preview_size' => 'file' == $args['type'] ? array( 350, 350 ) : array( 50, 50 ), |
||
1097 | 'render_row_cb' => array( $this, 'render_field_callback' ), |
||
1098 | 'display_cb' => array( $this, 'display_value_callback' ), |
||
1099 | 'label_cb' => 'title' != $args['type'] ? array( $this, 'label' ) : '', |
||
1100 | 'column' => false, |
||
1101 | 'js_dependencies' => array(), |
||
1102 | 'show_in_rest' => null, |
||
1103 | ) ); |
||
1104 | |||
1105 | /* |
||
1106 | * Deprecated usage: |
||
1107 | * |
||
1108 | * 'std' -- use 'default' (no longer works) |
||
1109 | * 'row_classes' -- use 'class', or 'class_cb' |
||
1110 | * 'default' -- as callback (use default_cb) |
||
1111 | */ |
||
1112 | $args = $this->convert_deprecated_params( $args ); |
||
1113 | |||
1114 | $args['repeatable'] = $args['repeatable'] && ! $this->repeatable_exception( $args['type'] ); |
||
1115 | $args['inline'] = $args['inline'] || false !== stripos( $args['type'], '_inline' ); |
||
1116 | |||
1117 | $args['options'] = 'group' == $args['type'] ? wp_parse_args( $args['options'], array( |
||
1118 | 'add_button' => esc_html__( 'Add Group', 'cmb2' ), |
||
1119 | 'remove_button' => esc_html__( 'Remove Group', 'cmb2' ), |
||
1120 | ) ) : $args['options']; |
||
1121 | |||
1122 | $args['_id'] = $args['id']; |
||
1123 | $args['_name'] = $args['id']; |
||
1124 | |||
1125 | if ( $this->group ) { |
||
1126 | |||
1127 | $args['id'] = $this->group->args( 'id' ) . '_' . $this->group->index . '_' . $args['id']; |
||
1128 | $args['_name'] = $this->group->args( 'id' ) . '[' . $this->group->index . '][' . $args['_name'] . ']'; |
||
1129 | } |
||
1130 | |||
1131 | if ( 'wysiwyg' == $args['type'] ) { |
||
1132 | $args['id'] = strtolower( str_ireplace( '-', '_', $args['id'] ) ); |
||
1133 | $args['options']['textarea_name'] = $args['_name']; |
||
1134 | } |
||
1135 | |||
1136 | $option_types = apply_filters( 'cmb2_all_or_nothing_types', array( 'select', 'radio', 'radio_inline', 'taxonomy_select', 'taxonomy_radio', 'taxonomy_radio_inline' ), $this ); |
||
1137 | |||
1138 | if ( in_array( $args['type'], $option_types, true ) ) { |
||
1139 | |||
1140 | $args['show_option_none'] = isset( $args['show_option_none'] ) ? $args['show_option_none'] : null; |
||
1141 | $args['show_option_none'] = true === $args['show_option_none'] ? esc_html__( 'None', 'cmb2' ) : $args['show_option_none']; |
||
1142 | |||
1143 | if ( null === $args['show_option_none'] ) { |
||
1144 | $off_by_default = in_array( $args['type'], array( 'select', 'radio', 'radio_inline' ), true ); |
||
1145 | $args['show_option_none'] = $off_by_default ? false : esc_html__( 'None', 'cmb2' ); |
||
1146 | } |
||
1147 | |||
1148 | } |
||
1149 | |||
1150 | $args['has_supporting_data'] = in_array( |
||
1151 | $args['type'], |
||
1152 | array( |
||
1153 | // CMB2_Sanitize::_save_file_id_value()/CMB2_Sanitize::_get_group_file_value_array() |
||
1154 | 'file', |
||
1155 | // See CMB2_Sanitize::_save_utc_value() |
||
1156 | 'text_datetime_timestamp_timezone', |
||
1157 | ), |
||
1158 | true |
||
1159 | ); |
||
1160 | |||
1161 | return $args; |
||
1162 | } |
||
1163 | |||
1164 | /** |
||
1165 | * Get default field arguments specific to this CMB2 object. |
||
1166 | * @since 2.2.0 |
||
1167 | * @param array $field_args Metabox field config array. |
||
1168 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
1169 | * @return array Array of field arguments. |
||
1170 | */ |
||
1171 | protected function get_default_args( $field_args, $field_group = null ) { |
||
1172 | $args = parent::get_default_args( array(), $this->group ); |
||
1173 | |||
1174 | if ( isset( $field_args['field_args'] ) ) { |
||
1175 | $args = wp_parse_args( $field_args, $args ); |
||
1176 | } else { |
||
1177 | $args['field_args'] = wp_parse_args( $field_args, $this->args ); |
||
1178 | } |
||
1179 | |||
1180 | return $args; |
||
1181 | } |
||
1182 | |||
1183 | /** |
||
1184 | * Returns a cloned version of this field object with, but with |
||
1185 | * modified/overridden field arguments. |
||
1186 | * |
||
1187 | * @since 2.2.2 |
||
1188 | * @param array $field_args Array of field arguments, or entire array of |
||
1189 | * arguments for CMB2_Field |
||
1190 | * |
||
1191 | * @return CMB2_Field The new CMB2_Field instance. |
||
1192 | */ |
||
1193 | public function get_field_clone( $field_args ) { |
||
1196 | |||
1197 | /** |
||
1198 | * Returns the CMB2 instance this field is registered to. |
||
1199 | * |
||
1200 | * @since 2.2.2 |
||
1201 | * |
||
1202 | * @return CMB2|WP_Error If new CMB2_Field is called without cmb_id arg, returns error. |
||
1203 | */ |
||
1204 | public function get_cmb() { |
||
1205 | if ( ! $this->cmb_id ) { |
||
1206 | return new WP_Error( 'no_cmb_id', esc_html__( 'Sorry, this field does not have a cmb_id specified.', 'cmb2' ) ); |
||
1207 | } |
||
1208 | |||
1209 | return cmb2_get_metabox( $this->cmb_id, $this->object_id, $this->object_type ); |
||
1210 | } |
||
1211 | |||
1212 | /** |
||
1213 | * Converts deprecated field parameters to the current/proper parameter, and throws a deprecation notice. |
||
1214 | * @since 2.2.3 |
||
1215 | * @param array $args Metabox field config array. |
||
1216 | * @param array Modified field config array. |
||
1217 | */ |
||
1218 | protected function convert_deprecated_params( $args ) { |
||
1219 | |||
1220 | if ( isset( $args['row_classes'] ) ) { |
||
1221 | |||
1222 | // We'll let this one be. |
||
1261 | |||
1262 | } |
||
1263 |
You can fix this by adding a namespace to your class:
When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.