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 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, and based on these observations, apply Extract Interface, too.
1 | <?php |
||
17 | class CMB2 extends CMB2_Base { |
||
|
|||
18 | |||
19 | /** |
||
20 | * The object properties name. |
||
21 | * @var string |
||
22 | * @since 2.2.3 |
||
23 | */ |
||
24 | protected $properties_name = 'meta_box'; |
||
25 | |||
26 | /** |
||
27 | * Metabox Config array |
||
28 | * @var array |
||
29 | * @since 0.9.0 |
||
30 | */ |
||
31 | protected $meta_box = array(); |
||
32 | |||
33 | /** |
||
34 | * Type of object registered for metabox. (e.g., post, user, or comment) |
||
35 | * @var string |
||
36 | * @since 1.0.0 |
||
37 | */ |
||
38 | protected $mb_object_type = null; |
||
39 | |||
40 | /** |
||
41 | * List of fields that are changed/updated on save |
||
42 | * @var array |
||
43 | * @since 1.1.0 |
||
44 | */ |
||
45 | protected $updated = array(); |
||
46 | |||
47 | /** |
||
48 | * Metabox Defaults |
||
49 | * @var array |
||
50 | * @since 1.0.1 |
||
51 | */ |
||
52 | protected $mb_defaults = array( |
||
53 | 'id' => '', |
||
54 | 'title' => '', |
||
55 | // Post type slug, or 'user', 'term', 'comment', or 'options-page' |
||
56 | 'object_types' => array(), |
||
57 | /* |
||
58 | * The context within the screen where the boxes should display. Available contexts vary |
||
59 | * from screen to screen. Post edit screen contexts include 'normal', 'side', and 'advanced'. |
||
60 | * |
||
61 | * For placement in locations outside of a metabox, other options include: |
||
62 | * 'form_top', 'before_permalink', 'after_title', 'after_editor' |
||
63 | * |
||
64 | * Comments screen contexts include 'normal' and 'side'. Default is 'normal'. |
||
65 | */ |
||
66 | 'context' => 'normal', |
||
67 | 'priority' => 'high', |
||
68 | 'show_names' => true, // Show field names on the left |
||
69 | 'show_on_cb' => null, // Callback to determine if metabox should display. |
||
70 | 'show_on' => array(), // Post IDs or page templates to display this metabox. overrides 'show_on_cb' |
||
71 | 'cmb_styles' => true, // Include CMB2 stylesheet |
||
72 | 'enqueue_js' => true, // Include CMB2 JS |
||
73 | 'fields' => array(), |
||
74 | 'hookup' => true, |
||
75 | 'save_fields' => true, // Will not save during hookup if false |
||
76 | 'closed' => false, // Default to metabox being closed? |
||
77 | 'taxonomies' => array(), |
||
78 | 'new_user_section' => 'add-new-user', // or 'add-existing-user' |
||
79 | 'new_term_section' => true, |
||
80 | 'show_in_rest' => false, |
||
81 | ); |
||
82 | |||
83 | /** |
||
84 | * Metabox field objects |
||
85 | * @var array |
||
86 | * @since 2.0.3 |
||
87 | */ |
||
88 | protected $fields = array(); |
||
89 | |||
90 | /** |
||
91 | * An array of hidden fields to output at the end of the form |
||
92 | * @var array |
||
93 | * @since 2.0.0 |
||
94 | */ |
||
95 | protected $hidden_fields = array(); |
||
96 | |||
97 | /** |
||
98 | * Array of key => value data for saving. Likely $_POST data. |
||
99 | * @var string |
||
100 | * @since 2.0.0 |
||
101 | */ |
||
102 | protected $generated_nonce = ''; |
||
103 | |||
104 | /** |
||
105 | * Whether there are fields to be shown in columns. Set in CMB2::add_field(). |
||
106 | * @var bool |
||
107 | * @since 2.2.2 |
||
108 | */ |
||
109 | protected $has_columns = false; |
||
110 | |||
111 | /** |
||
112 | * If taxonomy field is requesting to remove_default, we store the taxonomy here. |
||
113 | * @var array |
||
114 | * @since 2.2.3 |
||
115 | */ |
||
116 | protected $tax_metaboxes_to_remove = array(); |
||
117 | |||
118 | /** |
||
119 | * Get started |
||
120 | * @since 0.4.0 |
||
121 | * @param array $config Metabox config array |
||
122 | * @param integer $object_id Optional object id |
||
123 | */ |
||
124 | public function __construct( $config, $object_id = 0 ) { |
||
152 | |||
153 | /** |
||
154 | * Loops through and displays fields |
||
155 | * @since 1.0.0 |
||
156 | * @param int $object_id Object ID |
||
157 | * @param string $object_type Type of object being saved. (e.g., post, user, or comment) |
||
158 | */ |
||
159 | public function show_form( $object_id = 0, $object_type = '' ) { |
||
168 | |||
169 | /** |
||
170 | * Outputs the opening form markup and runs corresponding hooks: |
||
171 | * 'cmb2_before_form' and "cmb2_before_{$object_type}_form_{$this->cmb_id}" |
||
172 | * @since 2.2.0 |
||
173 | * @param integer $object_id Object ID |
||
174 | * @param string $object_type Object type |
||
175 | * @return void |
||
176 | */ |
||
177 | public function render_form_open( $object_id = 0, $object_type = '' ) { |
||
215 | |||
216 | /** |
||
217 | * Defines the classes for the CMB2 form/wrap. |
||
218 | * |
||
219 | * @since 2.0.0 |
||
220 | * @return string Space concatenated list of classes |
||
221 | */ |
||
222 | public function box_classes() { |
||
223 | |||
224 | $classes = array( 'cmb2-wrap', 'form-table' ); |
||
225 | |||
226 | // Use the callback to fetch classes. |
||
227 | View Code Duplication | if ( $added_classes = $this->get_param_callback_result( 'classes_cb' ) ) { |
|
228 | $added_classes = is_array( $added_classes ) ? $added_classes : array( $added_classes ); |
||
229 | $classes = array_merge( $classes, $added_classes ); |
||
230 | } |
||
231 | |||
232 | View Code Duplication | if ( $added_classes = $this->prop( 'classes' ) ) { |
|
233 | $added_classes = is_array( $added_classes ) ? $added_classes : array( $added_classes ); |
||
234 | $classes = array_merge( $classes, $added_classes ); |
||
235 | } |
||
236 | |||
237 | /** |
||
238 | * Add our context classes for non-standard metaboxes. |
||
239 | * |
||
240 | * @since 2.2.4 |
||
241 | */ |
||
242 | if ( ! empty( $this->prop( 'context' ) ) && in_array( $this->prop( 'context' ), array( 'form_top', 'before_permalink', 'after_title', 'after_editor' ) ) ) { |
||
243 | |||
244 | // Include the postbox wrapper if we have no title, unless that property is set to false. |
||
245 | if ( empty( $this->prop( 'title' ) ) && empty( $this->prop( 'remove_box_wrap' ) ) ) { |
||
246 | $context[] = 'postbox cmb2-context-wrap-no-title'; |
||
247 | } |
||
248 | |||
249 | // Include a generic context wrapper. |
||
250 | $context[] = 'cmb2-context-wrap'; |
||
251 | |||
252 | // Include a context-type based context wrapper. |
||
253 | $context[] = 'cmb2-context-wrap-' . $this->prop( 'context' ); |
||
254 | |||
255 | // Include an ID based context wrapper as well. |
||
256 | $context[] = 'cmb2-context-wrap-' . $this->prop( 'id' ); |
||
257 | |||
258 | // And merge all the classes back into the array. |
||
259 | $classes = array_merge( $classes, $context ); |
||
260 | } |
||
261 | |||
262 | /** |
||
263 | * Globally filter box wrap classes |
||
264 | * |
||
265 | * @since 2.2.2 |
||
266 | * |
||
267 | * @param string $classes Array of classes for the cmb2-wrap. |
||
268 | * @param CMB2 $cmb This CMB2 object. |
||
269 | */ |
||
270 | $classes = apply_filters( 'cmb2_wrap_classes', $classes, $this ); |
||
271 | |||
272 | // Clean up. |
||
273 | $classes = array_map( 'strip_tags', array_filter( $classes ) ); |
||
274 | |||
275 | // Remove any duplicates. |
||
276 | $classes = array_unique( $classes ); |
||
277 | |||
278 | // Make a string. |
||
279 | return implode( ' ', $classes ); |
||
280 | } |
||
281 | |||
282 | /** |
||
283 | * Outputs the closing form markup and runs corresponding hooks: |
||
284 | * 'cmb2_after_form' and "cmb2_after_{$object_type}_form_{$this->cmb_id}" |
||
285 | * @since 2.2.0 |
||
286 | * @param integer $object_id Object ID |
||
287 | * @param string $object_type Object type |
||
288 | * @return void |
||
289 | */ |
||
290 | public function render_form_close( $object_id = 0, $object_type = '' ) { |
||
327 | |||
328 | /** |
||
329 | * Renders a field based on the field type |
||
330 | * @since 2.2.0 |
||
331 | * @param array $field_args A field configuration array. |
||
332 | * @return mixed CMB2_Field object if successful. |
||
333 | */ |
||
334 | public function render_field( $field_args ) { |
||
358 | |||
359 | /** |
||
360 | * Render a repeatable group. |
||
361 | * @param array $args Array of field arguments for a group field parent. |
||
362 | * @return CMB2_Field|null Group field object. |
||
363 | */ |
||
364 | public function render_group( $args ) { |
||
418 | |||
419 | /** |
||
420 | * Get the group wrap attributes, which are passed through a filter. |
||
421 | * @since 2.2.3 |
||
422 | * @param CMB2_Field $field_group The group CMB2_Field object. |
||
423 | * @return string The attributes string. |
||
424 | */ |
||
425 | public function group_wrap_attributes( $field_group ) { |
||
450 | |||
451 | /** |
||
452 | * Render a repeatable group row |
||
453 | * @since 1.0.2 |
||
454 | * @param CMB2_Field $field_group CMB2_Field group field object |
||
455 | * @param string $remove_disabled Attribute string to disable the remove button |
||
456 | */ |
||
457 | public function render_group_row( $field_group, $remove_disabled ) { |
||
505 | |||
506 | /** |
||
507 | * Add a hidden field to the list of hidden fields to be rendered later |
||
508 | * @since 2.0.0 |
||
509 | * @param array $field_args Array of field arguments to be passed to CMB2_Field |
||
510 | * @param CMB2_Field|null $field_group CMB2_Field group field object |
||
511 | */ |
||
512 | public function add_hidden_field( $field_args, $field_group = null ) { |
||
530 | |||
531 | /** |
||
532 | * Loop through and output hidden fields |
||
533 | * @since 2.0.0 |
||
534 | */ |
||
535 | public function render_hidden_fields() { |
||
542 | |||
543 | /** |
||
544 | * Returns array of sanitized field values (without saving them) |
||
545 | * @since 2.0.3 |
||
546 | * @param array $data_to_sanitize Array of field_id => value data for sanitizing (likely $_POST data). |
||
547 | */ |
||
548 | public function get_sanitized_values( array $data_to_sanitize ) { |
||
578 | |||
579 | /** |
||
580 | * Loops through and saves field data |
||
581 | * @since 1.0.0 |
||
582 | * @param int $object_id Object ID |
||
583 | * @param string $object_type Type of object being saved. (e.g., post, user, or comment) |
||
584 | * @param array $data_to_save Array of key => value data for saving. Likely $_POST data. |
||
585 | */ |
||
586 | public function save_fields( $object_id = 0, $object_type = '', $data_to_save = array() ) { |
||
602 | |||
603 | /** |
||
604 | * Process and save form fields |
||
605 | * @since 2.0.0 |
||
606 | */ |
||
607 | public function process_fields() { |
||
621 | |||
622 | /** |
||
623 | * Process and save a field |
||
624 | * @since 2.0.0 |
||
625 | * @param array $field_args Array of field arguments |
||
626 | */ |
||
627 | public function process_field( $field_args ) { |
||
654 | |||
655 | public function pre_process() { |
||
670 | |||
671 | public function after_save() { |
||
706 | |||
707 | /** |
||
708 | * Save a repeatable group |
||
709 | * @since 1.x.x |
||
710 | * @param array $args Field arguments array |
||
711 | * @return mixed Return of CMB2_Field::update_data() |
||
712 | */ |
||
713 | public function save_group( $args ) { |
||
720 | |||
721 | /** |
||
722 | * Save a repeatable group |
||
723 | * @since 1.x.x |
||
724 | * @param CMB2_Field $field_group CMB2_Field group field object |
||
725 | * @return mixed Return of CMB2_Field::update_data() |
||
726 | */ |
||
727 | public function save_group_field( $field_group ) { |
||
804 | |||
805 | /** |
||
806 | * Get object id from global space if no id is provided |
||
807 | * @since 1.0.0 |
||
808 | * @param integer $object_id Object ID |
||
809 | * @return integer $object_id Object ID |
||
810 | */ |
||
811 | public function object_id( $object_id = 0 ) { |
||
850 | |||
851 | /** |
||
852 | * Sets the $object_type based on metabox settings |
||
853 | * @since 1.0.0 |
||
854 | * @return string Object type |
||
855 | */ |
||
856 | public function mb_object_type() { |
||
896 | |||
897 | /** |
||
898 | * Gets the box 'object_types' array based on box settings. |
||
899 | * @since 2.2.3 |
||
900 | * @return array Object types |
||
901 | */ |
||
902 | public function box_types() { |
||
905 | |||
906 | /** |
||
907 | * Determines if metabox is for an options page |
||
908 | * @since 1.0.1 |
||
909 | * @return boolean True/False |
||
910 | */ |
||
911 | public function is_options_page_mb() { |
||
914 | |||
915 | /** |
||
916 | * Returns the object type |
||
917 | * @since 1.0.0 |
||
918 | * @return string Object type |
||
919 | */ |
||
920 | public function object_type( $object_type = '' ) { |
||
934 | |||
935 | /** |
||
936 | * Get the object type for the current page, based on the $pagenow global. |
||
937 | * @since 2.2.2 |
||
938 | * @return string Page object type name. |
||
939 | */ |
||
940 | View Code Duplication | public function current_object_type() { |
|
958 | |||
959 | /** |
||
960 | * Set metabox property. |
||
961 | * @since 2.2.2 |
||
962 | * @param string $property Metabox config property to retrieve |
||
963 | * @param mixed $value Value to set if no value found |
||
964 | * @return mixed Metabox config property value or false |
||
965 | */ |
||
966 | public function set_prop( $property, $value ) { |
||
971 | |||
972 | /** |
||
973 | * Get metabox property and optionally set a fallback |
||
974 | * @since 2.0.0 |
||
975 | * @param string $property Metabox config property to retrieve |
||
976 | * @param mixed $fallback Fallback value to set if no value found |
||
977 | * @return mixed Metabox config property value or false |
||
978 | */ |
||
979 | public function prop( $property, $fallback = null ) { |
||
986 | |||
987 | /** |
||
988 | * Get a field object |
||
989 | * @since 2.0.3 |
||
990 | * @param string|array|CMB2_Field $field Metabox field id or field config array or CMB2_Field object |
||
991 | * @param CMB2_Field|null $field_group (optional) CMB2_Field object (group parent) |
||
992 | * @return CMB2_Field|false CMB2_Field object (or false) |
||
993 | */ |
||
994 | public function get_field( $field, $field_group = null ) { |
||
1019 | |||
1020 | /** |
||
1021 | * Handles determining which type of arguments to pass to CMB2_Field |
||
1022 | * @since 2.0.7 |
||
1023 | * @param mixed $field_id Field (or group field) ID |
||
1024 | * @param mixed $field_args Array of field arguments |
||
1025 | * @param mixed $sub_field_id Sub field ID (if field_group exists) |
||
1026 | * @param CMB2_Field|null $field_group If a sub-field, will be the parent group CMB2_Field object |
||
1027 | * @return array Array of CMB2_Field arguments |
||
1028 | */ |
||
1029 | public function get_field_args( $field_id, $field_args, $sub_field_id, $field_group ) { |
||
1046 | |||
1047 | /** |
||
1048 | * Get default field arguments specific to this CMB2 object. |
||
1049 | * @since 2.2.0 |
||
1050 | * @param array $field_args Metabox field config array. |
||
1051 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
1052 | * @return array Array of field arguments. |
||
1053 | */ |
||
1054 | View Code Duplication | protected function get_default_args( $field_args, $field_group = null ) { |
|
1071 | |||
1072 | /** |
||
1073 | * Get a new field object specific to this CMB2 object. |
||
1074 | * @since 2.2.0 |
||
1075 | * @param array $field_args Metabox field config array. |
||
1076 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
1077 | * @return CMB2_Field CMB2_Field object |
||
1078 | */ |
||
1079 | protected function get_new_field( $field_args, $field_group = null ) { |
||
1082 | |||
1083 | /** |
||
1084 | * When fields are added in the old-school way, intitate them as they should be |
||
1085 | * @since 2.1.0 |
||
1086 | * @param array $fields Array of fields to add |
||
1087 | * @param mixed $parent_field_id Parent field id or null |
||
1088 | */ |
||
1089 | protected function add_fields( $fields, $parent_field_id = null ) { |
||
1107 | |||
1108 | /** |
||
1109 | * Add a field to the metabox |
||
1110 | * @since 2.0.0 |
||
1111 | * @param array $field Metabox field config array |
||
1112 | * @param int $position (optional) Position of metabox. 1 for first, etc |
||
1113 | * @return string|false Field id or false |
||
1114 | */ |
||
1115 | public function add_field( array $field, $position = 0 ) { |
||
1141 | |||
1142 | /** |
||
1143 | * Defines a field's column if requesting to be show in admin columns. |
||
1144 | * @since 2.2.3 |
||
1145 | * @param array $field Metabox field config array. |
||
1146 | * @return array Modified metabox field config array. |
||
1147 | */ |
||
1148 | protected function define_field_column( array $field ) { |
||
1160 | |||
1161 | /** |
||
1162 | * Add a field to a group |
||
1163 | * @since 2.0.0 |
||
1164 | * @param string $parent_field_id The field id of the group field to add the field |
||
1165 | * @param array $field Metabox field config array |
||
1166 | * @param int $position (optional) Position of metabox. 1 for first, etc |
||
1167 | * @return mixed Array of parent/field ids or false |
||
1168 | */ |
||
1169 | public function add_group_field( $parent_field_id, array $field, $position = 0 ) { |
||
1192 | |||
1193 | /** |
||
1194 | * Add a field array to a fields array in desired position |
||
1195 | * @since 2.0.2 |
||
1196 | * @param array $field Metabox field config array |
||
1197 | * @param array &$fields Array (passed by reference) to append the field (array) to |
||
1198 | * @param integer $position Optionally specify a position in the array to be inserted |
||
1199 | */ |
||
1200 | protected function _add_field_to_array( $field, &$fields, $position = 0 ) { |
||
1207 | |||
1208 | /** |
||
1209 | * Remove a field from the metabox |
||
1210 | * @since 2.0.0 |
||
1211 | * @param string $field_id The field id of the field to remove |
||
1212 | * @param string $parent_field_id (optional) The field id of the group field to remove field from |
||
1213 | * @return bool True if field was removed |
||
1214 | */ |
||
1215 | public function remove_field( $field_id, $parent_field_id = '' ) { |
||
1239 | |||
1240 | /** |
||
1241 | * Update or add a property to a field |
||
1242 | * @since 2.0.0 |
||
1243 | * @param string $field_id Field id |
||
1244 | * @param string $property Field property to set/update |
||
1245 | * @param mixed $value Value to set the field property |
||
1246 | * @param string $parent_field_id (optional) The field id of the group field to remove field from |
||
1247 | * @return mixed Field id. Strict compare to false, as success can return a falsey value (like 0) |
||
1248 | */ |
||
1249 | public function update_field_property( $field_id, $property, $value, $parent_field_id = '' ) { |
||
1266 | |||
1267 | /** |
||
1268 | * Check if field ids match a field and return the index/field id |
||
1269 | * @since 2.0.2 |
||
1270 | * @param string $field_id Field id |
||
1271 | * @param string $parent_field_id (optional) Parent field id |
||
1272 | * @return mixed Array of field/parent ids, or false |
||
1273 | */ |
||
1274 | public function get_field_ids( $field_id, $parent_field_id = '' ) { |
||
1301 | |||
1302 | /** |
||
1303 | * When using the old array filter, it is unlikely field array indexes will be the field id |
||
1304 | * @since 2.0.2 |
||
1305 | * @param string $field_id The field id |
||
1306 | * @param array $fields Array of fields to search |
||
1307 | * @return mixed Field index or false |
||
1308 | */ |
||
1309 | public function search_old_school_array( $field_id, $fields ) { |
||
1314 | |||
1315 | /** |
||
1316 | * Handles metabox property callbacks, and passes this $cmb object as property. |
||
1317 | * @since 2.2.3 |
||
1318 | * @param callable $cb The callback method/function/closure |
||
1319 | * @return mixed Return of the callback function. |
||
1320 | */ |
||
1321 | protected function do_callback( $cb ) { |
||
1324 | |||
1325 | /** |
||
1326 | * Generate a unique nonce field for each registered meta_box |
||
1327 | * @since 2.0.0 |
||
1328 | * @return string unique nonce hidden input |
||
1329 | */ |
||
1330 | public function nonce_field() { |
||
1333 | |||
1334 | /** |
||
1335 | * Generate a unique nonce for each registered meta_box |
||
1336 | * @since 2.0.0 |
||
1337 | * @return string unique nonce string |
||
1338 | */ |
||
1339 | public function nonce() { |
||
1346 | |||
1347 | /** |
||
1348 | * Magic getter for our object. |
||
1349 | * @param string $field |
||
1350 | * @throws Exception Throws an exception if the field is invalid. |
||
1351 | * @return mixed |
||
1352 | */ |
||
1353 | public function __get( $field ) { |
||
1363 | |||
1364 | } |
||
1365 |
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.