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() { |
||
| 118 | |||
| 119 | public function comment_hooks() { |
||
| 128 | |||
| 129 | public function user_hooks() { |
||
| 145 | |||
| 146 | public function term_hooks() { |
||
| 185 | |||
| 186 | 1 | /** |
|
| 187 | 1 | * Registers styles for CMB2 |
|
| 188 | * @since 2.0.7 |
||
| 189 | */ |
||
| 190 | protected static function register_styles() { |
||
| 207 | |||
| 208 | /** |
||
| 209 | * Registers scripts for CMB2 |
||
| 210 | * @since 2.0.7 |
||
| 211 | */ |
||
| 212 | protected static function register_js() { |
||
| 222 | |||
| 223 | /** |
||
| 224 | * Registers scripts and styles for CMB2 |
||
| 225 | * @since 1.0.0 |
||
| 226 | */ |
||
| 227 | public static function register_scripts() { |
||
| 231 | |||
| 232 | /** |
||
| 233 | * Enqueues scripts and styles for CMB2 in admin_head. |
||
| 234 | * @since 1.0.0 |
||
| 235 | */ |
||
| 236 | public function do_scripts( $hook ) { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Register the CMB2 field column headers. |
||
| 263 | * @since 2.2.2 |
||
| 264 | */ |
||
| 265 | public function register_column_headers( $columns ) { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * The CMB2 field column display output. |
||
| 295 | * @since 2.2.2 |
||
| 296 | */ |
||
| 297 | public function column_display( $column_name, $object_id ) { |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Returns the column display. |
||
| 312 | * @since 2.2.2 |
||
| 313 | */ |
||
| 314 | public function return_column_display( $empty, $custom_column, $object_id ) { |
||
| 321 | |||
| 322 | /** |
||
| 323 | * Output the CMB2 fields in an alternate context (not in a metabox). |
||
| 324 | * @since 2.2.4 |
||
| 325 | */ |
||
| 326 | public function add_context_metaboxes() { |
||
| 327 | |||
| 328 | if ( ! $this->show_on() ) { |
||
| 329 | return; |
||
| 330 | } |
||
| 331 | |||
| 332 | $current_screen = get_current_screen(); |
||
| 333 | |||
| 334 | foreach ( $this->cmb->prop( 'object_types' ) as $object_type ) { |
||
| 335 | $screen = convert_to_screen( $object_type ); |
||
| 336 | |||
| 337 | // If we're on the right post-type/object, stop searching... |
||
| 338 | if ( isset( $screen->id ) && $screen->id === $current_screen->id ) { |
||
| 339 | // And show the form. |
||
| 340 | return $this->cmb->show_form(); |
||
| 341 | } |
||
| 342 | } |
||
| 343 | } |
||
| 344 | |||
| 345 | /** |
||
| 346 | * Add metaboxes (to 'post' or 'comment' object types) |
||
| 347 | * @since 1.0.0 |
||
| 348 | */ |
||
| 349 | public function add_metaboxes() { |
||
| 350 | |||
| 351 | if ( ! $this->show_on() ) { |
||
| 352 | return; |
||
| 353 | } |
||
| 354 | |||
| 355 | /** |
||
| 356 | * To keep from registering an actual post-screen metabox, |
||
| 357 | * omit the 'title' attribute from the metabox registration array. |
||
| 358 | * |
||
| 359 | * (WordPress will not display metaboxes without titles anyway) |
||
| 360 | * |
||
| 361 | * This is a good solution if you want to output your metaboxes |
||
| 362 | * Somewhere else in the post-screen |
||
| 363 | */ |
||
| 364 | if ( ! $this->cmb->prop( 'title' ) ) { |
||
| 365 | return; |
||
| 366 | } |
||
| 367 | |||
| 368 | foreach ( $this->cmb->prop( 'object_types' ) as $post_type ) { |
||
| 369 | if ( $this->cmb->prop( 'closed' ) ) { |
||
| 370 | add_filter( "postbox_classes_{$post_type}_{$this->cmb->cmb_id}", array( $this, 'close_metabox_class' ) ); |
||
| 371 | } |
||
| 372 | |||
| 373 | if ( count( $this->cmb->tax_metaboxes_to_remove ) ) { |
||
| 374 | $this->remove_default_tax_metaboxes( $post_type ); |
||
| 375 | } |
||
| 376 | |||
| 377 | add_meta_box( $this->cmb->cmb_id, $this->cmb->prop( 'title' ), array( $this, 'metabox_callback' ), $post_type, $this->cmb->prop( 'context' ), $this->cmb->prop( 'priority' ) ); |
||
| 378 | } |
||
| 379 | } |
||
| 380 | |||
| 381 | /** |
||
| 382 | * Remove the specified default taxonomy metaboxes for a post-type. |
||
| 383 | * @since 2.2.3 |
||
| 384 | * @param string $post_type Post type to remove the metabox for. |
||
| 385 | */ |
||
| 386 | protected function remove_default_tax_metaboxes( $post_type ) { |
||
| 387 | foreach ( $this->cmb->tax_metaboxes_to_remove as $taxonomy ) { |
||
| 388 | if ( ! taxonomy_exists( $taxonomy ) ) { |
||
| 389 | continue; |
||
| 390 | } |
||
| 391 | |||
| 392 | $mb_id = is_taxonomy_hierarchical( $taxonomy ) ? "{$taxonomy}div" : "tagsdiv-{$taxonomy}"; |
||
| 393 | remove_meta_box( $mb_id, $post_type, 'side' ); |
||
| 394 | } |
||
| 395 | } |
||
| 396 | |||
| 397 | /** |
||
| 398 | * Add 'closed' class to metabox |
||
| 399 | * @since 2.0.0 |
||
| 400 | * @param array $classes Array of classes |
||
| 401 | * @return array Modified array of classes |
||
| 402 | */ |
||
| 403 | public function close_metabox_class( $classes ) { |
||
| 404 | $classes[] = 'closed'; |
||
| 405 | return $classes; |
||
| 406 | } |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Display metaboxes for a post or comment object |
||
| 410 | * @since 1.0.0 |
||
| 411 | */ |
||
| 412 | public function metabox_callback() { |
||
| 413 | $object_id = 'comment' == $this->object_type ? get_comment_ID() : get_the_ID(); |
||
| 414 | $this->cmb->show_form( $object_id, $this->object_type ); |
||
| 415 | } |
||
| 416 | |||
| 417 | /** |
||
| 418 | * Display metaboxes for new user page |
||
| 419 | * @since 1.0.0 |
||
| 420 | */ |
||
| 421 | public function user_new_metabox( $section ) { |
||
| 422 | if ( $section == $this->cmb->prop( 'new_user_section' ) ) { |
||
| 423 | $object_id = $this->cmb->object_id(); |
||
| 424 | $this->cmb->object_id( isset( $_REQUEST['user_id'] ) ? $_REQUEST['user_id'] : $object_id ); |
||
| 425 | $this->user_metabox(); |
||
| 426 | } |
||
| 427 | } |
||
| 428 | |||
| 429 | /** |
||
| 430 | * Display metaboxes for a user object |
||
| 431 | * @since 1.0.0 |
||
| 432 | */ |
||
| 433 | public function user_metabox() { |
||
| 436 | |||
| 437 | /** |
||
| 438 | * Display metaboxes for a taxonomy term object |
||
| 439 | * @since 2.2.0 |
||
| 440 | */ |
||
| 441 | public function term_metabox() { |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Display metaboxes for an object type |
||
| 447 | * @since 2.2.0 |
||
| 448 | * @param string $type Object type |
||
| 449 | * @return void |
||
| 450 | */ |
||
| 451 | public function show_form_for_type( $type ) { |
||
| 452 | if ( $type != $this->cmb->mb_object_type() ) { |
||
| 453 | return; |
||
| 454 | } |
||
| 455 | |||
| 456 | if ( ! $this->show_on() ) { |
||
| 457 | return; |
||
| 458 | } |
||
| 459 | |||
| 460 | if ( $this->cmb->prop( 'cmb_styles' ) ) { |
||
| 461 | self::enqueue_cmb_css(); |
||
| 462 | } |
||
| 463 | if ( $this->cmb->prop( 'enqueue_js' ) ) { |
||
| 464 | self::enqueue_cmb_js(); |
||
| 465 | } |
||
| 466 | |||
| 467 | $this->cmb->show_form( 0, $type ); |
||
| 468 | } |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Determines if metabox should be shown in current context |
||
| 472 | * @since 2.0.0 |
||
| 473 | * @return bool Whether metabox should be added/shown |
||
| 474 | */ |
||
| 475 | public function show_on() { |
||
| 476 | // If metabox is requesting to be conditionally shown |
||
| 477 | $show = $this->cmb->should_show(); |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Filter to determine if metabox should show. Default is true |
||
| 481 | * |
||
| 482 | * @param array $show Default is true, show the metabox |
||
| 483 | * @param mixed $meta_box_args Array of the metabox arguments |
||
| 484 | * @param mixed $cmb The CMB2 instance |
||
| 485 | */ |
||
| 486 | $show = (bool) apply_filters( 'cmb2_show_on', $show, $this->cmb->meta_box, $this->cmb ); |
||
| 487 | |||
| 488 | return $show; |
||
| 489 | } |
||
| 490 | |||
| 491 | /** |
||
| 492 | * Get the CMB priority property set to numeric hook priority. |
||
| 493 | * @since 2.2.0 |
||
| 494 | * @param integer $default Default display hook priority. |
||
| 495 | * @return integer Hook priority. |
||
| 496 | */ |
||
| 497 | public function get_priority( $default = 10 ) { |
||
| 498 | $priority = $this->cmb->prop( 'priority' ); |
||
| 499 | |||
| 500 | if ( ! is_numeric( $priority ) ) { |
||
| 501 | switch ( $priority ) { |
||
| 502 | |||
| 503 | case 'high': |
||
| 504 | $priority = 5; |
||
| 505 | break; |
||
| 506 | |||
| 507 | case 'low': |
||
| 508 | $priority = 20; |
||
| 509 | break; |
||
| 510 | |||
| 511 | default: |
||
| 512 | $priority = $default; |
||
| 513 | break; |
||
| 514 | } |
||
| 515 | } |
||
| 516 | |||
| 517 | return $priority; |
||
| 518 | } |
||
| 519 | |||
| 520 | /** |
||
| 521 | * Save data from post metabox |
||
| 522 | * @since 1.0.0 |
||
| 523 | * @param int $post_id Post ID |
||
| 524 | * @param mixed $post Post object |
||
| 525 | * @return null |
||
| 526 | */ |
||
| 527 | public function save_post( $post_id, $post = false ) { |
||
| 528 | |||
| 529 | $post_type = $post ? $post->post_type : get_post_type( $post_id ); |
||
| 530 | |||
| 531 | $do_not_pass_go = ( |
||
| 532 | ! $this->can_save( $post_type ) |
||
| 533 | // check user editing permissions |
||
| 534 | || ( 'page' == $post_type && ! current_user_can( 'edit_page', $post_id ) ) |
||
| 535 | || ! current_user_can( 'edit_post', $post_id ) |
||
| 536 | ); |
||
| 537 | |||
| 538 | if ( $do_not_pass_go ) { |
||
| 539 | // do not collect $200 |
||
| 540 | return; |
||
| 541 | } |
||
| 542 | |||
| 543 | // take a trip to reading railroad – if you pass go collect $200 |
||
| 544 | $this->cmb->save_fields( $post_id, 'post', $_POST ); |
||
| 545 | } |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Save data from comment metabox |
||
| 549 | * @since 2.0.9 |
||
| 550 | * @param int $comment_id Comment ID |
||
| 551 | * @return null |
||
| 552 | */ |
||
| 553 | public function save_comment( $comment_id ) { |
||
| 554 | |||
| 555 | $can_edit = current_user_can( 'moderate_comments', $comment_id ); |
||
| 556 | |||
| 557 | if ( $this->can_save( get_comment_type( $comment_id ) ) && $can_edit ) { |
||
| 558 | $this->cmb->save_fields( $comment_id, 'comment', $_POST ); |
||
| 559 | } |
||
| 560 | } |
||
| 561 | |||
| 562 | /** |
||
| 563 | * Save data from user fields |
||
| 564 | * @since 1.0.x |
||
| 565 | * @param int $user_id User ID |
||
| 566 | * @return null |
||
| 567 | */ |
||
| 568 | public function save_user( $user_id ) { |
||
| 569 | // check permissions |
||
| 570 | if ( $this->can_save( 'user' ) ) { |
||
| 571 | $this->cmb->save_fields( $user_id, 'user', $_POST ); |
||
| 572 | } |
||
| 573 | } |
||
| 574 | |||
| 575 | /** |
||
| 576 | * Save data from term fields |
||
| 577 | * @since 2.2.0 |
||
| 578 | * @param int $term_id Term ID |
||
| 579 | * @param int $tt_id Term Taxonomy ID |
||
| 580 | * @param string $taxonomy Taxonomy |
||
| 581 | * @return null |
||
| 582 | */ |
||
| 583 | public function save_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
| 584 | $taxonomy = $taxonomy ? $taxonomy : $tt_id; |
||
| 585 | |||
| 586 | // check permissions |
||
| 587 | if ( $this->taxonomy_can_save( $taxonomy ) && $this->can_save( 'term' ) ) { |
||
| 588 | $this->cmb->save_fields( $term_id, 'term', $_POST ); |
||
| 589 | } |
||
| 590 | } |
||
| 591 | |||
| 592 | /** |
||
| 593 | * Delete term meta when a term is deleted. |
||
| 594 | * @since 2.2.0 |
||
| 595 | * @param int $term_id Term ID |
||
| 596 | * @param int $tt_id Term Taxonomy ID |
||
| 597 | * @param string $taxonomy Taxonomy |
||
| 598 | * @return null |
||
| 599 | */ |
||
| 600 | public function delete_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
| 601 | if ( $this->taxonomy_can_save( $taxonomy ) ) { |
||
| 602 | |||
| 603 | $data_to_delete = array(); |
||
| 604 | foreach ( $this->cmb->prop( 'fields' ) as $field ) { |
||
| 605 | $data_to_delete[ $field['id'] ] = ''; |
||
| 606 | } |
||
| 607 | |||
| 608 | $this->cmb->save_fields( $term_id, 'term', $data_to_delete ); |
||
| 609 | } |
||
| 610 | } |
||
| 611 | |||
| 612 | /** |
||
| 613 | * Determines if the current object is able to be saved |
||
| 614 | * @since 2.0.9 |
||
| 615 | * @param string $type Current post_type or comment_type |
||
| 616 | * @return bool Whether object can be saved |
||
| 617 | */ |
||
| 618 | public function can_save( $type = '' ) { |
||
| 619 | return apply_filters( 'cmb2_can_save', ( |
||
| 620 | $this->cmb->prop( 'save_fields' ) |
||
| 621 | // check nonce |
||
| 622 | && isset( $_POST[ $this->cmb->nonce() ] ) |
||
| 623 | && wp_verify_nonce( $_POST[ $this->cmb->nonce() ], $this->cmb->nonce() ) |
||
| 624 | 1 | // check if autosave |
|
| 625 | 1 | && ! ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) |
|
| 626 | // get the metabox types & compare it to this type |
||
| 627 | && ( $type && in_array( $type, $this->cmb->prop( 'object_types' ) ) ) |
||
| 628 | // Don't do updates during a switch-to-blog instance. |
||
| 629 | 1 | && ! ( is_multisite() && ms_is_switched() ) |
|
| 630 | ) ); |
||
| 631 | } |
||
| 632 | |||
| 633 | /** |
||
| 634 | * Determine if taxonomy of term being modified is cmb2-editable. |
||
| 635 | 1 | * @since 2.2.0 |
|
| 636 | * @param string $taxonomy Taxonomy of term being modified. |
||
| 637 | * @return bool Whether taxonomy is editable. |
||
| 638 | */ |
||
| 639 | public function taxonomy_can_save( $taxonomy ) { |
||
| 640 | if ( empty( $this->taxonomies ) || ! in_array( $taxonomy, $this->taxonomies ) ) { |
||
| 641 | return false; |
||
| 642 | 1 | } |
|
| 643 | 1 | ||
| 644 | $taxonomy_object = get_taxonomy( $taxonomy ); |
||
| 645 | // Can the user edit this term? |
||
| 646 | if ( ! isset( $taxonomy_object->cap ) || ! current_user_can( $taxonomy_object->cap->edit_terms ) ) { |
||
| 647 | 1 | return false; |
|
| 648 | 1 | } |
|
| 649 | |||
| 650 | return true; |
||
| 651 | } |
||
| 652 | |||
| 653 | /** |
||
| 654 | * Enqueues the 'cmb2-display-styles' if the conditions match (has columns, on the right page, etc). |
||
| 655 | * @since 2.2.2.1 |
||
| 656 | */ |
||
| 657 | protected function maybe_enqueue_column_display_styles() { |
||
| 668 | |||
| 669 | /** |
||
| 670 | * Includes CMB2 styles |
||
| 671 | * @since 2.0.0 |
||
| 672 | */ |
||
| 673 | public static function enqueue_cmb_css( $handle = 'cmb2-styles' ) { |
||
| 686 | |||
| 687 | /** |
||
| 688 | * Includes CMB2 JS |
||
| 689 | * @since 2.0.0 |
||
| 690 | */ |
||
| 691 | public static function enqueue_cmb_js() { |
||
| 699 | |||
| 700 | } |
||
| 701 |
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.