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() { |
||
80 | |||
81 | // Fetch the context we set in our call. |
||
82 | $context = $this->cmb->prop( 'context' ) ? $this->cmb->prop( 'context' ) : 'normal'; |
||
83 | |||
84 | // Call the proper hook based on the context provided. |
||
85 | switch ( $context ) { |
||
86 | |||
87 | case 'form_top': |
||
88 | add_action( 'edit_form_top', array( $this, 'add_context_metaboxes' ) ); |
||
89 | break; |
||
90 | |||
91 | case 'before_permalink': |
||
92 | add_action( 'edit_form_before_permalink', array( $this, 'add_context_metaboxes' ) ); |
||
93 | break; |
||
94 | |||
95 | case 'after_title': |
||
96 | add_action( 'edit_form_after_title', array( $this, 'add_context_metaboxes' ) ); |
||
97 | break; |
||
98 | |||
99 | case 'after_editor': |
||
100 | add_action( 'edit_form_after_editor', array( $this, 'add_context_metaboxes' ) ); |
||
101 | break; |
||
102 | |||
103 | default: |
||
104 | add_action( 'add_meta_boxes', array( $this, 'add_metaboxes' ) ); |
||
105 | } |
||
106 | |||
107 | add_action( 'add_attachment', array( $this, 'save_post' ) ); |
||
108 | add_action( 'edit_attachment', array( $this, 'save_post' ) ); |
||
109 | add_action( 'save_post', array( $this, 'save_post' ), 10, 2 ); |
||
110 | |||
111 | if ( $this->cmb->has_columns ) { |
||
112 | foreach ( $this->cmb->prop( 'object_types' ) as $post_type ) { |
||
113 | add_filter( "manage_{$post_type}_posts_columns", array( $this, 'register_column_headers' ) ); |
||
114 | add_action( "manage_{$post_type}_posts_custom_column", array( $this, 'column_display' ), 10, 2 ); |
||
115 | } |
||
116 | } |
||
117 | } |
||
118 | |||
119 | public function comment_hooks() { |
||
120 | add_action( 'add_meta_boxes_comment', array( $this, 'add_metaboxes' ) ); |
||
121 | add_action( 'edit_comment', array( $this, 'save_comment' ) ); |
||
122 | |||
123 | if ( $this->cmb->has_columns ) { |
||
124 | add_filter( 'manage_edit-comments_columns', array( $this, 'register_column_headers' ) ); |
||
125 | add_action( 'manage_comments_custom_column', array( $this, 'column_display' ), 10, 3 ); |
||
126 | } |
||
127 | } |
||
128 | |||
129 | public function user_hooks() { |
||
130 | $priority = $this->get_priority(); |
||
131 | |||
132 | add_action( 'show_user_profile', array( $this, 'user_metabox' ), $priority ); |
||
133 | add_action( 'edit_user_profile', array( $this, 'user_metabox' ), $priority ); |
||
134 | add_action( 'user_new_form', array( $this, 'user_new_metabox' ), $priority ); |
||
135 | |||
136 | add_action( 'personal_options_update', array( $this, 'save_user' ) ); |
||
137 | add_action( 'edit_user_profile_update', array( $this, 'save_user' ) ); |
||
138 | add_action( 'user_register', array( $this, 'save_user' ) ); |
||
139 | |||
140 | View Code Duplication | if ( $this->cmb->has_columns ) { |
|
141 | add_filter( 'manage_users_columns', array( $this, 'register_column_headers' ) ); |
||
142 | add_filter( 'manage_users_custom_column', array( $this, 'return_column_display' ), 10, 3 ); |
||
143 | } |
||
144 | } |
||
145 | |||
146 | public function term_hooks() { |
||
147 | if ( ! function_exists( 'get_term_meta' ) ) { |
||
148 | wp_die( esc_html__( 'Term Metadata is a WordPress 4.4+ feature. Please upgrade your WordPress install.', 'cmb2' ) ); |
||
149 | } |
||
150 | |||
151 | if ( ! $this->cmb->prop( 'taxonomies' ) ) { |
||
152 | wp_die( esc_html__( 'Term metaboxes configuration requires a "taxonomies" parameter.', 'cmb2' ) ); |
||
153 | } |
||
154 | |||
155 | $this->taxonomies = (array) $this->cmb->prop( 'taxonomies' ); |
||
156 | $show_on_term_add = $this->cmb->prop( 'new_term_section' ); |
||
157 | $priority = $this->get_priority( 8 ); |
||
158 | |||
159 | foreach ( $this->taxonomies as $taxonomy ) { |
||
160 | // Display our form data |
||
161 | add_action( "{$taxonomy}_edit_form", array( $this, 'term_metabox' ), $priority, 2 ); |
||
162 | |||
163 | $show_on_add = is_array( $show_on_term_add ) |
||
164 | ? in_array( $taxonomy, $show_on_term_add ) |
||
165 | : (bool) $show_on_term_add; |
||
166 | |||
167 | $show_on_add = apply_filters( "cmb2_show_on_term_add_form_{$this->cmb->cmb_id}", $show_on_add, $this->cmb ); |
||
168 | |||
169 | // Display form in add-new section (unless specified not to) |
||
170 | if ( $show_on_add ) { |
||
171 | add_action( "{$taxonomy}_add_form_fields", array( $this, 'term_metabox' ), $priority, 2 ); |
||
172 | } |
||
173 | |||
174 | View Code Duplication | if ( $this->cmb->has_columns ) { |
|
175 | add_filter( "manage_edit-{$taxonomy}_columns", array( $this, 'register_column_headers' ) ); |
||
176 | add_filter( "manage_{$taxonomy}_custom_column", array( $this, 'return_column_display' ), 10, 3 ); |
||
177 | } |
||
178 | } |
||
179 | |||
180 | add_action( 'created_term', array( $this, 'save_term' ), 10, 3 ); |
||
181 | add_action( 'edited_terms', array( $this, 'save_term' ), 10, 2 ); |
||
182 | add_action( 'delete_term', array( $this, 'delete_term' ), 10, 3 ); |
||
183 | |||
184 | } |
||
185 | |||
186 | /** |
||
187 | * Registers styles for CMB2 |
||
188 | * @since 2.0.7 |
||
189 | */ |
||
190 | 1 | protected static function register_styles() { |
|
191 | 1 | if ( self::$css_registration_done ) { |
|
192 | return; |
||
193 | } |
||
194 | |||
195 | // Only use minified files if SCRIPT_DEBUG is off |
||
196 | 1 | $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
|
197 | 1 | $front = is_admin() ? '' : '-front'; |
|
198 | 1 | $rtl = is_rtl() ? '-rtl' : ''; |
|
199 | |||
200 | // Filter required styles and register stylesheet |
||
201 | 1 | $dependencies = apply_filters( 'cmb2_style_dependencies', array() ); |
|
202 | 1 | wp_register_style( 'cmb2-styles', CMB2_Utils::url( "css/cmb2{$front}{$rtl}{$min}.css" ), $dependencies ); |
|
203 | 1 | wp_register_style( 'cmb2-display-styles', CMB2_Utils::url( "css/cmb2-display{$rtl}{$min}.css" ), $dependencies ); |
|
204 | |||
205 | 1 | self::$css_registration_done = true; |
|
206 | 1 | } |
|
207 | |||
208 | /** |
||
209 | * Registers scripts for CMB2 |
||
210 | * @since 2.0.7 |
||
211 | */ |
||
212 | 1 | protected static function register_js() { |
|
213 | 1 | if ( self::$js_registration_done ) { |
|
214 | return; |
||
215 | } |
||
216 | |||
217 | 1 | $hook = is_admin() ? 'admin_footer' : 'wp_footer'; |
|
218 | 1 | add_action( $hook, array( 'CMB2_JS', 'enqueue' ), 8 ); |
|
219 | |||
220 | 1 | self::$js_registration_done = true; |
|
221 | 1 | } |
|
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 ) { |
||
237 | $hooks = array( |
||
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 box/fields in an alternate context (not in a standard metabox area). |
||
324 | * @since 2.2.4 |
||
325 | */ |
||
326 | public function add_context_metaboxes() { |
||
345 | |||
346 | /** |
||
347 | * Output the CMB2 box/fields in an alternate context (not in a standard metabox area). |
||
348 | * @since 2.2.4 |
||
349 | */ |
||
350 | public function output_context_metabox() { |
||
372 | |||
373 | /** |
||
374 | * Output the opening markup for a context box. |
||
375 | * @since 2.2.4 |
||
376 | * @param $add_handle Whether to add the metabox handle and opening div for .inside |
||
377 | */ |
||
378 | public function context_box_title_markup_open( $add_handle = true ) { |
||
397 | |||
398 | /** |
||
399 | * Output the closing markup for a context box. |
||
400 | * @since 2.2.4 |
||
401 | * @param $add_inside_close Whether to add closing div for .inside. |
||
402 | */ |
||
403 | public function context_box_title_markup_close( $add_inside_close = true ) { |
||
412 | |||
413 | /** |
||
414 | * Add metaboxes (to 'post' or 'comment' object types) |
||
415 | * @since 1.0.0 |
||
416 | */ |
||
417 | public function add_metaboxes() { |
||
447 | |||
448 | /** |
||
449 | * Remove the specified default taxonomy metaboxes for a post-type. |
||
450 | * @since 2.2.3 |
||
451 | * @param string $post_type Post type to remove the metabox for. |
||
452 | */ |
||
453 | protected function remove_default_tax_metaboxes( $post_type ) { |
||
463 | |||
464 | /** |
||
465 | * Modify metabox postbox classes. |
||
466 | * @since 2.2.4 |
||
467 | * @param array $classes Array of classes |
||
468 | * @return array Modified array of classes |
||
469 | */ |
||
470 | public function postbox_classes( $classes ) { |
||
483 | |||
484 | /** |
||
485 | * Modify metabox altnernate context postbox classes. |
||
486 | * @since 2.2.4 |
||
487 | * @param array $classes Array of classes |
||
488 | * @return array Modified array of classes |
||
489 | */ |
||
490 | protected function alternate_context_postbox_classes( $classes ) { |
||
508 | |||
509 | /** |
||
510 | * Display metaboxes for a post or comment object |
||
511 | * @since 1.0.0 |
||
512 | */ |
||
513 | public function metabox_callback() { |
||
517 | |||
518 | /** |
||
519 | * Display metaboxes for new user page |
||
520 | * @since 1.0.0 |
||
521 | */ |
||
522 | public function user_new_metabox( $section ) { |
||
529 | |||
530 | /** |
||
531 | * Display metaboxes for a user object |
||
532 | * @since 1.0.0 |
||
533 | */ |
||
534 | public function user_metabox() { |
||
537 | |||
538 | /** |
||
539 | * Display metaboxes for a taxonomy term object |
||
540 | * @since 2.2.0 |
||
541 | */ |
||
542 | public function term_metabox() { |
||
545 | |||
546 | /** |
||
547 | * Display metaboxes for an object type |
||
548 | * @since 2.2.0 |
||
549 | * @param string $type Object type |
||
550 | * @return void |
||
551 | */ |
||
552 | public function show_form_for_type( $type ) { |
||
570 | |||
571 | /** |
||
572 | * Determines if metabox should be shown in current context |
||
573 | * @since 2.0.0 |
||
574 | * @return bool Whether metabox should be added/shown |
||
575 | */ |
||
576 | public function show_on() { |
||
591 | |||
592 | /** |
||
593 | * Get the CMB priority property set to numeric hook priority. |
||
594 | * @since 2.2.0 |
||
595 | * @param integer $default Default display hook priority. |
||
596 | * @return integer Hook priority. |
||
597 | */ |
||
598 | public function get_priority( $default = 10 ) { |
||
620 | |||
621 | /** |
||
622 | * Save data from post metabox |
||
623 | * @since 1.0.0 |
||
624 | * @param int $post_id Post ID |
||
625 | * @param mixed $post Post object |
||
626 | * @return null |
||
627 | */ |
||
628 | public function save_post( $post_id, $post = false ) { |
||
647 | |||
648 | /** |
||
649 | * Save data from comment metabox |
||
650 | * @since 2.0.9 |
||
651 | * @param int $comment_id Comment ID |
||
652 | * @return null |
||
653 | */ |
||
654 | public function save_comment( $comment_id ) { |
||
662 | |||
663 | /** |
||
664 | * Save data from user fields |
||
665 | * @since 1.0.x |
||
666 | * @param int $user_id User ID |
||
667 | * @return null |
||
668 | */ |
||
669 | public function save_user( $user_id ) { |
||
675 | |||
676 | /** |
||
677 | * Save data from term fields |
||
678 | * @since 2.2.0 |
||
679 | * @param int $term_id Term ID |
||
680 | * @param int $tt_id Term Taxonomy ID |
||
681 | * @param string $taxonomy Taxonomy |
||
682 | * @return null |
||
683 | */ |
||
684 | public function save_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
692 | |||
693 | /** |
||
694 | * Delete term meta when a term is deleted. |
||
695 | * @since 2.2.0 |
||
696 | * @param int $term_id Term ID |
||
697 | * @param int $tt_id Term Taxonomy ID |
||
698 | * @param string $taxonomy Taxonomy |
||
699 | * @return null |
||
700 | */ |
||
701 | public function delete_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
712 | |||
713 | /** |
||
714 | * Determines if the current object is able to be saved |
||
715 | * @since 2.0.9 |
||
716 | * @param string $type Current post_type or comment_type |
||
717 | * @return bool Whether object can be saved |
||
718 | */ |
||
719 | public function can_save( $type = '' ) { |
||
733 | |||
734 | /** |
||
735 | * Determine if taxonomy of term being modified is cmb2-editable. |
||
736 | * @since 2.2.0 |
||
737 | * @param string $taxonomy Taxonomy of term being modified. |
||
738 | * @return bool Whether taxonomy is editable. |
||
739 | */ |
||
740 | public function taxonomy_can_save( $taxonomy ) { |
||
753 | |||
754 | /** |
||
755 | * Enqueues the 'cmb2-display-styles' if the conditions match (has columns, on the right page, etc). |
||
756 | * @since 2.2.2.1 |
||
757 | */ |
||
758 | protected function maybe_enqueue_column_display_styles() { |
||
769 | |||
770 | /** |
||
771 | * Includes CMB2 styles |
||
772 | * @since 2.0.0 |
||
773 | */ |
||
774 | 1 | public static function enqueue_cmb_css( $handle = 'cmb2-styles' ) { |
|
787 | |||
788 | /** |
||
789 | * Includes CMB2 JS |
||
790 | * @since 2.0.0 |
||
791 | */ |
||
792 | 1 | public static function enqueue_cmb_js() { |
|
800 | |||
801 | } |
||
802 |
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.