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() { |
||
| 89 | |||
| 90 | public function post_hooks() { |
||
| 98 | |||
| 99 | public function comment_hooks() { |
||
| 105 | |||
| 106 | public function user_hooks() { |
||
| 117 | |||
| 118 | public function term_hooks() { |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Registers styles for CMB2 |
||
| 156 | * @since 2.0.7 |
||
| 157 | */ |
||
| 158 | 1 | protected static function register_styles() { |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Registers scripts for CMB2 |
||
| 177 | * @since 2.0.7 |
||
| 178 | */ |
||
| 179 | 1 | protected static function register_js() { |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Registers scripts and styles for CMB2 |
||
| 192 | * @since 1.0.0 |
||
| 193 | */ |
||
| 194 | public static function register_scripts() { |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Enqueues scripts and styles for CMB2 |
||
| 201 | * @since 1.0.0 |
||
| 202 | */ |
||
| 203 | public function do_scripts( $hook ) { |
||
| 214 | |||
| 215 | /** |
||
| 216 | * Add metaboxes (to 'post' or 'comment' object types) |
||
| 217 | * @since 1.0.0 |
||
| 218 | */ |
||
| 219 | public function add_metaboxes() { |
||
| 245 | |||
| 246 | /** |
||
| 247 | * Add 'closed' class to metabox |
||
| 248 | * @since 2.0.0 |
||
| 249 | * @param array $classes Array of classes |
||
| 250 | * @return array Modified array of classes |
||
| 251 | */ |
||
| 252 | public function close_metabox_class( $classes ) { |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Display metaboxes for a post or comment object |
||
| 259 | * @since 1.0.0 |
||
| 260 | */ |
||
| 261 | public function metabox_callback() { |
||
| 265 | |||
| 266 | /** |
||
| 267 | * Display metaboxes for new user page |
||
| 268 | * @since 1.0.0 |
||
| 269 | */ |
||
| 270 | public function user_new_metabox( $section ) { |
||
| 277 | |||
| 278 | /** |
||
| 279 | * Display metaboxes for a user object |
||
| 280 | * @since 1.0.0 |
||
| 281 | */ |
||
| 282 | public function user_metabox() { |
||
| 285 | |||
| 286 | /** |
||
| 287 | * Display metaboxes for a taxonomy term object |
||
| 288 | * @since 2.2.0 |
||
| 289 | */ |
||
| 290 | public function term_metabox() { |
||
| 293 | |||
| 294 | /** |
||
| 295 | * Display metaboxes for an object type |
||
| 296 | * @since 2.2.0 |
||
| 297 | * @param string $type Object type |
||
| 298 | * @return void |
||
| 299 | */ |
||
| 300 | public function show_form_for_type( $type ) { |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Determines if metabox should be shown in current context |
||
| 321 | * @since 2.0.0 |
||
| 322 | * @return bool Whether metabox should be added/shown |
||
| 323 | */ |
||
| 324 | public function show_on() { |
||
| 339 | |||
| 340 | /** |
||
| 341 | * Get the CMB priority property set to numeric hook priority. |
||
| 342 | * @since 2.2.0 |
||
| 343 | * @param integer $default Default display hook priority. |
||
| 344 | * @return integer Hook priority. |
||
| 345 | */ |
||
| 346 | public function get_priority( $default = 10 ) { |
||
| 368 | |||
| 369 | /** |
||
| 370 | * Save data from post metabox |
||
| 371 | * @since 1.0.0 |
||
| 372 | * @param int $post_id Post ID |
||
| 373 | * @param mixed $post Post object |
||
| 374 | * @return null |
||
| 375 | */ |
||
| 376 | public function save_post( $post_id, $post = false ) { |
||
| 395 | |||
| 396 | /** |
||
| 397 | * Save data from comment metabox |
||
| 398 | * @since 2.0.9 |
||
| 399 | * @param int $comment_id Comment ID |
||
| 400 | * @return null |
||
| 401 | */ |
||
| 402 | public function save_comment( $comment_id ) { |
||
| 410 | |||
| 411 | /** |
||
| 412 | * Save data from user fields |
||
| 413 | * @since 1.0.x |
||
| 414 | * @param int $user_id User ID |
||
| 415 | * @return null |
||
| 416 | */ |
||
| 417 | public function save_user( $user_id ) { |
||
| 423 | |||
| 424 | /** |
||
| 425 | * Save data from term fields |
||
| 426 | * @since 2.2.0 |
||
| 427 | * @param int $term_id Term ID |
||
| 428 | * @param int $tt_id Term Taxonomy ID |
||
| 429 | * @param string $taxonomy Taxonomy |
||
| 430 | * @return null |
||
| 431 | */ |
||
| 432 | public function save_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
| 440 | |||
| 441 | /** |
||
| 442 | * Delete term meta when a term is deleted. |
||
| 443 | * @since 2.2.0 |
||
| 444 | * @param int $term_id Term ID |
||
| 445 | * @param int $tt_id Term Taxonomy ID |
||
| 446 | * @param string $taxonomy Taxonomy |
||
| 447 | * @return null |
||
| 448 | */ |
||
| 449 | public function delete_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
| 459 | |||
| 460 | /** |
||
| 461 | * Determines if the current object is able to be saved |
||
| 462 | * @since 2.0.9 |
||
| 463 | * @param string $type Current post_type or comment_type |
||
| 464 | * @return bool Whether object can be saved |
||
| 465 | */ |
||
| 466 | public function can_save( $type = '' ) { |
||
| 478 | |||
| 479 | /** |
||
| 480 | * Determine if taxonomy of term being modified is cmb2-editable. |
||
| 481 | * @since 2.2.0 |
||
| 482 | * @param string $taxonomy Taxonomy of term being modified. |
||
| 483 | * @return bool Whether taxonomy is editable. |
||
| 484 | */ |
||
| 485 | public function taxonomy_can_save( $taxonomy ) { |
||
| 498 | |||
| 499 | /** |
||
| 500 | * Ensures WordPress hook only gets fired once |
||
| 501 | * @since 2.0.0 |
||
| 502 | * @param string $action The name of the filter to hook the $hook callback to. |
||
| 503 | * @param callback $hook The callback to be run when the filter is applied. |
||
| 504 | * @param integer $priority Order the functions are executed |
||
| 505 | * @param int $accepted_args The number of arguments the function accepts. |
||
| 506 | */ |
||
| 507 | public function once( $action, $hook, $priority = 10, $accepted_args = 1 ) { |
||
| 517 | |||
| 518 | /** |
||
| 519 | * Includes CMB2 styles |
||
| 520 | * @since 2.0.0 |
||
| 521 | */ |
||
| 522 | 1 | public static function enqueue_cmb_css() { |
|
| 530 | |||
| 531 | /** |
||
| 532 | * Includes CMB2 JS |
||
| 533 | * @since 2.0.0 |
||
| 534 | */ |
||
| 535 | 1 | public static function enqueue_cmb_js() { |
|
| 543 | |||
| 544 | } |
||
| 545 |
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.