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 | 48 | 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 | 1 | 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 | 1 | 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 | 1 | public function box_classes() { |
|
| 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 | 1 | 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 | 1 | 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 | 2 | 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 | 2 | 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 | 2 | 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 | 1 | 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 | 4 | 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 | 1 | 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 | 5 | 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 | 5 | public function process_field( $field_args ) { |
|
| 654 | |||
| 655 | 5 | public function pre_process() { |
|
| 670 | |||
| 671 | 1 | 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 | 2 | 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 | 2 | 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 | 52 | 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 | 48 | 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 | 47 | 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 | 48 | 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 | 52 | 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 | 47 | public function current_object_type() { |
|
| 941 | 47 | global $pagenow; |
|
| 942 | 47 | $type = 'post'; |
|
| 943 | |||
| 944 | 47 | if ( in_array( $pagenow, array( 'user-edit.php', 'profile.php', 'user-new.php' ), true ) ) { |
|
| 945 | $type = 'user'; |
||
| 946 | } |
||
| 947 | |||
| 948 | 47 | if ( in_array( $pagenow, array( 'edit-comments.php', 'comment.php' ), true ) ) { |
|
| 949 | $type = 'comment'; |
||
| 950 | } |
||
| 951 | |||
| 952 | 47 | if ( in_array( $pagenow, array( 'edit-tags.php', 'term.php' ), true ) ) { |
|
| 953 | $type = 'term'; |
||
| 954 | } |
||
| 955 | |||
| 956 | 47 | if ( defined( 'DOING_AJAX' ) && isset( $_POST['action'] ) && 'add-tag' === $_POST['action'] ) { |
|
| 957 | $type = 'term'; |
||
| 958 | } |
||
| 959 | |||
| 960 | 47 | return $type; |
|
| 961 | } |
||
| 962 | |||
| 963 | /** |
||
| 964 | * Set metabox property. |
||
| 965 | * @since 2.2.2 |
||
| 966 | * @param string $property Metabox config property to retrieve |
||
| 967 | * @param mixed $value Value to set if no value found |
||
| 968 | * @return mixed Metabox config property value or false |
||
| 969 | */ |
||
| 970 | 1 | public function set_prop( $property, $value ) { |
|
| 975 | |||
| 976 | /** |
||
| 977 | * Get metabox property and optionally set a fallback |
||
| 978 | * @since 2.0.0 |
||
| 979 | * @param string $property Metabox config property to retrieve |
||
| 980 | * @param mixed $fallback Fallback value to set if no value found |
||
| 981 | * @return mixed Metabox config property value or false |
||
| 982 | */ |
||
| 983 | 48 | public function prop( $property, $fallback = null ) { |
|
| 990 | |||
| 991 | /** |
||
| 992 | * Get a field object |
||
| 993 | * @since 2.0.3 |
||
| 994 | * @param string|array|CMB2_Field $field Metabox field id or field config array or CMB2_Field object |
||
| 995 | * @param CMB2_Field|null $field_group (optional) CMB2_Field object (group parent) |
||
| 996 | * @return CMB2_Field|false CMB2_Field object (or false) |
||
| 997 | */ |
||
| 998 | 15 | public function get_field( $field, $field_group = null ) { |
|
| 1023 | |||
| 1024 | /** |
||
| 1025 | * Handles determining which type of arguments to pass to CMB2_Field |
||
| 1026 | * @since 2.0.7 |
||
| 1027 | * @param mixed $field_id Field (or group field) ID |
||
| 1028 | * @param mixed $field_args Array of field arguments |
||
| 1029 | * @param mixed $sub_field_id Sub field ID (if field_group exists) |
||
| 1030 | * @param CMB2_Field|null $field_group If a sub-field, will be the parent group CMB2_Field object |
||
| 1031 | * @return array Array of CMB2_Field arguments |
||
| 1032 | */ |
||
| 1033 | 13 | public function get_field_args( $field_id, $field_args, $sub_field_id, $field_group ) { |
|
| 1050 | |||
| 1051 | /** |
||
| 1052 | * Get default field arguments specific to this CMB2 object. |
||
| 1053 | * @since 2.2.0 |
||
| 1054 | * @param array $field_args Metabox field config array. |
||
| 1055 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
| 1056 | * @return array Array of field arguments. |
||
| 1057 | */ |
||
| 1058 | 19 | View Code Duplication | protected function get_default_args( $field_args, $field_group = null ) { |
| 1075 | |||
| 1076 | /** |
||
| 1077 | * Get a new field object specific to this CMB2 object. |
||
| 1078 | * @since 2.2.0 |
||
| 1079 | * @param array $field_args Metabox field config array. |
||
| 1080 | * @param CMB2_Field $field_group (optional) CMB2_Field object (group parent) |
||
| 1081 | * @return CMB2_Field CMB2_Field object |
||
| 1082 | */ |
||
| 1083 | 7 | protected function get_new_field( $field_args, $field_group = null ) { |
|
| 1086 | |||
| 1087 | /** |
||
| 1088 | * When fields are added in the old-school way, intitate them as they should be |
||
| 1089 | * @since 2.1.0 |
||
| 1090 | * @param array $fields Array of fields to add |
||
| 1091 | * @param mixed $parent_field_id Parent field id or null |
||
| 1092 | */ |
||
| 1093 | 45 | protected function add_fields( $fields, $parent_field_id = null ) { |
|
| 1111 | |||
| 1112 | /** |
||
| 1113 | * Add a field to the metabox |
||
| 1114 | * @since 2.0.0 |
||
| 1115 | * @param array $field Metabox field config array |
||
| 1116 | * @param int $position (optional) Position of metabox. 1 for first, etc |
||
| 1117 | * @return string|false Field id or false |
||
| 1118 | */ |
||
| 1119 | 47 | public function add_field( array $field, $position = 0 ) { |
|
| 1156 | |||
| 1157 | /** |
||
| 1158 | * Defines a field's column if requesting to be show in admin columns. |
||
| 1159 | * @since 2.2.3 |
||
| 1160 | * @param array $field Metabox field config array. |
||
| 1161 | * @return array Modified metabox field config array. |
||
| 1162 | */ |
||
| 1163 | protected function define_field_column( array $field ) { |
||
| 1175 | |||
| 1176 | /** |
||
| 1177 | * Add a field to a group |
||
| 1178 | * @since 2.0.0 |
||
| 1179 | * @param string $parent_field_id The field id of the group field to add the field |
||
| 1180 | * @param array $field Metabox field config array |
||
| 1181 | * @param int $position (optional) Position of metabox. 1 for first, etc |
||
| 1182 | * @return mixed Array of parent/field ids or false |
||
| 1183 | */ |
||
| 1184 | 5 | public function add_group_field( $parent_field_id, array $field, $position = 0 ) { |
|
| 1207 | |||
| 1208 | /** |
||
| 1209 | * Add a field array to a fields array in desired position |
||
| 1210 | * @since 2.0.2 |
||
| 1211 | * @param array $field Metabox field config array |
||
| 1212 | * @param array &$fields Array (passed by reference) to append the field (array) to |
||
| 1213 | * @param integer $position Optionally specify a position in the array to be inserted |
||
| 1214 | */ |
||
| 1215 | 47 | protected function _add_field_to_array( $field, &$fields, $position = 0 ) { |
|
| 1222 | |||
| 1223 | /** |
||
| 1224 | * Remove a field from the metabox |
||
| 1225 | * @since 2.0.0 |
||
| 1226 | * @param string $field_id The field id of the field to remove |
||
| 1227 | * @param string $parent_field_id (optional) The field id of the group field to remove field from |
||
| 1228 | * @return bool True if field was removed |
||
| 1229 | */ |
||
| 1230 | 2 | public function remove_field( $field_id, $parent_field_id = '' ) { |
|
| 1254 | |||
| 1255 | /** |
||
| 1256 | * Update or add a property to a field |
||
| 1257 | * @since 2.0.0 |
||
| 1258 | * @param string $field_id Field id |
||
| 1259 | * @param string $property Field property to set/update |
||
| 1260 | * @param mixed $value Value to set the field property |
||
| 1261 | * @param string $parent_field_id (optional) The field id of the group field to remove field from |
||
| 1262 | * @return mixed Field id. Strict compare to false, as success can return a falsey value (like 0) |
||
| 1263 | */ |
||
| 1264 | 4 | public function update_field_property( $field_id, $property, $value, $parent_field_id = '' ) { |
|
| 1281 | |||
| 1282 | /** |
||
| 1283 | * Check if field ids match a field and return the index/field id |
||
| 1284 | * @since 2.0.2 |
||
| 1285 | * @param string $field_id Field id |
||
| 1286 | * @param string $parent_field_id (optional) Parent field id |
||
| 1287 | * @return mixed Array of field/parent ids, or false |
||
| 1288 | */ |
||
| 1289 | 19 | public function get_field_ids( $field_id, $parent_field_id = '' ) { |
|
| 1316 | |||
| 1317 | /** |
||
| 1318 | * When using the old array filter, it is unlikely field array indexes will be the field id |
||
| 1319 | * @since 2.0.2 |
||
| 1320 | * @param string $field_id The field id |
||
| 1321 | * @param array $fields Array of fields to search |
||
| 1322 | * @return mixed Field index or false |
||
| 1323 | */ |
||
| 1324 | 2 | public function search_old_school_array( $field_id, $fields ) { |
|
| 1329 | |||
| 1330 | /** |
||
| 1331 | * Handles metabox property callbacks, and passes this $cmb object as property. |
||
| 1332 | * @since 2.2.3 |
||
| 1333 | * @param callable $cb The callback method/function/closure |
||
| 1334 | * @return mixed Return of the callback function. |
||
| 1335 | */ |
||
| 1336 | 1 | protected function do_callback( $cb ) { |
|
| 1339 | |||
| 1340 | /** |
||
| 1341 | * Generate a unique nonce field for each registered meta_box |
||
| 1342 | * @since 2.0.0 |
||
| 1343 | * @return string unique nonce hidden input |
||
| 1344 | */ |
||
| 1345 | 1 | public function nonce_field() { |
|
| 1348 | |||
| 1349 | /** |
||
| 1350 | * Generate a unique nonce for each registered meta_box |
||
| 1351 | * @since 2.0.0 |
||
| 1352 | * @return string unique nonce string |
||
| 1353 | */ |
||
| 1354 | 1 | public function nonce() { |
|
| 1361 | |||
| 1362 | /** |
||
| 1363 | * Whether this box is an "alternate context" box. This means the box has a 'context' property defined as: |
||
| 1364 | * 'form_top', 'before_permalink', 'after_title', or 'after_editor'. |
||
| 1365 | * |
||
| 1366 | * @since 2.2.4 |
||
| 1367 | * @return bool |
||
| 1368 | */ |
||
| 1369 | 1 | public function is_alternate_context_box() { |
|
| 1372 | |||
| 1373 | /** |
||
| 1374 | * Magic getter for our object. |
||
| 1375 | * @param string $field |
||
| 1376 | * @throws Exception Throws an exception if the field is invalid. |
||
| 1377 | * @return mixed |
||
| 1378 | */ |
||
| 1379 | 48 | public function __get( $field ) { |
|
| 1389 | |||
| 1390 | } |
||
| 1391 |
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.