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 ) { |
||
| 50 | $this->cmb = $cmb; |
||
| 51 | $this->object_type = $this->cmb->mb_object_type(); |
||
| 52 | } |
||
| 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() { |
||
| 87 | add_action( 'add_meta_boxes_comment', array( $this, 'add_metaboxes' ) ); |
||
| 88 | add_action( 'edit_comment', array( $this, 'save_comment' ) ); |
||
| 89 | } |
||
| 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 | 1 | protected static function register_styles() { |
|
| 143 | 1 | if ( self::$css_registration_done ) { |
|
| 144 | return; |
||
| 145 | } |
||
| 146 | |||
| 147 | // Only use minified files if SCRIPT_DEBUG is off |
||
| 148 | 1 | $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
|
| 149 | 1 | $front = is_admin() ? '' : '-front'; |
|
| 150 | 1 | $rtl = is_rtl() ? '-rtl' : ''; |
|
| 151 | |||
| 152 | // Filter required styles and register stylesheet |
||
| 153 | 1 | $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 | |||
| 156 | 1 | self::$css_registration_done = true; |
|
| 157 | 1 | } |
|
| 158 | |||
| 159 | /** |
||
| 160 | * Registers scripts for CMB2 |
||
| 161 | * @since 2.0.7 |
||
| 162 | */ |
||
| 163 | 1 | protected static function register_js() { |
|
| 164 | 1 | if ( self::$js_registration_done ) { |
|
| 165 | return; |
||
| 166 | } |
||
| 167 | |||
| 168 | 1 | $hook = is_admin() ? 'admin_footer' : 'wp_footer'; |
|
| 169 | 1 | add_action( $hook, array( 'CMB2_JS', 'enqueue' ), 8 ); |
|
| 170 | |||
| 171 | 1 | self::$js_registration_done = true; |
|
| 172 | 1 | } |
|
| 173 | |||
| 174 | /** |
||
| 175 | * Registers scripts and styles for CMB2 |
||
| 176 | * @since 1.0.0 |
||
| 177 | */ |
||
| 178 | public static function register_scripts() { |
||
| 179 | self::register_styles(); |
||
| 180 | self::register_js(); |
||
| 181 | } |
||
| 182 | |||
| 183 | /** |
||
| 184 | * Enqueues scripts and styles for CMB2 in admin_head. |
||
| 185 | * @since 1.0.0 |
||
| 186 | */ |
||
| 187 | public function do_scripts( $hook ) { |
||
| 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 ) { |
||
| 250 | $classes[] = 'closed'; |
||
| 251 | return $classes; |
||
| 252 | } |
||
| 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() { |
||
| 280 | $this->show_form_for_type( 'user' ); |
||
| 281 | } |
||
| 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 ) { |
||
| 298 | if ( $type != $this->cmb->mb_object_type() ) { |
||
| 299 | return; |
||
| 300 | } |
||
| 301 | |||
| 302 | if ( ! $this->show_on() ) { |
||
| 303 | return; |
||
| 304 | } |
||
| 305 | |||
| 306 | if ( $this->cmb->prop( 'cmb_styles' ) ) { |
||
| 307 | self::enqueue_cmb_css(); |
||
| 308 | } |
||
| 309 | if ( $this->cmb->prop( 'enqueue_js' ) ) { |
||
| 310 | self::enqueue_cmb_js(); |
||
| 311 | } |
||
| 312 | |||
| 313 | $this->cmb->show_form( 0, $type ); |
||
| 314 | } |
||
| 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() { |
||
| 322 | // If metabox is requesting to be conditionally shown |
||
| 323 | $show = $this->cmb->should_show(); |
||
| 324 | |||
| 325 | /** |
||
| 326 | * Filter to determine if metabox should show. Default is true |
||
| 327 | * |
||
| 328 | * @param array $show Default is true, show the metabox |
||
| 329 | * @param mixed $meta_box_args Array of the metabox arguments |
||
| 330 | * @param mixed $cmb The CMB2 instance |
||
| 331 | */ |
||
| 332 | $show = (bool) apply_filters( 'cmb2_show_on', $show, $this->cmb->meta_box, $this->cmb ); |
||
| 333 | |||
| 334 | return $show; |
||
| 335 | } |
||
| 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 ) { |
||
| 344 | $priority = $this->cmb->prop( 'priority' ); |
||
| 345 | |||
| 346 | if ( ! is_numeric( $priority ) ) { |
||
| 347 | switch ( $priority ) { |
||
| 348 | |||
| 349 | case 'high': |
||
| 350 | $priority = 5; |
||
| 351 | break; |
||
| 352 | |||
| 353 | case 'low': |
||
| 354 | $priority = 20; |
||
| 355 | break; |
||
| 356 | |||
| 357 | default: |
||
| 358 | $priority = $default; |
||
| 359 | break; |
||
| 360 | } |
||
| 361 | } |
||
| 362 | |||
| 363 | return $priority; |
||
| 364 | } |
||
| 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 ) { |
||
| 374 | |||
| 375 | $post_type = $post ? $post->post_type : get_post_type( $post_id ); |
||
| 376 | |||
| 377 | $do_not_pass_go = ( |
||
| 378 | ! $this->can_save( $post_type ) |
||
| 379 | // check user editing permissions |
||
| 380 | || ( 'page' == $post_type && ! current_user_can( 'edit_page', $post_id ) ) |
||
| 381 | || ! current_user_can( 'edit_post', $post_id ) |
||
| 382 | ); |
||
| 383 | |||
| 384 | if ( $do_not_pass_go ) { |
||
| 385 | // do not collect $200 |
||
| 386 | return; |
||
| 387 | } |
||
| 388 | |||
| 389 | // take a trip to reading railroad – if you pass go collect $200 |
||
| 390 | $this->cmb->save_fields( $post_id, 'post', $_POST ); |
||
| 391 | } |
||
| 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 ) { |
||
| 400 | |||
| 401 | $can_edit = current_user_can( 'moderate_comments', $comment_id ); |
||
| 402 | |||
| 403 | if ( $this->can_save( get_comment_type( $comment_id ) ) && $can_edit ) { |
||
| 404 | $this->cmb->save_fields( $comment_id, 'comment', $_POST ); |
||
| 405 | } |
||
| 406 | } |
||
| 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 ) { |
||
| 415 | // check permissions |
||
| 416 | if ( $this->can_save( 'user' ) ) { |
||
| 417 | $this->cmb->save_fields( $user_id, 'user', $_POST ); |
||
| 418 | } |
||
| 419 | } |
||
| 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 = '' ) { |
||
| 430 | $taxonomy = $taxonomy ? $taxonomy : $tt_id; |
||
| 431 | |||
| 432 | // check permissions |
||
| 433 | if ( $this->taxonomy_can_save( $taxonomy ) && $this->can_save( 'term' ) ) { |
||
| 434 | $this->cmb->save_fields( $term_id, 'term', $_POST ); |
||
| 435 | } |
||
| 436 | } |
||
| 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 = '' ) { |
||
| 447 | if ( $this->taxonomy_can_save( $taxonomy ) ) { |
||
| 448 | |||
| 449 | foreach ( $this->cmb->prop( 'fields' ) as $field ) { |
||
| 450 | $data_to_delete[ $field['id'] ] = ''; |
||
| 451 | } |
||
| 452 | |||
| 453 | $this->cmb->save_fields( $term_id, 'term', $data_to_delete ); |
||
| 454 | } |
||
| 455 | } |
||
| 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 = '' ) { |
||
| 464 | return ( |
||
| 465 | $this->cmb->prop( 'save_fields' ) |
||
| 466 | // check nonce |
||
| 467 | && isset( $_POST[ $this->cmb->nonce() ] ) |
||
| 468 | && wp_verify_nonce( $_POST[ $this->cmb->nonce() ], $this->cmb->nonce() ) |
||
| 469 | // check if autosave |
||
| 470 | && ! ( defined( 'DOING_AUTOSAVE' ) && DOING_AUTOSAVE ) |
||
| 471 | // get the metabox types & compare it to this type |
||
| 472 | && ( $type && in_array( $type, $this->cmb->prop( 'object_types' ) ) ) |
||
| 473 | ); |
||
| 474 | } |
||
| 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 | 1 | public static function enqueue_cmb_css() { |
|
| 501 | 1 | if ( ! apply_filters( 'cmb2_enqueue_css', true ) ) { |
|
| 502 | return false; |
||
| 503 | } |
||
| 504 | |||
| 505 | 1 | self::register_styles(); |
|
| 506 | 1 | return wp_enqueue_style( 'cmb2-styles' ); |
|
| 507 | } |
||
| 508 | |||
| 509 | /** |
||
| 510 | * Includes CMB2 JS |
||
| 511 | * @since 2.0.0 |
||
| 512 | */ |
||
| 513 | 1 | public static function enqueue_cmb_js() { |
|
| 514 | 1 | if ( ! apply_filters( 'cmb2_enqueue_js', true ) ) { |
|
| 515 | return false; |
||
| 516 | } |
||
| 517 | |||
| 518 | 1 | self::register_js(); |
|
| 519 | 1 | return true; |
|
| 520 | } |
||
| 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.