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 | * 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 | * CMB taxonomies array for term meta |
||
| 39 | * @var array |
||
| 40 | * @since 2.2.0 |
||
| 41 | */ |
||
| 42 | protected $taxonomies = 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() { |
||
| 55 | |||
| 56 | foreach ( get_class_methods( 'CMB2_Show_Filters' ) as $filter ) { |
||
| 57 | add_filter( 'cmb2_show_on', array( 'CMB2_Show_Filters', $filter ), 10, 3 ); |
||
| 58 | } |
||
| 59 | |||
| 60 | if ( is_admin() ) { |
||
| 61 | // register our scripts and styles for cmb |
||
| 62 | $this->once( 'admin_enqueue_scripts', array( __CLASS__, 'register_scripts' ), 8 ); |
||
| 63 | $this->once( 'admin_enqueue_scripts', array( $this, 'do_scripts' ) ); |
||
| 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 post_hooks() { |
||
| 80 | add_action( 'add_meta_boxes', array( $this, 'add_metaboxes' ) ); |
||
| 81 | add_action( 'add_attachment', array( $this, 'save_post' ) ); |
||
| 82 | add_action( 'edit_attachment', array( $this, 'save_post' ) ); |
||
| 83 | add_action( 'save_post', array( $this, 'save_post' ), 10, 2 ); |
||
| 84 | } |
||
| 85 | |||
| 86 | public function comment_hooks() { |
||
| 90 | |||
| 91 | public function user_hooks() { |
||
| 92 | $priority = $this->get_priority(); |
||
| 93 | |||
| 94 | add_action( 'show_user_profile', array( $this, 'user_metabox' ), $priority ); |
||
| 95 | add_action( 'edit_user_profile', array( $this, 'user_metabox' ), $priority ); |
||
| 96 | add_action( 'user_new_form', array( $this, 'user_new_metabox' ), $priority ); |
||
| 97 | |||
| 98 | add_action( 'personal_options_update', array( $this, 'save_user' ) ); |
||
| 99 | add_action( 'edit_user_profile_update', array( $this, 'save_user' ) ); |
||
| 100 | add_action( 'user_register', array( $this, 'save_user' ) ); |
||
| 101 | } |
||
| 102 | |||
| 103 | public function term_hooks() { |
||
| 104 | if ( ! function_exists( 'get_term_meta' ) ) { |
||
| 105 | wp_die( __( 'Term Metadata is a WordPress > 4.4 feature. Please upgrade your WordPress install.', 'cmb2' ) ); |
||
| 106 | } |
||
| 107 | |||
| 108 | if ( ! $this->cmb->prop( 'taxonomies' ) ) { |
||
| 109 | wp_die( __( 'Term metaboxes configuration requires a \'taxonomies\' parameter', 'cmb2' ) ); |
||
| 110 | } |
||
| 111 | |||
| 112 | $this->taxonomies = (array) $this->cmb->prop( 'taxonomies' ); |
||
| 113 | $show_on_term_add = $this->cmb->prop( 'new_term_section' ); |
||
| 114 | $priority = $this->get_priority( 8 ); |
||
| 115 | |||
| 116 | foreach ( $this->taxonomies as $taxonomy ) { |
||
| 117 | // Display our form data |
||
| 118 | add_action( "{$taxonomy}_edit_form", array( $this, 'term_metabox' ), $priority, 2 ); |
||
| 119 | |||
| 120 | $show_on_add = is_array( $show_on_term_add ) |
||
| 121 | ? in_array( $taxonomy, $show_on_term_add ) |
||
| 122 | : (bool) $show_on_term_add; |
||
| 123 | |||
| 124 | $show_on_add = apply_filters( "cmb2_show_on_term_add_form_{$this->cmb->cmb_id}", $show_on_add, $this->cmb ); |
||
| 125 | |||
| 126 | // Display form in add-new section (unless specified not to) |
||
| 127 | if ( $show_on_add ) { |
||
| 128 | add_action( "{$taxonomy}_add_form_fields", array( $this, 'term_metabox' ), $priority, 2 ); |
||
| 129 | } |
||
| 130 | |||
| 131 | } |
||
| 132 | |||
| 133 | add_action( 'created_term', array( $this, 'save_term' ), 10, 3 ); |
||
| 134 | add_action( 'edited_terms', array( $this, 'save_term' ), 10, 2 ); |
||
| 135 | add_action( 'delete_term', array( $this, 'delete_term' ), 10, 3 ); |
||
| 136 | } |
||
| 137 | |||
| 138 | /** |
||
| 139 | * Registers styles for CMB2 |
||
| 140 | * @since 2.0.7 |
||
| 141 | */ |
||
| 142 | protected static function register_styles() { |
||
| 143 | if ( self::$css_registration_done ) { |
||
| 144 | return; |
||
| 145 | } |
||
| 146 | |||
| 147 | // Only use minified files if SCRIPT_DEBUG is off |
||
| 148 | $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
||
| 149 | $front = is_admin() ? '' : '-front'; |
||
| 150 | $rtl = is_rtl() ? '-rtl' : ''; |
||
| 151 | |||
| 152 | // Filter required styles and register stylesheet |
||
| 153 | $styles = apply_filters( 'cmb2_style_dependencies', array() ); |
||
| 154 | 1 | wp_register_style( 'cmb2-styles', cmb2_utils()->url( "css/cmb2{$front}{$rtl}{$min}.css" ), $styles ); |
|
| 155 | 1 | ||
| 156 | self::$css_registration_done = true; |
||
| 157 | } |
||
| 158 | |||
| 159 | /** |
||
| 160 | 1 | * Registers scripts for CMB2 |
|
| 161 | 1 | * @since 2.0.7 |
|
| 162 | 1 | */ |
|
| 163 | protected static function register_js() { |
||
| 164 | if ( self::$js_registration_done ) { |
||
| 165 | 1 | return; |
|
| 166 | 1 | } |
|
| 167 | |||
| 168 | 1 | $hook = is_admin() ? 'admin_footer' : 'wp_footer'; |
|
| 169 | 1 | add_action( $hook, array( 'CMB2_JS', 'enqueue' ), 8 ); |
|
| 170 | |||
| 171 | self::$js_registration_done = true; |
||
| 172 | } |
||
| 173 | |||
| 174 | /** |
||
| 175 | 1 | * Registers scripts and styles for CMB2 |
|
| 176 | 1 | * @since 1.0.0 |
|
| 177 | */ |
||
| 178 | public static function register_scripts() { |
||
| 179 | self::register_styles(); |
||
| 180 | 1 | self::register_js(); |
|
| 181 | 1 | } |
|
| 182 | |||
| 183 | 1 | /** |
|
| 184 | 1 | * Enqueues scripts and styles for CMB2 in admin_head. |
|
| 185 | * @since 1.0.0 |
||
| 186 | */ |
||
| 187 | public function do_scripts( $hook ) { |
||
| 188 | $hooks = array( |
||
| 189 | 'post.php', |
||
| 190 | 'post-new.php', |
||
| 191 | 'page-new.php', |
||
| 192 | 'page.php', |
||
| 193 | 'comment.php', |
||
| 194 | 'edit-tags.php', |
||
| 195 | 'term.php', |
||
| 196 | 'user-new.php', |
||
| 197 | 'profile.php', |
||
| 198 | 'user-edit.php', |
||
| 199 | ); |
||
| 200 | // only pre-enqueue our scripts/styles on the proper pages |
||
| 201 | // show_form_for_type will have us covered if we miss something here. |
||
| 202 | if ( in_array( $hook, $hooks, true ) ) { |
||
| 203 | if ( $this->cmb->prop( 'cmb_styles' ) ) { |
||
| 204 | self::enqueue_cmb_css(); |
||
| 205 | } |
||
| 206 | if ( $this->cmb->prop( 'enqueue_js' ) ) { |
||
| 207 | self::enqueue_cmb_js(); |
||
| 208 | } |
||
| 209 | } |
||
| 210 | } |
||
| 211 | |||
| 212 | /** |
||
| 213 | * Add metaboxes (to 'post' or 'comment' object types) |
||
| 214 | * @since 1.0.0 |
||
| 215 | */ |
||
| 216 | public function add_metaboxes() { |
||
| 217 | |||
| 218 | if ( ! $this->show_on() ) { |
||
| 219 | return; |
||
| 220 | } |
||
| 221 | |||
| 222 | foreach ( $this->cmb->prop( 'object_types' ) as $post_type ) { |
||
| 223 | /** |
||
| 224 | * To keep from registering an actual post-screen metabox, |
||
| 225 | * omit the 'title' attribute from the metabox registration array. |
||
| 226 | * |
||
| 227 | * (WordPress will not display metaboxes without titles anyway) |
||
| 228 | * |
||
| 229 | * This is a good solution if you want to output your metaboxes |
||
| 230 | * Somewhere else in the post-screen |
||
| 231 | */ |
||
| 232 | if ( $this->cmb->prop( 'title' ) ) { |
||
| 233 | |||
| 234 | if ( $this->cmb->prop( 'closed' ) ) { |
||
| 235 | add_filter( "postbox_classes_{$post_type}_{$this->cmb->cmb_id}", array( $this, 'close_metabox_class' ) ); |
||
| 236 | } |
||
| 237 | |||
| 238 | add_meta_box( $this->cmb->cmb_id, $this->cmb->prop( 'title' ), array( $this, 'metabox_callback' ), $post_type, $this->cmb->prop( 'context' ), $this->cmb->prop( 'priority' ) ); |
||
| 239 | } |
||
| 240 | } |
||
| 241 | } |
||
| 242 | |||
| 243 | /** |
||
| 244 | * Add 'closed' class to metabox |
||
| 245 | * @since 2.0.0 |
||
| 246 | * @param array $classes Array of classes |
||
| 247 | * @return array Modified array of classes |
||
| 248 | */ |
||
| 249 | public function close_metabox_class( $classes ) { |
||
| 253 | |||
| 254 | /** |
||
| 255 | * Display metaboxes for a post or comment object |
||
| 256 | * @since 1.0.0 |
||
| 257 | */ |
||
| 258 | public function metabox_callback() { |
||
| 259 | $object_id = 'comment' == $this->object_type ? get_comment_ID() : get_the_ID(); |
||
| 260 | $this->cmb->show_form( $object_id, $this->object_type ); |
||
| 261 | } |
||
| 262 | |||
| 263 | /** |
||
| 264 | * Display metaboxes for new user page |
||
| 265 | * @since 1.0.0 |
||
| 266 | */ |
||
| 267 | public function user_new_metabox( $section ) { |
||
| 268 | if ( $section == $this->cmb->prop( 'new_user_section' ) ) { |
||
| 269 | $object_id = $this->cmb->object_id(); |
||
| 270 | $this->cmb->object_id( isset( $_REQUEST['user_id'] ) ? $_REQUEST['user_id'] : $object_id ); |
||
| 271 | $this->user_metabox(); |
||
| 272 | } |
||
| 273 | } |
||
| 274 | |||
| 275 | /** |
||
| 276 | * Display metaboxes for a user object |
||
| 277 | * @since 1.0.0 |
||
| 278 | */ |
||
| 279 | public function user_metabox() { |
||
| 282 | |||
| 283 | /** |
||
| 284 | * Display metaboxes for a taxonomy term object |
||
| 285 | * @since 2.2.0 |
||
| 286 | */ |
||
| 287 | public function term_metabox() { |
||
| 288 | $this->show_form_for_type( 'term' ); |
||
| 289 | } |
||
| 290 | |||
| 291 | /** |
||
| 292 | * Display metaboxes for an object type |
||
| 293 | * @since 2.2.0 |
||
| 294 | * @param string $type Object type |
||
| 295 | * @return void |
||
| 296 | */ |
||
| 297 | public function show_form_for_type( $type ) { |
||
| 315 | |||
| 316 | /** |
||
| 317 | * Determines if metabox should be shown in current context |
||
| 318 | * @since 2.0.0 |
||
| 319 | * @return bool Whether metabox should be added/shown |
||
| 320 | */ |
||
| 321 | public function show_on() { |
||
| 336 | |||
| 337 | /** |
||
| 338 | * Get the CMB priority property set to numeric hook priority. |
||
| 339 | * @since 2.2.0 |
||
| 340 | * @param integer $default Default display hook priority. |
||
| 341 | * @return integer Hook priority. |
||
| 342 | */ |
||
| 343 | public function get_priority( $default = 10 ) { |
||
| 365 | |||
| 366 | /** |
||
| 367 | * Save data from post metabox |
||
| 368 | * @since 1.0.0 |
||
| 369 | * @param int $post_id Post ID |
||
| 370 | * @param mixed $post Post object |
||
| 371 | * @return null |
||
| 372 | */ |
||
| 373 | public function save_post( $post_id, $post = false ) { |
||
| 392 | |||
| 393 | /** |
||
| 394 | * Save data from comment metabox |
||
| 395 | * @since 2.0.9 |
||
| 396 | * @param int $comment_id Comment ID |
||
| 397 | * @return null |
||
| 398 | */ |
||
| 399 | public function save_comment( $comment_id ) { |
||
| 407 | |||
| 408 | /** |
||
| 409 | * Save data from user fields |
||
| 410 | * @since 1.0.x |
||
| 411 | * @param int $user_id User ID |
||
| 412 | * @return null |
||
| 413 | */ |
||
| 414 | public function save_user( $user_id ) { |
||
| 420 | |||
| 421 | /** |
||
| 422 | * Save data from term fields |
||
| 423 | * @since 2.2.0 |
||
| 424 | * @param int $term_id Term ID |
||
| 425 | * @param int $tt_id Term Taxonomy ID |
||
| 426 | * @param string $taxonomy Taxonomy |
||
| 427 | * @return null |
||
| 428 | */ |
||
| 429 | public function save_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
| 437 | |||
| 438 | /** |
||
| 439 | * Delete term meta when a term is deleted. |
||
| 440 | * @since 2.2.0 |
||
| 441 | * @param int $term_id Term ID |
||
| 442 | * @param int $tt_id Term Taxonomy ID |
||
| 443 | * @param string $taxonomy Taxonomy |
||
| 444 | * @return null |
||
| 445 | */ |
||
| 446 | public function delete_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
| 456 | |||
| 457 | /** |
||
| 458 | * Determines if the current object is able to be saved |
||
| 459 | * @since 2.0.9 |
||
| 460 | * @param string $type Current post_type or comment_type |
||
| 461 | * @return bool Whether object can be saved |
||
| 462 | */ |
||
| 463 | public function can_save( $type = '' ) { |
||
| 475 | |||
| 476 | /** |
||
| 477 | * Determine if taxonomy of term being modified is cmb2-editable. |
||
| 478 | * @since 2.2.0 |
||
| 479 | * @param string $taxonomy Taxonomy of term being modified. |
||
| 480 | * @return bool Whether taxonomy is editable. |
||
| 481 | */ |
||
| 482 | public function taxonomy_can_save( $taxonomy ) { |
||
| 495 | |||
| 496 | /** |
||
| 497 | * Includes CMB2 styles |
||
| 498 | * @since 2.0.0 |
||
| 499 | */ |
||
| 500 | public static function enqueue_cmb_css() { |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Includes CMB2 JS |
||
| 511 | * @since 2.0.0 |
||
| 512 | */ |
||
| 513 | public static function enqueue_cmb_js() { |
||
| 521 | |||
| 522 | } |
||
| 523 |
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.