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 |
||
| 13 | class Post_Meta_Container extends Container { |
||
| 14 | /** |
||
| 15 | * ID of the post the container is working with |
||
| 16 | * |
||
| 17 | * @see init() |
||
| 18 | * @var int |
||
| 19 | */ |
||
| 20 | protected $post_id; |
||
| 21 | |||
| 22 | /** |
||
| 23 | * List of default container settings |
||
| 24 | * |
||
| 25 | * @see init() |
||
| 26 | * @var array |
||
| 27 | */ |
||
| 28 | public $settings = array( |
||
| 29 | 'post_type' => array( 'post' ), |
||
| 30 | 'panel_context' => 'normal', |
||
| 31 | 'panel_priority' => 'high', |
||
| 32 | 'show_on' => array( |
||
| 33 | 'category' => null, |
||
| 34 | 'template_names' => array(), |
||
| 35 | 'not_in_template_names' => array(), |
||
| 36 | 'post_formats' => array(), |
||
| 37 | 'level_limit' => null, |
||
| 38 | 'tax_term_id' => null, |
||
| 39 | 'page_id' => null, |
||
| 40 | 'parent_page_id' => null, |
||
| 41 | 'post_path' => null, |
||
| 42 | ), |
||
| 43 | ); |
||
| 44 | |||
| 45 | /** |
||
| 46 | * Create a new container |
||
| 47 | * |
||
| 48 | * @param string $unique_id Unique id of the container |
||
| 49 | * @param string $title title of the container |
||
| 50 | * @param string $type Type of the container |
||
| 51 | **/ |
||
| 52 | public function __construct( $unique_id, $title, $type ) { |
||
| 53 | parent::__construct( $unique_id, $title, $type ); |
||
| 54 | |||
| 55 | if ( ! $this->get_datastore() ) { |
||
| 56 | $this->set_datastore( Datastore::make( 'post_meta' ), $this->has_default_datastore() ); |
||
| 57 | } |
||
| 58 | } |
||
| 59 | |||
| 60 | /** |
||
| 61 | * Create DataStore instance, set post ID to operate with (if such exists). |
||
| 62 | * Bind attach() and save() to the appropriate WordPress actions. |
||
| 63 | **/ |
||
| 64 | public function init() { |
||
| 82 | |||
| 83 | /** |
||
| 84 | * Perform checks whether the current save() request is valid |
||
| 85 | * Possible errors are triggering save() for autosave requests |
||
| 86 | * or performing post save outside of the post edit page (like Quick Edit) |
||
| 87 | * |
||
| 88 | * @param int $post_id ID of the post against which save() is ran |
||
| 89 | * @return bool |
||
| 90 | **/ |
||
| 91 | public function is_valid_save( $post_id = 0 ) { |
||
| 102 | |||
| 103 | /** |
||
| 104 | * Perform save operation after successful is_valid_save() check. |
||
| 105 | * The call is propagated to all fields in the container. |
||
| 106 | * |
||
| 107 | * @param int $post_id ID of the post against which save() is ran |
||
| 108 | **/ |
||
| 109 | View Code Duplication | public function save( $post_id = null ) { |
|
| 123 | |||
| 124 | /** |
||
| 125 | * Perform checks whether the container should be attached during the current request |
||
| 126 | * |
||
| 127 | * @return bool True if the container is allowed to be attached |
||
| 128 | **/ |
||
| 129 | public function is_valid_attach_for_request() { |
||
| 168 | |||
| 169 | /** |
||
| 170 | * Check container attachment rules against object id |
||
| 171 | * |
||
| 172 | * @param int $object_id |
||
| 173 | * @return bool |
||
| 174 | **/ |
||
| 175 | public function is_valid_attach_for_object( $object_id = null ) { |
||
| 200 | |||
| 201 | /** |
||
| 202 | * Add meta box for each of the container post types |
||
| 203 | **/ |
||
| 204 | public function attach() { |
||
| 220 | |||
| 221 | /** |
||
| 222 | * Classes to add to the post meta box |
||
| 223 | */ |
||
| 224 | public function add_postbox_classes( $classes ) { |
||
| 228 | |||
| 229 | /** |
||
| 230 | * Output the container markup |
||
| 231 | **/ |
||
| 232 | public function render() { |
||
| 235 | |||
| 236 | /** |
||
| 237 | * Set the post ID the container will operate with. |
||
| 238 | * |
||
| 239 | * @param int $post_id |
||
| 240 | **/ |
||
| 241 | public function set_post_id( $post_id ) { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * CONDITION TOOLS |
||
| 248 | */ |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Check a condition against all supported conditions |
||
| 252 | * |
||
| 253 | * @param string $condition |
||
| 254 | * @param mixed $value |
||
| 255 | * @param WP_Post $post |
||
| 256 | * @return bool |
||
| 257 | */ |
||
| 258 | protected function is_condition_fullfilled( $condition, $value, $post ) { |
||
| 320 | |||
| 321 | /** |
||
| 322 | * Check if a post is of a given post format |
||
| 323 | * |
||
| 324 | * @param WP_Post $post |
||
| 325 | * @param string $format |
||
| 326 | * @return bool |
||
| 327 | */ |
||
| 328 | protected function condition_is_post_of_format( $post, $format ) { |
||
| 340 | |||
| 341 | /** |
||
| 342 | * Check if a post is on a specific level in it's hierarchy |
||
| 343 | * |
||
| 344 | * @param WP_Post $post |
||
| 345 | * @param int $level |
||
| 346 | * @return bool |
||
| 347 | */ |
||
| 348 | protected function condition_is_post_on_level( $post, $level ) { |
||
| 353 | |||
| 354 | /** |
||
| 355 | * Check if a post uses one of a given array of templates |
||
| 356 | * |
||
| 357 | * @param WP_Post $post |
||
| 358 | * @param string|array<string> $templates |
||
| 359 | * @return bool |
||
| 360 | */ |
||
| 361 | protected function condition_is_post_using_template( $post, $templates ) { |
||
| 366 | |||
| 367 | /** |
||
| 368 | * COMMON USAGE METHODS |
||
| 369 | */ |
||
| 370 | |||
| 371 | /** |
||
| 372 | * Show the container only on particular page referenced by it's path. |
||
| 373 | * |
||
| 374 | * @param int|string $page page ID or page path |
||
| 375 | * @return object $this |
||
| 376 | **/ |
||
| 377 | 4 | public function show_on_page( $page ) { |
|
| 396 | |||
| 397 | /** |
||
| 398 | * Show the container only on pages whose parent is referenced by $parent_page_path. |
||
| 399 | * |
||
| 400 | * @param string $parent_page_path |
||
| 401 | * @return object $this |
||
| 402 | **/ |
||
| 403 | 1 | public function show_on_page_children( $parent_page_path ) { |
|
| 416 | |||
| 417 | /** |
||
| 418 | * Show the container only on posts from the specified category. |
||
| 419 | * |
||
| 420 | * @see show_on_taxonomy_term() |
||
| 421 | * |
||
| 422 | * @param string $category_slug |
||
| 423 | * @return object $this |
||
| 424 | **/ |
||
| 425 | public function show_on_category( $category_slug ) { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Show the container only on pages whose template has filename $template_path. |
||
| 433 | * |
||
| 434 | * @param string|array $template_path |
||
| 435 | * @return object $this |
||
| 436 | **/ |
||
| 437 | 2 | public function show_on_template( $template_path ) { |
|
| 455 | |||
| 456 | /** |
||
| 457 | * Hide the container from pages whose template has filename $template_path. |
||
| 458 | * |
||
| 459 | * @param string|array $template_path |
||
| 460 | * @return object $this |
||
| 461 | **/ |
||
| 462 | public function hide_on_template( $template_path ) { |
||
| 474 | |||
| 475 | /** |
||
| 476 | * Show the container only on hierarchical posts of level $level. |
||
| 477 | * Levels start from 1 (top level post) |
||
| 478 | * |
||
| 479 | * @param int $level |
||
| 480 | * @return object $this |
||
| 481 | **/ |
||
| 482 | public function show_on_level( $level ) { |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Show the container only on posts which have term $term_slug from the $taxonomy_slug taxonomy. |
||
| 494 | * |
||
| 495 | * @param string $taxonomy_slug |
||
| 496 | * @param string $term_slug |
||
| 497 | * @return object $this |
||
| 498 | **/ |
||
| 499 | public function show_on_taxonomy_term( $term_slug, $taxonomy_slug ) { |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Show the container only on posts from the specified format. |
||
| 511 | * Learn more about {@link http://codex.wordpress.org/Post_Formats Post Formats (Codex)} |
||
| 512 | * |
||
| 513 | * @param string|array $post_format Name of the format as listed on Codex |
||
| 514 | * @return object $this |
||
| 515 | **/ |
||
| 516 | public function show_on_post_format( $post_format ) { |
||
| 532 | |||
| 533 | /** |
||
| 534 | * Show the container only on posts from the specified type(s). |
||
| 535 | * |
||
| 536 | * @param string|array $post_types |
||
| 537 | * @return object $this |
||
| 538 | **/ |
||
| 539 | public function show_on_post_type( $post_types ) { |
||
| 546 | |||
| 547 | /** |
||
| 548 | * Sets the meta box container context |
||
| 549 | * |
||
| 550 | * @see https://codex.wordpress.org/Function_Reference/add_meta_box |
||
| 551 | * @param string $context ('normal', 'advanced' or 'side') |
||
| 552 | */ |
||
| 553 | public function set_context( $context ) { |
||
| 558 | |||
| 559 | /** |
||
| 560 | * Sets the meta box container priority |
||
| 561 | * |
||
| 562 | * @see https://codex.wordpress.org/Function_Reference/add_meta_box |
||
| 563 | * @param string $priority ('high', 'core', 'default' or 'low') |
||
| 564 | */ |
||
| 565 | public function set_priority( $priority ) { |
||
| 570 | } |
||
| 571 |