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() { |
||
| 90 | |||
| 91 | public function post_hooks() { |
||
| 97 | |||
| 98 | public function comment_hooks() { |
||
| 102 | |||
| 103 | public function user_hooks() { |
||
| 114 | |||
| 115 | public function term_hooks() { |
||
| 149 | |||
| 150 | /** |
||
| 151 | * Registers styles for CMB2 |
||
| 152 | * @since 2.0.7 |
||
| 153 | */ |
||
| 154 | 1 | protected static function register_styles() { |
|
| 170 | |||
| 171 | /** |
||
| 172 | * Registers scripts for CMB2 |
||
| 173 | * @since 2.0.7 |
||
| 174 | */ |
||
| 175 | 1 | protected static function register_js() { |
|
| 185 | |||
| 186 | /** |
||
| 187 | * Registers scripts and styles for CMB2 |
||
| 188 | * @since 1.0.0 |
||
| 189 | */ |
||
| 190 | public static function register_scripts() { |
||
| 194 | |||
| 195 | /** |
||
| 196 | * Enqueues scripts and styles for CMB2 in admin_head. |
||
| 197 | * @since 1.0.0 |
||
| 198 | */ |
||
| 199 | public function do_scripts( $hook ) { |
||
| 223 | |||
| 224 | /** |
||
| 225 | * Add metaboxes (to 'post' or 'comment' object types) |
||
| 226 | * @since 1.0.0 |
||
| 227 | */ |
||
| 228 | public function add_metaboxes() { |
||
| 254 | |||
| 255 | /** |
||
| 256 | * Add 'closed' class to metabox |
||
| 257 | * @since 2.0.0 |
||
| 258 | * @param array $classes Array of classes |
||
| 259 | * @return array Modified array of classes |
||
| 260 | */ |
||
| 261 | public function close_metabox_class( $classes ) { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Display metaboxes for a post or comment object |
||
| 268 | * @since 1.0.0 |
||
| 269 | */ |
||
| 270 | public function metabox_callback() { |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Display metaboxes for new user page |
||
| 277 | * @since 1.0.0 |
||
| 278 | */ |
||
| 279 | public function user_new_metabox( $section ) { |
||
| 286 | |||
| 287 | /** |
||
| 288 | * Display metaboxes for a user object |
||
| 289 | * @since 1.0.0 |
||
| 290 | */ |
||
| 291 | public function user_metabox() { |
||
| 294 | |||
| 295 | /** |
||
| 296 | * Display metaboxes for a taxonomy term object |
||
| 297 | * @since 2.2.0 |
||
| 298 | */ |
||
| 299 | public function term_metabox() { |
||
| 302 | |||
| 303 | /** |
||
| 304 | * Display metaboxes for an object type |
||
| 305 | * @since 2.2.0 |
||
| 306 | * @param string $type Object type |
||
| 307 | * @return void |
||
| 308 | */ |
||
| 309 | public function show_form_for_type( $type ) { |
||
| 327 | |||
| 328 | /** |
||
| 329 | * Determines if metabox should be shown in current context |
||
| 330 | * @since 2.0.0 |
||
| 331 | * @return bool Whether metabox should be added/shown |
||
| 332 | */ |
||
| 333 | public function show_on() { |
||
| 348 | |||
| 349 | /** |
||
| 350 | * Get the CMB priority property set to numeric hook priority. |
||
| 351 | * @since 2.2.0 |
||
| 352 | * @param integer $default Default display hook priority. |
||
| 353 | * @return integer Hook priority. |
||
| 354 | */ |
||
| 355 | public function get_priority( $default = 10 ) { |
||
| 377 | |||
| 378 | /** |
||
| 379 | * Save data from post metabox |
||
| 380 | * @since 1.0.0 |
||
| 381 | * @param int $post_id Post ID |
||
| 382 | * @param mixed $post Post object |
||
| 383 | * @return null |
||
| 384 | */ |
||
| 385 | public function save_post( $post_id, $post = false ) { |
||
| 404 | |||
| 405 | /** |
||
| 406 | * Save data from comment metabox |
||
| 407 | * @since 2.0.9 |
||
| 408 | * @param int $comment_id Comment ID |
||
| 409 | * @return null |
||
| 410 | */ |
||
| 411 | public function save_comment( $comment_id ) { |
||
| 419 | |||
| 420 | /** |
||
| 421 | * Save data from user fields |
||
| 422 | * @since 1.0.x |
||
| 423 | * @param int $user_id User ID |
||
| 424 | * @return null |
||
| 425 | */ |
||
| 426 | public function save_user( $user_id ) { |
||
| 432 | |||
| 433 | /** |
||
| 434 | * Save data from term fields |
||
| 435 | * @since 2.2.0 |
||
| 436 | * @param int $term_id Term ID |
||
| 437 | * @param int $tt_id Term Taxonomy ID |
||
| 438 | * @param string $taxonomy Taxonomy |
||
| 439 | * @return null |
||
| 440 | */ |
||
| 441 | public function save_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
| 449 | |||
| 450 | /** |
||
| 451 | * Delete term meta when a term is deleted. |
||
| 452 | * @since 2.2.0 |
||
| 453 | * @param int $term_id Term ID |
||
| 454 | * @param int $tt_id Term Taxonomy ID |
||
| 455 | * @param string $taxonomy Taxonomy |
||
| 456 | * @return null |
||
| 457 | */ |
||
| 458 | public function delete_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
| 468 | |||
| 469 | /** |
||
| 470 | * Determines if the current object is able to be saved |
||
| 471 | * @since 2.0.9 |
||
| 472 | * @param string $type Current post_type or comment_type |
||
| 473 | * @return bool Whether object can be saved |
||
| 474 | */ |
||
| 475 | public function can_save( $type = '' ) { |
||
| 487 | |||
| 488 | /** |
||
| 489 | * Determine if taxonomy of term being modified is cmb2-editable. |
||
| 490 | * @since 2.2.0 |
||
| 491 | * @param string $taxonomy Taxonomy of term being modified. |
||
| 492 | * @return bool Whether taxonomy is editable. |
||
| 493 | */ |
||
| 494 | public function taxonomy_can_save( $taxonomy ) { |
||
| 507 | |||
| 508 | /** |
||
| 509 | * Ensures WordPress hook only gets fired once |
||
| 510 | * @since 2.0.0 |
||
| 511 | * @param string $action The name of the filter to hook the $hook callback to. |
||
| 512 | * @param callback $hook The callback to be run when the filter is applied. |
||
| 513 | * @param integer $priority Order the functions are executed |
||
| 514 | * @param int $accepted_args The number of arguments the function accepts. |
||
| 515 | */ |
||
| 516 | public function once( $action, $hook, $priority = 10, $accepted_args = 1 ) { |
||
| 526 | |||
| 527 | /** |
||
| 528 | * Includes CMB2 styles |
||
| 529 | * @since 2.0.0 |
||
| 530 | */ |
||
| 531 | 1 | public static function enqueue_cmb_css() { |
|
| 539 | |||
| 540 | /** |
||
| 541 | * Includes CMB2 JS |
||
| 542 | * @since 2.0.0 |
||
| 543 | */ |
||
| 544 | 1 | public static function enqueue_cmb_js() { |
|
| 552 | |||
| 553 | } |
||
| 554 |
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.