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 | * Array of all hooks done (to be run once) |
||
| 18 | * @var array |
||
| 19 | * @since 2.0.0 |
||
| 20 | */ |
||
| 21 | protected static $hooks_completed = array(); |
||
| 22 | |||
| 23 | /** |
||
| 24 | * Only allow JS registration once |
||
| 25 | * @var bool |
||
| 26 | * @since 2.0.7 |
||
| 27 | */ |
||
| 28 | protected static $js_registration_done = false; |
||
| 29 | |||
| 30 | /** |
||
| 31 | * Only allow CSS registration once |
||
| 32 | * @var bool |
||
| 33 | * @since 2.0.7 |
||
| 34 | */ |
||
| 35 | protected static $css_registration_done = false; |
||
| 36 | |||
| 37 | /** |
||
| 38 | * @var CMB2 object |
||
| 39 | * @since 2.0.2 |
||
| 40 | */ |
||
| 41 | protected $cmb; |
||
| 42 | |||
| 43 | /** |
||
| 44 | * CMB taxonomies array for term meta |
||
| 45 | * @var array |
||
| 46 | * @since 2.2.0 |
||
| 47 | */ |
||
| 48 | protected $taxonomies = array(); |
||
| 49 | |||
| 50 | /** |
||
| 51 | * The object type we are performing the hookup for |
||
| 52 | * @var string |
||
| 53 | * @since 2.0.9 |
||
| 54 | */ |
||
| 55 | protected $object_type = 'post'; |
||
| 56 | |||
| 57 | /** |
||
| 58 | * Custom field columns. |
||
| 59 | * @var array |
||
| 60 | * @since 2.2.2 |
||
| 61 | */ |
||
| 62 | protected $columns = array(); |
||
| 63 | |||
| 64 | public function __construct( CMB2 $cmb ) { |
||
| 65 | $this->cmb = $cmb; |
||
| 66 | $this->object_type = $this->cmb->mb_object_type(); |
||
| 67 | |||
| 68 | $this->universal_hooks(); |
||
| 69 | |||
| 70 | if ( is_admin() ) { |
||
| 71 | |||
| 72 | switch ( $this->object_type ) { |
||
| 73 | case 'post': |
||
| 74 | return $this->post_hooks(); |
||
| 75 | case 'comment': |
||
| 76 | return $this->comment_hooks(); |
||
| 77 | case 'user': |
||
| 78 | return $this->user_hooks(); |
||
| 79 | case 'term': |
||
| 80 | return $this->term_hooks(); |
||
| 81 | } |
||
| 82 | |||
| 83 | } |
||
| 84 | } |
||
| 85 | |||
| 86 | public function universal_hooks() { |
||
| 87 | foreach ( get_class_methods( 'CMB2_Show_Filters' ) as $filter ) { |
||
| 88 | add_filter( 'cmb2_show_on', array( 'CMB2_Show_Filters', $filter ), 10, 3 ); |
||
| 89 | } |
||
| 90 | |||
| 91 | if ( is_admin() ) { |
||
| 92 | // register our scripts and styles for cmb |
||
| 93 | $this->once( 'admin_enqueue_scripts', array( __CLASS__, 'register_scripts' ), 8 ); |
||
| 94 | $this->once( 'admin_enqueue_scripts', array( $this, 'do_scripts' ) ); |
||
| 95 | |||
| 96 | if ( $this->cmb->has_columns && $this->cmb->prop( 'cmb_styles' ) ) { |
||
| 97 | self::enqueue_cmb_css( 'cmb2-display-styles' ); |
||
| 98 | } |
||
| 99 | } |
||
| 100 | } |
||
| 101 | |||
| 102 | public function post_hooks() { |
||
| 103 | add_action( 'add_meta_boxes', array( $this, 'add_metaboxes' ) ); |
||
| 104 | add_action( 'add_attachment', array( $this, 'save_post' ) ); |
||
| 105 | add_action( 'edit_attachment', array( $this, 'save_post' ) ); |
||
| 106 | add_action( 'save_post', array( $this, 'save_post' ), 10, 2 ); |
||
| 107 | |||
| 108 | if ( $this->cmb->has_columns ) { |
||
| 109 | foreach ( $this->cmb->prop( 'object_types' ) as $post_type ) { |
||
| 110 | add_filter( "manage_{$post_type}_posts_columns", array( $this, 'register_column_headers' ) ); |
||
| 111 | add_action( "manage_{$post_type}_posts_custom_column", array( $this, 'column_display' ), 10, 2 ); |
||
| 112 | } |
||
| 113 | } |
||
| 114 | } |
||
| 115 | |||
| 116 | public function comment_hooks() { |
||
| 117 | add_action( 'add_meta_boxes_comment', array( $this, 'add_metaboxes' ) ); |
||
| 118 | add_action( 'edit_comment', array( $this, 'save_comment' ) ); |
||
| 119 | |||
| 120 | if ( $this->cmb->has_columns ) { |
||
| 121 | add_filter( 'manage_edit-comments_columns', array( $this, 'register_column_headers' ) ); |
||
| 122 | add_action( 'manage_comments_custom_column', array( $this, 'column_display' ), 10, 3 ); |
||
| 123 | } |
||
| 124 | } |
||
| 125 | |||
| 126 | public function user_hooks() { |
||
| 127 | $priority = $this->get_priority(); |
||
| 128 | |||
| 129 | add_action( 'show_user_profile', array( $this, 'user_metabox' ), $priority ); |
||
| 130 | add_action( 'edit_user_profile', array( $this, 'user_metabox' ), $priority ); |
||
| 131 | add_action( 'user_new_form', array( $this, 'user_new_metabox' ), $priority ); |
||
| 132 | |||
| 133 | add_action( 'personal_options_update', array( $this, 'save_user' ) ); |
||
| 134 | add_action( 'edit_user_profile_update', array( $this, 'save_user' ) ); |
||
| 135 | add_action( 'user_register', array( $this, 'save_user' ) ); |
||
| 136 | |||
| 137 | View Code Duplication | if ( $this->cmb->has_columns ) { |
|
| 138 | add_filter( 'manage_users_columns', array( $this, 'register_column_headers' ) ); |
||
| 139 | add_filter( 'manage_users_custom_column', array( $this, 'return_column_display' ), 10, 3 ); |
||
| 140 | } |
||
| 141 | } |
||
| 142 | |||
| 143 | public function term_hooks() { |
||
| 144 | if ( ! function_exists( 'get_term_meta' ) ) { |
||
| 145 | wp_die( __( 'Term Metadata is a WordPress > 4.4 feature. Please upgrade your WordPress install.', 'cmb2' ) ); |
||
| 146 | } |
||
| 147 | |||
| 148 | if ( ! $this->cmb->prop( 'taxonomies' ) ) { |
||
| 149 | wp_die( __( 'Term metaboxes configuration requires a \'taxonomies\' parameter', 'cmb2' ) ); |
||
| 150 | } |
||
| 151 | |||
| 152 | $this->taxonomies = (array) $this->cmb->prop( 'taxonomies' ); |
||
| 153 | $show_on_term_add = $this->cmb->prop( 'new_term_section' ); |
||
| 154 | $priority = $this->get_priority( 8 ); |
||
| 155 | |||
| 156 | foreach ( $this->taxonomies as $taxonomy ) { |
||
| 157 | // Display our form data |
||
| 158 | add_action( "{$taxonomy}_edit_form", array( $this, 'term_metabox' ), $priority, 2 ); |
||
| 159 | |||
| 160 | $show_on_add = is_array( $show_on_term_add ) |
||
| 161 | ? in_array( $taxonomy, $show_on_term_add ) |
||
| 162 | : (bool) $show_on_term_add; |
||
| 163 | |||
| 164 | $show_on_add = apply_filters( "cmb2_show_on_term_add_form_{$this->cmb->cmb_id}", $show_on_add, $this->cmb ); |
||
| 165 | |||
| 166 | // Display form in add-new section (unless specified not to) |
||
| 167 | if ( $show_on_add ) { |
||
| 168 | add_action( "{$taxonomy}_add_form_fields", array( $this, 'term_metabox' ), $priority, 2 ); |
||
| 169 | } |
||
| 170 | |||
| 171 | View Code Duplication | if ( $this->cmb->has_columns ) { |
|
| 172 | add_filter( "manage_edit-{$taxonomy}_columns", array( $this, 'register_column_headers' ) ); |
||
| 173 | add_filter( "manage_{$taxonomy}_custom_column", array( $this, 'return_column_display' ), 10, 3 ); |
||
| 174 | } |
||
| 175 | } |
||
| 176 | |||
| 177 | add_action( 'created_term', array( $this, 'save_term' ), 10, 3 ); |
||
| 178 | add_action( 'edited_terms', array( $this, 'save_term' ), 10, 2 ); |
||
| 179 | add_action( 'delete_term', array( $this, 'delete_term' ), 10, 3 ); |
||
| 180 | |||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Registers styles for CMB2 |
||
| 185 | * @since 2.0.7 |
||
| 186 | */ |
||
| 187 | 1 | protected static function register_styles() { |
|
| 188 | 1 | if ( self::$css_registration_done ) { |
|
| 189 | return; |
||
| 190 | } |
||
| 191 | |||
| 192 | // Only use minified files if SCRIPT_DEBUG is off |
||
| 193 | 1 | $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
|
| 194 | 1 | $front = is_admin() ? '' : '-front'; |
|
| 195 | 1 | $rtl = is_rtl() ? '-rtl' : ''; |
|
| 196 | |||
| 197 | // Filter required styles and register stylesheet |
||
| 198 | 1 | $dependencies = apply_filters( 'cmb2_style_dependencies', array() ); |
|
| 199 | 1 | wp_register_style( 'cmb2-styles', cmb2_utils()->url( "css/cmb2{$front}{$rtl}{$min}.css" ), $dependencies ); |
|
| 200 | 1 | wp_register_style( 'cmb2-display-styles', cmb2_utils()->url( "css/cmb2-display{$rtl}{$min}.css" ), $dependencies ); |
|
| 201 | |||
| 202 | 1 | self::$css_registration_done = true; |
|
| 203 | 1 | } |
|
| 204 | |||
| 205 | /** |
||
| 206 | * Registers scripts for CMB2 |
||
| 207 | * @since 2.0.7 |
||
| 208 | */ |
||
| 209 | 1 | protected static function register_js() { |
|
| 219 | |||
| 220 | /** |
||
| 221 | * Registers scripts and styles for CMB2 |
||
| 222 | * @since 1.0.0 |
||
| 223 | */ |
||
| 224 | public static function register_scripts() { |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Enqueues scripts and styles for CMB2 in admin_head. |
||
| 231 | * @since 1.0.0 |
||
| 232 | */ |
||
| 233 | public function do_scripts( $hook ) { |
||
| 257 | |||
| 258 | /** |
||
| 259 | * Register the CMB2 field column headers. |
||
| 260 | * @since 2.2.2 |
||
| 261 | */ |
||
| 262 | public function register_column_headers( $columns ) { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * The CMB2 field column display output. |
||
| 292 | * @since 2.2.2 |
||
| 293 | */ |
||
| 294 | public function column_display( $column_name, $object_id ) { |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Returns the column display. |
||
| 309 | * @since 2.2.2 |
||
| 310 | */ |
||
| 311 | public function return_column_display( $empty, $custom_column, $object_id ) { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Add metaboxes (to 'post' or 'comment' object types) |
||
| 321 | * @since 1.0.0 |
||
| 322 | */ |
||
| 323 | public function add_metaboxes() { |
||
| 350 | |||
| 351 | /** |
||
| 352 | * Add 'closed' class to metabox |
||
| 353 | * @since 2.0.0 |
||
| 354 | * @param array $classes Array of classes |
||
| 355 | * @return array Modified array of classes |
||
| 356 | */ |
||
| 357 | public function close_metabox_class( $classes ) { |
||
| 361 | |||
| 362 | /** |
||
| 363 | * Display metaboxes for a post or comment object |
||
| 364 | * @since 1.0.0 |
||
| 365 | */ |
||
| 366 | public function metabox_callback() { |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Display metaboxes for new user page |
||
| 373 | * @since 1.0.0 |
||
| 374 | */ |
||
| 375 | public function user_new_metabox( $section ) { |
||
| 382 | |||
| 383 | /** |
||
| 384 | * Display metaboxes for a user object |
||
| 385 | * @since 1.0.0 |
||
| 386 | */ |
||
| 387 | public function user_metabox() { |
||
| 390 | |||
| 391 | /** |
||
| 392 | * Display metaboxes for a taxonomy term object |
||
| 393 | * @since 2.2.0 |
||
| 394 | */ |
||
| 395 | public function term_metabox() { |
||
| 396 | $this->show_form_for_type( 'term' ); |
||
| 397 | } |
||
| 398 | |||
| 399 | /** |
||
| 400 | * Display metaboxes for an object type |
||
| 401 | * @since 2.2.0 |
||
| 402 | * @param string $type Object type |
||
| 403 | * @return void |
||
| 404 | */ |
||
| 405 | public function show_form_for_type( $type ) { |
||
| 406 | if ( $type != $this->cmb->mb_object_type() ) { |
||
| 407 | return; |
||
| 408 | } |
||
| 409 | |||
| 410 | if ( ! $this->show_on() ) { |
||
| 411 | return; |
||
| 412 | } |
||
| 413 | |||
| 414 | if ( $this->cmb->prop( 'cmb_styles' ) ) { |
||
| 415 | self::enqueue_cmb_css(); |
||
| 416 | } |
||
| 417 | if ( $this->cmb->prop( 'enqueue_js' ) ) { |
||
| 418 | self::enqueue_cmb_js(); |
||
| 419 | } |
||
| 420 | |||
| 421 | $this->cmb->show_form( 0, $type ); |
||
| 422 | } |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Determines if metabox should be shown in current context |
||
| 426 | * @since 2.0.0 |
||
| 427 | * @return bool Whether metabox should be added/shown |
||
| 428 | */ |
||
| 429 | public function show_on() { |
||
| 430 | // If metabox is requesting to be conditionally shown |
||
| 431 | $show = $this->cmb->should_show(); |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Filter to determine if metabox should show. Default is true |
||
| 435 | * |
||
| 436 | * @param array $show Default is true, show the metabox |
||
| 437 | * @param mixed $meta_box_args Array of the metabox arguments |
||
| 438 | * @param mixed $cmb The CMB2 instance |
||
| 439 | */ |
||
| 440 | $show = (bool) apply_filters( 'cmb2_show_on', $show, $this->cmb->meta_box, $this->cmb ); |
||
| 441 | |||
| 442 | return $show; |
||
| 443 | } |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Get the CMB priority property set to numeric hook priority. |
||
| 447 | * @since 2.2.0 |
||
| 448 | * @param integer $default Default display hook priority. |
||
| 449 | * @return integer Hook priority. |
||
| 450 | */ |
||
| 451 | public function get_priority( $default = 10 ) { |
||
| 473 | |||
| 474 | /** |
||
| 475 | * Save data from post metabox |
||
| 476 | * @since 1.0.0 |
||
| 477 | * @param int $post_id Post ID |
||
| 478 | * @param mixed $post Post object |
||
| 479 | * @return null |
||
| 480 | */ |
||
| 481 | public function save_post( $post_id, $post = false ) { |
||
| 500 | |||
| 501 | /** |
||
| 502 | * Save data from comment metabox |
||
| 503 | * @since 2.0.9 |
||
| 504 | * @param int $comment_id Comment ID |
||
| 505 | * @return null |
||
| 506 | */ |
||
| 507 | public function save_comment( $comment_id ) { |
||
| 515 | |||
| 516 | /** |
||
| 517 | * Save data from user fields |
||
| 518 | * @since 1.0.x |
||
| 519 | * @param int $user_id User ID |
||
| 520 | * @return null |
||
| 521 | */ |
||
| 522 | public function save_user( $user_id ) { |
||
| 528 | |||
| 529 | /** |
||
| 530 | * Save data from term fields |
||
| 531 | * @since 2.2.0 |
||
| 532 | * @param int $term_id Term ID |
||
| 533 | * @param int $tt_id Term Taxonomy ID |
||
| 534 | * @param string $taxonomy Taxonomy |
||
| 535 | * @return null |
||
| 536 | */ |
||
| 537 | public function save_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
| 538 | $taxonomy = $taxonomy ? $taxonomy : $tt_id; |
||
| 539 | |||
| 540 | // check permissions |
||
| 541 | if ( $this->taxonomy_can_save( $taxonomy ) && $this->can_save( 'term' ) ) { |
||
| 542 | $this->cmb->save_fields( $term_id, 'term', $_POST ); |
||
| 543 | } |
||
| 544 | } |
||
| 545 | |||
| 546 | /** |
||
| 547 | * Delete term meta when a term is deleted. |
||
| 548 | * @since 2.2.0 |
||
| 549 | * @param int $term_id Term ID |
||
| 550 | * @param int $tt_id Term Taxonomy ID |
||
| 551 | * @param string $taxonomy Taxonomy |
||
| 552 | * @return null |
||
| 553 | */ |
||
| 554 | public function delete_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
| 555 | if ( $this->taxonomy_can_save( $taxonomy ) ) { |
||
| 556 | |||
| 557 | foreach ( $this->cmb->prop( 'fields' ) as $field ) { |
||
| 558 | $data_to_delete[ $field['id'] ] = ''; |
||
| 559 | } |
||
| 560 | |||
| 561 | $this->cmb->save_fields( $term_id, 'term', $data_to_delete ); |
||
| 562 | } |
||
| 563 | } |
||
| 564 | |||
| 565 | /** |
||
| 566 | * Determines if the current object is able to be saved |
||
| 567 | * @since 2.0.9 |
||
| 568 | * @param string $type Current post_type or comment_type |
||
| 569 | * @return bool Whether object can be saved |
||
| 570 | */ |
||
| 571 | public function can_save( $type = '' ) { |
||
| 583 | |||
| 584 | /** |
||
| 585 | * Determine if taxonomy of term being modified is cmb2-editable. |
||
| 586 | * @since 2.2.0 |
||
| 587 | * @param string $taxonomy Taxonomy of term being modified. |
||
| 588 | * @return bool Whether taxonomy is editable. |
||
| 589 | */ |
||
| 590 | public function taxonomy_can_save( $taxonomy ) { |
||
| 591 | if ( empty( $this->taxonomies ) || ! in_array( $taxonomy, $this->taxonomies ) ) { |
||
| 592 | return false; |
||
| 593 | } |
||
| 594 | |||
| 595 | $taxonomy_object = get_taxonomy( $taxonomy ); |
||
| 603 | |||
| 604 | /** |
||
| 605 | * Includes CMB2 styles |
||
| 606 | * @since 2.0.0 |
||
| 607 | */ |
||
| 608 | public static function enqueue_cmb_css( $handle = 'cmb2-styles' ) { |
||
| 616 | |||
| 617 | /** |
||
| 618 | * Includes CMB2 JS |
||
| 619 | * @since 2.0.0 |
||
| 620 | */ |
||
| 621 | public static function enqueue_cmb_js() { |
||
| 629 | |||
| 630 | } |
||
| 631 |
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.