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 |
||
| 15 | class CMB2 { |
||
|
|
|||
| 16 | |||
| 17 | /** |
||
| 18 | * Current CMB2 instance ID |
||
| 19 | * @var string |
||
| 20 | * @since 2.0.0 |
||
| 21 | */ |
||
| 22 | protected $cmb_id = ''; |
||
| 23 | |||
| 24 | /** |
||
| 25 | * Metabox Config array |
||
| 26 | * @var array |
||
| 27 | * @since 0.9.0 |
||
| 28 | */ |
||
| 29 | protected $meta_box = array(); |
||
| 30 | |||
| 31 | /** |
||
| 32 | * Object ID for metabox meta retrieving/saving |
||
| 33 | * @var mixed |
||
| 34 | * @since 1.0.0 |
||
| 35 | */ |
||
| 36 | protected $object_id = 0; |
||
| 37 | |||
| 38 | /** |
||
| 39 | * Type of object being saved. (e.g., post, user, or comment) |
||
| 40 | * @var string |
||
| 41 | * @since 1.0.0 |
||
| 42 | */ |
||
| 43 | protected $object_type = 'post'; |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Type of object registered for metabox. (e.g., post, user, or comment) |
||
| 47 | * @var string |
||
| 48 | * @since 1.0.0 |
||
| 49 | */ |
||
| 50 | protected $mb_object_type = null; |
||
| 51 | |||
| 52 | /** |
||
| 53 | * List of fields that are changed/updated on save |
||
| 54 | * @var array |
||
| 55 | * @since 1.1.0 |
||
| 56 | */ |
||
| 57 | protected $updated = array(); |
||
| 58 | |||
| 59 | /** |
||
| 60 | * Metabox Defaults |
||
| 61 | * @var array |
||
| 62 | * @since 1.0.1 |
||
| 63 | */ |
||
| 64 | protected $mb_defaults = array( |
||
| 65 | 'id' => '', |
||
| 66 | 'title' => '', |
||
| 67 | 'type' => '', |
||
| 68 | 'object_types' => array(), // Post type |
||
| 69 | 'context' => 'normal', |
||
| 70 | 'priority' => 'high', |
||
| 71 | 'show_names' => true, // Show field names on the left |
||
| 72 | 'show_on_cb' => null, // Callback to determine if metabox should display. |
||
| 73 | 'show_on' => array(), // Post IDs or page templates to display this metabox. overrides 'show_on_cb' |
||
| 74 | 'cmb_styles' => true, // Include CMB2 stylesheet |
||
| 75 | 'enqueue_js' => true, // Include CMB2 JS |
||
| 76 | 'fields' => array(), |
||
| 77 | 'hookup' => true, |
||
| 78 | 'save_fields' => true, // Will not save during hookup if false |
||
| 79 | 'closed' => false, // Default to metabox being closed? |
||
| 80 | 'taxonomies' => array(), |
||
| 81 | 'new_user_section' => 'add-new-user', // or 'add-existing-user' |
||
| 82 | 'new_term_section' => true, |
||
| 83 | ); |
||
| 84 | |||
| 85 | /** |
||
| 86 | * Metabox field objects |
||
| 87 | * @var array |
||
| 88 | * @since 2.0.3 |
||
| 89 | */ |
||
| 90 | protected $fields = array(); |
||
| 91 | |||
| 92 | /** |
||
| 93 | * An array of hidden fields to output at the end of the form |
||
| 94 | * @var array |
||
| 95 | * @since 2.0.0 |
||
| 96 | */ |
||
| 97 | protected $hidden_fields = array(); |
||
| 98 | |||
| 99 | /** |
||
| 100 | * Array of key => value data for saving. Likely $_POST data. |
||
| 101 | * @var array |
||
| 102 | * @since 2.0.0 |
||
| 103 | */ |
||
| 104 | public $data_to_save = array(); |
||
| 105 | |||
| 106 | /** |
||
| 107 | * Array of key => value data for saving. Likely $_POST data. |
||
| 108 | * @var string |
||
| 109 | * @since 2.0.0 |
||
| 110 | */ |
||
| 111 | protected $generated_nonce = ''; |
||
| 112 | |||
| 113 | /** |
||
| 114 | * Get started |
||
| 115 | * @since 0.4.0 |
||
| 116 | * @param array $meta_box Metabox config array |
||
| 117 | * @param integer $object_id Optional object id |
||
| 118 | */ |
||
| 119 | 44 | public function __construct( $meta_box, $object_id = 0 ) { |
|
| 147 | |||
| 148 | /** |
||
| 149 | * Loops through and displays fields |
||
| 150 | * @since 1.0.0 |
||
| 151 | * @param int $object_id Object ID |
||
| 152 | * @param string $object_type Type of object being saved. (e.g., post, user, or comment) |
||
| 153 | */ |
||
| 154 | 1 | public function show_form( $object_id = 0, $object_type = '' ) { |
|
| 163 | |||
| 164 | /** |
||
| 165 | * Outputs the opening form markup and runs corresponding hooks: |
||
| 166 | * 'cmb2_before_form' and "cmb2_before_{$object_type}_form_{$this->cmb_id}" |
||
| 167 | * @since 2.2.0 |
||
| 168 | * @param integer $object_id Object ID |
||
| 169 | * @param string $object_type Object type |
||
| 170 | * @return void |
||
| 171 | */ |
||
| 172 | 1 | View Code Duplication | public function render_form_open( $object_id = 0, $object_type = '' ) { |
| 210 | |||
| 211 | /** |
||
| 212 | * Outputs the closing form markup and runs corresponding hooks: |
||
| 213 | * 'cmb2_after_form' and "cmb2_after_{$object_type}_form_{$this->cmb_id}" |
||
| 214 | * @since 2.2.0 |
||
| 215 | * @param integer $object_id Object ID |
||
| 216 | * @param string $object_type Object type |
||
| 217 | * @return void |
||
| 218 | */ |
||
| 219 | 1 | View Code Duplication | public function render_form_close( $object_id = 0, $object_type = '' ) { |
| 256 | |||
| 257 | /** |
||
| 258 | * Renders a field based on the field type |
||
| 259 | * @since 2.2.0 |
||
| 260 | * @param array $field_args A field configuration array. |
||
| 261 | * @return mixed CMB2_Field object if successful. |
||
| 262 | */ |
||
| 263 | 1 | public function render_field( $field_args ) { |
|
| 287 | |||
| 288 | /** |
||
| 289 | * Render a repeatable group. |
||
| 290 | * @param array $args Array of field arguments for a group field parent. |
||
| 291 | * @return CMB2_Field|null Group field object. |
||
| 292 | */ |
||
| 293 | 2 | public function render_group( $args ) { |
|
| 294 | |||
| 295 | 2 | if ( ! isset( $args['id'], $args['fields'] ) || ! is_array( $args['fields'] ) ) { |
|
| 296 | return; |
||
| 297 | } |
||
| 298 | |||
| 299 | 2 | $field_group = $this->get_field( $args ); |
|
| 300 | |||
| 301 | // If field is requesting to be conditionally shown |
||
| 302 | 2 | if ( ! $field_group || ! $field_group->should_show() ) { |
|
| 303 | return; |
||
| 304 | } |
||
| 305 | |||
| 306 | 2 | $desc = $field_group->args( 'description' ); |
|
| 307 | 2 | $label = $field_group->args( 'name' ); |
|
| 308 | 2 | $sortable = $field_group->options( 'sortable' ) ? ' sortable' : ' non-sortable'; |
|
| 309 | 2 | $repeat_class = $field_group->args( 'repeatable' ) ? ' repeatable' : ' non-repeatable'; |
|
| 310 | 2 | $group_val = (array) $field_group->value(); |
|
| 311 | 2 | $nrows = count( $group_val ); |
|
| 312 | 2 | $remove_disabled = $nrows <= 1 ? 'disabled="disabled" ' : ''; |
|
| 313 | 2 | $field_group->index = 0; |
|
| 314 | |||
| 315 | 2 | $field_group->peform_param_callback( 'before_group' ); |
|
| 316 | |||
| 317 | 2 | echo '<div class="cmb-row cmb-repeat-group-wrap ', $field_group->row_classes(), '" data-fieldtype="group"><div class="cmb-td"><div id="', $field_group->id(), '_repeat" class="cmb-nested cmb-field-list cmb-repeatable-group', $sortable, $repeat_class, '" style="width:100%;">'; |
|
| 318 | |||
| 319 | 2 | if ( $desc || $label ) { |
|
| 320 | 2 | $class = $desc ? ' cmb-group-description' : ''; |
|
| 321 | 2 | echo '<div class="cmb-row', $class, '"><div class="cmb-th">'; |
|
| 322 | 2 | if ( $label ) { |
|
| 323 | 2 | echo '<h2 class="cmb-group-name">', $label, '</h2>'; |
|
| 324 | 2 | } |
|
| 325 | 2 | if ( $desc ) { |
|
| 326 | 1 | echo '<p class="cmb2-metabox-description">', $desc, '</p>'; |
|
| 327 | 1 | } |
|
| 328 | 2 | echo '</div></div>'; |
|
| 329 | 2 | } |
|
| 330 | |||
| 331 | 2 | if ( ! empty( $group_val ) ) { |
|
| 332 | |||
| 333 | foreach ( $group_val as $group_key => $field_id ) { |
||
| 334 | $this->render_group_row( $field_group, $remove_disabled ); |
||
| 335 | $field_group->index++; |
||
| 336 | } |
||
| 337 | } else { |
||
| 338 | 2 | $this->render_group_row( $field_group, $remove_disabled ); |
|
| 339 | } |
||
| 340 | |||
| 341 | 2 | if ( $field_group->args( 'repeatable' ) ) { |
|
| 342 | 1 | echo '<div class="cmb-row"><div class="cmb-td"><p class="cmb-add-row"><button type="button" data-selector="', $field_group->id(), '_repeat" data-grouptitle="', $field_group->options( 'group_title' ), '" class="cmb-add-group-row button">', $field_group->options( 'add_button' ), '</button></p></div></div>'; |
|
| 343 | 1 | } |
|
| 344 | |||
| 345 | 2 | echo '</div></div></div>'; |
|
| 346 | |||
| 347 | 2 | $field_group->peform_param_callback( 'after_group' ); |
|
| 348 | |||
| 349 | 2 | return $field_group; |
|
| 350 | } |
||
| 351 | |||
| 352 | /** |
||
| 353 | * Render a repeatable group row |
||
| 354 | * @since 1.0.2 |
||
| 355 | * @param CMB2_Field $field_group CMB2_Field group field object |
||
| 356 | * @param string $remove_disabled Attribute string to disable the remove button |
||
| 357 | */ |
||
| 358 | 2 | public function render_group_row( $field_group, $remove_disabled ) { |
|
| 406 | |||
| 407 | /** |
||
| 408 | * Add a hidden field to the list of hidden fields to be rendered later |
||
| 409 | * @since 2.0.0 |
||
| 410 | * @param array $field_args Array of field arguments to be passed to CMB2_Field |
||
| 411 | */ |
||
| 412 | public function add_hidden_field( $field_args, $field_group = null ) { |
||
| 424 | |||
| 425 | /** |
||
| 426 | * Loop through and output hidden fields |
||
| 427 | * @since 2.0.0 |
||
| 428 | */ |
||
| 429 | 1 | public function render_hidden_fields() { |
|
| 436 | |||
| 437 | /** |
||
| 438 | * Returns array of sanitized field values (without saving them) |
||
| 439 | * @since 2.0.3 |
||
| 440 | * @param array $data_to_sanitize Array of field_id => value data for sanitizing (likely $_POST data). |
||
| 441 | */ |
||
| 442 | 2 | public function get_sanitized_values( array $data_to_sanitize ) { |
|
| 466 | |||
| 467 | /** |
||
| 468 | * Loops through and saves field data |
||
| 469 | * @since 1.0.0 |
||
| 470 | * @param int $object_id Object ID |
||
| 471 | * @param string $object_type Type of object being saved. (e.g., post, user, or comment) |
||
| 472 | * @param array $data_to_save Array of key => value data for saving. Likely $_POST data. |
||
| 473 | */ |
||
| 474 | 1 | public function save_fields( $object_id = 0, $object_type = '', $data_to_save = array() ) { |
|
| 490 | |||
| 491 | /** |
||
| 492 | * Process and save form fields |
||
| 493 | * @since 2.0.0 |
||
| 494 | */ |
||
| 495 | 3 | public function process_fields() { |
|
| 509 | |||
| 510 | /** |
||
| 511 | * Process and save a field |
||
| 512 | * @since 2.0.0 |
||
| 513 | * @param array $field_args Array of field arguments |
||
| 514 | */ |
||
| 515 | 3 | public function process_field( $field_args ) { |
|
| 542 | |||
| 543 | 3 | public function pre_process() { |
|
| 558 | |||
| 559 | 1 | public function after_save() { |
|
| 594 | |||
| 595 | /** |
||
| 596 | * Save a repeatable group |
||
| 597 | * @since 1.x.x |
||
| 598 | * @param array $args Field arguments array |
||
| 599 | * @return mixed Return of CMB2_Field::update_data() |
||
| 600 | */ |
||
| 601 | 1 | public function save_group( $args ) { |
|
| 608 | |||
| 609 | /** |
||
| 610 | * Save a repeatable group |
||
| 611 | * @since 1.x.x |
||
| 612 | * @param array $field_group CMB2_Field group field object |
||
| 613 | * @return mixed Return of CMB2_Field::update_data() |
||
| 614 | */ |
||
| 615 | 1 | public function save_group_field( $field_group ) { |
|
| 685 | |||
| 686 | /** |
||
| 687 | * Get object id from global space if no id is provided |
||
| 688 | * @since 1.0.0 |
||
| 689 | * @param integer $object_id Object ID |
||
| 690 | * @return integer $object_id Object ID |
||
| 691 | */ |
||
| 692 | 48 | public function object_id( $object_id = 0 ) { |
|
| 731 | |||
| 732 | /** |
||
| 733 | * Sets the $object_type based on metabox settings |
||
| 734 | * @since 1.0.0 |
||
| 735 | * @return string Object type |
||
| 736 | */ |
||
| 737 | 44 | public function mb_object_type() { |
|
| 791 | |||
| 792 | /** |
||
| 793 | * Determines if metabox is for an options page |
||
| 794 | * @since 1.0.1 |
||
| 795 | * @return boolean True/False |
||
| 796 | */ |
||
| 797 | 44 | public function is_options_page_mb() { |
|
| 800 | |||
| 801 | /** |
||
| 802 | * Returns the object type |
||
| 803 | * @since 1.0.0 |
||
| 804 | * @return string Object type |
||
| 805 | */ |
||
| 806 | 48 | public function object_type( $object_type = '' ) { |
|
| 820 | |||
| 821 | /** |
||
| 822 | * Get the object type for the current page, based on the $pagenow global. |
||
| 823 | * @since 2.2.2 |
||
| 824 | * @return string Page object type name. |
||
| 825 | */ |
||
| 826 | public function current_object_type() { |
||
| 844 | |||
| 845 | /** |
||
| 846 | * Set metabox property. |
||
| 847 | * @since 2.2.0 |
||
| 848 | * @param string $property Metabox config property to retrieve |
||
| 849 | * @param mixed $value Value to set if no value found |
||
| 850 | * @return mixed Metabox config property value or false |
||
| 851 | */ |
||
| 852 | 1 | public function set_prop( $property, $value ) { |
|
| 857 | |||
| 858 | /** |
||
| 859 | * Get metabox property and optionally set a fallback |
||
| 860 | * @since 2.0.0 |
||
| 861 | * @param string $property Metabox config property to retrieve |
||
| 862 | * @param mixed $fallback Fallback value to set if no value found |
||
| 863 | * @return mixed Metabox config property value or false |
||
| 864 | */ |
||
| 865 | 44 | public function prop( $property, $fallback = null ) { |
|
| 872 | |||
| 873 | /** |
||
| 874 | * Get a field object |
||
| 875 | * @since 2.0.3 |
||
| 876 | * @param string|array|CMB2_Field $field Metabox field id or field config array or CMB2_Field object |
||
| 877 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
| 878 | * @return CMB2_Field|false CMB2_Field object (or false) |
||
| 879 | */ |
||
| 880 | 15 | public function get_field( $field, $field_group = null ) { |
|
| 905 | |||
| 906 | /** |
||
| 907 | * Handles determining which type of arguments to pass to CMB2_Field |
||
| 908 | * @since 2.0.7 |
||
| 909 | * @param mixed $field_id Field (or group field) ID |
||
| 910 | * @param mixed $field_args Array of field arguments |
||
| 911 | * @param mixed $sub_field_id Sub field ID (if field_group exists) |
||
| 912 | * @param mixed $field_group If a sub-field, will be the parent group CMB2_Field object |
||
| 913 | * @return array Array of CMB2_Field arguments |
||
| 914 | */ |
||
| 915 | 13 | public function get_field_args( $field_id, $field_args, $sub_field_id, $field_group ) { |
|
| 932 | |||
| 933 | /** |
||
| 934 | * Get default field arguments specific to this CMB2 object. |
||
| 935 | * @since 2.2.0 |
||
| 936 | * @param array $field_args Metabox field config array. |
||
| 937 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
| 938 | * @return array Array of field arguments. |
||
| 939 | */ |
||
| 940 | 17 | protected function get_default_args( $field_args, $field_group = null ) { |
|
| 957 | |||
| 958 | /** |
||
| 959 | * Get a new field object specific to this CMB2 object. |
||
| 960 | * @since 2.2.0 |
||
| 961 | * @param array $field_args Metabox field config array. |
||
| 962 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
| 963 | * @return CMB2_Field CMB2_Field object |
||
| 964 | */ |
||
| 965 | 5 | protected function get_new_field( $field_args, $field_group = null ) { |
|
| 968 | |||
| 969 | /** |
||
| 970 | * When fields are added in the old-school way, intitate them as they should be |
||
| 971 | * @since 2.1.0 |
||
| 972 | * @param array $fields Array of fields to add |
||
| 973 | * @param mixed $parent_field_id Parent field id or null |
||
| 974 | */ |
||
| 975 | 41 | protected function add_fields( $fields, $parent_field_id = null ) { |
|
| 993 | |||
| 994 | /** |
||
| 995 | * Add a field to the metabox |
||
| 996 | * @since 2.0.0 |
||
| 997 | * @param array $field Metabox field config array |
||
| 998 | * @param int $position (optional) Position of metabox. 1 for first, etc |
||
| 999 | * @return mixed Field id or false |
||
| 1000 | */ |
||
| 1001 | 43 | public function add_field( array $field, $position = 0 ) { |
|
| 1019 | |||
| 1020 | /** |
||
| 1021 | * Add a field to a group |
||
| 1022 | * @since 2.0.0 |
||
| 1023 | * @param string $parent_field_id The field id of the group field to add the field |
||
| 1024 | * @param array $field Metabox field config array |
||
| 1025 | * @param int $position (optional) Position of metabox. 1 for first, etc |
||
| 1026 | * @return mixed Array of parent/field ids or false |
||
| 1027 | */ |
||
| 1028 | 3 | public function add_group_field( $parent_field_id, array $field, $position = 0 ) { |
|
| 1051 | |||
| 1052 | /** |
||
| 1053 | * Add a field array to a fields array in desired position |
||
| 1054 | * @since 2.0.2 |
||
| 1055 | * @param array $field Metabox field config array |
||
| 1056 | * @param array &$fields Array (passed by reference) to append the field (array) to |
||
| 1057 | * @param integer $position Optionally specify a position in the array to be inserted |
||
| 1058 | */ |
||
| 1059 | 43 | protected function _add_field_to_array( $field, &$fields, $position = 0 ) { |
|
| 1066 | |||
| 1067 | /** |
||
| 1068 | * Remove a field from the metabox |
||
| 1069 | * @since 2.0.0 |
||
| 1070 | * @param string $field_id The field id of the field to remove |
||
| 1071 | * @param string $parent_field_id (optional) The field id of the group field to remove field from |
||
| 1072 | * @return bool True if field was removed |
||
| 1073 | */ |
||
| 1074 | 2 | public function remove_field( $field_id, $parent_field_id = '' ) { |
|
| 1098 | |||
| 1099 | /** |
||
| 1100 | * Update or add a property to a field |
||
| 1101 | * @since 2.0.0 |
||
| 1102 | * @param string $field_id Field id |
||
| 1103 | * @param string $property Field property to set/update |
||
| 1104 | * @param mixed $value Value to set the field property |
||
| 1105 | * @param string $parent_field_id (optional) The field id of the group field to remove field from |
||
| 1106 | * @return mixed Field id. Strict compare to false, as success can return a falsey value (like 0) |
||
| 1107 | */ |
||
| 1108 | 4 | public function update_field_property( $field_id, $property, $value, $parent_field_id = '' ) { |
|
| 1125 | |||
| 1126 | /** |
||
| 1127 | * Check if field ids match a field and return the index/field id |
||
| 1128 | * @since 2.0.2 |
||
| 1129 | * @param string $field_id Field id |
||
| 1130 | * @param string $parent_field_id (optional) Parent field id |
||
| 1131 | * @return mixed Array of field/parent ids, or false |
||
| 1132 | */ |
||
| 1133 | 19 | public function get_field_ids( $field_id, $parent_field_id = '' ) { |
|
| 1160 | |||
| 1161 | /** |
||
| 1162 | * When using the old array filter, it is unlikely field array indexes will be the field id |
||
| 1163 | * @since 2.0.2 |
||
| 1164 | * @param string $field_id The field id |
||
| 1165 | * @param array $fields Array of fields to search |
||
| 1166 | * @return mixed Field index or false |
||
| 1167 | */ |
||
| 1168 | 2 | public function search_old_school_array( $field_id, $fields ) { |
|
| 1173 | |||
| 1174 | /** |
||
| 1175 | * Determine whether this cmb object should show, based on the 'show_on_cb' callback. |
||
| 1176 | * |
||
| 1177 | * @since 2.0.9 |
||
| 1178 | * |
||
| 1179 | * @return bool Whether this cmb should be shown. |
||
| 1180 | */ |
||
| 1181 | View Code Duplication | public function should_show() { |
|
| 1192 | |||
| 1193 | /** |
||
| 1194 | * Generate a unique nonce field for each registered meta_box |
||
| 1195 | * @since 2.0.0 |
||
| 1196 | * @return string unique nonce hidden input |
||
| 1197 | */ |
||
| 1198 | 1 | public function nonce_field() { |
|
| 1201 | |||
| 1202 | /** |
||
| 1203 | * Generate a unique nonce for each registered meta_box |
||
| 1204 | * @since 2.0.0 |
||
| 1205 | * @return string unique nonce string |
||
| 1206 | */ |
||
| 1207 | 1 | public function nonce() { |
|
| 1214 | |||
| 1215 | /** |
||
| 1216 | * Magic getter for our object. |
||
| 1217 | * @param string $field |
||
| 1218 | * @throws Exception Throws an exception if the field is invalid. |
||
| 1219 | * @return mixed |
||
| 1220 | */ |
||
| 1221 | 44 | public function __get( $field ) { |
|
| 1233 | |||
| 1234 | } |
||
| 1235 |
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.