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 { |
||
|
|||
15 | |||
16 | /** |
||
17 | * Array of all hooks done (to be run once) |
||
18 | * @var array |
||
19 | * @since 2.0.0 |
||
20 | */ |
||
21 | protected static $hooks_completed = array(); |
||
22 | |||
23 | /** |
||
24 | * Only allow JS registration once |
||
25 | * @var bool |
||
26 | * @since 2.0.7 |
||
27 | */ |
||
28 | protected static $js_registration_done = false; |
||
29 | |||
30 | /** |
||
31 | * Only allow CSS registration once |
||
32 | * @var bool |
||
33 | * @since 2.0.7 |
||
34 | */ |
||
35 | protected static $css_registration_done = false; |
||
36 | |||
37 | /** |
||
38 | * @var CMB2 object |
||
39 | * @since 2.0.2 |
||
40 | */ |
||
41 | protected $cmb; |
||
42 | |||
43 | /** |
||
44 | * CMB taxonomies array for term meta |
||
45 | * @var array |
||
46 | * @since 2.2.0 |
||
47 | */ |
||
48 | protected $taxonomies = array(); |
||
49 | |||
50 | /** |
||
51 | * The object type we are performing the hookup for |
||
52 | * @var string |
||
53 | * @since 2.0.9 |
||
54 | */ |
||
55 | protected $object_type = 'post'; |
||
56 | |||
57 | public function __construct( CMB2 $cmb ) { |
||
58 | $this->cmb = $cmb; |
||
59 | $this->object_type = $this->cmb->mb_object_type(); |
||
60 | |||
61 | $this->universal_hooks(); |
||
62 | |||
63 | if ( is_admin() ) { |
||
64 | |||
65 | switch ( $this->object_type ) { |
||
66 | case 'post': |
||
67 | return $this->post_hooks(); |
||
68 | case 'comment': |
||
69 | return $this->comment_hooks(); |
||
70 | case 'user': |
||
71 | return $this->user_hooks(); |
||
72 | case 'term': |
||
73 | return $this->term_hooks(); |
||
74 | } |
||
75 | |||
76 | } |
||
77 | } |
||
78 | |||
79 | public function universal_hooks() { |
||
80 | // Handle oembed Ajax |
||
81 | $this->once( 'wp_ajax_cmb2_oembed_handler', array( cmb2_ajax(), 'oembed_handler' ) ); |
||
82 | $this->once( 'wp_ajax_nopriv_cmb2_oembed_handler', array( cmb2_ajax(), 'oembed_handler' ) ); |
||
83 | |||
84 | foreach ( get_class_methods( 'CMB2_Show_Filters' ) as $filter ) { |
||
85 | add_filter( 'cmb2_show_on', array( 'CMB2_Show_Filters', $filter ), 10, 3 ); |
||
86 | } |
||
87 | |||
88 | if ( is_admin() ) { |
||
89 | // register our scripts and styles for cmb |
||
90 | $this->once( 'admin_enqueue_scripts', array( __CLASS__, 'register_scripts' ), 8 ); |
||
91 | } |
||
92 | } |
||
93 | |||
94 | public function post_hooks() { |
||
95 | add_action( 'add_meta_boxes', array( $this, 'add_metaboxes' ) ); |
||
96 | add_action( 'add_attachment', array( $this, 'save_post' ) ); |
||
97 | add_action( 'edit_attachment', array( $this, 'save_post' ) ); |
||
98 | add_action( 'save_post', array( $this, 'save_post' ), 10, 2 ); |
||
99 | |||
100 | $this->once( 'admin_enqueue_scripts', array( $this, 'do_scripts' ) ); |
||
101 | } |
||
102 | |||
103 | public function comment_hooks() { |
||
104 | add_action( 'add_meta_boxes_comment', array( $this, 'add_metaboxes' ) ); |
||
105 | add_action( 'edit_comment', array( $this, 'save_comment' ) ); |
||
106 | |||
107 | $this->once( 'admin_enqueue_scripts', array( $this, 'do_scripts' ) ); |
||
108 | } |
||
109 | |||
110 | public function user_hooks() { |
||
111 | $priority = $this->get_priority(); |
||
112 | |||
113 | add_action( 'show_user_profile', array( $this, 'user_metabox' ), $priority ); |
||
114 | add_action( 'edit_user_profile', array( $this, 'user_metabox' ), $priority ); |
||
115 | add_action( 'user_new_form', array( $this, 'user_new_metabox' ), $priority ); |
||
116 | |||
117 | add_action( 'personal_options_update', array( $this, 'save_user' ) ); |
||
118 | add_action( 'edit_user_profile_update', array( $this, 'save_user' ) ); |
||
119 | add_action( 'user_register', array( $this, 'save_user' ) ); |
||
120 | } |
||
121 | |||
122 | public function term_hooks() { |
||
123 | if ( ! function_exists( 'get_term_meta' ) ) { |
||
124 | wp_die( __( 'Term Metadata is a WordPress > 4.4 feature. Please upgrade your WordPress install.', 'cmb2' ) ); |
||
125 | } |
||
126 | |||
127 | if ( ! $this->cmb->prop( 'taxonomies' ) ) { |
||
128 | wp_die( __( 'Term metaboxes configuration requires a \'taxonomies\' parameter', 'cmb2' ) ); |
||
129 | } |
||
130 | |||
131 | $this->taxonomies = (array) $this->cmb->prop( 'taxonomies' ); |
||
132 | $show_on_term_add = $this->cmb->prop( 'new_term_section' ); |
||
133 | $priority = $this->get_priority( 8 ); |
||
134 | |||
135 | foreach ( $this->taxonomies as $taxonomy ) { |
||
136 | // Display our form data |
||
137 | add_action( "{$taxonomy}_edit_form", array( $this, 'term_metabox' ), $priority, 2 ); |
||
138 | |||
139 | $show_on_add = is_array( $show_on_term_add ) |
||
140 | ? in_array( $taxonomy, $show_on_term_add ) |
||
141 | : (bool) $show_on_term_add; |
||
142 | |||
143 | $show_on_add = apply_filters( "cmb2_show_on_term_add_form_{$this->cmb->cmb_id}", $show_on_add, $this->cmb ); |
||
144 | |||
145 | // Display form in add-new section (unless specified not to) |
||
146 | if ( $show_on_add ) { |
||
147 | add_action( "{$taxonomy}_add_form_fields", array( $this, 'term_metabox' ), $priority, 2 ); |
||
148 | } |
||
149 | |||
150 | } |
||
151 | |||
152 | add_action( 'created_term', array( $this, 'save_term' ), 10, 3 ); |
||
153 | add_action( 'edited_terms', array( $this, 'save_term' ), 10, 2 ); |
||
154 | |||
155 | add_action( 'delete_term', array( $this, 'delete_term' ), 10, 3 ); |
||
156 | } |
||
157 | |||
158 | /** |
||
159 | * Registers styles for CMB2 |
||
160 | * @since 2.0.7 |
||
161 | */ |
||
162 | 1 | protected static function register_styles() { |
|
163 | 1 | if ( self::$css_registration_done ) { |
|
164 | return; |
||
165 | } |
||
166 | |||
167 | // Only use minified files if SCRIPT_DEBUG is off |
||
168 | 1 | $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
|
169 | 1 | $front = is_admin() ? '' : '-front'; |
|
170 | 1 | $rtl = is_rtl() ? '-rtl' : ''; |
|
171 | |||
172 | // Filter required styles and register stylesheet |
||
173 | 1 | $styles = apply_filters( 'cmb2_style_dependencies', array() ); |
|
174 | 1 | wp_register_style( 'cmb2-styles', cmb2_utils()->url( "css/cmb2{$front}{$rtl}{$min}.css" ), $styles ); |
|
175 | |||
176 | 1 | self::$css_registration_done = true; |
|
177 | 1 | } |
|
178 | |||
179 | /** |
||
180 | * Registers scripts for CMB2 |
||
181 | * @since 2.0.7 |
||
182 | */ |
||
183 | 1 | protected static function register_js() { |
|
184 | 1 | if ( self::$js_registration_done ) { |
|
185 | return; |
||
186 | } |
||
187 | |||
188 | 1 | $hook = is_admin() ? 'admin_footer' : 'wp_footer'; |
|
189 | 1 | add_action( $hook, array( 'CMB2_JS', 'enqueue' ), 8 ); |
|
190 | |||
191 | 1 | self::$js_registration_done = true; |
|
192 | 1 | } |
|
193 | |||
194 | /** |
||
195 | * Registers scripts and styles for CMB2 |
||
196 | * @since 1.0.0 |
||
197 | */ |
||
198 | public static function register_scripts() { |
||
202 | |||
203 | /** |
||
204 | * Enqueues scripts and styles for CMB2 |
||
205 | * @since 1.0.0 |
||
206 | */ |
||
207 | public function do_scripts() { |
||
208 | $screen = get_current_screen(); |
||
209 | |||
210 | if ( ! property_exists( $screen, 'base' ) || ! property_exists( $screen, 'post_type' ) ) { |
||
225 | |||
226 | /** |
||
227 | * Add metaboxes (to 'post' or 'comment' object types) |
||
228 | * @since 1.0.0 |
||
229 | */ |
||
230 | public function add_metaboxes() { |
||
256 | |||
257 | /** |
||
258 | * Add 'closed' class to metabox |
||
259 | * @since 2.0.0 |
||
260 | * @param array $classes Array of classes |
||
261 | * @return array Modified array of classes |
||
262 | */ |
||
263 | public function close_metabox_class( $classes ) { |
||
267 | |||
268 | /** |
||
269 | * Display metaboxes for a post or comment object |
||
270 | * @since 1.0.0 |
||
271 | */ |
||
272 | public function metabox_callback() { |
||
276 | |||
277 | /** |
||
278 | * Display metaboxes for new user page |
||
279 | * @since 1.0.0 |
||
280 | */ |
||
281 | public function user_new_metabox( $section ) { |
||
288 | |||
289 | /** |
||
290 | * Display metaboxes for a user object |
||
291 | * @since 1.0.0 |
||
292 | */ |
||
293 | public function user_metabox() { |
||
296 | |||
297 | /** |
||
298 | * Display metaboxes for a taxonomy term object |
||
299 | * @since 2.2.0 |
||
300 | */ |
||
301 | public function term_metabox() { |
||
304 | |||
305 | /** |
||
306 | * Display metaboxes for an object type |
||
307 | * @since 2.2.0 |
||
308 | * @param string $type Object type |
||
309 | * @return void |
||
310 | */ |
||
311 | public function show_form_for_type( $type ) { |
||
329 | |||
330 | /** |
||
331 | * Determines if metabox should be shown in current context |
||
332 | * @since 2.0.0 |
||
333 | * @return bool Whether metabox should be added/shown |
||
334 | */ |
||
335 | public function show_on() { |
||
350 | |||
351 | /** |
||
352 | * Get the CMB priority property set to numeric hook priority. |
||
353 | * @since 2.2.0 |
||
354 | * @param integer $default Default display hook priority. |
||
355 | * @return integer Hook priority. |
||
356 | */ |
||
357 | public function get_priority( $default = 10 ) { |
||
379 | |||
380 | /** |
||
381 | * Save data from post metabox |
||
382 | * @since 1.0.0 |
||
383 | * @param int $post_id Post ID |
||
384 | * @param mixed $post Post object |
||
385 | * @return null |
||
386 | */ |
||
387 | public function save_post( $post_id, $post = false ) { |
||
406 | |||
407 | /** |
||
408 | * Save data from comment metabox |
||
409 | * @since 2.0.9 |
||
410 | * @param int $comment_id Comment ID |
||
411 | * @return null |
||
412 | */ |
||
413 | public function save_comment( $comment_id ) { |
||
421 | |||
422 | /** |
||
423 | * Save data from user fields |
||
424 | * @since 1.0.x |
||
425 | * @param int $user_id User ID |
||
426 | * @return null |
||
427 | */ |
||
428 | public function save_user( $user_id ) { |
||
434 | |||
435 | /** |
||
436 | * Save data from term fields |
||
437 | * @since 1.0.x |
||
438 | * @param int $term_id Term ID |
||
439 | * @param int $tt_id Term Taxonomy ID |
||
440 | * @param string $taxonomy Taxonomy |
||
441 | * @return null |
||
442 | */ |
||
443 | public function save_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
451 | |||
452 | /** |
||
453 | * Delete term meta when a term is deleted. |
||
454 | * @since 1.0.x |
||
455 | * @param int $term_id Term ID |
||
456 | * @param int $tt_id Term Taxonomy ID |
||
457 | * @param string $taxonomy Taxonomy |
||
458 | * @return null |
||
459 | */ |
||
460 | public function delete_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
470 | |||
471 | /** |
||
472 | * Determines if the current object is able to be saved |
||
473 | * @since 2.0.9 |
||
474 | * @param string $type Current post_type or comment_type |
||
475 | * @return bool Whether object can be saved |
||
476 | */ |
||
477 | public function can_save( $type = '' ) { |
||
489 | |||
490 | /** |
||
491 | * Determine if taxonomy of term being modified is cmb2-editable. |
||
492 | * @since 2.2.0 |
||
493 | * @param string $taxonomy Taxonomy of term being modified. |
||
494 | * @return bool Whether taxonomy is editable. |
||
495 | */ |
||
496 | public function taxonomy_can_save( $taxonomy ) { |
||
511 | |||
512 | /** |
||
513 | * Ensures WordPress hook only gets fired once |
||
514 | * @since 2.0.0 |
||
515 | * @param string $action The name of the filter to hook the $hook callback to. |
||
516 | * @param callback $hook The callback to be run when the filter is applied. |
||
517 | * @param integer $priority Order the functions are executed |
||
518 | * @param int $accepted_args The number of arguments the function accepts. |
||
519 | */ |
||
520 | public function once( $action, $hook, $priority = 10, $accepted_args = 1 ) { |
||
530 | |||
531 | /** |
||
532 | * Includes CMB2 styles |
||
533 | * @since 2.0.0 |
||
534 | */ |
||
535 | 1 | public static function enqueue_cmb_css() { |
|
543 | |||
544 | /** |
||
545 | * Includes CMB2 JS |
||
546 | * @since 2.0.0 |
||
547 | */ |
||
548 | 1 | public static function enqueue_cmb_js() { |
|
556 | |||
557 | } |
||
558 |
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.