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 ) { |
||
| 78 | |||
| 79 | public function universal_hooks() { |
||
| 93 | |||
| 94 | public function post_hooks() { |
||
| 102 | |||
| 103 | public function comment_hooks() { |
||
| 109 | |||
| 110 | 1 | public function user_hooks() { |
|
| 121 | 1 | ||
| 122 | 1 | public function term_hooks() { |
|
| 157 | 1 | ||
| 158 | 1 | /** |
|
| 159 | 1 | * Registers styles for CMB2 |
|
| 160 | 1 | * @since 2.0.7 |
|
| 161 | 1 | */ |
|
| 162 | protected static function register_styles() { |
||
| 178 | 1 | ||
| 179 | /** |
||
| 180 | 1 | * Registers scripts for CMB2 |
|
| 181 | * @since 2.0.7 |
||
| 182 | 1 | */ |
|
| 183 | 1 | protected static function register_js() { |
|
| 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( $hook ) { |
||
| 218 | |||
| 219 | /** |
||
| 220 | * Add metaboxes (to 'post' or 'comment' object types) |
||
| 221 | * @since 1.0.0 |
||
| 222 | */ |
||
| 223 | public function add_metaboxes() { |
||
| 249 | |||
| 250 | /** |
||
| 251 | * Add 'closed' class to metabox |
||
| 252 | * @since 2.0.0 |
||
| 253 | * @param array $classes Array of classes |
||
| 254 | * @return array Modified array of classes |
||
| 255 | */ |
||
| 256 | public function close_metabox_class( $classes ) { |
||
| 260 | |||
| 261 | /** |
||
| 262 | * Display metaboxes for a post or comment object |
||
| 263 | * @since 1.0.0 |
||
| 264 | */ |
||
| 265 | public function metabox_callback() { |
||
| 269 | |||
| 270 | /** |
||
| 271 | * Display metaboxes for new user page |
||
| 272 | * @since 1.0.0 |
||
| 273 | */ |
||
| 274 | public function user_new_metabox( $section ) { |
||
| 281 | |||
| 282 | /** |
||
| 283 | * Display metaboxes for a user object |
||
| 284 | * @since 1.0.0 |
||
| 285 | */ |
||
| 286 | public function user_metabox() { |
||
| 289 | |||
| 290 | /** |
||
| 291 | * Display metaboxes for a taxonomy term object |
||
| 292 | * @since 2.2.0 |
||
| 293 | */ |
||
| 294 | public function term_metabox() { |
||
| 297 | |||
| 298 | /** |
||
| 299 | * Display metaboxes for an object type |
||
| 300 | * @since 2.2.0 |
||
| 301 | * @param string $type Object type |
||
| 302 | * @return void |
||
| 303 | */ |
||
| 304 | public function show_form_for_type( $type ) { |
||
| 322 | |||
| 323 | /** |
||
| 324 | * Determines if metabox should be shown in current context |
||
| 325 | * @since 2.0.0 |
||
| 326 | * @return bool Whether metabox should be added/shown |
||
| 327 | */ |
||
| 328 | public function show_on() { |
||
| 343 | |||
| 344 | /** |
||
| 345 | * Get the CMB priority property set to numeric hook priority. |
||
| 346 | * @since 2.2.0 |
||
| 347 | * @param integer $default Default display hook priority. |
||
| 348 | * @return integer Hook priority. |
||
| 349 | */ |
||
| 350 | 1 | public function get_priority( $default = 10 ) { |
|
| 372 | |||
| 373 | /** |
||
| 374 | * Save data from post metabox |
||
| 375 | * @since 1.0.0 |
||
| 376 | * @param int $post_id Post ID |
||
| 377 | * @param mixed $post Post object |
||
| 378 | * @return null |
||
| 379 | */ |
||
| 380 | public function save_post( $post_id, $post = false ) { |
||
| 399 | |||
| 400 | /** |
||
| 401 | * Save data from comment metabox |
||
| 402 | * @since 2.0.9 |
||
| 403 | * @param int $comment_id Comment ID |
||
| 404 | * @return null |
||
| 405 | */ |
||
| 406 | public function save_comment( $comment_id ) { |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Save data from user fields |
||
| 417 | * @since 1.0.x |
||
| 418 | * @param int $user_id User ID |
||
| 419 | * @return null |
||
| 420 | */ |
||
| 421 | public function save_user( $user_id ) { |
||
| 427 | |||
| 428 | /** |
||
| 429 | * Save data from term fields |
||
| 430 | * @since 1.0.x |
||
| 431 | * @param int $term_id Term ID |
||
| 432 | * @param int $tt_id Term Taxonomy ID |
||
| 433 | * @param string $taxonomy Taxonomy |
||
| 434 | * @return null |
||
| 435 | */ |
||
| 436 | public function save_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
| 444 | |||
| 445 | /** |
||
| 446 | * Delete term meta when a term is deleted. |
||
| 447 | * @since 1.0.x |
||
| 448 | * @param int $term_id Term ID |
||
| 449 | * @param int $tt_id Term Taxonomy ID |
||
| 450 | * @param string $taxonomy Taxonomy |
||
| 451 | * @return null |
||
| 452 | */ |
||
| 453 | public function delete_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
| 463 | |||
| 464 | /** |
||
| 465 | * Determines if the current object is able to be saved |
||
| 466 | * @since 2.0.9 |
||
| 467 | * @param string $type Current post_type or comment_type |
||
| 468 | * @return bool Whether object can be saved |
||
| 469 | */ |
||
| 470 | public function can_save( $type = '' ) { |
||
| 482 | |||
| 483 | /** |
||
| 484 | * Determine if taxonomy of term being modified is cmb2-editable. |
||
| 485 | * @since 2.2.0 |
||
| 486 | * @param string $taxonomy Taxonomy of term being modified. |
||
| 487 | * @return bool Whether taxonomy is editable. |
||
| 488 | */ |
||
| 489 | public function taxonomy_can_save( $taxonomy ) { |
||
| 504 | |||
| 505 | /** |
||
| 506 | * Ensures WordPress hook only gets fired once |
||
| 507 | * @since 2.0.0 |
||
| 508 | * @param string $action The name of the filter to hook the $hook callback to. |
||
| 509 | * @param callback $hook The callback to be run when the filter is applied. |
||
| 510 | * @param integer $priority Order the functions are executed |
||
| 511 | * @param int $accepted_args The number of arguments the function accepts. |
||
| 512 | */ |
||
| 513 | public function once( $action, $hook, $priority = 10, $accepted_args = 1 ) { |
||
| 523 | |||
| 524 | /** |
||
| 525 | * Includes CMB2 styles |
||
| 526 | * @since 2.0.0 |
||
| 527 | */ |
||
| 528 | public static function enqueue_cmb_css() { |
||
| 536 | |||
| 537 | /** |
||
| 538 | * Includes CMB2 JS |
||
| 539 | * @since 2.0.0 |
||
| 540 | */ |
||
| 541 | public static function enqueue_cmb_js() { |
||
| 549 | |||
| 550 | } |
||
| 551 |
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.