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 Post_Meta_Container 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 Post_Meta_Container, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 14 | class Post_Meta_Container extends Container { |
||
| 15 | /** |
||
| 16 | * List of registered unique field names |
||
| 17 | * |
||
| 18 | * @see verify_unique_field_name() |
||
| 19 | * @var array |
||
| 20 | */ |
||
| 21 | protected static $registered_field_names; |
||
| 22 | |||
| 23 | /** |
||
| 24 | * ID of the post the container is working with |
||
| 25 | * |
||
| 26 | * @see init() |
||
| 27 | * @var int |
||
| 28 | */ |
||
| 29 | protected $post_id; |
||
| 30 | |||
| 31 | /** |
||
| 32 | * List of default container settings |
||
| 33 | * |
||
| 34 | * @see init() |
||
| 35 | * @var array |
||
| 36 | */ |
||
| 37 | public $settings = array( |
||
| 38 | 'post_type' => array( 'post' ), |
||
| 39 | 'panel_context' => 'normal', |
||
| 40 | 'panel_priority' => 'high', |
||
| 41 | 'show_on' => array( |
||
| 42 | 'category' => null, |
||
| 43 | 'template_names' => array(), |
||
| 44 | 'not_in_template_names' => array(), |
||
| 45 | 'post_formats' => array(), |
||
| 46 | 'level_limit' => null, |
||
| 47 | 'tax_term_id' => null, |
||
| 48 | 'page_id' => null, |
||
| 49 | 'parent_page_id' => null, |
||
| 50 | 'post_path' => null, |
||
| 51 | ), |
||
| 52 | ); |
||
| 53 | |||
| 54 | /** |
||
| 55 | * Create a new post meta fields container |
||
| 56 | * |
||
| 57 | * @param string $title Unique title of the container |
||
| 58 | **/ |
||
| 59 | public function __construct( $title ) { |
||
| 60 | parent::__construct( $title ); |
||
| 61 | |||
| 62 | if ( ! $this->get_datastore() ) { |
||
| 63 | $this->set_datastore( new Post_Meta_Datastore() ); |
||
| 64 | } |
||
| 65 | } |
||
| 66 | |||
| 67 | /** |
||
| 68 | * Check if all required container settings have been specified |
||
| 69 | * |
||
| 70 | * @param array $settings Container settings |
||
| 71 | **/ |
||
| 72 | public function check_setup_settings( &$settings = array() ) { |
||
| 73 | if ( isset( $settings['show_on'] ) ) { |
||
| 74 | $invalid_settings = array_diff_key( $settings['show_on'], $this->settings['show_on'] ); |
||
| 75 | if ( ! empty( $invalid_settings ) ) { |
||
| 76 | Incorrect_Syntax_Exception::raise( 'Invalid show_on settings supplied to setup(): "' . implode( '", "', array_keys( $invalid_settings ) ) . '"' ); |
||
| 77 | } |
||
| 78 | } |
||
| 79 | |||
| 80 | if ( isset( $settings['show_on']['post_formats'] ) ) { |
||
| 81 | $settings['show_on']['post_formats'] = (array) $settings['show_on']['post_formats']; |
||
| 82 | } |
||
| 83 | |||
| 84 | if ( isset( $settings['show_on']['post_path'] ) ) { |
||
| 85 | $page = get_page_by_path( $settings['show_on']['post_path'] ); |
||
| 86 | |||
| 87 | if ( $page ) { |
||
| 88 | $settings['show_on']['page_id'] = $page->ID; |
||
| 89 | } else { |
||
| 90 | $settings['show_on']['page_id'] = -1; |
||
| 91 | } |
||
| 92 | } |
||
| 93 | |||
| 94 | // Transform category slug to taxonomy + term slug + term id |
||
| 95 | if ( isset( $settings['show_on']['category'] ) ) { |
||
| 96 | $term = get_term_by( 'slug', $settings['show_on']['category'], 'category' ); |
||
| 97 | |||
| 98 | if ( $term ) { |
||
| 99 | $settings['show_on']['tax_slug'] = $term->taxonomy; |
||
| 100 | $settings['show_on']['tax_term'] = $term->slug; |
||
| 101 | $settings['show_on']['tax_term_id'] = $term->term_id; |
||
| 102 | } |
||
| 103 | } |
||
| 104 | |||
| 105 | return parent::check_setup_settings( $settings ); |
||
| 106 | } |
||
| 107 | |||
| 108 | /** |
||
| 109 | * Create DataStore instance, set post ID to operate with (if such exists). |
||
| 110 | * Bind attach() and save() to the appropriate WordPress actions. |
||
| 111 | **/ |
||
| 112 | public function init() { |
||
| 129 | |||
| 130 | /** |
||
| 131 | * Perform save operation after successful is_valid_save() check. |
||
| 132 | * The call is propagated to all fields in the container. |
||
| 133 | * |
||
| 134 | * @param int $post_id ID of the post against which save() is ran |
||
| 135 | **/ |
||
| 136 | View Code Duplication | public function save( $post_id ) { |
|
| 150 | |||
| 151 | /** |
||
| 152 | * Perform checks whether the current save() request is valid |
||
| 153 | * Possible errors are triggering save() for autosave requests |
||
| 154 | * or performing post save outside of the post edit page (like Quick Edit) |
||
| 155 | * |
||
| 156 | * @see is_valid_save_conditions() |
||
| 157 | * @param int $post_id ID of the post against which save() is ran |
||
| 158 | * @return bool |
||
| 159 | **/ |
||
| 160 | public function is_valid_save( $post_id = 0 ) { |
||
| 171 | |||
| 172 | /** |
||
| 173 | * Perform checks whether the current save() request is valid |
||
| 174 | * Possible errors are triggering save() for autosave requests |
||
| 175 | * or performing post save outside of the post edit page (like Quick Edit) |
||
| 176 | * |
||
| 177 | * @param int $post_id ID of the post against which save() is ran |
||
| 178 | * @return bool |
||
| 179 | **/ |
||
| 180 | public function is_valid_save_conditions( $post_id ) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Add meta box for each of the container post types |
||
| 289 | **/ |
||
| 290 | public function attach() { |
||
| 291 | foreach ( $this->settings['post_type'] as $post_type ) { |
||
| 292 | add_meta_box( |
||
| 293 | $this->id, |
||
| 294 | $this->title, |
||
| 295 | array( $this, 'render' ), |
||
| 296 | $post_type, |
||
| 297 | $this->settings['panel_context'], |
||
| 298 | $this->settings['panel_priority'] |
||
| 299 | ); |
||
| 300 | } |
||
| 301 | |||
| 302 | foreach ( $this->settings['post_type'] as $post_type ) { |
||
| 303 | add_filter( "postbox_classes_{$post_type}_{$this->id}", array( $this, 'postbox_classes' ) ); |
||
| 304 | } |
||
| 305 | } |
||
| 306 | |||
| 307 | /** |
||
| 308 | * Classes to add to the post meta box |
||
| 309 | */ |
||
| 310 | public function postbox_classes( $classes ) { |
||
| 314 | |||
| 315 | /** |
||
| 316 | * Perform checks whether the container should be attached during the current request |
||
| 317 | * |
||
| 318 | * @return bool True if the container is allowed to be attached |
||
| 319 | **/ |
||
| 320 | public function is_valid_attach() { |
||
| 367 | |||
| 368 | /** |
||
| 369 | * Revert the result of attach() |
||
| 370 | **/ |
||
| 371 | View Code Duplication | public function detach() { |
|
| 382 | |||
| 383 | /** |
||
| 384 | * Output the container markup |
||
| 385 | **/ |
||
| 386 | public function render() { |
||
| 389 | |||
| 390 | /** |
||
| 391 | * Set the post ID the container will operate with. |
||
| 392 | * |
||
| 393 | * @param int $post_id |
||
| 394 | **/ |
||
| 395 | public function set_post_id( $post_id ) { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Perform checks whether there is a field registered with the name $name. |
||
| 402 | * If not, the field name is recorded. |
||
| 403 | * |
||
| 404 | * @param string $name |
||
| 405 | **/ |
||
| 406 | View Code Duplication | public function verify_unique_field_name( $name ) { |
|
| 423 | |||
| 424 | /** |
||
| 425 | * Remove field name $name from the list of unique field names |
||
| 426 | * |
||
| 427 | * @param string $name |
||
| 428 | **/ |
||
| 429 | View Code Duplication | public function drop_unique_field_name( $name ) { |
|
| 437 | |||
| 438 | /** |
||
| 439 | * Show the container only on pages whose parent is referenced by $parent_page_path. |
||
| 440 | * |
||
| 441 | * @param string $parent_page_path |
||
| 442 | * @return object $this |
||
| 443 | **/ |
||
| 444 | 1 | public function show_on_page_children( $parent_page_path ) { |
|
| 456 | |||
| 457 | /** |
||
| 458 | * Show the container only on particular page referenced by it's path. |
||
| 459 | * |
||
| 460 | * @param int|string $page page ID or page path |
||
| 461 | * @return object $this |
||
| 462 | **/ |
||
| 463 | 2 | public function show_on_page( $page ) { |
|
| 479 | |||
| 480 | /** |
||
| 481 | * Show the container only on posts from the specified category. |
||
| 482 | * |
||
| 483 | * @see show_on_taxonomy_term() |
||
| 484 | * |
||
| 485 | * @param string $category_slug |
||
| 486 | * @return object $this |
||
| 487 | **/ |
||
| 488 | public function show_on_category( $category_slug ) { |
||
| 493 | |||
| 494 | /** |
||
| 495 | * Show the container only on pages whose template has filename $template_path. |
||
| 496 | * |
||
| 497 | * @param string|array $template_path |
||
| 498 | * @return object $this |
||
| 499 | **/ |
||
| 500 | 2 | public function show_on_template( $template_path ) { |
|
| 515 | |||
| 516 | /** |
||
| 517 | * Hide the container from pages whose template has filename $template_path. |
||
| 518 | * |
||
| 519 | * @param string|array $template_path |
||
| 520 | * @return object $this |
||
| 521 | **/ |
||
| 522 | public function hide_on_template( $template_path ) { |
||
| 534 | |||
| 535 | /** |
||
| 536 | * Show the container only on hierarchical posts of level $level. |
||
| 537 | * Levels start from 1 (top level post) |
||
| 538 | * |
||
| 539 | * @param int $level |
||
| 540 | * @return object $this |
||
| 541 | **/ |
||
| 542 | public function show_on_level( $level ) { |
||
| 551 | |||
| 552 | /** |
||
| 553 | * Show the container only on posts which have term $term_slug from the $taxonomy_slug taxonomy. |
||
| 554 | * |
||
| 555 | * @param string $taxonomy_slug |
||
| 556 | * @param string $term_slug |
||
| 557 | * @return object $this |
||
| 558 | **/ |
||
| 559 | public function show_on_taxonomy_term( $term_slug, $taxonomy_slug ) { |
||
| 568 | |||
| 569 | /** |
||
| 570 | * Show the container only on posts from the specified format. |
||
| 571 | * Learn more about {@link http://codex.wordpress.org/Post_Formats Post Formats (Codex)} |
||
| 572 | * |
||
| 573 | * @param string|array $post_format Name of the format as listed on Codex |
||
| 574 | * @return object $this |
||
| 575 | **/ |
||
| 576 | public function show_on_post_format( $post_format ) { |
||
| 592 | |||
| 593 | /** |
||
| 594 | * Show the container only on posts from the specified type(s). |
||
| 595 | * |
||
| 596 | * @param string|array $post_types |
||
| 597 | * @return object $this |
||
| 598 | **/ |
||
| 599 | public function show_on_post_type( $post_types ) { |
||
| 606 | |||
| 607 | /** |
||
| 608 | * Sets the meta box container context |
||
| 609 | * |
||
| 610 | * @see https://codex.wordpress.org/Function_Reference/add_meta_box |
||
| 611 | * @param string $context ('normal', 'advanced' or 'side') |
||
| 612 | */ |
||
| 613 | public function set_context( $context ) { |
||
| 618 | |||
| 619 | /** |
||
| 620 | * Sets the meta box container priority |
||
| 621 | * |
||
| 622 | * @see https://codex.wordpress.org/Function_Reference/add_meta_box |
||
| 623 | * @param string $priority ('high', 'core', 'default' or 'low') |
||
| 624 | */ |
||
| 625 | public function set_priority( $priority ) { |
||
| 630 | } // END Post_Meta_Container |
||
| 631 |