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_hookup 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_hookup, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class CMB2_hookup extends CMB2_Hookup_Base { |
||
|
|
|||
| 15 | |||
| 16 | /** |
||
| 17 | * Only allow JS registration once |
||
| 18 | * @var bool |
||
| 19 | * @since 2.0.7 |
||
| 20 | */ |
||
| 21 | protected static $js_registration_done = false; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Only allow CSS registration once |
||
| 25 | * @var bool |
||
| 26 | * @since 2.0.7 |
||
| 27 | */ |
||
| 28 | protected static $css_registration_done = false; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * CMB taxonomies array for term meta |
||
| 32 | * @var array |
||
| 33 | * @since 2.2.0 |
||
| 34 | */ |
||
| 35 | protected $taxonomies = array(); |
||
| 36 | |||
| 37 | /** |
||
| 38 | * Custom field columns. |
||
| 39 | * @var array |
||
| 40 | * @since 2.2.2 |
||
| 41 | */ |
||
| 42 | protected $columns = array(); |
||
| 43 | |||
| 44 | /** |
||
| 45 | * Constructor |
||
| 46 | * @since 2.0.0 |
||
| 47 | * @param CMB2 $cmb The CMB2 object to hookup |
||
| 48 | */ |
||
| 49 | public function __construct( CMB2 $cmb ) { |
||
| 53 | |||
| 54 | public function universal_hooks() { |
||
| 78 | |||
| 79 | public function post_hooks() { |
||
| 80 | |||
| 81 | // Fetch the context we set in our call. |
||
| 82 | $context = ! empty( $this->cmb->prop( 'context' ) ) ? $this->cmb->prop( 'context' ) : 'normal'; |
||
| 83 | |||
| 84 | // Call the proper hook based on the context provided. |
||
| 85 | switch ( $context ) { |
||
| 86 | |||
| 87 | View Code Duplication | case 'form_top': |
|
| 88 | add_action( 'edit_form_top', array( $this, 'add_context_metabox' ) ); |
||
| 89 | add_filter( 'cmb2_wrap_classes', array( $this, 'add_context_class' ), 10, 2 ); |
||
| 90 | |||
| 91 | break; |
||
| 92 | |||
| 93 | View Code Duplication | case 'before_permalink': |
|
| 94 | add_action( 'edit_form_before_permalink', array( $this, 'add_context_metabox' ) ); |
||
| 95 | add_filter( 'cmb2_wrap_classes', array( $this, 'add_context_class' ), 10, 2 ); |
||
| 96 | |||
| 97 | break; |
||
| 98 | |||
| 99 | View Code Duplication | case 'after_title': |
|
| 100 | add_action( 'edit_form_after_title', array( $this, 'add_context_metabox' ) ); |
||
| 101 | add_filter( 'cmb2_wrap_classes', array( $this, 'add_context_class' ), 10, 2 ); |
||
| 102 | |||
| 103 | break; |
||
| 104 | |||
| 105 | View Code Duplication | case 'after_editor': |
|
| 106 | add_action( 'edit_form_after_editor', array( $this, 'add_context_metabox' ) ); |
||
| 107 | add_filter( 'cmb2_wrap_classes', array( $this, 'add_context_class' ), 10, 2 ); |
||
| 108 | |||
| 109 | break; |
||
| 110 | 1 | ||
| 111 | 1 | default: |
|
| 112 | 1 | add_action( 'add_meta_boxes', array( $this, 'add_metaboxes' ) ); |
|
| 113 | } |
||
| 114 | |||
| 115 | add_action( 'add_attachment', array( $this, 'save_post' ) ); |
||
| 116 | 1 | add_action( 'edit_attachment', array( $this, 'save_post' ) ); |
|
| 117 | add_action( 'save_post', array( $this, 'save_post' ), 10, 2 ); |
||
| 118 | 1 | ||
| 119 | if ( $this->cmb->has_columns ) { |
||
| 120 | 1 | foreach ( $this->cmb->prop( 'object_types' ) as $post_type ) { |
|
| 121 | 1 | add_filter( "manage_{$post_type}_posts_columns", array( $this, 'register_column_headers' ) ); |
|
| 122 | 1 | add_action( "manage_{$post_type}_posts_custom_column", array( $this, 'column_display' ), 10, 2 ); |
|
| 123 | 1 | } |
|
| 124 | 1 | } |
|
| 125 | 1 | } |
|
| 126 | 1 | ||
| 127 | 1 | public function comment_hooks() { |
|
| 128 | 1 | add_action( 'add_meta_boxes_comment', array( $this, 'add_metaboxes' ) ); |
|
| 129 | add_action( 'edit_comment', array( $this, 'save_comment' ) ); |
||
| 130 | 1 | ||
| 131 | if ( $this->cmb->has_columns ) { |
||
| 132 | add_filter( 'manage_edit-comments_columns', array( $this, 'register_column_headers' ) ); |
||
| 133 | 1 | add_action( 'manage_comments_custom_column', array( $this, 'column_display' ), 10, 3 ); |
|
| 134 | } |
||
| 135 | 1 | } |
|
| 136 | |||
| 137 | 1 | public function user_hooks() { |
|
| 138 | $priority = $this->get_priority(); |
||
| 139 | 1 | ||
| 140 | 1 | add_action( 'show_user_profile', array( $this, 'user_metabox' ), $priority ); |
|
| 141 | 1 | add_action( 'edit_user_profile', array( $this, 'user_metabox' ), $priority ); |
|
| 142 | 1 | add_action( 'user_new_form', array( $this, 'user_new_metabox' ), $priority ); |
|
| 143 | 1 | ||
| 144 | 1 | add_action( 'personal_options_update', array( $this, 'save_user' ) ); |
|
| 145 | add_action( 'edit_user_profile_update', array( $this, 'save_user' ) ); |
||
| 146 | 1 | add_action( 'user_register', array( $this, 'save_user' ) ); |
|
| 147 | |||
| 148 | 1 | View Code Duplication | if ( $this->cmb->has_columns ) { |
| 149 | 1 | add_filter( 'manage_users_columns', array( $this, 'register_column_headers' ) ); |
|
| 150 | 1 | add_filter( 'manage_users_custom_column', array( $this, 'return_column_display' ), 10, 3 ); |
|
| 151 | 1 | } |
|
| 152 | 1 | } |
|
| 153 | 1 | ||
| 154 | 1 | public function term_hooks() { |
|
| 155 | 1 | if ( ! function_exists( 'get_term_meta' ) ) { |
|
| 156 | 1 | wp_die( esc_html__( 'Term Metadata is a WordPress 4.4+ feature. Please upgrade your WordPress install.', 'cmb2' ) ); |
|
| 157 | 1 | } |
|
| 158 | 1 | ||
| 159 | 1 | if ( ! $this->cmb->prop( 'taxonomies' ) ) { |
|
| 160 | 1 | wp_die( esc_html__( 'Term metaboxes configuration requires a "taxonomies" parameter.', 'cmb2' ) ); |
|
| 161 | 1 | } |
|
| 162 | |||
| 163 | 1 | $this->taxonomies = (array) $this->cmb->prop( 'taxonomies' ); |
|
| 164 | 1 | $show_on_term_add = $this->cmb->prop( 'new_term_section' ); |
|
| 165 | 1 | $priority = $this->get_priority( 8 ); |
|
| 166 | 1 | ||
| 167 | foreach ( $this->taxonomies as $taxonomy ) { |
||
| 168 | 1 | // Display our form data |
|
| 169 | 1 | add_action( "{$taxonomy}_edit_form", array( $this, 'term_metabox' ), $priority, 2 ); |
|
| 170 | |||
| 171 | 1 | $show_on_add = is_array( $show_on_term_add ) |
|
| 172 | 1 | ? in_array( $taxonomy, $show_on_term_add ) |
|
| 173 | 1 | : (bool) $show_on_term_add; |
|
| 174 | 1 | ||
| 175 | 1 | $show_on_add = apply_filters( "cmb2_show_on_term_add_form_{$this->cmb->cmb_id}", $show_on_add, $this->cmb ); |
|
| 176 | 1 | ||
| 177 | 1 | // Display form in add-new section (unless specified not to) |
|
| 178 | 1 | if ( $show_on_add ) { |
|
| 179 | add_action( "{$taxonomy}_add_form_fields", array( $this, 'term_metabox' ), $priority, 2 ); |
||
| 180 | 1 | } |
|
| 181 | |||
| 182 | 1 | View Code Duplication | if ( $this->cmb->has_columns ) { |
| 183 | 1 | add_filter( "manage_edit-{$taxonomy}_columns", array( $this, 'register_column_headers' ) ); |
|
| 184 | add_filter( "manage_{$taxonomy}_custom_column", array( $this, 'return_column_display' ), 10, 3 ); |
||
| 185 | } |
||
| 186 | } |
||
| 187 | |||
| 188 | add_action( 'created_term', array( $this, 'save_term' ), 10, 3 ); |
||
| 189 | add_action( 'edited_terms', array( $this, 'save_term' ), 10, 2 ); |
||
| 190 | add_action( 'delete_term', array( $this, 'delete_term' ), 10, 3 ); |
||
| 191 | |||
| 192 | } |
||
| 193 | |||
| 194 | /** |
||
| 195 | * Registers styles for CMB2 |
||
| 196 | * @since 2.0.7 |
||
| 197 | */ |
||
| 198 | protected static function register_styles() { |
||
| 199 | if ( self::$css_registration_done ) { |
||
| 200 | return; |
||
| 201 | } |
||
| 202 | |||
| 203 | // Only use minified files if SCRIPT_DEBUG is off |
||
| 204 | $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
||
| 205 | $front = is_admin() ? '' : '-front'; |
||
| 206 | $rtl = is_rtl() ? '-rtl' : ''; |
||
| 207 | |||
| 208 | // Filter required styles and register stylesheet |
||
| 209 | $dependencies = apply_filters( 'cmb2_style_dependencies', array() ); |
||
| 210 | wp_register_style( 'cmb2-styles', CMB2_Utils::url( "css/cmb2{$front}{$rtl}{$min}.css" ), $dependencies ); |
||
| 211 | wp_register_style( 'cmb2-display-styles', CMB2_Utils::url( "css/cmb2-display{$rtl}{$min}.css" ), $dependencies ); |
||
| 212 | |||
| 213 | self::$css_registration_done = true; |
||
| 214 | } |
||
| 215 | |||
| 216 | /** |
||
| 217 | * Registers scripts for CMB2 |
||
| 218 | * @since 2.0.7 |
||
| 219 | */ |
||
| 220 | protected static function register_js() { |
||
| 221 | if ( self::$js_registration_done ) { |
||
| 222 | return; |
||
| 223 | } |
||
| 224 | |||
| 225 | $hook = is_admin() ? 'admin_footer' : 'wp_footer'; |
||
| 226 | add_action( $hook, array( 'CMB2_JS', 'enqueue' ), 8 ); |
||
| 227 | |||
| 228 | self::$js_registration_done = true; |
||
| 229 | } |
||
| 230 | |||
| 231 | /** |
||
| 232 | * Registers scripts and styles for CMB2 |
||
| 233 | * @since 1.0.0 |
||
| 234 | */ |
||
| 235 | public static function register_scripts() { |
||
| 236 | self::register_styles(); |
||
| 237 | self::register_js(); |
||
| 238 | } |
||
| 239 | |||
| 240 | /** |
||
| 241 | * Enqueues scripts and styles for CMB2 in admin_head. |
||
| 242 | * @since 1.0.0 |
||
| 243 | */ |
||
| 244 | public function do_scripts( $hook ) { |
||
| 245 | $hooks = array( |
||
| 246 | 'post.php', |
||
| 247 | 'post-new.php', |
||
| 248 | 'page-new.php', |
||
| 249 | 'page.php', |
||
| 250 | 'comment.php', |
||
| 251 | 'edit-tags.php', |
||
| 252 | 'term.php', |
||
| 253 | 'user-new.php', |
||
| 254 | 'profile.php', |
||
| 255 | 'user-edit.php', |
||
| 256 | ); |
||
| 257 | // only pre-enqueue our scripts/styles on the proper pages |
||
| 258 | // show_form_for_type will have us covered if we miss something here. |
||
| 259 | if ( in_array( $hook, $hooks, true ) ) { |
||
| 260 | if ( $this->cmb->prop( 'cmb_styles' ) ) { |
||
| 261 | self::enqueue_cmb_css(); |
||
| 262 | } |
||
| 263 | if ( $this->cmb->prop( 'enqueue_js' ) ) { |
||
| 264 | self::enqueue_cmb_js(); |
||
| 265 | } |
||
| 266 | } |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Register the CMB2 field column headers. |
||
| 271 | * @since 2.2.2 |
||
| 272 | */ |
||
| 273 | public function register_column_headers( $columns ) { |
||
| 274 | $fields = $this->cmb->prop( 'fields' ); |
||
| 275 | |||
| 276 | foreach ( $fields as $key => $field ) { |
||
| 277 | if ( ! isset( $field['column'] ) ) { |
||
| 278 | continue; |
||
| 279 | } |
||
| 280 | |||
| 281 | $column = $field['column']; |
||
| 282 | |||
| 283 | if ( false === $column['position'] ) { |
||
| 284 | |||
| 285 | $columns[ $field['id'] ] = $column['name']; |
||
| 286 | |||
| 287 | } else { |
||
| 288 | |||
| 289 | $before = array_slice( $columns, 0, absint( $column['position'] ) ); |
||
| 290 | $before[ $field['id'] ] = $column['name']; |
||
| 291 | $columns = $before + $columns; |
||
| 292 | } |
||
| 293 | |||
| 294 | $column['field'] = $field; |
||
| 295 | $this->columns[ $field['id'] ] = $column; |
||
| 296 | } |
||
| 297 | |||
| 298 | return $columns; |
||
| 299 | } |
||
| 300 | |||
| 301 | /** |
||
| 302 | * The CMB2 field column display output. |
||
| 303 | * @since 2.2.2 |
||
| 304 | */ |
||
| 305 | public function column_display( $column_name, $object_id ) { |
||
| 306 | if ( isset( $this->columns[ $column_name ] ) ) { |
||
| 307 | $field = new CMB2_Field( array( |
||
| 308 | 'field_args' => $this->columns[ $column_name ]['field'], |
||
| 309 | 'object_type' => $this->object_type, |
||
| 310 | 'object_id' => $this->cmb->object_id( $object_id ), |
||
| 311 | 'cmb_id' => $this->cmb->cmb_id, |
||
| 312 | ) ); |
||
| 313 | |||
| 314 | $this->cmb->get_field( $field )->render_column(); |
||
| 315 | } |
||
| 316 | } |
||
| 317 | |||
| 318 | /** |
||
| 319 | * Returns the column display. |
||
| 320 | * @since 2.2.2 |
||
| 321 | */ |
||
| 322 | public function return_column_display( $empty, $custom_column, $object_id ) { |
||
| 323 | ob_start(); |
||
| 324 | $this->column_display( $custom_column, $object_id ); |
||
| 325 | $column = ob_get_clean(); |
||
| 326 | |||
| 327 | return $column ? $column : $empty; |
||
| 328 | } |
||
| 329 | |||
| 330 | /** |
||
| 331 | * Output the CMB2 fields in an alternate context (not in a metabox). |
||
| 332 | * @since 2.2.4 |
||
| 333 | */ |
||
| 334 | public function add_context_metabox() { |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Add an additional CSS class for the form box when used in contexts outside of a metabox. |
||
| 347 | * @since 2.2.4 |
||
| 348 | * @param array $classes Array of classes for the cmb2-wrap. |
||
| 349 | * @param CMB2 $cmb This CMB2 object. |
||
| 350 | 1 | */ |
|
| 351 | 1 | public function add_context_class( $classes, $cmb ) { |
|
| 374 | |||
| 375 | /** |
||
| 376 | * Add metaboxes (to 'post' or 'comment' object types) |
||
| 377 | * @since 1.0.0 |
||
| 378 | */ |
||
| 379 | public function add_metaboxes() { |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Remove the specified default taxonomy metaboxes for a post-type. |
||
| 413 | * @since 2.2.3 |
||
| 414 | * @param string $post_type Post type to remove the metabox for. |
||
| 415 | */ |
||
| 416 | protected function remove_default_tax_metaboxes( $post_type ) { |
||
| 426 | |||
| 427 | /** |
||
| 428 | * Add 'closed' class to metabox |
||
| 429 | * @since 2.0.0 |
||
| 430 | * @param array $classes Array of classes |
||
| 431 | * @return array Modified array of classes |
||
| 432 | */ |
||
| 433 | public function close_metabox_class( $classes ) { |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Display metaboxes for a post or comment object |
||
| 440 | * @since 1.0.0 |
||
| 441 | */ |
||
| 442 | public function metabox_callback() { |
||
| 446 | |||
| 447 | /** |
||
| 448 | * Display metaboxes for new user page |
||
| 449 | * @since 1.0.0 |
||
| 450 | */ |
||
| 451 | public function user_new_metabox( $section ) { |
||
| 458 | |||
| 459 | /** |
||
| 460 | * Display metaboxes for a user object |
||
| 461 | * @since 1.0.0 |
||
| 462 | */ |
||
| 463 | public function user_metabox() { |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Display metaboxes for a taxonomy term object |
||
| 469 | * @since 2.2.0 |
||
| 470 | */ |
||
| 471 | public function term_metabox() { |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Display metaboxes for an object type |
||
| 477 | * @since 2.2.0 |
||
| 478 | * @param string $type Object type |
||
| 479 | * @return void |
||
| 480 | */ |
||
| 481 | public function show_form_for_type( $type ) { |
||
| 499 | |||
| 500 | /** |
||
| 501 | * Determines if metabox should be shown in current context |
||
| 502 | * @since 2.0.0 |
||
| 503 | * @return bool Whether metabox should be added/shown |
||
| 504 | */ |
||
| 505 | public function show_on() { |
||
| 520 | |||
| 521 | /** |
||
| 522 | * Get the CMB priority property set to numeric hook priority. |
||
| 523 | * @since 2.2.0 |
||
| 524 | * @param integer $default Default display hook priority. |
||
| 525 | * @return integer Hook priority. |
||
| 526 | */ |
||
| 527 | public function get_priority( $default = 10 ) { |
||
| 549 | |||
| 550 | /** |
||
| 551 | * Save data from post metabox |
||
| 552 | * @since 1.0.0 |
||
| 553 | * @param int $post_id Post ID |
||
| 554 | * @param mixed $post Post object |
||
| 555 | * @return null |
||
| 556 | */ |
||
| 557 | public function save_post( $post_id, $post = false ) { |
||
| 576 | |||
| 577 | /** |
||
| 578 | * Save data from comment metabox |
||
| 579 | * @since 2.0.9 |
||
| 580 | * @param int $comment_id Comment ID |
||
| 581 | * @return null |
||
| 582 | */ |
||
| 583 | public function save_comment( $comment_id ) { |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Save data from user fields |
||
| 594 | * @since 1.0.x |
||
| 595 | * @param int $user_id User ID |
||
| 596 | * @return null |
||
| 597 | */ |
||
| 598 | public function save_user( $user_id ) { |
||
| 604 | |||
| 605 | /** |
||
| 606 | * Save data from term fields |
||
| 607 | * @since 2.2.0 |
||
| 608 | * @param int $term_id Term ID |
||
| 609 | * @param int $tt_id Term Taxonomy ID |
||
| 610 | * @param string $taxonomy Taxonomy |
||
| 611 | * @return null |
||
| 612 | */ |
||
| 613 | public function save_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
| 621 | |||
| 622 | /** |
||
| 623 | * Delete term meta when a term is deleted. |
||
| 624 | * @since 2.2.0 |
||
| 625 | * @param int $term_id Term ID |
||
| 626 | * @param int $tt_id Term Taxonomy ID |
||
| 627 | * @param string $taxonomy Taxonomy |
||
| 628 | * @return null |
||
| 629 | */ |
||
| 630 | public function delete_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
| 641 | |||
| 642 | /** |
||
| 643 | * Determines if the current object is able to be saved |
||
| 644 | * @since 2.0.9 |
||
| 645 | * @param string $type Current post_type or comment_type |
||
| 646 | * @return bool Whether object can be saved |
||
| 647 | */ |
||
| 648 | public function can_save( $type = '' ) { |
||
| 662 | |||
| 663 | /** |
||
| 664 | * Determine if taxonomy of term being modified is cmb2-editable. |
||
| 665 | * @since 2.2.0 |
||
| 666 | * @param string $taxonomy Taxonomy of term being modified. |
||
| 667 | * @return bool Whether taxonomy is editable. |
||
| 668 | */ |
||
| 669 | public function taxonomy_can_save( $taxonomy ) { |
||
| 682 | |||
| 683 | /** |
||
| 684 | * Enqueues the 'cmb2-display-styles' if the conditions match (has columns, on the right page, etc). |
||
| 685 | * @since 2.2.2.1 |
||
| 686 | */ |
||
| 687 | protected function maybe_enqueue_column_display_styles() { |
||
| 698 | |||
| 699 | /** |
||
| 700 | * Includes CMB2 styles |
||
| 701 | * @since 2.0.0 |
||
| 702 | */ |
||
| 703 | public static function enqueue_cmb_css( $handle = 'cmb2-styles' ) { |
||
| 716 | |||
| 717 | /** |
||
| 718 | * Includes CMB2 JS |
||
| 719 | * @since 2.0.0 |
||
| 720 | */ |
||
| 721 | public static function enqueue_cmb_js() { |
||
| 729 | |||
| 730 | } |
||
| 731 |
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.