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 | foreach ( get_class_methods( 'CMB2_Show_Filters' ) as $filter ) { |
||
| 81 | add_filter( 'cmb2_show_on', array( 'CMB2_Show_Filters', $filter ), 10, 3 ); |
||
| 82 | } |
||
| 83 | |||
| 84 | if ( is_admin() ) { |
||
| 85 | // register our scripts and styles for cmb |
||
| 86 | $this->once( 'admin_enqueue_scripts', array( __CLASS__, 'register_scripts' ), 8 ); |
||
| 87 | } |
||
| 88 | } |
||
| 89 | |||
| 90 | public function post_hooks() { |
||
| 91 | add_action( 'add_meta_boxes', array( $this, 'add_metaboxes' ) ); |
||
| 92 | add_action( 'add_attachment', array( $this, 'save_post' ) ); |
||
| 93 | add_action( 'edit_attachment', array( $this, 'save_post' ) ); |
||
| 94 | add_action( 'save_post', array( $this, 'save_post' ), 10, 2 ); |
||
| 95 | |||
| 96 | $this->once( 'admin_enqueue_scripts', array( $this, 'do_scripts' ) ); |
||
| 97 | } |
||
| 98 | |||
| 99 | public function comment_hooks() { |
||
| 100 | add_action( 'add_meta_boxes_comment', array( $this, 'add_metaboxes' ) ); |
||
| 101 | add_action( 'edit_comment', array( $this, 'save_comment' ) ); |
||
| 102 | |||
| 103 | $this->once( 'admin_enqueue_scripts', array( $this, 'do_scripts' ) ); |
||
| 104 | } |
||
| 105 | |||
| 106 | public function user_hooks() { |
||
| 107 | $priority = $this->get_priority(); |
||
| 108 | |||
| 109 | add_action( 'show_user_profile', array( $this, 'user_metabox' ), $priority ); |
||
| 110 | add_action( 'edit_user_profile', array( $this, 'user_metabox' ), $priority ); |
||
| 111 | add_action( 'user_new_form', array( $this, 'user_new_metabox' ), $priority ); |
||
| 112 | |||
| 113 | add_action( 'personal_options_update', array( $this, 'save_user' ) ); |
||
| 114 | add_action( 'edit_user_profile_update', array( $this, 'save_user' ) ); |
||
| 115 | add_action( 'user_register', array( $this, 'save_user' ) ); |
||
| 116 | } |
||
| 117 | |||
| 118 | public function term_hooks() { |
||
| 119 | if ( ! function_exists( 'get_term_meta' ) ) { |
||
| 120 | wp_die( __( 'Term Metadata is a WordPress > 4.4 feature. Please upgrade your WordPress install.', 'cmb2' ) ); |
||
| 121 | } |
||
| 122 | |||
| 123 | if ( ! $this->cmb->prop( 'taxonomies' ) ) { |
||
| 124 | wp_die( __( 'Term metaboxes configuration requires a \'taxonomies\' parameter', 'cmb2' ) ); |
||
| 125 | } |
||
| 126 | |||
| 127 | $this->taxonomies = (array) $this->cmb->prop( 'taxonomies' ); |
||
| 128 | $show_on_term_add = $this->cmb->prop( 'new_term_section' ); |
||
| 129 | $priority = $this->get_priority( 8 ); |
||
| 130 | |||
| 131 | foreach ( $this->taxonomies as $taxonomy ) { |
||
| 132 | // Display our form data |
||
| 133 | add_action( "{$taxonomy}_edit_form", array( $this, 'term_metabox' ), $priority, 2 ); |
||
| 134 | |||
| 135 | $show_on_add = is_array( $show_on_term_add ) |
||
| 136 | ? in_array( $taxonomy, $show_on_term_add ) |
||
| 137 | : (bool) $show_on_term_add; |
||
| 138 | |||
| 139 | $show_on_add = apply_filters( "cmb2_show_on_term_add_form_{$this->cmb->cmb_id}", $show_on_add, $this->cmb ); |
||
| 140 | |||
| 141 | // Display form in add-new section (unless specified not to) |
||
| 142 | if ( $show_on_add ) { |
||
| 143 | add_action( "{$taxonomy}_add_form_fields", array( $this, 'term_metabox' ), $priority, 2 ); |
||
| 144 | } |
||
| 145 | |||
| 146 | } |
||
| 147 | |||
| 148 | add_action( 'created_term', array( $this, 'save_term' ), 10, 3 ); |
||
| 149 | add_action( 'edited_terms', array( $this, 'save_term' ), 10, 2 ); |
||
| 150 | |||
| 151 | add_action( 'delete_term', array( $this, 'delete_term' ), 10, 3 ); |
||
| 152 | } |
||
| 153 | |||
| 154 | /** |
||
| 155 | * Registers styles for CMB2 |
||
| 156 | * @since 2.0.7 |
||
| 157 | */ |
||
| 158 | 1 | protected static function register_styles() { |
|
| 159 | 1 | if ( self::$css_registration_done ) { |
|
| 160 | return; |
||
| 161 | } |
||
| 162 | |||
| 163 | // Only use minified files if SCRIPT_DEBUG is off |
||
| 164 | 1 | $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
|
| 165 | 1 | $front = is_admin() ? '' : '-front'; |
|
| 166 | 1 | $rtl = is_rtl() ? '-rtl' : ''; |
|
| 167 | |||
| 168 | // Filter required styles and register stylesheet |
||
| 169 | 1 | $styles = apply_filters( 'cmb2_style_dependencies', array() ); |
|
| 170 | 1 | wp_register_style( 'cmb2-styles', cmb2_utils()->url( "css/cmb2{$front}{$rtl}{$min}.css" ), $styles ); |
|
| 171 | |||
| 172 | 1 | self::$css_registration_done = true; |
|
| 173 | 1 | } |
|
| 174 | |||
| 175 | /** |
||
| 176 | * Registers scripts for CMB2 |
||
| 177 | * @since 2.0.7 |
||
| 178 | */ |
||
| 179 | 1 | protected static function register_js() { |
|
| 180 | 1 | if ( self::$js_registration_done ) { |
|
| 181 | return; |
||
| 182 | } |
||
| 183 | |||
| 184 | 1 | $hook = is_admin() ? 'admin_footer' : 'wp_footer'; |
|
| 185 | 1 | add_action( $hook, array( 'CMB2_JS', 'enqueue' ), 8 ); |
|
| 186 | |||
| 187 | 1 | self::$js_registration_done = true; |
|
| 188 | 1 | } |
|
| 189 | |||
| 190 | /** |
||
| 191 | * Registers scripts and styles for CMB2 |
||
| 192 | * @since 1.0.0 |
||
| 193 | */ |
||
| 194 | public static function register_scripts() { |
||
| 195 | self::register_styles(); |
||
| 196 | self::register_js(); |
||
| 197 | } |
||
| 198 | |||
| 199 | /** |
||
| 200 | * Enqueues scripts and styles for CMB2 |
||
| 201 | * @since 1.0.0 |
||
| 202 | */ |
||
| 203 | public function do_scripts() { |
||
| 204 | $screen = get_current_screen(); |
||
| 205 | |||
| 206 | if ( ! property_exists( $screen, 'base' ) || ! property_exists( $screen, 'post_type' ) ) { |
||
| 207 | return; |
||
| 208 | } |
||
| 209 | |||
| 210 | // Only enqueue our scripts/styles on the proper pages. |
||
| 211 | if ( ( 'post' === $screen->base && ( is_string( $screen->post_type ) && '' !== $screen->post_type ) ) || |
||
| 212 | 'comment' === $screen->base ) { |
||
| 213 | if ( $this->cmb->prop( 'cmb_styles' ) ) { |
||
| 214 | self::enqueue_cmb_css(); |
||
| 215 | } |
||
| 216 | if ( $this->cmb->prop( 'enqueue_js' ) ) { |
||
| 217 | self::enqueue_cmb_js(); |
||
| 218 | } |
||
| 219 | } |
||
| 220 | } |
||
| 221 | |||
| 222 | /** |
||
| 223 | * Add metaboxes (to 'post' or 'comment' object types) |
||
| 224 | * @since 1.0.0 |
||
| 225 | */ |
||
| 226 | public function add_metaboxes() { |
||
| 227 | |||
| 228 | if ( ! $this->show_on() ) { |
||
| 229 | return; |
||
| 230 | } |
||
| 231 | |||
| 232 | foreach ( $this->cmb->prop( 'object_types' ) as $post_type ) { |
||
| 233 | /** |
||
| 234 | * To keep from registering an actual post-screen metabox, |
||
| 235 | * omit the 'title' attribute from the metabox registration array. |
||
| 236 | * |
||
| 237 | * (WordPress will not display metaboxes without titles anyway) |
||
| 238 | * |
||
| 239 | * This is a good solution if you want to output your metaboxes |
||
| 240 | * Somewhere else in the post-screen |
||
| 241 | */ |
||
| 242 | if ( $this->cmb->prop( 'title' ) ) { |
||
| 243 | |||
| 244 | if ( $this->cmb->prop( 'closed' ) ) { |
||
| 245 | add_filter( "postbox_classes_{$post_type}_{$this->cmb->cmb_id}", array( $this, 'close_metabox_class' ) ); |
||
| 246 | } |
||
| 247 | |||
| 248 | 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' ) ); |
||
| 249 | } |
||
| 250 | } |
||
| 251 | } |
||
| 252 | |||
| 253 | /** |
||
| 254 | * Add 'closed' class to metabox |
||
| 255 | * @since 2.0.0 |
||
| 256 | * @param array $classes Array of classes |
||
| 257 | * @return array Modified array of classes |
||
| 258 | */ |
||
| 259 | public function close_metabox_class( $classes ) { |
||
| 260 | $classes[] = 'closed'; |
||
| 261 | return $classes; |
||
| 262 | } |
||
| 263 | |||
| 264 | /** |
||
| 265 | * Display metaboxes for a post or comment object |
||
| 266 | * @since 1.0.0 |
||
| 267 | */ |
||
| 268 | public function metabox_callback() { |
||
| 269 | $object_id = 'comment' == $this->object_type ? get_comment_ID() : get_the_ID(); |
||
| 270 | $this->cmb->show_form( $object_id, $this->object_type ); |
||
| 271 | } |
||
| 272 | |||
| 273 | /** |
||
| 274 | * Display metaboxes for new user page |
||
| 275 | * @since 1.0.0 |
||
| 276 | */ |
||
| 277 | public function user_new_metabox( $section ) { |
||
| 278 | if ( $section == $this->cmb->prop( 'new_user_section' ) ) { |
||
| 279 | $object_id = $this->cmb->object_id(); |
||
| 280 | $this->cmb->object_id( isset( $_REQUEST['user_id'] ) ? $_REQUEST['user_id'] : $object_id ); |
||
| 281 | $this->user_metabox(); |
||
| 282 | } |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Display metaboxes for a user object |
||
| 287 | * @since 1.0.0 |
||
| 288 | */ |
||
| 289 | public function user_metabox() { |
||
| 292 | |||
| 293 | /** |
||
| 294 | * Display metaboxes for a taxonomy term object |
||
| 295 | * @since 2.2.0 |
||
| 296 | */ |
||
| 297 | public function term_metabox() { |
||
| 300 | |||
| 301 | /** |
||
| 302 | * Display metaboxes for an object type |
||
| 303 | * @since 2.2.0 |
||
| 304 | * @param string $type Object type |
||
| 305 | * @return void |
||
| 306 | */ |
||
| 307 | public function show_form_for_type( $type ) { |
||
| 308 | if ( $type != $this->cmb->mb_object_type() ) { |
||
| 309 | return; |
||
| 310 | } |
||
| 311 | |||
| 312 | if ( ! $this->show_on() ) { |
||
| 313 | return; |
||
| 314 | } |
||
| 325 | |||
| 326 | /** |
||
| 327 | * Determines if metabox should be shown in current context |
||
| 328 | * @since 2.0.0 |
||
| 329 | * @return bool Whether metabox should be added/shown |
||
| 330 | */ |
||
| 331 | public function show_on() { |
||
| 346 | |||
| 347 | /** |
||
| 348 | * Get the CMB priority property set to numeric hook priority. |
||
| 349 | * @since 2.2.0 |
||
| 350 | * @param integer $default Default display hook priority. |
||
| 351 | * @return integer Hook priority. |
||
| 352 | */ |
||
| 353 | public function get_priority( $default = 10 ) { |
||
| 375 | |||
| 376 | /** |
||
| 377 | * Save data from post metabox |
||
| 378 | * @since 1.0.0 |
||
| 379 | * @param int $post_id Post ID |
||
| 380 | * @param mixed $post Post object |
||
| 381 | * @return null |
||
| 382 | */ |
||
| 383 | public function save_post( $post_id, $post = false ) { |
||
| 402 | |||
| 403 | /** |
||
| 404 | * Save data from comment metabox |
||
| 405 | * @since 2.0.9 |
||
| 406 | * @param int $comment_id Comment ID |
||
| 407 | * @return null |
||
| 408 | */ |
||
| 409 | public function save_comment( $comment_id ) { |
||
| 417 | |||
| 418 | /** |
||
| 419 | * Save data from user fields |
||
| 420 | * @since 1.0.x |
||
| 421 | * @param int $user_id User ID |
||
| 422 | * @return null |
||
| 423 | */ |
||
| 424 | public function save_user( $user_id ) { |
||
| 430 | |||
| 431 | /** |
||
| 432 | * Save data from term fields |
||
| 433 | * @since 2.2.0 |
||
| 434 | * @param int $term_id Term ID |
||
| 435 | * @param int $tt_id Term Taxonomy ID |
||
| 436 | * @param string $taxonomy Taxonomy |
||
| 437 | * @return null |
||
| 438 | */ |
||
| 439 | public function save_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
| 447 | |||
| 448 | /** |
||
| 449 | * Delete term meta when a term is deleted. |
||
| 450 | * @since 2.2.0 |
||
| 451 | * @param int $term_id Term ID |
||
| 452 | * @param int $tt_id Term Taxonomy ID |
||
| 453 | * @param string $taxonomy Taxonomy |
||
| 454 | * @return null |
||
| 455 | */ |
||
| 456 | public function delete_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
| 466 | |||
| 467 | /** |
||
| 468 | * Determines if the current object is able to be saved |
||
| 469 | * @since 2.0.9 |
||
| 470 | * @param string $type Current post_type or comment_type |
||
| 471 | * @return bool Whether object can be saved |
||
| 472 | */ |
||
| 473 | public function can_save( $type = '' ) { |
||
| 485 | |||
| 486 | /** |
||
| 487 | * Determine if taxonomy of term being modified is cmb2-editable. |
||
| 488 | * @since 2.2.0 |
||
| 489 | * @param string $taxonomy Taxonomy of term being modified. |
||
| 490 | * @return bool Whether taxonomy is editable. |
||
| 491 | */ |
||
| 492 | 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 | 1 | * @since 2.0.0 |
|
| 530 | 1 | */ |
|
| 531 | public static function enqueue_cmb_css() { |
||
| 539 | |||
| 540 | /** |
||
| 541 | * Includes CMB2 JS |
||
| 542 | 1 | * @since 2.0.0 |
|
| 543 | 1 | */ |
|
| 544 | 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.