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 | // Handle oembed Ajax |
||
| 56 | $this->once( 'wp_ajax_cmb2_oembed_handler', array( cmb2_ajax(), 'oembed_handler' ) ); |
||
| 57 | $this->once( 'wp_ajax_nopriv_cmb2_oembed_handler', array( cmb2_ajax(), 'oembed_handler' ) ); |
||
| 58 | |||
| 59 | foreach ( get_class_methods( 'CMB2_Show_Filters' ) as $filter ) { |
||
| 60 | add_filter( 'cmb2_show_on', array( 'CMB2_Show_Filters', $filter ), 10, 3 ); |
||
| 61 | } |
||
| 62 | |||
| 63 | if ( is_admin() ) { |
||
| 64 | // register our scripts and styles for cmb |
||
| 65 | $this->once( 'admin_enqueue_scripts', array( __CLASS__, 'register_scripts' ), 8 ); |
||
| 66 | |||
| 67 | switch ( $this->object_type ) { |
||
| 68 | case 'post': |
||
| 69 | return $this->post_hooks(); |
||
| 70 | case 'comment': |
||
| 71 | return $this->comment_hooks(); |
||
| 72 | case 'user': |
||
| 73 | return $this->user_hooks(); |
||
| 74 | case 'term': |
||
| 75 | return $this->term_hooks(); |
||
| 76 | } |
||
| 77 | |||
| 78 | } |
||
| 79 | } |
||
| 80 | |||
| 81 | public function post_hooks() { |
||
| 82 | add_action( 'add_meta_boxes', array( $this, 'add_metaboxes' ) ); |
||
| 83 | add_action( 'add_attachment', array( $this, 'save_post' ) ); |
||
| 84 | add_action( 'edit_attachment', array( $this, 'save_post' ) ); |
||
| 85 | add_action( 'save_post', array( $this, 'save_post' ), 10, 2 ); |
||
| 86 | |||
| 87 | $this->once( 'admin_enqueue_scripts', array( $this, 'do_scripts' ) ); |
||
| 88 | } |
||
| 89 | |||
| 90 | public function comment_hooks() { |
||
| 91 | add_action( 'add_meta_boxes_comment', array( $this, 'add_metaboxes' ) ); |
||
| 92 | add_action( 'edit_comment', array( $this, 'save_comment' ) ); |
||
| 93 | |||
| 94 | $this->once( 'admin_enqueue_scripts', array( $this, 'do_scripts' ) ); |
||
| 95 | } |
||
| 96 | |||
| 97 | public function user_hooks() { |
||
| 98 | $priority = $this->get_priority(); |
||
| 99 | |||
| 100 | add_action( 'show_user_profile', array( $this, 'user_metabox' ), $priority ); |
||
| 101 | add_action( 'edit_user_profile', array( $this, 'user_metabox' ), $priority ); |
||
| 102 | add_action( 'user_new_form', array( $this, 'user_new_metabox' ), $priority ); |
||
| 103 | |||
| 104 | add_action( 'personal_options_update', array( $this, 'save_user' ) ); |
||
| 105 | add_action( 'edit_user_profile_update', array( $this, 'save_user' ) ); |
||
| 106 | add_action( 'user_register', array( $this, 'save_user' ) ); |
||
| 107 | } |
||
| 108 | |||
| 109 | public function term_hooks() { |
||
| 110 | if ( ! function_exists( 'get_term_meta' ) ) { |
||
| 111 | wp_die( __( 'Term Metadata is a WordPress > 4.4 feature. Please upgrade your WordPress install.', 'cmb2' ) ); |
||
| 112 | } |
||
| 113 | |||
| 114 | if ( ! $this->cmb->prop( 'taxonomies' ) ) { |
||
| 115 | wp_die( __( 'Term metaboxes configuration requires a \'taxonomies\' parameter', 'cmb2' ) ); |
||
| 116 | } |
||
| 117 | |||
| 118 | $this->taxonomies = (array) $this->cmb->prop( 'taxonomies' ); |
||
| 119 | $show_on_term_add = $this->cmb->prop( 'new_term_section' ); |
||
| 120 | $priority = $this->get_priority( 8 ); |
||
| 121 | |||
| 122 | foreach ( $this->taxonomies as $taxonomy ) { |
||
| 123 | // Display our form data |
||
| 124 | add_action( "{$taxonomy}_edit_form", array( $this, 'term_metabox' ), $priority, 2 ); |
||
| 125 | |||
| 126 | $show_on_add = is_array( $show_on_term_add ) |
||
| 127 | ? in_array( $taxonomy, $show_on_term_add ) |
||
| 128 | : (bool) $show_on_term_add; |
||
| 129 | |||
| 130 | $show_on_add = apply_filters( "cmb2_show_on_term_add_form_{$this->cmb->cmb_id}", $show_on_add, $this->cmb ); |
||
| 131 | |||
| 132 | // Display form in add-new section (unless specified not to) |
||
| 133 | if ( $show_on_add ) { |
||
| 134 | add_action( "{$taxonomy}_add_form_fields", array( $this, 'term_metabox' ), $priority, 2 ); |
||
| 135 | } |
||
| 136 | |||
| 137 | } |
||
| 138 | |||
| 139 | add_action( 'created_term', array( $this, 'save_term' ), 10, 3 ); |
||
| 140 | add_action( 'edited_terms', array( $this, 'save_term' ), 10, 2 ); |
||
| 141 | |||
| 142 | add_action( 'delete_term', array( $this, 'delete_term' ), 10, 3 ); |
||
| 143 | } |
||
| 144 | |||
| 145 | /** |
||
| 146 | * Registers styles for CMB2 |
||
| 147 | * @since 2.0.7 |
||
| 148 | */ |
||
| 149 | 1 | protected static function register_styles() { |
|
| 150 | 1 | if ( self::$css_registration_done ) { |
|
| 151 | return; |
||
| 152 | } |
||
| 153 | |||
| 154 | // Only use minified files if SCRIPT_DEBUG is off |
||
| 155 | 1 | $min = defined( 'SCRIPT_DEBUG' ) && SCRIPT_DEBUG ? '' : '.min'; |
|
| 156 | 1 | $front = is_admin() ? '' : '-front'; |
|
| 157 | 1 | $rtl = is_rtl() ? '-rtl' : ''; |
|
| 158 | |||
| 159 | // Filter required styles and register stylesheet |
||
| 160 | 1 | $styles = apply_filters( 'cmb2_style_dependencies', array() ); |
|
| 161 | 1 | wp_register_style( 'cmb2-styles', cmb2_utils()->url( "css/cmb2{$front}{$rtl}{$min}.css" ), $styles ); |
|
| 162 | |||
| 163 | 1 | self::$css_registration_done = true; |
|
| 164 | 1 | } |
|
| 165 | |||
| 166 | /** |
||
| 167 | * Registers scripts for CMB2 |
||
| 168 | * @since 2.0.7 |
||
| 169 | */ |
||
| 170 | 1 | protected static function register_js() { |
|
| 171 | 1 | if ( self::$js_registration_done ) { |
|
| 172 | return; |
||
| 173 | } |
||
| 174 | |||
| 175 | 1 | $hook = is_admin() ? 'admin_footer' : 'wp_footer'; |
|
| 176 | 1 | add_action( $hook, array( 'CMB2_JS', 'enqueue' ), 8 ); |
|
| 177 | |||
| 178 | 1 | self::$js_registration_done = true; |
|
| 179 | 1 | } |
|
| 180 | |||
| 181 | /** |
||
| 182 | * Registers scripts and styles for CMB2 |
||
| 183 | * @since 1.0.0 |
||
| 184 | */ |
||
| 185 | public static function register_scripts() { |
||
| 186 | self::register_styles(); |
||
| 187 | self::register_js(); |
||
| 188 | } |
||
| 189 | |||
| 190 | /** |
||
| 191 | * Enqueues scripts and styles for CMB2 |
||
| 192 | * @since 1.0.0 |
||
| 193 | */ |
||
| 194 | public function do_scripts( $hook ) { |
||
| 195 | // only enqueue our scripts/styles on the proper pages |
||
| 196 | if ( in_array( $hook, array( 'post.php', 'post-new.php', 'page-new.php', 'page.php', 'comment.php' ), true ) ) { |
||
| 197 | if ( $this->cmb->prop( 'cmb_styles' ) ) { |
||
| 198 | self::enqueue_cmb_css(); |
||
| 199 | } |
||
| 200 | if ( $this->cmb->prop( 'enqueue_js' ) ) { |
||
| 201 | self::enqueue_cmb_js(); |
||
| 202 | } |
||
| 203 | } |
||
| 204 | } |
||
| 205 | |||
| 206 | /** |
||
| 207 | * Add metaboxes (to 'post' or 'comment' object types) |
||
| 208 | * @since 1.0.0 |
||
| 209 | */ |
||
| 210 | public function add_metaboxes() { |
||
| 211 | |||
| 212 | if ( ! $this->show_on() ) { |
||
| 213 | return; |
||
| 214 | } |
||
| 215 | |||
| 216 | foreach ( $this->cmb->prop( 'object_types' ) as $post_type ) { |
||
| 217 | /** |
||
| 218 | * To keep from registering an actual post-screen metabox, |
||
| 219 | * omit the 'title' attribute from the metabox registration array. |
||
| 220 | * |
||
| 221 | * (WordPress will not display metaboxes without titles anyway) |
||
| 222 | * |
||
| 223 | * This is a good solution if you want to output your metaboxes |
||
| 224 | * Somewhere else in the post-screen |
||
| 225 | */ |
||
| 226 | if ( $this->cmb->prop( 'title' ) ) { |
||
| 227 | |||
| 228 | if ( $this->cmb->prop( 'closed' ) ) { |
||
| 229 | add_filter( "postbox_classes_{$post_type}_{$this->cmb->cmb_id}", array( $this, 'close_metabox_class' ) ); |
||
| 230 | } |
||
| 231 | |||
| 232 | 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' ) ); |
||
| 233 | } |
||
| 234 | } |
||
| 235 | } |
||
| 236 | |||
| 237 | /** |
||
| 238 | * Add 'closed' class to metabox |
||
| 239 | * @since 2.0.0 |
||
| 240 | * @param array $classes Array of classes |
||
| 241 | * @return array Modified array of classes |
||
| 242 | */ |
||
| 243 | public function close_metabox_class( $classes ) { |
||
| 244 | $classes[] = 'closed'; |
||
| 245 | return $classes; |
||
| 246 | } |
||
| 247 | |||
| 248 | /** |
||
| 249 | * Display metaboxes for a post or comment object |
||
| 250 | * @since 1.0.0 |
||
| 251 | */ |
||
| 252 | public function metabox_callback() { |
||
| 253 | $object_id = 'comment' == $this->object_type ? get_comment_ID() : get_the_ID(); |
||
| 254 | $this->cmb->show_form( $object_id, $this->object_type ); |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * Display metaboxes for new user page |
||
| 259 | * @since 1.0.0 |
||
| 260 | */ |
||
| 261 | public function user_new_metabox( $section ) { |
||
| 262 | if ( $section == $this->cmb->prop( 'new_user_section' ) ) { |
||
| 263 | $object_id = $this->cmb->object_id(); |
||
| 264 | $this->cmb->object_id( isset( $_REQUEST['user_id'] ) ? $_REQUEST['user_id'] : $object_id ); |
||
| 265 | $this->user_metabox(); |
||
| 266 | } |
||
| 267 | } |
||
| 268 | |||
| 269 | /** |
||
| 270 | * Display metaboxes for a user object |
||
| 271 | * @since 1.0.0 |
||
| 272 | */ |
||
| 273 | public function user_metabox() { |
||
| 274 | $this->show_form_for_type( 'user' ); |
||
| 275 | } |
||
| 276 | |||
| 277 | /** |
||
| 278 | * Display metaboxes for a taxonomy term object |
||
| 279 | * @since 2.2.0 |
||
| 280 | */ |
||
| 281 | public function term_metabox() { |
||
| 282 | $this->show_form_for_type( 'term' ); |
||
| 283 | } |
||
| 284 | |||
| 285 | /** |
||
| 286 | * Display metaboxes for an object type |
||
| 287 | * @since 2.2.0 |
||
| 288 | * @param string $type Object type |
||
| 289 | * @return void |
||
| 290 | */ |
||
| 291 | public function show_form_for_type( $type ) { |
||
| 292 | if ( $type != $this->cmb->mb_object_type() ) { |
||
| 293 | return; |
||
| 294 | } |
||
| 295 | |||
| 296 | if ( ! $this->show_on() ) { |
||
| 297 | return; |
||
| 298 | } |
||
| 299 | |||
| 300 | if ( $this->cmb->prop( 'cmb_styles' ) ) { |
||
| 301 | self::enqueue_cmb_css(); |
||
| 302 | } |
||
| 303 | if ( $this->cmb->prop( 'enqueue_js' ) ) { |
||
| 304 | self::enqueue_cmb_js(); |
||
| 305 | } |
||
| 306 | |||
| 307 | $this->cmb->show_form( 0, $type ); |
||
| 308 | } |
||
| 309 | |||
| 310 | /** |
||
| 311 | * Determines if metabox should be shown in current context |
||
| 312 | * @since 2.0.0 |
||
| 313 | * @return bool Whether metabox should be added/shown |
||
| 314 | */ |
||
| 315 | public function show_on() { |
||
| 316 | // If metabox is requesting to be conditionally shown |
||
| 317 | $show = $this->cmb->should_show(); |
||
| 318 | |||
| 319 | /** |
||
| 320 | * Filter to determine if metabox should show. Default is true |
||
| 321 | * |
||
| 322 | * @param array $show Default is true, show the metabox |
||
| 323 | * @param mixed $meta_box_args Array of the metabox arguments |
||
| 324 | * @param mixed $cmb The CMB2 instance |
||
| 325 | */ |
||
| 326 | $show = (bool) apply_filters( 'cmb2_show_on', $show, $this->cmb->meta_box, $this->cmb ); |
||
| 327 | |||
| 328 | return $show; |
||
| 329 | } |
||
| 330 | |||
| 331 | /** |
||
| 332 | * Get the CMB priority property set to numeric hook priority. |
||
| 333 | * @since 2.2.0 |
||
| 334 | * @param integer $default Default display hook priority. |
||
| 335 | * @return integer Hook priority. |
||
| 336 | */ |
||
| 337 | public function get_priority( $default = 10 ) { |
||
| 338 | $priority = $this->cmb->prop( 'priority' ); |
||
| 339 | |||
| 340 | if ( ! is_numeric( $priority ) ) { |
||
| 341 | switch ( $priority ) { |
||
| 342 | |||
| 343 | case 'high': |
||
| 344 | $priority = 5; |
||
| 345 | break; |
||
| 346 | |||
| 347 | case 'low': |
||
| 348 | $priority = 20; |
||
| 349 | break; |
||
| 350 | |||
| 351 | default: |
||
| 352 | $priority = $default; |
||
| 353 | break; |
||
| 354 | } |
||
| 355 | } |
||
| 356 | |||
| 357 | return $priority; |
||
| 358 | } |
||
| 359 | |||
| 360 | /** |
||
| 361 | * Save data from post metabox |
||
| 362 | * @since 1.0.0 |
||
| 363 | * @param int $post_id Post ID |
||
| 364 | * @param mixed $post Post object |
||
| 365 | * @return null |
||
| 366 | */ |
||
| 367 | public function save_post( $post_id, $post = false ) { |
||
| 368 | |||
| 369 | $post_type = $post ? $post->post_type : get_post_type( $post_id ); |
||
| 370 | |||
| 371 | $do_not_pass_go = ( |
||
| 372 | ! $this->can_save( $post_type ) |
||
| 373 | // check user editing permissions |
||
| 374 | || ( 'page' == $post_type && ! current_user_can( 'edit_page', $post_id ) ) |
||
| 375 | || ! current_user_can( 'edit_post', $post_id ) |
||
| 376 | ); |
||
| 377 | |||
| 378 | if ( $do_not_pass_go ) { |
||
| 379 | // do not collect $200 |
||
| 380 | return; |
||
| 381 | } |
||
| 382 | |||
| 383 | // take a trip to reading railroad – if you pass go collect $200 |
||
| 384 | $this->cmb->save_fields( $post_id, 'post', $_POST ); |
||
| 385 | } |
||
| 386 | |||
| 387 | /** |
||
| 388 | * Save data from comment metabox |
||
| 389 | * @since 2.0.9 |
||
| 390 | * @param int $comment_id Comment ID |
||
| 391 | * @return null |
||
| 392 | */ |
||
| 393 | public function save_comment( $comment_id ) { |
||
| 394 | |||
| 395 | $can_edit = current_user_can( 'moderate_comments', $comment_id ); |
||
| 396 | |||
| 397 | if ( $this->can_save( get_comment_type( $comment_id ) ) && $can_edit ) { |
||
| 398 | $this->cmb->save_fields( $comment_id, 'comment', $_POST ); |
||
| 399 | } |
||
| 400 | } |
||
| 401 | |||
| 402 | /** |
||
| 403 | * Save data from user fields |
||
| 404 | * @since 1.0.x |
||
| 405 | * @param int $user_id User ID |
||
| 406 | * @return null |
||
| 407 | */ |
||
| 408 | public function save_user( $user_id ) { |
||
| 409 | // check permissions |
||
| 410 | if ( $this->can_save( 'user' ) ) { |
||
| 411 | $this->cmb->save_fields( $user_id, 'user', $_POST ); |
||
| 412 | } |
||
| 413 | } |
||
| 414 | |||
| 415 | /** |
||
| 416 | * Save data from term fields |
||
| 417 | * @since 1.0.x |
||
| 418 | * @param int $term_id Term ID |
||
| 419 | * @param int $tt_id Term Taxonomy ID |
||
| 420 | * @param string $taxonomy Taxonomy |
||
| 421 | * @return null |
||
| 422 | */ |
||
| 423 | public function save_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
| 431 | |||
| 432 | /** |
||
| 433 | * Delete term meta when a term is deleted. |
||
| 434 | * @since 1.0.x |
||
| 435 | * @param int $term_id Term ID |
||
| 436 | * @param int $tt_id Term Taxonomy ID |
||
| 437 | * @param string $taxonomy Taxonomy |
||
| 438 | * @return null |
||
| 439 | */ |
||
| 440 | public function delete_term( $term_id, $tt_id, $taxonomy = '' ) { |
||
| 441 | if ( $this->taxonomy_can_save( $taxonomy ) ) { |
||
| 442 | |||
| 443 | foreach ( $this->cmb->prop( 'fields' ) as $field ) { |
||
| 444 | $data_to_delete[ $field['id'] ] = ''; |
||
| 445 | } |
||
| 446 | |||
| 447 | $this->cmb->save_fields( $term_id, 'term', $data_to_delete ); |
||
| 448 | } |
||
| 449 | } |
||
| 450 | |||
| 451 | /** |
||
| 452 | * Determines if the current object is able to be saved |
||
| 453 | * @since 2.0.9 |
||
| 454 | * @param string $type Current post_type or comment_type |
||
| 455 | * @return bool Whether object can be saved |
||
| 456 | */ |
||
| 457 | public function can_save( $type = '' ) { |
||
| 469 | |||
| 470 | /** |
||
| 471 | * Determine if taxonomy of term being modified is cmb2-editable. |
||
| 472 | * @since 2.2.0 |
||
| 473 | * @param string $taxonomy Taxonomy of term being modified. |
||
| 474 | * @return bool Whether taxonomy is editable. |
||
| 475 | */ |
||
| 476 | public function taxonomy_can_save( $taxonomy ) { |
||
| 491 | |||
| 492 | /** |
||
| 493 | * Includes CMB2 styles |
||
| 494 | * @since 2.0.0 |
||
| 495 | */ |
||
| 496 | 1 | public static function enqueue_cmb_css() { |
|
| 504 | |||
| 505 | /** |
||
| 506 | * Includes CMB2 JS |
||
| 507 | * @since 2.0.0 |
||
| 508 | */ |
||
| 509 | 1 | public static function enqueue_cmb_js() { |
|
| 517 | |||
| 518 | } |
||
| 519 |
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.