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 | * ID of the post the container is working with |
||
17 | * |
||
18 | * @see init() |
||
19 | * @var int |
||
20 | */ |
||
21 | protected $post_id; |
||
22 | |||
23 | /** |
||
24 | * List of default container settings |
||
25 | * |
||
26 | * @see init() |
||
27 | * @var array |
||
28 | */ |
||
29 | public $settings = array( |
||
30 | 'post_type' => array( 'post' ), |
||
31 | 'panel_context' => 'normal', |
||
32 | 'panel_priority' => 'high', |
||
33 | 'show_on' => array( |
||
34 | 'category' => null, |
||
35 | 'template_names' => array(), |
||
36 | 'not_in_template_names' => array(), |
||
37 | 'post_formats' => array(), |
||
38 | 'level_limit' => null, |
||
39 | 'tax_term_id' => null, |
||
40 | 'page_id' => null, |
||
41 | 'parent_page_id' => null, |
||
42 | 'post_path' => null, |
||
43 | ), |
||
44 | ); |
||
45 | |||
46 | /** |
||
47 | * Create a new post meta fields container |
||
48 | * |
||
49 | * @param string $title Unique title of the container |
||
50 | **/ |
||
51 | public function __construct( $title ) { |
||
58 | |||
59 | /** |
||
60 | * Check if all required container settings have been specified |
||
61 | * |
||
62 | * @param array $settings Container settings |
||
63 | **/ |
||
64 | public function check_setup_settings( &$settings = array() ) { |
||
65 | if ( isset( $settings['show_on'] ) ) { |
||
66 | $invalid_settings = array_diff_key( $settings['show_on'], $this->settings['show_on'] ); |
||
67 | if ( ! empty( $invalid_settings ) ) { |
||
68 | Incorrect_Syntax_Exception::raise( 'Invalid show_on settings supplied to setup(): "' . implode( '", "', array_keys( $invalid_settings ) ) . '"' ); |
||
69 | } |
||
70 | } |
||
71 | |||
72 | if ( isset( $settings['show_on']['post_formats'] ) ) { |
||
73 | $settings['show_on']['post_formats'] = (array) $settings['show_on']['post_formats']; |
||
74 | } |
||
75 | |||
76 | if ( isset( $settings['show_on']['post_path'] ) ) { |
||
77 | $page = get_page_by_path( $settings['show_on']['post_path'] ); |
||
78 | |||
79 | if ( $page ) { |
||
80 | $settings['show_on']['page_id'] = $page->ID; |
||
81 | } else { |
||
82 | $settings['show_on']['page_id'] = -1; |
||
83 | } |
||
84 | } |
||
85 | |||
86 | // Transform category slug to taxonomy + term slug + term id |
||
87 | if ( isset( $settings['show_on']['category'] ) ) { |
||
88 | $term = get_term_by( 'slug', $settings['show_on']['category'], 'category' ); |
||
89 | |||
90 | if ( $term ) { |
||
91 | $settings['show_on']['tax_slug'] = $term->taxonomy; |
||
92 | $settings['show_on']['tax_term'] = $term->slug; |
||
93 | $settings['show_on']['tax_term_id'] = $term->term_id; |
||
94 | } |
||
95 | } |
||
96 | |||
97 | return parent::check_setup_settings( $settings ); |
||
98 | } |
||
99 | |||
100 | /** |
||
101 | * Create DataStore instance, set post ID to operate with (if such exists). |
||
102 | * Bind attach() and save() to the appropriate WordPress actions. |
||
103 | **/ |
||
104 | public function init() { |
||
121 | |||
122 | /** |
||
123 | * Perform save operation after successful is_valid_save() check. |
||
124 | * The call is propagated to all fields in the container. |
||
125 | * |
||
126 | * @param int $post_id ID of the post against which save() is ran |
||
127 | **/ |
||
128 | View Code Duplication | public function save( $post_id ) { |
|
142 | |||
143 | /** |
||
144 | * Perform checks whether the current save() request is valid |
||
145 | * Possible errors are triggering save() for autosave requests |
||
146 | * or performing post save outside of the post edit page (like Quick Edit) |
||
147 | * |
||
148 | * @see is_valid_save_conditions() |
||
149 | * @param int $post_id ID of the post against which save() is ran |
||
150 | * @return bool |
||
151 | **/ |
||
152 | public function is_valid_save( $post_id = 0 ) { |
||
163 | |||
164 | /** |
||
165 | * Perform checks whether the current save() request is valid |
||
166 | * Possible errors are triggering save() for autosave requests |
||
167 | * or performing post save outside of the post edit page (like Quick Edit) |
||
168 | * |
||
169 | * @param int $post_id ID of the post against which save() is ran |
||
170 | * @return bool |
||
171 | **/ |
||
172 | public function is_valid_save_conditions( $post_id ) { |
||
278 | |||
279 | /** |
||
280 | * Add meta box for each of the container post types |
||
281 | **/ |
||
282 | public function attach() { |
||
298 | |||
299 | /** |
||
300 | * Classes to add to the post meta box |
||
301 | */ |
||
302 | public function postbox_classes( $classes ) { |
||
306 | |||
307 | /** |
||
308 | * Perform checks whether the container should be attached during the current request |
||
309 | * |
||
310 | * @return bool True if the container is allowed to be attached |
||
311 | **/ |
||
312 | public function is_valid_attach() { |
||
359 | |||
360 | /** |
||
361 | * Revert the result of attach() |
||
362 | **/ |
||
363 | View Code Duplication | public function detach() { |
|
374 | |||
375 | /** |
||
376 | * Output the container markup |
||
377 | **/ |
||
378 | public function render() { |
||
381 | |||
382 | /** |
||
383 | * Set the post ID the container will operate with. |
||
384 | * |
||
385 | * @param int $post_id |
||
386 | **/ |
||
387 | public function set_post_id( $post_id ) { |
||
391 | |||
392 | /** |
||
393 | * Show the container only on pages whose parent is referenced by $parent_page_path. |
||
394 | * |
||
395 | * @param string $parent_page_path |
||
396 | * @return object $this |
||
397 | **/ |
||
398 | 1 | public function show_on_page_children( $parent_page_path ) { |
|
411 | |||
412 | /** |
||
413 | * Show the container only on particular page referenced by it's path. |
||
414 | * |
||
415 | * @param int|string $page page ID or page path |
||
416 | * @return object $this |
||
417 | **/ |
||
418 | 4 | public function show_on_page( $page ) { |
|
419 | 4 | $page_id = absint( $page ); |
|
420 | |||
421 | 4 | if ( $page_id && $page_id == $page ) { |
|
422 | 2 | $page_obj = get_post( $page_id ); |
|
423 | 2 | } else { |
|
424 | 2 | $page_obj = get_page_by_path( $page ); |
|
425 | } |
||
426 | |||
427 | 4 | $this->show_on_post_type( 'page' ); |
|
428 | |||
429 | 4 | if ( $page_obj ) { |
|
430 | 4 | $this->settings['show_on']['page_id'] = $page_obj->ID; |
|
431 | 4 | } else { |
|
432 | $this->settings['show_on']['page_id'] = -1; |
||
433 | } |
||
434 | |||
435 | 4 | return $this; |
|
436 | } |
||
437 | |||
438 | /** |
||
439 | * Show the container only on posts from the specified category. |
||
440 | * |
||
441 | * @see show_on_taxonomy_term() |
||
442 | * |
||
443 | * @param string $category_slug |
||
444 | * @return object $this |
||
445 | **/ |
||
446 | public function show_on_category( $category_slug ) { |
||
447 | $this->settings['show_on']['category'] = $category_slug; |
||
448 | |||
449 | return $this->show_on_taxonomy_term( $category_slug, 'category' ); |
||
450 | } |
||
451 | |||
452 | /** |
||
453 | * Show the container only on pages whose template has filename $template_path. |
||
454 | * |
||
455 | * @param string|array $template_path |
||
456 | * @return object $this |
||
457 | **/ |
||
458 | 2 | View Code Duplication | public function show_on_template( $template_path ) { |
459 | 2 | if ( is_array( $template_path ) ) { |
|
460 | 1 | foreach ( $template_path as $path ) { |
|
461 | 1 | $this->show_on_template( $path ); |
|
462 | 1 | } |
|
463 | |||
464 | 1 | return $this; |
|
465 | } |
||
466 | |||
467 | 2 | $this->settings['show_on']['template_names'][] = $template_path; |
|
468 | |||
469 | 2 | return $this; |
|
470 | } |
||
471 | |||
472 | /** |
||
473 | * Hide the container from pages whose template has filename $template_path. |
||
474 | * |
||
475 | * @param string|array $template_path |
||
476 | * @return object $this |
||
477 | **/ |
||
478 | View Code Duplication | public function hide_on_template( $template_path ) { |
|
479 | if ( is_array( $template_path ) ) { |
||
480 | foreach ( $template_path as $path ) { |
||
481 | $this->hide_on_template( $path ); |
||
482 | } |
||
483 | return $this; |
||
484 | } |
||
485 | |||
486 | $this->settings['show_on']['not_in_template_names'][] = $template_path; |
||
487 | |||
488 | return $this; |
||
489 | } |
||
490 | |||
491 | /** |
||
492 | * Show the container only on hierarchical posts of level $level. |
||
493 | * Levels start from 1 (top level post) |
||
494 | * |
||
495 | * @param int $level |
||
496 | * @return object $this |
||
497 | **/ |
||
498 | public function show_on_level( $level ) { |
||
507 | |||
508 | /** |
||
509 | * Show the container only on posts which have term $term_slug from the $taxonomy_slug taxonomy. |
||
510 | * |
||
511 | * @param string $taxonomy_slug |
||
512 | * @param string $term_slug |
||
513 | * @return object $this |
||
514 | **/ |
||
515 | public function show_on_taxonomy_term( $term_slug, $taxonomy_slug ) { |
||
524 | |||
525 | /** |
||
526 | * Show the container only on posts from the specified format. |
||
527 | * Learn more about {@link http://codex.wordpress.org/Post_Formats Post Formats (Codex)} |
||
528 | * |
||
529 | * @param string|array $post_format Name of the format as listed on Codex |
||
530 | * @return object $this |
||
531 | **/ |
||
532 | public function show_on_post_format( $post_format ) { |
||
548 | |||
549 | /** |
||
550 | * Show the container only on posts from the specified type(s). |
||
551 | * |
||
552 | * @param string|array $post_types |
||
553 | * @return object $this |
||
554 | **/ |
||
555 | public function show_on_post_type( $post_types ) { |
||
562 | |||
563 | /** |
||
564 | * Sets the meta box container context |
||
565 | * |
||
566 | * @see https://codex.wordpress.org/Function_Reference/add_meta_box |
||
567 | * @param string $context ('normal', 'advanced' or 'side') |
||
568 | */ |
||
569 | public function set_context( $context ) { |
||
574 | |||
575 | /** |
||
576 | * Sets the meta box container priority |
||
577 | * |
||
578 | * @see https://codex.wordpress.org/Function_Reference/add_meta_box |
||
579 | * @param string $priority ('high', 'core', 'default' or 'low') |
||
580 | */ |
||
581 | public function set_priority( $priority ) { |
||
586 | } // END Post_Meta_Container |
||
587 |