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 | * @var mixed |
||
50 | * @since 1.1.0 |
||
51 | */ |
||
52 | public $escaped_value = null; |
||
53 | |||
54 | /** |
||
55 | * Grouped Field's current numeric index during the save process |
||
56 | * @var mixed |
||
57 | * @since 2.0.0 |
||
58 | */ |
||
59 | public $index = 0; |
||
60 | |||
61 | /** |
||
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 | * @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 | 'before_group_row', |
||
98 | 'before_row', |
||
99 | 'before', |
||
100 | 'before_field', |
||
101 | 'after_field', |
||
102 | 'after', |
||
103 | '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 | */ |
||
113 | 109 | public function __construct( $args ) { |
|
114 | |||
115 | 109 | if ( ! empty( $args['group_field'] ) ) { |
|
116 | 3 | $this->group = $args['group_field']; |
|
117 | 3 | $this->object_id = $this->group->object_id; |
|
118 | 3 | $this->object_type = $this->group->object_type; |
|
119 | 3 | $this->cmb_id = $this->group->cmb_id; |
|
120 | 3 | } else { |
|
121 | 109 | $this->object_id = isset( $args['object_id'] ) && '_' !== $args['object_id'] ? $args['object_id'] : 0; |
|
122 | 109 | $this->object_type = isset( $args['object_type'] ) ? $args['object_type'] : 'post'; |
|
123 | |||
124 | 109 | if ( isset( $args['cmb_id'] ) ) { |
|
125 | 40 | $this->cmb_id = $args['cmb_id']; |
|
126 | 40 | } |
|
127 | } |
||
128 | |||
129 | 109 | $this->args = $this->_set_field_defaults( $args['field_args'], $args ); |
|
130 | |||
131 | 109 | if ( $this->object_id ) { |
|
132 | 103 | $this->value = $this->get_data(); |
|
133 | 103 | } |
|
134 | 109 | } |
|
135 | |||
136 | /** |
||
137 | * 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 | * @param array $arguments Array of passed-in arguments |
||
141 | * @return mixed Value of field argument |
||
142 | */ |
||
143 | 97 | public function __call( $name, $arguments ) { |
|
144 | 97 | if ( 'string' === $name ) { |
|
145 | return call_user_func_array( array( $this, 'get_string' ), $arguments ); |
||
146 | } |
||
147 | |||
148 | 97 | $key = isset( $arguments[0] ) ? $arguments[0] : false; |
|
149 | 97 | return $this->args( $name, $key ); |
|
150 | } |
||
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 | 107 | public function id( $raw = false ) { |
|
159 | 107 | $id = $raw ? '_id' : 'id'; |
|
160 | 107 | return $this->args( $id ); |
|
161 | 1 | } |
|
162 | |||
163 | /** |
||
164 | * Get a field argument |
||
165 | * @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 | 110 | public function args( $key = '', $_key = '' ) { |
|
184 | |||
185 | /** |
||
186 | * Retrieve a portion of a field property |
||
187 | * @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 | 110 | public function _data( $var, $key = '' ) { |
|
193 | 110 | $vars = $this->{$var}; |
|
194 | 110 | if ( $key ) { |
|
195 | 110 | return array_key_exists( $key, $vars ) ? $vars[ $key ] : false; |
|
196 | } |
||
197 | 59 | 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 | 46 | 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 | 105 | public function get_data( $field_id = '', $args = array() ) { |
|
218 | 105 | if ( $field_id ) { |
|
219 | $args['field_id'] = $field_id; |
||
220 | 105 | } else if ( $this->group ) { |
|
221 | $args['field_id'] = $this->group->id(); |
||
222 | } |
||
223 | |||
224 | 105 | $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 | 105 | $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 | 105 | $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 | 105 | if ( 'cmb2_field_no_override_val' === $data ) { |
|
266 | 102 | $data = 'options-page' === $a['type'] |
|
267 | 102 | ? cmb2_options( $a['id'] )->get( $a['field_id'] ) |
|
268 | 102 | : get_metadata( $a['type'], $a['id'], $a['field_id'], ( $a['single'] || $a['repeat'] ) ); |
|
269 | 102 | } |
|
270 | |||
271 | 105 | 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 | 105 | 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 | 11 | public function update_data( $new_value, $single = true ) { |
|
351 | |||
352 | /** |
||
353 | * Removes/updates metadata/option data |
||
354 | * @since 1.0.1 |
||
355 | * @param string $old Old value |
||
356 | */ |
||
357 | 3 | 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 | 105 | public function data_args( $args = array() ) { |
|
430 | |||
431 | /** |
||
432 | * 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 | */ |
||
437 | 13 | public function sanitization_cb( $meta_value ) { |
|
480 | |||
481 | /** |
||
482 | * 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 | 2 | public function save_field_from_data( array $data_to_save ) { |
|
496 | |||
497 | /** |
||
498 | * Sanitize/store a value to this field |
||
499 | * @since 2.0.0 |
||
500 | * @param array $meta_value Desired value to sanitize/store |
||
501 | * @return array|int|bool Result of save. false on failure |
||
502 | */ |
||
503 | 12 | public function save_field( $meta_value ) { |
|
504 | |||
505 | 12 | $updated = false; |
|
506 | 12 | $action = ''; |
|
507 | 12 | $new_value = $this->sanitization_cb( $meta_value ); |
|
508 | |||
509 | 12 | if ( ! $this->args( 'save_field' ) ) { |
|
510 | |||
511 | // Nothing to see here. |
||
512 | 1 | $action = 'disabled'; |
|
513 | |||
514 | 12 | } elseif ( $this->args( 'multiple' ) && ! $this->args( 'repeatable' ) && ! $this->group ) { |
|
515 | |||
516 | 1 | $this->remove_data(); |
|
517 | 1 | $count = 0; |
|
518 | |||
519 | 1 | if ( ! empty( $new_value ) ) { |
|
520 | 1 | foreach ( $new_value as $add_new ) { |
|
521 | 1 | if ( $this->update_data( $add_new, false ) ) { |
|
522 | 1 | $count++; |
|
523 | 1 | } |
|
524 | 1 | } |
|
525 | 1 | } |
|
526 | |||
527 | 1 | $updated = $count ? $count : false; |
|
528 | 1 | $action = 'repeatable'; |
|
529 | |||
530 | 11 | } elseif ( ! CMB2_Utils::isempty( $new_value ) && $new_value !== $this->get_data() ) { |
|
531 | 9 | $updated = $this->update_data( $new_value ); |
|
532 | 9 | $action = 'updated'; |
|
533 | 10 | } elseif ( CMB2_Utils::isempty( $new_value ) ) { |
|
534 | 2 | $updated = $this->remove_data(); |
|
535 | 2 | $action = 'removed'; |
|
536 | 2 | } |
|
537 | |||
538 | 12 | if ( $updated ) { |
|
539 | 11 | $this->value = $this->get_data(); |
|
540 | 11 | $this->escaped_value = null; |
|
541 | 11 | } |
|
542 | |||
543 | 12 | $field_id = $this->id( true ); |
|
544 | |||
545 | /** |
||
546 | * Hooks after save field action. |
||
547 | * |
||
548 | * @since 2.2.0 |
||
549 | * |
||
550 | * @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 | */ |
||
555 | 12 | do_action( 'cmb2_save_field', $field_id, $updated, $action, $this ); |
|
556 | |||
557 | /** |
||
558 | * 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 | */ |
||
569 | 11 | do_action( "cmb2_save_field_{$field_id}", $updated, $action, $this ); |
|
570 | |||
571 | 10 | 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 | */ |
||
579 | 46 | public function escaping_exception() { |
|
587 | |||
588 | /** |
||
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 | 4 | public function repeatable_exception( $type ) { |
|
595 | // These types cannot be repeatable. |
||
596 | $internal_fields = array( |
||
597 | // Use file_list instead |
||
598 | 4 | 'file' => 1, |
|
599 | 4 | 'radio' => 1, |
|
600 | 4 | 'title' => 1, |
|
601 | // @todo Ajax load wp_editor: http://wordpress.stackexchange.com/questions/51776/how-to-load-wp-editor-through-ajax-jquery |
||
602 | 4 | 'wysiwyg' => 1, |
|
603 | 4 | 'checkbox' => 1, |
|
604 | 4 | 'radio_inline' => 1, |
|
605 | 4 | 'taxonomy_radio' => 1, |
|
606 | 4 | 'taxonomy_select' => 1, |
|
607 | 4 | 'taxonomy_multicheck' => 1, |
|
608 | 4 | ); |
|
609 | |||
610 | /** |
||
611 | * Filter field types that are non-repeatable. |
||
612 | * |
||
613 | * Note that this does *not* allow overriding the default non-repeatable types. |
||
614 | * |
||
615 | * @since 2.1.1 |
||
616 | * |
||
617 | * @param array $fields Array of fields designated as non-repeatable. Note that the field names are *keys*, |
||
618 | * and not values. The value can be anything, because it is meaningless. Example: |
||
619 | * array( 'my_custom_field' => 1 ) |
||
620 | */ |
||
621 | 4 | $all_fields = array_merge( apply_filters( 'cmb2_non_repeatable_fields', array() ), $internal_fields ); |
|
622 | 4 | return isset( $all_fields[ $type ] ); |
|
623 | } |
||
624 | |||
625 | /** |
||
626 | * Escape the value before output. Defaults to 'esc_attr()' |
||
627 | * @since 1.0.1 |
||
628 | * @param callable $func Escaping function (if not esc_attr()) |
||
629 | * @param mixed $meta_value Meta value |
||
630 | * @return mixed Final value |
||
631 | */ |
||
632 | 46 | public function escaped_value( $func = 'esc_attr', $meta_value = '' ) { |
|
672 | |||
673 | /** |
||
674 | * Return non-empty value or field default if value IS empty |
||
675 | * @since 2.0.0 |
||
676 | * @param mixed $meta_value Field value |
||
677 | * @return mixed Field value, or default value |
||
678 | */ |
||
679 | 46 | public function val_or_default( $meta_value ) { |
|
682 | |||
683 | /** |
||
684 | * Offset a time value based on timezone |
||
685 | * @since 1.0.0 |
||
686 | * @return string Offset time string |
||
687 | */ |
||
688 | public function field_timezone_offset() { |
||
691 | |||
692 | /** |
||
693 | * Return timezone string |
||
694 | * @since 1.0.0 |
||
695 | * @return string Timezone string |
||
696 | */ |
||
697 | public function field_timezone() { |
||
711 | |||
712 | /** |
||
713 | * Format the timestamp field value based on the field date/time format arg |
||
714 | * @since 2.0.0 |
||
715 | * @param int $meta_value Timestamp |
||
716 | * @param string $format Either date_format or time_format |
||
717 | * @return string Formatted date |
||
718 | */ |
||
719 | 10 | public function format_timestamp( $meta_value, $format = 'date_format' ) { |
|
722 | |||
723 | /** |
||
724 | * Return a formatted timestamp for a field |
||
725 | * @since 2.0.0 |
||
726 | * @param string $format Either date_format or time_format |
||
727 | * @param string $meta_value Optional meta value to check |
||
728 | * @return string Formatted date |
||
729 | */ |
||
730 | 10 | public function get_timestamp_format( $format = 'date_format', $meta_value = 0 ) { |
|
742 | |||
743 | /** |
||
744 | * Get timestamp from text date |
||
745 | * @since 2.2.0 |
||
746 | * @param string $value Date value |
||
747 | * @return mixed Unix timestamp representing the date. |
||
748 | */ |
||
749 | public function get_timestamp_from_value( $value ) { |
||
752 | |||
753 | /** |
||
754 | * Get field render callback and Render the field row |
||
755 | * @since 1.0.0 |
||
756 | */ |
||
757 | 10 | public function render_field() { |
|
758 | 10 | $this->render_context = 'edit'; |
|
759 | |||
760 | 10 | $this->peform_param_callback( 'render_row_cb' ); |
|
761 | |||
762 | // For chaining |
||
763 | 10 | return $this; |
|
764 | } |
||
765 | |||
766 | /** |
||
767 | * Default field render callback |
||
768 | * @since 2.1.1 |
||
769 | */ |
||
770 | 9 | public function render_field_callback() { |
|
814 | |||
815 | /** |
||
816 | * The default label_cb callback (if not a title field) |
||
817 | * |
||
818 | * @since 2.1.1 |
||
819 | * @return string Label html markup |
||
820 | */ |
||
821 | 9 | public function label() { |
|
830 | |||
831 | /** |
||
832 | * Defines the classes for the current CMB2 field row |
||
833 | * |
||
834 | * @since 2.0.0 |
||
835 | * @return string Space concatenated list of classes |
||
836 | */ |
||
837 | 45 | public function row_classes() { |
|
838 | |||
839 | 45 | $classes = array(); |
|
840 | |||
841 | /** |
||
842 | * By default, 'text_url' and 'text' fields get table-like styling |
||
843 | * |
||
844 | * @since 2.0.0 |
||
845 | * |
||
846 | * @param array $field_types The types of fields which should get the 'table-layout' class |
||
847 | */ |
||
848 | 45 | $repeat_table_rows_types = apply_filters( 'cmb2_repeat_table_row_types', array( |
|
849 | 45 | 'text_url', 'text', |
|
850 | 45 | ) ); |
|
851 | |||
852 | $conditional_classes = array( |
||
853 | 45 | 'cmb-type-' . str_replace( '_', '-', sanitize_html_class( $this->type() ) ) => true, |
|
854 | 45 | 'cmb2-id-' . str_replace( '_', '-', sanitize_html_class( $this->id() ) ) => true, |
|
855 | 45 | 'cmb-repeat' => $this->args( 'repeatable' ), |
|
856 | 45 | 'cmb-repeat-group-field' => $this->group, |
|
857 | 45 | 'cmb-inline' => $this->args( 'inline' ), |
|
858 | 45 | 'table-layout' => 'edit' === $this->render_context && in_array( $this->type(), $repeat_table_rows_types ), |
|
859 | 45 | ); |
|
860 | |||
861 | 45 | foreach ( $conditional_classes as $class => $condition ) { |
|
862 | 45 | if ( $condition ) { |
|
863 | 45 | $classes[] = $class; |
|
864 | 45 | } |
|
865 | 45 | } |
|
866 | |||
867 | 45 | if ( $added_classes = $this->args( 'classes' ) ) { |
|
868 | 1 | $added_classes = is_array( $added_classes ) ? implode( ' ', $added_classes ) : (string) $added_classes; |
|
869 | 45 | } elseif ( $added_classes = $this->get_param_callback_result( 'classes_cb' ) ) { |
|
870 | 2 | $added_classes = is_array( $added_classes ) ? implode( ' ', $added_classes ) : (string) $added_classes; |
|
871 | 2 | } |
|
872 | |||
873 | 45 | if ( $added_classes ) { |
|
874 | 3 | $classes[] = esc_attr( $added_classes ); |
|
875 | 3 | } |
|
876 | |||
877 | /** |
||
878 | * Globally filter row classes |
||
879 | * |
||
880 | * @since 2.0.0 |
||
881 | * |
||
882 | * @param string $classes Space-separated list of row classes |
||
883 | * @param CMB2_Field object $field This field object |
||
884 | */ |
||
885 | 45 | return apply_filters( 'cmb2_row_classes', implode( ' ', $classes ), $this ); |
|
886 | } |
||
887 | |||
888 | |||
889 | |||
890 | /** |
||
891 | * Get field display callback and render the display value in the column. |
||
892 | * @since 2.2.2 |
||
893 | */ |
||
894 | 33 | public function render_column() { |
|
895 | 33 | $this->render_context = 'display'; |
|
896 | |||
897 | 33 | $this->peform_param_callback( 'display_cb' ); |
|
898 | |||
899 | // For chaining |
||
900 | 33 | return $this; |
|
901 | } |
||
902 | |||
903 | /** |
||
904 | * Default callback to outputs field value in a display format. |
||
905 | * @since 2.2.2 |
||
906 | */ |
||
907 | 33 | public function display_value_callback() { |
|
908 | // If field is requesting to be conditionally shown |
||
909 | 33 | if ( ! $this->should_show() ) { |
|
910 | return; |
||
911 | } |
||
912 | |||
913 | 33 | $display = new CMB2_Field_Display( $this ); |
|
914 | |||
915 | /** |
||
916 | * A filter to bypass the default display. |
||
917 | * |
||
918 | * The dynamic portion of the hook name, $this->type(), refers to the field type. |
||
919 | * |
||
920 | * Passing a non-null value to the filter will short-circuit the default display. |
||
921 | * |
||
922 | * @param bool|mixed $pre_output Default null value. |
||
923 | * @param CMB2_Field $field This field object. |
||
924 | * @param CMB2_Field_Display $display The `CMB2_Field_Display` object. |
||
925 | */ |
||
926 | 33 | $pre_output = apply_filters( "cmb2_pre_field_display_{$this->type()}", null, $this, $display ); |
|
927 | |||
928 | 33 | if ( null !== $pre_output ) { |
|
929 | echo $pre_output; |
||
930 | return; |
||
931 | } |
||
932 | |||
933 | 33 | $this->peform_param_callback( 'before_display_wrap' ); |
|
934 | |||
935 | 33 | printf( "<div class=\"cmb-column %s\" data-fieldtype=\"%s\">\n", $this->row_classes( 'display' ), $this->type() ); |
|
936 | |||
937 | 33 | $this->peform_param_callback( 'before_display' ); |
|
938 | |||
939 | 33 | CMB2_Field_Display::get( $this )->display(); |
|
940 | |||
941 | 33 | $this->peform_param_callback( 'after_display' ); |
|
942 | |||
943 | 33 | echo "\n</div>"; |
|
944 | |||
945 | 33 | $this->peform_param_callback( 'after_display_wrap' ); |
|
946 | |||
947 | // For chaining |
||
948 | 33 | return $this; |
|
949 | } |
||
950 | |||
951 | /** |
||
952 | * Replaces a hash key - {#} - with the repeatable index |
||
953 | * @since 1.2.0 |
||
954 | * @param string $value Value to update |
||
955 | * @return string Updated value |
||
956 | */ |
||
957 | 2 | public function replace_hash( $value ) { |
|
961 | |||
962 | /** |
||
963 | * Retrieve text parameter from field's text array (if it has one), or use fallback text |
||
964 | * For back-compatibility, falls back to checking the options array. |
||
965 | * |
||
966 | * @since 2.2.2 |
||
967 | * @param string $text_key Key in field's text array |
||
968 | * @param string $fallback Fallback text |
||
969 | * @return string Text |
||
970 | */ |
||
971 | 8 | public function get_string( $text_key, $fallback ) { |
|
995 | |||
996 | /** |
||
997 | * Retrieve options args. Calls options_cb if it exists. |
||
998 | * @since 2.0.0 |
||
999 | * @param string $key Specific option to retrieve |
||
1000 | * @return array Array of options |
||
1001 | */ |
||
1002 | 31 | public function options( $key = '' ) { |
|
1003 | 31 | if ( ! empty( $this->field_options ) ) { |
|
1004 | 5 | if ( $key ) { |
|
1005 | 5 | return array_key_exists( $key, $this->field_options ) ? $this->field_options[ $key ] : false; |
|
1006 | } |
||
1007 | |||
1008 | 1 | return $this->field_options; |
|
1009 | } |
||
1010 | |||
1011 | 31 | $this->field_options = (array) $this->args['options']; |
|
1012 | |||
1013 | 31 | View Code Duplication | if ( is_callable( $this->args['options_cb'] ) ) { |
1014 | 1 | $options = call_user_func( $this->args['options_cb'], $this ); |
|
1015 | |||
1016 | 1 | if ( $options && is_array( $options ) ) { |
|
1017 | 1 | $this->field_options = $options + $this->field_options; |
|
1018 | 1 | } |
|
1019 | 1 | } |
|
1020 | |||
1021 | 31 | if ( $key ) { |
|
1022 | 10 | return array_key_exists( $key, $this->field_options ) ? $this->field_options[ $key ] : false; |
|
1023 | } |
||
1024 | |||
1025 | 23 | return $this->field_options; |
|
1026 | } |
||
1027 | |||
1028 | /** |
||
1029 | * Store JS dependencies as part of the field args. |
||
1030 | * @since 2.2.0 |
||
1031 | * @param array $dependencies Dependies to register for this field. |
||
1032 | */ |
||
1033 | 12 | public function add_js_dependencies( $dependencies = array() ) { |
|
1034 | 12 | foreach ( (array) $dependencies as $dependency ) { |
|
1035 | 12 | $this->args['js_dependencies'][ $dependency ] = $dependency; |
|
1036 | 12 | } |
|
1037 | |||
1038 | 12 | CMB2_JS::add_dependencies( $dependencies ); |
|
1039 | 12 | } |
|
1040 | |||
1041 | /** |
||
1042 | * Get CMB2_Field default value, either from default param or default_cb param. |
||
1043 | * |
||
1044 | * @since 0.2.2 |
||
1045 | * |
||
1046 | * @return mixed Default field value |
||
1047 | */ |
||
1048 | 33 | public function get_default() { |
|
1049 | 33 | if ( null !== $this->args['default'] ) { |
|
1050 | 6 | return $this->args['default']; |
|
1051 | } |
||
1052 | |||
1053 | 32 | $param = is_callable( $this->args['default_cb'] ) ? 'default_cb' : 'default'; |
|
1054 | 32 | $default = $this->get_param_callback_result( $param ); |
|
1055 | |||
1056 | // Allow a filter override of the default value |
||
1057 | 32 | $this->args['default'] = apply_filters( 'cmb2_default_filter', $default, $this ); |
|
1058 | |||
1059 | 32 | return $this->args['default']; |
|
1060 | } |
||
1061 | |||
1062 | /** |
||
1063 | * Fills in empty field parameters with defaults |
||
1064 | * @since 1.1.0 |
||
1065 | * @param array $args Metabox field config array |
||
1066 | * @param array Modified field config array. |
||
1067 | */ |
||
1068 | 109 | public function _set_field_defaults( $args ) { |
|
1069 | |||
1070 | // Set up blank or default values for empty ones |
||
1071 | 109 | $args = wp_parse_args( $args, array( |
|
1072 | 109 | 'type' => '', |
|
1073 | 109 | 'name' => '', |
|
1074 | 109 | 'desc' => '', |
|
1075 | 109 | 'before' => '', |
|
1076 | 109 | 'after' => '', |
|
1077 | 109 | 'options' => array(), |
|
1078 | 109 | 'options_cb' => '', |
|
1079 | 109 | 'text' => array(), |
|
1080 | 109 | 'text_cb' => '', |
|
1081 | 109 | 'attributes' => array(), |
|
1082 | 109 | 'protocols' => null, |
|
1083 | 109 | 'default' => null, |
|
1084 | 109 | 'default_cb' => '', |
|
1085 | 109 | 'classes' => null, |
|
1086 | 109 | 'classes_cb' => '', |
|
1087 | 109 | 'select_all_button' => true, |
|
1088 | 109 | 'multiple' => false, |
|
1089 | 109 | 'repeatable' => isset( $args['type'] ) && 'group' == $args['type'], |
|
1090 | 109 | 'inline' => false, |
|
1091 | 109 | 'on_front' => true, |
|
1092 | 109 | 'show_names' => true, |
|
1093 | 109 | 'save_field' => true, // Will not save if false |
|
1094 | 109 | 'date_format' => 'm\/d\/Y', |
|
1095 | 109 | 'time_format' => 'h:i A', |
|
1096 | 109 | 'description' => isset( $args['desc'] ) ? $args['desc'] : '', |
|
1097 | 109 | 'preview_size' => 'file' == $args['type'] ? array( 350, 350 ) : array( 50, 50 ), |
|
1098 | 109 | 'render_row_cb' => array( $this, 'render_field_callback' ), |
|
1099 | 109 | 'display_cb' => array( $this, 'display_value_callback' ), |
|
1100 | 109 | 'label_cb' => 'title' != $args['type'] ? array( $this, 'label' ) : '', |
|
1101 | 109 | 'column' => false, |
|
1102 | 109 | 'js_dependencies' => array(), |
|
1103 | 109 | 'show_in_rest' => null, |
|
1104 | 109 | ) ); |
|
1105 | |||
1106 | /* |
||
1107 | * Deprecated usage: |
||
1108 | * |
||
1109 | * 'std' -- use 'default' (no longer works) |
||
1110 | * 'row_classes' -- use 'class', or 'class_cb' |
||
1111 | * 'default' -- as callback (use default_cb) |
||
1112 | */ |
||
1113 | 109 | $args = $this->convert_deprecated_params( $args ); |
|
1114 | |||
1115 | 109 | $args['repeatable'] = $args['repeatable'] && ! $this->repeatable_exception( $args['type'] ); |
|
1116 | 109 | $args['inline'] = $args['inline'] || false !== stripos( $args['type'], '_inline' ); |
|
1117 | |||
1118 | 109 | $args['options'] = 'group' == $args['type'] ? wp_parse_args( $args['options'], array( |
|
1119 | 3 | 'add_button' => esc_html__( 'Add Group', 'cmb2' ), |
|
1120 | 3 | 'remove_button' => esc_html__( 'Remove Group', 'cmb2' ), |
|
1121 | 109 | ) ) : $args['options']; |
|
1122 | |||
1123 | 109 | $args['_id'] = $args['id']; |
|
1124 | 109 | $args['_name'] = $args['id']; |
|
1125 | |||
1126 | 109 | if ( $this->group ) { |
|
1127 | |||
1128 | 3 | $args['id'] = $this->group->args( 'id' ) . '_' . $this->group->index . '_' . $args['id']; |
|
1129 | 3 | $args['_name'] = $this->group->args( 'id' ) . '[' . $this->group->index . '][' . $args['_name'] . ']'; |
|
1130 | 3 | } |
|
1131 | |||
1132 | 109 | if ( 'wysiwyg' == $args['type'] ) { |
|
1133 | 1 | $args['id'] = strtolower( str_ireplace( '-', '_', $args['id'] ) ); |
|
1134 | 1 | $args['options']['textarea_name'] = $args['_name']; |
|
1135 | 1 | } |
|
1136 | |||
1137 | 109 | $option_types = apply_filters( 'cmb2_all_or_nothing_types', array( 'select', 'radio', 'radio_inline', 'taxonomy_select', 'taxonomy_radio', 'taxonomy_radio_inline' ), $this ); |
|
1138 | |||
1139 | 109 | if ( in_array( $args['type'], $option_types, true ) ) { |
|
1140 | |||
1141 | 16 | $args['show_option_none'] = isset( $args['show_option_none'] ) ? $args['show_option_none'] : null; |
|
1142 | 16 | $args['show_option_none'] = true === $args['show_option_none'] ? esc_html__( 'None', 'cmb2' ) : $args['show_option_none']; |
|
1143 | |||
1144 | 16 | if ( null === $args['show_option_none'] ) { |
|
1145 | 15 | $off_by_default = in_array( $args['type'], array( 'select', 'radio', 'radio_inline' ), true ); |
|
1146 | 15 | $args['show_option_none'] = $off_by_default ? false : esc_html__( 'None', 'cmb2' ); |
|
1147 | 15 | } |
|
1148 | |||
1149 | 16 | } |
|
1150 | |||
1151 | 109 | $args['has_supporting_data'] = in_array( |
|
1152 | 109 | $args['type'], |
|
1153 | array( |
||
1154 | // CMB2_Sanitize::_save_file_id_value()/CMB2_Sanitize::_get_group_file_value_array() |
||
1155 | 109 | 'file', |
|
1156 | // See CMB2_Sanitize::_save_utc_value() |
||
1157 | 109 | 'text_datetime_timestamp_timezone', |
|
1158 | 109 | ), |
|
1159 | true |
||
1160 | 109 | ); |
|
1161 | |||
1162 | 109 | return $args; |
|
1163 | } |
||
1164 | |||
1165 | /** |
||
1166 | * Get default field arguments specific to this CMB2 object. |
||
1167 | * @since 2.2.0 |
||
1168 | * @param array $field_args Metabox field config array. |
||
1169 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
1170 | * @return array Array of field arguments. |
||
1171 | */ |
||
1172 | 5 | protected function get_default_args( $field_args, $field_group = null ) { |
|
1173 | 5 | $args = parent::get_default_args( array(), $this->group ); |
|
1174 | |||
1175 | 5 | if ( isset( $field_args['field_args'] ) ) { |
|
1176 | $args = wp_parse_args( $field_args, $args ); |
||
1177 | } else { |
||
1178 | 5 | $args['field_args'] = wp_parse_args( $field_args, $this->args ); |
|
1179 | } |
||
1180 | |||
1181 | 5 | return $args; |
|
1182 | } |
||
1183 | |||
1184 | /** |
||
1185 | * Returns a cloned version of this field object with, but with |
||
1186 | * modified/overridden field arguments. |
||
1187 | * |
||
1188 | * @since 2.2.2 |
||
1189 | * @param array $field_args Array of field arguments, or entire array of |
||
1190 | * arguments for CMB2_Field |
||
1191 | * |
||
1192 | * @return CMB2_Field The new CMB2_Field instance. |
||
1193 | */ |
||
1194 | 5 | public function get_field_clone( $field_args ) { |
|
1195 | 5 | return $this->get_new_field( $field_args ); |
|
1196 | } |
||
1197 | |||
1198 | /** |
||
1199 | * Returns the CMB2 instance this field is registered to. |
||
1200 | * |
||
1201 | * @since 2.2.2 |
||
1202 | * |
||
1203 | * @return CMB2|WP_Error If new CMB2_Field is called without cmb_id arg, returns error. |
||
1204 | */ |
||
1205 | 1 | public function get_cmb() { |
|
1212 | |||
1213 | /** |
||
1214 | * Converts deprecated field parameters to the current/proper parameter, and throws a deprecation notice. |
||
1215 | * @since 2.2.3 |
||
1216 | * @param array $args Metabox field config array. |
||
1217 | * @param array Modified field config array. |
||
1218 | */ |
||
1219 | 109 | protected function convert_deprecated_params( $args ) { |
|
1220 | |||
1221 | 109 | if ( isset( $args['row_classes'] ) ) { |
|
1222 | |||
1223 | 2 | $this->deprecated_param( __CLASS__ . '::__construct()', '2.2.3', self::DEPRECATED_PARAM, 'row_classes', 'classes' ); |
|
1224 | |||
1225 | // row_classes param could be a callback |
||
1226 | 2 | if ( is_callable( $args['row_classes'] ) ) { |
|
1227 | |||
1228 | 1 | $this->deprecated_param( __CLASS__ . '::__construct()', '2.2.3', self::DEPRECATED_CB_PARAM, 'row_classes', 'classes_cb' ); |
|
1229 | |||
1262 | |||
1263 | } |
||
1264 |
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.